blob: bdf1229b0cb527fb03dea6de12ab154c10870978 [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"
Chris Craik710f46d2012-09-17 17:25:49 -070036#include "PathRenderer.h"
Romain Guya957eea2010-12-08 18:34:42 -080037#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070038
39namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070040namespace uirenderer {
41
42///////////////////////////////////////////////////////////////////////////////
43// Defines
44///////////////////////////////////////////////////////////////////////////////
45
Romain Guy759ea802010-09-16 20:49:46 -070046#define RAD_TO_DEG (180.0f / 3.14159265f)
47#define MIN_ANGLE 0.001f
48
Romain Guyf8773082012-07-12 18:01:00 -070049#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070050
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 Guy49c5fc02012-05-15 11:10:01 -0700143bool OpenGLRenderer::isDeferred() {
144 return false;
145}
146
Romain Guy85bf02f2010-06-22 13:11:24 -0700147void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700148 initViewport(width, height);
149
150 glDisable(GL_DITHER);
151 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
152
153 glEnableVertexAttribArray(Program::kBindingPosition);
154}
155
156void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700157 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700158
159 mWidth = width;
160 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700161
162 mFirstSnapshot->height = height;
163 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700164}
165
Chet Haase44b2fe32012-06-06 19:03:58 -0700166int OpenGLRenderer::prepare(bool opaque) {
167 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800168}
169
Chet Haase44b2fe32012-06-06 19:03:58 -0700170int OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800171 mCaches.clearGarbage();
172
Romain Guy8aef54f2010-09-01 15:13:49 -0700173 mSnapshot = new Snapshot(mFirstSnapshot,
174 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800175 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700176 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700177
Romain Guy7d7b5492011-01-24 16:33:45 -0800178 mSnapshot->setClip(left, top, right, bottom);
Romain Guy57b52682012-09-20 17:38:46 -0700179 mDirtyClip = opaque;
Romain Guyddf74372012-05-22 14:07:07 -0700180
Romain Guy11cb6422012-09-21 00:39:43 -0700181 updateLayers();
182
Romain Guy45e4c3d2012-09-11 17:17:07 -0700183 // If we know that we are going to redraw the entire framebuffer,
184 // perform a discard to let the driver know we don't need to preserve
185 // the back buffer for this frame.
186 if (mCaches.extensions.hasDiscardFramebuffer() &&
187 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
188 const GLenum attachments[] = { getTargetFbo() == 0 ? GL_COLOR_EXT : GL_COLOR_ATTACHMENT0 };
189 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
190 }
191
Romain Guyddf74372012-05-22 14:07:07 -0700192 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800193
Romain Guy2b7028e2012-09-19 17:25:38 -0700194 mTilingSnapshot = mSnapshot;
Romain Guy57b52682012-09-20 17:38:46 -0700195 startTiling(mTilingSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700196
Romain Guy6b7bd242010-10-06 19:49:23 -0700197 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700198 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700199 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700200 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700201 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700202 } else {
203 mCaches.resetScissor();
204 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700205
206 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700207}
208
209void OpenGLRenderer::syncState() {
210 glViewport(0, 0, mWidth, mHeight);
211
212 if (mCaches.blend) {
213 glEnable(GL_BLEND);
214 } else {
215 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700216 }
Romain Guybb9524b2010-06-22 18:56:38 -0700217}
218
Romain Guy57b52682012-09-20 17:38:46 -0700219void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700220 Rect* clip = mTilingSnapshot->clipRect;
Romain Guy2b7028e2012-09-19 17:25:38 -0700221 if (s->flags & Snapshot::kFlagIsFboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700222 clip = s->clipRect;
223 }
224
225 mCaches.startTiling(clip->left, s->height - clip->bottom,
226 clip->right - clip->left, clip->bottom - clip->top, opaque);
227}
228
229void OpenGLRenderer::endTiling() {
230 mCaches.endTiling();
231}
232
Romain Guyb025b9c2010-09-16 14:16:48 -0700233void OpenGLRenderer::finish() {
Romain Guy2b7028e2012-09-19 17:25:38 -0700234 endTiling();
235
Romain Guy11cb6422012-09-21 00:39:43 -0700236 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700237#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700238 GLenum status = GL_NO_ERROR;
239 while ((status = glGetError()) != GL_NO_ERROR) {
240 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
241 switch (status) {
242 case GL_INVALID_ENUM:
243 ALOGE(" GL_INVALID_ENUM");
244 break;
245 case GL_INVALID_VALUE:
246 ALOGE(" GL_INVALID_VALUE");
247 break;
248 case GL_INVALID_OPERATION:
249 ALOGE(" GL_INVALID_OPERATION");
250 break;
251 case GL_OUT_OF_MEMORY:
252 ALOGE(" Out of memory!");
253 break;
254 }
Romain Guya07105b2011-01-10 21:14:18 -0800255 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700256#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700257
Romain Guyc15008e2010-11-10 11:59:15 -0800258#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800259 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700260#else
261 if (mCaches.getDebugLevel() & kDebugMemory) {
262 mCaches.dumpMemoryUsage();
263 }
Romain Guyc15008e2010-11-10 11:59:15 -0800264#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700265 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700266}
267
Romain Guy6c319ca2011-01-11 14:29:25 -0800268void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700269 if (mCaches.currentProgram) {
270 if (mCaches.currentProgram->isInUse()) {
271 mCaches.currentProgram->remove();
272 mCaches.currentProgram = NULL;
273 }
274 }
Romain Guy50c0f092010-10-19 11:42:22 -0700275 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800276 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800277 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800278 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700279}
280
Romain Guy6c319ca2011-01-11 14:29:25 -0800281void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800282 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800283 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700284 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
285
Romain Guy3e263fa2011-12-12 16:47:48 -0800286 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
287
Chet Haase80250612012-08-15 13:46:54 -0700288 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700289 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800290 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700291 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700292
Romain Guya1d3c912011-12-13 14:55:06 -0800293 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700294
Romain Guy50c0f092010-10-19 11:42:22 -0700295 mCaches.blend = true;
296 glEnable(GL_BLEND);
297 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
298 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700299}
300
Romain Guy35643dd2012-09-18 15:40:58 -0700301void OpenGLRenderer::resumeAfterLayer() {
302 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
303 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
304 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
305
306 mCaches.resetScissor();
307 dirtyClip();
308}
309
Romain Guyba6be8a2012-04-23 18:22:09 -0700310void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700311 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700312}
313
314void OpenGLRenderer::attachFunctor(Functor* functor) {
315 mFunctors.add(functor);
316}
317
Romain Guy8f3b8e32012-03-27 16:33:45 -0700318status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
319 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700320 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700321
Romain Guyba6be8a2012-04-23 18:22:09 -0700322 if (count > 0) {
323 SortedVector<Functor*> functors(mFunctors);
324 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700325
Romain Guyba6be8a2012-04-23 18:22:09 -0700326 DrawGlInfo info;
327 info.clipLeft = 0;
328 info.clipTop = 0;
329 info.clipRight = 0;
330 info.clipBottom = 0;
331 info.isLayer = false;
332 info.width = 0;
333 info.height = 0;
334 memset(info.transform, 0, sizeof(float) * 16);
335
336 for (size_t i = 0; i < count; i++) {
337 Functor* f = functors.itemAt(i);
338 result |= (*f)(DrawGlInfo::kModeProcess, &info);
339
Chris Craikc2c95432012-04-25 15:13:52 -0700340 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700341 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
342 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700343 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700344
Chris Craikc2c95432012-04-25 15:13:52 -0700345 if (result & DrawGlInfo::kStatusInvoke) {
346 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700347 }
348 }
Chet Haasebeb8bd02012-09-09 16:13:26 -0700349 // protect against functors binding to other buffers
350 mCaches.unbindMeshBuffer();
351 mCaches.unbindIndicesBuffer();
352 mCaches.activeTexture(0);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700353 }
354
355 return result;
356}
357
358status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800359 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700360 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700361
Romain Guy8a4ac612012-07-17 17:32:48 -0700362 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800363 if (mDirtyClip) {
364 setScissorFromClip();
365 }
Romain Guyd643bb52011-03-01 14:55:21 -0800366
Romain Guy80911b82011-03-16 15:30:12 -0700367 Rect clip(*mSnapshot->clipRect);
368 clip.snapToPixelBoundaries();
369
Romain Guyd643bb52011-03-01 14:55:21 -0800370 // Since we don't know what the functor will draw, let's dirty
371 // tne entire clip region
372 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800373 dirtyLayerUnchecked(clip, getRegion());
374 }
Romain Guyd643bb52011-03-01 14:55:21 -0800375
Romain Guy08aa2cb2011-03-17 11:06:57 -0700376 DrawGlInfo info;
377 info.clipLeft = clip.left;
378 info.clipTop = clip.top;
379 info.clipRight = clip.right;
380 info.clipBottom = clip.bottom;
381 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700382 info.width = getSnapshot()->viewport.getWidth();
383 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700384 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700385
Chet Haase48659092012-05-31 15:21:51 -0700386 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800387
Romain Guy8f3b8e32012-03-27 16:33:45 -0700388 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700389 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800390 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700391
Chris Craik65924a32012-04-05 17:52:11 -0700392 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700393 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700394 }
Romain Guycabfcc12011-03-07 18:06:46 -0800395 }
396
Chet Haasedaf98e92011-01-10 14:10:36 -0800397 resume();
Romain Guy65549432012-03-26 16:45:05 -0700398 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800399}
400
Romain Guyf6a11b82010-06-23 17:47:49 -0700401///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700402// Layers
403///////////////////////////////////////////////////////////////////////////////
404
405bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
406 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
407 OpenGLRenderer* renderer = layer->renderer;
408 Rect& dirty = layer->dirtyRect;
409
410 if (inFrame) endTiling();
411
412 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
413 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
414 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
415 renderer->finish();
416
417 if (inFrame) {
418 resumeAfterLayer();
419 startTiling(mSnapshot);
420 }
421
422 dirty.setEmpty();
423 layer->deferredUpdateScheduled = false;
424 layer->renderer = NULL;
425 layer->displayList = NULL;
426
427 return true;
428 }
429
430 return false;
431}
432
433void OpenGLRenderer::updateLayers() {
434 int count = mLayerUpdates.size();
435 if (count > 0) {
436 startMark("Layer Updates");
437
438 // Note: it is very important to update the layers in reverse order
439 for (int i = count - 1; i >= 0; i--) {
440 Layer* layer = mLayerUpdates.itemAt(i);
441 updateLayer(layer, false);
442 mCaches.resourceCache.decrementRefcount(layer);
443 }
444 mLayerUpdates.clear();
445
446 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
447 endMark();
448 }
449}
450
451void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
452 if (layer) {
453 mLayerUpdates.push_back(layer);
454 mCaches.resourceCache.incrementRefcount(layer);
455 }
456}
457
458void OpenGLRenderer::clearLayerUpdates() {
459 size_t count = mLayerUpdates.size();
460 if (count > 0) {
461 mCaches.resourceCache.lock();
462 for (size_t i = 0; i < count; i++) {
463 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
464 }
465 mCaches.resourceCache.unlock();
466 mLayerUpdates.clear();
467 }
468}
469
470///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700471// State management
472///////////////////////////////////////////////////////////////////////////////
473
Romain Guybb9524b2010-06-22 18:56:38 -0700474int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700475 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700476}
477
478int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700479 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700480}
481
482void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700483 if (mSaveCount > 1) {
484 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700485 }
Romain Guybb9524b2010-06-22 18:56:38 -0700486}
487
488void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700489 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700490
Romain Guy8fb95422010-08-17 18:38:51 -0700491 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700492 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700493 }
Romain Guybb9524b2010-06-22 18:56:38 -0700494}
495
Romain Guy8aef54f2010-09-01 15:13:49 -0700496int OpenGLRenderer::saveSnapshot(int flags) {
497 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700498 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700499}
500
501bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700502 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700503 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700504 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700505
Romain Guybd6b79b2010-06-26 00:13:53 -0700506 sp<Snapshot> current = mSnapshot;
507 sp<Snapshot> previous = mSnapshot->previous;
508
Romain Guyeb993562010-10-05 18:14:38 -0700509 if (restoreOrtho) {
510 Rect& r = previous->viewport;
511 glViewport(r.left, r.top, r.right, r.bottom);
512 mOrthoMatrix.load(current->orthoMatrix);
513 }
514
Romain Guy8b55f372010-08-18 17:10:07 -0700515 mSaveCount--;
516 mSnapshot = previous;
517
Romain Guy2542d192010-08-18 11:47:12 -0700518 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700519 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700520 }
Romain Guy2542d192010-08-18 11:47:12 -0700521
Romain Guy5ec99242010-11-03 16:19:08 -0700522 if (restoreLayer) {
523 composeLayer(current, previous);
524 }
525
Romain Guy2542d192010-08-18 11:47:12 -0700526 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700527}
528
Romain Guyf6a11b82010-06-23 17:47:49 -0700529///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700530// Layers
531///////////////////////////////////////////////////////////////////////////////
532
533int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700534 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700535 const GLuint previousFbo = mSnapshot->fbo;
536 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700537
Romain Guyaf636eb2010-12-09 17:47:21 -0800538 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700539 int alpha = 255;
540 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700541
Romain Guye45362c2010-11-03 19:58:32 -0700542 if (p) {
543 alpha = p->getAlpha();
Chris Craik710f46d2012-09-17 17:25:49 -0700544 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700545 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700546 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700547 }
Romain Guyd55a8612010-06-28 17:42:46 -0700548
Chet Haased48885a2012-08-28 17:43:28 -0700549 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700550 }
Romain Guyd55a8612010-06-28 17:42:46 -0700551
552 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700553}
554
555int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
556 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700557 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700558 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700559 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700560 SkPaint paint;
561 paint.setAlpha(alpha);
562 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700563 }
Romain Guyd55a8612010-06-28 17:42:46 -0700564}
Romain Guybd6b79b2010-06-26 00:13:53 -0700565
Romain Guy1c740bc2010-09-13 18:00:09 -0700566/**
567 * Layers are viewed by Skia are slightly different than layers in image editing
568 * programs (for instance.) When a layer is created, previously created layers
569 * and the frame buffer still receive every drawing command. For instance, if a
570 * layer is created and a shape intersecting the bounds of the layers and the
571 * framebuffer is draw, the shape will be drawn on both (unless the layer was
572 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
573 *
574 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
575 * texture. Unfortunately, this is inefficient as it requires every primitive to
576 * be drawn n + 1 times, where n is the number of active layers. In practice this
577 * means, for every primitive:
578 * - Switch active frame buffer
579 * - Change viewport, clip and projection matrix
580 * - Issue the drawing
581 *
582 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700583 * To avoid this, layers are implemented in a different way here, at least in the
584 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
585 * is set. When this flag is set we can redirect all drawing operations into a
586 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700587 *
588 * This implementation relies on the frame buffer being at least RGBA 8888. When
589 * a layer is created, only a texture is created, not an FBO. The content of the
590 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700591 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700592 * buffer and drawing continues as normal. This technique therefore treats the
593 * frame buffer as a scratch buffer for the layers.
594 *
595 * To compose the layers back onto the frame buffer, each layer texture
596 * (containing the original frame buffer data) is drawn as a simple quad over
597 * the frame buffer. The trick is that the quad is set as the composition
598 * destination in the blending equation, and the frame buffer becomes the source
599 * of the composition.
600 *
601 * Drawing layers with an alpha value requires an extra step before composition.
602 * An empty quad is drawn over the layer's region in the frame buffer. This quad
603 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
604 * quad is used to multiply the colors in the frame buffer. This is achieved by
605 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
606 * GL_ZERO, GL_SRC_ALPHA.
607 *
608 * Because glCopyTexImage2D() can be slow, an alternative implementation might
609 * be use to draw a single clipped layer. The implementation described above
610 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700611 *
612 * (1) The frame buffer is actually not cleared right away. To allow the GPU
613 * to potentially optimize series of calls to glCopyTexImage2D, the frame
614 * buffer is left untouched until the first drawing operation. Only when
615 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700616 */
Chet Haased48885a2012-08-28 17:43:28 -0700617bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
618 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700619 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700620 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700621
Romain Guyeb993562010-10-05 18:14:38 -0700622 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
623
Romain Guyf607bdc2010-09-10 19:20:06 -0700624 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700625 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700626 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700627 Rect untransformedBounds(bounds);
628 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700629
Chet Haased48885a2012-08-28 17:43:28 -0700630 // Layers only make sense if they are in the framebuffer's bounds
631 if (bounds.intersect(*mSnapshot->clipRect)) {
632 // We cannot work with sub-pixels in this case
633 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700634
Chet Haased48885a2012-08-28 17:43:28 -0700635 // When the layer is not an FBO, we may use glCopyTexImage so we
636 // need to make sure the layer does not extend outside the bounds
637 // of the framebuffer
638 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700639 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700640 } else if (fboLayer) {
641 clip.set(bounds);
642 mat4 inverse;
643 inverse.loadInverse(*mSnapshot->transform);
644 inverse.mapRect(clip);
645 clip.snapToPixelBoundaries();
646 if (clip.intersect(untransformedBounds)) {
647 clip.translate(-left, -top);
648 bounds.set(untransformedBounds);
649 } else {
650 clip.setEmpty();
651 }
Romain Guyad37cd32011-03-15 11:12:25 -0700652 }
Chet Haased48885a2012-08-28 17:43:28 -0700653 } else {
654 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700655 }
Romain Guybf434112010-09-16 14:40:17 -0700656
Romain Guy746b7402010-10-26 16:27:31 -0700657 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700658 bounds.getHeight() > mCaches.maxTextureSize ||
659 (fboLayer && clip.isEmpty())) {
660 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700661 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700662 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700663 }
664
665 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700666 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700667 return false;
668 }
Romain Guyf18fd992010-07-08 11:45:51 -0700669
Romain Guya1d3c912011-12-13 14:55:06 -0800670 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700671 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700672 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700673 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700674 }
675
Romain Guy9ace8f52011-07-07 20:50:11 -0700676 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700677 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700678 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
679 bounds.getWidth() / float(layer->getWidth()), 0.0f);
680 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700681 layer->setBlend(true);
Romain Guydda570202010-07-06 11:39:32 -0700682
Romain Guy8fb95422010-08-17 18:38:51 -0700683 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700684 mSnapshot->flags |= Snapshot::kFlagIsLayer;
685 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700686
Romain Guyeb993562010-10-05 18:14:38 -0700687 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700688 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700689 } else {
690 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700691 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800692 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700693 if (layer->isEmpty()) {
694 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700695 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700696 layer->getWidth(), layer->getHeight(), 0);
697 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800698 } else {
699 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700700 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800701 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700702
Romain Guy54be1cd2011-06-13 19:04:27 -0700703 // Enqueue the buffer coordinates to clear the corresponding region later
704 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700705 }
Romain Guyeb993562010-10-05 18:14:38 -0700706 }
Romain Guyf86ef572010-07-01 11:05:42 -0700707
Romain Guyd55a8612010-06-28 17:42:46 -0700708 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700709}
710
Chet Haased48885a2012-08-28 17:43:28 -0700711bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700712 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700713
Chet Haased48885a2012-08-28 17:43:28 -0700714 mSnapshot->region = &mSnapshot->layer->region;
715 mSnapshot->flags |= Snapshot::kFlagFboTarget;
Romain Guy5b3b3522010-10-27 18:57:51 -0700716
Chet Haased48885a2012-08-28 17:43:28 -0700717 mSnapshot->flags |= Snapshot::kFlagIsFboLayer;
718 mSnapshot->fbo = layer->getFbo();
719 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
720 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
721 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
722 mSnapshot->height = bounds.getHeight();
723 mSnapshot->flags |= Snapshot::kFlagDirtyOrtho;
724 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700725
Romain Guy2b7028e2012-09-19 17:25:38 -0700726 endTiling();
Romain Guy5b3b3522010-10-27 18:57:51 -0700727 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700728 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
729 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700730
731 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700732 if (layer->isEmpty()) {
733 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
734 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700735 }
736
737 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700738 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700739
Romain Guy2b7028e2012-09-19 17:25:38 -0700740 startTiling(mSnapshot);
Romain Guy5b3b3522010-10-27 18:57:51 -0700741
742 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700743 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800744 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700745 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700746 glClear(GL_COLOR_BUFFER_BIT);
747
748 dirtyClip();
749
750 // Change the ortho projection
751 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
752 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
753
754 return true;
755}
756
Romain Guy1c740bc2010-09-13 18:00:09 -0700757/**
758 * Read the documentation of createLayer() before doing anything in this method.
759 */
Romain Guy1d83e192010-08-17 11:37:00 -0700760void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
761 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000762 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700763 return;
764 }
765
Romain Guy5b3b3522010-10-27 18:57:51 -0700766 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700767
768 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700769 endTiling();
770
Romain Guye0aa84b2012-04-03 19:30:26 -0700771 // Detach the texture from the FBO
772 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guyeb993562010-10-05 18:14:38 -0700773 // Unbind current FBO and restore previous one
774 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy2b7028e2012-09-19 17:25:38 -0700775
776 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700777 }
778
Romain Guy1d83e192010-08-17 11:37:00 -0700779 Layer* layer = current->layer;
780 const Rect& rect = layer->layer;
781
Romain Guy9ace8f52011-07-07 20:50:11 -0700782 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700783 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700784 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700785 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700786 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700787 }
788
Romain Guy03750a02010-10-18 14:06:08 -0700789 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700790
Romain Guya1d3c912011-12-13 14:55:06 -0800791 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700792
Romain Guy5b3b3522010-10-27 18:57:51 -0700793 // When the layer is stored in an FBO, we can save a bit of fillrate by
794 // drawing only the dirty region
795 if (fboLayer) {
796 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700797 if (layer->getColorFilter()) {
798 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800799 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700800 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700801 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800802 resetColorFilter();
803 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700804 } else if (!rect.isEmpty()) {
805 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
806 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700807 }
Romain Guy8b55f372010-08-18 17:10:07 -0700808
Romain Guyeb993562010-10-05 18:14:38 -0700809 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800810 // Note: No need to use glDiscardFramebufferEXT() since we never
811 // create/compose layers that are not on screen with this
812 // code path
813 // See LayerRenderer::destroyLayer(Layer*)
814
Romain Guyeb993562010-10-05 18:14:38 -0700815 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
816 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700817 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700818 }
819
Romain Guy746b7402010-10-26 16:27:31 -0700820 dirtyClip();
821
Romain Guyeb993562010-10-05 18:14:38 -0700822 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700823 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700824 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700825 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700826 }
827}
828
Romain Guyaa6c24c2011-04-28 18:40:04 -0700829void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700830 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700831
Romain Guy302a9df2011-08-16 13:55:02 -0700832 mat4& transform = layer->getTransform();
833 if (!transform.isIdentity()) {
834 save(0);
835 mSnapshot->transform->multiply(transform);
836 }
837
Romain Guyaa6c24c2011-04-28 18:40:04 -0700838 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700839 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700840 setupDrawWithTexture();
841 } else {
842 setupDrawWithExternalTexture();
843 }
844 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700845 setupDrawColor(alpha, alpha, alpha, alpha);
846 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700847 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700848 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700849 setupDrawPureColorUniforms();
850 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700851 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
852 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700853 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700854 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700855 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700856 if (mSnapshot->transform->isPureTranslate() &&
857 layer->getWidth() == (uint32_t) rect.getWidth() &&
858 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700859 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 setupDrawModelView(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 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
867 }
868 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700869 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
870
871 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
872
873 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700874
875 if (!transform.isIdentity()) {
876 restore();
877 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700878}
879
Romain Guy5b3b3522010-10-27 18:57:51 -0700880void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700881 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700882 const Rect& texCoords = layer->texCoords;
883 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
884 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700885
Romain Guy9ace8f52011-07-07 20:50:11 -0700886 float x = rect.left;
887 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700888 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700889 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700890 layer->getHeight() == (uint32_t) rect.getHeight();
891
892 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700893 // When we're swapping, the layer is already in screen coordinates
894 if (!swap) {
895 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
896 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
897 }
898
Romain Guyd21b6e12011-11-30 20:21:23 -0800899 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700900 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800901 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700902 }
903
904 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
905 layer->getTexture(), layer->getAlpha() / 255.0f,
906 layer->getMode(), layer->isBlend(),
907 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
908 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700909
Romain Guyaa6c24c2011-04-28 18:40:04 -0700910 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
911 } else {
912 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
913 drawTextureLayer(layer, rect);
914 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
915 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700916}
917
918void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700919 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700920 layer->setRegionAsRect();
921
Romain Guy40667672011-03-18 14:34:03 -0700922 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700923
Romain Guy5b3b3522010-10-27 18:57:51 -0700924 layer->region.clear();
925 return;
926 }
927
Romain Guy8a3957d2011-09-07 17:55:15 -0700928 // TODO: See LayerRenderer.cpp::generateMesh() for important
929 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800930 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700931 size_t count;
932 const android::Rect* rects = layer->region.getArray(&count);
933
Romain Guy9ace8f52011-07-07 20:50:11 -0700934 const float alpha = layer->getAlpha() / 255.0f;
935 const float texX = 1.0f / float(layer->getWidth());
936 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800937 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700938
939 TextureVertex* mesh = mCaches.getRegionMesh();
940 GLsizei numQuads = 0;
941
Romain Guy7230a742011-01-10 22:26:16 -0800942 setupDraw();
943 setupDrawWithTexture();
944 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800945 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700946 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800947 setupDrawProgram();
948 setupDrawDirtyRegionsDisabled();
949 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800950 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700951 setupDrawTexture(layer->getTexture());
952 if (mSnapshot->transform->isPureTranslate()) {
953 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
954 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
955
Romain Guyd21b6e12011-11-30 20:21:23 -0800956 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700957 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
958 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800959 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700960 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
961 }
Romain Guy15bc6432011-12-13 13:11:32 -0800962 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700963
964 for (size_t i = 0; i < count; i++) {
965 const android::Rect* r = &rects[i];
966
967 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800968 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700969 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800970 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700971
972 // TODO: Reject quads outside of the clip
973 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
974 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
975 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
976 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
977
978 numQuads++;
979
980 if (numQuads >= REGION_MESH_QUAD_COUNT) {
981 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
982 numQuads = 0;
983 mesh = mCaches.getRegionMesh();
984 }
985 }
986
987 if (numQuads > 0) {
988 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
989 }
990
Romain Guy7230a742011-01-10 22:26:16 -0800991 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700992
993#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800994 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700995#endif
996
997 layer->region.clear();
998 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700999}
1000
Romain Guy3a3133d2011-02-01 22:59:58 -08001001void OpenGLRenderer::drawRegionRects(const Region& region) {
1002#if DEBUG_LAYERS_AS_REGIONS
1003 size_t count;
1004 const android::Rect* rects = region.getArray(&count);
1005
1006 uint32_t colors[] = {
1007 0x7fff0000, 0x7f00ff00,
1008 0x7f0000ff, 0x7fff00ff,
1009 };
1010
1011 int offset = 0;
1012 int32_t top = rects[0].top;
1013
1014 for (size_t i = 0; i < count; i++) {
1015 if (top != rects[i].top) {
1016 offset ^= 0x2;
1017 top = rects[i].top;
1018 }
1019
1020 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1021 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1022 SkXfermode::kSrcOver_Mode);
1023 }
1024#endif
1025}
1026
Romain Guy5b3b3522010-10-27 18:57:51 -07001027void OpenGLRenderer::dirtyLayer(const float left, const float top,
1028 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001029 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001030 Rect bounds(left, top, right, bottom);
1031 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001032 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001033 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001034}
1035
1036void OpenGLRenderer::dirtyLayer(const float left, const float top,
1037 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001038 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001039 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001040 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001041 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001042}
1043
1044void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001045 if (bounds.intersect(*mSnapshot->clipRect)) {
1046 bounds.snapToPixelBoundaries();
1047 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1048 if (!dirty.isEmpty()) {
1049 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001050 }
1051 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001052}
1053
Romain Guy54be1cd2011-06-13 19:04:27 -07001054void OpenGLRenderer::clearLayerRegions() {
1055 const size_t count = mLayers.size();
1056 if (count == 0) return;
1057
1058 if (!mSnapshot->isIgnored()) {
1059 // Doing several glScissor/glClear here can negatively impact
1060 // GPUs with a tiler architecture, instead we draw quads with
1061 // the Clear blending mode
1062
1063 // The list contains bounds that have already been clipped
1064 // against their initial clip rect, and the current clip
1065 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001066 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001067
1068 Vertex mesh[count * 6];
1069 Vertex* vertex = mesh;
1070
1071 for (uint32_t i = 0; i < count; i++) {
1072 Rect* bounds = mLayers.itemAt(i);
1073
1074 Vertex::set(vertex++, bounds->left, bounds->bottom);
1075 Vertex::set(vertex++, bounds->left, bounds->top);
1076 Vertex::set(vertex++, bounds->right, bounds->top);
1077 Vertex::set(vertex++, bounds->left, bounds->bottom);
1078 Vertex::set(vertex++, bounds->right, bounds->top);
1079 Vertex::set(vertex++, bounds->right, bounds->bottom);
1080
1081 delete bounds;
1082 }
1083
1084 setupDraw(false);
1085 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1086 setupDrawBlending(true, SkXfermode::kClear_Mode);
1087 setupDrawProgram();
1088 setupDrawPureColorUniforms();
1089 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001090 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001091
Romain Guy54be1cd2011-06-13 19:04:27 -07001092 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001093
1094 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001095 } else {
1096 for (uint32_t i = 0; i < count; i++) {
1097 delete mLayers.itemAt(i);
1098 }
1099 }
1100
1101 mLayers.clear();
1102}
1103
Romain Guybd6b79b2010-06-26 00:13:53 -07001104///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001105// Transforms
1106///////////////////////////////////////////////////////////////////////////////
1107
1108void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001109 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001110}
1111
1112void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001113 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001114}
1115
1116void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001117 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001118}
1119
Romain Guy807daf72011-01-18 11:19:19 -08001120void OpenGLRenderer::skew(float sx, float sy) {
1121 mSnapshot->transform->skew(sx, sy);
1122}
1123
Romain Guyf6a11b82010-06-23 17:47:49 -07001124void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001125 if (matrix) {
1126 mSnapshot->transform->load(*matrix);
1127 } else {
1128 mSnapshot->transform->loadIdentity();
1129 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001130}
1131
1132void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001133 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001134}
1135
1136void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001137 SkMatrix transform;
1138 mSnapshot->transform->copyTo(transform);
1139 transform.preConcat(*matrix);
1140 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001141}
1142
1143///////////////////////////////////////////////////////////////////////////////
1144// Clipping
1145///////////////////////////////////////////////////////////////////////////////
1146
Romain Guybb9524b2010-06-22 18:56:38 -07001147void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001148 Rect clip(*mSnapshot->clipRect);
1149 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001150
Romain Guy8a4ac612012-07-17 17:32:48 -07001151 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1152 clip.getWidth(), clip.getHeight())) {
1153 mDirtyClip = false;
1154 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001155}
1156
1157const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001158 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001159}
1160
Romain Guy8a4ac612012-07-17 17:32:48 -07001161bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1162 if (mSnapshot->isIgnored()) {
1163 return true;
1164 }
1165
1166 Rect r(left, top, right, bottom);
1167 mSnapshot->transform->mapRect(r);
1168 r.snapToPixelBoundaries();
1169
1170 Rect clipRect(*mSnapshot->clipRect);
1171 clipRect.snapToPixelBoundaries();
1172
1173 return !clipRect.intersects(r);
1174}
1175
Romain Guy35643dd2012-09-18 15:40:58 -07001176bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1177 Rect& transformed, Rect& clip) {
1178 if (mSnapshot->isIgnored()) {
1179 return true;
1180 }
1181
1182 transformed.set(left, top, right, bottom);
1183 mSnapshot->transform->mapRect(transformed);
1184 transformed.snapToPixelBoundaries();
1185
1186 clip.set(*mSnapshot->clipRect);
1187 clip.snapToPixelBoundaries();
1188
1189 return !clip.intersects(transformed);
1190}
1191
Romain Guyc7d53492010-06-25 13:41:57 -07001192bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001193 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001194 return true;
1195 }
1196
Romain Guy1d83e192010-08-17 11:37:00 -07001197 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001198 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001199 r.snapToPixelBoundaries();
1200
1201 Rect clipRect(*mSnapshot->clipRect);
1202 clipRect.snapToPixelBoundaries();
1203
Romain Guy586cae32012-07-13 15:28:31 -07001204 bool rejected = !clipRect.intersects(r);
1205 if (!isDeferred() && !rejected) {
1206 mCaches.setScissorEnabled(!clipRect.contains(r));
1207 }
1208
1209 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001210}
1211
Romain Guy079ba2c2010-07-16 14:12:24 -07001212bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1213 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001214 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001215 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001216 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001217 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001218}
1219
Chet Haasea23eed82012-04-12 15:19:04 -07001220Rect* OpenGLRenderer::getClipRect() {
1221 return mSnapshot->clipRect;
1222}
1223
Romain Guyf6a11b82010-06-23 17:47:49 -07001224///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001225// Drawing commands
1226///////////////////////////////////////////////////////////////////////////////
1227
Romain Guy54be1cd2011-06-13 19:04:27 -07001228void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001229 // TODO: It would be best if we could do this before quickReject()
1230 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001231 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001232 if (mDirtyClip) {
1233 setScissorFromClip();
1234 }
1235 mDescription.reset();
1236 mSetShaderColor = false;
1237 mColorSet = false;
1238 mColorA = mColorR = mColorG = mColorB = 0.0f;
1239 mTextureUnit = 0;
1240 mTrackDirtyRegions = true;
1241}
1242
1243void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1244 mDescription.hasTexture = true;
1245 mDescription.hasAlpha8Texture = isAlpha8;
1246}
1247
Romain Guyaa6c24c2011-04-28 18:40:04 -07001248void OpenGLRenderer::setupDrawWithExternalTexture() {
1249 mDescription.hasExternalTexture = true;
1250}
1251
Romain Guy15bc6432011-12-13 13:11:32 -08001252void OpenGLRenderer::setupDrawNoTexture() {
1253 mCaches.disbaleTexCoordsVertexArray();
1254}
1255
Chris Craik710f46d2012-09-17 17:25:49 -07001256void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001257 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001258}
1259
Chris Craik710f46d2012-09-17 17:25:49 -07001260void OpenGLRenderer::setupDrawVertexShape() {
1261 mDescription.isVertexShape = true;
Chris Craik6ebdc112012-08-31 18:24:33 -07001262}
1263
Romain Guyed6fcb02011-03-21 13:11:28 -07001264void OpenGLRenderer::setupDrawPoint(float pointSize) {
1265 mDescription.isPoint = true;
1266 mDescription.pointSize = pointSize;
1267}
1268
Romain Guy70ca14e2010-12-13 18:24:33 -08001269void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001270 setupDrawColor(color, (color >> 24) & 0xFF);
1271}
1272
1273void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1274 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001275 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1276 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001277 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001278 mColorR = a * ((color >> 16) & 0xFF);
1279 mColorG = a * ((color >> 8) & 0xFF);
1280 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001281 mColorSet = true;
1282 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1283}
1284
Romain Guy86568192010-12-14 15:55:39 -08001285void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1286 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001287 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1288 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001289 const float a = mColorA / 255.0f;
1290 mColorR = a * ((color >> 16) & 0xFF);
1291 mColorG = a * ((color >> 8) & 0xFF);
1292 mColorB = a * ((color ) & 0xFF);
1293 mColorSet = true;
1294 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1295}
1296
Romain Guy41210632012-07-16 17:04:24 -07001297void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1298 mCaches.fontRenderer->describe(mDescription, paint);
1299}
1300
Romain Guy70ca14e2010-12-13 18:24:33 -08001301void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1302 mColorA = a;
1303 mColorR = r;
1304 mColorG = g;
1305 mColorB = b;
1306 mColorSet = true;
1307 mSetShaderColor = mDescription.setColor(r, g, b, a);
1308}
1309
1310void OpenGLRenderer::setupDrawShader() {
1311 if (mShader) {
1312 mShader->describe(mDescription, mCaches.extensions);
1313 }
1314}
1315
1316void OpenGLRenderer::setupDrawColorFilter() {
1317 if (mColorFilter) {
1318 mColorFilter->describe(mDescription, mCaches.extensions);
1319 }
1320}
1321
Romain Guyf09ef512011-05-27 11:43:46 -07001322void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1323 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1324 mColorA = 1.0f;
1325 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001326 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001327 }
1328}
1329
Romain Guy70ca14e2010-12-13 18:24:33 -08001330void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001331 // When the blending mode is kClear_Mode, we need to use a modulate color
1332 // argb=1,0,0,0
1333 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001334 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1335 mDescription, swapSrcDst);
1336}
1337
1338void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001339 // When the blending mode is kClear_Mode, we need to use a modulate color
1340 // argb=1,0,0,0
1341 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001342 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1343 mDescription, swapSrcDst);
1344}
1345
1346void OpenGLRenderer::setupDrawProgram() {
1347 useProgram(mCaches.programCache.get(mDescription));
1348}
1349
1350void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1351 mTrackDirtyRegions = false;
1352}
1353
1354void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1355 bool ignoreTransform) {
1356 mModelView.loadTranslate(left, top, 0.0f);
1357 if (!ignoreTransform) {
1358 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1359 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1360 } else {
1361 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1362 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1363 }
1364}
1365
Chet Haase8a5cc922011-04-26 07:28:09 -07001366void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1367 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001368}
1369
Romain Guy70ca14e2010-12-13 18:24:33 -08001370void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1371 bool ignoreTransform, bool ignoreModelView) {
1372 if (!ignoreModelView) {
1373 mModelView.loadTranslate(left, top, 0.0f);
1374 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001375 } else {
1376 mModelView.loadIdentity();
1377 }
Romain Guy86568192010-12-14 15:55:39 -08001378 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1379 if (!ignoreTransform) {
1380 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1381 if (mTrackDirtyRegions && dirty) {
1382 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1383 }
1384 } else {
1385 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1386 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1387 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001388}
1389
Romain Guyed6fcb02011-03-21 13:11:28 -07001390void OpenGLRenderer::setupDrawPointUniforms() {
1391 int slot = mCaches.currentProgram->getUniform("pointSize");
1392 glUniform1f(slot, mDescription.pointSize);
1393}
1394
Romain Guy70ca14e2010-12-13 18:24:33 -08001395void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001396 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001397 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1398 }
1399}
1400
Romain Guy86568192010-12-14 15:55:39 -08001401void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001402 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001403 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001404 }
1405}
1406
Romain Guy70ca14e2010-12-13 18:24:33 -08001407void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1408 if (mShader) {
1409 if (ignoreTransform) {
1410 mModelView.loadInverse(*mSnapshot->transform);
1411 }
1412 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1413 }
1414}
1415
Romain Guy8d0d4782010-12-14 20:13:35 -08001416void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1417 if (mShader) {
1418 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1419 }
1420}
1421
Romain Guy70ca14e2010-12-13 18:24:33 -08001422void OpenGLRenderer::setupDrawColorFilterUniforms() {
1423 if (mColorFilter) {
1424 mColorFilter->setupProgram(mCaches.currentProgram);
1425 }
1426}
1427
Romain Guy41210632012-07-16 17:04:24 -07001428void OpenGLRenderer::setupDrawTextGammaUniforms() {
1429 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1430}
1431
Romain Guy70ca14e2010-12-13 18:24:33 -08001432void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001433 bool force = mCaches.bindMeshBuffer();
1434 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001435 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001436}
1437
1438void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1439 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001440 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001441 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001442}
1443
Romain Guyaa6c24c2011-04-28 18:40:04 -07001444void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1445 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001446 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001447 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001448}
1449
Romain Guy8f0095c2011-05-02 17:24:22 -07001450void OpenGLRenderer::setupDrawTextureTransform() {
1451 mDescription.hasTextureTransform = true;
1452}
1453
1454void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001455 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1456 GL_FALSE, &transform.data[0]);
1457}
1458
Romain Guy70ca14e2010-12-13 18:24:33 -08001459void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001460 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001461 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001462 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001463 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001464 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001465 }
Romain Guyd71dd362011-12-12 19:03:35 -08001466
Romain Guyf3a910b42011-12-12 20:35:21 -08001467 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001468 if (mCaches.currentProgram->texCoords >= 0) {
1469 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1470 }
1471
1472 mCaches.unbindIndicesBuffer();
1473}
1474
1475void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1476 bool force = mCaches.unbindMeshBuffer();
1477 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1478 if (mCaches.currentProgram->texCoords >= 0) {
1479 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001480 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001481}
1482
Chet Haase5b0200b2011-04-13 17:58:08 -07001483void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001484 bool force = mCaches.unbindMeshBuffer();
1485 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1486 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001487 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001488}
1489
1490/**
1491 * 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 -07001492 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1493 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1494 * attributes (one per vertex) are values from zero to one that tells the fragment
1495 * shader where the fragment is in relation to the line width/length overall; these values are
1496 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1497 * region of the line.
1498 * Note that we only pass down the width values in this setup function. The length coordinates
1499 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001500 */
Chet Haase99585ad2011-05-02 15:00:16 -07001501void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001502 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001503 bool force = mCaches.unbindMeshBuffer();
1504 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1505 vertices, gAAVertexStride);
1506 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001507 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001508
Romain Guy7b631422012-04-04 11:38:54 -07001509 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001510 glEnableVertexAttribArray(widthSlot);
1511 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001512
Romain Guy7b631422012-04-04 11:38:54 -07001513 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001514 glEnableVertexAttribArray(lengthSlot);
1515 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001516
Chet Haase5b0200b2011-04-13 17:58:08 -07001517 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001518 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001519}
1520
1521void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1522 glDisableVertexAttribArray(widthSlot);
1523 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001524}
1525
Romain Guy70ca14e2010-12-13 18:24:33 -08001526void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001527}
1528
1529///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001530// Drawing
1531///////////////////////////////////////////////////////////////////////////////
1532
Chet Haase1271e2c2012-04-20 09:54:27 -07001533status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001534 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001535
Romain Guy0fe478e2010-11-08 12:08:41 -08001536 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1537 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001538 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001539 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001540 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001541
Romain Guy65549432012-03-26 16:45:05 -07001542 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001543}
1544
Chet Haaseed30fd82011-04-22 16:18:45 -07001545void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1546 if (displayList) {
1547 displayList->output(*this, level);
1548 }
1549}
1550
Romain Guya168d732011-03-18 16:50:13 -07001551void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1552 int alpha;
1553 SkXfermode::Mode mode;
1554 getAlphaAndMode(paint, &alpha, &mode);
1555
Romain Guya168d732011-03-18 16:50:13 -07001556 float x = left;
1557 float y = top;
1558
Romain Guye3c26852011-07-25 16:36:01 -07001559 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001560 bool ignoreTransform = false;
1561 if (mSnapshot->transform->isPureTranslate()) {
1562 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1563 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1564 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001565 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001566 } else {
1567 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001568 }
1569
1570 setupDraw();
1571 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001572 if (paint) {
1573 setupDrawAlpha8Color(paint->getColor(), alpha);
1574 }
Romain Guya168d732011-03-18 16:50:13 -07001575 setupDrawColorFilter();
1576 setupDrawShader();
1577 setupDrawBlending(true, mode);
1578 setupDrawProgram();
1579 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001580
Romain Guya168d732011-03-18 16:50:13 -07001581 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001582 texture->setWrap(GL_CLAMP_TO_EDGE);
1583 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001584
Romain Guya168d732011-03-18 16:50:13 -07001585 setupDrawPureColorUniforms();
1586 setupDrawColorFilterUniforms();
1587 setupDrawShaderUniforms();
1588 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1589
1590 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1591
1592 finishDrawTexture();
1593}
1594
Chet Haase48659092012-05-31 15:21:51 -07001595status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001596 const float right = left + bitmap->width();
1597 const float bottom = top + bitmap->height();
1598
1599 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001600 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001601 }
1602
Romain Guya1d3c912011-12-13 14:55:06 -08001603 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001604 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001605 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001606 const AutoTexture autoCleanup(texture);
1607
Romain Guy211370f2012-02-01 16:10:55 -08001608 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001609 drawAlphaBitmap(texture, left, top, paint);
1610 } else {
1611 drawTextureRect(left, top, right, bottom, texture, paint);
1612 }
Chet Haase48659092012-05-31 15:21:51 -07001613
1614 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001615}
1616
Chet Haase48659092012-05-31 15:21:51 -07001617status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001618 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1619 const mat4 transform(*matrix);
1620 transform.mapRect(r);
1621
Romain Guy6926c722010-07-12 20:20:03 -07001622 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001623 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001624 }
1625
Romain Guya1d3c912011-12-13 14:55:06 -08001626 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001627 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001628 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001629 const AutoTexture autoCleanup(texture);
1630
Romain Guy5b3b3522010-10-27 18:57:51 -07001631 // This could be done in a cheaper way, all we need is pass the matrix
1632 // to the vertex shader. The save/restore is a bit overkill.
1633 save(SkCanvas::kMatrix_SaveFlag);
1634 concatMatrix(matrix);
1635 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1636 restore();
Chet Haase48659092012-05-31 15:21:51 -07001637
1638 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001639}
1640
Chet Haase48659092012-05-31 15:21:51 -07001641status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001642 const float right = left + bitmap->width();
1643 const float bottom = top + bitmap->height();
1644
1645 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001646 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001647 }
1648
1649 mCaches.activeTexture(0);
1650 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1651 const AutoTexture autoCleanup(texture);
1652
1653 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001654
1655 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001656}
1657
Chet Haase48659092012-05-31 15:21:51 -07001658status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001659 float* vertices, int* colors, SkPaint* paint) {
1660 // TODO: Do a quickReject
1661 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001662 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001663 }
1664
Romain Guya1d3c912011-12-13 14:55:06 -08001665 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001666 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001667 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001668 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001669
Romain Guyd21b6e12011-11-30 20:21:23 -08001670 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1671 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001672
1673 int alpha;
1674 SkXfermode::Mode mode;
1675 getAlphaAndMode(paint, &alpha, &mode);
1676
Romain Guy5a7b4662011-01-20 19:09:30 -08001677 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001678
Romain Guyb18d2d02011-02-10 15:52:54 -08001679 float left = FLT_MAX;
1680 float top = FLT_MAX;
1681 float right = FLT_MIN;
1682 float bottom = FLT_MIN;
1683
Romain Guy211370f2012-02-01 16:10:55 -08001684 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001685
Romain Guya566b7c2011-01-23 16:36:11 -08001686 // TODO: Support the colors array
1687 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001688 TextureVertex* vertex = mesh;
1689 for (int32_t y = 0; y < meshHeight; y++) {
1690 for (int32_t x = 0; x < meshWidth; x++) {
1691 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1692
1693 float u1 = float(x) / meshWidth;
1694 float u2 = float(x + 1) / meshWidth;
1695 float v1 = float(y) / meshHeight;
1696 float v2 = float(y + 1) / meshHeight;
1697
1698 int ax = i + (meshWidth + 1) * 2;
1699 int ay = ax + 1;
1700 int bx = i;
1701 int by = bx + 1;
1702 int cx = i + 2;
1703 int cy = cx + 1;
1704 int dx = i + (meshWidth + 1) * 2 + 2;
1705 int dy = dx + 1;
1706
1707 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1708 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1709 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1710
1711 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1712 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1713 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001714
Romain Guyb18d2d02011-02-10 15:52:54 -08001715 if (hasActiveLayer) {
1716 // TODO: This could be optimized to avoid unnecessary ops
1717 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1718 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1719 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1720 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1721 }
Romain Guy5a7b4662011-01-20 19:09:30 -08001722 }
1723 }
1724
Romain Guyb18d2d02011-02-10 15:52:54 -08001725 if (hasActiveLayer) {
1726 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1727 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001728
Romain Guy5a7b4662011-01-20 19:09:30 -08001729 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1730 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001731 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001732
1733 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001734}
1735
Chet Haase48659092012-05-31 15:21:51 -07001736status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001737 float srcLeft, float srcTop, float srcRight, float srcBottom,
1738 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001739 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001740 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001741 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001742 }
1743
Romain Guya1d3c912011-12-13 14:55:06 -08001744 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001745 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001746 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001747 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001748
Romain Guy8ba548f2010-06-30 19:21:21 -07001749 const float width = texture->width;
1750 const float height = texture->height;
1751
Romain Guy68169722011-08-22 17:33:33 -07001752 const float u1 = fmax(0.0f, srcLeft / width);
1753 const float v1 = fmax(0.0f, srcTop / height);
1754 const float u2 = fmin(1.0f, srcRight / width);
1755 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001756
Romain Guy03750a02010-10-18 14:06:08 -07001757 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001758 resetDrawTextureTexCoords(u1, v1, u2, v2);
1759
Romain Guy03750a02010-10-18 14:06:08 -07001760 int alpha;
1761 SkXfermode::Mode mode;
1762 getAlphaAndMode(paint, &alpha, &mode);
1763
Romain Guyd21b6e12011-11-30 20:21:23 -08001764 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1765
Romain Guy211370f2012-02-01 16:10:55 -08001766 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001767 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1768 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1769
Romain Guyb5014982011-07-28 15:39:12 -07001770 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001771 // Enable linear filtering if the source rectangle is scaled
1772 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001773 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001774 }
Romain Guyb5014982011-07-28 15:39:12 -07001775
Romain Guyd21b6e12011-11-30 20:21:23 -08001776 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001777 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1778 texture->id, alpha / 255.0f, mode, texture->blend,
1779 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1780 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1781 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001782 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001783 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1784 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1785 GL_TRIANGLE_STRIP, gMeshCount);
1786 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001787
1788 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001789
1790 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001791}
1792
Chet Haase48659092012-05-31 15:21:51 -07001793status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001794 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001795 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001796 int alpha;
1797 SkXfermode::Mode mode;
1798 getAlphaAndModeDirect(paint, &alpha, &mode);
1799
1800 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1801 left, top, right, bottom, alpha, mode);
1802}
1803
1804status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1805 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1806 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001807 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001808 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001809 }
1810
Romain Guybe6f9dc2012-07-16 12:41:17 -07001811 alpha *= mSnapshot->alpha;
1812
Romain Guya1d3c912011-12-13 14:55:06 -08001813 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001814 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001815 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001816 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001817 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1818 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001819
Romain Guy2728f962010-10-08 18:36:15 -07001820 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001821 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001822
Romain Guy211370f2012-02-01 16:10:55 -08001823 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001824 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001825 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001826 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001827 const float offsetX = left + mSnapshot->transform->getTranslateX();
1828 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001829 const size_t count = mesh->quads.size();
1830 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001831 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001832 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001833 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1834 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1835 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001836 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001837 dirtyLayer(left + bounds.left, top + bounds.top,
1838 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001839 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001840 }
1841 }
1842
Romain Guy211370f2012-02-01 16:10:55 -08001843 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001844 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1845 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1846
1847 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1848 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1849 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1850 true, !mesh->hasEmptyQuads);
1851 } else {
1852 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1853 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1854 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1855 true, !mesh->hasEmptyQuads);
1856 }
Romain Guy054dc182010-10-15 17:55:25 -07001857 }
Chet Haase48659092012-05-31 15:21:51 -07001858
1859 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001860}
1861
Chet Haase99ecdc42011-05-06 12:06:34 -07001862/**
Chet Haase858aa932011-05-12 09:06:00 -07001863 * This function uses a similar approach to that of AA lines in the drawLines() function.
Chris Craik6ebdc112012-08-31 18:24:33 -07001864 * We expand the rectangle by a half pixel in screen space on all sides. However, instead of using
1865 * a fragment shader to compute the translucency of the color from its position, we simply use a
1866 * varying parameter to define how far a given pixel is into the region.
Chet Haase858aa932011-05-12 09:06:00 -07001867 */
Chris Craik710f46d2012-09-17 17:25:49 -07001868void OpenGLRenderer::drawConvexPath(const SkPath& path, int color, SkXfermode::Mode mode, bool isAA) {
1869 VertexBuffer vertexBuffer;
1870 // TODO: try clipping large paths to viewport
1871 PathRenderer::convexPathFillVertices(path, mSnapshot->transform, vertexBuffer, isAA);
Romain Guy04299382012-07-18 17:15:41 -07001872
Chris Craik710f46d2012-09-17 17:25:49 -07001873 setupDraw();
1874 setupDrawNoTexture();
1875 if (isAA) setupDrawAA();
1876 setupDrawVertexShape();
1877 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1878 setupDrawColorFilter();
1879 setupDrawShader();
1880 setupDrawBlending(isAA, mode);
1881 setupDrawProgram();
1882 setupDrawModelViewIdentity(true);
1883 setupDrawColorUniforms();
1884 setupDrawColorFilterUniforms();
1885 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07001886
Chris Craik710f46d2012-09-17 17:25:49 -07001887 void* vertices = vertexBuffer.getBuffer();
1888 bool force = mCaches.unbindMeshBuffer();
1889 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1890 vertices, isAA ? gAlphaVertexStride : gVertexStride);
1891 mCaches.resetTexCoordsVertexPointer();
1892 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07001893
Chris Craik710f46d2012-09-17 17:25:49 -07001894 int alphaSlot = -1;
1895 if (isAA) {
1896 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
1897 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07001898
Chris Craik710f46d2012-09-17 17:25:49 -07001899 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07001900 glEnableVertexAttribArray(alphaSlot);
1901 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07001902 }
Romain Guy04299382012-07-18 17:15:41 -07001903
Chris Craik710f46d2012-09-17 17:25:49 -07001904 SkRect bounds = path.getBounds();
1905 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy04299382012-07-18 17:15:41 -07001906
Chris Craik710f46d2012-09-17 17:25:49 -07001907 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07001908
Chris Craik710f46d2012-09-17 17:25:49 -07001909 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07001910 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07001911 }
Chet Haase858aa932011-05-12 09:06:00 -07001912}
1913
1914/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001915 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1916 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1917 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1918 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1919 * of the line. Hairlines are more involved because we need to account for transform scaling
1920 * to end up with a one-pixel-wide line in screen space..
1921 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1922 * in combination with values that we calculate and pass down in this method. The basic approach
1923 * is that the quad we create contains both the core line area plus a bounding area in which
1924 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1925 * proportion of the width and the length of a given segment is represented by the boundary
1926 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1927 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1928 * on the inside). This ends up giving the result we want, with pixels that are completely
1929 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1930 * how far into the boundary region they are, which is determined by shader interpolation.
1931 */
Chet Haase48659092012-05-31 15:21:51 -07001932status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1933 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07001934
1935 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001936 // We use half the stroke width here because we're going to position the quad
1937 // corner vertices half of the width away from the line endpoints
1938 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001939 // A stroke width of 0 has a special meaning in Skia:
1940 // it draws a line 1 px wide regardless of current transform
1941 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001942
Chet Haase99ecdc42011-05-06 12:06:34 -07001943 float inverseScaleX = 1.0f;
1944 float inverseScaleY = 1.0f;
1945 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001946
Chet Haase8a5cc922011-04-26 07:28:09 -07001947 int alpha;
1948 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001949
Chet Haase8a5cc922011-04-26 07:28:09 -07001950 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001951 int verticesCount = count;
1952 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001953 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001954 verticesCount += (count - 4);
1955 }
1956
1957 if (isHairLine || isAA) {
1958 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1959 // the line on the screen should always be one pixel wide regardless of scale. For
1960 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001961 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001962 Matrix4 *mat = mSnapshot->transform;
1963 float m00 = mat->data[Matrix4::kScaleX];
1964 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07001965 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07001966 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07001967
Romain Guy211370f2012-02-01 16:10:55 -08001968 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1969 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001970
Chet Haase99ecdc42011-05-06 12:06:34 -07001971 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1972 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001973
Chet Haase99ecdc42011-05-06 12:06:34 -07001974 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1975 scaled = true;
1976 }
1977 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001978 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001979
1980 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07001981
1982 mCaches.enableScissor();
1983
Chet Haase8a5cc922011-04-26 07:28:09 -07001984 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001985 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001986 if (isAA) {
Chris Craik710f46d2012-09-17 17:25:49 -07001987 setupDrawAA();
Chet Haase8a5cc922011-04-26 07:28:09 -07001988 }
1989 setupDrawColor(paint->getColor(), alpha);
1990 setupDrawColorFilter();
1991 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001992 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001993 setupDrawProgram();
1994 setupDrawModelViewIdentity(true);
1995 setupDrawColorUniforms();
1996 setupDrawColorFilterUniforms();
1997 setupDrawShaderIdentityUniforms();
1998
1999 if (isHairLine) {
2000 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07002001 halfStrokeWidth = isAA? 1 : .5;
2002 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002003 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07002004 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07002005 }
Romain Guy7b631422012-04-04 11:38:54 -07002006
2007 int widthSlot;
2008 int lengthSlot;
2009
Chet Haase5b0200b2011-04-13 17:58:08 -07002010 Vertex lines[verticesCount];
2011 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002012
Chet Haase99585ad2011-05-02 15:00:16 -07002013 AAVertex wLines[verticesCount];
2014 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002015
Romain Guy211370f2012-02-01 16:10:55 -08002016 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002017 setupDrawVertices(vertices);
2018 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002019 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2020 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002021 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002022 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2023 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002024 // We will need to calculate the actual width proportion on each segment for
2025 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002026 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002027 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2028 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002029 }
2030
Chet Haase99ecdc42011-05-06 12:06:34 -07002031 AAVertex* prevAAVertex = NULL;
2032 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002033
Chet Haase99585ad2011-05-02 15:00:16 -07002034 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002035 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002036
Chet Haase5b0200b2011-04-13 17:58:08 -07002037 for (int i = 0; i < count; i += 4) {
2038 // a = start point, b = end point
2039 vec2 a(points[i], points[i + 1]);
2040 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002041
Chet Haase99585ad2011-05-02 15:00:16 -07002042 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002043 float boundaryLengthProportion = 0;
2044 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002045
Chet Haase5b0200b2011-04-13 17:58:08 -07002046 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002047 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002048 float x = n.x;
2049 n.x = -n.y;
2050 n.y = x;
2051
Chet Haase8a5cc922011-04-26 07:28:09 -07002052 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002053 if (isAA) {
2054 float wideningFactor;
2055 if (fabs(n.x) >= fabs(n.y)) {
2056 wideningFactor = fabs(1.0f / n.x);
2057 } else {
2058 wideningFactor = fabs(1.0f / n.y);
2059 }
2060 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002061 }
Romain Guy7b631422012-04-04 11:38:54 -07002062
Chet Haase99ecdc42011-05-06 12:06:34 -07002063 if (scaled) {
2064 n.x *= inverseScaleX;
2065 n.y *= inverseScaleY;
2066 }
2067 } else if (scaled) {
2068 // Extend n by .5 pixel on each side, post-transform
2069 vec2 extendedN = n.copyNormalized();
2070 extendedN /= 2;
2071 extendedN.x *= inverseScaleX;
2072 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002073
Chet Haase99ecdc42011-05-06 12:06:34 -07002074 float extendedNLength = extendedN.length();
2075 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002076 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002077 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002078 }
Romain Guy7b631422012-04-04 11:38:54 -07002079
Chet Haase99585ad2011-05-02 15:00:16 -07002080 // aa lines expand the endpoint vertices to encompass the AA boundary
2081 if (isAA) {
2082 vec2 abVector = (b - a);
2083 length = abVector.length();
2084 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002085
Chet Haase99ecdc42011-05-06 12:06:34 -07002086 if (scaled) {
2087 abVector.x *= inverseScaleX;
2088 abVector.y *= inverseScaleY;
2089 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002090 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002091 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002092 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002093 }
Romain Guy7b631422012-04-04 11:38:54 -07002094
Chet Haase99ecdc42011-05-06 12:06:34 -07002095 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002096 a -= abVector;
2097 b += abVector;
2098 }
2099
Chet Haase5b0200b2011-04-13 17:58:08 -07002100 // Four corners of the rectangle defining a thick line
2101 vec2 p1 = a - n;
2102 vec2 p2 = a + n;
2103 vec2 p3 = b + n;
2104 vec2 p4 = b - n;
2105
Chet Haase99585ad2011-05-02 15:00:16 -07002106
Chet Haase5b0200b2011-04-13 17:58:08 -07002107 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2108 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2109 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2110 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2111
Romain Guy04299382012-07-18 17:15:41 -07002112 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002113 if (!isAA) {
2114 if (prevVertex != NULL) {
2115 // Issue two repeat vertices to create degenerate triangles to bridge
2116 // between the previous line and the new one. This is necessary because
2117 // we are creating a single triangle_strip which will contain
2118 // potentially discontinuous line segments.
2119 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2120 Vertex::set(vertices++, p1.x, p1.y);
2121 generatedVerticesCount += 2;
2122 }
Romain Guy7b631422012-04-04 11:38:54 -07002123
Chet Haase5b0200b2011-04-13 17:58:08 -07002124 Vertex::set(vertices++, p1.x, p1.y);
2125 Vertex::set(vertices++, p2.x, p2.y);
2126 Vertex::set(vertices++, p4.x, p4.y);
2127 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002128
Chet Haase5b0200b2011-04-13 17:58:08 -07002129 prevVertex = vertices - 1;
2130 generatedVerticesCount += 4;
2131 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002132 if (!isHairLine && scaled) {
2133 // Must set width proportions per-segment for scaled non-hairlines to use the
2134 // correct AA boundary dimensions
2135 if (boundaryWidthSlot < 0) {
2136 boundaryWidthSlot =
2137 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002138 }
Romain Guy7b631422012-04-04 11:38:54 -07002139
Chet Haase99ecdc42011-05-06 12:06:34 -07002140 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002141 }
Romain Guy7b631422012-04-04 11:38:54 -07002142
Chet Haase99585ad2011-05-02 15:00:16 -07002143 if (boundaryLengthSlot < 0) {
2144 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002145 }
Romain Guy7b631422012-04-04 11:38:54 -07002146
Chet Haase99ecdc42011-05-06 12:06:34 -07002147 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002148
Chet Haase5b0200b2011-04-13 17:58:08 -07002149 if (prevAAVertex != NULL) {
2150 // Issue two repeat vertices to create degenerate triangles to bridge
2151 // between the previous line and the new one. This is necessary because
2152 // we are creating a single triangle_strip which will contain
2153 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002154 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2155 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2156 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002157 generatedVerticesCount += 2;
2158 }
Romain Guy7b631422012-04-04 11:38:54 -07002159
Chet Haase99585ad2011-05-02 15:00:16 -07002160 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2161 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2162 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2163 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002164
Chet Haase5b0200b2011-04-13 17:58:08 -07002165 prevAAVertex = aaVertices - 1;
2166 generatedVerticesCount += 4;
2167 }
Romain Guy7b631422012-04-04 11:38:54 -07002168
Chet Haase99585ad2011-05-02 15:00:16 -07002169 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2170 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2171 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002172 }
2173 }
Romain Guy7b631422012-04-04 11:38:54 -07002174
Chet Haase5b0200b2011-04-13 17:58:08 -07002175 if (generatedVerticesCount > 0) {
2176 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2177 }
Romain Guy7b631422012-04-04 11:38:54 -07002178
2179 if (isAA) {
2180 finishDrawAALine(widthSlot, lengthSlot);
2181 }
Chet Haase48659092012-05-31 15:21:51 -07002182
2183 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002184}
2185
Chet Haase48659092012-05-31 15:21:51 -07002186status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2187 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002188
2189 // TODO: The paint's cap style defines whether the points are square or circular
2190 // TODO: Handle AA for round points
2191
Chet Haase5b0200b2011-04-13 17:58:08 -07002192 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002193 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002194 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002195 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002196 if (isHairLine) {
2197 // Now that we know it's hairline, we can set the effective width, to be used later
2198 strokeWidth = 1.0f;
2199 }
2200 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002201 int alpha;
2202 SkXfermode::Mode mode;
2203 getAlphaAndMode(paint, &alpha, &mode);
2204
2205 int verticesCount = count >> 1;
2206 int generatedVerticesCount = 0;
2207
2208 TextureVertex pointsData[verticesCount];
2209 TextureVertex* vertex = &pointsData[0];
2210
Romain Guy04299382012-07-18 17:15:41 -07002211 // TODO: We should optimize this method to not generate vertices for points
2212 // that lie outside of the clip.
2213 mCaches.enableScissor();
2214
Romain Guyed6fcb02011-03-21 13:11:28 -07002215 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002216 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002217 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002218 setupDrawColor(paint->getColor(), alpha);
2219 setupDrawColorFilter();
2220 setupDrawShader();
2221 setupDrawBlending(mode);
2222 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002223 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002224 setupDrawColorUniforms();
2225 setupDrawColorFilterUniforms();
2226 setupDrawPointUniforms();
2227 setupDrawShaderIdentityUniforms();
2228 setupDrawMesh(vertex);
2229
2230 for (int i = 0; i < count; i += 2) {
2231 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2232 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002233
Chet Haase8a5cc922011-04-26 07:28:09 -07002234 float left = points[i] - halfWidth;
2235 float right = points[i] + halfWidth;
2236 float top = points[i + 1] - halfWidth;
2237 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002238
Chet Haase8a5cc922011-04-26 07:28:09 -07002239 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002240 }
2241
2242 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002243
2244 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002245}
2246
Chet Haase48659092012-05-31 15:21:51 -07002247status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002248 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002249 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002250
Romain Guyae88e5e2010-10-22 17:49:18 -07002251 Rect& clip(*mSnapshot->clipRect);
2252 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002253
Romain Guy3d58c032010-07-14 16:34:53 -07002254 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002255
2256 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002257}
Romain Guy9d5316e2010-06-24 19:30:36 -07002258
Chet Haase48659092012-05-31 15:21:51 -07002259status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2260 SkPaint* paint) {
2261 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002262 const AutoTexture autoCleanup(texture);
2263
2264 const float x = left + texture->left - texture->offset;
2265 const float y = top + texture->top - texture->offset;
2266
2267 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002268
2269 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002270}
2271
Chet Haase48659092012-05-31 15:21:51 -07002272status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002273 float rx, float ry, SkPaint* p) {
2274 if (mSnapshot->isIgnored() || quickReject(left, top, right, bottom)) {
2275 return DrawGlInfo::kStatusDone;
2276 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002277
Chris Craik710f46d2012-09-17 17:25:49 -07002278 if (p->getStyle() != SkPaint::kFill_Style) {
2279 mCaches.activeTexture(0);
2280 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2281 right - left, bottom - top, rx, ry, p);
2282 return drawShape(left, top, texture, p);
2283 }
2284
2285 SkPath path;
2286 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2287 path.addRoundRect(rect, rx, ry);
2288 drawConvexPath(path, p->getColor(), getXfermode(p->getXfermode()), p->isAntiAlias());
2289
2290 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002291}
2292
Chris Craik710f46d2012-09-17 17:25:49 -07002293status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
2294 if (mSnapshot->isIgnored() || quickReject(x - radius, y - radius, x + radius, y + radius)) {
2295 return DrawGlInfo::kStatusDone;
2296 }
Romain Guy01d58e42011-01-19 21:54:02 -08002297
Chris Craik710f46d2012-09-17 17:25:49 -07002298 if (p->getStyle() != SkPaint::kFill_Style) {
2299 mCaches.activeTexture(0);
2300 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2301 return drawShape(x - radius, y - radius, texture, p);
2302 }
2303
2304 SkPath path;
2305 path.addCircle(x, y, radius);
2306 drawConvexPath(path, p->getColor(), getXfermode(p->getXfermode()), p->isAntiAlias());
2307
2308 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002309}
Romain Guy01d58e42011-01-19 21:54:02 -08002310
Chet Haase48659092012-05-31 15:21:51 -07002311status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002312 SkPaint* p) {
2313 if (mSnapshot->isIgnored() || quickReject(left, top, right, bottom)) {
2314 return DrawGlInfo::kStatusDone;
2315 }
Romain Guy01d58e42011-01-19 21:54:02 -08002316
Chris Craik710f46d2012-09-17 17:25:49 -07002317 if (p->getStyle() != SkPaint::kFill_Style) {
2318 mCaches.activeTexture(0);
2319 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2320 return drawShape(left, top, texture, p);
2321 }
2322
2323 SkPath path;
2324 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2325 path.addOval(rect);
2326 drawConvexPath(path, p->getColor(), getXfermode(p->getXfermode()), p->isAntiAlias());
2327
2328 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002329}
2330
Chet Haase48659092012-05-31 15:21:51 -07002331status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002332 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002333 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002334
2335 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002336 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002337 }
2338
Romain Guya1d3c912011-12-13 14:55:06 -08002339 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002340 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2341 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002342 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002343}
2344
Chet Haase48659092012-05-31 15:21:51 -07002345status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craik710f46d2012-09-17 17:25:49 -07002346 if (mSnapshot->isIgnored() || quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002347 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002348 }
2349
Chris Craik710f46d2012-09-17 17:25:49 -07002350 if (p->getStyle() != SkPaint::kFill_Style) {
2351 mCaches.activeTexture(0);
2352 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2353 return drawShape(left, top, texture, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002354 }
2355
Romain Guy181d0a62011-06-09 18:52:38 -07002356 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002357 SkPath path;
2358 path.addRect(left, top, right, bottom);
2359 drawConvexPath(path, p->getColor(), getXfermode(p->getXfermode()), true);
Chet Haase858aa932011-05-12 09:06:00 -07002360 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002361 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chet Haase858aa932011-05-12 09:06:00 -07002362 }
Chet Haase48659092012-05-31 15:21:51 -07002363
2364 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002365}
Romain Guy9d5316e2010-06-24 19:30:36 -07002366
Raph Levien416a8472012-07-19 22:48:17 -07002367void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2368 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2369 float x, float y) {
2370 mCaches.activeTexture(0);
2371
2372 // NOTE: The drop shadow will not perform gamma correction
2373 // if shader-based correction is enabled
2374 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2375 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2376 paint, text, bytesCount, count, mShadowRadius, positions);
2377 const AutoTexture autoCleanup(shadow);
2378
2379 const float sx = x - shadow->left + mShadowDx;
2380 const float sy = y - shadow->top + mShadowDy;
2381
2382 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2383 int shadowColor = mShadowColor;
2384 if (mShader) {
2385 shadowColor = 0xffffffff;
2386 }
2387
2388 setupDraw();
2389 setupDrawWithTexture(true);
2390 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2391 setupDrawColorFilter();
2392 setupDrawShader();
2393 setupDrawBlending(true, mode);
2394 setupDrawProgram();
2395 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2396 setupDrawTexture(shadow->id);
2397 setupDrawPureColorUniforms();
2398 setupDrawColorFilterUniforms();
2399 setupDrawShaderUniforms();
2400 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2401
2402 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2403}
2404
Chet Haase48659092012-05-31 15:21:51 -07002405status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002406 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002407 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002408 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002409 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002410 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002411
Romain Guy671d6cf2012-01-18 12:39:17 -08002412 // NOTE: Skia does not support perspective transform on drawPosText yet
2413 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002414 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002415 }
2416
2417 float x = 0.0f;
2418 float y = 0.0f;
2419 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2420 if (pureTranslate) {
2421 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2422 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2423 }
2424
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002425 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002426 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2427 paint->getTextSize());
2428
2429 int alpha;
2430 SkXfermode::Mode mode;
2431 getAlphaAndMode(paint, &alpha, &mode);
2432
Raph Levien416a8472012-07-19 22:48:17 -07002433 if (CC_UNLIKELY(mHasShadow)) {
2434 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2435 0.0f, 0.0f);
2436 }
2437
Romain Guy671d6cf2012-01-18 12:39:17 -08002438 // Pick the appropriate texture filtering
2439 bool linearFilter = mSnapshot->transform->changesBounds();
2440 if (pureTranslate && !linearFilter) {
2441 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2442 }
2443
2444 mCaches.activeTexture(0);
2445 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002446 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002447 setupDrawDirtyRegionsDisabled();
2448 setupDrawWithTexture(true);
2449 setupDrawAlpha8Color(paint->getColor(), alpha);
2450 setupDrawColorFilter();
2451 setupDrawShader();
2452 setupDrawBlending(true, mode);
2453 setupDrawProgram();
2454 setupDrawModelView(x, y, x, y, pureTranslate, true);
2455 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2456 setupDrawPureColorUniforms();
2457 setupDrawColorFilterUniforms();
2458 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002459 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002460
2461 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2462 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2463
Romain Guy211370f2012-02-01 16:10:55 -08002464 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002465
2466 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2467 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002468 if (hasActiveLayer) {
2469 if (!pureTranslate) {
2470 mSnapshot->transform->mapRect(bounds);
2471 }
2472 dirtyLayerUnchecked(bounds, getRegion());
2473 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002474 }
Chet Haase48659092012-05-31 15:21:51 -07002475
2476 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002477}
2478
Romain Guyc2525952012-07-27 16:41:22 -07002479status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002480 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002481 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002482 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002483 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002484 }
2485
Chet Haasea1cff502012-02-21 13:43:44 -08002486 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002487 switch (paint->getTextAlign()) {
2488 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002489 x -= length / 2.0f;
2490 break;
2491 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002492 x -= length;
2493 break;
2494 default:
2495 break;
2496 }
2497
Romain Guycac5fd32011-12-01 20:08:50 -08002498 SkPaint::FontMetrics metrics;
2499 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002500 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002501 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002502 }
2503
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002504 const float oldX = x;
2505 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002506 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002507 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002508 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2509 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2510 }
2511
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002512#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002513 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002514 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002515#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002516
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002517 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002518 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002519 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002520
Romain Guy86568192010-12-14 15:55:39 -08002521 int alpha;
2522 SkXfermode::Mode mode;
2523 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002524
Romain Guy211370f2012-02-01 16:10:55 -08002525 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002526 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2527 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002528 }
2529
Romain Guy6620c6d2010-12-06 18:07:02 -08002530 // Pick the appropriate texture filtering
2531 bool linearFilter = mSnapshot->transform->changesBounds();
2532 if (pureTranslate && !linearFilter) {
2533 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2534 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002535
Romain Guy16c88082012-06-11 16:03:47 -07002536 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002537 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002538 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002539 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002540 setupDrawDirtyRegionsDisabled();
2541 setupDrawWithTexture(true);
2542 setupDrawAlpha8Color(paint->getColor(), alpha);
2543 setupDrawColorFilter();
2544 setupDrawShader();
2545 setupDrawBlending(true, mode);
2546 setupDrawProgram();
2547 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002548 // See comment above; the font renderer must use texture unit 0
2549 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002550 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2551 setupDrawPureColorUniforms();
2552 setupDrawColorFilterUniforms();
2553 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002554 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002555
Romain Guy6620c6d2010-12-06 18:07:02 -08002556 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002557 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2558
Romain Guy211370f2012-02-01 16:10:55 -08002559 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002560
Raph Levien996e57c2012-07-23 15:22:52 -07002561 bool status;
Raph Levien8b4072d2012-07-30 15:50:00 -07002562 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
2563 SkPaint paintCopy(*paint);
2564 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2565 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Raph Levien996e57c2012-07-23 15:22:52 -07002566 positions, hasActiveLayer ? &bounds : NULL);
2567 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002568 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2569 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002570 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002571
2572 if (status && hasActiveLayer) {
2573 if (!pureTranslate) {
2574 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002575 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002576 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002577 }
Romain Guy694b5192010-07-21 21:33:20 -07002578
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002579 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002580
2581 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002582}
2583
Chet Haase48659092012-05-31 15:21:51 -07002584status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002585 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002586 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2587 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002588 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002589 }
2590
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002591 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002592 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2593 paint->getTextSize());
2594
2595 int alpha;
2596 SkXfermode::Mode mode;
2597 getAlphaAndMode(paint, &alpha, &mode);
2598
2599 mCaches.activeTexture(0);
2600 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002601 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002602 setupDrawDirtyRegionsDisabled();
2603 setupDrawWithTexture(true);
2604 setupDrawAlpha8Color(paint->getColor(), alpha);
2605 setupDrawColorFilter();
2606 setupDrawShader();
2607 setupDrawBlending(true, mode);
2608 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002609 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002610 setupDrawTexture(fontRenderer.getTexture(true));
2611 setupDrawPureColorUniforms();
2612 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002613 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002614 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002615
Romain Guy97771732012-02-28 18:17:02 -08002616 const Rect* clip = &mSnapshot->getLocalClip();
2617 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 -08002618
Romain Guy97771732012-02-28 18:17:02 -08002619 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002620
2621 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2622 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002623 if (hasActiveLayer) {
2624 mSnapshot->transform->mapRect(bounds);
2625 dirtyLayerUnchecked(bounds, getRegion());
2626 }
Romain Guy97771732012-02-28 18:17:02 -08002627 }
Chet Haase48659092012-05-31 15:21:51 -07002628
2629 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002630}
2631
Chet Haase48659092012-05-31 15:21:51 -07002632status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2633 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002634
Romain Guya1d3c912011-12-13 14:55:06 -08002635 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002636
Romain Guy33f6beb2012-02-16 19:24:51 -08002637 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002638 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002639 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002640 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002641
Romain Guy8b55f372010-08-18 17:10:07 -07002642 const float x = texture->left - texture->offset;
2643 const float y = texture->top - texture->offset;
2644
Romain Guy01d58e42011-01-19 21:54:02 -08002645 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002646
2647 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002648}
2649
Chet Haase48659092012-05-31 15:21:51 -07002650status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002651 if (!layer) {
2652 return DrawGlInfo::kStatusDone;
2653 }
2654
2655 Rect transformed;
2656 Rect clip;
2657 const bool rejected = quickRejectNoScissor(x, y,
2658 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2659
2660 if (rejected) {
Chet Haase48659092012-05-31 15:21:51 -07002661 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002662 }
2663
Romain Guy4ff0cf42012-08-06 14:51:10 -07002664 bool debugLayerUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -07002665 if (updateLayer(layer, true)) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002666 debugLayerUpdate = mCaches.debugLayersUpdates;
Romain Guy2bf68f02012-03-02 13:37:47 -08002667 }
2668
Romain Guy35643dd2012-09-18 15:40:58 -07002669 mCaches.setScissorEnabled(!clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002670 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002671
Romain Guy211370f2012-02-01 16:10:55 -08002672 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002673 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002674 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002675 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002676 const float a = layer->getAlpha() / 255.0f;
2677 SkiaColorFilter *oldFilter = mColorFilter;
2678 mColorFilter = layer->getColorFilter();
Romain Guyf219da52011-01-16 12:54:25 -08002679
Romain Guyc88e3572011-01-22 00:32:12 -08002680 setupDraw();
2681 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002682 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002683 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002684 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002685 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002686 setupDrawPureColorUniforms();
2687 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002688 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002689 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002690 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2691 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002692
Romain Guyd21b6e12011-11-30 20:21:23 -08002693 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002694 setupDrawModelViewTranslate(tx, ty,
2695 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002696 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002697 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002698 setupDrawModelViewTranslate(x, y,
2699 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2700 }
Romain Guyc88e3572011-01-22 00:32:12 -08002701 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002702
Romain Guyc88e3572011-01-22 00:32:12 -08002703 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2704 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002705
Romain Guyc88e3572011-01-22 00:32:12 -08002706 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002707
Chet Haased15ebf22012-09-05 11:40:29 -07002708 mColorFilter = oldFilter;
2709
Romain Guy3a3133d2011-02-01 22:59:58 -08002710#if DEBUG_LAYERS_AS_REGIONS
2711 drawRegionRects(layer->region);
2712#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002713 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002714
2715 if (debugLayerUpdate) {
2716 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2717 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2718 }
Romain Guyf219da52011-01-16 12:54:25 -08002719 }
Chet Haase48659092012-05-31 15:21:51 -07002720
2721 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002722}
2723
Romain Guy6926c722010-07-12 20:20:03 -07002724///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002725// Shaders
2726///////////////////////////////////////////////////////////////////////////////
2727
2728void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002729 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002730}
2731
Romain Guy06f96e22010-07-30 19:18:16 -07002732void OpenGLRenderer::setupShader(SkiaShader* shader) {
2733 mShader = shader;
2734 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002735 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002736 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002737}
2738
Romain Guyd27977d2010-07-14 19:18:51 -07002739///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002740// Color filters
2741///////////////////////////////////////////////////////////////////////////////
2742
2743void OpenGLRenderer::resetColorFilter() {
2744 mColorFilter = NULL;
2745}
2746
2747void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2748 mColorFilter = filter;
2749}
2750
2751///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002752// Drop shadow
2753///////////////////////////////////////////////////////////////////////////////
2754
2755void OpenGLRenderer::resetShadow() {
2756 mHasShadow = false;
2757}
2758
2759void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2760 mHasShadow = true;
2761 mShadowRadius = radius;
2762 mShadowDx = dx;
2763 mShadowDy = dy;
2764 mShadowColor = color;
2765}
2766
2767///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002768// Draw filters
2769///////////////////////////////////////////////////////////////////////////////
2770
2771void OpenGLRenderer::resetPaintFilter() {
2772 mHasDrawFilter = false;
2773}
2774
2775void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2776 mHasDrawFilter = true;
2777 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2778 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2779}
2780
2781SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002782 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002783
2784 uint32_t flags = paint->getFlags();
2785
2786 mFilteredPaint = *paint;
2787 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2788
2789 return &mFilteredPaint;
2790}
2791
2792///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002793// Drawing implementation
2794///////////////////////////////////////////////////////////////////////////////
2795
Romain Guy01d58e42011-01-19 21:54:02 -08002796void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2797 float x, float y, SkPaint* paint) {
2798 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2799 return;
2800 }
2801
2802 int alpha;
2803 SkXfermode::Mode mode;
2804 getAlphaAndMode(paint, &alpha, &mode);
2805
2806 setupDraw();
2807 setupDrawWithTexture(true);
2808 setupDrawAlpha8Color(paint->getColor(), alpha);
2809 setupDrawColorFilter();
2810 setupDrawShader();
2811 setupDrawBlending(true, mode);
2812 setupDrawProgram();
2813 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2814 setupDrawTexture(texture->id);
2815 setupDrawPureColorUniforms();
2816 setupDrawColorFilterUniforms();
2817 setupDrawShaderUniforms();
2818 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2819
2820 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2821
2822 finishDrawTexture();
2823}
2824
Romain Guyf607bdc2010-09-10 19:20:06 -07002825// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002826#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2827#define kStdUnderline_Offset (1.0f / 9.0f)
2828#define kStdUnderline_Thickness (1.0f / 18.0f)
2829
2830void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2831 float x, float y, SkPaint* paint) {
2832 // Handle underline and strike-through
2833 uint32_t flags = paint->getFlags();
2834 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002835 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002836 float underlineWidth = length;
2837 // If length is > 0.0f, we already measured the text for the text alignment
2838 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002839 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002840 }
2841
Romain Guy211370f2012-02-01 16:10:55 -08002842 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002843 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002844 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002845
Raph Levien8b4072d2012-07-30 15:50:00 -07002846 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002847 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002848
Romain Guyf6834472011-01-23 13:32:12 -08002849 int linesCount = 0;
2850 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2851 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2852
2853 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002854 float points[pointsCount];
2855 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002856
2857 if (flags & SkPaint::kUnderlineText_Flag) {
2858 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002859 points[currentPoint++] = left;
2860 points[currentPoint++] = top;
2861 points[currentPoint++] = left + underlineWidth;
2862 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002863 }
2864
2865 if (flags & SkPaint::kStrikeThruText_Flag) {
2866 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002867 points[currentPoint++] = left;
2868 points[currentPoint++] = top;
2869 points[currentPoint++] = left + underlineWidth;
2870 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002871 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002872
Romain Guy726aeba2011-06-01 14:52:00 -07002873 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002874
Romain Guy726aeba2011-06-01 14:52:00 -07002875 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002876 }
2877 }
2878}
2879
Romain Guy026c5e162010-06-28 17:12:22 -07002880void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002881 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002882 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002883 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002884 color |= 0x00ffffff;
2885 }
2886
Romain Guy70ca14e2010-12-13 18:24:33 -08002887 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002888 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002889 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002890 setupDrawShader();
2891 setupDrawColorFilter();
2892 setupDrawBlending(mode);
2893 setupDrawProgram();
2894 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2895 setupDrawColorUniforms();
2896 setupDrawShaderUniforms(ignoreTransform);
2897 setupDrawColorFilterUniforms();
2898 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002899
Romain Guyc95c8d62010-09-17 15:31:32 -07002900 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2901}
2902
Romain Guy82ba8142010-07-09 13:25:56 -07002903void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002904 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002905 int alpha;
2906 SkXfermode::Mode mode;
2907 getAlphaAndMode(paint, &alpha, &mode);
2908
Romain Guyd21b6e12011-11-30 20:21:23 -08002909 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002910
Romain Guy211370f2012-02-01 16:10:55 -08002911 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002912 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2913 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2914
Romain Guyd21b6e12011-11-30 20:21:23 -08002915 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002916 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2917 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2918 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2919 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002920 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002921 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2922 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2923 GL_TRIANGLE_STRIP, gMeshCount);
2924 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002925}
2926
Romain Guybd6b79b2010-06-26 00:13:53 -07002927void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002928 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2929 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002930 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002931}
2932
2933void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002934 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002935 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002936 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002937
Romain Guy746b7402010-10-26 16:27:31 -07002938 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002939 setupDrawWithTexture();
2940 setupDrawColor(alpha, alpha, alpha, alpha);
2941 setupDrawColorFilter();
2942 setupDrawBlending(blend, mode, swapSrcDst);
2943 setupDrawProgram();
2944 if (!dirty) {
2945 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002946 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002947 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002948 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002949 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002950 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002951 }
Romain Guy86568192010-12-14 15:55:39 -08002952 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002953 setupDrawColorFilterUniforms();
2954 setupDrawTexture(texture);
2955 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002956
Romain Guy6820ac82010-09-15 18:11:50 -07002957 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002958
2959 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002960}
2961
Romain Guya5aed0d2010-09-09 14:42:43 -07002962void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002963 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002964 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002965
Romain Guy82ba8142010-07-09 13:25:56 -07002966 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002967 // These blend modes are not supported by OpenGL directly and have
2968 // to be implemented using shaders. Since the shader will perform
2969 // the blending, turn blending off here
2970 // If the blend mode cannot be implemented using shaders, fall
2971 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07002972 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08002973 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002974 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002975 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002976
Romain Guy82bc7a72012-01-03 14:13:39 -08002977 if (mCaches.blend) {
2978 glDisable(GL_BLEND);
2979 mCaches.blend = false;
2980 }
2981
2982 return;
2983 } else {
2984 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002985 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002986 }
2987
2988 if (!mCaches.blend) {
2989 glEnable(GL_BLEND);
2990 }
2991
2992 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2993 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2994
2995 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2996 glBlendFunc(sourceMode, destMode);
2997 mCaches.lastSrcMode = sourceMode;
2998 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002999 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003000 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003001 glDisable(GL_BLEND);
3002 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003003 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003004}
3005
Romain Guy889f8d12010-07-29 14:37:42 -07003006bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003007 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003008 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003009 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003010 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003011 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003012 }
Romain Guy6926c722010-07-12 20:20:03 -07003013 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003014}
3015
Romain Guy026c5e162010-06-28 17:12:22 -07003016void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003017 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003018 TextureVertex::setUV(v++, u1, v1);
3019 TextureVertex::setUV(v++, u2, v1);
3020 TextureVertex::setUV(v++, u1, v2);
3021 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003022}
3023
Chet Haase5c13d892010-10-08 08:37:55 -07003024void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003025 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003026 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003027}
3028
Romain Guy9d5316e2010-06-24 19:30:36 -07003029}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003030}; // namespace android