blob: a4403c8dc590d37ec06228aedd17b419e61ed353 [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 Guy7c450aa2012-09-21 19:15:00 -0700197 debugOverdraw(true, true);
198
Romain Guy6b7bd242010-10-06 19:49:23 -0700199 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700200 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700201 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700202 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700203 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700204 } else {
205 mCaches.resetScissor();
206 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700207
208 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700209}
210
211void OpenGLRenderer::syncState() {
212 glViewport(0, 0, mWidth, mHeight);
213
214 if (mCaches.blend) {
215 glEnable(GL_BLEND);
216 } else {
217 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700218 }
Romain Guybb9524b2010-06-22 18:56:38 -0700219}
220
Romain Guy57b52682012-09-20 17:38:46 -0700221void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700222 Rect* clip = mTilingSnapshot->clipRect;
Romain Guy2b7028e2012-09-19 17:25:38 -0700223 if (s->flags & Snapshot::kFlagIsFboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700224 clip = s->clipRect;
225 }
226
227 mCaches.startTiling(clip->left, s->height - clip->bottom,
228 clip->right - clip->left, clip->bottom - clip->top, opaque);
229}
230
231void OpenGLRenderer::endTiling() {
232 mCaches.endTiling();
233}
234
Romain Guyb025b9c2010-09-16 14:16:48 -0700235void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700236 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700237 endTiling();
238
Romain Guy11cb6422012-09-21 00:39:43 -0700239 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700240#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700241 GLenum status = GL_NO_ERROR;
242 while ((status = glGetError()) != GL_NO_ERROR) {
243 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
244 switch (status) {
245 case GL_INVALID_ENUM:
246 ALOGE(" GL_INVALID_ENUM");
247 break;
248 case GL_INVALID_VALUE:
249 ALOGE(" GL_INVALID_VALUE");
250 break;
251 case GL_INVALID_OPERATION:
252 ALOGE(" GL_INVALID_OPERATION");
253 break;
254 case GL_OUT_OF_MEMORY:
255 ALOGE(" Out of memory!");
256 break;
257 }
Romain Guya07105b2011-01-10 21:14:18 -0800258 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700259#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700260
Romain Guyc15008e2010-11-10 11:59:15 -0800261#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800262 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700263#else
264 if (mCaches.getDebugLevel() & kDebugMemory) {
265 mCaches.dumpMemoryUsage();
266 }
Romain Guyc15008e2010-11-10 11:59:15 -0800267#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700268 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700269}
270
Romain Guy7c450aa2012-09-21 19:15:00 -0700271void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
272 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
273 if (clear) {
274 mCaches.disableScissor();
275 mCaches.stencil.clear();
276 }
277 if (enable) {
278 mCaches.stencil.enableDebugWrite();
279 } else {
280 mCaches.stencil.disable();
281 }
282 }
283}
284
285void OpenGLRenderer::renderOverdraw() {
286 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
287 const Rect* clip = mTilingSnapshot->clipRect;
288
289 mCaches.enableScissor();
290 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
291 clip->right - clip->left, clip->bottom - clip->top);
292
293 mCaches.stencil.enableDebugTest(2);
294 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
295 mCaches.stencil.enableDebugTest(3);
296 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
297 mCaches.stencil.enableDebugTest(4);
298 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
299 mCaches.stencil.enableDebugTest(4, true);
300 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
301 mCaches.stencil.disable();
302 }
303}
304
Romain Guy6c319ca2011-01-11 14:29:25 -0800305void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700306 if (mCaches.currentProgram) {
307 if (mCaches.currentProgram->isInUse()) {
308 mCaches.currentProgram->remove();
309 mCaches.currentProgram = NULL;
310 }
311 }
Romain Guy50c0f092010-10-19 11:42:22 -0700312 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800313 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800314 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800315 mCaches.disbaleTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700316 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700317}
318
Romain Guy6c319ca2011-01-11 14:29:25 -0800319void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800320 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800321 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700322 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700323 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700324
Romain Guy3e263fa2011-12-12 16:47:48 -0800325 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
326
Chet Haase80250612012-08-15 13:46:54 -0700327 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700328 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800329 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700330 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700331
Romain Guya1d3c912011-12-13 14:55:06 -0800332 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700333
Romain Guy50c0f092010-10-19 11:42:22 -0700334 mCaches.blend = true;
335 glEnable(GL_BLEND);
336 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
337 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700338}
339
Romain Guy35643dd2012-09-18 15:40:58 -0700340void OpenGLRenderer::resumeAfterLayer() {
341 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
342 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
343 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700344 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700345
346 mCaches.resetScissor();
347 dirtyClip();
348}
349
Romain Guyba6be8a2012-04-23 18:22:09 -0700350void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700351 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700352}
353
354void OpenGLRenderer::attachFunctor(Functor* functor) {
355 mFunctors.add(functor);
356}
357
Romain Guy8f3b8e32012-03-27 16:33:45 -0700358status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
359 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700360 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700361
Romain Guyba6be8a2012-04-23 18:22:09 -0700362 if (count > 0) {
363 SortedVector<Functor*> functors(mFunctors);
364 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700365
Romain Guyba6be8a2012-04-23 18:22:09 -0700366 DrawGlInfo info;
367 info.clipLeft = 0;
368 info.clipTop = 0;
369 info.clipRight = 0;
370 info.clipBottom = 0;
371 info.isLayer = false;
372 info.width = 0;
373 info.height = 0;
374 memset(info.transform, 0, sizeof(float) * 16);
375
376 for (size_t i = 0; i < count; i++) {
377 Functor* f = functors.itemAt(i);
378 result |= (*f)(DrawGlInfo::kModeProcess, &info);
379
Chris Craikc2c95432012-04-25 15:13:52 -0700380 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700381 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
382 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700383 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700384
Chris Craikc2c95432012-04-25 15:13:52 -0700385 if (result & DrawGlInfo::kStatusInvoke) {
386 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700387 }
388 }
Chet Haasebeb8bd02012-09-09 16:13:26 -0700389 // protect against functors binding to other buffers
390 mCaches.unbindMeshBuffer();
391 mCaches.unbindIndicesBuffer();
392 mCaches.activeTexture(0);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700393 }
394
395 return result;
396}
397
398status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800399 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700400 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700401
Romain Guy8a4ac612012-07-17 17:32:48 -0700402 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800403 if (mDirtyClip) {
404 setScissorFromClip();
405 }
Romain Guyd643bb52011-03-01 14:55:21 -0800406
Romain Guy80911b82011-03-16 15:30:12 -0700407 Rect clip(*mSnapshot->clipRect);
408 clip.snapToPixelBoundaries();
409
Romain Guyd643bb52011-03-01 14:55:21 -0800410 // Since we don't know what the functor will draw, let's dirty
411 // tne entire clip region
412 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800413 dirtyLayerUnchecked(clip, getRegion());
414 }
Romain Guyd643bb52011-03-01 14:55:21 -0800415
Romain Guy08aa2cb2011-03-17 11:06:57 -0700416 DrawGlInfo info;
417 info.clipLeft = clip.left;
418 info.clipTop = clip.top;
419 info.clipRight = clip.right;
420 info.clipBottom = clip.bottom;
421 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700422 info.width = getSnapshot()->viewport.getWidth();
423 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700424 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700425
Chet Haase48659092012-05-31 15:21:51 -0700426 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800427
Romain Guy8f3b8e32012-03-27 16:33:45 -0700428 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700429 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800430 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700431
Chris Craik65924a32012-04-05 17:52:11 -0700432 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700433 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700434 }
Romain Guycabfcc12011-03-07 18:06:46 -0800435 }
436
Chet Haasedaf98e92011-01-10 14:10:36 -0800437 resume();
Romain Guy65549432012-03-26 16:45:05 -0700438 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800439}
440
Romain Guyf6a11b82010-06-23 17:47:49 -0700441///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700442// Layers
443///////////////////////////////////////////////////////////////////////////////
444
445bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
446 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
447 OpenGLRenderer* renderer = layer->renderer;
448 Rect& dirty = layer->dirtyRect;
449
Romain Guy7c450aa2012-09-21 19:15:00 -0700450 if (inFrame) {
451 endTiling();
452 debugOverdraw(false, false);
453 }
Romain Guy11cb6422012-09-21 00:39:43 -0700454
455 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
456 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
457 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
458 renderer->finish();
459
460 if (inFrame) {
461 resumeAfterLayer();
462 startTiling(mSnapshot);
463 }
464
465 dirty.setEmpty();
466 layer->deferredUpdateScheduled = false;
467 layer->renderer = NULL;
468 layer->displayList = NULL;
469
470 return true;
471 }
472
473 return false;
474}
475
476void OpenGLRenderer::updateLayers() {
477 int count = mLayerUpdates.size();
478 if (count > 0) {
479 startMark("Layer Updates");
480
481 // Note: it is very important to update the layers in reverse order
482 for (int i = count - 1; i >= 0; i--) {
483 Layer* layer = mLayerUpdates.itemAt(i);
484 updateLayer(layer, false);
485 mCaches.resourceCache.decrementRefcount(layer);
486 }
487 mLayerUpdates.clear();
488
489 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
490 endMark();
491 }
492}
493
494void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
495 if (layer) {
496 mLayerUpdates.push_back(layer);
497 mCaches.resourceCache.incrementRefcount(layer);
498 }
499}
500
501void OpenGLRenderer::clearLayerUpdates() {
502 size_t count = mLayerUpdates.size();
503 if (count > 0) {
504 mCaches.resourceCache.lock();
505 for (size_t i = 0; i < count; i++) {
506 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
507 }
508 mCaches.resourceCache.unlock();
509 mLayerUpdates.clear();
510 }
511}
512
513///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700514// State management
515///////////////////////////////////////////////////////////////////////////////
516
Romain Guybb9524b2010-06-22 18:56:38 -0700517int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700518 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700519}
520
521int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700522 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700523}
524
525void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700526 if (mSaveCount > 1) {
527 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700528 }
Romain Guybb9524b2010-06-22 18:56:38 -0700529}
530
531void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700532 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700533
Romain Guy8fb95422010-08-17 18:38:51 -0700534 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700535 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700536 }
Romain Guybb9524b2010-06-22 18:56:38 -0700537}
538
Romain Guy8aef54f2010-09-01 15:13:49 -0700539int OpenGLRenderer::saveSnapshot(int flags) {
540 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700541 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700542}
543
544bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700545 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700546 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700547 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700548
Romain Guybd6b79b2010-06-26 00:13:53 -0700549 sp<Snapshot> current = mSnapshot;
550 sp<Snapshot> previous = mSnapshot->previous;
551
Romain Guyeb993562010-10-05 18:14:38 -0700552 if (restoreOrtho) {
553 Rect& r = previous->viewport;
554 glViewport(r.left, r.top, r.right, r.bottom);
555 mOrthoMatrix.load(current->orthoMatrix);
556 }
557
Romain Guy8b55f372010-08-18 17:10:07 -0700558 mSaveCount--;
559 mSnapshot = previous;
560
Romain Guy2542d192010-08-18 11:47:12 -0700561 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700562 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700563 }
Romain Guy2542d192010-08-18 11:47:12 -0700564
Romain Guy5ec99242010-11-03 16:19:08 -0700565 if (restoreLayer) {
566 composeLayer(current, previous);
567 }
568
Romain Guy2542d192010-08-18 11:47:12 -0700569 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700570}
571
Romain Guyf6a11b82010-06-23 17:47:49 -0700572///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700573// Layers
574///////////////////////////////////////////////////////////////////////////////
575
576int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700577 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700578 const GLuint previousFbo = mSnapshot->fbo;
579 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700580
Romain Guyaf636eb2010-12-09 17:47:21 -0800581 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700582 int alpha = 255;
583 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700584
Romain Guye45362c2010-11-03 19:58:32 -0700585 if (p) {
586 alpha = p->getAlpha();
Chris Craik710f46d2012-09-17 17:25:49 -0700587 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700588 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700589 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700590 }
Romain Guyd55a8612010-06-28 17:42:46 -0700591
Chet Haased48885a2012-08-28 17:43:28 -0700592 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700593 }
Romain Guyd55a8612010-06-28 17:42:46 -0700594
595 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700596}
597
598int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
599 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700600 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700601 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700602 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700603 SkPaint paint;
604 paint.setAlpha(alpha);
605 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700606 }
Romain Guyd55a8612010-06-28 17:42:46 -0700607}
Romain Guybd6b79b2010-06-26 00:13:53 -0700608
Romain Guy1c740bc2010-09-13 18:00:09 -0700609/**
610 * Layers are viewed by Skia are slightly different than layers in image editing
611 * programs (for instance.) When a layer is created, previously created layers
612 * and the frame buffer still receive every drawing command. For instance, if a
613 * layer is created and a shape intersecting the bounds of the layers and the
614 * framebuffer is draw, the shape will be drawn on both (unless the layer was
615 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
616 *
617 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
618 * texture. Unfortunately, this is inefficient as it requires every primitive to
619 * be drawn n + 1 times, where n is the number of active layers. In practice this
620 * means, for every primitive:
621 * - Switch active frame buffer
622 * - Change viewport, clip and projection matrix
623 * - Issue the drawing
624 *
625 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700626 * To avoid this, layers are implemented in a different way here, at least in the
627 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
628 * is set. When this flag is set we can redirect all drawing operations into a
629 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700630 *
631 * This implementation relies on the frame buffer being at least RGBA 8888. When
632 * a layer is created, only a texture is created, not an FBO. The content of the
633 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700634 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700635 * buffer and drawing continues as normal. This technique therefore treats the
636 * frame buffer as a scratch buffer for the layers.
637 *
638 * To compose the layers back onto the frame buffer, each layer texture
639 * (containing the original frame buffer data) is drawn as a simple quad over
640 * the frame buffer. The trick is that the quad is set as the composition
641 * destination in the blending equation, and the frame buffer becomes the source
642 * of the composition.
643 *
644 * Drawing layers with an alpha value requires an extra step before composition.
645 * An empty quad is drawn over the layer's region in the frame buffer. This quad
646 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
647 * quad is used to multiply the colors in the frame buffer. This is achieved by
648 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
649 * GL_ZERO, GL_SRC_ALPHA.
650 *
651 * Because glCopyTexImage2D() can be slow, an alternative implementation might
652 * be use to draw a single clipped layer. The implementation described above
653 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700654 *
655 * (1) The frame buffer is actually not cleared right away. To allow the GPU
656 * to potentially optimize series of calls to glCopyTexImage2D, the frame
657 * buffer is left untouched until the first drawing operation. Only when
658 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700659 */
Chet Haased48885a2012-08-28 17:43:28 -0700660bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
661 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700662 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700663 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700664
Romain Guyeb993562010-10-05 18:14:38 -0700665 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
666
Romain Guyf607bdc2010-09-10 19:20:06 -0700667 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700668 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700669 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700670 Rect untransformedBounds(bounds);
671 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700672
Chet Haased48885a2012-08-28 17:43:28 -0700673 // Layers only make sense if they are in the framebuffer's bounds
674 if (bounds.intersect(*mSnapshot->clipRect)) {
675 // We cannot work with sub-pixels in this case
676 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700677
Chet Haased48885a2012-08-28 17:43:28 -0700678 // When the layer is not an FBO, we may use glCopyTexImage so we
679 // need to make sure the layer does not extend outside the bounds
680 // of the framebuffer
681 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700682 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700683 } else if (fboLayer) {
684 clip.set(bounds);
685 mat4 inverse;
686 inverse.loadInverse(*mSnapshot->transform);
687 inverse.mapRect(clip);
688 clip.snapToPixelBoundaries();
689 if (clip.intersect(untransformedBounds)) {
690 clip.translate(-left, -top);
691 bounds.set(untransformedBounds);
692 } else {
693 clip.setEmpty();
694 }
Romain Guyad37cd32011-03-15 11:12:25 -0700695 }
Chet Haased48885a2012-08-28 17:43:28 -0700696 } else {
697 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700698 }
Romain Guybf434112010-09-16 14:40:17 -0700699
Romain Guy746b7402010-10-26 16:27:31 -0700700 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700701 bounds.getHeight() > mCaches.maxTextureSize ||
702 (fboLayer && clip.isEmpty())) {
703 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700704 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700705 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700706 }
707
708 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700709 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700710 return false;
711 }
Romain Guyf18fd992010-07-08 11:45:51 -0700712
Romain Guya1d3c912011-12-13 14:55:06 -0800713 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700714 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700715 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700716 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700717 }
718
Romain Guy9ace8f52011-07-07 20:50:11 -0700719 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700720 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700721 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
722 bounds.getWidth() / float(layer->getWidth()), 0.0f);
723 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700724 layer->setBlend(true);
Romain Guydda570202010-07-06 11:39:32 -0700725
Romain Guy8fb95422010-08-17 18:38:51 -0700726 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700727 mSnapshot->flags |= Snapshot::kFlagIsLayer;
728 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700729
Romain Guyeb993562010-10-05 18:14:38 -0700730 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700731 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700732 } else {
733 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700734 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800735 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700736 if (layer->isEmpty()) {
737 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700738 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700739 layer->getWidth(), layer->getHeight(), 0);
740 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800741 } else {
742 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700743 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800744 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700745
Romain Guy54be1cd2011-06-13 19:04:27 -0700746 // Enqueue the buffer coordinates to clear the corresponding region later
747 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700748 }
Romain Guyeb993562010-10-05 18:14:38 -0700749 }
Romain Guyf86ef572010-07-01 11:05:42 -0700750
Romain Guyd55a8612010-06-28 17:42:46 -0700751 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700752}
753
Chet Haased48885a2012-08-28 17:43:28 -0700754bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700755 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700756
Chet Haased48885a2012-08-28 17:43:28 -0700757 mSnapshot->region = &mSnapshot->layer->region;
758 mSnapshot->flags |= Snapshot::kFlagFboTarget;
Romain Guy5b3b3522010-10-27 18:57:51 -0700759
Chet Haased48885a2012-08-28 17:43:28 -0700760 mSnapshot->flags |= Snapshot::kFlagIsFboLayer;
761 mSnapshot->fbo = layer->getFbo();
762 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
763 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
764 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
765 mSnapshot->height = bounds.getHeight();
766 mSnapshot->flags |= Snapshot::kFlagDirtyOrtho;
767 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700768
Romain Guy2b7028e2012-09-19 17:25:38 -0700769 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700770 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700771 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700772 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
773 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700774
775 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700776 if (layer->isEmpty()) {
777 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
778 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700779 }
780
781 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700782 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700783
Romain Guy2b7028e2012-09-19 17:25:38 -0700784 startTiling(mSnapshot);
Romain Guy5b3b3522010-10-27 18:57:51 -0700785
786 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700787 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800788 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700789 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700790 glClear(GL_COLOR_BUFFER_BIT);
791
792 dirtyClip();
793
794 // Change the ortho projection
795 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
796 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
797
798 return true;
799}
800
Romain Guy1c740bc2010-09-13 18:00:09 -0700801/**
802 * Read the documentation of createLayer() before doing anything in this method.
803 */
Romain Guy1d83e192010-08-17 11:37:00 -0700804void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
805 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000806 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700807 return;
808 }
809
Romain Guy5b3b3522010-10-27 18:57:51 -0700810 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700811
812 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700813 endTiling();
814
Romain Guye0aa84b2012-04-03 19:30:26 -0700815 // Detach the texture from the FBO
816 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guyeb993562010-10-05 18:14:38 -0700817 // Unbind current FBO and restore previous one
818 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700819 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700820
821 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700822 }
823
Romain Guy1d83e192010-08-17 11:37:00 -0700824 Layer* layer = current->layer;
825 const Rect& rect = layer->layer;
826
Romain Guy9ace8f52011-07-07 20:50:11 -0700827 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700828 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700829 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700830 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700831 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700832 }
833
Romain Guy03750a02010-10-18 14:06:08 -0700834 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700835
Romain Guya1d3c912011-12-13 14:55:06 -0800836 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700837
Romain Guy5b3b3522010-10-27 18:57:51 -0700838 // When the layer is stored in an FBO, we can save a bit of fillrate by
839 // drawing only the dirty region
840 if (fboLayer) {
841 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700842 if (layer->getColorFilter()) {
843 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800844 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700845 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700846 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800847 resetColorFilter();
848 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700849 } else if (!rect.isEmpty()) {
850 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
851 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700852 }
Romain Guy8b55f372010-08-18 17:10:07 -0700853
Romain Guyeb993562010-10-05 18:14:38 -0700854 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800855 // Note: No need to use glDiscardFramebufferEXT() since we never
856 // create/compose layers that are not on screen with this
857 // code path
858 // See LayerRenderer::destroyLayer(Layer*)
859
Romain Guyeb993562010-10-05 18:14:38 -0700860 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
861 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700862 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700863 }
864
Romain Guy746b7402010-10-26 16:27:31 -0700865 dirtyClip();
866
Romain Guyeb993562010-10-05 18:14:38 -0700867 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700868 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700869 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700870 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700871 }
872}
873
Romain Guyaa6c24c2011-04-28 18:40:04 -0700874void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700875 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700876
Romain Guy302a9df2011-08-16 13:55:02 -0700877 mat4& transform = layer->getTransform();
878 if (!transform.isIdentity()) {
879 save(0);
880 mSnapshot->transform->multiply(transform);
881 }
882
Romain Guyaa6c24c2011-04-28 18:40:04 -0700883 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700884 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700885 setupDrawWithTexture();
886 } else {
887 setupDrawWithExternalTexture();
888 }
889 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700890 setupDrawColor(alpha, alpha, alpha, alpha);
891 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700892 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700893 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700894 setupDrawPureColorUniforms();
895 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700896 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
897 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700898 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700899 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700900 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700901 if (mSnapshot->transform->isPureTranslate() &&
902 layer->getWidth() == (uint32_t) rect.getWidth() &&
903 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700904 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
905 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
906
Romain Guyd21b6e12011-11-30 20:21:23 -0800907 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700908 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
909 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800910 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700911 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
912 }
913 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700914 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
915
916 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
917
918 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700919
920 if (!transform.isIdentity()) {
921 restore();
922 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700923}
924
Romain Guy5b3b3522010-10-27 18:57:51 -0700925void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700926 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700927 const Rect& texCoords = layer->texCoords;
928 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
929 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700930
Romain Guy9ace8f52011-07-07 20:50:11 -0700931 float x = rect.left;
932 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700933 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700934 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700935 layer->getHeight() == (uint32_t) rect.getHeight();
936
937 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700938 // When we're swapping, the layer is already in screen coordinates
939 if (!swap) {
940 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
941 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
942 }
943
Romain Guyd21b6e12011-11-30 20:21:23 -0800944 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700945 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800946 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700947 }
948
949 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
950 layer->getTexture(), layer->getAlpha() / 255.0f,
951 layer->getMode(), layer->isBlend(),
952 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
953 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700954
Romain Guyaa6c24c2011-04-28 18:40:04 -0700955 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
956 } else {
957 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
958 drawTextureLayer(layer, rect);
959 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
960 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700961}
962
963void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700964 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700965 layer->setRegionAsRect();
966
Romain Guy40667672011-03-18 14:34:03 -0700967 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700968
Romain Guy5b3b3522010-10-27 18:57:51 -0700969 layer->region.clear();
970 return;
971 }
972
Romain Guy8a3957d2011-09-07 17:55:15 -0700973 // TODO: See LayerRenderer.cpp::generateMesh() for important
974 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800975 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700976 size_t count;
977 const android::Rect* rects = layer->region.getArray(&count);
978
Romain Guy9ace8f52011-07-07 20:50:11 -0700979 const float alpha = layer->getAlpha() / 255.0f;
980 const float texX = 1.0f / float(layer->getWidth());
981 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800982 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700983
984 TextureVertex* mesh = mCaches.getRegionMesh();
985 GLsizei numQuads = 0;
986
Romain Guy7230a742011-01-10 22:26:16 -0800987 setupDraw();
988 setupDrawWithTexture();
989 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800990 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700991 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800992 setupDrawProgram();
993 setupDrawDirtyRegionsDisabled();
994 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800995 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700996 setupDrawTexture(layer->getTexture());
997 if (mSnapshot->transform->isPureTranslate()) {
998 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
999 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
1000
Romain Guyd21b6e12011-11-30 20:21:23 -08001001 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001002 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1003 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001004 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001005 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1006 }
Romain Guy15bc6432011-12-13 13:11:32 -08001007 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001008
1009 for (size_t i = 0; i < count; i++) {
1010 const android::Rect* r = &rects[i];
1011
1012 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001013 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001014 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001015 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001016
1017 // TODO: Reject quads outside of the clip
1018 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1019 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1020 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1021 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1022
1023 numQuads++;
1024
1025 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1026 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1027 numQuads = 0;
1028 mesh = mCaches.getRegionMesh();
1029 }
1030 }
1031
1032 if (numQuads > 0) {
1033 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1034 }
1035
Romain Guy7230a742011-01-10 22:26:16 -08001036 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001037
1038#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001039 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001040#endif
1041
1042 layer->region.clear();
1043 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001044}
1045
Romain Guy3a3133d2011-02-01 22:59:58 -08001046void OpenGLRenderer::drawRegionRects(const Region& region) {
1047#if DEBUG_LAYERS_AS_REGIONS
1048 size_t count;
1049 const android::Rect* rects = region.getArray(&count);
1050
1051 uint32_t colors[] = {
1052 0x7fff0000, 0x7f00ff00,
1053 0x7f0000ff, 0x7fff00ff,
1054 };
1055
1056 int offset = 0;
1057 int32_t top = rects[0].top;
1058
1059 for (size_t i = 0; i < count; i++) {
1060 if (top != rects[i].top) {
1061 offset ^= 0x2;
1062 top = rects[i].top;
1063 }
1064
1065 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1066 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1067 SkXfermode::kSrcOver_Mode);
1068 }
1069#endif
1070}
1071
Romain Guy5b3b3522010-10-27 18:57:51 -07001072void OpenGLRenderer::dirtyLayer(const float left, const float top,
1073 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001074 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001075 Rect bounds(left, top, right, bottom);
1076 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001077 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001078 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001079}
1080
1081void OpenGLRenderer::dirtyLayer(const float left, const float top,
1082 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001083 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001084 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001085 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001086 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001087}
1088
1089void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001090 if (bounds.intersect(*mSnapshot->clipRect)) {
1091 bounds.snapToPixelBoundaries();
1092 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1093 if (!dirty.isEmpty()) {
1094 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001095 }
1096 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001097}
1098
Romain Guy54be1cd2011-06-13 19:04:27 -07001099void OpenGLRenderer::clearLayerRegions() {
1100 const size_t count = mLayers.size();
1101 if (count == 0) return;
1102
1103 if (!mSnapshot->isIgnored()) {
1104 // Doing several glScissor/glClear here can negatively impact
1105 // GPUs with a tiler architecture, instead we draw quads with
1106 // the Clear blending mode
1107
1108 // The list contains bounds that have already been clipped
1109 // against their initial clip rect, and the current clip
1110 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001111 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001112
1113 Vertex mesh[count * 6];
1114 Vertex* vertex = mesh;
1115
1116 for (uint32_t i = 0; i < count; i++) {
1117 Rect* bounds = mLayers.itemAt(i);
1118
1119 Vertex::set(vertex++, bounds->left, bounds->bottom);
1120 Vertex::set(vertex++, bounds->left, bounds->top);
1121 Vertex::set(vertex++, bounds->right, bounds->top);
1122 Vertex::set(vertex++, bounds->left, bounds->bottom);
1123 Vertex::set(vertex++, bounds->right, bounds->top);
1124 Vertex::set(vertex++, bounds->right, bounds->bottom);
1125
1126 delete bounds;
1127 }
1128
1129 setupDraw(false);
1130 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1131 setupDrawBlending(true, SkXfermode::kClear_Mode);
1132 setupDrawProgram();
1133 setupDrawPureColorUniforms();
1134 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001135 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001136
Romain Guy54be1cd2011-06-13 19:04:27 -07001137 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001138
1139 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001140 } else {
1141 for (uint32_t i = 0; i < count; i++) {
1142 delete mLayers.itemAt(i);
1143 }
1144 }
1145
1146 mLayers.clear();
1147}
1148
Romain Guybd6b79b2010-06-26 00:13:53 -07001149///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001150// Transforms
1151///////////////////////////////////////////////////////////////////////////////
1152
1153void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001154 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001155}
1156
1157void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001158 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001159}
1160
1161void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001162 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001163}
1164
Romain Guy807daf72011-01-18 11:19:19 -08001165void OpenGLRenderer::skew(float sx, float sy) {
1166 mSnapshot->transform->skew(sx, sy);
1167}
1168
Romain Guyf6a11b82010-06-23 17:47:49 -07001169void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001170 if (matrix) {
1171 mSnapshot->transform->load(*matrix);
1172 } else {
1173 mSnapshot->transform->loadIdentity();
1174 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001175}
1176
1177void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001178 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001179}
1180
1181void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001182 SkMatrix transform;
1183 mSnapshot->transform->copyTo(transform);
1184 transform.preConcat(*matrix);
1185 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001186}
1187
1188///////////////////////////////////////////////////////////////////////////////
1189// Clipping
1190///////////////////////////////////////////////////////////////////////////////
1191
Romain Guybb9524b2010-06-22 18:56:38 -07001192void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001193 Rect clip(*mSnapshot->clipRect);
1194 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001195
Romain Guy8a4ac612012-07-17 17:32:48 -07001196 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1197 clip.getWidth(), clip.getHeight())) {
1198 mDirtyClip = false;
1199 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001200}
1201
1202const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001203 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001204}
1205
Romain Guy8a4ac612012-07-17 17:32:48 -07001206bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1207 if (mSnapshot->isIgnored()) {
1208 return true;
1209 }
1210
1211 Rect r(left, top, right, bottom);
1212 mSnapshot->transform->mapRect(r);
1213 r.snapToPixelBoundaries();
1214
1215 Rect clipRect(*mSnapshot->clipRect);
1216 clipRect.snapToPixelBoundaries();
1217
1218 return !clipRect.intersects(r);
1219}
1220
Romain Guy35643dd2012-09-18 15:40:58 -07001221bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1222 Rect& transformed, Rect& clip) {
1223 if (mSnapshot->isIgnored()) {
1224 return true;
1225 }
1226
1227 transformed.set(left, top, right, bottom);
1228 mSnapshot->transform->mapRect(transformed);
1229 transformed.snapToPixelBoundaries();
1230
1231 clip.set(*mSnapshot->clipRect);
1232 clip.snapToPixelBoundaries();
1233
1234 return !clip.intersects(transformed);
1235}
1236
Romain Guyc7d53492010-06-25 13:41:57 -07001237bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001238 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001239 return true;
1240 }
1241
Romain Guy1d83e192010-08-17 11:37:00 -07001242 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001243 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001244 r.snapToPixelBoundaries();
1245
1246 Rect clipRect(*mSnapshot->clipRect);
1247 clipRect.snapToPixelBoundaries();
1248
Romain Guy586cae32012-07-13 15:28:31 -07001249 bool rejected = !clipRect.intersects(r);
1250 if (!isDeferred() && !rejected) {
1251 mCaches.setScissorEnabled(!clipRect.contains(r));
1252 }
1253
1254 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001255}
1256
Romain Guy079ba2c2010-07-16 14:12:24 -07001257bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1258 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001259 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001260 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001261 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001262 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001263}
1264
Chet Haasea23eed82012-04-12 15:19:04 -07001265Rect* OpenGLRenderer::getClipRect() {
1266 return mSnapshot->clipRect;
1267}
1268
Romain Guyf6a11b82010-06-23 17:47:49 -07001269///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001270// Drawing commands
1271///////////////////////////////////////////////////////////////////////////////
1272
Romain Guy54be1cd2011-06-13 19:04:27 -07001273void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001274 // TODO: It would be best if we could do this before quickReject()
1275 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001276 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001277 if (mDirtyClip) {
1278 setScissorFromClip();
1279 }
1280 mDescription.reset();
1281 mSetShaderColor = false;
1282 mColorSet = false;
1283 mColorA = mColorR = mColorG = mColorB = 0.0f;
1284 mTextureUnit = 0;
1285 mTrackDirtyRegions = true;
1286}
1287
1288void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1289 mDescription.hasTexture = true;
1290 mDescription.hasAlpha8Texture = isAlpha8;
1291}
1292
Romain Guyaa6c24c2011-04-28 18:40:04 -07001293void OpenGLRenderer::setupDrawWithExternalTexture() {
1294 mDescription.hasExternalTexture = true;
1295}
1296
Romain Guy15bc6432011-12-13 13:11:32 -08001297void OpenGLRenderer::setupDrawNoTexture() {
1298 mCaches.disbaleTexCoordsVertexArray();
1299}
1300
Chris Craik710f46d2012-09-17 17:25:49 -07001301void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001302 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001303}
1304
Chris Craik710f46d2012-09-17 17:25:49 -07001305void OpenGLRenderer::setupDrawVertexShape() {
1306 mDescription.isVertexShape = true;
Chris Craik6ebdc112012-08-31 18:24:33 -07001307}
1308
Romain Guyed6fcb02011-03-21 13:11:28 -07001309void OpenGLRenderer::setupDrawPoint(float pointSize) {
1310 mDescription.isPoint = true;
1311 mDescription.pointSize = pointSize;
1312}
1313
Romain Guy70ca14e2010-12-13 18:24:33 -08001314void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001315 setupDrawColor(color, (color >> 24) & 0xFF);
1316}
1317
1318void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1319 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001320 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1321 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001322 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001323 mColorR = a * ((color >> 16) & 0xFF);
1324 mColorG = a * ((color >> 8) & 0xFF);
1325 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001326 mColorSet = true;
1327 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1328}
1329
Romain Guy86568192010-12-14 15:55:39 -08001330void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1331 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001332 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1333 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001334 const float a = mColorA / 255.0f;
1335 mColorR = a * ((color >> 16) & 0xFF);
1336 mColorG = a * ((color >> 8) & 0xFF);
1337 mColorB = a * ((color ) & 0xFF);
1338 mColorSet = true;
1339 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1340}
1341
Romain Guy41210632012-07-16 17:04:24 -07001342void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1343 mCaches.fontRenderer->describe(mDescription, paint);
1344}
1345
Romain Guy70ca14e2010-12-13 18:24:33 -08001346void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1347 mColorA = a;
1348 mColorR = r;
1349 mColorG = g;
1350 mColorB = b;
1351 mColorSet = true;
1352 mSetShaderColor = mDescription.setColor(r, g, b, a);
1353}
1354
1355void OpenGLRenderer::setupDrawShader() {
1356 if (mShader) {
1357 mShader->describe(mDescription, mCaches.extensions);
1358 }
1359}
1360
1361void OpenGLRenderer::setupDrawColorFilter() {
1362 if (mColorFilter) {
1363 mColorFilter->describe(mDescription, mCaches.extensions);
1364 }
1365}
1366
Romain Guyf09ef512011-05-27 11:43:46 -07001367void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1368 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1369 mColorA = 1.0f;
1370 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001371 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001372 }
1373}
1374
Romain Guy70ca14e2010-12-13 18:24:33 -08001375void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001376 // When the blending mode is kClear_Mode, we need to use a modulate color
1377 // argb=1,0,0,0
1378 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001379 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1380 mDescription, swapSrcDst);
1381}
1382
1383void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001384 // When the blending mode is kClear_Mode, we need to use a modulate color
1385 // argb=1,0,0,0
1386 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001387 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1388 mDescription, swapSrcDst);
1389}
1390
1391void OpenGLRenderer::setupDrawProgram() {
1392 useProgram(mCaches.programCache.get(mDescription));
1393}
1394
1395void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1396 mTrackDirtyRegions = false;
1397}
1398
1399void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1400 bool ignoreTransform) {
1401 mModelView.loadTranslate(left, top, 0.0f);
1402 if (!ignoreTransform) {
1403 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1404 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1405 } else {
1406 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1407 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1408 }
1409}
1410
Chet Haase8a5cc922011-04-26 07:28:09 -07001411void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1412 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001413}
1414
Romain Guy70ca14e2010-12-13 18:24:33 -08001415void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1416 bool ignoreTransform, bool ignoreModelView) {
1417 if (!ignoreModelView) {
1418 mModelView.loadTranslate(left, top, 0.0f);
1419 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001420 } else {
1421 mModelView.loadIdentity();
1422 }
Romain Guy86568192010-12-14 15:55:39 -08001423 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1424 if (!ignoreTransform) {
1425 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1426 if (mTrackDirtyRegions && dirty) {
1427 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1428 }
1429 } else {
1430 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1431 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1432 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001433}
1434
Romain Guyed6fcb02011-03-21 13:11:28 -07001435void OpenGLRenderer::setupDrawPointUniforms() {
1436 int slot = mCaches.currentProgram->getUniform("pointSize");
1437 glUniform1f(slot, mDescription.pointSize);
1438}
1439
Romain Guy70ca14e2010-12-13 18:24:33 -08001440void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001441 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001442 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1443 }
1444}
1445
Romain Guy86568192010-12-14 15:55:39 -08001446void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001447 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001448 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001449 }
1450}
1451
Romain Guy70ca14e2010-12-13 18:24:33 -08001452void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1453 if (mShader) {
1454 if (ignoreTransform) {
1455 mModelView.loadInverse(*mSnapshot->transform);
1456 }
1457 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1458 }
1459}
1460
Romain Guy8d0d4782010-12-14 20:13:35 -08001461void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1462 if (mShader) {
1463 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1464 }
1465}
1466
Romain Guy70ca14e2010-12-13 18:24:33 -08001467void OpenGLRenderer::setupDrawColorFilterUniforms() {
1468 if (mColorFilter) {
1469 mColorFilter->setupProgram(mCaches.currentProgram);
1470 }
1471}
1472
Romain Guy41210632012-07-16 17:04:24 -07001473void OpenGLRenderer::setupDrawTextGammaUniforms() {
1474 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1475}
1476
Romain Guy70ca14e2010-12-13 18:24:33 -08001477void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001478 bool force = mCaches.bindMeshBuffer();
1479 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001480 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001481}
1482
1483void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1484 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001485 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001486 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001487}
1488
Romain Guyaa6c24c2011-04-28 18:40:04 -07001489void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1490 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001491 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001492 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001493}
1494
Romain Guy8f0095c2011-05-02 17:24:22 -07001495void OpenGLRenderer::setupDrawTextureTransform() {
1496 mDescription.hasTextureTransform = true;
1497}
1498
1499void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001500 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1501 GL_FALSE, &transform.data[0]);
1502}
1503
Romain Guy70ca14e2010-12-13 18:24:33 -08001504void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001505 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001506 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001507 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001508 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001509 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001510 }
Romain Guyd71dd362011-12-12 19:03:35 -08001511
Romain Guyf3a910b42011-12-12 20:35:21 -08001512 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001513 if (mCaches.currentProgram->texCoords >= 0) {
1514 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1515 }
1516
1517 mCaches.unbindIndicesBuffer();
1518}
1519
1520void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1521 bool force = mCaches.unbindMeshBuffer();
1522 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1523 if (mCaches.currentProgram->texCoords >= 0) {
1524 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001525 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001526}
1527
Chet Haase5b0200b2011-04-13 17:58:08 -07001528void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001529 bool force = mCaches.unbindMeshBuffer();
1530 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1531 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001532 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001533}
1534
1535/**
1536 * 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 -07001537 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1538 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1539 * attributes (one per vertex) are values from zero to one that tells the fragment
1540 * shader where the fragment is in relation to the line width/length overall; these values are
1541 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1542 * region of the line.
1543 * Note that we only pass down the width values in this setup function. The length coordinates
1544 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001545 */
Chet Haase99585ad2011-05-02 15:00:16 -07001546void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001547 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001548 bool force = mCaches.unbindMeshBuffer();
1549 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1550 vertices, gAAVertexStride);
1551 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001552 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001553
Romain Guy7b631422012-04-04 11:38:54 -07001554 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001555 glEnableVertexAttribArray(widthSlot);
1556 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001557
Romain Guy7b631422012-04-04 11:38:54 -07001558 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001559 glEnableVertexAttribArray(lengthSlot);
1560 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001561
Chet Haase5b0200b2011-04-13 17:58:08 -07001562 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001563 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001564}
1565
1566void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1567 glDisableVertexAttribArray(widthSlot);
1568 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001569}
1570
Romain Guy70ca14e2010-12-13 18:24:33 -08001571void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001572}
1573
1574///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001575// Drawing
1576///////////////////////////////////////////////////////////////////////////////
1577
Chet Haase1271e2c2012-04-20 09:54:27 -07001578status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001579 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001580
Romain Guy0fe478e2010-11-08 12:08:41 -08001581 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1582 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001583 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001584 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001585 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001586
Romain Guy65549432012-03-26 16:45:05 -07001587 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001588}
1589
Chet Haaseed30fd82011-04-22 16:18:45 -07001590void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1591 if (displayList) {
1592 displayList->output(*this, level);
1593 }
1594}
1595
Romain Guya168d732011-03-18 16:50:13 -07001596void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1597 int alpha;
1598 SkXfermode::Mode mode;
1599 getAlphaAndMode(paint, &alpha, &mode);
1600
Romain Guya168d732011-03-18 16:50:13 -07001601 float x = left;
1602 float y = top;
1603
Romain Guye3c26852011-07-25 16:36:01 -07001604 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001605 bool ignoreTransform = false;
1606 if (mSnapshot->transform->isPureTranslate()) {
1607 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1608 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1609 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001610 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001611 } else {
1612 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001613 }
1614
1615 setupDraw();
1616 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001617 if (paint) {
1618 setupDrawAlpha8Color(paint->getColor(), alpha);
1619 }
Romain Guya168d732011-03-18 16:50:13 -07001620 setupDrawColorFilter();
1621 setupDrawShader();
1622 setupDrawBlending(true, mode);
1623 setupDrawProgram();
1624 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001625
Romain Guya168d732011-03-18 16:50:13 -07001626 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001627 texture->setWrap(GL_CLAMP_TO_EDGE);
1628 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001629
Romain Guya168d732011-03-18 16:50:13 -07001630 setupDrawPureColorUniforms();
1631 setupDrawColorFilterUniforms();
1632 setupDrawShaderUniforms();
1633 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1634
1635 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1636
1637 finishDrawTexture();
1638}
1639
Chet Haase48659092012-05-31 15:21:51 -07001640status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001641 const float right = left + bitmap->width();
1642 const float bottom = top + bitmap->height();
1643
1644 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001645 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001646 }
1647
Romain Guya1d3c912011-12-13 14:55:06 -08001648 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001649 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001650 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001651 const AutoTexture autoCleanup(texture);
1652
Romain Guy211370f2012-02-01 16:10:55 -08001653 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001654 drawAlphaBitmap(texture, left, top, paint);
1655 } else {
1656 drawTextureRect(left, top, right, bottom, texture, paint);
1657 }
Chet Haase48659092012-05-31 15:21:51 -07001658
1659 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001660}
1661
Chet Haase48659092012-05-31 15:21:51 -07001662status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001663 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1664 const mat4 transform(*matrix);
1665 transform.mapRect(r);
1666
Romain Guy6926c722010-07-12 20:20:03 -07001667 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001668 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001669 }
1670
Romain Guya1d3c912011-12-13 14:55:06 -08001671 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001672 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001673 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001674 const AutoTexture autoCleanup(texture);
1675
Romain Guy5b3b3522010-10-27 18:57:51 -07001676 // This could be done in a cheaper way, all we need is pass the matrix
1677 // to the vertex shader. The save/restore is a bit overkill.
1678 save(SkCanvas::kMatrix_SaveFlag);
1679 concatMatrix(matrix);
1680 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1681 restore();
Chet Haase48659092012-05-31 15:21:51 -07001682
1683 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001684}
1685
Chet Haase48659092012-05-31 15:21:51 -07001686status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001687 const float right = left + bitmap->width();
1688 const float bottom = top + bitmap->height();
1689
1690 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001691 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001692 }
1693
1694 mCaches.activeTexture(0);
1695 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1696 const AutoTexture autoCleanup(texture);
1697
1698 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001699
1700 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001701}
1702
Chet Haase48659092012-05-31 15:21:51 -07001703status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001704 float* vertices, int* colors, SkPaint* paint) {
1705 // TODO: Do a quickReject
1706 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001707 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001708 }
1709
Romain Guya1d3c912011-12-13 14:55:06 -08001710 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001711 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001712 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001713 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001714
Romain Guyd21b6e12011-11-30 20:21:23 -08001715 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1716 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001717
1718 int alpha;
1719 SkXfermode::Mode mode;
1720 getAlphaAndMode(paint, &alpha, &mode);
1721
Romain Guy5a7b4662011-01-20 19:09:30 -08001722 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001723
Romain Guyb18d2d02011-02-10 15:52:54 -08001724 float left = FLT_MAX;
1725 float top = FLT_MAX;
1726 float right = FLT_MIN;
1727 float bottom = FLT_MIN;
1728
Romain Guy211370f2012-02-01 16:10:55 -08001729 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001730
Romain Guya566b7c2011-01-23 16:36:11 -08001731 // TODO: Support the colors array
1732 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001733 TextureVertex* vertex = mesh;
1734 for (int32_t y = 0; y < meshHeight; y++) {
1735 for (int32_t x = 0; x < meshWidth; x++) {
1736 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1737
1738 float u1 = float(x) / meshWidth;
1739 float u2 = float(x + 1) / meshWidth;
1740 float v1 = float(y) / meshHeight;
1741 float v2 = float(y + 1) / meshHeight;
1742
1743 int ax = i + (meshWidth + 1) * 2;
1744 int ay = ax + 1;
1745 int bx = i;
1746 int by = bx + 1;
1747 int cx = i + 2;
1748 int cy = cx + 1;
1749 int dx = i + (meshWidth + 1) * 2 + 2;
1750 int dy = dx + 1;
1751
1752 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1753 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1754 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1755
1756 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1757 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1758 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001759
Romain Guyb18d2d02011-02-10 15:52:54 -08001760 if (hasActiveLayer) {
1761 // TODO: This could be optimized to avoid unnecessary ops
1762 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1763 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1764 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1765 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1766 }
Romain Guy5a7b4662011-01-20 19:09:30 -08001767 }
1768 }
1769
Romain Guyb18d2d02011-02-10 15:52:54 -08001770 if (hasActiveLayer) {
1771 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1772 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001773
Romain Guy5a7b4662011-01-20 19:09:30 -08001774 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1775 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001776 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001777
1778 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001779}
1780
Chet Haase48659092012-05-31 15:21:51 -07001781status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001782 float srcLeft, float srcTop, float srcRight, float srcBottom,
1783 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001784 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001785 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001786 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001787 }
1788
Romain Guya1d3c912011-12-13 14:55:06 -08001789 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001790 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001791 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001792 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001793
Romain Guy8ba548f2010-06-30 19:21:21 -07001794 const float width = texture->width;
1795 const float height = texture->height;
1796
Romain Guy68169722011-08-22 17:33:33 -07001797 const float u1 = fmax(0.0f, srcLeft / width);
1798 const float v1 = fmax(0.0f, srcTop / height);
1799 const float u2 = fmin(1.0f, srcRight / width);
1800 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001801
Romain Guy03750a02010-10-18 14:06:08 -07001802 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001803 resetDrawTextureTexCoords(u1, v1, u2, v2);
1804
Romain Guy03750a02010-10-18 14:06:08 -07001805 int alpha;
1806 SkXfermode::Mode mode;
1807 getAlphaAndMode(paint, &alpha, &mode);
1808
Romain Guyd21b6e12011-11-30 20:21:23 -08001809 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1810
Romain Guy211370f2012-02-01 16:10:55 -08001811 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001812 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1813 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1814
Romain Guyb5014982011-07-28 15:39:12 -07001815 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001816 // Enable linear filtering if the source rectangle is scaled
1817 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001818 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001819 }
Romain Guyb5014982011-07-28 15:39:12 -07001820
Romain Guyd21b6e12011-11-30 20:21:23 -08001821 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001822 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1823 texture->id, alpha / 255.0f, mode, texture->blend,
1824 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1825 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1826 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001827 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001828 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1829 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1830 GL_TRIANGLE_STRIP, gMeshCount);
1831 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001832
1833 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001834
1835 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001836}
1837
Chet Haase48659092012-05-31 15:21:51 -07001838status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001839 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001840 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001841 int alpha;
1842 SkXfermode::Mode mode;
1843 getAlphaAndModeDirect(paint, &alpha, &mode);
1844
1845 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1846 left, top, right, bottom, alpha, mode);
1847}
1848
1849status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1850 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1851 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001852 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001853 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001854 }
1855
Romain Guybe6f9dc2012-07-16 12:41:17 -07001856 alpha *= mSnapshot->alpha;
1857
Romain Guya1d3c912011-12-13 14:55:06 -08001858 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001859 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001860 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001861 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001862 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1863 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001864
Romain Guy2728f962010-10-08 18:36:15 -07001865 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001866 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001867
Romain Guy211370f2012-02-01 16:10:55 -08001868 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001869 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001870 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001871 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001872 const float offsetX = left + mSnapshot->transform->getTranslateX();
1873 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001874 const size_t count = mesh->quads.size();
1875 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001876 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001877 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001878 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1879 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1880 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001881 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001882 dirtyLayer(left + bounds.left, top + bounds.top,
1883 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001884 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001885 }
1886 }
1887
Romain Guy211370f2012-02-01 16:10:55 -08001888 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001889 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1890 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1891
1892 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1893 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1894 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1895 true, !mesh->hasEmptyQuads);
1896 } else {
1897 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1898 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1899 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1900 true, !mesh->hasEmptyQuads);
1901 }
Romain Guy054dc182010-10-15 17:55:25 -07001902 }
Chet Haase48659092012-05-31 15:21:51 -07001903
1904 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001905}
1906
Chet Haase99ecdc42011-05-06 12:06:34 -07001907/**
Chet Haase858aa932011-05-12 09:06:00 -07001908 * This function uses a similar approach to that of AA lines in the drawLines() function.
Chris Craik6ebdc112012-08-31 18:24:33 -07001909 * We expand the rectangle by a half pixel in screen space on all sides. However, instead of using
1910 * a fragment shader to compute the translucency of the color from its position, we simply use a
1911 * varying parameter to define how far a given pixel is into the region.
Chet Haase858aa932011-05-12 09:06:00 -07001912 */
Chris Craik710f46d2012-09-17 17:25:49 -07001913void OpenGLRenderer::drawConvexPath(const SkPath& path, int color, SkXfermode::Mode mode, bool isAA) {
1914 VertexBuffer vertexBuffer;
1915 // TODO: try clipping large paths to viewport
1916 PathRenderer::convexPathFillVertices(path, mSnapshot->transform, vertexBuffer, isAA);
Romain Guy04299382012-07-18 17:15:41 -07001917
Chris Craik710f46d2012-09-17 17:25:49 -07001918 setupDraw();
1919 setupDrawNoTexture();
1920 if (isAA) setupDrawAA();
1921 setupDrawVertexShape();
1922 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1923 setupDrawColorFilter();
1924 setupDrawShader();
1925 setupDrawBlending(isAA, mode);
1926 setupDrawProgram();
1927 setupDrawModelViewIdentity(true);
1928 setupDrawColorUniforms();
1929 setupDrawColorFilterUniforms();
1930 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07001931
Chris Craik710f46d2012-09-17 17:25:49 -07001932 void* vertices = vertexBuffer.getBuffer();
1933 bool force = mCaches.unbindMeshBuffer();
1934 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1935 vertices, isAA ? gAlphaVertexStride : gVertexStride);
1936 mCaches.resetTexCoordsVertexPointer();
1937 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07001938
Chris Craik710f46d2012-09-17 17:25:49 -07001939 int alphaSlot = -1;
1940 if (isAA) {
1941 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
1942 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07001943
Chris Craik710f46d2012-09-17 17:25:49 -07001944 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07001945 glEnableVertexAttribArray(alphaSlot);
1946 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07001947 }
Romain Guy04299382012-07-18 17:15:41 -07001948
Chris Craik710f46d2012-09-17 17:25:49 -07001949 SkRect bounds = path.getBounds();
1950 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy04299382012-07-18 17:15:41 -07001951
Chris Craik710f46d2012-09-17 17:25:49 -07001952 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07001953
Chris Craik710f46d2012-09-17 17:25:49 -07001954 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07001955 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07001956 }
Chet Haase858aa932011-05-12 09:06:00 -07001957}
1958
1959/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001960 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1961 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1962 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1963 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1964 * of the line. Hairlines are more involved because we need to account for transform scaling
1965 * to end up with a one-pixel-wide line in screen space..
1966 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1967 * in combination with values that we calculate and pass down in this method. The basic approach
1968 * is that the quad we create contains both the core line area plus a bounding area in which
1969 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1970 * proportion of the width and the length of a given segment is represented by the boundary
1971 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1972 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1973 * on the inside). This ends up giving the result we want, with pixels that are completely
1974 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1975 * how far into the boundary region they are, which is determined by shader interpolation.
1976 */
Chet Haase48659092012-05-31 15:21:51 -07001977status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1978 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07001979
1980 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001981 // We use half the stroke width here because we're going to position the quad
1982 // corner vertices half of the width away from the line endpoints
1983 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001984 // A stroke width of 0 has a special meaning in Skia:
1985 // it draws a line 1 px wide regardless of current transform
1986 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001987
Chet Haase99ecdc42011-05-06 12:06:34 -07001988 float inverseScaleX = 1.0f;
1989 float inverseScaleY = 1.0f;
1990 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001991
Chet Haase8a5cc922011-04-26 07:28:09 -07001992 int alpha;
1993 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001994
Chet Haase8a5cc922011-04-26 07:28:09 -07001995 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001996 int verticesCount = count;
1997 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001998 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001999 verticesCount += (count - 4);
2000 }
2001
2002 if (isHairLine || isAA) {
2003 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
2004 // the line on the screen should always be one pixel wide regardless of scale. For
2005 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08002006 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07002007 Matrix4 *mat = mSnapshot->transform;
2008 float m00 = mat->data[Matrix4::kScaleX];
2009 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07002010 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07002011 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07002012
Romain Guy211370f2012-02-01 16:10:55 -08002013 float scaleX = sqrtf(m00 * m00 + m01 * m01);
2014 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07002015
Chet Haase99ecdc42011-05-06 12:06:34 -07002016 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
2017 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07002018
Chet Haase99ecdc42011-05-06 12:06:34 -07002019 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
2020 scaled = true;
2021 }
2022 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002023 }
Chet Haase8a5cc922011-04-26 07:28:09 -07002024
2025 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07002026
2027 mCaches.enableScissor();
2028
Chet Haase8a5cc922011-04-26 07:28:09 -07002029 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002030 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002031 if (isAA) {
Chris Craik710f46d2012-09-17 17:25:49 -07002032 setupDrawAA();
Chet Haase8a5cc922011-04-26 07:28:09 -07002033 }
2034 setupDrawColor(paint->getColor(), alpha);
2035 setupDrawColorFilter();
2036 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08002037 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07002038 setupDrawProgram();
2039 setupDrawModelViewIdentity(true);
2040 setupDrawColorUniforms();
2041 setupDrawColorFilterUniforms();
2042 setupDrawShaderIdentityUniforms();
2043
2044 if (isHairLine) {
2045 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07002046 halfStrokeWidth = isAA? 1 : .5;
2047 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002048 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07002049 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07002050 }
Romain Guy7b631422012-04-04 11:38:54 -07002051
2052 int widthSlot;
2053 int lengthSlot;
2054
Chet Haase5b0200b2011-04-13 17:58:08 -07002055 Vertex lines[verticesCount];
2056 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002057
Chet Haase99585ad2011-05-02 15:00:16 -07002058 AAVertex wLines[verticesCount];
2059 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002060
Romain Guy211370f2012-02-01 16:10:55 -08002061 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002062 setupDrawVertices(vertices);
2063 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002064 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2065 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002066 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002067 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2068 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002069 // We will need to calculate the actual width proportion on each segment for
2070 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002071 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002072 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2073 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002074 }
2075
Chet Haase99ecdc42011-05-06 12:06:34 -07002076 AAVertex* prevAAVertex = NULL;
2077 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002078
Chet Haase99585ad2011-05-02 15:00:16 -07002079 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002080 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002081
Chet Haase5b0200b2011-04-13 17:58:08 -07002082 for (int i = 0; i < count; i += 4) {
2083 // a = start point, b = end point
2084 vec2 a(points[i], points[i + 1]);
2085 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002086
Chet Haase99585ad2011-05-02 15:00:16 -07002087 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002088 float boundaryLengthProportion = 0;
2089 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002090
Chet Haase5b0200b2011-04-13 17:58:08 -07002091 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002092 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002093 float x = n.x;
2094 n.x = -n.y;
2095 n.y = x;
2096
Chet Haase8a5cc922011-04-26 07:28:09 -07002097 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002098 if (isAA) {
2099 float wideningFactor;
2100 if (fabs(n.x) >= fabs(n.y)) {
2101 wideningFactor = fabs(1.0f / n.x);
2102 } else {
2103 wideningFactor = fabs(1.0f / n.y);
2104 }
2105 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002106 }
Romain Guy7b631422012-04-04 11:38:54 -07002107
Chet Haase99ecdc42011-05-06 12:06:34 -07002108 if (scaled) {
2109 n.x *= inverseScaleX;
2110 n.y *= inverseScaleY;
2111 }
2112 } else if (scaled) {
2113 // Extend n by .5 pixel on each side, post-transform
2114 vec2 extendedN = n.copyNormalized();
2115 extendedN /= 2;
2116 extendedN.x *= inverseScaleX;
2117 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002118
Chet Haase99ecdc42011-05-06 12:06:34 -07002119 float extendedNLength = extendedN.length();
2120 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002121 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002122 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002123 }
Romain Guy7b631422012-04-04 11:38:54 -07002124
Chet Haase99585ad2011-05-02 15:00:16 -07002125 // aa lines expand the endpoint vertices to encompass the AA boundary
2126 if (isAA) {
2127 vec2 abVector = (b - a);
2128 length = abVector.length();
2129 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002130
Chet Haase99ecdc42011-05-06 12:06:34 -07002131 if (scaled) {
2132 abVector.x *= inverseScaleX;
2133 abVector.y *= inverseScaleY;
2134 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002135 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002136 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002137 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002138 }
Romain Guy7b631422012-04-04 11:38:54 -07002139
Chet Haase99ecdc42011-05-06 12:06:34 -07002140 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002141 a -= abVector;
2142 b += abVector;
2143 }
2144
Chet Haase5b0200b2011-04-13 17:58:08 -07002145 // Four corners of the rectangle defining a thick line
2146 vec2 p1 = a - n;
2147 vec2 p2 = a + n;
2148 vec2 p3 = b + n;
2149 vec2 p4 = b - n;
2150
Chet Haase99585ad2011-05-02 15:00:16 -07002151
Chet Haase5b0200b2011-04-13 17:58:08 -07002152 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2153 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2154 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2155 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2156
Romain Guy04299382012-07-18 17:15:41 -07002157 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002158 if (!isAA) {
2159 if (prevVertex != NULL) {
2160 // Issue two repeat vertices to create degenerate triangles to bridge
2161 // between the previous line and the new one. This is necessary because
2162 // we are creating a single triangle_strip which will contain
2163 // potentially discontinuous line segments.
2164 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2165 Vertex::set(vertices++, p1.x, p1.y);
2166 generatedVerticesCount += 2;
2167 }
Romain Guy7b631422012-04-04 11:38:54 -07002168
Chet Haase5b0200b2011-04-13 17:58:08 -07002169 Vertex::set(vertices++, p1.x, p1.y);
2170 Vertex::set(vertices++, p2.x, p2.y);
2171 Vertex::set(vertices++, p4.x, p4.y);
2172 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002173
Chet Haase5b0200b2011-04-13 17:58:08 -07002174 prevVertex = vertices - 1;
2175 generatedVerticesCount += 4;
2176 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002177 if (!isHairLine && scaled) {
2178 // Must set width proportions per-segment for scaled non-hairlines to use the
2179 // correct AA boundary dimensions
2180 if (boundaryWidthSlot < 0) {
2181 boundaryWidthSlot =
2182 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002183 }
Romain Guy7b631422012-04-04 11:38:54 -07002184
Chet Haase99ecdc42011-05-06 12:06:34 -07002185 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002186 }
Romain Guy7b631422012-04-04 11:38:54 -07002187
Chet Haase99585ad2011-05-02 15:00:16 -07002188 if (boundaryLengthSlot < 0) {
2189 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002190 }
Romain Guy7b631422012-04-04 11:38:54 -07002191
Chet Haase99ecdc42011-05-06 12:06:34 -07002192 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002193
Chet Haase5b0200b2011-04-13 17:58:08 -07002194 if (prevAAVertex != NULL) {
2195 // Issue two repeat vertices to create degenerate triangles to bridge
2196 // between the previous line and the new one. This is necessary because
2197 // we are creating a single triangle_strip which will contain
2198 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002199 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2200 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2201 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002202 generatedVerticesCount += 2;
2203 }
Romain Guy7b631422012-04-04 11:38:54 -07002204
Chet Haase99585ad2011-05-02 15:00:16 -07002205 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2206 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2207 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2208 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002209
Chet Haase5b0200b2011-04-13 17:58:08 -07002210 prevAAVertex = aaVertices - 1;
2211 generatedVerticesCount += 4;
2212 }
Romain Guy7b631422012-04-04 11:38:54 -07002213
Chet Haase99585ad2011-05-02 15:00:16 -07002214 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2215 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2216 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002217 }
2218 }
Romain Guy7b631422012-04-04 11:38:54 -07002219
Chet Haase5b0200b2011-04-13 17:58:08 -07002220 if (generatedVerticesCount > 0) {
2221 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2222 }
Romain Guy7b631422012-04-04 11:38:54 -07002223
2224 if (isAA) {
2225 finishDrawAALine(widthSlot, lengthSlot);
2226 }
Chet Haase48659092012-05-31 15:21:51 -07002227
2228 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002229}
2230
Chet Haase48659092012-05-31 15:21:51 -07002231status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2232 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002233
2234 // TODO: The paint's cap style defines whether the points are square or circular
2235 // TODO: Handle AA for round points
2236
Chet Haase5b0200b2011-04-13 17:58:08 -07002237 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002238 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002239 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002240 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002241 if (isHairLine) {
2242 // Now that we know it's hairline, we can set the effective width, to be used later
2243 strokeWidth = 1.0f;
2244 }
2245 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002246 int alpha;
2247 SkXfermode::Mode mode;
2248 getAlphaAndMode(paint, &alpha, &mode);
2249
2250 int verticesCount = count >> 1;
2251 int generatedVerticesCount = 0;
2252
2253 TextureVertex pointsData[verticesCount];
2254 TextureVertex* vertex = &pointsData[0];
2255
Romain Guy04299382012-07-18 17:15:41 -07002256 // TODO: We should optimize this method to not generate vertices for points
2257 // that lie outside of the clip.
2258 mCaches.enableScissor();
2259
Romain Guyed6fcb02011-03-21 13:11:28 -07002260 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002261 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002262 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002263 setupDrawColor(paint->getColor(), alpha);
2264 setupDrawColorFilter();
2265 setupDrawShader();
2266 setupDrawBlending(mode);
2267 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002268 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002269 setupDrawColorUniforms();
2270 setupDrawColorFilterUniforms();
2271 setupDrawPointUniforms();
2272 setupDrawShaderIdentityUniforms();
2273 setupDrawMesh(vertex);
2274
2275 for (int i = 0; i < count; i += 2) {
2276 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2277 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002278
Chet Haase8a5cc922011-04-26 07:28:09 -07002279 float left = points[i] - halfWidth;
2280 float right = points[i] + halfWidth;
2281 float top = points[i + 1] - halfWidth;
2282 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002283
Chet Haase8a5cc922011-04-26 07:28:09 -07002284 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002285 }
2286
2287 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002288
2289 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002290}
2291
Chet Haase48659092012-05-31 15:21:51 -07002292status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002293 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002294 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002295
Romain Guyae88e5e2010-10-22 17:49:18 -07002296 Rect& clip(*mSnapshot->clipRect);
2297 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002298
Romain Guy3d58c032010-07-14 16:34:53 -07002299 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002300
2301 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002302}
Romain Guy9d5316e2010-06-24 19:30:36 -07002303
Chet Haase48659092012-05-31 15:21:51 -07002304status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2305 SkPaint* paint) {
2306 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002307 const AutoTexture autoCleanup(texture);
2308
2309 const float x = left + texture->left - texture->offset;
2310 const float y = top + texture->top - texture->offset;
2311
2312 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002313
2314 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002315}
2316
Chet Haase48659092012-05-31 15:21:51 -07002317status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002318 float rx, float ry, SkPaint* p) {
2319 if (mSnapshot->isIgnored() || quickReject(left, top, right, bottom)) {
2320 return DrawGlInfo::kStatusDone;
2321 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002322
Chris Craik710f46d2012-09-17 17:25:49 -07002323 if (p->getStyle() != SkPaint::kFill_Style) {
2324 mCaches.activeTexture(0);
2325 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2326 right - left, bottom - top, rx, ry, p);
2327 return drawShape(left, top, texture, p);
2328 }
2329
2330 SkPath path;
2331 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2332 path.addRoundRect(rect, rx, ry);
2333 drawConvexPath(path, p->getColor(), getXfermode(p->getXfermode()), p->isAntiAlias());
2334
2335 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002336}
2337
Chris Craik710f46d2012-09-17 17:25:49 -07002338status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
2339 if (mSnapshot->isIgnored() || quickReject(x - radius, y - radius, x + radius, y + radius)) {
2340 return DrawGlInfo::kStatusDone;
2341 }
Romain Guy01d58e42011-01-19 21:54:02 -08002342
Chris Craik710f46d2012-09-17 17:25:49 -07002343 if (p->getStyle() != SkPaint::kFill_Style) {
2344 mCaches.activeTexture(0);
2345 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2346 return drawShape(x - radius, y - radius, texture, p);
2347 }
2348
2349 SkPath path;
2350 path.addCircle(x, y, radius);
2351 drawConvexPath(path, p->getColor(), getXfermode(p->getXfermode()), p->isAntiAlias());
2352
2353 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002354}
Romain Guy01d58e42011-01-19 21:54:02 -08002355
Chet Haase48659092012-05-31 15:21:51 -07002356status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002357 SkPaint* p) {
2358 if (mSnapshot->isIgnored() || quickReject(left, top, right, bottom)) {
2359 return DrawGlInfo::kStatusDone;
2360 }
Romain Guy01d58e42011-01-19 21:54:02 -08002361
Chris Craik710f46d2012-09-17 17:25:49 -07002362 if (p->getStyle() != SkPaint::kFill_Style) {
2363 mCaches.activeTexture(0);
2364 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2365 return drawShape(left, top, texture, p);
2366 }
2367
2368 SkPath path;
2369 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2370 path.addOval(rect);
2371 drawConvexPath(path, p->getColor(), getXfermode(p->getXfermode()), p->isAntiAlias());
2372
2373 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002374}
2375
Chet Haase48659092012-05-31 15:21:51 -07002376status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002377 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002378 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002379
2380 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002381 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002382 }
2383
Romain Guya1d3c912011-12-13 14:55:06 -08002384 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002385 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2386 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002387 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002388}
2389
Chet Haase48659092012-05-31 15:21:51 -07002390status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craik710f46d2012-09-17 17:25:49 -07002391 if (mSnapshot->isIgnored() || quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002392 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002393 }
2394
Chris Craik710f46d2012-09-17 17:25:49 -07002395 if (p->getStyle() != SkPaint::kFill_Style) {
2396 mCaches.activeTexture(0);
2397 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2398 return drawShape(left, top, texture, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002399 }
2400
Romain Guy181d0a62011-06-09 18:52:38 -07002401 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002402 SkPath path;
2403 path.addRect(left, top, right, bottom);
2404 drawConvexPath(path, p->getColor(), getXfermode(p->getXfermode()), true);
Chet Haase858aa932011-05-12 09:06:00 -07002405 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002406 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chet Haase858aa932011-05-12 09:06:00 -07002407 }
Chet Haase48659092012-05-31 15:21:51 -07002408
2409 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002410}
Romain Guy9d5316e2010-06-24 19:30:36 -07002411
Raph Levien416a8472012-07-19 22:48:17 -07002412void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2413 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2414 float x, float y) {
2415 mCaches.activeTexture(0);
2416
2417 // NOTE: The drop shadow will not perform gamma correction
2418 // if shader-based correction is enabled
2419 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2420 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2421 paint, text, bytesCount, count, mShadowRadius, positions);
2422 const AutoTexture autoCleanup(shadow);
2423
2424 const float sx = x - shadow->left + mShadowDx;
2425 const float sy = y - shadow->top + mShadowDy;
2426
2427 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2428 int shadowColor = mShadowColor;
2429 if (mShader) {
2430 shadowColor = 0xffffffff;
2431 }
2432
2433 setupDraw();
2434 setupDrawWithTexture(true);
2435 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2436 setupDrawColorFilter();
2437 setupDrawShader();
2438 setupDrawBlending(true, mode);
2439 setupDrawProgram();
2440 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2441 setupDrawTexture(shadow->id);
2442 setupDrawPureColorUniforms();
2443 setupDrawColorFilterUniforms();
2444 setupDrawShaderUniforms();
2445 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2446
2447 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2448}
2449
Chet Haase48659092012-05-31 15:21:51 -07002450status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002451 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002452 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002453 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002454 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002455 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002456
Romain Guy671d6cf2012-01-18 12:39:17 -08002457 // NOTE: Skia does not support perspective transform on drawPosText yet
2458 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002459 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002460 }
2461
2462 float x = 0.0f;
2463 float y = 0.0f;
2464 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2465 if (pureTranslate) {
2466 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2467 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2468 }
2469
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002470 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002471 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2472 paint->getTextSize());
2473
2474 int alpha;
2475 SkXfermode::Mode mode;
2476 getAlphaAndMode(paint, &alpha, &mode);
2477
Raph Levien416a8472012-07-19 22:48:17 -07002478 if (CC_UNLIKELY(mHasShadow)) {
2479 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2480 0.0f, 0.0f);
2481 }
2482
Romain Guy671d6cf2012-01-18 12:39:17 -08002483 // Pick the appropriate texture filtering
2484 bool linearFilter = mSnapshot->transform->changesBounds();
2485 if (pureTranslate && !linearFilter) {
2486 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2487 }
2488
2489 mCaches.activeTexture(0);
2490 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002491 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002492 setupDrawDirtyRegionsDisabled();
2493 setupDrawWithTexture(true);
2494 setupDrawAlpha8Color(paint->getColor(), alpha);
2495 setupDrawColorFilter();
2496 setupDrawShader();
2497 setupDrawBlending(true, mode);
2498 setupDrawProgram();
2499 setupDrawModelView(x, y, x, y, pureTranslate, true);
2500 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2501 setupDrawPureColorUniforms();
2502 setupDrawColorFilterUniforms();
2503 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002504 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002505
2506 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2507 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2508
Romain Guy211370f2012-02-01 16:10:55 -08002509 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002510
2511 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2512 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002513 if (hasActiveLayer) {
2514 if (!pureTranslate) {
2515 mSnapshot->transform->mapRect(bounds);
2516 }
2517 dirtyLayerUnchecked(bounds, getRegion());
2518 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002519 }
Chet Haase48659092012-05-31 15:21:51 -07002520
2521 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002522}
2523
Romain Guyc2525952012-07-27 16:41:22 -07002524status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002525 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002526 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002527 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002528 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002529 }
2530
Chet Haasea1cff502012-02-21 13:43:44 -08002531 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002532 switch (paint->getTextAlign()) {
2533 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002534 x -= length / 2.0f;
2535 break;
2536 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002537 x -= length;
2538 break;
2539 default:
2540 break;
2541 }
2542
Romain Guycac5fd32011-12-01 20:08:50 -08002543 SkPaint::FontMetrics metrics;
2544 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002545 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002546 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002547 }
2548
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002549 const float oldX = x;
2550 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002551 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002552 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002553 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2554 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2555 }
2556
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002557#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002558 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002559 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002560#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002561
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002562 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002563 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002564 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002565
Romain Guy86568192010-12-14 15:55:39 -08002566 int alpha;
2567 SkXfermode::Mode mode;
2568 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002569
Romain Guy211370f2012-02-01 16:10:55 -08002570 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002571 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2572 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002573 }
2574
Romain Guy6620c6d2010-12-06 18:07:02 -08002575 // Pick the appropriate texture filtering
2576 bool linearFilter = mSnapshot->transform->changesBounds();
2577 if (pureTranslate && !linearFilter) {
2578 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2579 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002580
Romain Guy16c88082012-06-11 16:03:47 -07002581 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002582 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002583 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002584 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002585 setupDrawDirtyRegionsDisabled();
2586 setupDrawWithTexture(true);
2587 setupDrawAlpha8Color(paint->getColor(), alpha);
2588 setupDrawColorFilter();
2589 setupDrawShader();
2590 setupDrawBlending(true, mode);
2591 setupDrawProgram();
2592 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002593 // See comment above; the font renderer must use texture unit 0
2594 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002595 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2596 setupDrawPureColorUniforms();
2597 setupDrawColorFilterUniforms();
2598 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002599 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002600
Romain Guy6620c6d2010-12-06 18:07:02 -08002601 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002602 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2603
Romain Guy211370f2012-02-01 16:10:55 -08002604 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002605
Raph Levien996e57c2012-07-23 15:22:52 -07002606 bool status;
Raph Levien8b4072d2012-07-30 15:50:00 -07002607 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
2608 SkPaint paintCopy(*paint);
2609 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2610 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Raph Levien996e57c2012-07-23 15:22:52 -07002611 positions, hasActiveLayer ? &bounds : NULL);
2612 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002613 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2614 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002615 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002616
2617 if (status && hasActiveLayer) {
2618 if (!pureTranslate) {
2619 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002620 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002621 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002622 }
Romain Guy694b5192010-07-21 21:33:20 -07002623
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002624 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002625
2626 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002627}
2628
Chet Haase48659092012-05-31 15:21:51 -07002629status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002630 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002631 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2632 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002633 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002634 }
2635
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002636 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002637 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2638 paint->getTextSize());
2639
2640 int alpha;
2641 SkXfermode::Mode mode;
2642 getAlphaAndMode(paint, &alpha, &mode);
2643
2644 mCaches.activeTexture(0);
2645 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002646 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002647 setupDrawDirtyRegionsDisabled();
2648 setupDrawWithTexture(true);
2649 setupDrawAlpha8Color(paint->getColor(), alpha);
2650 setupDrawColorFilter();
2651 setupDrawShader();
2652 setupDrawBlending(true, mode);
2653 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002654 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002655 setupDrawTexture(fontRenderer.getTexture(true));
2656 setupDrawPureColorUniforms();
2657 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002658 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002659 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002660
Romain Guy97771732012-02-28 18:17:02 -08002661 const Rect* clip = &mSnapshot->getLocalClip();
2662 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 -08002663
Romain Guy97771732012-02-28 18:17:02 -08002664 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002665
2666 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2667 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002668 if (hasActiveLayer) {
2669 mSnapshot->transform->mapRect(bounds);
2670 dirtyLayerUnchecked(bounds, getRegion());
2671 }
Romain Guy97771732012-02-28 18:17:02 -08002672 }
Chet Haase48659092012-05-31 15:21:51 -07002673
2674 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002675}
2676
Chet Haase48659092012-05-31 15:21:51 -07002677status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2678 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002679
Romain Guya1d3c912011-12-13 14:55:06 -08002680 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002681
Romain Guy33f6beb2012-02-16 19:24:51 -08002682 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002683 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002684 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002685 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002686
Romain Guy8b55f372010-08-18 17:10:07 -07002687 const float x = texture->left - texture->offset;
2688 const float y = texture->top - texture->offset;
2689
Romain Guy01d58e42011-01-19 21:54:02 -08002690 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002691
2692 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002693}
2694
Chet Haase48659092012-05-31 15:21:51 -07002695status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002696 if (!layer) {
2697 return DrawGlInfo::kStatusDone;
2698 }
2699
2700 Rect transformed;
2701 Rect clip;
2702 const bool rejected = quickRejectNoScissor(x, y,
2703 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2704
2705 if (rejected) {
Chet Haase48659092012-05-31 15:21:51 -07002706 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002707 }
2708
Romain Guy4ff0cf42012-08-06 14:51:10 -07002709 bool debugLayerUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -07002710 if (updateLayer(layer, true)) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002711 debugLayerUpdate = mCaches.debugLayersUpdates;
Romain Guy2bf68f02012-03-02 13:37:47 -08002712 }
2713
Romain Guy35643dd2012-09-18 15:40:58 -07002714 mCaches.setScissorEnabled(!clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002715 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002716
Romain Guy211370f2012-02-01 16:10:55 -08002717 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002718 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002719 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002720 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002721 const float a = layer->getAlpha() / 255.0f;
2722 SkiaColorFilter *oldFilter = mColorFilter;
2723 mColorFilter = layer->getColorFilter();
Romain Guyf219da52011-01-16 12:54:25 -08002724
Romain Guyc88e3572011-01-22 00:32:12 -08002725 setupDraw();
2726 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002727 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002728 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002729 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002730 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002731 setupDrawPureColorUniforms();
2732 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002733 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002734 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002735 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2736 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002737
Romain Guyd21b6e12011-11-30 20:21:23 -08002738 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002739 setupDrawModelViewTranslate(tx, ty,
2740 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002741 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002742 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002743 setupDrawModelViewTranslate(x, y,
2744 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2745 }
Romain Guyc88e3572011-01-22 00:32:12 -08002746 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002747
Romain Guyc88e3572011-01-22 00:32:12 -08002748 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2749 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002750
Romain Guyc88e3572011-01-22 00:32:12 -08002751 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002752
Chet Haased15ebf22012-09-05 11:40:29 -07002753 mColorFilter = oldFilter;
2754
Romain Guy3a3133d2011-02-01 22:59:58 -08002755#if DEBUG_LAYERS_AS_REGIONS
2756 drawRegionRects(layer->region);
2757#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002758 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002759
2760 if (debugLayerUpdate) {
2761 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2762 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2763 }
Romain Guyf219da52011-01-16 12:54:25 -08002764 }
Chet Haase48659092012-05-31 15:21:51 -07002765
2766 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002767}
2768
Romain Guy6926c722010-07-12 20:20:03 -07002769///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002770// Shaders
2771///////////////////////////////////////////////////////////////////////////////
2772
2773void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002774 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002775}
2776
Romain Guy06f96e22010-07-30 19:18:16 -07002777void OpenGLRenderer::setupShader(SkiaShader* shader) {
2778 mShader = shader;
2779 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002780 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002781 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002782}
2783
Romain Guyd27977d2010-07-14 19:18:51 -07002784///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002785// Color filters
2786///////////////////////////////////////////////////////////////////////////////
2787
2788void OpenGLRenderer::resetColorFilter() {
2789 mColorFilter = NULL;
2790}
2791
2792void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2793 mColorFilter = filter;
2794}
2795
2796///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002797// Drop shadow
2798///////////////////////////////////////////////////////////////////////////////
2799
2800void OpenGLRenderer::resetShadow() {
2801 mHasShadow = false;
2802}
2803
2804void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2805 mHasShadow = true;
2806 mShadowRadius = radius;
2807 mShadowDx = dx;
2808 mShadowDy = dy;
2809 mShadowColor = color;
2810}
2811
2812///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002813// Draw filters
2814///////////////////////////////////////////////////////////////////////////////
2815
2816void OpenGLRenderer::resetPaintFilter() {
2817 mHasDrawFilter = false;
2818}
2819
2820void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2821 mHasDrawFilter = true;
2822 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2823 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2824}
2825
2826SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002827 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002828
2829 uint32_t flags = paint->getFlags();
2830
2831 mFilteredPaint = *paint;
2832 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2833
2834 return &mFilteredPaint;
2835}
2836
2837///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002838// Drawing implementation
2839///////////////////////////////////////////////////////////////////////////////
2840
Romain Guy01d58e42011-01-19 21:54:02 -08002841void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2842 float x, float y, SkPaint* paint) {
2843 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2844 return;
2845 }
2846
2847 int alpha;
2848 SkXfermode::Mode mode;
2849 getAlphaAndMode(paint, &alpha, &mode);
2850
2851 setupDraw();
2852 setupDrawWithTexture(true);
2853 setupDrawAlpha8Color(paint->getColor(), alpha);
2854 setupDrawColorFilter();
2855 setupDrawShader();
2856 setupDrawBlending(true, mode);
2857 setupDrawProgram();
2858 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2859 setupDrawTexture(texture->id);
2860 setupDrawPureColorUniforms();
2861 setupDrawColorFilterUniforms();
2862 setupDrawShaderUniforms();
2863 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2864
2865 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2866
2867 finishDrawTexture();
2868}
2869
Romain Guyf607bdc2010-09-10 19:20:06 -07002870// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002871#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2872#define kStdUnderline_Offset (1.0f / 9.0f)
2873#define kStdUnderline_Thickness (1.0f / 18.0f)
2874
2875void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2876 float x, float y, SkPaint* paint) {
2877 // Handle underline and strike-through
2878 uint32_t flags = paint->getFlags();
2879 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002880 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002881 float underlineWidth = length;
2882 // If length is > 0.0f, we already measured the text for the text alignment
2883 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002884 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002885 }
2886
Romain Guy211370f2012-02-01 16:10:55 -08002887 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002888 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002889 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002890
Raph Levien8b4072d2012-07-30 15:50:00 -07002891 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002892 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002893
Romain Guyf6834472011-01-23 13:32:12 -08002894 int linesCount = 0;
2895 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2896 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2897
2898 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002899 float points[pointsCount];
2900 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002901
2902 if (flags & SkPaint::kUnderlineText_Flag) {
2903 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002904 points[currentPoint++] = left;
2905 points[currentPoint++] = top;
2906 points[currentPoint++] = left + underlineWidth;
2907 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002908 }
2909
2910 if (flags & SkPaint::kStrikeThruText_Flag) {
2911 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002912 points[currentPoint++] = left;
2913 points[currentPoint++] = top;
2914 points[currentPoint++] = left + underlineWidth;
2915 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002916 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002917
Romain Guy726aeba2011-06-01 14:52:00 -07002918 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002919
Romain Guy726aeba2011-06-01 14:52:00 -07002920 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002921 }
2922 }
2923}
2924
Romain Guy026c5e162010-06-28 17:12:22 -07002925void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002926 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002927 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002928 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002929 color |= 0x00ffffff;
2930 }
2931
Romain Guy70ca14e2010-12-13 18:24:33 -08002932 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002933 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002934 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002935 setupDrawShader();
2936 setupDrawColorFilter();
2937 setupDrawBlending(mode);
2938 setupDrawProgram();
2939 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2940 setupDrawColorUniforms();
2941 setupDrawShaderUniforms(ignoreTransform);
2942 setupDrawColorFilterUniforms();
2943 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002944
Romain Guyc95c8d62010-09-17 15:31:32 -07002945 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2946}
2947
Romain Guy82ba8142010-07-09 13:25:56 -07002948void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002949 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002950 int alpha;
2951 SkXfermode::Mode mode;
2952 getAlphaAndMode(paint, &alpha, &mode);
2953
Romain Guyd21b6e12011-11-30 20:21:23 -08002954 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002955
Romain Guy211370f2012-02-01 16:10:55 -08002956 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002957 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2958 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2959
Romain Guyd21b6e12011-11-30 20:21:23 -08002960 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002961 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2962 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2963 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2964 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002965 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002966 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2967 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2968 GL_TRIANGLE_STRIP, gMeshCount);
2969 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002970}
2971
Romain Guybd6b79b2010-06-26 00:13:53 -07002972void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002973 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2974 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002975 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002976}
2977
2978void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002979 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002980 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002981 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002982
Romain Guy746b7402010-10-26 16:27:31 -07002983 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002984 setupDrawWithTexture();
2985 setupDrawColor(alpha, alpha, alpha, alpha);
2986 setupDrawColorFilter();
2987 setupDrawBlending(blend, mode, swapSrcDst);
2988 setupDrawProgram();
2989 if (!dirty) {
2990 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002991 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002992 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002993 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002994 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002995 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002996 }
Romain Guy86568192010-12-14 15:55:39 -08002997 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002998 setupDrawColorFilterUniforms();
2999 setupDrawTexture(texture);
3000 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003001
Romain Guy6820ac82010-09-15 18:11:50 -07003002 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003003
3004 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003005}
3006
Romain Guya5aed0d2010-09-09 14:42:43 -07003007void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003008 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003009 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003010
Romain Guy82ba8142010-07-09 13:25:56 -07003011 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003012 // These blend modes are not supported by OpenGL directly and have
3013 // to be implemented using shaders. Since the shader will perform
3014 // the blending, turn blending off here
3015 // If the blend mode cannot be implemented using shaders, fall
3016 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003017 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08003018 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003019 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003020 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003021
Romain Guy82bc7a72012-01-03 14:13:39 -08003022 if (mCaches.blend) {
3023 glDisable(GL_BLEND);
3024 mCaches.blend = false;
3025 }
3026
3027 return;
3028 } else {
3029 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003030 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003031 }
3032
3033 if (!mCaches.blend) {
3034 glEnable(GL_BLEND);
3035 }
3036
3037 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3038 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3039
3040 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3041 glBlendFunc(sourceMode, destMode);
3042 mCaches.lastSrcMode = sourceMode;
3043 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003044 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003045 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003046 glDisable(GL_BLEND);
3047 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003048 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003049}
3050
Romain Guy889f8d12010-07-29 14:37:42 -07003051bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003052 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003053 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003054 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003055 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003056 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003057 }
Romain Guy6926c722010-07-12 20:20:03 -07003058 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003059}
3060
Romain Guy026c5e162010-06-28 17:12:22 -07003061void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003062 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003063 TextureVertex::setUV(v++, u1, v1);
3064 TextureVertex::setUV(v++, u2, v1);
3065 TextureVertex::setUV(v++, u1, v2);
3066 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003067}
3068
Chet Haase5c13d892010-10-08 08:37:55 -07003069void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003070 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003071 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003072}
3073
Romain Guy9d5316e2010-06-24 19:30:36 -07003074}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003075}; // namespace android