blob: a1da87868701a57e41f1f6af4d534988ac0be23f [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 Guy530041d2012-01-25 18:56:29 -0800142uint32_t OpenGLRenderer::getStencilSize() {
143 return STENCIL_BUFFER_SIZE;
144}
145
Romain Guy49c5fc02012-05-15 11:10:01 -0700146bool OpenGLRenderer::isDeferred() {
147 return false;
148}
149
Romain Guy85bf02f2010-06-22 13:11:24 -0700150void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700151 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700152
153 mWidth = width;
154 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700155
156 mFirstSnapshot->height = height;
157 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700158
Romain Guy3e263fa2011-12-12 16:47:48 -0800159 glDisable(GL_DITHER);
Romain Guy3e263fa2011-12-12 16:47:48 -0800160 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800161
Romain Guy3e263fa2011-12-12 16:47:48 -0800162 glEnableVertexAttribArray(Program::kBindingPosition);
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 Guyddf74372012-05-22 14:07:07 -0700178 mDirtyClip = opaque;
179
180 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800181
Romain Guy6b7bd242010-10-06 19:49:23 -0700182 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700183 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700184 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700185 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700186 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700187 } else {
188 mCaches.resetScissor();
189 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700190
191 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700192}
193
194void OpenGLRenderer::syncState() {
195 glViewport(0, 0, mWidth, mHeight);
196
197 if (mCaches.blend) {
198 glEnable(GL_BLEND);
199 } else {
200 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700201 }
Romain Guybb9524b2010-06-22 18:56:38 -0700202}
203
Romain Guyb025b9c2010-09-16 14:16:48 -0700204void OpenGLRenderer::finish() {
205#if DEBUG_OPENGL
206 GLenum status = GL_NO_ERROR;
207 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000208 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800209 switch (status) {
Romain Guya44a63a2012-04-26 14:05:02 -0700210 case GL_INVALID_ENUM:
211 ALOGE(" GL_INVALID_ENUM");
212 break;
213 case GL_INVALID_VALUE:
214 ALOGE(" GL_INVALID_VALUE");
215 break;
216 case GL_INVALID_OPERATION:
217 ALOGE(" GL_INVALID_OPERATION");
218 break;
Romain Guya07105b2011-01-10 21:14:18 -0800219 case GL_OUT_OF_MEMORY:
Romain Guya44a63a2012-04-26 14:05:02 -0700220 ALOGE(" Out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800221 break;
222 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700223 }
224#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800225#if DEBUG_MEMORY_USAGE
226 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800227#else
228 if (mCaches.getDebugLevel() & kDebugMemory) {
229 mCaches.dumpMemoryUsage();
230 }
Romain Guyc15008e2010-11-10 11:59:15 -0800231#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700232}
233
Romain Guy6c319ca2011-01-11 14:29:25 -0800234void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700235 if (mCaches.currentProgram) {
236 if (mCaches.currentProgram->isInUse()) {
237 mCaches.currentProgram->remove();
238 mCaches.currentProgram = NULL;
239 }
240 }
Romain Guy50c0f092010-10-19 11:42:22 -0700241 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800242 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800243 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800244 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700245}
246
Romain Guy6c319ca2011-01-11 14:29:25 -0800247void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800248 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
249
250 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800251 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
252
Romain Guy586cae32012-07-13 15:28:31 -0700253 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800254 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700255 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700256
Romain Guya1d3c912011-12-13 14:55:06 -0800257 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800258 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700259
Romain Guy50c0f092010-10-19 11:42:22 -0700260 mCaches.blend = true;
261 glEnable(GL_BLEND);
262 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
263 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700264}
265
Romain Guyba6be8a2012-04-23 18:22:09 -0700266void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700267 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700268}
269
270void OpenGLRenderer::attachFunctor(Functor* functor) {
271 mFunctors.add(functor);
272}
273
Romain Guy8f3b8e32012-03-27 16:33:45 -0700274status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
275 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700276 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700277
Romain Guyba6be8a2012-04-23 18:22:09 -0700278 if (count > 0) {
279 SortedVector<Functor*> functors(mFunctors);
280 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700281
Romain Guyba6be8a2012-04-23 18:22:09 -0700282 DrawGlInfo info;
283 info.clipLeft = 0;
284 info.clipTop = 0;
285 info.clipRight = 0;
286 info.clipBottom = 0;
287 info.isLayer = false;
288 info.width = 0;
289 info.height = 0;
290 memset(info.transform, 0, sizeof(float) * 16);
291
292 for (size_t i = 0; i < count; i++) {
293 Functor* f = functors.itemAt(i);
294 result |= (*f)(DrawGlInfo::kModeProcess, &info);
295
Chris Craikc2c95432012-04-25 15:13:52 -0700296 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700297 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
298 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700299 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700300
Chris Craikc2c95432012-04-25 15:13:52 -0700301 if (result & DrawGlInfo::kStatusInvoke) {
302 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700303 }
304 }
305 }
306
Romain Guyc189ef52012-04-25 20:02:53 -0700307 mCaches.activeTexture(0);
308
Romain Guy8f3b8e32012-03-27 16:33:45 -0700309 return result;
310}
311
312status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800313 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700314 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700315
Romain Guy8a4ac612012-07-17 17:32:48 -0700316 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800317 if (mDirtyClip) {
318 setScissorFromClip();
319 }
Romain Guyd643bb52011-03-01 14:55:21 -0800320
Romain Guy80911b82011-03-16 15:30:12 -0700321 Rect clip(*mSnapshot->clipRect);
322 clip.snapToPixelBoundaries();
323
Romain Guyd643bb52011-03-01 14:55:21 -0800324#if RENDER_LAYERS_AS_REGIONS
325 // Since we don't know what the functor will draw, let's dirty
326 // tne entire clip region
327 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800328 dirtyLayerUnchecked(clip, getRegion());
329 }
330#endif
331
Romain Guy08aa2cb2011-03-17 11:06:57 -0700332 DrawGlInfo info;
333 info.clipLeft = clip.left;
334 info.clipTop = clip.top;
335 info.clipRight = clip.right;
336 info.clipBottom = clip.bottom;
337 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700338 info.width = getSnapshot()->viewport.getWidth();
339 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700340 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700341
Chet Haase48659092012-05-31 15:21:51 -0700342 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800343
Romain Guy8f3b8e32012-03-27 16:33:45 -0700344 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700345 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800346 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700347
Chris Craik65924a32012-04-05 17:52:11 -0700348 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700349 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700350 }
Romain Guycabfcc12011-03-07 18:06:46 -0800351 }
352
Chet Haasedaf98e92011-01-10 14:10:36 -0800353 resume();
Romain Guy65549432012-03-26 16:45:05 -0700354 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800355}
356
Romain Guyf6a11b82010-06-23 17:47:49 -0700357///////////////////////////////////////////////////////////////////////////////
358// State management
359///////////////////////////////////////////////////////////////////////////////
360
Romain Guybb9524b2010-06-22 18:56:38 -0700361int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700362 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700363}
364
365int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700366 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700367}
368
369void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700370 if (mSaveCount > 1) {
371 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700372 }
Romain Guybb9524b2010-06-22 18:56:38 -0700373}
374
375void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700376 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700377
Romain Guy8fb95422010-08-17 18:38:51 -0700378 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700379 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700380 }
Romain Guybb9524b2010-06-22 18:56:38 -0700381}
382
Romain Guy8aef54f2010-09-01 15:13:49 -0700383int OpenGLRenderer::saveSnapshot(int flags) {
384 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700385 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700386}
387
388bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700389 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700390 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700391 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700392
Romain Guybd6b79b2010-06-26 00:13:53 -0700393 sp<Snapshot> current = mSnapshot;
394 sp<Snapshot> previous = mSnapshot->previous;
395
Romain Guyeb993562010-10-05 18:14:38 -0700396 if (restoreOrtho) {
397 Rect& r = previous->viewport;
398 glViewport(r.left, r.top, r.right, r.bottom);
399 mOrthoMatrix.load(current->orthoMatrix);
400 }
401
Romain Guy8b55f372010-08-18 17:10:07 -0700402 mSaveCount--;
403 mSnapshot = previous;
404
Romain Guy2542d192010-08-18 11:47:12 -0700405 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700406 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700407 }
Romain Guy2542d192010-08-18 11:47:12 -0700408
Romain Guy5ec99242010-11-03 16:19:08 -0700409 if (restoreLayer) {
410 composeLayer(current, previous);
411 }
412
Romain Guy2542d192010-08-18 11:47:12 -0700413 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700414}
415
Romain Guyf6a11b82010-06-23 17:47:49 -0700416///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700417// Layers
418///////////////////////////////////////////////////////////////////////////////
419
420int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700421 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700422 const GLuint previousFbo = mSnapshot->fbo;
423 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700424
Romain Guyaf636eb2010-12-09 17:47:21 -0800425 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700426 int alpha = 255;
427 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700428
Romain Guye45362c2010-11-03 19:58:32 -0700429 if (p) {
430 alpha = p->getAlpha();
431 if (!mCaches.extensions.hasFramebufferFetch()) {
432 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
433 if (!isMode) {
434 // Assume SRC_OVER
435 mode = SkXfermode::kSrcOver_Mode;
436 }
437 } else {
438 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700439 }
440 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700441 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700442 }
Romain Guyd55a8612010-06-28 17:42:46 -0700443
Romain Guydbc26d22010-10-11 17:58:29 -0700444 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
445 }
Romain Guyd55a8612010-06-28 17:42:46 -0700446
447 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700448}
449
450int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
451 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700452 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700453 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700454 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700455 SkPaint paint;
456 paint.setAlpha(alpha);
457 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700458 }
Romain Guyd55a8612010-06-28 17:42:46 -0700459}
Romain Guybd6b79b2010-06-26 00:13:53 -0700460
Romain Guy1c740bc2010-09-13 18:00:09 -0700461/**
462 * Layers are viewed by Skia are slightly different than layers in image editing
463 * programs (for instance.) When a layer is created, previously created layers
464 * and the frame buffer still receive every drawing command. For instance, if a
465 * layer is created and a shape intersecting the bounds of the layers and the
466 * framebuffer is draw, the shape will be drawn on both (unless the layer was
467 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
468 *
469 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
470 * texture. Unfortunately, this is inefficient as it requires every primitive to
471 * be drawn n + 1 times, where n is the number of active layers. In practice this
472 * means, for every primitive:
473 * - Switch active frame buffer
474 * - Change viewport, clip and projection matrix
475 * - Issue the drawing
476 *
477 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700478 * To avoid this, layers are implemented in a different way here, at least in the
479 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
480 * is set. When this flag is set we can redirect all drawing operations into a
481 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700482 *
483 * This implementation relies on the frame buffer being at least RGBA 8888. When
484 * a layer is created, only a texture is created, not an FBO. The content of the
485 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700486 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700487 * buffer and drawing continues as normal. This technique therefore treats the
488 * frame buffer as a scratch buffer for the layers.
489 *
490 * To compose the layers back onto the frame buffer, each layer texture
491 * (containing the original frame buffer data) is drawn as a simple quad over
492 * the frame buffer. The trick is that the quad is set as the composition
493 * destination in the blending equation, and the frame buffer becomes the source
494 * of the composition.
495 *
496 * Drawing layers with an alpha value requires an extra step before composition.
497 * An empty quad is drawn over the layer's region in the frame buffer. This quad
498 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
499 * quad is used to multiply the colors in the frame buffer. This is achieved by
500 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
501 * GL_ZERO, GL_SRC_ALPHA.
502 *
503 * Because glCopyTexImage2D() can be slow, an alternative implementation might
504 * be use to draw a single clipped layer. The implementation described above
505 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700506 *
507 * (1) The frame buffer is actually not cleared right away. To allow the GPU
508 * to potentially optimize series of calls to glCopyTexImage2D, the frame
509 * buffer is left untouched until the first drawing operation. Only when
510 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700511 */
Romain Guyd55a8612010-06-28 17:42:46 -0700512bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700513 float right, float bottom, int alpha, SkXfermode::Mode mode,
514 int flags, GLuint previousFbo) {
515 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700516 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700517
Romain Guyeb993562010-10-05 18:14:38 -0700518 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
519
Romain Guyf607bdc2010-09-10 19:20:06 -0700520 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700521 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700522 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700523 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700524
Romain Guyeb993562010-10-05 18:14:38 -0700525 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700526 if (bounds.intersect(*snapshot->clipRect)) {
527 // We cannot work with sub-pixels in this case
528 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700529
Romain Guyad37cd32011-03-15 11:12:25 -0700530 // When the layer is not an FBO, we may use glCopyTexImage so we
531 // need to make sure the layer does not extend outside the bounds
532 // of the framebuffer
533 if (!bounds.intersect(snapshot->previous->viewport)) {
534 bounds.setEmpty();
535 }
536 } else {
537 bounds.setEmpty();
538 }
Romain Guyeb993562010-10-05 18:14:38 -0700539 }
Romain Guybf434112010-09-16 14:40:17 -0700540
Romain Guy746b7402010-10-26 16:27:31 -0700541 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
542 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800543 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700544 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700545 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700546 }
547
548 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800549 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700550 return false;
551 }
Romain Guyf18fd992010-07-08 11:45:51 -0700552
Romain Guya1d3c912011-12-13 14:55:06 -0800553 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700554 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700555 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700556 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700557 }
558
Romain Guy9ace8f52011-07-07 20:50:11 -0700559 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700560 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700561 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
562 bounds.getWidth() / float(layer->getWidth()), 0.0f);
563 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700564 layer->setBlend(true);
Romain Guydda57022010-07-06 11:39:32 -0700565
Romain Guy8fb95422010-08-17 18:38:51 -0700566 // Save the layer in the snapshot
567 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700568 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700569
Romain Guyeb993562010-10-05 18:14:38 -0700570 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700571 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700572 } else {
573 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700574 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800575 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700576 if (layer->isEmpty()) {
577 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
578 bounds.left, snapshot->height - bounds.bottom,
579 layer->getWidth(), layer->getHeight(), 0);
580 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800581 } else {
582 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
583 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
584 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700585
Romain Guy54be1cd2011-06-13 19:04:27 -0700586 // Enqueue the buffer coordinates to clear the corresponding region later
587 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700588 }
Romain Guyeb993562010-10-05 18:14:38 -0700589 }
Romain Guyf86ef572010-07-01 11:05:42 -0700590
Romain Guyd55a8612010-06-28 17:42:46 -0700591 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700592}
593
Romain Guy5b3b3522010-10-27 18:57:51 -0700594bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
595 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700596 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700597
598#if RENDER_LAYERS_AS_REGIONS
599 snapshot->region = &snapshot->layer->region;
600 snapshot->flags |= Snapshot::kFlagFboTarget;
601#endif
602
603 Rect clip(bounds);
604 snapshot->transform->mapRect(clip);
605 clip.intersect(*snapshot->clipRect);
606 clip.snapToPixelBoundaries();
607 clip.intersect(snapshot->previous->viewport);
608
609 mat4 inverse;
610 inverse.loadInverse(*mSnapshot->transform);
611
612 inverse.mapRect(clip);
613 clip.snapToPixelBoundaries();
614 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700615 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700616
617 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700618 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700619 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700620 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
621 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
622 snapshot->height = bounds.getHeight();
623 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
624 snapshot->orthoMatrix.load(mOrthoMatrix);
625
626 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700627 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
628 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700629
630 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700631 if (layer->isEmpty()) {
632 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
633 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700634 }
635
636 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700637 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700638
639#if DEBUG_LAYERS_AS_REGIONS
640 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
641 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000642 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700643
644 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700645 layer->deleteTexture();
646 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700647
648 delete layer;
649
650 return false;
651 }
652#endif
653
654 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700655 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800656 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700657 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700658 glClear(GL_COLOR_BUFFER_BIT);
659
660 dirtyClip();
661
662 // Change the ortho projection
663 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
664 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
665
666 return true;
667}
668
Romain Guy1c740bc2010-09-13 18:00:09 -0700669/**
670 * Read the documentation of createLayer() before doing anything in this method.
671 */
Romain Guy1d83e192010-08-17 11:37:00 -0700672void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
673 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000674 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700675 return;
676 }
677
Romain Guy5b3b3522010-10-27 18:57:51 -0700678 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700679
680 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700681 // Detach the texture from the FBO
682 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
683
Romain Guyeb993562010-10-05 18:14:38 -0700684 // Unbind current FBO and restore previous one
685 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
686 }
687
Romain Guy1d83e192010-08-17 11:37:00 -0700688 Layer* layer = current->layer;
689 const Rect& rect = layer->layer;
690
Romain Guy9ace8f52011-07-07 20:50:11 -0700691 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700692 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700693 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700694 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700695 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700696 }
697
Romain Guy03750a02010-10-18 14:06:08 -0700698 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700699
Romain Guya1d3c912011-12-13 14:55:06 -0800700 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700701
Romain Guy5b3b3522010-10-27 18:57:51 -0700702 // When the layer is stored in an FBO, we can save a bit of fillrate by
703 // drawing only the dirty region
704 if (fboLayer) {
705 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700706 if (layer->getColorFilter()) {
707 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800708 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700709 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700710 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800711 resetColorFilter();
712 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700713 } else if (!rect.isEmpty()) {
714 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
715 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700716 }
Romain Guy8b55f372010-08-18 17:10:07 -0700717
Romain Guyeb993562010-10-05 18:14:38 -0700718 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800719 // Note: No need to use glDiscardFramebufferEXT() since we never
720 // create/compose layers that are not on screen with this
721 // code path
722 // See LayerRenderer::destroyLayer(Layer*)
723
Romain Guyeb993562010-10-05 18:14:38 -0700724 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
725 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700726 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700727 }
728
Romain Guy746b7402010-10-26 16:27:31 -0700729 dirtyClip();
730
Romain Guyeb993562010-10-05 18:14:38 -0700731 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700732 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700733 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700734 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700735 delete layer;
736 }
737}
738
Romain Guyaa6c24c2011-04-28 18:40:04 -0700739void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700740 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700741
Romain Guy302a9df2011-08-16 13:55:02 -0700742 mat4& transform = layer->getTransform();
743 if (!transform.isIdentity()) {
744 save(0);
745 mSnapshot->transform->multiply(transform);
746 }
747
Romain Guyaa6c24c2011-04-28 18:40:04 -0700748 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700749 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700750 setupDrawWithTexture();
751 } else {
752 setupDrawWithExternalTexture();
753 }
754 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700755 setupDrawColor(alpha, alpha, alpha, alpha);
756 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700757 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700758 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700759 setupDrawPureColorUniforms();
760 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700761 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
762 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700763 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700764 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700765 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700766 if (mSnapshot->transform->isPureTranslate() &&
767 layer->getWidth() == (uint32_t) rect.getWidth() &&
768 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700769 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
770 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
771
Romain Guyd21b6e12011-11-30 20:21:23 -0800772 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700773 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
774 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800775 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700776 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
777 }
778 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700779 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
780
781 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
782
783 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700784
785 if (!transform.isIdentity()) {
786 restore();
787 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700788}
789
Romain Guy5b3b3522010-10-27 18:57:51 -0700790void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700791 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700792 const Rect& texCoords = layer->texCoords;
793 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
794 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700795
Romain Guy9ace8f52011-07-07 20:50:11 -0700796 float x = rect.left;
797 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700798 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700799 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700800 layer->getHeight() == (uint32_t) rect.getHeight();
801
802 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700803 // When we're swapping, the layer is already in screen coordinates
804 if (!swap) {
805 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
806 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
807 }
808
Romain Guyd21b6e12011-11-30 20:21:23 -0800809 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700810 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800811 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700812 }
813
814 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
815 layer->getTexture(), layer->getAlpha() / 255.0f,
816 layer->getMode(), layer->isBlend(),
817 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
818 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700819
Romain Guyaa6c24c2011-04-28 18:40:04 -0700820 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
821 } else {
822 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
823 drawTextureLayer(layer, rect);
824 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
825 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700826}
827
828void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
829#if RENDER_LAYERS_AS_REGIONS
830 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700831 layer->setRegionAsRect();
832
Romain Guy40667672011-03-18 14:34:03 -0700833 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700834
Romain Guy5b3b3522010-10-27 18:57:51 -0700835 layer->region.clear();
836 return;
837 }
838
Romain Guy8a3957d2011-09-07 17:55:15 -0700839 // TODO: See LayerRenderer.cpp::generateMesh() for important
840 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800841 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700842 size_t count;
843 const android::Rect* rects = layer->region.getArray(&count);
844
Romain Guy9ace8f52011-07-07 20:50:11 -0700845 const float alpha = layer->getAlpha() / 255.0f;
846 const float texX = 1.0f / float(layer->getWidth());
847 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800848 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700849
850 TextureVertex* mesh = mCaches.getRegionMesh();
851 GLsizei numQuads = 0;
852
Romain Guy7230a742011-01-10 22:26:16 -0800853 setupDraw();
854 setupDrawWithTexture();
855 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800856 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700857 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800858 setupDrawProgram();
859 setupDrawDirtyRegionsDisabled();
860 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800861 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700862 setupDrawTexture(layer->getTexture());
863 if (mSnapshot->transform->isPureTranslate()) {
864 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
865 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
866
Romain Guyd21b6e12011-11-30 20:21:23 -0800867 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700868 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
869 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800870 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700871 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
872 }
Romain Guy15bc6432011-12-13 13:11:32 -0800873 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700874
875 for (size_t i = 0; i < count; i++) {
876 const android::Rect* r = &rects[i];
877
878 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800879 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700880 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800881 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700882
883 // TODO: Reject quads outside of the clip
884 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
885 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
886 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
887 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
888
889 numQuads++;
890
891 if (numQuads >= REGION_MESH_QUAD_COUNT) {
892 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
893 numQuads = 0;
894 mesh = mCaches.getRegionMesh();
895 }
896 }
897
898 if (numQuads > 0) {
899 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
900 }
901
Romain Guy7230a742011-01-10 22:26:16 -0800902 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700903
904#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800905 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700906#endif
907
908 layer->region.clear();
909 }
910#else
911 composeLayerRect(layer, rect);
912#endif
913}
914
Romain Guy3a3133d2011-02-01 22:59:58 -0800915void OpenGLRenderer::drawRegionRects(const Region& region) {
916#if DEBUG_LAYERS_AS_REGIONS
917 size_t count;
918 const android::Rect* rects = region.getArray(&count);
919
920 uint32_t colors[] = {
921 0x7fff0000, 0x7f00ff00,
922 0x7f0000ff, 0x7fff00ff,
923 };
924
925 int offset = 0;
926 int32_t top = rects[0].top;
927
928 for (size_t i = 0; i < count; i++) {
929 if (top != rects[i].top) {
930 offset ^= 0x2;
931 top = rects[i].top;
932 }
933
934 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
935 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
936 SkXfermode::kSrcOver_Mode);
937 }
938#endif
939}
940
Romain Guy5b3b3522010-10-27 18:57:51 -0700941void OpenGLRenderer::dirtyLayer(const float left, const float top,
942 const float right, const float bottom, const mat4 transform) {
943#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800944 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700945 Rect bounds(left, top, right, bottom);
946 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800947 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700948 }
949#endif
950}
951
952void OpenGLRenderer::dirtyLayer(const float left, const float top,
953 const float right, const float bottom) {
954#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800955 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700956 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800957 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800958 }
959#endif
960}
961
962void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
963#if RENDER_LAYERS_AS_REGIONS
964 if (bounds.intersect(*mSnapshot->clipRect)) {
965 bounds.snapToPixelBoundaries();
966 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
967 if (!dirty.isEmpty()) {
968 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700969 }
970 }
971#endif
972}
973
Romain Guy54be1cd2011-06-13 19:04:27 -0700974void OpenGLRenderer::clearLayerRegions() {
975 const size_t count = mLayers.size();
976 if (count == 0) return;
977
978 if (!mSnapshot->isIgnored()) {
979 // Doing several glScissor/glClear here can negatively impact
980 // GPUs with a tiler architecture, instead we draw quads with
981 // the Clear blending mode
982
983 // The list contains bounds that have already been clipped
984 // against their initial clip rect, and the current clip
985 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -0700986 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -0700987
988 Vertex mesh[count * 6];
989 Vertex* vertex = mesh;
990
991 for (uint32_t i = 0; i < count; i++) {
992 Rect* bounds = mLayers.itemAt(i);
993
994 Vertex::set(vertex++, bounds->left, bounds->bottom);
995 Vertex::set(vertex++, bounds->left, bounds->top);
996 Vertex::set(vertex++, bounds->right, bounds->top);
997 Vertex::set(vertex++, bounds->left, bounds->bottom);
998 Vertex::set(vertex++, bounds->right, bounds->top);
999 Vertex::set(vertex++, bounds->right, bounds->bottom);
1000
1001 delete bounds;
1002 }
1003
1004 setupDraw(false);
1005 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1006 setupDrawBlending(true, SkXfermode::kClear_Mode);
1007 setupDrawProgram();
1008 setupDrawPureColorUniforms();
1009 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001010 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001011
Romain Guy54be1cd2011-06-13 19:04:27 -07001012 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001013
1014 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001015 } else {
1016 for (uint32_t i = 0; i < count; i++) {
1017 delete mLayers.itemAt(i);
1018 }
1019 }
1020
1021 mLayers.clear();
1022}
1023
Romain Guybd6b79b2010-06-26 00:13:53 -07001024///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001025// Transforms
1026///////////////////////////////////////////////////////////////////////////////
1027
1028void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001029 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001030}
1031
1032void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001033 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001034}
1035
1036void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001037 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001038}
1039
Romain Guy807daf72011-01-18 11:19:19 -08001040void OpenGLRenderer::skew(float sx, float sy) {
1041 mSnapshot->transform->skew(sx, sy);
1042}
1043
Romain Guyf6a11b82010-06-23 17:47:49 -07001044void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001045 if (matrix) {
1046 mSnapshot->transform->load(*matrix);
1047 } else {
1048 mSnapshot->transform->loadIdentity();
1049 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001050}
1051
1052void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001053 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001054}
1055
1056void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001057 SkMatrix transform;
1058 mSnapshot->transform->copyTo(transform);
1059 transform.preConcat(*matrix);
1060 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001061}
1062
1063///////////////////////////////////////////////////////////////////////////////
1064// Clipping
1065///////////////////////////////////////////////////////////////////////////////
1066
Romain Guybb9524b2010-06-22 18:56:38 -07001067void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001068 Rect clip(*mSnapshot->clipRect);
1069 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001070
Romain Guy8a4ac612012-07-17 17:32:48 -07001071 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1072 clip.getWidth(), clip.getHeight())) {
1073 mDirtyClip = false;
1074 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001075}
1076
1077const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001078 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001079}
1080
Romain Guy8a4ac612012-07-17 17:32:48 -07001081bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1082 if (mSnapshot->isIgnored()) {
1083 return true;
1084 }
1085
1086 Rect r(left, top, right, bottom);
1087 mSnapshot->transform->mapRect(r);
1088 r.snapToPixelBoundaries();
1089
1090 Rect clipRect(*mSnapshot->clipRect);
1091 clipRect.snapToPixelBoundaries();
1092
1093 return !clipRect.intersects(r);
1094}
1095
Romain Guyc7d53492010-06-25 13:41:57 -07001096bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001097 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001098 return true;
1099 }
1100
Romain Guy1d83e192010-08-17 11:37:00 -07001101 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001102 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001103 r.snapToPixelBoundaries();
1104
1105 Rect clipRect(*mSnapshot->clipRect);
1106 clipRect.snapToPixelBoundaries();
1107
Romain Guy586cae32012-07-13 15:28:31 -07001108 bool rejected = !clipRect.intersects(r);
1109 if (!isDeferred() && !rejected) {
1110 mCaches.setScissorEnabled(!clipRect.contains(r));
1111 }
1112
1113 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001114}
1115
Romain Guy079ba2c2010-07-16 14:12:24 -07001116bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1117 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001118 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001119 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001120 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001121 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001122}
1123
Chet Haasea23eed82012-04-12 15:19:04 -07001124Rect* OpenGLRenderer::getClipRect() {
1125 return mSnapshot->clipRect;
1126}
1127
Romain Guyf6a11b82010-06-23 17:47:49 -07001128///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001129// Drawing commands
1130///////////////////////////////////////////////////////////////////////////////
1131
Romain Guy54be1cd2011-06-13 19:04:27 -07001132void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001133 // TODO: It would be best if we could do this before quickReject()
1134 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001135 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001136 if (mDirtyClip) {
1137 setScissorFromClip();
1138 }
1139 mDescription.reset();
1140 mSetShaderColor = false;
1141 mColorSet = false;
1142 mColorA = mColorR = mColorG = mColorB = 0.0f;
1143 mTextureUnit = 0;
1144 mTrackDirtyRegions = true;
1145}
1146
1147void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1148 mDescription.hasTexture = true;
1149 mDescription.hasAlpha8Texture = isAlpha8;
1150}
1151
Romain Guyaa6c24c2011-04-28 18:40:04 -07001152void OpenGLRenderer::setupDrawWithExternalTexture() {
1153 mDescription.hasExternalTexture = true;
1154}
1155
Romain Guy15bc6432011-12-13 13:11:32 -08001156void OpenGLRenderer::setupDrawNoTexture() {
1157 mCaches.disbaleTexCoordsVertexArray();
1158}
1159
Chet Haase5b0200b2011-04-13 17:58:08 -07001160void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001161 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001162}
1163
Romain Guyed6fcb02011-03-21 13:11:28 -07001164void OpenGLRenderer::setupDrawPoint(float pointSize) {
1165 mDescription.isPoint = true;
1166 mDescription.pointSize = pointSize;
1167}
1168
Romain Guy70ca14e2010-12-13 18:24:33 -08001169void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001170 setupDrawColor(color, (color >> 24) & 0xFF);
1171}
1172
1173void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1174 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001175 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1176 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001177 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001178 mColorR = a * ((color >> 16) & 0xFF);
1179 mColorG = a * ((color >> 8) & 0xFF);
1180 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001181 mColorSet = true;
1182 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1183}
1184
Romain Guy86568192010-12-14 15:55:39 -08001185void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1186 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001187 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1188 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001189 const float a = mColorA / 255.0f;
1190 mColorR = a * ((color >> 16) & 0xFF);
1191 mColorG = a * ((color >> 8) & 0xFF);
1192 mColorB = a * ((color ) & 0xFF);
1193 mColorSet = true;
1194 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1195}
1196
Romain Guy41210632012-07-16 17:04:24 -07001197void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1198 mCaches.fontRenderer->describe(mDescription, paint);
1199}
1200
Romain Guy70ca14e2010-12-13 18:24:33 -08001201void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1202 mColorA = a;
1203 mColorR = r;
1204 mColorG = g;
1205 mColorB = b;
1206 mColorSet = true;
1207 mSetShaderColor = mDescription.setColor(r, g, b, a);
1208}
1209
1210void OpenGLRenderer::setupDrawShader() {
1211 if (mShader) {
1212 mShader->describe(mDescription, mCaches.extensions);
1213 }
1214}
1215
1216void OpenGLRenderer::setupDrawColorFilter() {
1217 if (mColorFilter) {
1218 mColorFilter->describe(mDescription, mCaches.extensions);
1219 }
1220}
1221
Romain Guyf09ef512011-05-27 11:43:46 -07001222void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1223 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1224 mColorA = 1.0f;
1225 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001226 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001227 }
1228}
1229
Romain Guy70ca14e2010-12-13 18:24:33 -08001230void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001231 // When the blending mode is kClear_Mode, we need to use a modulate color
1232 // argb=1,0,0,0
1233 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001234 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1235 mDescription, swapSrcDst);
1236}
1237
1238void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001239 // When the blending mode is kClear_Mode, we need to use a modulate color
1240 // argb=1,0,0,0
1241 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001242 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1243 mDescription, swapSrcDst);
1244}
1245
1246void OpenGLRenderer::setupDrawProgram() {
1247 useProgram(mCaches.programCache.get(mDescription));
1248}
1249
1250void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1251 mTrackDirtyRegions = false;
1252}
1253
1254void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1255 bool ignoreTransform) {
1256 mModelView.loadTranslate(left, top, 0.0f);
1257 if (!ignoreTransform) {
1258 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1259 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1260 } else {
1261 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1262 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1263 }
1264}
1265
Chet Haase8a5cc922011-04-26 07:28:09 -07001266void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1267 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001268}
1269
Romain Guy70ca14e2010-12-13 18:24:33 -08001270void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1271 bool ignoreTransform, bool ignoreModelView) {
1272 if (!ignoreModelView) {
1273 mModelView.loadTranslate(left, top, 0.0f);
1274 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001275 } else {
1276 mModelView.loadIdentity();
1277 }
Romain Guy86568192010-12-14 15:55:39 -08001278 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1279 if (!ignoreTransform) {
1280 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1281 if (mTrackDirtyRegions && dirty) {
1282 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1283 }
1284 } else {
1285 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1286 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1287 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001288}
1289
Romain Guyed6fcb02011-03-21 13:11:28 -07001290void OpenGLRenderer::setupDrawPointUniforms() {
1291 int slot = mCaches.currentProgram->getUniform("pointSize");
1292 glUniform1f(slot, mDescription.pointSize);
1293}
1294
Romain Guy70ca14e2010-12-13 18:24:33 -08001295void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001296 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001297 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1298 }
1299}
1300
Romain Guy86568192010-12-14 15:55:39 -08001301void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001302 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001303 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001304 }
1305}
1306
Romain Guy70ca14e2010-12-13 18:24:33 -08001307void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1308 if (mShader) {
1309 if (ignoreTransform) {
1310 mModelView.loadInverse(*mSnapshot->transform);
1311 }
1312 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1313 }
1314}
1315
Romain Guy8d0d4782010-12-14 20:13:35 -08001316void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1317 if (mShader) {
1318 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1319 }
1320}
1321
Romain Guy70ca14e2010-12-13 18:24:33 -08001322void OpenGLRenderer::setupDrawColorFilterUniforms() {
1323 if (mColorFilter) {
1324 mColorFilter->setupProgram(mCaches.currentProgram);
1325 }
1326}
1327
Romain Guy41210632012-07-16 17:04:24 -07001328void OpenGLRenderer::setupDrawTextGammaUniforms() {
1329 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1330}
1331
Romain Guy70ca14e2010-12-13 18:24:33 -08001332void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001333 bool force = mCaches.bindMeshBuffer();
1334 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001335 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001336}
1337
1338void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1339 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001340 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001341 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001342}
1343
Romain Guyaa6c24c2011-04-28 18:40:04 -07001344void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1345 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001346 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001347 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001348}
1349
Romain Guy8f0095c2011-05-02 17:24:22 -07001350void OpenGLRenderer::setupDrawTextureTransform() {
1351 mDescription.hasTextureTransform = true;
1352}
1353
1354void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001355 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1356 GL_FALSE, &transform.data[0]);
1357}
1358
Romain Guy70ca14e2010-12-13 18:24:33 -08001359void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001360 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001361 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001362 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001363 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001364 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001365 }
Romain Guyd71dd362011-12-12 19:03:35 -08001366
Romain Guyf3a910b42011-12-12 20:35:21 -08001367 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001368 if (mCaches.currentProgram->texCoords >= 0) {
1369 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1370 }
1371
1372 mCaches.unbindIndicesBuffer();
1373}
1374
1375void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1376 bool force = mCaches.unbindMeshBuffer();
1377 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1378 if (mCaches.currentProgram->texCoords >= 0) {
1379 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001380 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001381}
1382
Chet Haase5b0200b2011-04-13 17:58:08 -07001383void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001384 bool force = mCaches.unbindMeshBuffer();
1385 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1386 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001387 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001388}
1389
1390/**
1391 * 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 -07001392 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1393 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1394 * attributes (one per vertex) are values from zero to one that tells the fragment
1395 * shader where the fragment is in relation to the line width/length overall; these values are
1396 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1397 * region of the line.
1398 * Note that we only pass down the width values in this setup function. The length coordinates
1399 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001400 */
Chet Haase99585ad2011-05-02 15:00:16 -07001401void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001402 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001403 bool force = mCaches.unbindMeshBuffer();
1404 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1405 vertices, gAAVertexStride);
1406 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001407 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001408
Romain Guy7b631422012-04-04 11:38:54 -07001409 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001410 glEnableVertexAttribArray(widthSlot);
1411 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001412
Romain Guy7b631422012-04-04 11:38:54 -07001413 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001414 glEnableVertexAttribArray(lengthSlot);
1415 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001416
Chet Haase5b0200b2011-04-13 17:58:08 -07001417 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001418 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001419
Chet Haase99585ad2011-05-02 15:00:16 -07001420 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001421 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Romain Guy7b631422012-04-04 11:38:54 -07001422 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion);
1423}
1424
1425void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1426 glDisableVertexAttribArray(widthSlot);
1427 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001428}
1429
Romain Guy70ca14e2010-12-13 18:24:33 -08001430void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001431}
1432
1433///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001434// Drawing
1435///////////////////////////////////////////////////////////////////////////////
1436
Chet Haase1271e2c2012-04-20 09:54:27 -07001437status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001438 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001439
Romain Guy0fe478e2010-11-08 12:08:41 -08001440 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1441 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001442 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001443 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001444 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001445
Romain Guy65549432012-03-26 16:45:05 -07001446 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001447}
1448
Chet Haaseed30fd82011-04-22 16:18:45 -07001449void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1450 if (displayList) {
1451 displayList->output(*this, level);
1452 }
1453}
1454
Romain Guya168d732011-03-18 16:50:13 -07001455void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1456 int alpha;
1457 SkXfermode::Mode mode;
1458 getAlphaAndMode(paint, &alpha, &mode);
1459
Romain Guya168d732011-03-18 16:50:13 -07001460 float x = left;
1461 float y = top;
1462
Romain Guye3c26852011-07-25 16:36:01 -07001463 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001464 bool ignoreTransform = false;
1465 if (mSnapshot->transform->isPureTranslate()) {
1466 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1467 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1468 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001469 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001470 } else {
1471 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001472 }
1473
1474 setupDraw();
1475 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001476 if (paint) {
1477 setupDrawAlpha8Color(paint->getColor(), alpha);
1478 }
Romain Guya168d732011-03-18 16:50:13 -07001479 setupDrawColorFilter();
1480 setupDrawShader();
1481 setupDrawBlending(true, mode);
1482 setupDrawProgram();
1483 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001484
Romain Guya168d732011-03-18 16:50:13 -07001485 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001486 texture->setWrap(GL_CLAMP_TO_EDGE);
1487 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001488
Romain Guya168d732011-03-18 16:50:13 -07001489 setupDrawPureColorUniforms();
1490 setupDrawColorFilterUniforms();
1491 setupDrawShaderUniforms();
1492 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1493
1494 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1495
1496 finishDrawTexture();
1497}
1498
Chet Haase48659092012-05-31 15:21:51 -07001499status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001500 const float right = left + bitmap->width();
1501 const float bottom = top + bitmap->height();
1502
1503 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001504 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001505 }
1506
Romain Guya1d3c912011-12-13 14:55:06 -08001507 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001508 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001509 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001510 const AutoTexture autoCleanup(texture);
1511
Romain Guy211370f2012-02-01 16:10:55 -08001512 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001513 drawAlphaBitmap(texture, left, top, paint);
1514 } else {
1515 drawTextureRect(left, top, right, bottom, texture, paint);
1516 }
Chet Haase48659092012-05-31 15:21:51 -07001517
1518 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001519}
1520
Chet Haase48659092012-05-31 15:21:51 -07001521status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001522 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1523 const mat4 transform(*matrix);
1524 transform.mapRect(r);
1525
Romain Guy6926c722010-07-12 20:20:03 -07001526 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001527 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001528 }
1529
Romain Guya1d3c912011-12-13 14:55:06 -08001530 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001531 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001532 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001533 const AutoTexture autoCleanup(texture);
1534
Romain Guy5b3b3522010-10-27 18:57:51 -07001535 // This could be done in a cheaper way, all we need is pass the matrix
1536 // to the vertex shader. The save/restore is a bit overkill.
1537 save(SkCanvas::kMatrix_SaveFlag);
1538 concatMatrix(matrix);
1539 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1540 restore();
Chet Haase48659092012-05-31 15:21:51 -07001541
1542 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001543}
1544
Chet Haase48659092012-05-31 15:21:51 -07001545status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001546 const float right = left + bitmap->width();
1547 const float bottom = top + bitmap->height();
1548
1549 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001550 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001551 }
1552
1553 mCaches.activeTexture(0);
1554 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1555 const AutoTexture autoCleanup(texture);
1556
1557 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001558
1559 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001560}
1561
Chet Haase48659092012-05-31 15:21:51 -07001562status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001563 float* vertices, int* colors, SkPaint* paint) {
1564 // TODO: Do a quickReject
1565 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001566 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001567 }
1568
Romain Guya1d3c912011-12-13 14:55:06 -08001569 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001570 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001571 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001572 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001573
Romain Guyd21b6e12011-11-30 20:21:23 -08001574 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1575 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001576
1577 int alpha;
1578 SkXfermode::Mode mode;
1579 getAlphaAndMode(paint, &alpha, &mode);
1580
Romain Guy5a7b4662011-01-20 19:09:30 -08001581 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001582
Romain Guyb18d2d02011-02-10 15:52:54 -08001583 float left = FLT_MAX;
1584 float top = FLT_MAX;
1585 float right = FLT_MIN;
1586 float bottom = FLT_MIN;
1587
1588#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001589 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001590#else
Romain Guy211370f2012-02-01 16:10:55 -08001591 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001592#endif
1593
Romain Guya566b7c2011-01-23 16:36:11 -08001594 // TODO: Support the colors array
1595 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001596 TextureVertex* vertex = mesh;
1597 for (int32_t y = 0; y < meshHeight; y++) {
1598 for (int32_t x = 0; x < meshWidth; x++) {
1599 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1600
1601 float u1 = float(x) / meshWidth;
1602 float u2 = float(x + 1) / meshWidth;
1603 float v1 = float(y) / meshHeight;
1604 float v2 = float(y + 1) / meshHeight;
1605
1606 int ax = i + (meshWidth + 1) * 2;
1607 int ay = ax + 1;
1608 int bx = i;
1609 int by = bx + 1;
1610 int cx = i + 2;
1611 int cy = cx + 1;
1612 int dx = i + (meshWidth + 1) * 2 + 2;
1613 int dy = dx + 1;
1614
1615 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1616 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1617 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1618
1619 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1620 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1621 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001622
1623#if RENDER_LAYERS_AS_REGIONS
1624 if (hasActiveLayer) {
1625 // TODO: This could be optimized to avoid unnecessary ops
1626 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1627 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1628 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1629 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1630 }
1631#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001632 }
1633 }
1634
Romain Guyb18d2d02011-02-10 15:52:54 -08001635#if RENDER_LAYERS_AS_REGIONS
1636 if (hasActiveLayer) {
1637 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1638 }
1639#endif
1640
Romain Guy5a7b4662011-01-20 19:09:30 -08001641 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1642 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001643 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001644
1645 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001646}
1647
Chet Haase48659092012-05-31 15:21:51 -07001648status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001649 float srcLeft, float srcTop, float srcRight, float srcBottom,
1650 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001651 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001652 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001653 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001654 }
1655
Romain Guya1d3c912011-12-13 14:55:06 -08001656 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001657 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001658 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001659 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001660
Romain Guy8ba548f2010-06-30 19:21:21 -07001661 const float width = texture->width;
1662 const float height = texture->height;
1663
Romain Guy68169722011-08-22 17:33:33 -07001664 const float u1 = fmax(0.0f, srcLeft / width);
1665 const float v1 = fmax(0.0f, srcTop / height);
1666 const float u2 = fmin(1.0f, srcRight / width);
1667 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001668
Romain Guy03750a02010-10-18 14:06:08 -07001669 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001670 resetDrawTextureTexCoords(u1, v1, u2, v2);
1671
Romain Guy03750a02010-10-18 14:06:08 -07001672 int alpha;
1673 SkXfermode::Mode mode;
1674 getAlphaAndMode(paint, &alpha, &mode);
1675
Romain Guyd21b6e12011-11-30 20:21:23 -08001676 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1677
Romain Guy211370f2012-02-01 16:10:55 -08001678 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001679 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1680 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1681
Romain Guyb5014982011-07-28 15:39:12 -07001682 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001683 // Enable linear filtering if the source rectangle is scaled
1684 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001685 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001686 }
Romain Guyb5014982011-07-28 15:39:12 -07001687
Romain Guyd21b6e12011-11-30 20:21:23 -08001688 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001689 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1690 texture->id, alpha / 255.0f, mode, texture->blend,
1691 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1692 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1693 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001694 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001695 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1696 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1697 GL_TRIANGLE_STRIP, gMeshCount);
1698 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001699
1700 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001701
1702 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001703}
1704
Chet Haase48659092012-05-31 15:21:51 -07001705status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001706 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001707 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001708 int alpha;
1709 SkXfermode::Mode mode;
1710 getAlphaAndModeDirect(paint, &alpha, &mode);
1711
1712 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1713 left, top, right, bottom, alpha, mode);
1714}
1715
1716status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1717 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1718 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001719 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001720 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001721 }
1722
Romain Guybe6f9dc2012-07-16 12:41:17 -07001723 alpha *= mSnapshot->alpha;
1724
Romain Guya1d3c912011-12-13 14:55:06 -08001725 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001726 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001727 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001728 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001729 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1730 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001731
Romain Guy2728f962010-10-08 18:36:15 -07001732 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001733 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001734
Romain Guy211370f2012-02-01 16:10:55 -08001735 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001736 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001737#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001738 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001739 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001740 const float offsetX = left + mSnapshot->transform->getTranslateX();
1741 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001742 const size_t count = mesh->quads.size();
1743 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001744 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001745 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001746 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1747 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1748 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001749 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001750 dirtyLayer(left + bounds.left, top + bounds.top,
1751 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001752 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001753 }
1754 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001755#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001756
Romain Guy211370f2012-02-01 16:10:55 -08001757 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001758 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1759 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1760
1761 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1762 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1763 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1764 true, !mesh->hasEmptyQuads);
1765 } else {
1766 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1767 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1768 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1769 true, !mesh->hasEmptyQuads);
1770 }
Romain Guy054dc182010-10-15 17:55:25 -07001771 }
Chet Haase48659092012-05-31 15:21:51 -07001772
1773 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001774}
1775
Chet Haase99ecdc42011-05-06 12:06:34 -07001776/**
Chet Haase858aa932011-05-12 09:06:00 -07001777 * This function uses a similar approach to that of AA lines in the drawLines() function.
1778 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1779 * shader to compute the translucency of the color, determined by whether a given pixel is
1780 * within that boundary region and how far into the region it is.
1781 */
1782void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001783 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001784 float inverseScaleX = 1.0f;
1785 float inverseScaleY = 1.0f;
Romain Guy04299382012-07-18 17:15:41 -07001786
Chet Haase858aa932011-05-12 09:06:00 -07001787 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001788 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001789 Matrix4 *mat = mSnapshot->transform;
1790 float m00 = mat->data[Matrix4::kScaleX];
1791 float m01 = mat->data[Matrix4::kSkewY];
1792 float m02 = mat->data[2];
1793 float m10 = mat->data[Matrix4::kSkewX];
1794 float m11 = mat->data[Matrix4::kScaleX];
1795 float m12 = mat->data[6];
1796 float scaleX = sqrt(m00 * m00 + m01 * m01);
1797 float scaleY = sqrt(m10 * m10 + m11 * m11);
1798 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1799 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1800 }
1801
Chet Haase858aa932011-05-12 09:06:00 -07001802 float boundarySizeX = .5 * inverseScaleX;
1803 float boundarySizeY = .5 * inverseScaleY;
1804
1805 // Adjust the rect by the AA boundary padding
1806 left -= boundarySizeX;
1807 right += boundarySizeX;
1808 top -= boundarySizeY;
1809 bottom += boundarySizeY;
1810
Chet Haase858aa932011-05-12 09:06:00 -07001811 if (!quickReject(left, top, right, bottom)) {
Romain Guy04299382012-07-18 17:15:41 -07001812 setupDraw();
1813 setupDrawNoTexture();
1814 setupDrawAALine();
1815 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1816 setupDrawColorFilter();
1817 setupDrawShader();
1818 setupDrawBlending(true, mode);
1819 setupDrawProgram();
1820 setupDrawModelViewIdentity(true);
1821 setupDrawColorUniforms();
1822 setupDrawColorFilterUniforms();
1823 setupDrawShaderIdentityUniforms();
1824
1825 AAVertex rects[4];
1826 AAVertex* aaVertices = &rects[0];
1827 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1828 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1829
1830 int widthSlot;
1831 int lengthSlot;
1832
1833 float width = right - left;
1834 float height = bottom - top;
1835
1836 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1837 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1838 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1839 boundaryWidthProportion, widthSlot, lengthSlot);
1840
1841 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1842 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1843 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1844 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion));
1845
Chet Haase858aa932011-05-12 09:06:00 -07001846 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1847 AAVertex::set(aaVertices++, left, top, 1, 0);
1848 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1849 AAVertex::set(aaVertices++, right, top, 0, 0);
1850 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guy7b631422012-04-04 11:38:54 -07001851
Romain Guy04299382012-07-18 17:15:41 -07001852 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1853
1854 finishDrawAALine(widthSlot, lengthSlot);
1855 }
Chet Haase858aa932011-05-12 09:06:00 -07001856}
1857
1858/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001859 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1860 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1861 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1862 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1863 * of the line. Hairlines are more involved because we need to account for transform scaling
1864 * to end up with a one-pixel-wide line in screen space..
1865 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1866 * in combination with values that we calculate and pass down in this method. The basic approach
1867 * is that the quad we create contains both the core line area plus a bounding area in which
1868 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1869 * proportion of the width and the length of a given segment is represented by the boundary
1870 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1871 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1872 * on the inside). This ends up giving the result we want, with pixels that are completely
1873 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1874 * how far into the boundary region they are, which is determined by shader interpolation.
1875 */
Chet Haase48659092012-05-31 15:21:51 -07001876status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1877 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07001878
1879 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001880 // We use half the stroke width here because we're going to position the quad
1881 // corner vertices half of the width away from the line endpoints
1882 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001883 // A stroke width of 0 has a special meaning in Skia:
1884 // it draws a line 1 px wide regardless of current transform
1885 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001886
Chet Haase99ecdc42011-05-06 12:06:34 -07001887 float inverseScaleX = 1.0f;
1888 float inverseScaleY = 1.0f;
1889 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001890
Chet Haase8a5cc922011-04-26 07:28:09 -07001891 int alpha;
1892 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001893
Chet Haase8a5cc922011-04-26 07:28:09 -07001894 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001895 int verticesCount = count;
1896 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001897 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001898 verticesCount += (count - 4);
1899 }
1900
1901 if (isHairLine || isAA) {
1902 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1903 // the line on the screen should always be one pixel wide regardless of scale. For
1904 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001905 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001906 Matrix4 *mat = mSnapshot->transform;
1907 float m00 = mat->data[Matrix4::kScaleX];
1908 float m01 = mat->data[Matrix4::kSkewY];
1909 float m02 = mat->data[2];
1910 float m10 = mat->data[Matrix4::kSkewX];
1911 float m11 = mat->data[Matrix4::kScaleX];
1912 float m12 = mat->data[6];
Romain Guy7b631422012-04-04 11:38:54 -07001913
Romain Guy211370f2012-02-01 16:10:55 -08001914 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1915 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001916
Chet Haase99ecdc42011-05-06 12:06:34 -07001917 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1918 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001919
Chet Haase99ecdc42011-05-06 12:06:34 -07001920 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1921 scaled = true;
1922 }
1923 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001924 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001925
1926 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07001927
1928 mCaches.enableScissor();
1929
Chet Haase8a5cc922011-04-26 07:28:09 -07001930 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001931 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001932 if (isAA) {
1933 setupDrawAALine();
1934 }
1935 setupDrawColor(paint->getColor(), alpha);
1936 setupDrawColorFilter();
1937 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001938 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001939 setupDrawProgram();
1940 setupDrawModelViewIdentity(true);
1941 setupDrawColorUniforms();
1942 setupDrawColorFilterUniforms();
1943 setupDrawShaderIdentityUniforms();
1944
1945 if (isHairLine) {
1946 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001947 halfStrokeWidth = isAA? 1 : .5;
1948 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001949 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001950 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001951 }
Romain Guy7b631422012-04-04 11:38:54 -07001952
1953 int widthSlot;
1954 int lengthSlot;
1955
Chet Haase5b0200b2011-04-13 17:58:08 -07001956 Vertex lines[verticesCount];
1957 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001958
Chet Haase99585ad2011-05-02 15:00:16 -07001959 AAVertex wLines[verticesCount];
1960 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001961
Romain Guy211370f2012-02-01 16:10:55 -08001962 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001963 setupDrawVertices(vertices);
1964 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001965 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1966 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001967 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001968 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1969 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001970 // We will need to calculate the actual width proportion on each segment for
1971 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1972 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001973 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1974 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001975 }
1976
Chet Haase99ecdc42011-05-06 12:06:34 -07001977 AAVertex* prevAAVertex = NULL;
1978 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001979
Chet Haase99585ad2011-05-02 15:00:16 -07001980 int boundaryLengthSlot = -1;
1981 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001982 int boundaryWidthSlot = -1;
1983 int inverseBoundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001984
Chet Haase5b0200b2011-04-13 17:58:08 -07001985 for (int i = 0; i < count; i += 4) {
1986 // a = start point, b = end point
1987 vec2 a(points[i], points[i + 1]);
1988 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001989
Chet Haase99585ad2011-05-02 15:00:16 -07001990 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001991 float boundaryLengthProportion = 0;
1992 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001993
Chet Haase5b0200b2011-04-13 17:58:08 -07001994 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001995 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001996 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001997 if (isAA) {
1998 float wideningFactor;
1999 if (fabs(n.x) >= fabs(n.y)) {
2000 wideningFactor = fabs(1.0f / n.x);
2001 } else {
2002 wideningFactor = fabs(1.0f / n.y);
2003 }
2004 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002005 }
Romain Guy7b631422012-04-04 11:38:54 -07002006
Chet Haase99ecdc42011-05-06 12:06:34 -07002007 if (scaled) {
2008 n.x *= inverseScaleX;
2009 n.y *= inverseScaleY;
2010 }
2011 } else if (scaled) {
2012 // Extend n by .5 pixel on each side, post-transform
2013 vec2 extendedN = n.copyNormalized();
2014 extendedN /= 2;
2015 extendedN.x *= inverseScaleX;
2016 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002017
Chet Haase99ecdc42011-05-06 12:06:34 -07002018 float extendedNLength = extendedN.length();
2019 // We need to set this value on the shader prior to drawing
2020 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
2021 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002022 }
Romain Guy7b631422012-04-04 11:38:54 -07002023
Chet Haase5b0200b2011-04-13 17:58:08 -07002024 float x = n.x;
2025 n.x = -n.y;
2026 n.y = x;
2027
Chet Haase99585ad2011-05-02 15:00:16 -07002028 // aa lines expand the endpoint vertices to encompass the AA boundary
2029 if (isAA) {
2030 vec2 abVector = (b - a);
2031 length = abVector.length();
2032 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002033
Chet Haase99ecdc42011-05-06 12:06:34 -07002034 if (scaled) {
2035 abVector.x *= inverseScaleX;
2036 abVector.y *= inverseScaleY;
2037 float abLength = abVector.length();
2038 boundaryLengthProportion = abLength / (length + abLength);
2039 } else {
2040 boundaryLengthProportion = .5 / (length + 1);
2041 }
Romain Guy7b631422012-04-04 11:38:54 -07002042
Chet Haase99ecdc42011-05-06 12:06:34 -07002043 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002044 a -= abVector;
2045 b += abVector;
2046 }
2047
Chet Haase5b0200b2011-04-13 17:58:08 -07002048 // Four corners of the rectangle defining a thick line
2049 vec2 p1 = a - n;
2050 vec2 p2 = a + n;
2051 vec2 p3 = b + n;
2052 vec2 p4 = b - n;
2053
Chet Haase99585ad2011-05-02 15:00:16 -07002054
Chet Haase5b0200b2011-04-13 17:58:08 -07002055 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2056 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2057 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2058 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2059
Romain Guy04299382012-07-18 17:15:41 -07002060 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002061 if (!isAA) {
2062 if (prevVertex != NULL) {
2063 // Issue two repeat vertices to create degenerate triangles to bridge
2064 // between the previous line and the new one. This is necessary because
2065 // we are creating a single triangle_strip which will contain
2066 // potentially discontinuous line segments.
2067 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2068 Vertex::set(vertices++, p1.x, p1.y);
2069 generatedVerticesCount += 2;
2070 }
Romain Guy7b631422012-04-04 11:38:54 -07002071
Chet Haase5b0200b2011-04-13 17:58:08 -07002072 Vertex::set(vertices++, p1.x, p1.y);
2073 Vertex::set(vertices++, p2.x, p2.y);
2074 Vertex::set(vertices++, p4.x, p4.y);
2075 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002076
Chet Haase5b0200b2011-04-13 17:58:08 -07002077 prevVertex = vertices - 1;
2078 generatedVerticesCount += 4;
2079 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002080 if (!isHairLine && scaled) {
2081 // Must set width proportions per-segment for scaled non-hairlines to use the
2082 // correct AA boundary dimensions
2083 if (boundaryWidthSlot < 0) {
2084 boundaryWidthSlot =
2085 mCaches.currentProgram->getUniform("boundaryWidth");
2086 inverseBoundaryWidthSlot =
2087 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
2088 }
Romain Guy7b631422012-04-04 11:38:54 -07002089
Chet Haase99ecdc42011-05-06 12:06:34 -07002090 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
2091 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
2092 }
Romain Guy7b631422012-04-04 11:38:54 -07002093
Chet Haase99585ad2011-05-02 15:00:16 -07002094 if (boundaryLengthSlot < 0) {
2095 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
2096 inverseBoundaryLengthSlot =
2097 mCaches.currentProgram->getUniform("inverseBoundaryLength");
2098 }
Romain Guy7b631422012-04-04 11:38:54 -07002099
Chet Haase99ecdc42011-05-06 12:06:34 -07002100 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
2101 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07002102
Chet Haase5b0200b2011-04-13 17:58:08 -07002103 if (prevAAVertex != NULL) {
2104 // Issue two repeat vertices to create degenerate triangles to bridge
2105 // between the previous line and the new one. This is necessary because
2106 // we are creating a single triangle_strip which will contain
2107 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002108 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2109 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2110 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002111 generatedVerticesCount += 2;
2112 }
Romain Guy7b631422012-04-04 11:38:54 -07002113
Chet Haase99585ad2011-05-02 15:00:16 -07002114 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2115 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2116 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2117 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002118
Chet Haase5b0200b2011-04-13 17:58:08 -07002119 prevAAVertex = aaVertices - 1;
2120 generatedVerticesCount += 4;
2121 }
Romain Guy7b631422012-04-04 11:38:54 -07002122
Chet Haase99585ad2011-05-02 15:00:16 -07002123 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2124 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2125 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002126 }
2127 }
Romain Guy7b631422012-04-04 11:38:54 -07002128
Chet Haase5b0200b2011-04-13 17:58:08 -07002129 if (generatedVerticesCount > 0) {
2130 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2131 }
Romain Guy7b631422012-04-04 11:38:54 -07002132
2133 if (isAA) {
2134 finishDrawAALine(widthSlot, lengthSlot);
2135 }
Chet Haase48659092012-05-31 15:21:51 -07002136
2137 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002138}
2139
Chet Haase48659092012-05-31 15:21:51 -07002140status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2141 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002142
2143 // TODO: The paint's cap style defines whether the points are square or circular
2144 // TODO: Handle AA for round points
2145
Chet Haase5b0200b2011-04-13 17:58:08 -07002146 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002147 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002148 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002149 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002150 if (isHairLine) {
2151 // Now that we know it's hairline, we can set the effective width, to be used later
2152 strokeWidth = 1.0f;
2153 }
2154 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002155 int alpha;
2156 SkXfermode::Mode mode;
2157 getAlphaAndMode(paint, &alpha, &mode);
2158
2159 int verticesCount = count >> 1;
2160 int generatedVerticesCount = 0;
2161
2162 TextureVertex pointsData[verticesCount];
2163 TextureVertex* vertex = &pointsData[0];
2164
Romain Guy04299382012-07-18 17:15:41 -07002165 // TODO: We should optimize this method to not generate vertices for points
2166 // that lie outside of the clip.
2167 mCaches.enableScissor();
2168
Romain Guyed6fcb02011-03-21 13:11:28 -07002169 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002170 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002171 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002172 setupDrawColor(paint->getColor(), alpha);
2173 setupDrawColorFilter();
2174 setupDrawShader();
2175 setupDrawBlending(mode);
2176 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002177 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002178 setupDrawColorUniforms();
2179 setupDrawColorFilterUniforms();
2180 setupDrawPointUniforms();
2181 setupDrawShaderIdentityUniforms();
2182 setupDrawMesh(vertex);
2183
2184 for (int i = 0; i < count; i += 2) {
2185 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2186 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002187
Chet Haase8a5cc922011-04-26 07:28:09 -07002188 float left = points[i] - halfWidth;
2189 float right = points[i] + halfWidth;
2190 float top = points[i + 1] - halfWidth;
2191 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002192
Chet Haase8a5cc922011-04-26 07:28:09 -07002193 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002194 }
2195
2196 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002197
2198 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002199}
2200
Chet Haase48659092012-05-31 15:21:51 -07002201status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002202 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002203 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002204
Romain Guyae88e5e2010-10-22 17:49:18 -07002205 Rect& clip(*mSnapshot->clipRect);
2206 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002207
Romain Guy3d58c032010-07-14 16:34:53 -07002208 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002209
2210 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002211}
Romain Guy9d5316e2010-06-24 19:30:36 -07002212
Chet Haase48659092012-05-31 15:21:51 -07002213status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2214 SkPaint* paint) {
2215 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002216 const AutoTexture autoCleanup(texture);
2217
2218 const float x = left + texture->left - texture->offset;
2219 const float y = top + texture->top - texture->offset;
2220
2221 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002222
2223 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002224}
2225
Chet Haase48659092012-05-31 15:21:51 -07002226status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002227 float rx, float ry, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002228 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002229
Romain Guya1d3c912011-12-13 14:55:06 -08002230 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002231 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2232 right - left, bottom - top, rx, ry, paint);
Chet Haase48659092012-05-31 15:21:51 -07002233 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002234}
2235
Chet Haase48659092012-05-31 15:21:51 -07002236status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2237 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002238
Romain Guya1d3c912011-12-13 14:55:06 -08002239 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002240 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Chet Haase48659092012-05-31 15:21:51 -07002241 return drawShape(x - radius, y - radius, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002242}
Romain Guy01d58e42011-01-19 21:54:02 -08002243
Chet Haase48659092012-05-31 15:21:51 -07002244status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
2245 SkPaint* paint) {
2246 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002247
Romain Guya1d3c912011-12-13 14:55:06 -08002248 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002249 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002250 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002251}
2252
Chet Haase48659092012-05-31 15:21:51 -07002253status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002254 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002255 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002256
2257 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002258 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002259 }
2260
Romain Guya1d3c912011-12-13 14:55:06 -08002261 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002262 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2263 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002264 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002265}
2266
Chet Haase48659092012-05-31 15:21:51 -07002267status_t OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002268 SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002269 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002270
Romain Guya1d3c912011-12-13 14:55:06 -08002271 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002272 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002273 return drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002274}
2275
Chet Haase48659092012-05-31 15:21:51 -07002276status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002277 if (p->getStyle() != SkPaint::kFill_Style) {
Chet Haase48659092012-05-31 15:21:51 -07002278 return drawRectAsShape(left, top, right, bottom, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002279 }
2280
Romain Guy6926c722010-07-12 20:20:03 -07002281 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002282 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002283 }
2284
Romain Guy026c5e162010-06-28 17:12:22 -07002285 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002286 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002287 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2288 if (!isMode) {
2289 // Assume SRC_OVER
2290 mode = SkXfermode::kSrcOver_Mode;
2291 }
2292 } else {
2293 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002294 }
2295
Romain Guy026c5e162010-06-28 17:12:22 -07002296 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002297 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002298 drawAARect(left, top, right, bottom, color, mode);
2299 } else {
2300 drawColorRect(left, top, right, bottom, color, mode);
2301 }
Chet Haase48659092012-05-31 15:21:51 -07002302
2303 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002304}
Romain Guy9d5316e2010-06-24 19:30:36 -07002305
Raph Levien416a8472012-07-19 22:48:17 -07002306void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2307 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2308 float x, float y) {
2309 mCaches.activeTexture(0);
2310
2311 // NOTE: The drop shadow will not perform gamma correction
2312 // if shader-based correction is enabled
2313 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2314 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2315 paint, text, bytesCount, count, mShadowRadius, positions);
2316 const AutoTexture autoCleanup(shadow);
2317
2318 const float sx = x - shadow->left + mShadowDx;
2319 const float sy = y - shadow->top + mShadowDy;
2320
2321 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2322 int shadowColor = mShadowColor;
2323 if (mShader) {
2324 shadowColor = 0xffffffff;
2325 }
2326
2327 setupDraw();
2328 setupDrawWithTexture(true);
2329 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2330 setupDrawColorFilter();
2331 setupDrawShader();
2332 setupDrawBlending(true, mode);
2333 setupDrawProgram();
2334 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2335 setupDrawTexture(shadow->id);
2336 setupDrawPureColorUniforms();
2337 setupDrawColorFilterUniforms();
2338 setupDrawShaderUniforms();
2339 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2340
2341 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2342}
2343
Chet Haase48659092012-05-31 15:21:51 -07002344status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002345 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002346 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002347 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002348 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002349 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002350
Romain Guy671d6cf2012-01-18 12:39:17 -08002351 // NOTE: Skia does not support perspective transform on drawPosText yet
2352 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002353 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002354 }
2355
2356 float x = 0.0f;
2357 float y = 0.0f;
2358 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2359 if (pureTranslate) {
2360 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2361 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2362 }
2363
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002364 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002365 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2366 paint->getTextSize());
2367
2368 int alpha;
2369 SkXfermode::Mode mode;
2370 getAlphaAndMode(paint, &alpha, &mode);
2371
Raph Levien416a8472012-07-19 22:48:17 -07002372 if (CC_UNLIKELY(mHasShadow)) {
2373 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2374 0.0f, 0.0f);
2375 }
2376
Romain Guy671d6cf2012-01-18 12:39:17 -08002377 // Pick the appropriate texture filtering
2378 bool linearFilter = mSnapshot->transform->changesBounds();
2379 if (pureTranslate && !linearFilter) {
2380 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2381 }
2382
2383 mCaches.activeTexture(0);
2384 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002385 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002386 setupDrawDirtyRegionsDisabled();
2387 setupDrawWithTexture(true);
2388 setupDrawAlpha8Color(paint->getColor(), alpha);
2389 setupDrawColorFilter();
2390 setupDrawShader();
2391 setupDrawBlending(true, mode);
2392 setupDrawProgram();
2393 setupDrawModelView(x, y, x, y, pureTranslate, true);
2394 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2395 setupDrawPureColorUniforms();
2396 setupDrawColorFilterUniforms();
2397 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002398 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002399
2400 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2401 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2402
2403#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002404 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002405#else
Romain Guy211370f2012-02-01 16:10:55 -08002406 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002407#endif
2408
2409 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2410 positions, hasActiveLayer ? &bounds : NULL)) {
2411#if RENDER_LAYERS_AS_REGIONS
2412 if (hasActiveLayer) {
2413 if (!pureTranslate) {
2414 mSnapshot->transform->mapRect(bounds);
2415 }
2416 dirtyLayerUnchecked(bounds, getRegion());
2417 }
2418#endif
2419 }
Chet Haase48659092012-05-31 15:21:51 -07002420
2421 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002422}
2423
Romain Guyc2525952012-07-27 16:41:22 -07002424status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002425 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002426 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002427 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002428 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002429 }
2430
Chet Haasea1cff502012-02-21 13:43:44 -08002431 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002432 switch (paint->getTextAlign()) {
2433 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002434 x -= length / 2.0f;
2435 break;
2436 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002437 x -= length;
2438 break;
2439 default:
2440 break;
2441 }
2442
Romain Guycac5fd32011-12-01 20:08:50 -08002443 SkPaint::FontMetrics metrics;
2444 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002445 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002446 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002447 }
2448
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002449 const float oldX = x;
2450 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002451 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002452 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002453 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2454 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2455 }
2456
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002457#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002458 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002459 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002460#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002461
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002462 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002463 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002464 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002465
Romain Guy86568192010-12-14 15:55:39 -08002466 int alpha;
2467 SkXfermode::Mode mode;
2468 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002469
Romain Guy211370f2012-02-01 16:10:55 -08002470 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002471 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2472 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002473 }
2474
Romain Guy6620c6d2010-12-06 18:07:02 -08002475 // Pick the appropriate texture filtering
2476 bool linearFilter = mSnapshot->transform->changesBounds();
2477 if (pureTranslate && !linearFilter) {
2478 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2479 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002480
Romain Guy16c88082012-06-11 16:03:47 -07002481 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002482 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002483 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002484 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002485 setupDrawDirtyRegionsDisabled();
2486 setupDrawWithTexture(true);
2487 setupDrawAlpha8Color(paint->getColor(), alpha);
2488 setupDrawColorFilter();
2489 setupDrawShader();
2490 setupDrawBlending(true, mode);
2491 setupDrawProgram();
2492 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002493 // See comment above; the font renderer must use texture unit 0
2494 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002495 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2496 setupDrawPureColorUniforms();
2497 setupDrawColorFilterUniforms();
2498 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002499 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002500
Romain Guy6620c6d2010-12-06 18:07:02 -08002501 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002502 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2503
2504#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002505 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002506#else
Romain Guy211370f2012-02-01 16:10:55 -08002507 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002508#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002509
Raph Levien996e57c2012-07-23 15:22:52 -07002510 bool status;
Raph Levien8b4072d2012-07-30 15:50:00 -07002511 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
2512 SkPaint paintCopy(*paint);
2513 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2514 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Raph Levien996e57c2012-07-23 15:22:52 -07002515 positions, hasActiveLayer ? &bounds : NULL);
2516 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002517 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2518 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002519 }
2520 if (status) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002521#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002522 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002523 if (!pureTranslate) {
2524 mSnapshot->transform->mapRect(bounds);
2525 }
Romain Guyf219da52011-01-16 12:54:25 -08002526 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002527 }
2528#endif
2529 }
Romain Guy694b5192010-07-21 21:33:20 -07002530
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002531 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002532
2533 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002534}
2535
Chet Haase48659092012-05-31 15:21:51 -07002536status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002537 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002538 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2539 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002540 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002541 }
2542
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002543 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002544 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2545 paint->getTextSize());
2546
2547 int alpha;
2548 SkXfermode::Mode mode;
2549 getAlphaAndMode(paint, &alpha, &mode);
2550
2551 mCaches.activeTexture(0);
2552 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002553 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002554 setupDrawDirtyRegionsDisabled();
2555 setupDrawWithTexture(true);
2556 setupDrawAlpha8Color(paint->getColor(), alpha);
2557 setupDrawColorFilter();
2558 setupDrawShader();
2559 setupDrawBlending(true, mode);
2560 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002561 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002562 setupDrawTexture(fontRenderer.getTexture(true));
2563 setupDrawPureColorUniforms();
2564 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002565 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002566 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002567
Romain Guy97771732012-02-28 18:17:02 -08002568 const Rect* clip = &mSnapshot->getLocalClip();
2569 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 -08002570
Romain Guy97771732012-02-28 18:17:02 -08002571#if RENDER_LAYERS_AS_REGIONS
2572 const bool hasActiveLayer = hasLayer();
2573#else
2574 const bool hasActiveLayer = false;
2575#endif
2576
2577 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2578 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2579#if RENDER_LAYERS_AS_REGIONS
2580 if (hasActiveLayer) {
2581 mSnapshot->transform->mapRect(bounds);
2582 dirtyLayerUnchecked(bounds, getRegion());
2583 }
2584#endif
2585 }
Chet Haase48659092012-05-31 15:21:51 -07002586
2587 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002588}
2589
Chet Haase48659092012-05-31 15:21:51 -07002590status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2591 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002592
Romain Guya1d3c912011-12-13 14:55:06 -08002593 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002594
Romain Guy33f6beb2012-02-16 19:24:51 -08002595 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002596 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002597 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002598 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002599
Romain Guy8b55f372010-08-18 17:10:07 -07002600 const float x = texture->left - texture->offset;
2601 const float y = texture->top - texture->offset;
2602
Romain Guy01d58e42011-01-19 21:54:02 -08002603 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002604
2605 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002606}
2607
Chet Haase48659092012-05-31 15:21:51 -07002608status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guyada830f2011-01-13 12:13:20 -08002609 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Chet Haase48659092012-05-31 15:21:51 -07002610 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002611 }
2612
Romain Guy2bf68f02012-03-02 13:37:47 -08002613 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2614 OpenGLRenderer* renderer = layer->renderer;
2615 Rect& dirty = layer->dirtyRect;
2616
2617 interrupt();
2618 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2619 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002620 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002621 renderer->finish();
2622 resume();
2623
2624 dirty.setEmpty();
2625 layer->deferredUpdateScheduled = false;
2626 layer->renderer = NULL;
2627 layer->displayList = NULL;
2628 }
2629
Romain Guya1d3c912011-12-13 14:55:06 -08002630 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002631
2632 int alpha;
2633 SkXfermode::Mode mode;
2634 getAlphaAndMode(paint, &alpha, &mode);
2635
Romain Guy9ace8f52011-07-07 20:50:11 -07002636 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002637
Romain Guyf219da52011-01-16 12:54:25 -08002638#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002639 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002640 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002641 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002642 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002643 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002644 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002645
Romain Guyc88e3572011-01-22 00:32:12 -08002646 setupDraw();
2647 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002648 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002649 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002650 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002651 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002652 setupDrawPureColorUniforms();
2653 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002654 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002655 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002656 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2657 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2658
Romain Guyd21b6e12011-11-30 20:21:23 -08002659 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002660 setupDrawModelViewTranslate(x, y,
2661 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2662 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002663 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002664 setupDrawModelViewTranslate(x, y,
2665 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2666 }
Romain Guyc88e3572011-01-22 00:32:12 -08002667 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002668
Romain Guyc88e3572011-01-22 00:32:12 -08002669 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2670 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002671
Romain Guyc88e3572011-01-22 00:32:12 -08002672 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002673
2674#if DEBUG_LAYERS_AS_REGIONS
2675 drawRegionRects(layer->region);
2676#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002677 }
Romain Guyf219da52011-01-16 12:54:25 -08002678 }
2679#else
Romain Guyada830f2011-01-13 12:13:20 -08002680 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2681 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002682#endif
Chet Haase48659092012-05-31 15:21:51 -07002683
2684 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002685}
2686
Romain Guy6926c722010-07-12 20:20:03 -07002687///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002688// Shaders
2689///////////////////////////////////////////////////////////////////////////////
2690
2691void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002692 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002693}
2694
Romain Guy06f96e22010-07-30 19:18:16 -07002695void OpenGLRenderer::setupShader(SkiaShader* shader) {
2696 mShader = shader;
2697 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002698 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002699 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002700}
2701
Romain Guyd27977d2010-07-14 19:18:51 -07002702///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002703// Color filters
2704///////////////////////////////////////////////////////////////////////////////
2705
2706void OpenGLRenderer::resetColorFilter() {
2707 mColorFilter = NULL;
2708}
2709
2710void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2711 mColorFilter = filter;
2712}
2713
2714///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002715// Drop shadow
2716///////////////////////////////////////////////////////////////////////////////
2717
2718void OpenGLRenderer::resetShadow() {
2719 mHasShadow = false;
2720}
2721
2722void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2723 mHasShadow = true;
2724 mShadowRadius = radius;
2725 mShadowDx = dx;
2726 mShadowDy = dy;
2727 mShadowColor = color;
2728}
2729
2730///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002731// Draw filters
2732///////////////////////////////////////////////////////////////////////////////
2733
2734void OpenGLRenderer::resetPaintFilter() {
2735 mHasDrawFilter = false;
2736}
2737
2738void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2739 mHasDrawFilter = true;
2740 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2741 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2742}
2743
2744SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002745 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002746
2747 uint32_t flags = paint->getFlags();
2748
2749 mFilteredPaint = *paint;
2750 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2751
2752 return &mFilteredPaint;
2753}
2754
2755///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002756// Drawing implementation
2757///////////////////////////////////////////////////////////////////////////////
2758
Romain Guy01d58e42011-01-19 21:54:02 -08002759void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2760 float x, float y, SkPaint* paint) {
2761 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2762 return;
2763 }
2764
2765 int alpha;
2766 SkXfermode::Mode mode;
2767 getAlphaAndMode(paint, &alpha, &mode);
2768
2769 setupDraw();
2770 setupDrawWithTexture(true);
2771 setupDrawAlpha8Color(paint->getColor(), alpha);
2772 setupDrawColorFilter();
2773 setupDrawShader();
2774 setupDrawBlending(true, mode);
2775 setupDrawProgram();
2776 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2777 setupDrawTexture(texture->id);
2778 setupDrawPureColorUniforms();
2779 setupDrawColorFilterUniforms();
2780 setupDrawShaderUniforms();
2781 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2782
2783 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2784
2785 finishDrawTexture();
2786}
2787
Romain Guyf607bdc2010-09-10 19:20:06 -07002788// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002789#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2790#define kStdUnderline_Offset (1.0f / 9.0f)
2791#define kStdUnderline_Thickness (1.0f / 18.0f)
2792
2793void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2794 float x, float y, SkPaint* paint) {
2795 // Handle underline and strike-through
2796 uint32_t flags = paint->getFlags();
2797 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002798 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002799 float underlineWidth = length;
2800 // If length is > 0.0f, we already measured the text for the text alignment
2801 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002802 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002803 }
2804
Romain Guy211370f2012-02-01 16:10:55 -08002805 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002806 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002807 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002808
Raph Levien8b4072d2012-07-30 15:50:00 -07002809 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002810 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002811
Romain Guyf6834472011-01-23 13:32:12 -08002812 int linesCount = 0;
2813 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2814 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2815
2816 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002817 float points[pointsCount];
2818 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002819
2820 if (flags & SkPaint::kUnderlineText_Flag) {
2821 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002822 points[currentPoint++] = left;
2823 points[currentPoint++] = top;
2824 points[currentPoint++] = left + underlineWidth;
2825 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002826 }
2827
2828 if (flags & SkPaint::kStrikeThruText_Flag) {
2829 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002830 points[currentPoint++] = left;
2831 points[currentPoint++] = top;
2832 points[currentPoint++] = left + underlineWidth;
2833 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002834 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002835
Romain Guy726aeba2011-06-01 14:52:00 -07002836 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002837
Romain Guy726aeba2011-06-01 14:52:00 -07002838 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002839 }
2840 }
2841}
2842
Romain Guy026c5e162010-06-28 17:12:22 -07002843void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002844 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002845 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002846 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002847 color |= 0x00ffffff;
2848 }
2849
Romain Guy70ca14e2010-12-13 18:24:33 -08002850 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002851 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002852 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002853 setupDrawShader();
2854 setupDrawColorFilter();
2855 setupDrawBlending(mode);
2856 setupDrawProgram();
2857 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2858 setupDrawColorUniforms();
2859 setupDrawShaderUniforms(ignoreTransform);
2860 setupDrawColorFilterUniforms();
2861 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002862
Romain Guyc95c8d62010-09-17 15:31:32 -07002863 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2864}
2865
Romain Guy82ba8142010-07-09 13:25:56 -07002866void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002867 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002868 int alpha;
2869 SkXfermode::Mode mode;
2870 getAlphaAndMode(paint, &alpha, &mode);
2871
Romain Guyd21b6e12011-11-30 20:21:23 -08002872 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002873
Romain Guy211370f2012-02-01 16:10:55 -08002874 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002875 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2876 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2877
Romain Guyd21b6e12011-11-30 20:21:23 -08002878 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002879 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2880 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2881 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2882 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002883 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002884 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2885 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2886 GL_TRIANGLE_STRIP, gMeshCount);
2887 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002888}
2889
Romain Guybd6b79b2010-06-26 00:13:53 -07002890void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002891 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2892 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002893 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002894}
2895
2896void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002897 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002898 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002899 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002900
Romain Guy746b7402010-10-26 16:27:31 -07002901 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002902 setupDrawWithTexture();
2903 setupDrawColor(alpha, alpha, alpha, alpha);
2904 setupDrawColorFilter();
2905 setupDrawBlending(blend, mode, swapSrcDst);
2906 setupDrawProgram();
2907 if (!dirty) {
2908 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002909 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002910 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002911 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002912 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002913 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002914 }
Romain Guy86568192010-12-14 15:55:39 -08002915 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002916 setupDrawColorFilterUniforms();
2917 setupDrawTexture(texture);
2918 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002919
Romain Guy6820ac82010-09-15 18:11:50 -07002920 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002921
2922 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002923}
2924
Romain Guya5aed0d2010-09-09 14:42:43 -07002925void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002926 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002927 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002928
Romain Guy82ba8142010-07-09 13:25:56 -07002929 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002930 // These blend modes are not supported by OpenGL directly and have
2931 // to be implemented using shaders. Since the shader will perform
2932 // the blending, turn blending off here
2933 // If the blend mode cannot be implemented using shaders, fall
2934 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002935 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2936 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002937 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002938 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002939
Romain Guy82bc7a72012-01-03 14:13:39 -08002940 if (mCaches.blend) {
2941 glDisable(GL_BLEND);
2942 mCaches.blend = false;
2943 }
2944
2945 return;
2946 } else {
2947 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002948 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002949 }
2950
2951 if (!mCaches.blend) {
2952 glEnable(GL_BLEND);
2953 }
2954
2955 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2956 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2957
2958 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2959 glBlendFunc(sourceMode, destMode);
2960 mCaches.lastSrcMode = sourceMode;
2961 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002962 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002963 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002964 glDisable(GL_BLEND);
2965 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002966 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002967}
2968
Romain Guy889f8d12010-07-29 14:37:42 -07002969bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002970 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002971 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002972 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002973 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002974 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002975 }
Romain Guy6926c722010-07-12 20:20:03 -07002976 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002977}
2978
Romain Guy026c5e162010-06-28 17:12:22 -07002979void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002980 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002981 TextureVertex::setUV(v++, u1, v1);
2982 TextureVertex::setUV(v++, u2, v1);
2983 TextureVertex::setUV(v++, u1, v2);
2984 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002985}
2986
Chet Haase5c13d892010-10-08 08:37:55 -07002987void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002988 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07002989 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002990}
2991
Romain Guy9d5316e2010-06-24 19:30:36 -07002992}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002993}; // namespace android