blob: 9865ec4433792c88da88e6b2e66493e146bf73e3 [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
Chet Haase80250612012-08-15 13:46:54 -0700253 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700254 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800255 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700256 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700257
Romain Guya1d3c912011-12-13 14:55:06 -0800258 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800259 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700260
Romain Guy50c0f092010-10-19 11:42:22 -0700261 mCaches.blend = true;
262 glEnable(GL_BLEND);
263 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
264 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700265}
266
Romain Guyba6be8a2012-04-23 18:22:09 -0700267void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700268 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700269}
270
271void OpenGLRenderer::attachFunctor(Functor* functor) {
272 mFunctors.add(functor);
273}
274
Romain Guy8f3b8e32012-03-27 16:33:45 -0700275status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
276 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700277 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700278
Romain Guyba6be8a2012-04-23 18:22:09 -0700279 if (count > 0) {
280 SortedVector<Functor*> functors(mFunctors);
281 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700282
Romain Guyba6be8a2012-04-23 18:22:09 -0700283 DrawGlInfo info;
284 info.clipLeft = 0;
285 info.clipTop = 0;
286 info.clipRight = 0;
287 info.clipBottom = 0;
288 info.isLayer = false;
289 info.width = 0;
290 info.height = 0;
291 memset(info.transform, 0, sizeof(float) * 16);
292
293 for (size_t i = 0; i < count; i++) {
294 Functor* f = functors.itemAt(i);
295 result |= (*f)(DrawGlInfo::kModeProcess, &info);
296
Chris Craikc2c95432012-04-25 15:13:52 -0700297 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700298 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
299 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700300 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700301
Chris Craikc2c95432012-04-25 15:13:52 -0700302 if (result & DrawGlInfo::kStatusInvoke) {
303 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700304 }
305 }
306 }
307
Romain Guyc189ef52012-04-25 20:02:53 -0700308 mCaches.activeTexture(0);
309
Romain Guy8f3b8e32012-03-27 16:33:45 -0700310 return result;
311}
312
313status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800314 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700315 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700316
Romain Guy8a4ac612012-07-17 17:32:48 -0700317 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800318 if (mDirtyClip) {
319 setScissorFromClip();
320 }
Romain Guyd643bb52011-03-01 14:55:21 -0800321
Romain Guy80911b82011-03-16 15:30:12 -0700322 Rect clip(*mSnapshot->clipRect);
323 clip.snapToPixelBoundaries();
324
Romain Guyd643bb52011-03-01 14:55:21 -0800325 // 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 }
Romain Guyd643bb52011-03-01 14:55:21 -0800330
Romain Guy08aa2cb2011-03-17 11:06:57 -0700331 DrawGlInfo info;
332 info.clipLeft = clip.left;
333 info.clipTop = clip.top;
334 info.clipRight = clip.right;
335 info.clipBottom = clip.bottom;
336 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700337 info.width = getSnapshot()->viewport.getWidth();
338 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700339 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700340
Chet Haase48659092012-05-31 15:21:51 -0700341 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800342
Romain Guy8f3b8e32012-03-27 16:33:45 -0700343 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700344 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800345 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700346
Chris Craik65924a32012-04-05 17:52:11 -0700347 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700348 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700349 }
Romain Guycabfcc12011-03-07 18:06:46 -0800350 }
351
Chet Haasedaf98e92011-01-10 14:10:36 -0800352 resume();
Romain Guy65549432012-03-26 16:45:05 -0700353 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800354}
355
Romain Guyf6a11b82010-06-23 17:47:49 -0700356///////////////////////////////////////////////////////////////////////////////
357// State management
358///////////////////////////////////////////////////////////////////////////////
359
Romain Guybb9524b2010-06-22 18:56:38 -0700360int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700361 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700362}
363
364int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700365 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700366}
367
368void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700369 if (mSaveCount > 1) {
370 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700371 }
Romain Guybb9524b2010-06-22 18:56:38 -0700372}
373
374void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700375 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700376
Romain Guy8fb95422010-08-17 18:38:51 -0700377 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700378 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700379 }
Romain Guybb9524b2010-06-22 18:56:38 -0700380}
381
Romain Guy8aef54f2010-09-01 15:13:49 -0700382int OpenGLRenderer::saveSnapshot(int flags) {
383 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700384 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700385}
386
387bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700388 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700389 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700390 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700391
Romain Guybd6b79b2010-06-26 00:13:53 -0700392 sp<Snapshot> current = mSnapshot;
393 sp<Snapshot> previous = mSnapshot->previous;
394
Romain Guyeb993562010-10-05 18:14:38 -0700395 if (restoreOrtho) {
396 Rect& r = previous->viewport;
397 glViewport(r.left, r.top, r.right, r.bottom);
398 mOrthoMatrix.load(current->orthoMatrix);
399 }
400
Romain Guy8b55f372010-08-18 17:10:07 -0700401 mSaveCount--;
402 mSnapshot = previous;
403
Romain Guy2542d192010-08-18 11:47:12 -0700404 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700405 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700406 }
Romain Guy2542d192010-08-18 11:47:12 -0700407
Romain Guy5ec99242010-11-03 16:19:08 -0700408 if (restoreLayer) {
409 composeLayer(current, previous);
410 }
411
Romain Guy2542d192010-08-18 11:47:12 -0700412 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700413}
414
Romain Guyf6a11b82010-06-23 17:47:49 -0700415///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700416// Layers
417///////////////////////////////////////////////////////////////////////////////
418
419int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700420 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700421 const GLuint previousFbo = mSnapshot->fbo;
422 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700423
Romain Guyaf636eb2010-12-09 17:47:21 -0800424 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700425 int alpha = 255;
426 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700427
Romain Guye45362c2010-11-03 19:58:32 -0700428 if (p) {
429 alpha = p->getAlpha();
430 if (!mCaches.extensions.hasFramebufferFetch()) {
431 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
432 if (!isMode) {
433 // Assume SRC_OVER
434 mode = SkXfermode::kSrcOver_Mode;
435 }
436 } else {
437 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700438 }
439 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700440 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700441 }
Romain Guyd55a8612010-06-28 17:42:46 -0700442
Chet Haased48885a2012-08-28 17:43:28 -0700443 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700444 }
Romain Guyd55a8612010-06-28 17:42:46 -0700445
446 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700447}
448
449int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
450 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700451 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700452 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700453 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700454 SkPaint paint;
455 paint.setAlpha(alpha);
456 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700457 }
Romain Guyd55a8612010-06-28 17:42:46 -0700458}
Romain Guybd6b79b2010-06-26 00:13:53 -0700459
Romain Guy1c740bc2010-09-13 18:00:09 -0700460/**
461 * Layers are viewed by Skia are slightly different than layers in image editing
462 * programs (for instance.) When a layer is created, previously created layers
463 * and the frame buffer still receive every drawing command. For instance, if a
464 * layer is created and a shape intersecting the bounds of the layers and the
465 * framebuffer is draw, the shape will be drawn on both (unless the layer was
466 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
467 *
468 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
469 * texture. Unfortunately, this is inefficient as it requires every primitive to
470 * be drawn n + 1 times, where n is the number of active layers. In practice this
471 * means, for every primitive:
472 * - Switch active frame buffer
473 * - Change viewport, clip and projection matrix
474 * - Issue the drawing
475 *
476 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700477 * To avoid this, layers are implemented in a different way here, at least in the
478 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
479 * is set. When this flag is set we can redirect all drawing operations into a
480 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700481 *
482 * This implementation relies on the frame buffer being at least RGBA 8888. When
483 * a layer is created, only a texture is created, not an FBO. The content of the
484 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700485 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700486 * buffer and drawing continues as normal. This technique therefore treats the
487 * frame buffer as a scratch buffer for the layers.
488 *
489 * To compose the layers back onto the frame buffer, each layer texture
490 * (containing the original frame buffer data) is drawn as a simple quad over
491 * the frame buffer. The trick is that the quad is set as the composition
492 * destination in the blending equation, and the frame buffer becomes the source
493 * of the composition.
494 *
495 * Drawing layers with an alpha value requires an extra step before composition.
496 * An empty quad is drawn over the layer's region in the frame buffer. This quad
497 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
498 * quad is used to multiply the colors in the frame buffer. This is achieved by
499 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
500 * GL_ZERO, GL_SRC_ALPHA.
501 *
502 * Because glCopyTexImage2D() can be slow, an alternative implementation might
503 * be use to draw a single clipped layer. The implementation described above
504 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700505 *
506 * (1) The frame buffer is actually not cleared right away. To allow the GPU
507 * to potentially optimize series of calls to glCopyTexImage2D, the frame
508 * buffer is left untouched until the first drawing operation. Only when
509 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700510 */
Chet Haased48885a2012-08-28 17:43:28 -0700511bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
512 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700513 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700514 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700515
Romain Guyeb993562010-10-05 18:14:38 -0700516 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
517
Romain Guyf607bdc2010-09-10 19:20:06 -0700518 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700519 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700520 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700521 Rect untransformedBounds(bounds);
522 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700523
Chet Haased48885a2012-08-28 17:43:28 -0700524 // Layers only make sense if they are in the framebuffer's bounds
525 if (bounds.intersect(*mSnapshot->clipRect)) {
526 // We cannot work with sub-pixels in this case
527 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700528
Chet Haased48885a2012-08-28 17:43:28 -0700529 // When the layer is not an FBO, we may use glCopyTexImage so we
530 // need to make sure the layer does not extend outside the bounds
531 // of the framebuffer
532 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700533 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700534 } else if (fboLayer) {
535 clip.set(bounds);
536 mat4 inverse;
537 inverse.loadInverse(*mSnapshot->transform);
538 inverse.mapRect(clip);
539 clip.snapToPixelBoundaries();
540 if (clip.intersect(untransformedBounds)) {
541 clip.translate(-left, -top);
542 bounds.set(untransformedBounds);
543 } else {
544 clip.setEmpty();
545 }
Romain Guyad37cd32011-03-15 11:12:25 -0700546 }
Chet Haased48885a2012-08-28 17:43:28 -0700547 } else {
548 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700549 }
Romain Guybf434112010-09-16 14:40:17 -0700550
Romain Guy746b7402010-10-26 16:27:31 -0700551 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700552 bounds.getHeight() > mCaches.maxTextureSize ||
553 (fboLayer && clip.isEmpty())) {
554 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700555 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700556 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700557 }
558
559 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700560 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700561 return false;
562 }
Romain Guyf18fd992010-07-08 11:45:51 -0700563
Romain Guya1d3c912011-12-13 14:55:06 -0800564 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700565 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700566 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700567 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700568 }
569
Romain Guy9ace8f52011-07-07 20:50:11 -0700570 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700571 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700572 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
573 bounds.getWidth() / float(layer->getWidth()), 0.0f);
574 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700575 layer->setBlend(true);
Romain Guydda570202010-07-06 11:39:32 -0700576
Romain Guy8fb95422010-08-17 18:38:51 -0700577 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700578 mSnapshot->flags |= Snapshot::kFlagIsLayer;
579 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700580
Romain Guyeb993562010-10-05 18:14:38 -0700581 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700582 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700583 } else {
584 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700585 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800586 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700587 if (layer->isEmpty()) {
588 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700589 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700590 layer->getWidth(), layer->getHeight(), 0);
591 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800592 } else {
593 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700594 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800595 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700596
Romain Guy54be1cd2011-06-13 19:04:27 -0700597 // Enqueue the buffer coordinates to clear the corresponding region later
598 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700599 }
Romain Guyeb993562010-10-05 18:14:38 -0700600 }
Romain Guyf86ef572010-07-01 11:05:42 -0700601
Romain Guyd55a8612010-06-28 17:42:46 -0700602 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700603}
604
Chet Haased48885a2012-08-28 17:43:28 -0700605bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700606 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700607
Chet Haased48885a2012-08-28 17:43:28 -0700608 mSnapshot->region = &mSnapshot->layer->region;
609 mSnapshot->flags |= Snapshot::kFlagFboTarget;
Romain Guy5b3b3522010-10-27 18:57:51 -0700610
Chet Haased48885a2012-08-28 17:43:28 -0700611 mSnapshot->flags |= Snapshot::kFlagIsFboLayer;
612 mSnapshot->fbo = layer->getFbo();
613 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
614 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
615 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
616 mSnapshot->height = bounds.getHeight();
617 mSnapshot->flags |= Snapshot::kFlagDirtyOrtho;
618 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700619
620 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700621 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
622 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700623
624 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700625 if (layer->isEmpty()) {
626 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
627 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700628 }
629
630 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700631 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700632
633#if DEBUG_LAYERS_AS_REGIONS
634 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
635 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000636 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700637
638 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700639 layer->deleteTexture();
640 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700641
642 delete layer;
643
644 return false;
645 }
646#endif
647
648 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700649 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800650 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700651 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700652 glClear(GL_COLOR_BUFFER_BIT);
653
654 dirtyClip();
655
656 // Change the ortho projection
657 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
658 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
659
660 return true;
661}
662
Romain Guy1c740bc2010-09-13 18:00:09 -0700663/**
664 * Read the documentation of createLayer() before doing anything in this method.
665 */
Romain Guy1d83e192010-08-17 11:37:00 -0700666void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
667 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000668 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700669 return;
670 }
671
Romain Guy5b3b3522010-10-27 18:57:51 -0700672 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700673
674 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700675 // Detach the texture from the FBO
676 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
677
Romain Guyeb993562010-10-05 18:14:38 -0700678 // Unbind current FBO and restore previous one
679 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
680 }
681
Romain Guy1d83e192010-08-17 11:37:00 -0700682 Layer* layer = current->layer;
683 const Rect& rect = layer->layer;
684
Romain Guy9ace8f52011-07-07 20:50:11 -0700685 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700686 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700687 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700688 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700689 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700690 }
691
Romain Guy03750a02010-10-18 14:06:08 -0700692 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700693
Romain Guya1d3c912011-12-13 14:55:06 -0800694 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700695
Romain Guy5b3b3522010-10-27 18:57:51 -0700696 // When the layer is stored in an FBO, we can save a bit of fillrate by
697 // drawing only the dirty region
698 if (fboLayer) {
699 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700700 if (layer->getColorFilter()) {
701 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800702 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700703 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700704 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800705 resetColorFilter();
706 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700707 } else if (!rect.isEmpty()) {
708 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
709 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700710 }
Romain Guy8b55f372010-08-18 17:10:07 -0700711
Romain Guyeb993562010-10-05 18:14:38 -0700712 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800713 // Note: No need to use glDiscardFramebufferEXT() since we never
714 // create/compose layers that are not on screen with this
715 // code path
716 // See LayerRenderer::destroyLayer(Layer*)
717
Romain Guyeb993562010-10-05 18:14:38 -0700718 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
719 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700720 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700721 }
722
Romain Guy746b7402010-10-26 16:27:31 -0700723 dirtyClip();
724
Romain Guyeb993562010-10-05 18:14:38 -0700725 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700726 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700727 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700728 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700729 delete layer;
730 }
731}
732
Romain Guyaa6c24c2011-04-28 18:40:04 -0700733void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700734 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700735
Romain Guy302a9df2011-08-16 13:55:02 -0700736 mat4& transform = layer->getTransform();
737 if (!transform.isIdentity()) {
738 save(0);
739 mSnapshot->transform->multiply(transform);
740 }
741
Romain Guyaa6c24c2011-04-28 18:40:04 -0700742 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700743 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700744 setupDrawWithTexture();
745 } else {
746 setupDrawWithExternalTexture();
747 }
748 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700749 setupDrawColor(alpha, alpha, alpha, alpha);
750 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700751 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700752 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700753 setupDrawPureColorUniforms();
754 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700755 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
756 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700757 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700758 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700759 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700760 if (mSnapshot->transform->isPureTranslate() &&
761 layer->getWidth() == (uint32_t) rect.getWidth() &&
762 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700763 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
764 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
765
Romain Guyd21b6e12011-11-30 20:21:23 -0800766 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700767 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
768 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800769 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700770 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
771 }
772 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700773 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
774
775 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
776
777 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700778
779 if (!transform.isIdentity()) {
780 restore();
781 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700782}
783
Romain Guy5b3b3522010-10-27 18:57:51 -0700784void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700785 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700786 const Rect& texCoords = layer->texCoords;
787 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
788 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700789
Romain Guy9ace8f52011-07-07 20:50:11 -0700790 float x = rect.left;
791 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700792 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700793 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700794 layer->getHeight() == (uint32_t) rect.getHeight();
795
796 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700797 // When we're swapping, the layer is already in screen coordinates
798 if (!swap) {
799 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
800 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
801 }
802
Romain Guyd21b6e12011-11-30 20:21:23 -0800803 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700804 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800805 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700806 }
807
808 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
809 layer->getTexture(), layer->getAlpha() / 255.0f,
810 layer->getMode(), layer->isBlend(),
811 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
812 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700813
Romain Guyaa6c24c2011-04-28 18:40:04 -0700814 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
815 } else {
816 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
817 drawTextureLayer(layer, rect);
818 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
819 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700820}
821
822void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700823 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700824 layer->setRegionAsRect();
825
Romain Guy40667672011-03-18 14:34:03 -0700826 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700827
Romain Guy5b3b3522010-10-27 18:57:51 -0700828 layer->region.clear();
829 return;
830 }
831
Romain Guy8a3957d2011-09-07 17:55:15 -0700832 // TODO: See LayerRenderer.cpp::generateMesh() for important
833 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800834 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700835 size_t count;
836 const android::Rect* rects = layer->region.getArray(&count);
837
Romain Guy9ace8f52011-07-07 20:50:11 -0700838 const float alpha = layer->getAlpha() / 255.0f;
839 const float texX = 1.0f / float(layer->getWidth());
840 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800841 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700842
843 TextureVertex* mesh = mCaches.getRegionMesh();
844 GLsizei numQuads = 0;
845
Romain Guy7230a742011-01-10 22:26:16 -0800846 setupDraw();
847 setupDrawWithTexture();
848 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800849 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700850 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800851 setupDrawProgram();
852 setupDrawDirtyRegionsDisabled();
853 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800854 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700855 setupDrawTexture(layer->getTexture());
856 if (mSnapshot->transform->isPureTranslate()) {
857 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
858 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
859
Romain Guyd21b6e12011-11-30 20:21:23 -0800860 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700861 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
862 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800863 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700864 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
865 }
Romain Guy15bc6432011-12-13 13:11:32 -0800866 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700867
868 for (size_t i = 0; i < count; i++) {
869 const android::Rect* r = &rects[i];
870
871 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800872 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700873 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800874 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700875
876 // TODO: Reject quads outside of the clip
877 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
878 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
879 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
880 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
881
882 numQuads++;
883
884 if (numQuads >= REGION_MESH_QUAD_COUNT) {
885 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
886 numQuads = 0;
887 mesh = mCaches.getRegionMesh();
888 }
889 }
890
891 if (numQuads > 0) {
892 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
893 }
894
Romain Guy7230a742011-01-10 22:26:16 -0800895 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700896
897#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800898 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700899#endif
900
901 layer->region.clear();
902 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700903}
904
Romain Guy3a3133d2011-02-01 22:59:58 -0800905void OpenGLRenderer::drawRegionRects(const Region& region) {
906#if DEBUG_LAYERS_AS_REGIONS
907 size_t count;
908 const android::Rect* rects = region.getArray(&count);
909
910 uint32_t colors[] = {
911 0x7fff0000, 0x7f00ff00,
912 0x7f0000ff, 0x7fff00ff,
913 };
914
915 int offset = 0;
916 int32_t top = rects[0].top;
917
918 for (size_t i = 0; i < count; i++) {
919 if (top != rects[i].top) {
920 offset ^= 0x2;
921 top = rects[i].top;
922 }
923
924 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
925 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
926 SkXfermode::kSrcOver_Mode);
927 }
928#endif
929}
930
Romain Guy5b3b3522010-10-27 18:57:51 -0700931void OpenGLRenderer::dirtyLayer(const float left, const float top,
932 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -0800933 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700934 Rect bounds(left, top, right, bottom);
935 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800936 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700937 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700938}
939
940void OpenGLRenderer::dirtyLayer(const float left, const float top,
941 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -0800942 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700943 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800944 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800945 }
Romain Guy1bd1bad2011-01-14 20:07:20 -0800946}
947
948void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -0800949 if (bounds.intersect(*mSnapshot->clipRect)) {
950 bounds.snapToPixelBoundaries();
951 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
952 if (!dirty.isEmpty()) {
953 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700954 }
955 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700956}
957
Romain Guy54be1cd2011-06-13 19:04:27 -0700958void OpenGLRenderer::clearLayerRegions() {
959 const size_t count = mLayers.size();
960 if (count == 0) return;
961
962 if (!mSnapshot->isIgnored()) {
963 // Doing several glScissor/glClear here can negatively impact
964 // GPUs with a tiler architecture, instead we draw quads with
965 // the Clear blending mode
966
967 // The list contains bounds that have already been clipped
968 // against their initial clip rect, and the current clip
969 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -0700970 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -0700971
972 Vertex mesh[count * 6];
973 Vertex* vertex = mesh;
974
975 for (uint32_t i = 0; i < count; i++) {
976 Rect* bounds = mLayers.itemAt(i);
977
978 Vertex::set(vertex++, bounds->left, bounds->bottom);
979 Vertex::set(vertex++, bounds->left, bounds->top);
980 Vertex::set(vertex++, bounds->right, bounds->top);
981 Vertex::set(vertex++, bounds->left, bounds->bottom);
982 Vertex::set(vertex++, bounds->right, bounds->top);
983 Vertex::set(vertex++, bounds->right, bounds->bottom);
984
985 delete bounds;
986 }
987
988 setupDraw(false);
989 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
990 setupDrawBlending(true, SkXfermode::kClear_Mode);
991 setupDrawProgram();
992 setupDrawPureColorUniforms();
993 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800994 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700995
Romain Guy54be1cd2011-06-13 19:04:27 -0700996 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -0700997
998 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -0700999 } else {
1000 for (uint32_t i = 0; i < count; i++) {
1001 delete mLayers.itemAt(i);
1002 }
1003 }
1004
1005 mLayers.clear();
1006}
1007
Romain Guybd6b79b2010-06-26 00:13:53 -07001008///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001009// Transforms
1010///////////////////////////////////////////////////////////////////////////////
1011
1012void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001013 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001014}
1015
1016void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001017 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001018}
1019
1020void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001021 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001022}
1023
Romain Guy807daf72011-01-18 11:19:19 -08001024void OpenGLRenderer::skew(float sx, float sy) {
1025 mSnapshot->transform->skew(sx, sy);
1026}
1027
Romain Guyf6a11b82010-06-23 17:47:49 -07001028void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001029 if (matrix) {
1030 mSnapshot->transform->load(*matrix);
1031 } else {
1032 mSnapshot->transform->loadIdentity();
1033 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001034}
1035
1036void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001037 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001038}
1039
1040void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001041 SkMatrix transform;
1042 mSnapshot->transform->copyTo(transform);
1043 transform.preConcat(*matrix);
1044 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001045}
1046
1047///////////////////////////////////////////////////////////////////////////////
1048// Clipping
1049///////////////////////////////////////////////////////////////////////////////
1050
Romain Guybb9524b2010-06-22 18:56:38 -07001051void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001052 Rect clip(*mSnapshot->clipRect);
1053 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001054
Romain Guy8a4ac612012-07-17 17:32:48 -07001055 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1056 clip.getWidth(), clip.getHeight())) {
1057 mDirtyClip = false;
1058 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001059}
1060
1061const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001062 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001063}
1064
Romain Guy8a4ac612012-07-17 17:32:48 -07001065bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1066 if (mSnapshot->isIgnored()) {
1067 return true;
1068 }
1069
1070 Rect r(left, top, right, bottom);
1071 mSnapshot->transform->mapRect(r);
1072 r.snapToPixelBoundaries();
1073
1074 Rect clipRect(*mSnapshot->clipRect);
1075 clipRect.snapToPixelBoundaries();
1076
1077 return !clipRect.intersects(r);
1078}
1079
Romain Guyc7d53492010-06-25 13:41:57 -07001080bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001081 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001082 return true;
1083 }
1084
Romain Guy1d83e192010-08-17 11:37:00 -07001085 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001086 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001087 r.snapToPixelBoundaries();
1088
1089 Rect clipRect(*mSnapshot->clipRect);
1090 clipRect.snapToPixelBoundaries();
1091
Romain Guy586cae32012-07-13 15:28:31 -07001092 bool rejected = !clipRect.intersects(r);
1093 if (!isDeferred() && !rejected) {
1094 mCaches.setScissorEnabled(!clipRect.contains(r));
1095 }
1096
1097 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001098}
1099
Romain Guy079ba2c2010-07-16 14:12:24 -07001100bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1101 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001102 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001103 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001104 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001105 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001106}
1107
Chet Haasea23eed82012-04-12 15:19:04 -07001108Rect* OpenGLRenderer::getClipRect() {
1109 return mSnapshot->clipRect;
1110}
1111
Romain Guyf6a11b82010-06-23 17:47:49 -07001112///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001113// Drawing commands
1114///////////////////////////////////////////////////////////////////////////////
1115
Romain Guy54be1cd2011-06-13 19:04:27 -07001116void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001117 // TODO: It would be best if we could do this before quickReject()
1118 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001119 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001120 if (mDirtyClip) {
1121 setScissorFromClip();
1122 }
1123 mDescription.reset();
1124 mSetShaderColor = false;
1125 mColorSet = false;
1126 mColorA = mColorR = mColorG = mColorB = 0.0f;
1127 mTextureUnit = 0;
1128 mTrackDirtyRegions = true;
1129}
1130
1131void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1132 mDescription.hasTexture = true;
1133 mDescription.hasAlpha8Texture = isAlpha8;
1134}
1135
Romain Guyaa6c24c2011-04-28 18:40:04 -07001136void OpenGLRenderer::setupDrawWithExternalTexture() {
1137 mDescription.hasExternalTexture = true;
1138}
1139
Romain Guy15bc6432011-12-13 13:11:32 -08001140void OpenGLRenderer::setupDrawNoTexture() {
1141 mCaches.disbaleTexCoordsVertexArray();
1142}
1143
Chet Haase5b0200b2011-04-13 17:58:08 -07001144void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001145 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001146}
1147
Chris Craik6ebdc112012-08-31 18:24:33 -07001148void OpenGLRenderer::setupDrawAARect() {
1149 mDescription.isAARect = true;
1150}
1151
Romain Guyed6fcb02011-03-21 13:11:28 -07001152void OpenGLRenderer::setupDrawPoint(float pointSize) {
1153 mDescription.isPoint = true;
1154 mDescription.pointSize = pointSize;
1155}
1156
Romain Guy70ca14e2010-12-13 18:24:33 -08001157void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001158 setupDrawColor(color, (color >> 24) & 0xFF);
1159}
1160
1161void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1162 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001163 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1164 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001165 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001166 mColorR = a * ((color >> 16) & 0xFF);
1167 mColorG = a * ((color >> 8) & 0xFF);
1168 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001169 mColorSet = true;
1170 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1171}
1172
Romain Guy86568192010-12-14 15:55:39 -08001173void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1174 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001175 // Double-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 Guy86568192010-12-14 15:55:39 -08001177 const float a = mColorA / 255.0f;
1178 mColorR = a * ((color >> 16) & 0xFF);
1179 mColorG = a * ((color >> 8) & 0xFF);
1180 mColorB = a * ((color ) & 0xFF);
1181 mColorSet = true;
1182 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1183}
1184
Romain Guy41210632012-07-16 17:04:24 -07001185void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1186 mCaches.fontRenderer->describe(mDescription, paint);
1187}
1188
Romain Guy70ca14e2010-12-13 18:24:33 -08001189void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1190 mColorA = a;
1191 mColorR = r;
1192 mColorG = g;
1193 mColorB = b;
1194 mColorSet = true;
1195 mSetShaderColor = mDescription.setColor(r, g, b, a);
1196}
1197
1198void OpenGLRenderer::setupDrawShader() {
1199 if (mShader) {
1200 mShader->describe(mDescription, mCaches.extensions);
1201 }
1202}
1203
1204void OpenGLRenderer::setupDrawColorFilter() {
1205 if (mColorFilter) {
1206 mColorFilter->describe(mDescription, mCaches.extensions);
1207 }
1208}
1209
Romain Guyf09ef512011-05-27 11:43:46 -07001210void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1211 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1212 mColorA = 1.0f;
1213 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001214 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001215 }
1216}
1217
Romain Guy70ca14e2010-12-13 18:24:33 -08001218void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001219 // When the blending mode is kClear_Mode, we need to use a modulate color
1220 // argb=1,0,0,0
1221 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001222 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1223 mDescription, swapSrcDst);
1224}
1225
1226void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001227 // When the blending mode is kClear_Mode, we need to use a modulate color
1228 // argb=1,0,0,0
1229 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001230 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1231 mDescription, swapSrcDst);
1232}
1233
1234void OpenGLRenderer::setupDrawProgram() {
1235 useProgram(mCaches.programCache.get(mDescription));
1236}
1237
1238void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1239 mTrackDirtyRegions = false;
1240}
1241
1242void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1243 bool ignoreTransform) {
1244 mModelView.loadTranslate(left, top, 0.0f);
1245 if (!ignoreTransform) {
1246 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1247 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1248 } else {
1249 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1250 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1251 }
1252}
1253
Chet Haase8a5cc922011-04-26 07:28:09 -07001254void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1255 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001256}
1257
Romain Guy70ca14e2010-12-13 18:24:33 -08001258void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1259 bool ignoreTransform, bool ignoreModelView) {
1260 if (!ignoreModelView) {
1261 mModelView.loadTranslate(left, top, 0.0f);
1262 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001263 } else {
1264 mModelView.loadIdentity();
1265 }
Romain Guy86568192010-12-14 15:55:39 -08001266 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1267 if (!ignoreTransform) {
1268 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1269 if (mTrackDirtyRegions && dirty) {
1270 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1271 }
1272 } else {
1273 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1274 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1275 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001276}
1277
Romain Guyed6fcb02011-03-21 13:11:28 -07001278void OpenGLRenderer::setupDrawPointUniforms() {
1279 int slot = mCaches.currentProgram->getUniform("pointSize");
1280 glUniform1f(slot, mDescription.pointSize);
1281}
1282
Romain Guy70ca14e2010-12-13 18:24:33 -08001283void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001284 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001285 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1286 }
1287}
1288
Romain Guy86568192010-12-14 15:55:39 -08001289void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001290 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001291 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001292 }
1293}
1294
Romain Guy70ca14e2010-12-13 18:24:33 -08001295void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1296 if (mShader) {
1297 if (ignoreTransform) {
1298 mModelView.loadInverse(*mSnapshot->transform);
1299 }
1300 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1301 }
1302}
1303
Romain Guy8d0d4782010-12-14 20:13:35 -08001304void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1305 if (mShader) {
1306 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1307 }
1308}
1309
Romain Guy70ca14e2010-12-13 18:24:33 -08001310void OpenGLRenderer::setupDrawColorFilterUniforms() {
1311 if (mColorFilter) {
1312 mColorFilter->setupProgram(mCaches.currentProgram);
1313 }
1314}
1315
Romain Guy41210632012-07-16 17:04:24 -07001316void OpenGLRenderer::setupDrawTextGammaUniforms() {
1317 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1318}
1319
Romain Guy70ca14e2010-12-13 18:24:33 -08001320void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001321 bool force = mCaches.bindMeshBuffer();
1322 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001323 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001324}
1325
1326void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1327 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001328 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001329 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001330}
1331
Romain Guyaa6c24c2011-04-28 18:40:04 -07001332void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1333 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001334 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001335 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001336}
1337
Romain Guy8f0095c2011-05-02 17:24:22 -07001338void OpenGLRenderer::setupDrawTextureTransform() {
1339 mDescription.hasTextureTransform = true;
1340}
1341
1342void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001343 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1344 GL_FALSE, &transform.data[0]);
1345}
1346
Romain Guy70ca14e2010-12-13 18:24:33 -08001347void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001348 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001349 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001350 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001351 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001352 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001353 }
Romain Guyd71dd362011-12-12 19:03:35 -08001354
Romain Guyf3a910b42011-12-12 20:35:21 -08001355 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001356 if (mCaches.currentProgram->texCoords >= 0) {
1357 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1358 }
1359
1360 mCaches.unbindIndicesBuffer();
1361}
1362
1363void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1364 bool force = mCaches.unbindMeshBuffer();
1365 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1366 if (mCaches.currentProgram->texCoords >= 0) {
1367 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001368 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001369}
1370
Chet Haase5b0200b2011-04-13 17:58:08 -07001371void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001372 bool force = mCaches.unbindMeshBuffer();
1373 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1374 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001375 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001376}
1377
1378/**
1379 * 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 -07001380 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1381 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1382 * attributes (one per vertex) are values from zero to one that tells the fragment
1383 * shader where the fragment is in relation to the line width/length overall; these values are
1384 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1385 * region of the line.
1386 * Note that we only pass down the width values in this setup function. The length coordinates
1387 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001388 */
Chet Haase99585ad2011-05-02 15:00:16 -07001389void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001390 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001391 bool force = mCaches.unbindMeshBuffer();
1392 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1393 vertices, gAAVertexStride);
1394 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001395 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001396
Romain Guy7b631422012-04-04 11:38:54 -07001397 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001398 glEnableVertexAttribArray(widthSlot);
1399 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001400
Romain Guy7b631422012-04-04 11:38:54 -07001401 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001402 glEnableVertexAttribArray(lengthSlot);
1403 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001404
Chet Haase5b0200b2011-04-13 17:58:08 -07001405 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001406 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001407}
1408
1409void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1410 glDisableVertexAttribArray(widthSlot);
1411 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001412}
1413
Romain Guy70ca14e2010-12-13 18:24:33 -08001414void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001415}
1416
1417///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001418// Drawing
1419///////////////////////////////////////////////////////////////////////////////
1420
Chet Haase1271e2c2012-04-20 09:54:27 -07001421status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001422 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001423
Romain Guy0fe478e2010-11-08 12:08:41 -08001424 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1425 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001426 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001427 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001428 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001429
Romain Guy65549432012-03-26 16:45:05 -07001430 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001431}
1432
Chet Haaseed30fd82011-04-22 16:18:45 -07001433void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1434 if (displayList) {
1435 displayList->output(*this, level);
1436 }
1437}
1438
Romain Guya168d732011-03-18 16:50:13 -07001439void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1440 int alpha;
1441 SkXfermode::Mode mode;
1442 getAlphaAndMode(paint, &alpha, &mode);
1443
Romain Guya168d732011-03-18 16:50:13 -07001444 float x = left;
1445 float y = top;
1446
Romain Guye3c26852011-07-25 16:36:01 -07001447 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001448 bool ignoreTransform = false;
1449 if (mSnapshot->transform->isPureTranslate()) {
1450 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1451 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1452 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001453 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001454 } else {
1455 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001456 }
1457
1458 setupDraw();
1459 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001460 if (paint) {
1461 setupDrawAlpha8Color(paint->getColor(), alpha);
1462 }
Romain Guya168d732011-03-18 16:50:13 -07001463 setupDrawColorFilter();
1464 setupDrawShader();
1465 setupDrawBlending(true, mode);
1466 setupDrawProgram();
1467 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001468
Romain Guya168d732011-03-18 16:50:13 -07001469 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001470 texture->setWrap(GL_CLAMP_TO_EDGE);
1471 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001472
Romain Guya168d732011-03-18 16:50:13 -07001473 setupDrawPureColorUniforms();
1474 setupDrawColorFilterUniforms();
1475 setupDrawShaderUniforms();
1476 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1477
1478 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1479
1480 finishDrawTexture();
1481}
1482
Chet Haase48659092012-05-31 15:21:51 -07001483status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001484 const float right = left + bitmap->width();
1485 const float bottom = top + bitmap->height();
1486
1487 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001488 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001489 }
1490
Romain Guya1d3c912011-12-13 14:55:06 -08001491 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001492 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001493 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001494 const AutoTexture autoCleanup(texture);
1495
Romain Guy211370f2012-02-01 16:10:55 -08001496 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001497 drawAlphaBitmap(texture, left, top, paint);
1498 } else {
1499 drawTextureRect(left, top, right, bottom, texture, paint);
1500 }
Chet Haase48659092012-05-31 15:21:51 -07001501
1502 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001503}
1504
Chet Haase48659092012-05-31 15:21:51 -07001505status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001506 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1507 const mat4 transform(*matrix);
1508 transform.mapRect(r);
1509
Romain Guy6926c722010-07-12 20:20:03 -07001510 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001511 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001512 }
1513
Romain Guya1d3c912011-12-13 14:55:06 -08001514 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001515 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001516 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001517 const AutoTexture autoCleanup(texture);
1518
Romain Guy5b3b3522010-10-27 18:57:51 -07001519 // This could be done in a cheaper way, all we need is pass the matrix
1520 // to the vertex shader. The save/restore is a bit overkill.
1521 save(SkCanvas::kMatrix_SaveFlag);
1522 concatMatrix(matrix);
1523 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1524 restore();
Chet Haase48659092012-05-31 15:21:51 -07001525
1526 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001527}
1528
Chet Haase48659092012-05-31 15:21:51 -07001529status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001530 const float right = left + bitmap->width();
1531 const float bottom = top + bitmap->height();
1532
1533 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001534 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001535 }
1536
1537 mCaches.activeTexture(0);
1538 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1539 const AutoTexture autoCleanup(texture);
1540
1541 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001542
1543 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001544}
1545
Chet Haase48659092012-05-31 15:21:51 -07001546status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001547 float* vertices, int* colors, SkPaint* paint) {
1548 // TODO: Do a quickReject
1549 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001550 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001551 }
1552
Romain Guya1d3c912011-12-13 14:55:06 -08001553 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001554 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001555 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001556 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001557
Romain Guyd21b6e12011-11-30 20:21:23 -08001558 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1559 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001560
1561 int alpha;
1562 SkXfermode::Mode mode;
1563 getAlphaAndMode(paint, &alpha, &mode);
1564
Romain Guy5a7b4662011-01-20 19:09:30 -08001565 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001566
Romain Guyb18d2d02011-02-10 15:52:54 -08001567 float left = FLT_MAX;
1568 float top = FLT_MAX;
1569 float right = FLT_MIN;
1570 float bottom = FLT_MIN;
1571
Romain Guy211370f2012-02-01 16:10:55 -08001572 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001573
Romain Guya566b7c2011-01-23 16:36:11 -08001574 // TODO: Support the colors array
1575 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001576 TextureVertex* vertex = mesh;
1577 for (int32_t y = 0; y < meshHeight; y++) {
1578 for (int32_t x = 0; x < meshWidth; x++) {
1579 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1580
1581 float u1 = float(x) / meshWidth;
1582 float u2 = float(x + 1) / meshWidth;
1583 float v1 = float(y) / meshHeight;
1584 float v2 = float(y + 1) / meshHeight;
1585
1586 int ax = i + (meshWidth + 1) * 2;
1587 int ay = ax + 1;
1588 int bx = i;
1589 int by = bx + 1;
1590 int cx = i + 2;
1591 int cy = cx + 1;
1592 int dx = i + (meshWidth + 1) * 2 + 2;
1593 int dy = dx + 1;
1594
1595 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1596 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1597 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1598
1599 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1600 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1601 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001602
Romain Guyb18d2d02011-02-10 15:52:54 -08001603 if (hasActiveLayer) {
1604 // TODO: This could be optimized to avoid unnecessary ops
1605 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1606 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1607 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1608 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1609 }
Romain Guy5a7b4662011-01-20 19:09:30 -08001610 }
1611 }
1612
Romain Guyb18d2d02011-02-10 15:52:54 -08001613 if (hasActiveLayer) {
1614 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1615 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001616
Romain Guy5a7b4662011-01-20 19:09:30 -08001617 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1618 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001619 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001620
1621 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001622}
1623
Chet Haase48659092012-05-31 15:21:51 -07001624status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001625 float srcLeft, float srcTop, float srcRight, float srcBottom,
1626 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001627 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001628 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001629 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001630 }
1631
Romain Guya1d3c912011-12-13 14:55:06 -08001632 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001633 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001634 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001635 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001636
Romain Guy8ba548f2010-06-30 19:21:21 -07001637 const float width = texture->width;
1638 const float height = texture->height;
1639
Romain Guy68169722011-08-22 17:33:33 -07001640 const float u1 = fmax(0.0f, srcLeft / width);
1641 const float v1 = fmax(0.0f, srcTop / height);
1642 const float u2 = fmin(1.0f, srcRight / width);
1643 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001644
Romain Guy03750a02010-10-18 14:06:08 -07001645 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001646 resetDrawTextureTexCoords(u1, v1, u2, v2);
1647
Romain Guy03750a02010-10-18 14:06:08 -07001648 int alpha;
1649 SkXfermode::Mode mode;
1650 getAlphaAndMode(paint, &alpha, &mode);
1651
Romain Guyd21b6e12011-11-30 20:21:23 -08001652 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1653
Romain Guy211370f2012-02-01 16:10:55 -08001654 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001655 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1656 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1657
Romain Guyb5014982011-07-28 15:39:12 -07001658 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001659 // Enable linear filtering if the source rectangle is scaled
1660 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001661 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001662 }
Romain Guyb5014982011-07-28 15:39:12 -07001663
Romain Guyd21b6e12011-11-30 20:21:23 -08001664 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001665 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1666 texture->id, alpha / 255.0f, mode, texture->blend,
1667 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1668 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1669 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001670 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001671 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1672 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1673 GL_TRIANGLE_STRIP, gMeshCount);
1674 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001675
1676 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001677
1678 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001679}
1680
Chet Haase48659092012-05-31 15:21:51 -07001681status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001682 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001683 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001684 int alpha;
1685 SkXfermode::Mode mode;
1686 getAlphaAndModeDirect(paint, &alpha, &mode);
1687
1688 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1689 left, top, right, bottom, alpha, mode);
1690}
1691
1692status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1693 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1694 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001695 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001696 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001697 }
1698
Romain Guybe6f9dc2012-07-16 12:41:17 -07001699 alpha *= mSnapshot->alpha;
1700
Romain Guya1d3c912011-12-13 14:55:06 -08001701 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001702 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001703 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001704 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001705 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1706 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001707
Romain Guy2728f962010-10-08 18:36:15 -07001708 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001709 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001710
Romain Guy211370f2012-02-01 16:10:55 -08001711 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001712 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001713 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001714 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001715 const float offsetX = left + mSnapshot->transform->getTranslateX();
1716 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001717 const size_t count = mesh->quads.size();
1718 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001719 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001720 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001721 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1722 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1723 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001724 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001725 dirtyLayer(left + bounds.left, top + bounds.top,
1726 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001727 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001728 }
1729 }
1730
Romain Guy211370f2012-02-01 16:10:55 -08001731 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001732 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1733 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1734
1735 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1736 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1737 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1738 true, !mesh->hasEmptyQuads);
1739 } else {
1740 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1741 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1742 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1743 true, !mesh->hasEmptyQuads);
1744 }
Romain Guy054dc182010-10-15 17:55:25 -07001745 }
Chet Haase48659092012-05-31 15:21:51 -07001746
1747 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001748}
1749
Chet Haase99ecdc42011-05-06 12:06:34 -07001750/**
Chet Haase858aa932011-05-12 09:06:00 -07001751 * This function uses a similar approach to that of AA lines in the drawLines() function.
Chris Craik6ebdc112012-08-31 18:24:33 -07001752 * We expand the rectangle by a half pixel in screen space on all sides. However, instead of using
1753 * a fragment shader to compute the translucency of the color from its position, we simply use a
1754 * varying parameter to define how far a given pixel is into the region.
Chet Haase858aa932011-05-12 09:06:00 -07001755 */
1756void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001757 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001758 float inverseScaleX = 1.0f;
1759 float inverseScaleY = 1.0f;
Romain Guy04299382012-07-18 17:15:41 -07001760
Chet Haase858aa932011-05-12 09:06:00 -07001761 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001762 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001763 Matrix4 *mat = mSnapshot->transform;
1764 float m00 = mat->data[Matrix4::kScaleX];
1765 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase858aa932011-05-12 09:06:00 -07001766 float m10 = mat->data[Matrix4::kSkewX];
1767 float m11 = mat->data[Matrix4::kScaleX];
Chet Haase858aa932011-05-12 09:06:00 -07001768 float scaleX = sqrt(m00 * m00 + m01 * m01);
1769 float scaleY = sqrt(m10 * m10 + m11 * m11);
1770 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1771 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1772 }
1773
Chet Haase858aa932011-05-12 09:06:00 -07001774 float boundarySizeX = .5 * inverseScaleX;
1775 float boundarySizeY = .5 * inverseScaleY;
1776
Chris Craik6ebdc112012-08-31 18:24:33 -07001777 float innerLeft = left + boundarySizeX;
1778 float innerRight = right - boundarySizeX;
1779 float innerTop = top + boundarySizeY;
1780 float innerBottom = bottom - boundarySizeY;
1781
Chet Haase858aa932011-05-12 09:06:00 -07001782 // Adjust the rect by the AA boundary padding
1783 left -= boundarySizeX;
1784 right += boundarySizeX;
1785 top -= boundarySizeY;
1786 bottom += boundarySizeY;
1787
Chet Haase858aa932011-05-12 09:06:00 -07001788 if (!quickReject(left, top, right, bottom)) {
Romain Guy04299382012-07-18 17:15:41 -07001789 setupDraw();
1790 setupDrawNoTexture();
Chris Craik6ebdc112012-08-31 18:24:33 -07001791 setupDrawAARect();
Romain Guy04299382012-07-18 17:15:41 -07001792 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1793 setupDrawColorFilter();
1794 setupDrawShader();
1795 setupDrawBlending(true, mode);
1796 setupDrawProgram();
1797 setupDrawModelViewIdentity(true);
1798 setupDrawColorUniforms();
1799 setupDrawColorFilterUniforms();
1800 setupDrawShaderIdentityUniforms();
1801
Chris Craik6ebdc112012-08-31 18:24:33 -07001802 AlphaVertex rects[14];
1803 AlphaVertex* aVertices = &rects[0];
1804 void* alphaCoords = ((GLbyte*) aVertices) + gVertexAlphaOffset;
Romain Guy04299382012-07-18 17:15:41 -07001805
Chris Craik6ebdc112012-08-31 18:24:33 -07001806 bool force = mCaches.unbindMeshBuffer();
1807 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1808 aVertices, gAlphaVertexStride);
1809 mCaches.resetTexCoordsVertexPointer();
1810 mCaches.unbindIndicesBuffer();
Romain Guy04299382012-07-18 17:15:41 -07001811
Chris Craik6ebdc112012-08-31 18:24:33 -07001812 int alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
1813 glEnableVertexAttribArray(alphaSlot);
1814 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Romain Guy04299382012-07-18 17:15:41 -07001815
Chris Craik6ebdc112012-08-31 18:24:33 -07001816 // draw left
1817 AlphaVertex::set(aVertices++, left, bottom, 0);
1818 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1819 AlphaVertex::set(aVertices++, left, top, 0);
1820 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001821
Chris Craik6ebdc112012-08-31 18:24:33 -07001822 // draw top
1823 AlphaVertex::set(aVertices++, right, top, 0);
1824 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001825
Chris Craik6ebdc112012-08-31 18:24:33 -07001826 // draw right
1827 AlphaVertex::set(aVertices++, right, bottom, 0);
1828 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1829
1830 // draw bottom
1831 AlphaVertex::set(aVertices++, left, bottom, 0);
1832 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1833
1834 // draw inner rect (repeating last vertex to create degenerate bridge triangles)
1835 // TODO: also consider drawing the inner rect without the blending-forced shader, if
1836 // blending is expensive. Note: can't use drawColorRect() since it doesn't use vertex
1837 // buffers like below, resulting in slightly different transformed coordinates.
1838 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1839 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
1840 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1841 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
1842
Chet Haase858aa932011-05-12 09:06:00 -07001843 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guy7b631422012-04-04 11:38:54 -07001844
Chris Craik6ebdc112012-08-31 18:24:33 -07001845 glDrawArrays(GL_TRIANGLE_STRIP, 0, 14);
Romain Guy04299382012-07-18 17:15:41 -07001846
Chris Craik6ebdc112012-08-31 18:24:33 -07001847 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07001848 }
Chet Haase858aa932011-05-12 09:06:00 -07001849}
1850
1851/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001852 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1853 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1854 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1855 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1856 * of the line. Hairlines are more involved because we need to account for transform scaling
1857 * to end up with a one-pixel-wide line in screen space..
1858 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1859 * in combination with values that we calculate and pass down in this method. The basic approach
1860 * is that the quad we create contains both the core line area plus a bounding area in which
1861 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1862 * proportion of the width and the length of a given segment is represented by the boundary
1863 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1864 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1865 * on the inside). This ends up giving the result we want, with pixels that are completely
1866 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1867 * how far into the boundary region they are, which is determined by shader interpolation.
1868 */
Chet Haase48659092012-05-31 15:21:51 -07001869status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1870 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07001871
1872 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001873 // We use half the stroke width here because we're going to position the quad
1874 // corner vertices half of the width away from the line endpoints
1875 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001876 // A stroke width of 0 has a special meaning in Skia:
1877 // it draws a line 1 px wide regardless of current transform
1878 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001879
Chet Haase99ecdc42011-05-06 12:06:34 -07001880 float inverseScaleX = 1.0f;
1881 float inverseScaleY = 1.0f;
1882 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001883
Chet Haase8a5cc922011-04-26 07:28:09 -07001884 int alpha;
1885 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001886
Chet Haase8a5cc922011-04-26 07:28:09 -07001887 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001888 int verticesCount = count;
1889 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001890 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001891 verticesCount += (count - 4);
1892 }
1893
1894 if (isHairLine || isAA) {
1895 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1896 // the line on the screen should always be one pixel wide regardless of scale. For
1897 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001898 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001899 Matrix4 *mat = mSnapshot->transform;
1900 float m00 = mat->data[Matrix4::kScaleX];
1901 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07001902 float m10 = mat->data[Matrix4::kSkewX];
1903 float m11 = mat->data[Matrix4::kScaleX];
Romain Guy7b631422012-04-04 11:38:54 -07001904
Romain Guy211370f2012-02-01 16:10:55 -08001905 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1906 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001907
Chet Haase99ecdc42011-05-06 12:06:34 -07001908 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1909 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001910
Chet Haase99ecdc42011-05-06 12:06:34 -07001911 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1912 scaled = true;
1913 }
1914 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001915 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001916
1917 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07001918
1919 mCaches.enableScissor();
1920
Chet Haase8a5cc922011-04-26 07:28:09 -07001921 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001922 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001923 if (isAA) {
1924 setupDrawAALine();
1925 }
1926 setupDrawColor(paint->getColor(), alpha);
1927 setupDrawColorFilter();
1928 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001929 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001930 setupDrawProgram();
1931 setupDrawModelViewIdentity(true);
1932 setupDrawColorUniforms();
1933 setupDrawColorFilterUniforms();
1934 setupDrawShaderIdentityUniforms();
1935
1936 if (isHairLine) {
1937 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001938 halfStrokeWidth = isAA? 1 : .5;
1939 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001940 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001941 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001942 }
Romain Guy7b631422012-04-04 11:38:54 -07001943
1944 int widthSlot;
1945 int lengthSlot;
1946
Chet Haase5b0200b2011-04-13 17:58:08 -07001947 Vertex lines[verticesCount];
1948 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001949
Chet Haase99585ad2011-05-02 15:00:16 -07001950 AAVertex wLines[verticesCount];
1951 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001952
Romain Guy211370f2012-02-01 16:10:55 -08001953 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001954 setupDrawVertices(vertices);
1955 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001956 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1957 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001958 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001959 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1960 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001961 // We will need to calculate the actual width proportion on each segment for
1962 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1963 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001964 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1965 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001966 }
1967
Chet Haase99ecdc42011-05-06 12:06:34 -07001968 AAVertex* prevAAVertex = NULL;
1969 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001970
Chet Haase99585ad2011-05-02 15:00:16 -07001971 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001972 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001973
Chet Haase5b0200b2011-04-13 17:58:08 -07001974 for (int i = 0; i < count; i += 4) {
1975 // a = start point, b = end point
1976 vec2 a(points[i], points[i + 1]);
1977 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001978
Chet Haase99585ad2011-05-02 15:00:16 -07001979 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001980 float boundaryLengthProportion = 0;
1981 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001982
Chet Haase5b0200b2011-04-13 17:58:08 -07001983 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001984 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001985 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001986 if (isAA) {
1987 float wideningFactor;
1988 if (fabs(n.x) >= fabs(n.y)) {
1989 wideningFactor = fabs(1.0f / n.x);
1990 } else {
1991 wideningFactor = fabs(1.0f / n.y);
1992 }
1993 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001994 }
Romain Guy7b631422012-04-04 11:38:54 -07001995
Chet Haase99ecdc42011-05-06 12:06:34 -07001996 if (scaled) {
1997 n.x *= inverseScaleX;
1998 n.y *= inverseScaleY;
1999 }
2000 } else if (scaled) {
2001 // Extend n by .5 pixel on each side, post-transform
2002 vec2 extendedN = n.copyNormalized();
2003 extendedN /= 2;
2004 extendedN.x *= inverseScaleX;
2005 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002006
Chet Haase99ecdc42011-05-06 12:06:34 -07002007 float extendedNLength = extendedN.length();
2008 // We need to set this value on the shader prior to drawing
2009 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
2010 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002011 }
Romain Guy7b631422012-04-04 11:38:54 -07002012
Chet Haase5b0200b2011-04-13 17:58:08 -07002013 float x = n.x;
2014 n.x = -n.y;
2015 n.y = x;
2016
Chet Haase99585ad2011-05-02 15:00:16 -07002017 // aa lines expand the endpoint vertices to encompass the AA boundary
2018 if (isAA) {
2019 vec2 abVector = (b - a);
2020 length = abVector.length();
2021 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002022
Chet Haase99ecdc42011-05-06 12:06:34 -07002023 if (scaled) {
2024 abVector.x *= inverseScaleX;
2025 abVector.y *= inverseScaleY;
2026 float abLength = abVector.length();
2027 boundaryLengthProportion = abLength / (length + abLength);
2028 } else {
2029 boundaryLengthProportion = .5 / (length + 1);
2030 }
Romain Guy7b631422012-04-04 11:38:54 -07002031
Chet Haase99ecdc42011-05-06 12:06:34 -07002032 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002033 a -= abVector;
2034 b += abVector;
2035 }
2036
Chet Haase5b0200b2011-04-13 17:58:08 -07002037 // Four corners of the rectangle defining a thick line
2038 vec2 p1 = a - n;
2039 vec2 p2 = a + n;
2040 vec2 p3 = b + n;
2041 vec2 p4 = b - n;
2042
Chet Haase99585ad2011-05-02 15:00:16 -07002043
Chet Haase5b0200b2011-04-13 17:58:08 -07002044 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2045 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2046 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2047 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2048
Romain Guy04299382012-07-18 17:15:41 -07002049 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002050 if (!isAA) {
2051 if (prevVertex != NULL) {
2052 // Issue two repeat vertices to create degenerate triangles to bridge
2053 // between the previous line and the new one. This is necessary because
2054 // we are creating a single triangle_strip which will contain
2055 // potentially discontinuous line segments.
2056 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2057 Vertex::set(vertices++, p1.x, p1.y);
2058 generatedVerticesCount += 2;
2059 }
Romain Guy7b631422012-04-04 11:38:54 -07002060
Chet Haase5b0200b2011-04-13 17:58:08 -07002061 Vertex::set(vertices++, p1.x, p1.y);
2062 Vertex::set(vertices++, p2.x, p2.y);
2063 Vertex::set(vertices++, p4.x, p4.y);
2064 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002065
Chet Haase5b0200b2011-04-13 17:58:08 -07002066 prevVertex = vertices - 1;
2067 generatedVerticesCount += 4;
2068 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002069 if (!isHairLine && scaled) {
2070 // Must set width proportions per-segment for scaled non-hairlines to use the
2071 // correct AA boundary dimensions
2072 if (boundaryWidthSlot < 0) {
2073 boundaryWidthSlot =
2074 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002075 }
Romain Guy7b631422012-04-04 11:38:54 -07002076
Chet Haase99ecdc42011-05-06 12:06:34 -07002077 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002078 }
Romain Guy7b631422012-04-04 11:38:54 -07002079
Chet Haase99585ad2011-05-02 15:00:16 -07002080 if (boundaryLengthSlot < 0) {
2081 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002082 }
Romain Guy7b631422012-04-04 11:38:54 -07002083
Chet Haase99ecdc42011-05-06 12:06:34 -07002084 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002085
Chet Haase5b0200b2011-04-13 17:58:08 -07002086 if (prevAAVertex != NULL) {
2087 // Issue two repeat vertices to create degenerate triangles to bridge
2088 // between the previous line and the new one. This is necessary because
2089 // we are creating a single triangle_strip which will contain
2090 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002091 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2092 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2093 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002094 generatedVerticesCount += 2;
2095 }
Romain Guy7b631422012-04-04 11:38:54 -07002096
Chet Haase99585ad2011-05-02 15:00:16 -07002097 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2098 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2099 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2100 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002101
Chet Haase5b0200b2011-04-13 17:58:08 -07002102 prevAAVertex = aaVertices - 1;
2103 generatedVerticesCount += 4;
2104 }
Romain Guy7b631422012-04-04 11:38:54 -07002105
Chet Haase99585ad2011-05-02 15:00:16 -07002106 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2107 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2108 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002109 }
2110 }
Romain Guy7b631422012-04-04 11:38:54 -07002111
Chet Haase5b0200b2011-04-13 17:58:08 -07002112 if (generatedVerticesCount > 0) {
2113 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2114 }
Romain Guy7b631422012-04-04 11:38:54 -07002115
2116 if (isAA) {
2117 finishDrawAALine(widthSlot, lengthSlot);
2118 }
Chet Haase48659092012-05-31 15:21:51 -07002119
2120 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002121}
2122
Chet Haase48659092012-05-31 15:21:51 -07002123status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2124 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002125
2126 // TODO: The paint's cap style defines whether the points are square or circular
2127 // TODO: Handle AA for round points
2128
Chet Haase5b0200b2011-04-13 17:58:08 -07002129 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002130 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002131 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002132 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002133 if (isHairLine) {
2134 // Now that we know it's hairline, we can set the effective width, to be used later
2135 strokeWidth = 1.0f;
2136 }
2137 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002138 int alpha;
2139 SkXfermode::Mode mode;
2140 getAlphaAndMode(paint, &alpha, &mode);
2141
2142 int verticesCount = count >> 1;
2143 int generatedVerticesCount = 0;
2144
2145 TextureVertex pointsData[verticesCount];
2146 TextureVertex* vertex = &pointsData[0];
2147
Romain Guy04299382012-07-18 17:15:41 -07002148 // TODO: We should optimize this method to not generate vertices for points
2149 // that lie outside of the clip.
2150 mCaches.enableScissor();
2151
Romain Guyed6fcb02011-03-21 13:11:28 -07002152 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002153 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002154 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002155 setupDrawColor(paint->getColor(), alpha);
2156 setupDrawColorFilter();
2157 setupDrawShader();
2158 setupDrawBlending(mode);
2159 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002160 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002161 setupDrawColorUniforms();
2162 setupDrawColorFilterUniforms();
2163 setupDrawPointUniforms();
2164 setupDrawShaderIdentityUniforms();
2165 setupDrawMesh(vertex);
2166
2167 for (int i = 0; i < count; i += 2) {
2168 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2169 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002170
Chet Haase8a5cc922011-04-26 07:28:09 -07002171 float left = points[i] - halfWidth;
2172 float right = points[i] + halfWidth;
2173 float top = points[i + 1] - halfWidth;
2174 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002175
Chet Haase8a5cc922011-04-26 07:28:09 -07002176 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002177 }
2178
2179 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002180
2181 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002182}
2183
Chet Haase48659092012-05-31 15:21:51 -07002184status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002185 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002186 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002187
Romain Guyae88e5e2010-10-22 17:49:18 -07002188 Rect& clip(*mSnapshot->clipRect);
2189 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002190
Romain Guy3d58c032010-07-14 16:34:53 -07002191 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002192
2193 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002194}
Romain Guy9d5316e2010-06-24 19:30:36 -07002195
Chet Haase48659092012-05-31 15:21:51 -07002196status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2197 SkPaint* paint) {
2198 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002199 const AutoTexture autoCleanup(texture);
2200
2201 const float x = left + texture->left - texture->offset;
2202 const float y = top + texture->top - texture->offset;
2203
2204 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002205
2206 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002207}
2208
Chet Haase48659092012-05-31 15:21:51 -07002209status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002210 float rx, float ry, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002211 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002212
Romain Guya1d3c912011-12-13 14:55:06 -08002213 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002214 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2215 right - left, bottom - top, rx, ry, paint);
Chet Haase48659092012-05-31 15:21:51 -07002216 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002217}
2218
Chet Haase48659092012-05-31 15:21:51 -07002219status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2220 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002221
Romain Guya1d3c912011-12-13 14:55:06 -08002222 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002223 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Chet Haase48659092012-05-31 15:21:51 -07002224 return drawShape(x - radius, y - radius, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002225}
Romain Guy01d58e42011-01-19 21:54:02 -08002226
Chet Haase48659092012-05-31 15:21:51 -07002227status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
2228 SkPaint* paint) {
2229 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002230
Romain Guya1d3c912011-12-13 14:55:06 -08002231 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002232 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, 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::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002237 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002238 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002239
2240 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002241 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002242 }
2243
Romain Guya1d3c912011-12-13 14:55:06 -08002244 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002245 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2246 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002247 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002248}
2249
Chet Haase48659092012-05-31 15:21:51 -07002250status_t OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002251 SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002252 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002253
Romain Guya1d3c912011-12-13 14:55:06 -08002254 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002255 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002256 return drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002257}
2258
Chet Haase48659092012-05-31 15:21:51 -07002259status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002260 if (p->getStyle() != SkPaint::kFill_Style) {
Chet Haase48659092012-05-31 15:21:51 -07002261 return drawRectAsShape(left, top, right, bottom, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002262 }
2263
Romain Guy6926c722010-07-12 20:20:03 -07002264 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002265 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002266 }
2267
Romain Guy026c5e162010-06-28 17:12:22 -07002268 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002269 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002270 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2271 if (!isMode) {
2272 // Assume SRC_OVER
2273 mode = SkXfermode::kSrcOver_Mode;
2274 }
2275 } else {
2276 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002277 }
2278
Romain Guy026c5e162010-06-28 17:12:22 -07002279 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002280 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002281 drawAARect(left, top, right, bottom, color, mode);
2282 } else {
2283 drawColorRect(left, top, right, bottom, color, mode);
2284 }
Chet Haase48659092012-05-31 15:21:51 -07002285
2286 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002287}
Romain Guy9d5316e2010-06-24 19:30:36 -07002288
Raph Levien416a8472012-07-19 22:48:17 -07002289void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2290 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2291 float x, float y) {
2292 mCaches.activeTexture(0);
2293
2294 // NOTE: The drop shadow will not perform gamma correction
2295 // if shader-based correction is enabled
2296 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2297 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2298 paint, text, bytesCount, count, mShadowRadius, positions);
2299 const AutoTexture autoCleanup(shadow);
2300
2301 const float sx = x - shadow->left + mShadowDx;
2302 const float sy = y - shadow->top + mShadowDy;
2303
2304 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2305 int shadowColor = mShadowColor;
2306 if (mShader) {
2307 shadowColor = 0xffffffff;
2308 }
2309
2310 setupDraw();
2311 setupDrawWithTexture(true);
2312 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2313 setupDrawColorFilter();
2314 setupDrawShader();
2315 setupDrawBlending(true, mode);
2316 setupDrawProgram();
2317 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2318 setupDrawTexture(shadow->id);
2319 setupDrawPureColorUniforms();
2320 setupDrawColorFilterUniforms();
2321 setupDrawShaderUniforms();
2322 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2323
2324 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2325}
2326
Chet Haase48659092012-05-31 15:21:51 -07002327status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002328 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002329 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002330 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002331 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002332 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002333
Romain Guy671d6cf2012-01-18 12:39:17 -08002334 // NOTE: Skia does not support perspective transform on drawPosText yet
2335 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002336 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002337 }
2338
2339 float x = 0.0f;
2340 float y = 0.0f;
2341 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2342 if (pureTranslate) {
2343 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2344 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2345 }
2346
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002347 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002348 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2349 paint->getTextSize());
2350
2351 int alpha;
2352 SkXfermode::Mode mode;
2353 getAlphaAndMode(paint, &alpha, &mode);
2354
Raph Levien416a8472012-07-19 22:48:17 -07002355 if (CC_UNLIKELY(mHasShadow)) {
2356 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2357 0.0f, 0.0f);
2358 }
2359
Romain Guy671d6cf2012-01-18 12:39:17 -08002360 // Pick the appropriate texture filtering
2361 bool linearFilter = mSnapshot->transform->changesBounds();
2362 if (pureTranslate && !linearFilter) {
2363 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2364 }
2365
2366 mCaches.activeTexture(0);
2367 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002368 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002369 setupDrawDirtyRegionsDisabled();
2370 setupDrawWithTexture(true);
2371 setupDrawAlpha8Color(paint->getColor(), alpha);
2372 setupDrawColorFilter();
2373 setupDrawShader();
2374 setupDrawBlending(true, mode);
2375 setupDrawProgram();
2376 setupDrawModelView(x, y, x, y, pureTranslate, true);
2377 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2378 setupDrawPureColorUniforms();
2379 setupDrawColorFilterUniforms();
2380 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002381 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002382
2383 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2384 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2385
Romain Guy211370f2012-02-01 16:10:55 -08002386 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002387
2388 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2389 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002390 if (hasActiveLayer) {
2391 if (!pureTranslate) {
2392 mSnapshot->transform->mapRect(bounds);
2393 }
2394 dirtyLayerUnchecked(bounds, getRegion());
2395 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002396 }
Chet Haase48659092012-05-31 15:21:51 -07002397
2398 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002399}
2400
Romain Guyc2525952012-07-27 16:41:22 -07002401status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002402 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002403 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002404 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002405 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002406 }
2407
Chet Haasea1cff502012-02-21 13:43:44 -08002408 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002409 switch (paint->getTextAlign()) {
2410 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002411 x -= length / 2.0f;
2412 break;
2413 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002414 x -= length;
2415 break;
2416 default:
2417 break;
2418 }
2419
Romain Guycac5fd32011-12-01 20:08:50 -08002420 SkPaint::FontMetrics metrics;
2421 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002422 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002423 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002424 }
2425
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002426 const float oldX = x;
2427 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002428 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002429 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002430 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2431 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2432 }
2433
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002434#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002435 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002436 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002437#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002438
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002439 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002440 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002441 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002442
Romain Guy86568192010-12-14 15:55:39 -08002443 int alpha;
2444 SkXfermode::Mode mode;
2445 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002446
Romain Guy211370f2012-02-01 16:10:55 -08002447 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002448 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2449 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002450 }
2451
Romain Guy6620c6d2010-12-06 18:07:02 -08002452 // Pick the appropriate texture filtering
2453 bool linearFilter = mSnapshot->transform->changesBounds();
2454 if (pureTranslate && !linearFilter) {
2455 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2456 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002457
Romain Guy16c88082012-06-11 16:03:47 -07002458 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002459 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002460 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002461 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002462 setupDrawDirtyRegionsDisabled();
2463 setupDrawWithTexture(true);
2464 setupDrawAlpha8Color(paint->getColor(), alpha);
2465 setupDrawColorFilter();
2466 setupDrawShader();
2467 setupDrawBlending(true, mode);
2468 setupDrawProgram();
2469 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002470 // See comment above; the font renderer must use texture unit 0
2471 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002472 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2473 setupDrawPureColorUniforms();
2474 setupDrawColorFilterUniforms();
2475 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002476 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002477
Romain Guy6620c6d2010-12-06 18:07:02 -08002478 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002479 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2480
Romain Guy211370f2012-02-01 16:10:55 -08002481 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002482
Raph Levien996e57c2012-07-23 15:22:52 -07002483 bool status;
Raph Levien8b4072d2012-07-30 15:50:00 -07002484 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
2485 SkPaint paintCopy(*paint);
2486 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2487 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Raph Levien996e57c2012-07-23 15:22:52 -07002488 positions, hasActiveLayer ? &bounds : NULL);
2489 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002490 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2491 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002492 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002493
2494 if (status && hasActiveLayer) {
2495 if (!pureTranslate) {
2496 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002497 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002498 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002499 }
Romain Guy694b5192010-07-21 21:33:20 -07002500
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002501 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002502
2503 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002504}
2505
Chet Haase48659092012-05-31 15:21:51 -07002506status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002507 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002508 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2509 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002510 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002511 }
2512
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002513 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002514 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2515 paint->getTextSize());
2516
2517 int alpha;
2518 SkXfermode::Mode mode;
2519 getAlphaAndMode(paint, &alpha, &mode);
2520
2521 mCaches.activeTexture(0);
2522 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002523 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002524 setupDrawDirtyRegionsDisabled();
2525 setupDrawWithTexture(true);
2526 setupDrawAlpha8Color(paint->getColor(), alpha);
2527 setupDrawColorFilter();
2528 setupDrawShader();
2529 setupDrawBlending(true, mode);
2530 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002531 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002532 setupDrawTexture(fontRenderer.getTexture(true));
2533 setupDrawPureColorUniforms();
2534 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002535 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002536 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002537
Romain Guy97771732012-02-28 18:17:02 -08002538 const Rect* clip = &mSnapshot->getLocalClip();
2539 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 -08002540
Romain Guy97771732012-02-28 18:17:02 -08002541 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002542
2543 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2544 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002545 if (hasActiveLayer) {
2546 mSnapshot->transform->mapRect(bounds);
2547 dirtyLayerUnchecked(bounds, getRegion());
2548 }
Romain Guy97771732012-02-28 18:17:02 -08002549 }
Chet Haase48659092012-05-31 15:21:51 -07002550
2551 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002552}
2553
Chet Haase48659092012-05-31 15:21:51 -07002554status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2555 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002556
Romain Guya1d3c912011-12-13 14:55:06 -08002557 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002558
Romain Guy33f6beb2012-02-16 19:24:51 -08002559 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002560 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002561 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002562 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002563
Romain Guy8b55f372010-08-18 17:10:07 -07002564 const float x = texture->left - texture->offset;
2565 const float y = texture->top - texture->offset;
2566
Romain Guy01d58e42011-01-19 21:54:02 -08002567 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002568
2569 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002570}
2571
Chet Haase48659092012-05-31 15:21:51 -07002572status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guyada830f2011-01-13 12:13:20 -08002573 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Chet Haase48659092012-05-31 15:21:51 -07002574 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002575 }
2576
Romain Guy4ff0cf42012-08-06 14:51:10 -07002577 bool debugLayerUpdate = false;
2578
Romain Guy2bf68f02012-03-02 13:37:47 -08002579 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2580 OpenGLRenderer* renderer = layer->renderer;
2581 Rect& dirty = layer->dirtyRect;
2582
2583 interrupt();
2584 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2585 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002586 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002587 renderer->finish();
2588 resume();
2589
2590 dirty.setEmpty();
2591 layer->deferredUpdateScheduled = false;
2592 layer->renderer = NULL;
2593 layer->displayList = NULL;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002594
2595 debugLayerUpdate = mCaches.debugLayersUpdates;
Romain Guy2bf68f02012-03-02 13:37:47 -08002596 }
2597
Romain Guya1d3c912011-12-13 14:55:06 -08002598 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002599
2600 int alpha;
2601 SkXfermode::Mode mode;
2602 getAlphaAndMode(paint, &alpha, &mode);
2603
Romain Guy9ace8f52011-07-07 20:50:11 -07002604 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002605
Romain Guy211370f2012-02-01 16:10:55 -08002606 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002607 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002608 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002609 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002610 const float a = alpha / 255.0f;
Romain Guyf219da52011-01-16 12:54:25 -08002611
Romain Guyc88e3572011-01-22 00:32:12 -08002612 setupDraw();
2613 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002614 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002615 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002616 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002617 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002618 setupDrawPureColorUniforms();
2619 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002620 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002621 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002622 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2623 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002624
Romain Guyd21b6e12011-11-30 20:21:23 -08002625 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002626 setupDrawModelViewTranslate(tx, ty,
2627 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002628 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002629 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002630 setupDrawModelViewTranslate(x, y,
2631 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2632 }
Romain Guyc88e3572011-01-22 00:32:12 -08002633 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002634
Romain Guyc88e3572011-01-22 00:32:12 -08002635 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2636 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002637
Romain Guyc88e3572011-01-22 00:32:12 -08002638 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002639
2640#if DEBUG_LAYERS_AS_REGIONS
2641 drawRegionRects(layer->region);
2642#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002643 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002644
2645 if (debugLayerUpdate) {
2646 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2647 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2648 }
Romain Guyf219da52011-01-16 12:54:25 -08002649 }
Chet Haase48659092012-05-31 15:21:51 -07002650
2651 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002652}
2653
Romain Guy6926c722010-07-12 20:20:03 -07002654///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002655// Shaders
2656///////////////////////////////////////////////////////////////////////////////
2657
2658void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002659 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002660}
2661
Romain Guy06f96e22010-07-30 19:18:16 -07002662void OpenGLRenderer::setupShader(SkiaShader* shader) {
2663 mShader = shader;
2664 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002665 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002666 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002667}
2668
Romain Guyd27977d2010-07-14 19:18:51 -07002669///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002670// Color filters
2671///////////////////////////////////////////////////////////////////////////////
2672
2673void OpenGLRenderer::resetColorFilter() {
2674 mColorFilter = NULL;
2675}
2676
2677void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2678 mColorFilter = filter;
2679}
2680
2681///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002682// Drop shadow
2683///////////////////////////////////////////////////////////////////////////////
2684
2685void OpenGLRenderer::resetShadow() {
2686 mHasShadow = false;
2687}
2688
2689void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2690 mHasShadow = true;
2691 mShadowRadius = radius;
2692 mShadowDx = dx;
2693 mShadowDy = dy;
2694 mShadowColor = color;
2695}
2696
2697///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002698// Draw filters
2699///////////////////////////////////////////////////////////////////////////////
2700
2701void OpenGLRenderer::resetPaintFilter() {
2702 mHasDrawFilter = false;
2703}
2704
2705void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2706 mHasDrawFilter = true;
2707 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2708 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2709}
2710
2711SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002712 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002713
2714 uint32_t flags = paint->getFlags();
2715
2716 mFilteredPaint = *paint;
2717 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2718
2719 return &mFilteredPaint;
2720}
2721
2722///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002723// Drawing implementation
2724///////////////////////////////////////////////////////////////////////////////
2725
Romain Guy01d58e42011-01-19 21:54:02 -08002726void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2727 float x, float y, SkPaint* paint) {
2728 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2729 return;
2730 }
2731
2732 int alpha;
2733 SkXfermode::Mode mode;
2734 getAlphaAndMode(paint, &alpha, &mode);
2735
2736 setupDraw();
2737 setupDrawWithTexture(true);
2738 setupDrawAlpha8Color(paint->getColor(), alpha);
2739 setupDrawColorFilter();
2740 setupDrawShader();
2741 setupDrawBlending(true, mode);
2742 setupDrawProgram();
2743 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2744 setupDrawTexture(texture->id);
2745 setupDrawPureColorUniforms();
2746 setupDrawColorFilterUniforms();
2747 setupDrawShaderUniforms();
2748 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2749
2750 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2751
2752 finishDrawTexture();
2753}
2754
Romain Guyf607bdc2010-09-10 19:20:06 -07002755// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002756#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2757#define kStdUnderline_Offset (1.0f / 9.0f)
2758#define kStdUnderline_Thickness (1.0f / 18.0f)
2759
2760void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2761 float x, float y, SkPaint* paint) {
2762 // Handle underline and strike-through
2763 uint32_t flags = paint->getFlags();
2764 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002765 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002766 float underlineWidth = length;
2767 // If length is > 0.0f, we already measured the text for the text alignment
2768 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002769 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002770 }
2771
Romain Guy211370f2012-02-01 16:10:55 -08002772 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002773 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002774 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002775
Raph Levien8b4072d2012-07-30 15:50:00 -07002776 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002777 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002778
Romain Guyf6834472011-01-23 13:32:12 -08002779 int linesCount = 0;
2780 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2781 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2782
2783 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002784 float points[pointsCount];
2785 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002786
2787 if (flags & SkPaint::kUnderlineText_Flag) {
2788 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002789 points[currentPoint++] = left;
2790 points[currentPoint++] = top;
2791 points[currentPoint++] = left + underlineWidth;
2792 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002793 }
2794
2795 if (flags & SkPaint::kStrikeThruText_Flag) {
2796 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002797 points[currentPoint++] = left;
2798 points[currentPoint++] = top;
2799 points[currentPoint++] = left + underlineWidth;
2800 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002801 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002802
Romain Guy726aeba2011-06-01 14:52:00 -07002803 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002804
Romain Guy726aeba2011-06-01 14:52:00 -07002805 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002806 }
2807 }
2808}
2809
Romain Guy026c5e162010-06-28 17:12:22 -07002810void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002811 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002812 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002813 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002814 color |= 0x00ffffff;
2815 }
2816
Romain Guy70ca14e2010-12-13 18:24:33 -08002817 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002818 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002819 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002820 setupDrawShader();
2821 setupDrawColorFilter();
2822 setupDrawBlending(mode);
2823 setupDrawProgram();
2824 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2825 setupDrawColorUniforms();
2826 setupDrawShaderUniforms(ignoreTransform);
2827 setupDrawColorFilterUniforms();
2828 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002829
Romain Guyc95c8d62010-09-17 15:31:32 -07002830 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2831}
2832
Romain Guy82ba8142010-07-09 13:25:56 -07002833void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002834 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002835 int alpha;
2836 SkXfermode::Mode mode;
2837 getAlphaAndMode(paint, &alpha, &mode);
2838
Romain Guyd21b6e12011-11-30 20:21:23 -08002839 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002840
Romain Guy211370f2012-02-01 16:10:55 -08002841 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002842 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2843 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2844
Romain Guyd21b6e12011-11-30 20:21:23 -08002845 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002846 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2847 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2848 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2849 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002850 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002851 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2852 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2853 GL_TRIANGLE_STRIP, gMeshCount);
2854 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002855}
2856
Romain Guybd6b79b2010-06-26 00:13:53 -07002857void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002858 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2859 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002860 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002861}
2862
2863void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002864 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002865 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002866 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002867
Romain Guy746b7402010-10-26 16:27:31 -07002868 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002869 setupDrawWithTexture();
2870 setupDrawColor(alpha, alpha, alpha, alpha);
2871 setupDrawColorFilter();
2872 setupDrawBlending(blend, mode, swapSrcDst);
2873 setupDrawProgram();
2874 if (!dirty) {
2875 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002876 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002877 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002878 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002879 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002880 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002881 }
Romain Guy86568192010-12-14 15:55:39 -08002882 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002883 setupDrawColorFilterUniforms();
2884 setupDrawTexture(texture);
2885 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002886
Romain Guy6820ac82010-09-15 18:11:50 -07002887 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002888
2889 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002890}
2891
Romain Guya5aed0d2010-09-09 14:42:43 -07002892void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002893 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002894 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002895
Romain Guy82ba8142010-07-09 13:25:56 -07002896 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002897 // These blend modes are not supported by OpenGL directly and have
2898 // to be implemented using shaders. Since the shader will perform
2899 // the blending, turn blending off here
2900 // If the blend mode cannot be implemented using shaders, fall
2901 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07002902 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08002903 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002904 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002905 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002906
Romain Guy82bc7a72012-01-03 14:13:39 -08002907 if (mCaches.blend) {
2908 glDisable(GL_BLEND);
2909 mCaches.blend = false;
2910 }
2911
2912 return;
2913 } else {
2914 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002915 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002916 }
2917
2918 if (!mCaches.blend) {
2919 glEnable(GL_BLEND);
2920 }
2921
2922 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2923 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2924
2925 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2926 glBlendFunc(sourceMode, destMode);
2927 mCaches.lastSrcMode = sourceMode;
2928 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002929 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002930 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002931 glDisable(GL_BLEND);
2932 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002933 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002934}
2935
Romain Guy889f8d12010-07-29 14:37:42 -07002936bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002937 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002938 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002939 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002940 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002941 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002942 }
Romain Guy6926c722010-07-12 20:20:03 -07002943 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002944}
2945
Romain Guy026c5e162010-06-28 17:12:22 -07002946void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002947 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002948 TextureVertex::setUV(v++, u1, v1);
2949 TextureVertex::setUV(v++, u2, v1);
2950 TextureVertex::setUV(v++, u1, v2);
2951 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002952}
2953
Chet Haase5c13d892010-10-08 08:37:55 -07002954void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002955 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07002956 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002957}
2958
Romain Guy9d5316e2010-06-24 19:30:36 -07002959}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002960}; // namespace android