blob: c475f2091c34c3699712335cfc71e92f4014adf6 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy03d58522012-02-24 17:54:07 -080024#include <SkPathMeasure.h>
Romain Guy694b5192010-07-21 21:33:20 -070025#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070026
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070028#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070029
Romain Guy08aa2cb2011-03-17 11:06:57 -070030#include <private/hwui/DrawGlInfo.h>
31
Romain Guy5b3b3522010-10-27 18:57:51 -070032#include <ui/Rect.h>
33
Romain Guy85bf02f2010-06-22 13:11:24 -070034#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Romain Guya957eea2010-12-08 18:34:42 -080036#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070037
38namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070039namespace uirenderer {
40
41///////////////////////////////////////////////////////////////////////////////
42// Defines
43///////////////////////////////////////////////////////////////////////////////
44
Romain Guy759ea802010-09-16 20:49:46 -070045#define RAD_TO_DEG (180.0f / 3.14159265f)
46#define MIN_ANGLE 0.001f
47
Romain Guyf8773082012-07-12 18:01:00 -070048#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070049
Romain Guyd21b6e12011-11-30 20:21:23 -080050#define FILTER(paint) (paint && paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
51
Romain Guy9d5316e2010-06-24 19:30:36 -070052///////////////////////////////////////////////////////////////////////////////
53// Globals
54///////////////////////////////////////////////////////////////////////////////
55
Romain Guy889f8d12010-07-29 14:37:42 -070056/**
57 * Structure mapping Skia xfermodes to OpenGL blending factors.
58 */
59struct Blender {
60 SkXfermode::Mode mode;
61 GLenum src;
62 GLenum dst;
63}; // struct Blender
64
Romain Guy026c5e162010-06-28 17:12:22 -070065// In this array, the index of each Blender equals the value of the first
66// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
67static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070068 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
69 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
70 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
71 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
73 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
74 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
75 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
76 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
77 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
79 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
81 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
82 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070083};
Romain Guye4d01122010-06-16 18:44:05 -070084
Romain Guy87a76572010-09-13 18:11:21 -070085// This array contains the swapped version of each SkXfermode. For instance
86// this array's SrcOver blending mode is actually DstOver. You can refer to
87// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070088static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070089 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
90 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
91 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
92 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
93 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
94 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
95 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
96 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
98 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
99 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
101 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
102 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
103 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700104};
105
Romain Guyf6a11b82010-06-23 17:47:49 -0700106///////////////////////////////////////////////////////////////////////////////
107// Constructors/destructor
108///////////////////////////////////////////////////////////////////////////////
109
Romain Guyfb8b7632010-08-23 21:05:08 -0700110OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700111 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700112 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700113 mHasShadow = false;
Romain Guy5ff9df62012-01-23 17:09:05 -0800114 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700115
Romain Guyac670c02010-07-27 17:39:27 -0700116 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
117
Romain Guyae5575b2010-07-29 18:48:04 -0700118 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700119}
120
Romain Guy85bf02f2010-06-22 13:11:24 -0700121OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700122 // The context has already been destroyed at this point, do not call
123 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700124}
125
Romain Guyf6a11b82010-06-23 17:47:49 -0700126///////////////////////////////////////////////////////////////////////////////
Romain Guy13631f32012-01-30 17:41:55 -0800127// Debug
128///////////////////////////////////////////////////////////////////////////////
129
130void OpenGLRenderer::startMark(const char* name) const {
131 mCaches.startMark(0, name);
132}
133
134void OpenGLRenderer::endMark() const {
135 mCaches.endMark();
136}
137
138///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700139// Setup
140///////////////////////////////////////////////////////////////////////////////
141
Romain Guy49c5fc02012-05-15 11:10:01 -0700142bool OpenGLRenderer::isDeferred() {
143 return false;
144}
145
Romain Guy85bf02f2010-06-22 13:11:24 -0700146void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700147 initViewport(width, height);
148
149 glDisable(GL_DITHER);
150 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
151
152 glEnableVertexAttribArray(Program::kBindingPosition);
153}
154
155void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700156 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700157
158 mWidth = width;
159 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700160
161 mFirstSnapshot->height = height;
162 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700163}
164
Chet Haase44b2fe32012-06-06 19:03:58 -0700165int OpenGLRenderer::prepare(bool opaque) {
166 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800167}
168
Chet Haase44b2fe32012-06-06 19:03:58 -0700169int OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800170 mCaches.clearGarbage();
171
Romain Guy8aef54f2010-09-01 15:13:49 -0700172 mSnapshot = new Snapshot(mFirstSnapshot,
173 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800174 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700175 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700176
Romain Guy7d7b5492011-01-24 16:33:45 -0800177 mSnapshot->setClip(left, top, right, bottom);
Romain Guy57b52682012-09-20 17:38:46 -0700178 mDirtyClip = opaque;
Romain Guyddf74372012-05-22 14:07:07 -0700179
Romain Guy11cb6422012-09-21 00:39:43 -0700180 updateLayers();
181
Romain Guy45e4c3d2012-09-11 17:17:07 -0700182 // If we know that we are going to redraw the entire framebuffer,
183 // perform a discard to let the driver know we don't need to preserve
184 // the back buffer for this frame.
185 if (mCaches.extensions.hasDiscardFramebuffer() &&
186 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
187 const GLenum attachments[] = { getTargetFbo() == 0 ? GL_COLOR_EXT : GL_COLOR_ATTACHMENT0 };
188 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
189 }
190
Romain Guyddf74372012-05-22 14:07:07 -0700191 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800192
Romain Guy2b7028e2012-09-19 17:25:38 -0700193 mTilingSnapshot = mSnapshot;
Romain Guy57b52682012-09-20 17:38:46 -0700194 startTiling(mTilingSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700195
Romain Guy6b7bd242010-10-06 19:49:23 -0700196 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700197 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700198 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700199 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700200 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700201 } else {
202 mCaches.resetScissor();
203 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700204
205 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700206}
207
208void OpenGLRenderer::syncState() {
209 glViewport(0, 0, mWidth, mHeight);
210
211 if (mCaches.blend) {
212 glEnable(GL_BLEND);
213 } else {
214 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700215 }
Romain Guybb9524b2010-06-22 18:56:38 -0700216}
217
Romain Guy57b52682012-09-20 17:38:46 -0700218void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700219 Rect* clip = mTilingSnapshot->clipRect;
Romain Guy2b7028e2012-09-19 17:25:38 -0700220 if (s->flags & Snapshot::kFlagIsFboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700221 clip = s->clipRect;
222 }
223
224 mCaches.startTiling(clip->left, s->height - clip->bottom,
225 clip->right - clip->left, clip->bottom - clip->top, opaque);
226}
227
228void OpenGLRenderer::endTiling() {
229 mCaches.endTiling();
230}
231
Romain Guyb025b9c2010-09-16 14:16:48 -0700232void OpenGLRenderer::finish() {
Romain Guy2b7028e2012-09-19 17:25:38 -0700233 endTiling();
234
Romain Guy11cb6422012-09-21 00:39:43 -0700235 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700236#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700237 GLenum status = GL_NO_ERROR;
238 while ((status = glGetError()) != GL_NO_ERROR) {
239 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
240 switch (status) {
241 case GL_INVALID_ENUM:
242 ALOGE(" GL_INVALID_ENUM");
243 break;
244 case GL_INVALID_VALUE:
245 ALOGE(" GL_INVALID_VALUE");
246 break;
247 case GL_INVALID_OPERATION:
248 ALOGE(" GL_INVALID_OPERATION");
249 break;
250 case GL_OUT_OF_MEMORY:
251 ALOGE(" Out of memory!");
252 break;
253 }
Romain Guya07105b2011-01-10 21:14:18 -0800254 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700255#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700256
Romain Guyc15008e2010-11-10 11:59:15 -0800257#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800258 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700259#else
260 if (mCaches.getDebugLevel() & kDebugMemory) {
261 mCaches.dumpMemoryUsage();
262 }
Romain Guyc15008e2010-11-10 11:59:15 -0800263#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700264 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700265}
266
Romain Guy6c319ca2011-01-11 14:29:25 -0800267void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700268 if (mCaches.currentProgram) {
269 if (mCaches.currentProgram->isInUse()) {
270 mCaches.currentProgram->remove();
271 mCaches.currentProgram = NULL;
272 }
273 }
Romain Guy50c0f092010-10-19 11:42:22 -0700274 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800275 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800276 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800277 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700278}
279
Romain Guy6c319ca2011-01-11 14:29:25 -0800280void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800281 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800282 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700283 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
284
Romain Guy3e263fa2011-12-12 16:47:48 -0800285 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
286
Chet Haase80250612012-08-15 13:46:54 -0700287 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700288 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800289 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700290 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700291
Romain Guya1d3c912011-12-13 14:55:06 -0800292 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700293
Romain Guy50c0f092010-10-19 11:42:22 -0700294 mCaches.blend = true;
295 glEnable(GL_BLEND);
296 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
297 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700298}
299
Romain Guy35643dd2012-09-18 15:40:58 -0700300void OpenGLRenderer::resumeAfterLayer() {
301 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
302 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
303 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
304
305 mCaches.resetScissor();
306 dirtyClip();
307}
308
Romain Guyba6be8a2012-04-23 18:22:09 -0700309void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700310 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700311}
312
313void OpenGLRenderer::attachFunctor(Functor* functor) {
314 mFunctors.add(functor);
315}
316
Romain Guy8f3b8e32012-03-27 16:33:45 -0700317status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
318 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700319 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700320
Romain Guyba6be8a2012-04-23 18:22:09 -0700321 if (count > 0) {
322 SortedVector<Functor*> functors(mFunctors);
323 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700324
Romain Guyba6be8a2012-04-23 18:22:09 -0700325 DrawGlInfo info;
326 info.clipLeft = 0;
327 info.clipTop = 0;
328 info.clipRight = 0;
329 info.clipBottom = 0;
330 info.isLayer = false;
331 info.width = 0;
332 info.height = 0;
333 memset(info.transform, 0, sizeof(float) * 16);
334
335 for (size_t i = 0; i < count; i++) {
336 Functor* f = functors.itemAt(i);
337 result |= (*f)(DrawGlInfo::kModeProcess, &info);
338
Chris Craikc2c95432012-04-25 15:13:52 -0700339 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700340 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
341 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700342 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700343
Chris Craikc2c95432012-04-25 15:13:52 -0700344 if (result & DrawGlInfo::kStatusInvoke) {
345 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700346 }
347 }
Chet Haasebeb8bd02012-09-09 16:13:26 -0700348 // protect against functors binding to other buffers
349 mCaches.unbindMeshBuffer();
350 mCaches.unbindIndicesBuffer();
351 mCaches.activeTexture(0);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700352 }
353
354 return result;
355}
356
357status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800358 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700359 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700360
Romain Guy8a4ac612012-07-17 17:32:48 -0700361 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800362 if (mDirtyClip) {
363 setScissorFromClip();
364 }
Romain Guyd643bb52011-03-01 14:55:21 -0800365
Romain Guy80911b82011-03-16 15:30:12 -0700366 Rect clip(*mSnapshot->clipRect);
367 clip.snapToPixelBoundaries();
368
Romain Guyd643bb52011-03-01 14:55:21 -0800369 // Since we don't know what the functor will draw, let's dirty
370 // tne entire clip region
371 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800372 dirtyLayerUnchecked(clip, getRegion());
373 }
Romain Guyd643bb52011-03-01 14:55:21 -0800374
Romain Guy08aa2cb2011-03-17 11:06:57 -0700375 DrawGlInfo info;
376 info.clipLeft = clip.left;
377 info.clipTop = clip.top;
378 info.clipRight = clip.right;
379 info.clipBottom = clip.bottom;
380 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700381 info.width = getSnapshot()->viewport.getWidth();
382 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700383 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700384
Chet Haase48659092012-05-31 15:21:51 -0700385 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800386
Romain Guy8f3b8e32012-03-27 16:33:45 -0700387 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700388 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800389 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700390
Chris Craik65924a32012-04-05 17:52:11 -0700391 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700392 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700393 }
Romain Guycabfcc12011-03-07 18:06:46 -0800394 }
395
Chet Haasedaf98e92011-01-10 14:10:36 -0800396 resume();
Romain Guy65549432012-03-26 16:45:05 -0700397 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800398}
399
Romain Guyf6a11b82010-06-23 17:47:49 -0700400///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700401// Layers
402///////////////////////////////////////////////////////////////////////////////
403
404bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
405 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
406 OpenGLRenderer* renderer = layer->renderer;
407 Rect& dirty = layer->dirtyRect;
408
409 if (inFrame) endTiling();
410
411 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
412 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
413 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
414 renderer->finish();
415
416 if (inFrame) {
417 resumeAfterLayer();
418 startTiling(mSnapshot);
419 }
420
421 dirty.setEmpty();
422 layer->deferredUpdateScheduled = false;
423 layer->renderer = NULL;
424 layer->displayList = NULL;
425
426 return true;
427 }
428
429 return false;
430}
431
432void OpenGLRenderer::updateLayers() {
433 int count = mLayerUpdates.size();
434 if (count > 0) {
435 startMark("Layer Updates");
436
437 // Note: it is very important to update the layers in reverse order
438 for (int i = count - 1; i >= 0; i--) {
439 Layer* layer = mLayerUpdates.itemAt(i);
440 updateLayer(layer, false);
441 mCaches.resourceCache.decrementRefcount(layer);
442 }
443 mLayerUpdates.clear();
444
445 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
446 endMark();
447 }
448}
449
450void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
451 if (layer) {
452 mLayerUpdates.push_back(layer);
453 mCaches.resourceCache.incrementRefcount(layer);
454 }
455}
456
457void OpenGLRenderer::clearLayerUpdates() {
458 size_t count = mLayerUpdates.size();
459 if (count > 0) {
460 mCaches.resourceCache.lock();
461 for (size_t i = 0; i < count; i++) {
462 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
463 }
464 mCaches.resourceCache.unlock();
465 mLayerUpdates.clear();
466 }
467}
468
469///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700470// State management
471///////////////////////////////////////////////////////////////////////////////
472
Romain Guybb9524b2010-06-22 18:56:38 -0700473int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700474 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700475}
476
477int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700478 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700479}
480
481void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700482 if (mSaveCount > 1) {
483 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700484 }
Romain Guybb9524b2010-06-22 18:56:38 -0700485}
486
487void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700488 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700489
Romain Guy8fb95422010-08-17 18:38:51 -0700490 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700491 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700492 }
Romain Guybb9524b2010-06-22 18:56:38 -0700493}
494
Romain Guy8aef54f2010-09-01 15:13:49 -0700495int OpenGLRenderer::saveSnapshot(int flags) {
496 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700497 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700498}
499
500bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700501 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700502 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700503 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700504
Romain Guybd6b79b2010-06-26 00:13:53 -0700505 sp<Snapshot> current = mSnapshot;
506 sp<Snapshot> previous = mSnapshot->previous;
507
Romain Guyeb993562010-10-05 18:14:38 -0700508 if (restoreOrtho) {
509 Rect& r = previous->viewport;
510 glViewport(r.left, r.top, r.right, r.bottom);
511 mOrthoMatrix.load(current->orthoMatrix);
512 }
513
Romain Guy8b55f372010-08-18 17:10:07 -0700514 mSaveCount--;
515 mSnapshot = previous;
516
Romain Guy2542d192010-08-18 11:47:12 -0700517 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700518 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700519 }
Romain Guy2542d192010-08-18 11:47:12 -0700520
Romain Guy5ec99242010-11-03 16:19:08 -0700521 if (restoreLayer) {
522 composeLayer(current, previous);
523 }
524
Romain Guy2542d192010-08-18 11:47:12 -0700525 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700526}
527
Romain Guyf6a11b82010-06-23 17:47:49 -0700528///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700529// Layers
530///////////////////////////////////////////////////////////////////////////////
531
532int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700533 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700534 const GLuint previousFbo = mSnapshot->fbo;
535 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700536
Romain Guyaf636eb2010-12-09 17:47:21 -0800537 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700538 int alpha = 255;
539 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700540
Romain Guye45362c2010-11-03 19:58:32 -0700541 if (p) {
542 alpha = p->getAlpha();
543 if (!mCaches.extensions.hasFramebufferFetch()) {
544 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
545 if (!isMode) {
546 // Assume SRC_OVER
547 mode = SkXfermode::kSrcOver_Mode;
548 }
549 } else {
550 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700551 }
552 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700553 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700554 }
Romain Guyd55a8612010-06-28 17:42:46 -0700555
Chet Haased48885a2012-08-28 17:43:28 -0700556 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700557 }
Romain Guyd55a8612010-06-28 17:42:46 -0700558
559 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700560}
561
562int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
563 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700564 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700565 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700566 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700567 SkPaint paint;
568 paint.setAlpha(alpha);
569 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700570 }
Romain Guyd55a8612010-06-28 17:42:46 -0700571}
Romain Guybd6b79b2010-06-26 00:13:53 -0700572
Romain Guy1c740bc2010-09-13 18:00:09 -0700573/**
574 * Layers are viewed by Skia are slightly different than layers in image editing
575 * programs (for instance.) When a layer is created, previously created layers
576 * and the frame buffer still receive every drawing command. For instance, if a
577 * layer is created and a shape intersecting the bounds of the layers and the
578 * framebuffer is draw, the shape will be drawn on both (unless the layer was
579 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
580 *
581 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
582 * texture. Unfortunately, this is inefficient as it requires every primitive to
583 * be drawn n + 1 times, where n is the number of active layers. In practice this
584 * means, for every primitive:
585 * - Switch active frame buffer
586 * - Change viewport, clip and projection matrix
587 * - Issue the drawing
588 *
589 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700590 * To avoid this, layers are implemented in a different way here, at least in the
591 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
592 * is set. When this flag is set we can redirect all drawing operations into a
593 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700594 *
595 * This implementation relies on the frame buffer being at least RGBA 8888. When
596 * a layer is created, only a texture is created, not an FBO. The content of the
597 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700598 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700599 * buffer and drawing continues as normal. This technique therefore treats the
600 * frame buffer as a scratch buffer for the layers.
601 *
602 * To compose the layers back onto the frame buffer, each layer texture
603 * (containing the original frame buffer data) is drawn as a simple quad over
604 * the frame buffer. The trick is that the quad is set as the composition
605 * destination in the blending equation, and the frame buffer becomes the source
606 * of the composition.
607 *
608 * Drawing layers with an alpha value requires an extra step before composition.
609 * An empty quad is drawn over the layer's region in the frame buffer. This quad
610 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
611 * quad is used to multiply the colors in the frame buffer. This is achieved by
612 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
613 * GL_ZERO, GL_SRC_ALPHA.
614 *
615 * Because glCopyTexImage2D() can be slow, an alternative implementation might
616 * be use to draw a single clipped layer. The implementation described above
617 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700618 *
619 * (1) The frame buffer is actually not cleared right away. To allow the GPU
620 * to potentially optimize series of calls to glCopyTexImage2D, the frame
621 * buffer is left untouched until the first drawing operation. Only when
622 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700623 */
Chet Haased48885a2012-08-28 17:43:28 -0700624bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
625 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700626 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700627 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700628
Romain Guyeb993562010-10-05 18:14:38 -0700629 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
630
Romain Guyf607bdc2010-09-10 19:20:06 -0700631 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700632 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700633 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700634 Rect untransformedBounds(bounds);
635 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700636
Chet Haased48885a2012-08-28 17:43:28 -0700637 // Layers only make sense if they are in the framebuffer's bounds
638 if (bounds.intersect(*mSnapshot->clipRect)) {
639 // We cannot work with sub-pixels in this case
640 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700641
Chet Haased48885a2012-08-28 17:43:28 -0700642 // When the layer is not an FBO, we may use glCopyTexImage so we
643 // need to make sure the layer does not extend outside the bounds
644 // of the framebuffer
645 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700646 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700647 } else if (fboLayer) {
648 clip.set(bounds);
649 mat4 inverse;
650 inverse.loadInverse(*mSnapshot->transform);
651 inverse.mapRect(clip);
652 clip.snapToPixelBoundaries();
653 if (clip.intersect(untransformedBounds)) {
654 clip.translate(-left, -top);
655 bounds.set(untransformedBounds);
656 } else {
657 clip.setEmpty();
658 }
Romain Guyad37cd32011-03-15 11:12:25 -0700659 }
Chet Haased48885a2012-08-28 17:43:28 -0700660 } else {
661 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700662 }
Romain Guybf434112010-09-16 14:40:17 -0700663
Romain Guy746b7402010-10-26 16:27:31 -0700664 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700665 bounds.getHeight() > mCaches.maxTextureSize ||
666 (fboLayer && clip.isEmpty())) {
667 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700668 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700669 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700670 }
671
672 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700673 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700674 return false;
675 }
Romain Guyf18fd992010-07-08 11:45:51 -0700676
Romain Guya1d3c912011-12-13 14:55:06 -0800677 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700678 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700679 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700680 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700681 }
682
Romain Guy9ace8f52011-07-07 20:50:11 -0700683 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700684 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700685 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
686 bounds.getWidth() / float(layer->getWidth()), 0.0f);
687 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700688 layer->setBlend(true);
Romain Guydda57022010-07-06 11:39:32 -0700689
Romain Guy8fb95422010-08-17 18:38:51 -0700690 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700691 mSnapshot->flags |= Snapshot::kFlagIsLayer;
692 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700693
Romain Guyeb993562010-10-05 18:14:38 -0700694 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700695 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700696 } else {
697 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700698 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800699 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700700 if (layer->isEmpty()) {
701 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700702 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700703 layer->getWidth(), layer->getHeight(), 0);
704 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800705 } else {
706 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700707 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800708 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700709
Romain Guy54be1cd2011-06-13 19:04:27 -0700710 // Enqueue the buffer coordinates to clear the corresponding region later
711 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700712 }
Romain Guyeb993562010-10-05 18:14:38 -0700713 }
Romain Guyf86ef572010-07-01 11:05:42 -0700714
Romain Guyd55a8612010-06-28 17:42:46 -0700715 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700716}
717
Chet Haased48885a2012-08-28 17:43:28 -0700718bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700719 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700720
Chet Haased48885a2012-08-28 17:43:28 -0700721 mSnapshot->region = &mSnapshot->layer->region;
722 mSnapshot->flags |= Snapshot::kFlagFboTarget;
Romain Guy5b3b3522010-10-27 18:57:51 -0700723
Chet Haased48885a2012-08-28 17:43:28 -0700724 mSnapshot->flags |= Snapshot::kFlagIsFboLayer;
725 mSnapshot->fbo = layer->getFbo();
726 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
727 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
728 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
729 mSnapshot->height = bounds.getHeight();
730 mSnapshot->flags |= Snapshot::kFlagDirtyOrtho;
731 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700732
Romain Guy2b7028e2012-09-19 17:25:38 -0700733 endTiling();
Romain Guy5b3b3522010-10-27 18:57:51 -0700734 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700735 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
736 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700737
738 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700739 if (layer->isEmpty()) {
740 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
741 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700742 }
743
744 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700745 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700746
Romain Guy2b7028e2012-09-19 17:25:38 -0700747 startTiling(mSnapshot);
Romain Guy5b3b3522010-10-27 18:57:51 -0700748
749 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700750 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800751 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700752 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700753 glClear(GL_COLOR_BUFFER_BIT);
754
755 dirtyClip();
756
757 // Change the ortho projection
758 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
759 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
760
761 return true;
762}
763
Romain Guy1c740bc2010-09-13 18:00:09 -0700764/**
765 * Read the documentation of createLayer() before doing anything in this method.
766 */
Romain Guy1d83e192010-08-17 11:37:00 -0700767void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
768 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000769 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700770 return;
771 }
772
Romain Guy5b3b3522010-10-27 18:57:51 -0700773 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700774
775 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700776 endTiling();
777
Romain Guye0aa84b2012-04-03 19:30:26 -0700778 // Detach the texture from the FBO
779 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guyeb993562010-10-05 18:14:38 -0700780 // Unbind current FBO and restore previous one
781 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy2b7028e2012-09-19 17:25:38 -0700782
783 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700784 }
785
Romain Guy1d83e192010-08-17 11:37:00 -0700786 Layer* layer = current->layer;
787 const Rect& rect = layer->layer;
788
Romain Guy9ace8f52011-07-07 20:50:11 -0700789 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700790 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700791 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700792 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700793 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700794 }
795
Romain Guy03750a02010-10-18 14:06:08 -0700796 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700797
Romain Guya1d3c912011-12-13 14:55:06 -0800798 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700799
Romain Guy5b3b3522010-10-27 18:57:51 -0700800 // When the layer is stored in an FBO, we can save a bit of fillrate by
801 // drawing only the dirty region
802 if (fboLayer) {
803 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700804 if (layer->getColorFilter()) {
805 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800806 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700807 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700808 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800809 resetColorFilter();
810 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700811 } else if (!rect.isEmpty()) {
812 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
813 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700814 }
Romain Guy8b55f372010-08-18 17:10:07 -0700815
Romain Guyeb993562010-10-05 18:14:38 -0700816 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800817 // Note: No need to use glDiscardFramebufferEXT() since we never
818 // create/compose layers that are not on screen with this
819 // code path
820 // See LayerRenderer::destroyLayer(Layer*)
821
Romain Guyeb993562010-10-05 18:14:38 -0700822 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
823 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700824 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700825 }
826
Romain Guy746b7402010-10-26 16:27:31 -0700827 dirtyClip();
828
Romain Guyeb993562010-10-05 18:14:38 -0700829 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700830 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700831 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700832 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700833 }
834}
835
Romain Guyaa6c24c2011-04-28 18:40:04 -0700836void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700837 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700838
Romain Guy302a9df2011-08-16 13:55:02 -0700839 mat4& transform = layer->getTransform();
840 if (!transform.isIdentity()) {
841 save(0);
842 mSnapshot->transform->multiply(transform);
843 }
844
Romain Guyaa6c24c2011-04-28 18:40:04 -0700845 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700846 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700847 setupDrawWithTexture();
848 } else {
849 setupDrawWithExternalTexture();
850 }
851 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700852 setupDrawColor(alpha, alpha, alpha, alpha);
853 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700854 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700855 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700856 setupDrawPureColorUniforms();
857 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700858 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
859 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700860 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700861 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700862 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700863 if (mSnapshot->transform->isPureTranslate() &&
864 layer->getWidth() == (uint32_t) rect.getWidth() &&
865 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700866 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
867 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
868
Romain Guyd21b6e12011-11-30 20:21:23 -0800869 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700870 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
871 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800872 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700873 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
874 }
875 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700876 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
877
878 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
879
880 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700881
882 if (!transform.isIdentity()) {
883 restore();
884 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700885}
886
Romain Guy5b3b3522010-10-27 18:57:51 -0700887void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700888 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700889 const Rect& texCoords = layer->texCoords;
890 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
891 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700892
Romain Guy9ace8f52011-07-07 20:50:11 -0700893 float x = rect.left;
894 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700895 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700896 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700897 layer->getHeight() == (uint32_t) rect.getHeight();
898
899 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700900 // When we're swapping, the layer is already in screen coordinates
901 if (!swap) {
902 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
903 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
904 }
905
Romain Guyd21b6e12011-11-30 20:21:23 -0800906 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700907 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800908 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700909 }
910
911 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
912 layer->getTexture(), layer->getAlpha() / 255.0f,
913 layer->getMode(), layer->isBlend(),
914 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
915 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700916
Romain Guyaa6c24c2011-04-28 18:40:04 -0700917 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
918 } else {
919 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
920 drawTextureLayer(layer, rect);
921 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
922 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700923}
924
925void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700926 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700927 layer->setRegionAsRect();
928
Romain Guy40667672011-03-18 14:34:03 -0700929 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700930
Romain Guy5b3b3522010-10-27 18:57:51 -0700931 layer->region.clear();
932 return;
933 }
934
Romain Guy8a3957d2011-09-07 17:55:15 -0700935 // TODO: See LayerRenderer.cpp::generateMesh() for important
936 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800937 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700938 size_t count;
939 const android::Rect* rects = layer->region.getArray(&count);
940
Romain Guy9ace8f52011-07-07 20:50:11 -0700941 const float alpha = layer->getAlpha() / 255.0f;
942 const float texX = 1.0f / float(layer->getWidth());
943 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800944 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700945
946 TextureVertex* mesh = mCaches.getRegionMesh();
947 GLsizei numQuads = 0;
948
Romain Guy7230a742011-01-10 22:26:16 -0800949 setupDraw();
950 setupDrawWithTexture();
951 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800952 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700953 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800954 setupDrawProgram();
955 setupDrawDirtyRegionsDisabled();
956 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800957 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700958 setupDrawTexture(layer->getTexture());
959 if (mSnapshot->transform->isPureTranslate()) {
960 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
961 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
962
Romain Guyd21b6e12011-11-30 20:21:23 -0800963 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700964 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
965 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800966 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700967 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
968 }
Romain Guy15bc6432011-12-13 13:11:32 -0800969 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700970
971 for (size_t i = 0; i < count; i++) {
972 const android::Rect* r = &rects[i];
973
974 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800975 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700976 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800977 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700978
979 // TODO: Reject quads outside of the clip
980 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
981 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
982 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
983 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
984
985 numQuads++;
986
987 if (numQuads >= REGION_MESH_QUAD_COUNT) {
988 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
989 numQuads = 0;
990 mesh = mCaches.getRegionMesh();
991 }
992 }
993
994 if (numQuads > 0) {
995 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
996 }
997
Romain Guy7230a742011-01-10 22:26:16 -0800998 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700999
1000#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001001 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001002#endif
1003
1004 layer->region.clear();
1005 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001006}
1007
Romain Guy3a3133d2011-02-01 22:59:58 -08001008void OpenGLRenderer::drawRegionRects(const Region& region) {
1009#if DEBUG_LAYERS_AS_REGIONS
1010 size_t count;
1011 const android::Rect* rects = region.getArray(&count);
1012
1013 uint32_t colors[] = {
1014 0x7fff0000, 0x7f00ff00,
1015 0x7f0000ff, 0x7fff00ff,
1016 };
1017
1018 int offset = 0;
1019 int32_t top = rects[0].top;
1020
1021 for (size_t i = 0; i < count; i++) {
1022 if (top != rects[i].top) {
1023 offset ^= 0x2;
1024 top = rects[i].top;
1025 }
1026
1027 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1028 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1029 SkXfermode::kSrcOver_Mode);
1030 }
1031#endif
1032}
1033
Romain Guy5b3b3522010-10-27 18:57:51 -07001034void OpenGLRenderer::dirtyLayer(const float left, const float top,
1035 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001036 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001037 Rect bounds(left, top, right, bottom);
1038 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001039 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001040 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001041}
1042
1043void OpenGLRenderer::dirtyLayer(const float left, const float top,
1044 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001045 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001046 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001047 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001048 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001049}
1050
1051void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001052 if (bounds.intersect(*mSnapshot->clipRect)) {
1053 bounds.snapToPixelBoundaries();
1054 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1055 if (!dirty.isEmpty()) {
1056 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001057 }
1058 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001059}
1060
Romain Guy54be1cd2011-06-13 19:04:27 -07001061void OpenGLRenderer::clearLayerRegions() {
1062 const size_t count = mLayers.size();
1063 if (count == 0) return;
1064
1065 if (!mSnapshot->isIgnored()) {
1066 // Doing several glScissor/glClear here can negatively impact
1067 // GPUs with a tiler architecture, instead we draw quads with
1068 // the Clear blending mode
1069
1070 // The list contains bounds that have already been clipped
1071 // against their initial clip rect, and the current clip
1072 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001073 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001074
1075 Vertex mesh[count * 6];
1076 Vertex* vertex = mesh;
1077
1078 for (uint32_t i = 0; i < count; i++) {
1079 Rect* bounds = mLayers.itemAt(i);
1080
1081 Vertex::set(vertex++, bounds->left, bounds->bottom);
1082 Vertex::set(vertex++, bounds->left, bounds->top);
1083 Vertex::set(vertex++, bounds->right, bounds->top);
1084 Vertex::set(vertex++, bounds->left, bounds->bottom);
1085 Vertex::set(vertex++, bounds->right, bounds->top);
1086 Vertex::set(vertex++, bounds->right, bounds->bottom);
1087
1088 delete bounds;
1089 }
1090
1091 setupDraw(false);
1092 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1093 setupDrawBlending(true, SkXfermode::kClear_Mode);
1094 setupDrawProgram();
1095 setupDrawPureColorUniforms();
1096 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001097 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001098
Romain Guy54be1cd2011-06-13 19:04:27 -07001099 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001100
1101 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001102 } else {
1103 for (uint32_t i = 0; i < count; i++) {
1104 delete mLayers.itemAt(i);
1105 }
1106 }
1107
1108 mLayers.clear();
1109}
1110
Romain Guybd6b79b2010-06-26 00:13:53 -07001111///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001112// Transforms
1113///////////////////////////////////////////////////////////////////////////////
1114
1115void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001116 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001117}
1118
1119void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001120 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001121}
1122
1123void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001124 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001125}
1126
Romain Guy807daf72011-01-18 11:19:19 -08001127void OpenGLRenderer::skew(float sx, float sy) {
1128 mSnapshot->transform->skew(sx, sy);
1129}
1130
Romain Guyf6a11b82010-06-23 17:47:49 -07001131void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001132 if (matrix) {
1133 mSnapshot->transform->load(*matrix);
1134 } else {
1135 mSnapshot->transform->loadIdentity();
1136 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001137}
1138
1139void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001140 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001141}
1142
1143void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001144 SkMatrix transform;
1145 mSnapshot->transform->copyTo(transform);
1146 transform.preConcat(*matrix);
1147 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001148}
1149
1150///////////////////////////////////////////////////////////////////////////////
1151// Clipping
1152///////////////////////////////////////////////////////////////////////////////
1153
Romain Guybb9524b2010-06-22 18:56:38 -07001154void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001155 Rect clip(*mSnapshot->clipRect);
1156 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001157
Romain Guy8a4ac612012-07-17 17:32:48 -07001158 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1159 clip.getWidth(), clip.getHeight())) {
1160 mDirtyClip = false;
1161 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001162}
1163
1164const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001165 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001166}
1167
Romain Guy8a4ac612012-07-17 17:32:48 -07001168bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1169 if (mSnapshot->isIgnored()) {
1170 return true;
1171 }
1172
1173 Rect r(left, top, right, bottom);
1174 mSnapshot->transform->mapRect(r);
1175 r.snapToPixelBoundaries();
1176
1177 Rect clipRect(*mSnapshot->clipRect);
1178 clipRect.snapToPixelBoundaries();
1179
1180 return !clipRect.intersects(r);
1181}
1182
Romain Guy35643dd2012-09-18 15:40:58 -07001183bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1184 Rect& transformed, Rect& clip) {
1185 if (mSnapshot->isIgnored()) {
1186 return true;
1187 }
1188
1189 transformed.set(left, top, right, bottom);
1190 mSnapshot->transform->mapRect(transformed);
1191 transformed.snapToPixelBoundaries();
1192
1193 clip.set(*mSnapshot->clipRect);
1194 clip.snapToPixelBoundaries();
1195
1196 return !clip.intersects(transformed);
1197}
1198
Romain Guyc7d53492010-06-25 13:41:57 -07001199bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001200 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001201 return true;
1202 }
1203
Romain Guy1d83e192010-08-17 11:37:00 -07001204 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001205 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001206 r.snapToPixelBoundaries();
1207
1208 Rect clipRect(*mSnapshot->clipRect);
1209 clipRect.snapToPixelBoundaries();
1210
Romain Guy586cae32012-07-13 15:28:31 -07001211 bool rejected = !clipRect.intersects(r);
1212 if (!isDeferred() && !rejected) {
1213 mCaches.setScissorEnabled(!clipRect.contains(r));
1214 }
1215
1216 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001217}
1218
Romain Guy079ba2c2010-07-16 14:12:24 -07001219bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1220 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001221 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001222 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001223 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001224 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001225}
1226
Chet Haasea23eed82012-04-12 15:19:04 -07001227Rect* OpenGLRenderer::getClipRect() {
1228 return mSnapshot->clipRect;
1229}
1230
Romain Guyf6a11b82010-06-23 17:47:49 -07001231///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001232// Drawing commands
1233///////////////////////////////////////////////////////////////////////////////
1234
Romain Guy54be1cd2011-06-13 19:04:27 -07001235void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001236 // TODO: It would be best if we could do this before quickReject()
1237 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001238 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001239 if (mDirtyClip) {
1240 setScissorFromClip();
1241 }
1242 mDescription.reset();
1243 mSetShaderColor = false;
1244 mColorSet = false;
1245 mColorA = mColorR = mColorG = mColorB = 0.0f;
1246 mTextureUnit = 0;
1247 mTrackDirtyRegions = true;
1248}
1249
1250void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1251 mDescription.hasTexture = true;
1252 mDescription.hasAlpha8Texture = isAlpha8;
1253}
1254
Romain Guyaa6c24c2011-04-28 18:40:04 -07001255void OpenGLRenderer::setupDrawWithExternalTexture() {
1256 mDescription.hasExternalTexture = true;
1257}
1258
Romain Guy15bc6432011-12-13 13:11:32 -08001259void OpenGLRenderer::setupDrawNoTexture() {
1260 mCaches.disbaleTexCoordsVertexArray();
1261}
1262
Chet Haase5b0200b2011-04-13 17:58:08 -07001263void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001264 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001265}
1266
Chris Craik6ebdc112012-08-31 18:24:33 -07001267void OpenGLRenderer::setupDrawAARect() {
1268 mDescription.isAARect = true;
1269}
1270
Romain Guyed6fcb02011-03-21 13:11:28 -07001271void OpenGLRenderer::setupDrawPoint(float pointSize) {
1272 mDescription.isPoint = true;
1273 mDescription.pointSize = pointSize;
1274}
1275
Romain Guy70ca14e2010-12-13 18:24:33 -08001276void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001277 setupDrawColor(color, (color >> 24) & 0xFF);
1278}
1279
1280void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1281 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001282 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1283 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001284 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001285 mColorR = a * ((color >> 16) & 0xFF);
1286 mColorG = a * ((color >> 8) & 0xFF);
1287 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001288 mColorSet = true;
1289 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1290}
1291
Romain Guy86568192010-12-14 15:55:39 -08001292void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1293 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001294 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1295 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001296 const float a = mColorA / 255.0f;
1297 mColorR = a * ((color >> 16) & 0xFF);
1298 mColorG = a * ((color >> 8) & 0xFF);
1299 mColorB = a * ((color ) & 0xFF);
1300 mColorSet = true;
1301 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1302}
1303
Romain Guy41210632012-07-16 17:04:24 -07001304void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1305 mCaches.fontRenderer->describe(mDescription, paint);
1306}
1307
Romain Guy70ca14e2010-12-13 18:24:33 -08001308void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1309 mColorA = a;
1310 mColorR = r;
1311 mColorG = g;
1312 mColorB = b;
1313 mColorSet = true;
1314 mSetShaderColor = mDescription.setColor(r, g, b, a);
1315}
1316
1317void OpenGLRenderer::setupDrawShader() {
1318 if (mShader) {
1319 mShader->describe(mDescription, mCaches.extensions);
1320 }
1321}
1322
1323void OpenGLRenderer::setupDrawColorFilter() {
1324 if (mColorFilter) {
1325 mColorFilter->describe(mDescription, mCaches.extensions);
1326 }
1327}
1328
Romain Guyf09ef512011-05-27 11:43:46 -07001329void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1330 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1331 mColorA = 1.0f;
1332 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001333 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001334 }
1335}
1336
Romain Guy70ca14e2010-12-13 18:24:33 -08001337void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001338 // When the blending mode is kClear_Mode, we need to use a modulate color
1339 // argb=1,0,0,0
1340 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001341 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1342 mDescription, swapSrcDst);
1343}
1344
1345void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001346 // When the blending mode is kClear_Mode, we need to use a modulate color
1347 // argb=1,0,0,0
1348 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001349 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1350 mDescription, swapSrcDst);
1351}
1352
1353void OpenGLRenderer::setupDrawProgram() {
1354 useProgram(mCaches.programCache.get(mDescription));
1355}
1356
1357void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1358 mTrackDirtyRegions = false;
1359}
1360
1361void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1362 bool ignoreTransform) {
1363 mModelView.loadTranslate(left, top, 0.0f);
1364 if (!ignoreTransform) {
1365 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1366 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1367 } else {
1368 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1369 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1370 }
1371}
1372
Chet Haase8a5cc922011-04-26 07:28:09 -07001373void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1374 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001375}
1376
Romain Guy70ca14e2010-12-13 18:24:33 -08001377void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1378 bool ignoreTransform, bool ignoreModelView) {
1379 if (!ignoreModelView) {
1380 mModelView.loadTranslate(left, top, 0.0f);
1381 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001382 } else {
1383 mModelView.loadIdentity();
1384 }
Romain Guy86568192010-12-14 15:55:39 -08001385 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1386 if (!ignoreTransform) {
1387 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1388 if (mTrackDirtyRegions && dirty) {
1389 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1390 }
1391 } else {
1392 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1393 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1394 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001395}
1396
Romain Guyed6fcb02011-03-21 13:11:28 -07001397void OpenGLRenderer::setupDrawPointUniforms() {
1398 int slot = mCaches.currentProgram->getUniform("pointSize");
1399 glUniform1f(slot, mDescription.pointSize);
1400}
1401
Romain Guy70ca14e2010-12-13 18:24:33 -08001402void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001403 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001404 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1405 }
1406}
1407
Romain Guy86568192010-12-14 15:55:39 -08001408void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001409 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001410 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001411 }
1412}
1413
Romain Guy70ca14e2010-12-13 18:24:33 -08001414void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1415 if (mShader) {
1416 if (ignoreTransform) {
1417 mModelView.loadInverse(*mSnapshot->transform);
1418 }
1419 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1420 }
1421}
1422
Romain Guy8d0d4782010-12-14 20:13:35 -08001423void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1424 if (mShader) {
1425 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1426 }
1427}
1428
Romain Guy70ca14e2010-12-13 18:24:33 -08001429void OpenGLRenderer::setupDrawColorFilterUniforms() {
1430 if (mColorFilter) {
1431 mColorFilter->setupProgram(mCaches.currentProgram);
1432 }
1433}
1434
Romain Guy41210632012-07-16 17:04:24 -07001435void OpenGLRenderer::setupDrawTextGammaUniforms() {
1436 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1437}
1438
Romain Guy70ca14e2010-12-13 18:24:33 -08001439void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001440 bool force = mCaches.bindMeshBuffer();
1441 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001442 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001443}
1444
1445void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1446 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001447 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001448 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001449}
1450
Romain Guyaa6c24c2011-04-28 18:40:04 -07001451void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1452 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001453 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001454 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001455}
1456
Romain Guy8f0095c2011-05-02 17:24:22 -07001457void OpenGLRenderer::setupDrawTextureTransform() {
1458 mDescription.hasTextureTransform = true;
1459}
1460
1461void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001462 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1463 GL_FALSE, &transform.data[0]);
1464}
1465
Romain Guy70ca14e2010-12-13 18:24:33 -08001466void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001467 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001468 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001469 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001470 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001471 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001472 }
Romain Guyd71dd362011-12-12 19:03:35 -08001473
Romain Guyf3a910b42011-12-12 20:35:21 -08001474 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001475 if (mCaches.currentProgram->texCoords >= 0) {
1476 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1477 }
1478
1479 mCaches.unbindIndicesBuffer();
1480}
1481
1482void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1483 bool force = mCaches.unbindMeshBuffer();
1484 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1485 if (mCaches.currentProgram->texCoords >= 0) {
1486 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001487 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001488}
1489
Chet Haase5b0200b2011-04-13 17:58:08 -07001490void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001491 bool force = mCaches.unbindMeshBuffer();
1492 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1493 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001494 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001495}
1496
1497/**
1498 * 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 -07001499 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1500 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1501 * attributes (one per vertex) are values from zero to one that tells the fragment
1502 * shader where the fragment is in relation to the line width/length overall; these values are
1503 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1504 * region of the line.
1505 * Note that we only pass down the width values in this setup function. The length coordinates
1506 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001507 */
Chet Haase99585ad2011-05-02 15:00:16 -07001508void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001509 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001510 bool force = mCaches.unbindMeshBuffer();
1511 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1512 vertices, gAAVertexStride);
1513 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001514 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001515
Romain Guy7b631422012-04-04 11:38:54 -07001516 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001517 glEnableVertexAttribArray(widthSlot);
1518 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001519
Romain Guy7b631422012-04-04 11:38:54 -07001520 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001521 glEnableVertexAttribArray(lengthSlot);
1522 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001523
Chet Haase5b0200b2011-04-13 17:58:08 -07001524 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001525 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001526}
1527
1528void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1529 glDisableVertexAttribArray(widthSlot);
1530 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001531}
1532
Romain Guy70ca14e2010-12-13 18:24:33 -08001533void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001534}
1535
1536///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001537// Drawing
1538///////////////////////////////////////////////////////////////////////////////
1539
Chet Haase1271e2c2012-04-20 09:54:27 -07001540status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001541 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001542
Romain Guy0fe478e2010-11-08 12:08:41 -08001543 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1544 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001545 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001546 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001547 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001548
Romain Guy65549432012-03-26 16:45:05 -07001549 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001550}
1551
Chet Haaseed30fd82011-04-22 16:18:45 -07001552void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1553 if (displayList) {
1554 displayList->output(*this, level);
1555 }
1556}
1557
Romain Guya168d732011-03-18 16:50:13 -07001558void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1559 int alpha;
1560 SkXfermode::Mode mode;
1561 getAlphaAndMode(paint, &alpha, &mode);
1562
Romain Guya168d732011-03-18 16:50:13 -07001563 float x = left;
1564 float y = top;
1565
Romain Guye3c26852011-07-25 16:36:01 -07001566 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001567 bool ignoreTransform = false;
1568 if (mSnapshot->transform->isPureTranslate()) {
1569 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1570 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1571 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001572 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001573 } else {
1574 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001575 }
1576
1577 setupDraw();
1578 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001579 if (paint) {
1580 setupDrawAlpha8Color(paint->getColor(), alpha);
1581 }
Romain Guya168d732011-03-18 16:50:13 -07001582 setupDrawColorFilter();
1583 setupDrawShader();
1584 setupDrawBlending(true, mode);
1585 setupDrawProgram();
1586 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001587
Romain Guya168d732011-03-18 16:50:13 -07001588 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001589 texture->setWrap(GL_CLAMP_TO_EDGE);
1590 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001591
Romain Guya168d732011-03-18 16:50:13 -07001592 setupDrawPureColorUniforms();
1593 setupDrawColorFilterUniforms();
1594 setupDrawShaderUniforms();
1595 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1596
1597 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1598
1599 finishDrawTexture();
1600}
1601
Chet Haase48659092012-05-31 15:21:51 -07001602status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001603 const float right = left + bitmap->width();
1604 const float bottom = top + bitmap->height();
1605
1606 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001607 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001608 }
1609
Romain Guya1d3c912011-12-13 14:55:06 -08001610 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001611 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001612 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001613 const AutoTexture autoCleanup(texture);
1614
Romain Guy211370f2012-02-01 16:10:55 -08001615 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001616 drawAlphaBitmap(texture, left, top, paint);
1617 } else {
1618 drawTextureRect(left, top, right, bottom, texture, paint);
1619 }
Chet Haase48659092012-05-31 15:21:51 -07001620
1621 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001622}
1623
Chet Haase48659092012-05-31 15:21:51 -07001624status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001625 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1626 const mat4 transform(*matrix);
1627 transform.mapRect(r);
1628
Romain Guy6926c722010-07-12 20:20:03 -07001629 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001630 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001631 }
1632
Romain Guya1d3c912011-12-13 14:55:06 -08001633 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001634 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001635 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001636 const AutoTexture autoCleanup(texture);
1637
Romain Guy5b3b3522010-10-27 18:57:51 -07001638 // This could be done in a cheaper way, all we need is pass the matrix
1639 // to the vertex shader. The save/restore is a bit overkill.
1640 save(SkCanvas::kMatrix_SaveFlag);
1641 concatMatrix(matrix);
1642 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1643 restore();
Chet Haase48659092012-05-31 15:21:51 -07001644
1645 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001646}
1647
Chet Haase48659092012-05-31 15:21:51 -07001648status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001649 const float right = left + bitmap->width();
1650 const float bottom = top + bitmap->height();
1651
1652 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001653 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001654 }
1655
1656 mCaches.activeTexture(0);
1657 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1658 const AutoTexture autoCleanup(texture);
1659
1660 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001661
1662 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001663}
1664
Chet Haase48659092012-05-31 15:21:51 -07001665status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001666 float* vertices, int* colors, SkPaint* paint) {
1667 // TODO: Do a quickReject
1668 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001669 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001670 }
1671
Romain Guya1d3c912011-12-13 14:55:06 -08001672 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001673 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001674 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001675 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001676
Romain Guyd21b6e12011-11-30 20:21:23 -08001677 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1678 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001679
1680 int alpha;
1681 SkXfermode::Mode mode;
1682 getAlphaAndMode(paint, &alpha, &mode);
1683
Romain Guy5a7b4662011-01-20 19:09:30 -08001684 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001685
Romain Guyb18d2d02011-02-10 15:52:54 -08001686 float left = FLT_MAX;
1687 float top = FLT_MAX;
1688 float right = FLT_MIN;
1689 float bottom = FLT_MIN;
1690
Romain Guy211370f2012-02-01 16:10:55 -08001691 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001692
Romain Guya566b7c2011-01-23 16:36:11 -08001693 // TODO: Support the colors array
1694 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001695 TextureVertex* vertex = mesh;
1696 for (int32_t y = 0; y < meshHeight; y++) {
1697 for (int32_t x = 0; x < meshWidth; x++) {
1698 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1699
1700 float u1 = float(x) / meshWidth;
1701 float u2 = float(x + 1) / meshWidth;
1702 float v1 = float(y) / meshHeight;
1703 float v2 = float(y + 1) / meshHeight;
1704
1705 int ax = i + (meshWidth + 1) * 2;
1706 int ay = ax + 1;
1707 int bx = i;
1708 int by = bx + 1;
1709 int cx = i + 2;
1710 int cy = cx + 1;
1711 int dx = i + (meshWidth + 1) * 2 + 2;
1712 int dy = dx + 1;
1713
1714 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1715 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1716 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1717
1718 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1719 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1720 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001721
Romain Guyb18d2d02011-02-10 15:52:54 -08001722 if (hasActiveLayer) {
1723 // TODO: This could be optimized to avoid unnecessary ops
1724 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1725 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1726 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1727 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1728 }
Romain Guy5a7b4662011-01-20 19:09:30 -08001729 }
1730 }
1731
Romain Guyb18d2d02011-02-10 15:52:54 -08001732 if (hasActiveLayer) {
1733 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1734 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001735
Romain Guy5a7b4662011-01-20 19:09:30 -08001736 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1737 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001738 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001739
1740 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001741}
1742
Chet Haase48659092012-05-31 15:21:51 -07001743status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001744 float srcLeft, float srcTop, float srcRight, float srcBottom,
1745 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001746 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001747 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001748 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001749 }
1750
Romain Guya1d3c912011-12-13 14:55:06 -08001751 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001752 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001753 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001754 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001755
Romain Guy8ba548f2010-06-30 19:21:21 -07001756 const float width = texture->width;
1757 const float height = texture->height;
1758
Romain Guy68169722011-08-22 17:33:33 -07001759 const float u1 = fmax(0.0f, srcLeft / width);
1760 const float v1 = fmax(0.0f, srcTop / height);
1761 const float u2 = fmin(1.0f, srcRight / width);
1762 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001763
Romain Guy03750a02010-10-18 14:06:08 -07001764 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001765 resetDrawTextureTexCoords(u1, v1, u2, v2);
1766
Romain Guy03750a02010-10-18 14:06:08 -07001767 int alpha;
1768 SkXfermode::Mode mode;
1769 getAlphaAndMode(paint, &alpha, &mode);
1770
Romain Guyd21b6e12011-11-30 20:21:23 -08001771 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1772
Romain Guy211370f2012-02-01 16:10:55 -08001773 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001774 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1775 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1776
Romain Guyb5014982011-07-28 15:39:12 -07001777 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001778 // Enable linear filtering if the source rectangle is scaled
1779 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001780 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001781 }
Romain Guyb5014982011-07-28 15:39:12 -07001782
Romain Guyd21b6e12011-11-30 20:21:23 -08001783 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001784 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1785 texture->id, alpha / 255.0f, mode, texture->blend,
1786 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1787 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1788 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001789 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001790 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1791 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1792 GL_TRIANGLE_STRIP, gMeshCount);
1793 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001794
1795 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001796
1797 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001798}
1799
Chet Haase48659092012-05-31 15:21:51 -07001800status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001801 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001802 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001803 int alpha;
1804 SkXfermode::Mode mode;
1805 getAlphaAndModeDirect(paint, &alpha, &mode);
1806
1807 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1808 left, top, right, bottom, alpha, mode);
1809}
1810
1811status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1812 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1813 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001814 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001815 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001816 }
1817
Romain Guybe6f9dc2012-07-16 12:41:17 -07001818 alpha *= mSnapshot->alpha;
1819
Romain Guya1d3c912011-12-13 14:55:06 -08001820 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001821 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001822 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001823 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001824 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1825 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001826
Romain Guy2728f962010-10-08 18:36:15 -07001827 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001828 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001829
Romain Guy211370f2012-02-01 16:10:55 -08001830 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001831 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001832 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001833 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001834 const float offsetX = left + mSnapshot->transform->getTranslateX();
1835 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001836 const size_t count = mesh->quads.size();
1837 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001838 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001839 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001840 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1841 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1842 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001843 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001844 dirtyLayer(left + bounds.left, top + bounds.top,
1845 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001846 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001847 }
1848 }
1849
Romain Guy211370f2012-02-01 16:10:55 -08001850 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001851 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1852 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1853
1854 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1855 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1856 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1857 true, !mesh->hasEmptyQuads);
1858 } else {
1859 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1860 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1861 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1862 true, !mesh->hasEmptyQuads);
1863 }
Romain Guy054dc182010-10-15 17:55:25 -07001864 }
Chet Haase48659092012-05-31 15:21:51 -07001865
1866 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001867}
1868
Chet Haase99ecdc42011-05-06 12:06:34 -07001869/**
Chet Haase858aa932011-05-12 09:06:00 -07001870 * This function uses a similar approach to that of AA lines in the drawLines() function.
Chris Craik6ebdc112012-08-31 18:24:33 -07001871 * We expand the rectangle by a half pixel in screen space on all sides. However, instead of using
1872 * a fragment shader to compute the translucency of the color from its position, we simply use a
1873 * varying parameter to define how far a given pixel is into the region.
Chet Haase858aa932011-05-12 09:06:00 -07001874 */
1875void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001876 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001877 float inverseScaleX = 1.0f;
1878 float inverseScaleY = 1.0f;
Romain Guy04299382012-07-18 17:15:41 -07001879
Chet Haase858aa932011-05-12 09:06:00 -07001880 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001881 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001882 Matrix4 *mat = mSnapshot->transform;
1883 float m00 = mat->data[Matrix4::kScaleX];
1884 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase858aa932011-05-12 09:06:00 -07001885 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07001886 float m11 = mat->data[Matrix4::kScaleY];
Chet Haase858aa932011-05-12 09:06:00 -07001887 float scaleX = sqrt(m00 * m00 + m01 * m01);
1888 float scaleY = sqrt(m10 * m10 + m11 * m11);
1889 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1890 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1891 }
1892
Chet Haase858aa932011-05-12 09:06:00 -07001893 float boundarySizeX = .5 * inverseScaleX;
1894 float boundarySizeY = .5 * inverseScaleY;
1895
Chris Craik6ebdc112012-08-31 18:24:33 -07001896 float innerLeft = left + boundarySizeX;
1897 float innerRight = right - boundarySizeX;
1898 float innerTop = top + boundarySizeY;
1899 float innerBottom = bottom - boundarySizeY;
1900
Chet Haase858aa932011-05-12 09:06:00 -07001901 // Adjust the rect by the AA boundary padding
1902 left -= boundarySizeX;
1903 right += boundarySizeX;
1904 top -= boundarySizeY;
1905 bottom += boundarySizeY;
1906
Chet Haase858aa932011-05-12 09:06:00 -07001907 if (!quickReject(left, top, right, bottom)) {
Romain Guy04299382012-07-18 17:15:41 -07001908 setupDraw();
1909 setupDrawNoTexture();
Chris Craik6ebdc112012-08-31 18:24:33 -07001910 setupDrawAARect();
Romain Guy04299382012-07-18 17:15:41 -07001911 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1912 setupDrawColorFilter();
1913 setupDrawShader();
1914 setupDrawBlending(true, mode);
1915 setupDrawProgram();
1916 setupDrawModelViewIdentity(true);
1917 setupDrawColorUniforms();
1918 setupDrawColorFilterUniforms();
1919 setupDrawShaderIdentityUniforms();
1920
Chris Craik6ebdc112012-08-31 18:24:33 -07001921 AlphaVertex rects[14];
1922 AlphaVertex* aVertices = &rects[0];
1923 void* alphaCoords = ((GLbyte*) aVertices) + gVertexAlphaOffset;
Romain Guy04299382012-07-18 17:15:41 -07001924
Chris Craik6ebdc112012-08-31 18:24:33 -07001925 bool force = mCaches.unbindMeshBuffer();
1926 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1927 aVertices, gAlphaVertexStride);
1928 mCaches.resetTexCoordsVertexPointer();
1929 mCaches.unbindIndicesBuffer();
Romain Guy04299382012-07-18 17:15:41 -07001930
Chris Craik6ebdc112012-08-31 18:24:33 -07001931 int alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
1932 glEnableVertexAttribArray(alphaSlot);
1933 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Romain Guy04299382012-07-18 17:15:41 -07001934
Chris Craik6ebdc112012-08-31 18:24:33 -07001935 // draw left
1936 AlphaVertex::set(aVertices++, left, bottom, 0);
1937 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1938 AlphaVertex::set(aVertices++, left, top, 0);
1939 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001940
Chris Craik6ebdc112012-08-31 18:24:33 -07001941 // draw top
1942 AlphaVertex::set(aVertices++, right, top, 0);
1943 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001944
Chris Craik6ebdc112012-08-31 18:24:33 -07001945 // draw right
1946 AlphaVertex::set(aVertices++, right, bottom, 0);
1947 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1948
1949 // draw bottom
1950 AlphaVertex::set(aVertices++, left, bottom, 0);
1951 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1952
1953 // draw inner rect (repeating last vertex to create degenerate bridge triangles)
1954 // TODO: also consider drawing the inner rect without the blending-forced shader, if
1955 // blending is expensive. Note: can't use drawColorRect() since it doesn't use vertex
1956 // buffers like below, resulting in slightly different transformed coordinates.
1957 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1958 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
1959 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1960 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
1961
Chet Haase858aa932011-05-12 09:06:00 -07001962 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guy7b631422012-04-04 11:38:54 -07001963
Chris Craik6ebdc112012-08-31 18:24:33 -07001964 glDrawArrays(GL_TRIANGLE_STRIP, 0, 14);
Romain Guy04299382012-07-18 17:15:41 -07001965
Chris Craik6ebdc112012-08-31 18:24:33 -07001966 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07001967 }
Chet Haase858aa932011-05-12 09:06:00 -07001968}
1969
1970/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001971 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1972 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1973 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1974 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1975 * of the line. Hairlines are more involved because we need to account for transform scaling
1976 * to end up with a one-pixel-wide line in screen space..
1977 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1978 * in combination with values that we calculate and pass down in this method. The basic approach
1979 * is that the quad we create contains both the core line area plus a bounding area in which
1980 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1981 * proportion of the width and the length of a given segment is represented by the boundary
1982 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1983 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1984 * on the inside). This ends up giving the result we want, with pixels that are completely
1985 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1986 * how far into the boundary region they are, which is determined by shader interpolation.
1987 */
Chet Haase48659092012-05-31 15:21:51 -07001988status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1989 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07001990
1991 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001992 // We use half the stroke width here because we're going to position the quad
1993 // corner vertices half of the width away from the line endpoints
1994 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001995 // A stroke width of 0 has a special meaning in Skia:
1996 // it draws a line 1 px wide regardless of current transform
1997 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001998
Chet Haase99ecdc42011-05-06 12:06:34 -07001999 float inverseScaleX = 1.0f;
2000 float inverseScaleY = 1.0f;
2001 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07002002
Chet Haase8a5cc922011-04-26 07:28:09 -07002003 int alpha;
2004 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07002005
Chet Haase8a5cc922011-04-26 07:28:09 -07002006 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002007 int verticesCount = count;
2008 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07002009 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07002010 verticesCount += (count - 4);
2011 }
2012
2013 if (isHairLine || isAA) {
2014 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
2015 // the line on the screen should always be one pixel wide regardless of scale. For
2016 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08002017 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07002018 Matrix4 *mat = mSnapshot->transform;
2019 float m00 = mat->data[Matrix4::kScaleX];
2020 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07002021 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07002022 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07002023
Romain Guy211370f2012-02-01 16:10:55 -08002024 float scaleX = sqrtf(m00 * m00 + m01 * m01);
2025 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07002026
Chet Haase99ecdc42011-05-06 12:06:34 -07002027 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
2028 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07002029
Chet Haase99ecdc42011-05-06 12:06:34 -07002030 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
2031 scaled = true;
2032 }
2033 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002034 }
Chet Haase8a5cc922011-04-26 07:28:09 -07002035
2036 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07002037
2038 mCaches.enableScissor();
2039
Chet Haase8a5cc922011-04-26 07:28:09 -07002040 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002041 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002042 if (isAA) {
2043 setupDrawAALine();
2044 }
2045 setupDrawColor(paint->getColor(), alpha);
2046 setupDrawColorFilter();
2047 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08002048 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07002049 setupDrawProgram();
2050 setupDrawModelViewIdentity(true);
2051 setupDrawColorUniforms();
2052 setupDrawColorFilterUniforms();
2053 setupDrawShaderIdentityUniforms();
2054
2055 if (isHairLine) {
2056 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07002057 halfStrokeWidth = isAA? 1 : .5;
2058 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002059 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07002060 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07002061 }
Romain Guy7b631422012-04-04 11:38:54 -07002062
2063 int widthSlot;
2064 int lengthSlot;
2065
Chet Haase5b0200b2011-04-13 17:58:08 -07002066 Vertex lines[verticesCount];
2067 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002068
Chet Haase99585ad2011-05-02 15:00:16 -07002069 AAVertex wLines[verticesCount];
2070 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002071
Romain Guy211370f2012-02-01 16:10:55 -08002072 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002073 setupDrawVertices(vertices);
2074 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002075 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2076 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002077 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002078 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2079 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002080 // We will need to calculate the actual width proportion on each segment for
2081 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002082 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002083 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2084 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002085 }
2086
Chet Haase99ecdc42011-05-06 12:06:34 -07002087 AAVertex* prevAAVertex = NULL;
2088 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002089
Chet Haase99585ad2011-05-02 15:00:16 -07002090 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002091 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002092
Chet Haase5b0200b2011-04-13 17:58:08 -07002093 for (int i = 0; i < count; i += 4) {
2094 // a = start point, b = end point
2095 vec2 a(points[i], points[i + 1]);
2096 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002097
Chet Haase99585ad2011-05-02 15:00:16 -07002098 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002099 float boundaryLengthProportion = 0;
2100 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002101
Chet Haase5b0200b2011-04-13 17:58:08 -07002102 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002103 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002104 float x = n.x;
2105 n.x = -n.y;
2106 n.y = x;
2107
Chet Haase8a5cc922011-04-26 07:28:09 -07002108 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002109 if (isAA) {
2110 float wideningFactor;
2111 if (fabs(n.x) >= fabs(n.y)) {
2112 wideningFactor = fabs(1.0f / n.x);
2113 } else {
2114 wideningFactor = fabs(1.0f / n.y);
2115 }
2116 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002117 }
Romain Guy7b631422012-04-04 11:38:54 -07002118
Chet Haase99ecdc42011-05-06 12:06:34 -07002119 if (scaled) {
2120 n.x *= inverseScaleX;
2121 n.y *= inverseScaleY;
2122 }
2123 } else if (scaled) {
2124 // Extend n by .5 pixel on each side, post-transform
2125 vec2 extendedN = n.copyNormalized();
2126 extendedN /= 2;
2127 extendedN.x *= inverseScaleX;
2128 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002129
Chet Haase99ecdc42011-05-06 12:06:34 -07002130 float extendedNLength = extendedN.length();
2131 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002132 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002133 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002134 }
Romain Guy7b631422012-04-04 11:38:54 -07002135
Chet Haase99585ad2011-05-02 15:00:16 -07002136 // aa lines expand the endpoint vertices to encompass the AA boundary
2137 if (isAA) {
2138 vec2 abVector = (b - a);
2139 length = abVector.length();
2140 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002141
Chet Haase99ecdc42011-05-06 12:06:34 -07002142 if (scaled) {
2143 abVector.x *= inverseScaleX;
2144 abVector.y *= inverseScaleY;
2145 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002146 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002147 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002148 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002149 }
Romain Guy7b631422012-04-04 11:38:54 -07002150
Chet Haase99ecdc42011-05-06 12:06:34 -07002151 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002152 a -= abVector;
2153 b += abVector;
2154 }
2155
Chet Haase5b0200b2011-04-13 17:58:08 -07002156 // Four corners of the rectangle defining a thick line
2157 vec2 p1 = a - n;
2158 vec2 p2 = a + n;
2159 vec2 p3 = b + n;
2160 vec2 p4 = b - n;
2161
Chet Haase99585ad2011-05-02 15:00:16 -07002162
Chet Haase5b0200b2011-04-13 17:58:08 -07002163 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2164 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2165 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2166 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2167
Romain Guy04299382012-07-18 17:15:41 -07002168 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002169 if (!isAA) {
2170 if (prevVertex != NULL) {
2171 // Issue two repeat vertices to create degenerate triangles to bridge
2172 // between the previous line and the new one. This is necessary because
2173 // we are creating a single triangle_strip which will contain
2174 // potentially discontinuous line segments.
2175 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2176 Vertex::set(vertices++, p1.x, p1.y);
2177 generatedVerticesCount += 2;
2178 }
Romain Guy7b631422012-04-04 11:38:54 -07002179
Chet Haase5b0200b2011-04-13 17:58:08 -07002180 Vertex::set(vertices++, p1.x, p1.y);
2181 Vertex::set(vertices++, p2.x, p2.y);
2182 Vertex::set(vertices++, p4.x, p4.y);
2183 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002184
Chet Haase5b0200b2011-04-13 17:58:08 -07002185 prevVertex = vertices - 1;
2186 generatedVerticesCount += 4;
2187 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002188 if (!isHairLine && scaled) {
2189 // Must set width proportions per-segment for scaled non-hairlines to use the
2190 // correct AA boundary dimensions
2191 if (boundaryWidthSlot < 0) {
2192 boundaryWidthSlot =
2193 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002194 }
Romain Guy7b631422012-04-04 11:38:54 -07002195
Chet Haase99ecdc42011-05-06 12:06:34 -07002196 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002197 }
Romain Guy7b631422012-04-04 11:38:54 -07002198
Chet Haase99585ad2011-05-02 15:00:16 -07002199 if (boundaryLengthSlot < 0) {
2200 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002201 }
Romain Guy7b631422012-04-04 11:38:54 -07002202
Chet Haase99ecdc42011-05-06 12:06:34 -07002203 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002204
Chet Haase5b0200b2011-04-13 17:58:08 -07002205 if (prevAAVertex != NULL) {
2206 // Issue two repeat vertices to create degenerate triangles to bridge
2207 // between the previous line and the new one. This is necessary because
2208 // we are creating a single triangle_strip which will contain
2209 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002210 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2211 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2212 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002213 generatedVerticesCount += 2;
2214 }
Romain Guy7b631422012-04-04 11:38:54 -07002215
Chet Haase99585ad2011-05-02 15:00:16 -07002216 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2217 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2218 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2219 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002220
Chet Haase5b0200b2011-04-13 17:58:08 -07002221 prevAAVertex = aaVertices - 1;
2222 generatedVerticesCount += 4;
2223 }
Romain Guy7b631422012-04-04 11:38:54 -07002224
Chet Haase99585ad2011-05-02 15:00:16 -07002225 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2226 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2227 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002228 }
2229 }
Romain Guy7b631422012-04-04 11:38:54 -07002230
Chet Haase5b0200b2011-04-13 17:58:08 -07002231 if (generatedVerticesCount > 0) {
2232 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2233 }
Romain Guy7b631422012-04-04 11:38:54 -07002234
2235 if (isAA) {
2236 finishDrawAALine(widthSlot, lengthSlot);
2237 }
Chet Haase48659092012-05-31 15:21:51 -07002238
2239 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002240}
2241
Chet Haase48659092012-05-31 15:21:51 -07002242status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2243 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002244
2245 // TODO: The paint's cap style defines whether the points are square or circular
2246 // TODO: Handle AA for round points
2247
Chet Haase5b0200b2011-04-13 17:58:08 -07002248 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002249 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002250 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002251 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002252 if (isHairLine) {
2253 // Now that we know it's hairline, we can set the effective width, to be used later
2254 strokeWidth = 1.0f;
2255 }
2256 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002257 int alpha;
2258 SkXfermode::Mode mode;
2259 getAlphaAndMode(paint, &alpha, &mode);
2260
2261 int verticesCount = count >> 1;
2262 int generatedVerticesCount = 0;
2263
2264 TextureVertex pointsData[verticesCount];
2265 TextureVertex* vertex = &pointsData[0];
2266
Romain Guy04299382012-07-18 17:15:41 -07002267 // TODO: We should optimize this method to not generate vertices for points
2268 // that lie outside of the clip.
2269 mCaches.enableScissor();
2270
Romain Guyed6fcb02011-03-21 13:11:28 -07002271 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002272 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002273 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002274 setupDrawColor(paint->getColor(), alpha);
2275 setupDrawColorFilter();
2276 setupDrawShader();
2277 setupDrawBlending(mode);
2278 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002279 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002280 setupDrawColorUniforms();
2281 setupDrawColorFilterUniforms();
2282 setupDrawPointUniforms();
2283 setupDrawShaderIdentityUniforms();
2284 setupDrawMesh(vertex);
2285
2286 for (int i = 0; i < count; i += 2) {
2287 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2288 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002289
Chet Haase8a5cc922011-04-26 07:28:09 -07002290 float left = points[i] - halfWidth;
2291 float right = points[i] + halfWidth;
2292 float top = points[i + 1] - halfWidth;
2293 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002294
Chet Haase8a5cc922011-04-26 07:28:09 -07002295 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002296 }
2297
2298 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002299
2300 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002301}
2302
Chet Haase48659092012-05-31 15:21:51 -07002303status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002304 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002305 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002306
Romain Guyae88e5e2010-10-22 17:49:18 -07002307 Rect& clip(*mSnapshot->clipRect);
2308 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002309
Romain Guy3d58c032010-07-14 16:34:53 -07002310 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002311
2312 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002313}
Romain Guy9d5316e2010-06-24 19:30:36 -07002314
Chet Haase48659092012-05-31 15:21:51 -07002315status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2316 SkPaint* paint) {
2317 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002318 const AutoTexture autoCleanup(texture);
2319
2320 const float x = left + texture->left - texture->offset;
2321 const float y = top + texture->top - texture->offset;
2322
2323 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002324
2325 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002326}
2327
Chet Haase48659092012-05-31 15:21:51 -07002328status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002329 float rx, float ry, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002330 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002331
Romain Guya1d3c912011-12-13 14:55:06 -08002332 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002333 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2334 right - left, bottom - top, rx, ry, paint);
Chet Haase48659092012-05-31 15:21:51 -07002335 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002336}
2337
Chet Haase48659092012-05-31 15:21:51 -07002338status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2339 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002340
Romain Guya1d3c912011-12-13 14:55:06 -08002341 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002342 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Chet Haase48659092012-05-31 15:21:51 -07002343 return drawShape(x - radius, y - radius, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002344}
Romain Guy01d58e42011-01-19 21:54:02 -08002345
Chet Haase48659092012-05-31 15:21:51 -07002346status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
2347 SkPaint* paint) {
2348 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002349
Romain Guya1d3c912011-12-13 14:55:06 -08002350 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002351 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002352 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002353}
2354
Chet Haase48659092012-05-31 15:21:51 -07002355status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002356 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002357 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002358
2359 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002360 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002361 }
2362
Romain Guya1d3c912011-12-13 14:55:06 -08002363 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002364 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2365 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002366 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002367}
2368
Chet Haase48659092012-05-31 15:21:51 -07002369status_t OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002370 SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002371 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002372
Romain Guya1d3c912011-12-13 14:55:06 -08002373 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002374 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002375 return drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002376}
2377
Chet Haase48659092012-05-31 15:21:51 -07002378status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002379 if (p->getStyle() != SkPaint::kFill_Style) {
Chet Haase48659092012-05-31 15:21:51 -07002380 return drawRectAsShape(left, top, right, bottom, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002381 }
2382
Romain Guy6926c722010-07-12 20:20:03 -07002383 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002384 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002385 }
2386
Romain Guy026c5e162010-06-28 17:12:22 -07002387 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002388 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002389 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2390 if (!isMode) {
2391 // Assume SRC_OVER
2392 mode = SkXfermode::kSrcOver_Mode;
2393 }
2394 } else {
2395 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002396 }
2397
Romain Guy026c5e162010-06-28 17:12:22 -07002398 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002399 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002400 drawAARect(left, top, right, bottom, color, mode);
2401 } else {
2402 drawColorRect(left, top, right, bottom, color, mode);
2403 }
Chet Haase48659092012-05-31 15:21:51 -07002404
2405 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002406}
Romain Guy9d5316e2010-06-24 19:30:36 -07002407
Raph Levien416a8472012-07-19 22:48:17 -07002408void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2409 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2410 float x, float y) {
2411 mCaches.activeTexture(0);
2412
2413 // NOTE: The drop shadow will not perform gamma correction
2414 // if shader-based correction is enabled
2415 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2416 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2417 paint, text, bytesCount, count, mShadowRadius, positions);
2418 const AutoTexture autoCleanup(shadow);
2419
2420 const float sx = x - shadow->left + mShadowDx;
2421 const float sy = y - shadow->top + mShadowDy;
2422
2423 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2424 int shadowColor = mShadowColor;
2425 if (mShader) {
2426 shadowColor = 0xffffffff;
2427 }
2428
2429 setupDraw();
2430 setupDrawWithTexture(true);
2431 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2432 setupDrawColorFilter();
2433 setupDrawShader();
2434 setupDrawBlending(true, mode);
2435 setupDrawProgram();
2436 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2437 setupDrawTexture(shadow->id);
2438 setupDrawPureColorUniforms();
2439 setupDrawColorFilterUniforms();
2440 setupDrawShaderUniforms();
2441 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2442
2443 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2444}
2445
Chet Haase48659092012-05-31 15:21:51 -07002446status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002447 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002448 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002449 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002450 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002451 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002452
Romain Guy671d6cf2012-01-18 12:39:17 -08002453 // NOTE: Skia does not support perspective transform on drawPosText yet
2454 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002455 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002456 }
2457
2458 float x = 0.0f;
2459 float y = 0.0f;
2460 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2461 if (pureTranslate) {
2462 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2463 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2464 }
2465
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002466 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002467 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2468 paint->getTextSize());
2469
2470 int alpha;
2471 SkXfermode::Mode mode;
2472 getAlphaAndMode(paint, &alpha, &mode);
2473
Raph Levien416a8472012-07-19 22:48:17 -07002474 if (CC_UNLIKELY(mHasShadow)) {
2475 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2476 0.0f, 0.0f);
2477 }
2478
Romain Guy671d6cf2012-01-18 12:39:17 -08002479 // Pick the appropriate texture filtering
2480 bool linearFilter = mSnapshot->transform->changesBounds();
2481 if (pureTranslate && !linearFilter) {
2482 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2483 }
2484
2485 mCaches.activeTexture(0);
2486 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002487 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002488 setupDrawDirtyRegionsDisabled();
2489 setupDrawWithTexture(true);
2490 setupDrawAlpha8Color(paint->getColor(), alpha);
2491 setupDrawColorFilter();
2492 setupDrawShader();
2493 setupDrawBlending(true, mode);
2494 setupDrawProgram();
2495 setupDrawModelView(x, y, x, y, pureTranslate, true);
2496 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2497 setupDrawPureColorUniforms();
2498 setupDrawColorFilterUniforms();
2499 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002500 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002501
2502 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2503 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2504
Romain Guy211370f2012-02-01 16:10:55 -08002505 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002506
2507 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2508 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002509 if (hasActiveLayer) {
2510 if (!pureTranslate) {
2511 mSnapshot->transform->mapRect(bounds);
2512 }
2513 dirtyLayerUnchecked(bounds, getRegion());
2514 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002515 }
Chet Haase48659092012-05-31 15:21:51 -07002516
2517 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002518}
2519
Romain Guyc2525952012-07-27 16:41:22 -07002520status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002521 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002522 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002523 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002524 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002525 }
2526
Chet Haasea1cff502012-02-21 13:43:44 -08002527 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002528 switch (paint->getTextAlign()) {
2529 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002530 x -= length / 2.0f;
2531 break;
2532 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002533 x -= length;
2534 break;
2535 default:
2536 break;
2537 }
2538
Romain Guycac5fd32011-12-01 20:08:50 -08002539 SkPaint::FontMetrics metrics;
2540 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002541 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002542 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002543 }
2544
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002545 const float oldX = x;
2546 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002547 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002548 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002549 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2550 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2551 }
2552
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002553#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002554 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002555 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002556#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002557
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002558 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002559 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002560 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002561
Romain Guy86568192010-12-14 15:55:39 -08002562 int alpha;
2563 SkXfermode::Mode mode;
2564 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002565
Romain Guy211370f2012-02-01 16:10:55 -08002566 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002567 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2568 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002569 }
2570
Romain Guy6620c6d2010-12-06 18:07:02 -08002571 // Pick the appropriate texture filtering
2572 bool linearFilter = mSnapshot->transform->changesBounds();
2573 if (pureTranslate && !linearFilter) {
2574 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2575 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002576
Romain Guy16c88082012-06-11 16:03:47 -07002577 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002578 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002579 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002580 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002581 setupDrawDirtyRegionsDisabled();
2582 setupDrawWithTexture(true);
2583 setupDrawAlpha8Color(paint->getColor(), alpha);
2584 setupDrawColorFilter();
2585 setupDrawShader();
2586 setupDrawBlending(true, mode);
2587 setupDrawProgram();
2588 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002589 // See comment above; the font renderer must use texture unit 0
2590 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002591 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2592 setupDrawPureColorUniforms();
2593 setupDrawColorFilterUniforms();
2594 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002595 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002596
Romain Guy6620c6d2010-12-06 18:07:02 -08002597 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002598 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2599
Romain Guy211370f2012-02-01 16:10:55 -08002600 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002601
Raph Levien996e57c2012-07-23 15:22:52 -07002602 bool status;
Raph Levien8b4072d2012-07-30 15:50:00 -07002603 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
2604 SkPaint paintCopy(*paint);
2605 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2606 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Raph Levien996e57c2012-07-23 15:22:52 -07002607 positions, hasActiveLayer ? &bounds : NULL);
2608 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002609 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2610 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002611 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002612
2613 if (status && hasActiveLayer) {
2614 if (!pureTranslate) {
2615 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002616 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002617 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002618 }
Romain Guy694b5192010-07-21 21:33:20 -07002619
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002620 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002621
2622 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002623}
2624
Chet Haase48659092012-05-31 15:21:51 -07002625status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002626 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002627 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2628 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002629 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002630 }
2631
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002632 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002633 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2634 paint->getTextSize());
2635
2636 int alpha;
2637 SkXfermode::Mode mode;
2638 getAlphaAndMode(paint, &alpha, &mode);
2639
2640 mCaches.activeTexture(0);
2641 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002642 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002643 setupDrawDirtyRegionsDisabled();
2644 setupDrawWithTexture(true);
2645 setupDrawAlpha8Color(paint->getColor(), alpha);
2646 setupDrawColorFilter();
2647 setupDrawShader();
2648 setupDrawBlending(true, mode);
2649 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002650 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002651 setupDrawTexture(fontRenderer.getTexture(true));
2652 setupDrawPureColorUniforms();
2653 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002654 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002655 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002656
Romain Guy97771732012-02-28 18:17:02 -08002657 const Rect* clip = &mSnapshot->getLocalClip();
2658 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 -08002659
Romain Guy97771732012-02-28 18:17:02 -08002660 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002661
2662 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2663 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002664 if (hasActiveLayer) {
2665 mSnapshot->transform->mapRect(bounds);
2666 dirtyLayerUnchecked(bounds, getRegion());
2667 }
Romain Guy97771732012-02-28 18:17:02 -08002668 }
Chet Haase48659092012-05-31 15:21:51 -07002669
2670 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002671}
2672
Chet Haase48659092012-05-31 15:21:51 -07002673status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2674 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002675
Romain Guya1d3c912011-12-13 14:55:06 -08002676 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002677
Romain Guy33f6beb2012-02-16 19:24:51 -08002678 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002679 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002680 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002681 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002682
Romain Guy8b55f372010-08-18 17:10:07 -07002683 const float x = texture->left - texture->offset;
2684 const float y = texture->top - texture->offset;
2685
Romain Guy01d58e42011-01-19 21:54:02 -08002686 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002687
2688 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002689}
2690
Chet Haase48659092012-05-31 15:21:51 -07002691status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002692 if (!layer) {
2693 return DrawGlInfo::kStatusDone;
2694 }
2695
2696 Rect transformed;
2697 Rect clip;
2698 const bool rejected = quickRejectNoScissor(x, y,
2699 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2700
2701 if (rejected) {
Chet Haase48659092012-05-31 15:21:51 -07002702 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002703 }
2704
Romain Guy4ff0cf42012-08-06 14:51:10 -07002705 bool debugLayerUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -07002706 if (updateLayer(layer, true)) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002707 debugLayerUpdate = mCaches.debugLayersUpdates;
Romain Guy2bf68f02012-03-02 13:37:47 -08002708 }
2709
Romain Guy35643dd2012-09-18 15:40:58 -07002710 mCaches.setScissorEnabled(!clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002711 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002712
Romain Guy211370f2012-02-01 16:10:55 -08002713 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002714 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002715 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002716 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002717 const float a = layer->getAlpha() / 255.0f;
2718 SkiaColorFilter *oldFilter = mColorFilter;
2719 mColorFilter = layer->getColorFilter();
Romain Guyf219da52011-01-16 12:54:25 -08002720
Romain Guyc88e3572011-01-22 00:32:12 -08002721 setupDraw();
2722 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002723 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002724 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002725 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002726 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002727 setupDrawPureColorUniforms();
2728 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002729 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002730 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002731 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2732 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002733
Romain Guyd21b6e12011-11-30 20:21:23 -08002734 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002735 setupDrawModelViewTranslate(tx, ty,
2736 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002737 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002738 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002739 setupDrawModelViewTranslate(x, y,
2740 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2741 }
Romain Guyc88e3572011-01-22 00:32:12 -08002742 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002743
Romain Guyc88e3572011-01-22 00:32:12 -08002744 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2745 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002746
Romain Guyc88e3572011-01-22 00:32:12 -08002747 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002748
Chet Haased15ebf22012-09-05 11:40:29 -07002749 mColorFilter = oldFilter;
2750
Romain Guy3a3133d2011-02-01 22:59:58 -08002751#if DEBUG_LAYERS_AS_REGIONS
2752 drawRegionRects(layer->region);
2753#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002754 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002755
2756 if (debugLayerUpdate) {
2757 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2758 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2759 }
Romain Guyf219da52011-01-16 12:54:25 -08002760 }
Chet Haase48659092012-05-31 15:21:51 -07002761
2762 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002763}
2764
Romain Guy6926c722010-07-12 20:20:03 -07002765///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002766// Shaders
2767///////////////////////////////////////////////////////////////////////////////
2768
2769void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002770 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002771}
2772
Romain Guy06f96e22010-07-30 19:18:16 -07002773void OpenGLRenderer::setupShader(SkiaShader* shader) {
2774 mShader = shader;
2775 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002776 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002777 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002778}
2779
Romain Guyd27977d2010-07-14 19:18:51 -07002780///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002781// Color filters
2782///////////////////////////////////////////////////////////////////////////////
2783
2784void OpenGLRenderer::resetColorFilter() {
2785 mColorFilter = NULL;
2786}
2787
2788void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2789 mColorFilter = filter;
2790}
2791
2792///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002793// Drop shadow
2794///////////////////////////////////////////////////////////////////////////////
2795
2796void OpenGLRenderer::resetShadow() {
2797 mHasShadow = false;
2798}
2799
2800void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2801 mHasShadow = true;
2802 mShadowRadius = radius;
2803 mShadowDx = dx;
2804 mShadowDy = dy;
2805 mShadowColor = color;
2806}
2807
2808///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002809// Draw filters
2810///////////////////////////////////////////////////////////////////////////////
2811
2812void OpenGLRenderer::resetPaintFilter() {
2813 mHasDrawFilter = false;
2814}
2815
2816void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2817 mHasDrawFilter = true;
2818 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2819 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2820}
2821
2822SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002823 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002824
2825 uint32_t flags = paint->getFlags();
2826
2827 mFilteredPaint = *paint;
2828 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2829
2830 return &mFilteredPaint;
2831}
2832
2833///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002834// Drawing implementation
2835///////////////////////////////////////////////////////////////////////////////
2836
Romain Guy01d58e42011-01-19 21:54:02 -08002837void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2838 float x, float y, SkPaint* paint) {
2839 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2840 return;
2841 }
2842
2843 int alpha;
2844 SkXfermode::Mode mode;
2845 getAlphaAndMode(paint, &alpha, &mode);
2846
2847 setupDraw();
2848 setupDrawWithTexture(true);
2849 setupDrawAlpha8Color(paint->getColor(), alpha);
2850 setupDrawColorFilter();
2851 setupDrawShader();
2852 setupDrawBlending(true, mode);
2853 setupDrawProgram();
2854 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2855 setupDrawTexture(texture->id);
2856 setupDrawPureColorUniforms();
2857 setupDrawColorFilterUniforms();
2858 setupDrawShaderUniforms();
2859 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2860
2861 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2862
2863 finishDrawTexture();
2864}
2865
Romain Guyf607bdc2010-09-10 19:20:06 -07002866// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002867#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2868#define kStdUnderline_Offset (1.0f / 9.0f)
2869#define kStdUnderline_Thickness (1.0f / 18.0f)
2870
2871void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2872 float x, float y, SkPaint* paint) {
2873 // Handle underline and strike-through
2874 uint32_t flags = paint->getFlags();
2875 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002876 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002877 float underlineWidth = length;
2878 // If length is > 0.0f, we already measured the text for the text alignment
2879 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002880 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002881 }
2882
Romain Guy211370f2012-02-01 16:10:55 -08002883 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002884 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002885 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002886
Raph Levien8b4072d2012-07-30 15:50:00 -07002887 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002888 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002889
Romain Guyf6834472011-01-23 13:32:12 -08002890 int linesCount = 0;
2891 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2892 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2893
2894 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002895 float points[pointsCount];
2896 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002897
2898 if (flags & SkPaint::kUnderlineText_Flag) {
2899 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002900 points[currentPoint++] = left;
2901 points[currentPoint++] = top;
2902 points[currentPoint++] = left + underlineWidth;
2903 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002904 }
2905
2906 if (flags & SkPaint::kStrikeThruText_Flag) {
2907 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002908 points[currentPoint++] = left;
2909 points[currentPoint++] = top;
2910 points[currentPoint++] = left + underlineWidth;
2911 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002912 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002913
Romain Guy726aeba2011-06-01 14:52:00 -07002914 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002915
Romain Guy726aeba2011-06-01 14:52:00 -07002916 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002917 }
2918 }
2919}
2920
Romain Guy026c5e162010-06-28 17:12:22 -07002921void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002922 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002923 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002924 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002925 color |= 0x00ffffff;
2926 }
2927
Romain Guy70ca14e2010-12-13 18:24:33 -08002928 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002929 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002930 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002931 setupDrawShader();
2932 setupDrawColorFilter();
2933 setupDrawBlending(mode);
2934 setupDrawProgram();
2935 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2936 setupDrawColorUniforms();
2937 setupDrawShaderUniforms(ignoreTransform);
2938 setupDrawColorFilterUniforms();
2939 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002940
Romain Guyc95c8d62010-09-17 15:31:32 -07002941 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2942}
2943
Romain Guy82ba8142010-07-09 13:25:56 -07002944void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002945 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002946 int alpha;
2947 SkXfermode::Mode mode;
2948 getAlphaAndMode(paint, &alpha, &mode);
2949
Romain Guyd21b6e12011-11-30 20:21:23 -08002950 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002951
Romain Guy211370f2012-02-01 16:10:55 -08002952 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002953 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2954 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2955
Romain Guyd21b6e12011-11-30 20:21:23 -08002956 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002957 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2958 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2959 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2960 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002961 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002962 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2963 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2964 GL_TRIANGLE_STRIP, gMeshCount);
2965 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002966}
2967
Romain Guybd6b79b2010-06-26 00:13:53 -07002968void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002969 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2970 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002971 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002972}
2973
2974void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002975 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002976 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002977 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002978
Romain Guy746b7402010-10-26 16:27:31 -07002979 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002980 setupDrawWithTexture();
2981 setupDrawColor(alpha, alpha, alpha, alpha);
2982 setupDrawColorFilter();
2983 setupDrawBlending(blend, mode, swapSrcDst);
2984 setupDrawProgram();
2985 if (!dirty) {
2986 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002987 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002988 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002989 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002990 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002991 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002992 }
Romain Guy86568192010-12-14 15:55:39 -08002993 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002994 setupDrawColorFilterUniforms();
2995 setupDrawTexture(texture);
2996 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002997
Romain Guy6820ac82010-09-15 18:11:50 -07002998 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002999
3000 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003001}
3002
Romain Guya5aed0d2010-09-09 14:42:43 -07003003void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003004 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003005 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003006
Romain Guy82ba8142010-07-09 13:25:56 -07003007 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003008 // These blend modes are not supported by OpenGL directly and have
3009 // to be implemented using shaders. Since the shader will perform
3010 // the blending, turn blending off here
3011 // If the blend mode cannot be implemented using shaders, fall
3012 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003013 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08003014 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003015 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003016 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003017
Romain Guy82bc7a72012-01-03 14:13:39 -08003018 if (mCaches.blend) {
3019 glDisable(GL_BLEND);
3020 mCaches.blend = false;
3021 }
3022
3023 return;
3024 } else {
3025 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003026 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003027 }
3028
3029 if (!mCaches.blend) {
3030 glEnable(GL_BLEND);
3031 }
3032
3033 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3034 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3035
3036 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3037 glBlendFunc(sourceMode, destMode);
3038 mCaches.lastSrcMode = sourceMode;
3039 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003040 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003041 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003042 glDisable(GL_BLEND);
3043 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003044 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003045}
3046
Romain Guy889f8d12010-07-29 14:37:42 -07003047bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003048 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003049 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003050 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003051 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003052 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003053 }
Romain Guy6926c722010-07-12 20:20:03 -07003054 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003055}
3056
Romain Guy026c5e162010-06-28 17:12:22 -07003057void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003058 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003059 TextureVertex::setUV(v++, u1, v1);
3060 TextureVertex::setUV(v++, u2, v1);
3061 TextureVertex::setUV(v++, u1, v2);
3062 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003063}
3064
Chet Haase5c13d892010-10-08 08:37:55 -07003065void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003066 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003067 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003068}
3069
Romain Guy9d5316e2010-06-24 19:30:36 -07003070}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003071}; // namespace android