blob: f4c267538ff031c177c1c86c7dc01492b0ce643d [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy03d58522012-02-24 17:54:07 -080024#include <SkPathMeasure.h>
Romain Guy694b5192010-07-21 21:33:20 -070025#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070026
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070028#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070029
Romain Guy08aa2cb2011-03-17 11:06:57 -070030#include <private/hwui/DrawGlInfo.h>
31
Romain Guy5b3b3522010-10-27 18:57:51 -070032#include <ui/Rect.h>
33
Romain Guy85bf02f2010-06-22 13:11:24 -070034#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Romain Guya957eea2010-12-08 18:34:42 -080036#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070037
38namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070039namespace uirenderer {
40
41///////////////////////////////////////////////////////////////////////////////
42// Defines
43///////////////////////////////////////////////////////////////////////////////
44
Romain Guy759ea802010-09-16 20:49:46 -070045#define RAD_TO_DEG (180.0f / 3.14159265f)
46#define MIN_ANGLE 0.001f
47
Romain Guyf8773082012-07-12 18:01:00 -070048#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070049
Romain Guyd21b6e12011-11-30 20:21:23 -080050#define FILTER(paint) (paint && paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
51
Romain Guy9d5316e2010-06-24 19:30:36 -070052///////////////////////////////////////////////////////////////////////////////
53// Globals
54///////////////////////////////////////////////////////////////////////////////
55
Romain Guy889f8d12010-07-29 14:37:42 -070056/**
57 * Structure mapping Skia xfermodes to OpenGL blending factors.
58 */
59struct Blender {
60 SkXfermode::Mode mode;
61 GLenum src;
62 GLenum dst;
63}; // struct Blender
64
Romain Guy026c5e162010-06-28 17:12:22 -070065// In this array, the index of each Blender equals the value of the first
66// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
67static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070068 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
69 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
70 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
71 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
73 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
74 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
75 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
76 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
77 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
79 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
81 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
82 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070083};
Romain Guye4d01122010-06-16 18:44:05 -070084
Romain Guy87a76572010-09-13 18:11:21 -070085// This array contains the swapped version of each SkXfermode. For instance
86// this array's SrcOver blending mode is actually DstOver. You can refer to
87// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070088static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070089 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
90 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
91 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
92 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
93 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
94 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
95 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
96 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
98 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
99 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
101 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
102 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
103 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700104};
105
Romain Guyf6a11b82010-06-23 17:47:49 -0700106///////////////////////////////////////////////////////////////////////////////
107// Constructors/destructor
108///////////////////////////////////////////////////////////////////////////////
109
Romain Guyfb8b7632010-08-23 21:05:08 -0700110OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700111 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700112 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700113 mHasShadow = false;
Romain Guy5ff9df62012-01-23 17:09:05 -0800114 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700115
Romain Guyac670c02010-07-27 17:39:27 -0700116 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
117
Romain Guyae5575b2010-07-29 18:48:04 -0700118 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700119}
120
Romain Guy85bf02f2010-06-22 13:11:24 -0700121OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700122 // The context has already been destroyed at this point, do not call
123 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700124}
125
Romain Guyf6a11b82010-06-23 17:47:49 -0700126///////////////////////////////////////////////////////////////////////////////
Romain Guy13631f32012-01-30 17:41:55 -0800127// Debug
128///////////////////////////////////////////////////////////////////////////////
129
130void OpenGLRenderer::startMark(const char* name) const {
131 mCaches.startMark(0, name);
132}
133
134void OpenGLRenderer::endMark() const {
135 mCaches.endMark();
136}
137
138///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700139// Setup
140///////////////////////////////////////////////////////////////////////////////
141
Romain Guy49c5fc02012-05-15 11:10:01 -0700142bool OpenGLRenderer::isDeferred() {
143 return false;
144}
145
Romain Guy85bf02f2010-06-22 13:11:24 -0700146void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700147 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700148
149 mWidth = width;
150 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700151
152 mFirstSnapshot->height = height;
153 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700154
Romain Guy3e263fa2011-12-12 16:47:48 -0800155 glDisable(GL_DITHER);
Romain Guy3e263fa2011-12-12 16:47:48 -0800156 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800157
Romain Guy3e263fa2011-12-12 16:47:48 -0800158 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700159}
160
Chet Haase44b2fe32012-06-06 19:03:58 -0700161int OpenGLRenderer::prepare(bool opaque) {
162 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800163}
164
Chet Haase44b2fe32012-06-06 19:03:58 -0700165int OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800166 mCaches.clearGarbage();
167
Romain Guy8aef54f2010-09-01 15:13:49 -0700168 mSnapshot = new Snapshot(mFirstSnapshot,
169 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800170 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700171 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700172
Romain Guy7d7b5492011-01-24 16:33:45 -0800173 mSnapshot->setClip(left, top, right, bottom);
Romain Guyddf74372012-05-22 14:07:07 -0700174 mDirtyClip = opaque;
175
176 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800177
Romain Guy6b7bd242010-10-06 19:49:23 -0700178 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700179 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700180 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700181 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700182 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700183 } else {
184 mCaches.resetScissor();
185 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700186
187 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700188}
189
190void OpenGLRenderer::syncState() {
191 glViewport(0, 0, mWidth, mHeight);
192
193 if (mCaches.blend) {
194 glEnable(GL_BLEND);
195 } else {
196 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700197 }
Romain Guybb9524b2010-06-22 18:56:38 -0700198}
199
Romain Guyb025b9c2010-09-16 14:16:48 -0700200void OpenGLRenderer::finish() {
201#if DEBUG_OPENGL
202 GLenum status = GL_NO_ERROR;
203 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000204 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800205 switch (status) {
Romain Guya44a63a2012-04-26 14:05:02 -0700206 case GL_INVALID_ENUM:
207 ALOGE(" GL_INVALID_ENUM");
208 break;
209 case GL_INVALID_VALUE:
210 ALOGE(" GL_INVALID_VALUE");
211 break;
212 case GL_INVALID_OPERATION:
213 ALOGE(" GL_INVALID_OPERATION");
214 break;
Romain Guya07105b2011-01-10 21:14:18 -0800215 case GL_OUT_OF_MEMORY:
Romain Guya44a63a2012-04-26 14:05:02 -0700216 ALOGE(" Out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800217 break;
218 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700219 }
220#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800221#if DEBUG_MEMORY_USAGE
222 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800223#else
224 if (mCaches.getDebugLevel() & kDebugMemory) {
225 mCaches.dumpMemoryUsage();
226 }
Romain Guyc15008e2010-11-10 11:59:15 -0800227#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700228}
229
Romain Guy6c319ca2011-01-11 14:29:25 -0800230void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700231 if (mCaches.currentProgram) {
232 if (mCaches.currentProgram->isInUse()) {
233 mCaches.currentProgram->remove();
234 mCaches.currentProgram = NULL;
235 }
236 }
Romain Guy50c0f092010-10-19 11:42:22 -0700237 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800238 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800239 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800240 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700241}
242
Romain Guy6c319ca2011-01-11 14:29:25 -0800243void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800244 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
245
246 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800247 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
248
Chet Haase80250612012-08-15 13:46:54 -0700249 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700250 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800251 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700252 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700253
Romain Guya1d3c912011-12-13 14:55:06 -0800254 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800255 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700256
Romain Guy50c0f092010-10-19 11:42:22 -0700257 mCaches.blend = true;
258 glEnable(GL_BLEND);
259 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
260 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700261}
262
Romain Guyba6be8a2012-04-23 18:22:09 -0700263void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700264 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700265}
266
267void OpenGLRenderer::attachFunctor(Functor* functor) {
268 mFunctors.add(functor);
269}
270
Romain Guy8f3b8e32012-03-27 16:33:45 -0700271status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
272 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700273 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700274
Romain Guyba6be8a2012-04-23 18:22:09 -0700275 if (count > 0) {
276 SortedVector<Functor*> functors(mFunctors);
277 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700278
Romain Guyba6be8a2012-04-23 18:22:09 -0700279 DrawGlInfo info;
280 info.clipLeft = 0;
281 info.clipTop = 0;
282 info.clipRight = 0;
283 info.clipBottom = 0;
284 info.isLayer = false;
285 info.width = 0;
286 info.height = 0;
287 memset(info.transform, 0, sizeof(float) * 16);
288
289 for (size_t i = 0; i < count; i++) {
290 Functor* f = functors.itemAt(i);
291 result |= (*f)(DrawGlInfo::kModeProcess, &info);
292
Chris Craikc2c95432012-04-25 15:13:52 -0700293 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700294 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
295 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700296 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700297
Chris Craikc2c95432012-04-25 15:13:52 -0700298 if (result & DrawGlInfo::kStatusInvoke) {
299 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700300 }
301 }
Chet Haasebeb8bd02012-09-09 16:13:26 -0700302 // protect against functors binding to other buffers
303 mCaches.unbindMeshBuffer();
304 mCaches.unbindIndicesBuffer();
305 mCaches.activeTexture(0);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700306 }
307
308 return result;
309}
310
311status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800312 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700313 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700314
Romain Guy8a4ac612012-07-17 17:32:48 -0700315 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800316 if (mDirtyClip) {
317 setScissorFromClip();
318 }
Romain Guyd643bb52011-03-01 14:55:21 -0800319
Romain Guy80911b82011-03-16 15:30:12 -0700320 Rect clip(*mSnapshot->clipRect);
321 clip.snapToPixelBoundaries();
322
Romain Guyd643bb52011-03-01 14:55:21 -0800323 // Since we don't know what the functor will draw, let's dirty
324 // tne entire clip region
325 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800326 dirtyLayerUnchecked(clip, getRegion());
327 }
Romain Guyd643bb52011-03-01 14:55:21 -0800328
Romain Guy08aa2cb2011-03-17 11:06:57 -0700329 DrawGlInfo info;
330 info.clipLeft = clip.left;
331 info.clipTop = clip.top;
332 info.clipRight = clip.right;
333 info.clipBottom = clip.bottom;
334 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700335 info.width = getSnapshot()->viewport.getWidth();
336 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700337 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700338
Chet Haase48659092012-05-31 15:21:51 -0700339 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800340
Romain Guy8f3b8e32012-03-27 16:33:45 -0700341 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700342 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800343 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700344
Chris Craik65924a32012-04-05 17:52:11 -0700345 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700346 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700347 }
Romain Guycabfcc12011-03-07 18:06:46 -0800348 }
349
Chet Haasedaf98e92011-01-10 14:10:36 -0800350 resume();
Romain Guy65549432012-03-26 16:45:05 -0700351 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800352}
353
Romain Guyf6a11b82010-06-23 17:47:49 -0700354///////////////////////////////////////////////////////////////////////////////
355// State management
356///////////////////////////////////////////////////////////////////////////////
357
Romain Guybb9524b2010-06-22 18:56:38 -0700358int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700359 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700360}
361
362int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700363 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700364}
365
366void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700367 if (mSaveCount > 1) {
368 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700369 }
Romain Guybb9524b2010-06-22 18:56:38 -0700370}
371
372void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700373 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700374
Romain Guy8fb95422010-08-17 18:38:51 -0700375 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700376 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700377 }
Romain Guybb9524b2010-06-22 18:56:38 -0700378}
379
Romain Guy8aef54f2010-09-01 15:13:49 -0700380int OpenGLRenderer::saveSnapshot(int flags) {
381 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700382 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700383}
384
385bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700386 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700387 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700388 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700389
Romain Guybd6b79b2010-06-26 00:13:53 -0700390 sp<Snapshot> current = mSnapshot;
391 sp<Snapshot> previous = mSnapshot->previous;
392
Romain Guyeb993562010-10-05 18:14:38 -0700393 if (restoreOrtho) {
394 Rect& r = previous->viewport;
395 glViewport(r.left, r.top, r.right, r.bottom);
396 mOrthoMatrix.load(current->orthoMatrix);
397 }
398
Romain Guy8b55f372010-08-18 17:10:07 -0700399 mSaveCount--;
400 mSnapshot = previous;
401
Romain Guy2542d192010-08-18 11:47:12 -0700402 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700403 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700404 }
Romain Guy2542d192010-08-18 11:47:12 -0700405
Romain Guy5ec99242010-11-03 16:19:08 -0700406 if (restoreLayer) {
407 composeLayer(current, previous);
408 }
409
Romain Guy2542d192010-08-18 11:47:12 -0700410 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700411}
412
Romain Guyf6a11b82010-06-23 17:47:49 -0700413///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700414// Layers
415///////////////////////////////////////////////////////////////////////////////
416
417int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700418 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700419 const GLuint previousFbo = mSnapshot->fbo;
420 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700421
Romain Guyaf636eb2010-12-09 17:47:21 -0800422 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700423 int alpha = 255;
424 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700425
Romain Guye45362c2010-11-03 19:58:32 -0700426 if (p) {
427 alpha = p->getAlpha();
428 if (!mCaches.extensions.hasFramebufferFetch()) {
429 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
430 if (!isMode) {
431 // Assume SRC_OVER
432 mode = SkXfermode::kSrcOver_Mode;
433 }
434 } else {
435 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700436 }
437 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700438 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700439 }
Romain Guyd55a8612010-06-28 17:42:46 -0700440
Chet Haased48885a2012-08-28 17:43:28 -0700441 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700442 }
Romain Guyd55a8612010-06-28 17:42:46 -0700443
444 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700445}
446
447int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
448 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700449 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700450 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700451 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700452 SkPaint paint;
453 paint.setAlpha(alpha);
454 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700455 }
Romain Guyd55a8612010-06-28 17:42:46 -0700456}
Romain Guybd6b79b2010-06-26 00:13:53 -0700457
Romain Guy1c740bc2010-09-13 18:00:09 -0700458/**
459 * Layers are viewed by Skia are slightly different than layers in image editing
460 * programs (for instance.) When a layer is created, previously created layers
461 * and the frame buffer still receive every drawing command. For instance, if a
462 * layer is created and a shape intersecting the bounds of the layers and the
463 * framebuffer is draw, the shape will be drawn on both (unless the layer was
464 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
465 *
466 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
467 * texture. Unfortunately, this is inefficient as it requires every primitive to
468 * be drawn n + 1 times, where n is the number of active layers. In practice this
469 * means, for every primitive:
470 * - Switch active frame buffer
471 * - Change viewport, clip and projection matrix
472 * - Issue the drawing
473 *
474 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700475 * To avoid this, layers are implemented in a different way here, at least in the
476 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
477 * is set. When this flag is set we can redirect all drawing operations into a
478 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700479 *
480 * This implementation relies on the frame buffer being at least RGBA 8888. When
481 * a layer is created, only a texture is created, not an FBO. The content of the
482 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700483 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700484 * buffer and drawing continues as normal. This technique therefore treats the
485 * frame buffer as a scratch buffer for the layers.
486 *
487 * To compose the layers back onto the frame buffer, each layer texture
488 * (containing the original frame buffer data) is drawn as a simple quad over
489 * the frame buffer. The trick is that the quad is set as the composition
490 * destination in the blending equation, and the frame buffer becomes the source
491 * of the composition.
492 *
493 * Drawing layers with an alpha value requires an extra step before composition.
494 * An empty quad is drawn over the layer's region in the frame buffer. This quad
495 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
496 * quad is used to multiply the colors in the frame buffer. This is achieved by
497 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
498 * GL_ZERO, GL_SRC_ALPHA.
499 *
500 * Because glCopyTexImage2D() can be slow, an alternative implementation might
501 * be use to draw a single clipped layer. The implementation described above
502 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700503 *
504 * (1) The frame buffer is actually not cleared right away. To allow the GPU
505 * to potentially optimize series of calls to glCopyTexImage2D, the frame
506 * buffer is left untouched until the first drawing operation. Only when
507 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700508 */
Chet Haased48885a2012-08-28 17:43:28 -0700509bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
510 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700511 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700512 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700513
Romain Guyeb993562010-10-05 18:14:38 -0700514 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
515
Romain Guyf607bdc2010-09-10 19:20:06 -0700516 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700517 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700518 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700519 Rect untransformedBounds(bounds);
520 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700521
Chet Haased48885a2012-08-28 17:43:28 -0700522 // Layers only make sense if they are in the framebuffer's bounds
523 if (bounds.intersect(*mSnapshot->clipRect)) {
524 // We cannot work with sub-pixels in this case
525 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700526
Chet Haased48885a2012-08-28 17:43:28 -0700527 // When the layer is not an FBO, we may use glCopyTexImage so we
528 // need to make sure the layer does not extend outside the bounds
529 // of the framebuffer
530 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700531 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700532 } else if (fboLayer) {
533 clip.set(bounds);
534 mat4 inverse;
535 inverse.loadInverse(*mSnapshot->transform);
536 inverse.mapRect(clip);
537 clip.snapToPixelBoundaries();
538 if (clip.intersect(untransformedBounds)) {
539 clip.translate(-left, -top);
540 bounds.set(untransformedBounds);
541 } else {
542 clip.setEmpty();
543 }
Romain Guyad37cd32011-03-15 11:12:25 -0700544 }
Chet Haased48885a2012-08-28 17:43:28 -0700545 } else {
546 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700547 }
Romain Guybf434112010-09-16 14:40:17 -0700548
Romain Guy746b7402010-10-26 16:27:31 -0700549 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700550 bounds.getHeight() > mCaches.maxTextureSize ||
551 (fboLayer && clip.isEmpty())) {
552 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700553 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700554 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700555 }
556
557 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700558 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700559 return false;
560 }
Romain Guyf18fd992010-07-08 11:45:51 -0700561
Romain Guya1d3c912011-12-13 14:55:06 -0800562 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700563 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700564 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700565 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700566 }
567
Romain Guy9ace8f52011-07-07 20:50:11 -0700568 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700569 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700570 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
571 bounds.getWidth() / float(layer->getWidth()), 0.0f);
572 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700573 layer->setBlend(true);
Romain Guydda570202010-07-06 11:39:32 -0700574
Romain Guy8fb95422010-08-17 18:38:51 -0700575 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700576 mSnapshot->flags |= Snapshot::kFlagIsLayer;
577 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700578
Romain Guyeb993562010-10-05 18:14:38 -0700579 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700580 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700581 } else {
582 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700583 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800584 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700585 if (layer->isEmpty()) {
586 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700587 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700588 layer->getWidth(), layer->getHeight(), 0);
589 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800590 } else {
591 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700592 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800593 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700594
Romain Guy54be1cd2011-06-13 19:04:27 -0700595 // Enqueue the buffer coordinates to clear the corresponding region later
596 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700597 }
Romain Guyeb993562010-10-05 18:14:38 -0700598 }
Romain Guyf86ef572010-07-01 11:05:42 -0700599
Romain Guyd55a8612010-06-28 17:42:46 -0700600 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700601}
602
Chet Haased48885a2012-08-28 17:43:28 -0700603bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700604 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700605
Chet Haased48885a2012-08-28 17:43:28 -0700606 mSnapshot->region = &mSnapshot->layer->region;
607 mSnapshot->flags |= Snapshot::kFlagFboTarget;
Romain Guy5b3b3522010-10-27 18:57:51 -0700608
Chet Haased48885a2012-08-28 17:43:28 -0700609 mSnapshot->flags |= Snapshot::kFlagIsFboLayer;
610 mSnapshot->fbo = layer->getFbo();
611 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
612 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
613 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
614 mSnapshot->height = bounds.getHeight();
615 mSnapshot->flags |= Snapshot::kFlagDirtyOrtho;
616 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700617
618 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700619 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
620 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700621
622 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700623 if (layer->isEmpty()) {
624 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
625 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700626 }
627
628 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700629 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700630
631#if DEBUG_LAYERS_AS_REGIONS
632 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
633 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000634 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700635
636 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700637 layer->deleteTexture();
638 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700639
640 delete layer;
641
642 return false;
643 }
644#endif
645
646 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700647 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800648 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700649 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700650 glClear(GL_COLOR_BUFFER_BIT);
651
652 dirtyClip();
653
654 // Change the ortho projection
655 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
656 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
657
658 return true;
659}
660
Romain Guy1c740bc2010-09-13 18:00:09 -0700661/**
662 * Read the documentation of createLayer() before doing anything in this method.
663 */
Romain Guy1d83e192010-08-17 11:37:00 -0700664void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
665 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000666 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700667 return;
668 }
669
Romain Guy5b3b3522010-10-27 18:57:51 -0700670 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700671
672 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700673 // Detach the texture from the FBO
674 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
675
Romain Guyeb993562010-10-05 18:14:38 -0700676 // Unbind current FBO and restore previous one
677 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
678 }
679
Romain Guy1d83e192010-08-17 11:37:00 -0700680 Layer* layer = current->layer;
681 const Rect& rect = layer->layer;
682
Romain Guy9ace8f52011-07-07 20:50:11 -0700683 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700684 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700685 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700686 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700687 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700688 }
689
Romain Guy03750a02010-10-18 14:06:08 -0700690 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700691
Romain Guya1d3c912011-12-13 14:55:06 -0800692 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700693
Romain Guy5b3b3522010-10-27 18:57:51 -0700694 // When the layer is stored in an FBO, we can save a bit of fillrate by
695 // drawing only the dirty region
696 if (fboLayer) {
697 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700698 if (layer->getColorFilter()) {
699 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800700 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700701 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700702 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800703 resetColorFilter();
704 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700705 } else if (!rect.isEmpty()) {
706 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
707 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700708 }
Romain Guy8b55f372010-08-18 17:10:07 -0700709
Romain Guyeb993562010-10-05 18:14:38 -0700710 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800711 // Note: No need to use glDiscardFramebufferEXT() since we never
712 // create/compose layers that are not on screen with this
713 // code path
714 // See LayerRenderer::destroyLayer(Layer*)
715
Romain Guyeb993562010-10-05 18:14:38 -0700716 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
717 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700718 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700719 }
720
Romain Guy746b7402010-10-26 16:27:31 -0700721 dirtyClip();
722
Romain Guyeb993562010-10-05 18:14:38 -0700723 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700724 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700725 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700726 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700727 delete layer;
728 }
729}
730
Romain Guyaa6c24c2011-04-28 18:40:04 -0700731void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700732 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700733
Romain Guy302a9df2011-08-16 13:55:02 -0700734 mat4& transform = layer->getTransform();
735 if (!transform.isIdentity()) {
736 save(0);
737 mSnapshot->transform->multiply(transform);
738 }
739
Romain Guyaa6c24c2011-04-28 18:40:04 -0700740 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700741 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700742 setupDrawWithTexture();
743 } else {
744 setupDrawWithExternalTexture();
745 }
746 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700747 setupDrawColor(alpha, alpha, alpha, alpha);
748 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700749 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700750 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700751 setupDrawPureColorUniforms();
752 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700753 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
754 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700755 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700756 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700757 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700758 if (mSnapshot->transform->isPureTranslate() &&
759 layer->getWidth() == (uint32_t) rect.getWidth() &&
760 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700761 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
762 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
763
Romain Guyd21b6e12011-11-30 20:21:23 -0800764 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700765 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
766 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800767 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700768 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
769 }
770 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700771 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
772
773 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
774
775 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700776
777 if (!transform.isIdentity()) {
778 restore();
779 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700780}
781
Romain Guy5b3b3522010-10-27 18:57:51 -0700782void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700783 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700784 const Rect& texCoords = layer->texCoords;
785 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
786 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700787
Romain Guy9ace8f52011-07-07 20:50:11 -0700788 float x = rect.left;
789 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700790 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700791 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700792 layer->getHeight() == (uint32_t) rect.getHeight();
793
794 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700795 // When we're swapping, the layer is already in screen coordinates
796 if (!swap) {
797 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
798 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
799 }
800
Romain Guyd21b6e12011-11-30 20:21:23 -0800801 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700802 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800803 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700804 }
805
806 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
807 layer->getTexture(), layer->getAlpha() / 255.0f,
808 layer->getMode(), layer->isBlend(),
809 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
810 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700811
Romain Guyaa6c24c2011-04-28 18:40:04 -0700812 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
813 } else {
814 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
815 drawTextureLayer(layer, rect);
816 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
817 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700818}
819
820void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700821 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700822 layer->setRegionAsRect();
823
Romain Guy40667672011-03-18 14:34:03 -0700824 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700825
Romain Guy5b3b3522010-10-27 18:57:51 -0700826 layer->region.clear();
827 return;
828 }
829
Romain Guy8a3957d2011-09-07 17:55:15 -0700830 // TODO: See LayerRenderer.cpp::generateMesh() for important
831 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800832 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700833 size_t count;
834 const android::Rect* rects = layer->region.getArray(&count);
835
Romain Guy9ace8f52011-07-07 20:50:11 -0700836 const float alpha = layer->getAlpha() / 255.0f;
837 const float texX = 1.0f / float(layer->getWidth());
838 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800839 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700840
841 TextureVertex* mesh = mCaches.getRegionMesh();
842 GLsizei numQuads = 0;
843
Romain Guy7230a742011-01-10 22:26:16 -0800844 setupDraw();
845 setupDrawWithTexture();
846 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800847 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700848 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800849 setupDrawProgram();
850 setupDrawDirtyRegionsDisabled();
851 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800852 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700853 setupDrawTexture(layer->getTexture());
854 if (mSnapshot->transform->isPureTranslate()) {
855 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
856 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
857
Romain Guyd21b6e12011-11-30 20:21:23 -0800858 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700859 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
860 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800861 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700862 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
863 }
Romain Guy15bc6432011-12-13 13:11:32 -0800864 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700865
866 for (size_t i = 0; i < count; i++) {
867 const android::Rect* r = &rects[i];
868
869 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800870 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700871 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800872 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700873
874 // TODO: Reject quads outside of the clip
875 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
876 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
877 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
878 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
879
880 numQuads++;
881
882 if (numQuads >= REGION_MESH_QUAD_COUNT) {
883 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
884 numQuads = 0;
885 mesh = mCaches.getRegionMesh();
886 }
887 }
888
889 if (numQuads > 0) {
890 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
891 }
892
Romain Guy7230a742011-01-10 22:26:16 -0800893 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700894
895#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800896 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700897#endif
898
899 layer->region.clear();
900 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700901}
902
Romain Guy3a3133d2011-02-01 22:59:58 -0800903void OpenGLRenderer::drawRegionRects(const Region& region) {
904#if DEBUG_LAYERS_AS_REGIONS
905 size_t count;
906 const android::Rect* rects = region.getArray(&count);
907
908 uint32_t colors[] = {
909 0x7fff0000, 0x7f00ff00,
910 0x7f0000ff, 0x7fff00ff,
911 };
912
913 int offset = 0;
914 int32_t top = rects[0].top;
915
916 for (size_t i = 0; i < count; i++) {
917 if (top != rects[i].top) {
918 offset ^= 0x2;
919 top = rects[i].top;
920 }
921
922 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
923 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
924 SkXfermode::kSrcOver_Mode);
925 }
926#endif
927}
928
Romain Guy5b3b3522010-10-27 18:57:51 -0700929void OpenGLRenderer::dirtyLayer(const float left, const float top,
930 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -0800931 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700932 Rect bounds(left, top, right, bottom);
933 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800934 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700935 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700936}
937
938void OpenGLRenderer::dirtyLayer(const float left, const float top,
939 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -0800940 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700941 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800942 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800943 }
Romain Guy1bd1bad2011-01-14 20:07:20 -0800944}
945
946void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -0800947 if (bounds.intersect(*mSnapshot->clipRect)) {
948 bounds.snapToPixelBoundaries();
949 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
950 if (!dirty.isEmpty()) {
951 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700952 }
953 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700954}
955
Romain Guy54be1cd2011-06-13 19:04:27 -0700956void OpenGLRenderer::clearLayerRegions() {
957 const size_t count = mLayers.size();
958 if (count == 0) return;
959
960 if (!mSnapshot->isIgnored()) {
961 // Doing several glScissor/glClear here can negatively impact
962 // GPUs with a tiler architecture, instead we draw quads with
963 // the Clear blending mode
964
965 // The list contains bounds that have already been clipped
966 // against their initial clip rect, and the current clip
967 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -0700968 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -0700969
970 Vertex mesh[count * 6];
971 Vertex* vertex = mesh;
972
973 for (uint32_t i = 0; i < count; i++) {
974 Rect* bounds = mLayers.itemAt(i);
975
976 Vertex::set(vertex++, bounds->left, bounds->bottom);
977 Vertex::set(vertex++, bounds->left, bounds->top);
978 Vertex::set(vertex++, bounds->right, bounds->top);
979 Vertex::set(vertex++, bounds->left, bounds->bottom);
980 Vertex::set(vertex++, bounds->right, bounds->top);
981 Vertex::set(vertex++, bounds->right, bounds->bottom);
982
983 delete bounds;
984 }
985
986 setupDraw(false);
987 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
988 setupDrawBlending(true, SkXfermode::kClear_Mode);
989 setupDrawProgram();
990 setupDrawPureColorUniforms();
991 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800992 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700993
Romain Guy54be1cd2011-06-13 19:04:27 -0700994 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -0700995
996 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -0700997 } else {
998 for (uint32_t i = 0; i < count; i++) {
999 delete mLayers.itemAt(i);
1000 }
1001 }
1002
1003 mLayers.clear();
1004}
1005
Romain Guybd6b79b2010-06-26 00:13:53 -07001006///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001007// Transforms
1008///////////////////////////////////////////////////////////////////////////////
1009
1010void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001011 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001012}
1013
1014void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001015 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001016}
1017
1018void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001019 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001020}
1021
Romain Guy807daf72011-01-18 11:19:19 -08001022void OpenGLRenderer::skew(float sx, float sy) {
1023 mSnapshot->transform->skew(sx, sy);
1024}
1025
Romain Guyf6a11b82010-06-23 17:47:49 -07001026void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001027 if (matrix) {
1028 mSnapshot->transform->load(*matrix);
1029 } else {
1030 mSnapshot->transform->loadIdentity();
1031 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001032}
1033
1034void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001035 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001036}
1037
1038void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001039 SkMatrix transform;
1040 mSnapshot->transform->copyTo(transform);
1041 transform.preConcat(*matrix);
1042 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001043}
1044
1045///////////////////////////////////////////////////////////////////////////////
1046// Clipping
1047///////////////////////////////////////////////////////////////////////////////
1048
Romain Guybb9524b2010-06-22 18:56:38 -07001049void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001050 Rect clip(*mSnapshot->clipRect);
1051 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001052
Romain Guy8a4ac612012-07-17 17:32:48 -07001053 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1054 clip.getWidth(), clip.getHeight())) {
1055 mDirtyClip = false;
1056 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001057}
1058
1059const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001060 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001061}
1062
Romain Guy8a4ac612012-07-17 17:32:48 -07001063bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1064 if (mSnapshot->isIgnored()) {
1065 return true;
1066 }
1067
1068 Rect r(left, top, right, bottom);
1069 mSnapshot->transform->mapRect(r);
1070 r.snapToPixelBoundaries();
1071
1072 Rect clipRect(*mSnapshot->clipRect);
1073 clipRect.snapToPixelBoundaries();
1074
1075 return !clipRect.intersects(r);
1076}
1077
Romain Guyc7d53492010-06-25 13:41:57 -07001078bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001079 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001080 return true;
1081 }
1082
Romain Guy1d83e192010-08-17 11:37:00 -07001083 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001084 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001085 r.snapToPixelBoundaries();
1086
1087 Rect clipRect(*mSnapshot->clipRect);
1088 clipRect.snapToPixelBoundaries();
1089
Romain Guy586cae32012-07-13 15:28:31 -07001090 bool rejected = !clipRect.intersects(r);
1091 if (!isDeferred() && !rejected) {
1092 mCaches.setScissorEnabled(!clipRect.contains(r));
1093 }
1094
1095 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001096}
1097
Romain Guy079ba2c2010-07-16 14:12:24 -07001098bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1099 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001100 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001101 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001102 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001103 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001104}
1105
Chet Haasea23eed82012-04-12 15:19:04 -07001106Rect* OpenGLRenderer::getClipRect() {
1107 return mSnapshot->clipRect;
1108}
1109
Romain Guyf6a11b82010-06-23 17:47:49 -07001110///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001111// Drawing commands
1112///////////////////////////////////////////////////////////////////////////////
1113
Romain Guy54be1cd2011-06-13 19:04:27 -07001114void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001115 // TODO: It would be best if we could do this before quickReject()
1116 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001117 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001118 if (mDirtyClip) {
1119 setScissorFromClip();
1120 }
1121 mDescription.reset();
1122 mSetShaderColor = false;
1123 mColorSet = false;
1124 mColorA = mColorR = mColorG = mColorB = 0.0f;
1125 mTextureUnit = 0;
1126 mTrackDirtyRegions = true;
1127}
1128
1129void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1130 mDescription.hasTexture = true;
1131 mDescription.hasAlpha8Texture = isAlpha8;
1132}
1133
Romain Guyaa6c24c2011-04-28 18:40:04 -07001134void OpenGLRenderer::setupDrawWithExternalTexture() {
1135 mDescription.hasExternalTexture = true;
1136}
1137
Romain Guy15bc6432011-12-13 13:11:32 -08001138void OpenGLRenderer::setupDrawNoTexture() {
1139 mCaches.disbaleTexCoordsVertexArray();
1140}
1141
Chet Haase5b0200b2011-04-13 17:58:08 -07001142void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001143 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001144}
1145
Chris Craik6ebdc112012-08-31 18:24:33 -07001146void OpenGLRenderer::setupDrawAARect() {
1147 mDescription.isAARect = true;
1148}
1149
Romain Guyed6fcb02011-03-21 13:11:28 -07001150void OpenGLRenderer::setupDrawPoint(float pointSize) {
1151 mDescription.isPoint = true;
1152 mDescription.pointSize = pointSize;
1153}
1154
Romain Guy70ca14e2010-12-13 18:24:33 -08001155void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001156 setupDrawColor(color, (color >> 24) & 0xFF);
1157}
1158
1159void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1160 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001161 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1162 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001163 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001164 mColorR = a * ((color >> 16) & 0xFF);
1165 mColorG = a * ((color >> 8) & 0xFF);
1166 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001167 mColorSet = true;
1168 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1169}
1170
Romain Guy86568192010-12-14 15:55:39 -08001171void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1172 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001173 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1174 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001175 const float a = mColorA / 255.0f;
1176 mColorR = a * ((color >> 16) & 0xFF);
1177 mColorG = a * ((color >> 8) & 0xFF);
1178 mColorB = a * ((color ) & 0xFF);
1179 mColorSet = true;
1180 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1181}
1182
Romain Guy41210632012-07-16 17:04:24 -07001183void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1184 mCaches.fontRenderer->describe(mDescription, paint);
1185}
1186
Romain Guy70ca14e2010-12-13 18:24:33 -08001187void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1188 mColorA = a;
1189 mColorR = r;
1190 mColorG = g;
1191 mColorB = b;
1192 mColorSet = true;
1193 mSetShaderColor = mDescription.setColor(r, g, b, a);
1194}
1195
1196void OpenGLRenderer::setupDrawShader() {
1197 if (mShader) {
1198 mShader->describe(mDescription, mCaches.extensions);
1199 }
1200}
1201
1202void OpenGLRenderer::setupDrawColorFilter() {
1203 if (mColorFilter) {
1204 mColorFilter->describe(mDescription, mCaches.extensions);
1205 }
1206}
1207
Romain Guyf09ef512011-05-27 11:43:46 -07001208void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1209 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1210 mColorA = 1.0f;
1211 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001212 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001213 }
1214}
1215
Romain Guy70ca14e2010-12-13 18:24:33 -08001216void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001217 // When the blending mode is kClear_Mode, we need to use a modulate color
1218 // argb=1,0,0,0
1219 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001220 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1221 mDescription, swapSrcDst);
1222}
1223
1224void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001225 // When the blending mode is kClear_Mode, we need to use a modulate color
1226 // argb=1,0,0,0
1227 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001228 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1229 mDescription, swapSrcDst);
1230}
1231
1232void OpenGLRenderer::setupDrawProgram() {
1233 useProgram(mCaches.programCache.get(mDescription));
1234}
1235
1236void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1237 mTrackDirtyRegions = false;
1238}
1239
1240void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1241 bool ignoreTransform) {
1242 mModelView.loadTranslate(left, top, 0.0f);
1243 if (!ignoreTransform) {
1244 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1245 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1246 } else {
1247 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1248 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1249 }
1250}
1251
Chet Haase8a5cc922011-04-26 07:28:09 -07001252void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1253 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001254}
1255
Romain Guy70ca14e2010-12-13 18:24:33 -08001256void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1257 bool ignoreTransform, bool ignoreModelView) {
1258 if (!ignoreModelView) {
1259 mModelView.loadTranslate(left, top, 0.0f);
1260 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001261 } else {
1262 mModelView.loadIdentity();
1263 }
Romain Guy86568192010-12-14 15:55:39 -08001264 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1265 if (!ignoreTransform) {
1266 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1267 if (mTrackDirtyRegions && dirty) {
1268 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1269 }
1270 } else {
1271 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1272 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1273 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001274}
1275
Romain Guyed6fcb02011-03-21 13:11:28 -07001276void OpenGLRenderer::setupDrawPointUniforms() {
1277 int slot = mCaches.currentProgram->getUniform("pointSize");
1278 glUniform1f(slot, mDescription.pointSize);
1279}
1280
Romain Guy70ca14e2010-12-13 18:24:33 -08001281void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001282 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001283 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1284 }
1285}
1286
Romain Guy86568192010-12-14 15:55:39 -08001287void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001288 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001289 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001290 }
1291}
1292
Romain Guy70ca14e2010-12-13 18:24:33 -08001293void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1294 if (mShader) {
1295 if (ignoreTransform) {
1296 mModelView.loadInverse(*mSnapshot->transform);
1297 }
1298 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1299 }
1300}
1301
Romain Guy8d0d4782010-12-14 20:13:35 -08001302void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1303 if (mShader) {
1304 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1305 }
1306}
1307
Romain Guy70ca14e2010-12-13 18:24:33 -08001308void OpenGLRenderer::setupDrawColorFilterUniforms() {
1309 if (mColorFilter) {
1310 mColorFilter->setupProgram(mCaches.currentProgram);
1311 }
1312}
1313
Romain Guy41210632012-07-16 17:04:24 -07001314void OpenGLRenderer::setupDrawTextGammaUniforms() {
1315 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1316}
1317
Romain Guy70ca14e2010-12-13 18:24:33 -08001318void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001319 bool force = mCaches.bindMeshBuffer();
1320 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001321 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001322}
1323
1324void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1325 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001326 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001327 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001328}
1329
Romain Guyaa6c24c2011-04-28 18:40:04 -07001330void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1331 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001332 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001333 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001334}
1335
Romain Guy8f0095c2011-05-02 17:24:22 -07001336void OpenGLRenderer::setupDrawTextureTransform() {
1337 mDescription.hasTextureTransform = true;
1338}
1339
1340void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001341 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1342 GL_FALSE, &transform.data[0]);
1343}
1344
Romain Guy70ca14e2010-12-13 18:24:33 -08001345void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001346 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001347 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001348 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001349 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001350 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001351 }
Romain Guyd71dd362011-12-12 19:03:35 -08001352
Romain Guyf3a910b42011-12-12 20:35:21 -08001353 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001354 if (mCaches.currentProgram->texCoords >= 0) {
1355 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1356 }
1357
1358 mCaches.unbindIndicesBuffer();
1359}
1360
1361void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1362 bool force = mCaches.unbindMeshBuffer();
1363 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1364 if (mCaches.currentProgram->texCoords >= 0) {
1365 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001366 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001367}
1368
Chet Haase5b0200b2011-04-13 17:58:08 -07001369void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001370 bool force = mCaches.unbindMeshBuffer();
1371 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1372 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001373 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001374}
1375
1376/**
1377 * 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 -07001378 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1379 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1380 * attributes (one per vertex) are values from zero to one that tells the fragment
1381 * shader where the fragment is in relation to the line width/length overall; these values are
1382 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1383 * region of the line.
1384 * Note that we only pass down the width values in this setup function. The length coordinates
1385 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001386 */
Chet Haase99585ad2011-05-02 15:00:16 -07001387void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001388 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001389 bool force = mCaches.unbindMeshBuffer();
1390 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1391 vertices, gAAVertexStride);
1392 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001393 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001394
Romain Guy7b631422012-04-04 11:38:54 -07001395 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001396 glEnableVertexAttribArray(widthSlot);
1397 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001398
Romain Guy7b631422012-04-04 11:38:54 -07001399 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001400 glEnableVertexAttribArray(lengthSlot);
1401 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001402
Chet Haase5b0200b2011-04-13 17:58:08 -07001403 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001404 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001405}
1406
1407void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1408 glDisableVertexAttribArray(widthSlot);
1409 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001410}
1411
Romain Guy70ca14e2010-12-13 18:24:33 -08001412void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001413}
1414
1415///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001416// Drawing
1417///////////////////////////////////////////////////////////////////////////////
1418
Chet Haase1271e2c2012-04-20 09:54:27 -07001419status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001420 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001421
Romain Guy0fe478e2010-11-08 12:08:41 -08001422 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1423 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001424 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001425 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001426 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001427
Romain Guy65549432012-03-26 16:45:05 -07001428 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001429}
1430
Chet Haaseed30fd82011-04-22 16:18:45 -07001431void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1432 if (displayList) {
1433 displayList->output(*this, level);
1434 }
1435}
1436
Romain Guya168d732011-03-18 16:50:13 -07001437void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1438 int alpha;
1439 SkXfermode::Mode mode;
1440 getAlphaAndMode(paint, &alpha, &mode);
1441
Romain Guya168d732011-03-18 16:50:13 -07001442 float x = left;
1443 float y = top;
1444
Romain Guye3c26852011-07-25 16:36:01 -07001445 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001446 bool ignoreTransform = false;
1447 if (mSnapshot->transform->isPureTranslate()) {
1448 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1449 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1450 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001451 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001452 } else {
1453 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001454 }
1455
1456 setupDraw();
1457 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001458 if (paint) {
1459 setupDrawAlpha8Color(paint->getColor(), alpha);
1460 }
Romain Guya168d732011-03-18 16:50:13 -07001461 setupDrawColorFilter();
1462 setupDrawShader();
1463 setupDrawBlending(true, mode);
1464 setupDrawProgram();
1465 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001466
Romain Guya168d732011-03-18 16:50:13 -07001467 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001468 texture->setWrap(GL_CLAMP_TO_EDGE);
1469 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001470
Romain Guya168d732011-03-18 16:50:13 -07001471 setupDrawPureColorUniforms();
1472 setupDrawColorFilterUniforms();
1473 setupDrawShaderUniforms();
1474 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1475
1476 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1477
1478 finishDrawTexture();
1479}
1480
Chet Haase48659092012-05-31 15:21:51 -07001481status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001482 const float right = left + bitmap->width();
1483 const float bottom = top + bitmap->height();
1484
1485 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001486 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001487 }
1488
Romain Guya1d3c912011-12-13 14:55:06 -08001489 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001490 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001491 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001492 const AutoTexture autoCleanup(texture);
1493
Romain Guy211370f2012-02-01 16:10:55 -08001494 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001495 drawAlphaBitmap(texture, left, top, paint);
1496 } else {
1497 drawTextureRect(left, top, right, bottom, texture, paint);
1498 }
Chet Haase48659092012-05-31 15:21:51 -07001499
1500 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001501}
1502
Chet Haase48659092012-05-31 15:21:51 -07001503status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001504 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1505 const mat4 transform(*matrix);
1506 transform.mapRect(r);
1507
Romain Guy6926c722010-07-12 20:20:03 -07001508 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001509 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001510 }
1511
Romain Guya1d3c912011-12-13 14:55:06 -08001512 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001513 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001514 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001515 const AutoTexture autoCleanup(texture);
1516
Romain Guy5b3b3522010-10-27 18:57:51 -07001517 // This could be done in a cheaper way, all we need is pass the matrix
1518 // to the vertex shader. The save/restore is a bit overkill.
1519 save(SkCanvas::kMatrix_SaveFlag);
1520 concatMatrix(matrix);
1521 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1522 restore();
Chet Haase48659092012-05-31 15:21:51 -07001523
1524 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001525}
1526
Chet Haase48659092012-05-31 15:21:51 -07001527status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001528 const float right = left + bitmap->width();
1529 const float bottom = top + bitmap->height();
1530
1531 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001532 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001533 }
1534
1535 mCaches.activeTexture(0);
1536 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1537 const AutoTexture autoCleanup(texture);
1538
1539 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001540
1541 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001542}
1543
Chet Haase48659092012-05-31 15:21:51 -07001544status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001545 float* vertices, int* colors, SkPaint* paint) {
1546 // TODO: Do a quickReject
1547 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001548 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001549 }
1550
Romain Guya1d3c912011-12-13 14:55:06 -08001551 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001552 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001553 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001554 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001555
Romain Guyd21b6e12011-11-30 20:21:23 -08001556 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1557 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001558
1559 int alpha;
1560 SkXfermode::Mode mode;
1561 getAlphaAndMode(paint, &alpha, &mode);
1562
Romain Guy5a7b4662011-01-20 19:09:30 -08001563 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001564
Romain Guyb18d2d02011-02-10 15:52:54 -08001565 float left = FLT_MAX;
1566 float top = FLT_MAX;
1567 float right = FLT_MIN;
1568 float bottom = FLT_MIN;
1569
Romain Guy211370f2012-02-01 16:10:55 -08001570 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001571
Romain Guya566b7c2011-01-23 16:36:11 -08001572 // TODO: Support the colors array
1573 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001574 TextureVertex* vertex = mesh;
1575 for (int32_t y = 0; y < meshHeight; y++) {
1576 for (int32_t x = 0; x < meshWidth; x++) {
1577 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1578
1579 float u1 = float(x) / meshWidth;
1580 float u2 = float(x + 1) / meshWidth;
1581 float v1 = float(y) / meshHeight;
1582 float v2 = float(y + 1) / meshHeight;
1583
1584 int ax = i + (meshWidth + 1) * 2;
1585 int ay = ax + 1;
1586 int bx = i;
1587 int by = bx + 1;
1588 int cx = i + 2;
1589 int cy = cx + 1;
1590 int dx = i + (meshWidth + 1) * 2 + 2;
1591 int dy = dx + 1;
1592
1593 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1594 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1595 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1596
1597 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1598 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1599 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001600
Romain Guyb18d2d02011-02-10 15:52:54 -08001601 if (hasActiveLayer) {
1602 // TODO: This could be optimized to avoid unnecessary ops
1603 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1604 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1605 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1606 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1607 }
Romain Guy5a7b4662011-01-20 19:09:30 -08001608 }
1609 }
1610
Romain Guyb18d2d02011-02-10 15:52:54 -08001611 if (hasActiveLayer) {
1612 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1613 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001614
Romain Guy5a7b4662011-01-20 19:09:30 -08001615 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1616 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001617 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001618
1619 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001620}
1621
Chet Haase48659092012-05-31 15:21:51 -07001622status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001623 float srcLeft, float srcTop, float srcRight, float srcBottom,
1624 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001625 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001626 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001627 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001628 }
1629
Romain Guya1d3c912011-12-13 14:55:06 -08001630 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001631 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001632 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001633 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001634
Romain Guy8ba548f2010-06-30 19:21:21 -07001635 const float width = texture->width;
1636 const float height = texture->height;
1637
Romain Guy68169722011-08-22 17:33:33 -07001638 const float u1 = fmax(0.0f, srcLeft / width);
1639 const float v1 = fmax(0.0f, srcTop / height);
1640 const float u2 = fmin(1.0f, srcRight / width);
1641 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001642
Romain Guy03750a02010-10-18 14:06:08 -07001643 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001644 resetDrawTextureTexCoords(u1, v1, u2, v2);
1645
Romain Guy03750a02010-10-18 14:06:08 -07001646 int alpha;
1647 SkXfermode::Mode mode;
1648 getAlphaAndMode(paint, &alpha, &mode);
1649
Romain Guyd21b6e12011-11-30 20:21:23 -08001650 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1651
Romain Guy211370f2012-02-01 16:10:55 -08001652 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001653 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1654 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1655
Romain Guyb5014982011-07-28 15:39:12 -07001656 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001657 // Enable linear filtering if the source rectangle is scaled
1658 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001659 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001660 }
Romain Guyb5014982011-07-28 15:39:12 -07001661
Romain Guyd21b6e12011-11-30 20:21:23 -08001662 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001663 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1664 texture->id, alpha / 255.0f, mode, texture->blend,
1665 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1666 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1667 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001668 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001669 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1670 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1671 GL_TRIANGLE_STRIP, gMeshCount);
1672 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001673
1674 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001675
1676 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001677}
1678
Chet Haase48659092012-05-31 15:21:51 -07001679status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001680 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001681 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001682 int alpha;
1683 SkXfermode::Mode mode;
1684 getAlphaAndModeDirect(paint, &alpha, &mode);
1685
1686 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1687 left, top, right, bottom, alpha, mode);
1688}
1689
1690status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1691 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1692 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001693 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001694 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001695 }
1696
Romain Guybe6f9dc2012-07-16 12:41:17 -07001697 alpha *= mSnapshot->alpha;
1698
Romain Guya1d3c912011-12-13 14:55:06 -08001699 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001700 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001701 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001702 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001703 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1704 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001705
Romain Guy2728f962010-10-08 18:36:15 -07001706 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001707 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001708
Romain Guy211370f2012-02-01 16:10:55 -08001709 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001710 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001711 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001712 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001713 const float offsetX = left + mSnapshot->transform->getTranslateX();
1714 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001715 const size_t count = mesh->quads.size();
1716 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001717 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001718 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001719 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1720 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1721 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001722 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001723 dirtyLayer(left + bounds.left, top + bounds.top,
1724 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001725 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001726 }
1727 }
1728
Romain Guy211370f2012-02-01 16:10:55 -08001729 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001730 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1731 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1732
1733 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1734 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1735 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1736 true, !mesh->hasEmptyQuads);
1737 } else {
1738 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1739 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1740 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1741 true, !mesh->hasEmptyQuads);
1742 }
Romain Guy054dc182010-10-15 17:55:25 -07001743 }
Chet Haase48659092012-05-31 15:21:51 -07001744
1745 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001746}
1747
Chet Haase99ecdc42011-05-06 12:06:34 -07001748/**
Chet Haase858aa932011-05-12 09:06:00 -07001749 * This function uses a similar approach to that of AA lines in the drawLines() function.
Chris Craik6ebdc112012-08-31 18:24:33 -07001750 * We expand the rectangle by a half pixel in screen space on all sides. However, instead of using
1751 * a fragment shader to compute the translucency of the color from its position, we simply use a
1752 * varying parameter to define how far a given pixel is into the region.
Chet Haase858aa932011-05-12 09:06:00 -07001753 */
1754void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001755 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001756 float inverseScaleX = 1.0f;
1757 float inverseScaleY = 1.0f;
Romain Guy04299382012-07-18 17:15:41 -07001758
Chet Haase858aa932011-05-12 09:06:00 -07001759 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001760 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001761 Matrix4 *mat = mSnapshot->transform;
1762 float m00 = mat->data[Matrix4::kScaleX];
1763 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase858aa932011-05-12 09:06:00 -07001764 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07001765 float m11 = mat->data[Matrix4::kScaleY];
Chet Haase858aa932011-05-12 09:06:00 -07001766 float scaleX = sqrt(m00 * m00 + m01 * m01);
1767 float scaleY = sqrt(m10 * m10 + m11 * m11);
1768 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1769 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1770 }
1771
Chet Haase858aa932011-05-12 09:06:00 -07001772 float boundarySizeX = .5 * inverseScaleX;
1773 float boundarySizeY = .5 * inverseScaleY;
1774
Chris Craik6ebdc112012-08-31 18:24:33 -07001775 float innerLeft = left + boundarySizeX;
1776 float innerRight = right - boundarySizeX;
1777 float innerTop = top + boundarySizeY;
1778 float innerBottom = bottom - boundarySizeY;
1779
Chet Haase858aa932011-05-12 09:06:00 -07001780 // Adjust the rect by the AA boundary padding
1781 left -= boundarySizeX;
1782 right += boundarySizeX;
1783 top -= boundarySizeY;
1784 bottom += boundarySizeY;
1785
Chet Haase858aa932011-05-12 09:06:00 -07001786 if (!quickReject(left, top, right, bottom)) {
Romain Guy04299382012-07-18 17:15:41 -07001787 setupDraw();
1788 setupDrawNoTexture();
Chris Craik6ebdc112012-08-31 18:24:33 -07001789 setupDrawAARect();
Romain Guy04299382012-07-18 17:15:41 -07001790 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1791 setupDrawColorFilter();
1792 setupDrawShader();
1793 setupDrawBlending(true, mode);
1794 setupDrawProgram();
1795 setupDrawModelViewIdentity(true);
1796 setupDrawColorUniforms();
1797 setupDrawColorFilterUniforms();
1798 setupDrawShaderIdentityUniforms();
1799
Chris Craik6ebdc112012-08-31 18:24:33 -07001800 AlphaVertex rects[14];
1801 AlphaVertex* aVertices = &rects[0];
1802 void* alphaCoords = ((GLbyte*) aVertices) + gVertexAlphaOffset;
Romain Guy04299382012-07-18 17:15:41 -07001803
Chris Craik6ebdc112012-08-31 18:24:33 -07001804 bool force = mCaches.unbindMeshBuffer();
1805 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1806 aVertices, gAlphaVertexStride);
1807 mCaches.resetTexCoordsVertexPointer();
1808 mCaches.unbindIndicesBuffer();
Romain Guy04299382012-07-18 17:15:41 -07001809
Chris Craik6ebdc112012-08-31 18:24:33 -07001810 int alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
1811 glEnableVertexAttribArray(alphaSlot);
1812 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Romain Guy04299382012-07-18 17:15:41 -07001813
Chris Craik6ebdc112012-08-31 18:24:33 -07001814 // draw left
1815 AlphaVertex::set(aVertices++, left, bottom, 0);
1816 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1817 AlphaVertex::set(aVertices++, left, top, 0);
1818 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001819
Chris Craik6ebdc112012-08-31 18:24:33 -07001820 // draw top
1821 AlphaVertex::set(aVertices++, right, top, 0);
1822 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001823
Chris Craik6ebdc112012-08-31 18:24:33 -07001824 // draw right
1825 AlphaVertex::set(aVertices++, right, bottom, 0);
1826 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1827
1828 // draw bottom
1829 AlphaVertex::set(aVertices++, left, bottom, 0);
1830 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1831
1832 // draw inner rect (repeating last vertex to create degenerate bridge triangles)
1833 // TODO: also consider drawing the inner rect without the blending-forced shader, if
1834 // blending is expensive. Note: can't use drawColorRect() since it doesn't use vertex
1835 // buffers like below, resulting in slightly different transformed coordinates.
1836 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1837 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
1838 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1839 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
1840
Chet Haase858aa932011-05-12 09:06:00 -07001841 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guy7b631422012-04-04 11:38:54 -07001842
Chris Craik6ebdc112012-08-31 18:24:33 -07001843 glDrawArrays(GL_TRIANGLE_STRIP, 0, 14);
Romain Guy04299382012-07-18 17:15:41 -07001844
Chris Craik6ebdc112012-08-31 18:24:33 -07001845 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07001846 }
Chet Haase858aa932011-05-12 09:06:00 -07001847}
1848
1849/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001850 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1851 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1852 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1853 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1854 * of the line. Hairlines are more involved because we need to account for transform scaling
1855 * to end up with a one-pixel-wide line in screen space..
1856 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1857 * in combination with values that we calculate and pass down in this method. The basic approach
1858 * is that the quad we create contains both the core line area plus a bounding area in which
1859 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1860 * proportion of the width and the length of a given segment is represented by the boundary
1861 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1862 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1863 * on the inside). This ends up giving the result we want, with pixels that are completely
1864 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1865 * how far into the boundary region they are, which is determined by shader interpolation.
1866 */
Chet Haase48659092012-05-31 15:21:51 -07001867status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1868 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07001869
1870 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001871 // We use half the stroke width here because we're going to position the quad
1872 // corner vertices half of the width away from the line endpoints
1873 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001874 // A stroke width of 0 has a special meaning in Skia:
1875 // it draws a line 1 px wide regardless of current transform
1876 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001877
Chet Haase99ecdc42011-05-06 12:06:34 -07001878 float inverseScaleX = 1.0f;
1879 float inverseScaleY = 1.0f;
1880 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001881
Chet Haase8a5cc922011-04-26 07:28:09 -07001882 int alpha;
1883 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001884
Chet Haase8a5cc922011-04-26 07:28:09 -07001885 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001886 int verticesCount = count;
1887 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001888 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001889 verticesCount += (count - 4);
1890 }
1891
1892 if (isHairLine || isAA) {
1893 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1894 // the line on the screen should always be one pixel wide regardless of scale. For
1895 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001896 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001897 Matrix4 *mat = mSnapshot->transform;
1898 float m00 = mat->data[Matrix4::kScaleX];
1899 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07001900 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07001901 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07001902
Romain Guy211370f2012-02-01 16:10:55 -08001903 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1904 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001905
Chet Haase99ecdc42011-05-06 12:06:34 -07001906 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1907 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001908
Chet Haase99ecdc42011-05-06 12:06:34 -07001909 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1910 scaled = true;
1911 }
1912 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001913 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001914
1915 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07001916
1917 mCaches.enableScissor();
1918
Chet Haase8a5cc922011-04-26 07:28:09 -07001919 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001920 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001921 if (isAA) {
1922 setupDrawAALine();
1923 }
1924 setupDrawColor(paint->getColor(), alpha);
1925 setupDrawColorFilter();
1926 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001927 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001928 setupDrawProgram();
1929 setupDrawModelViewIdentity(true);
1930 setupDrawColorUniforms();
1931 setupDrawColorFilterUniforms();
1932 setupDrawShaderIdentityUniforms();
1933
1934 if (isHairLine) {
1935 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001936 halfStrokeWidth = isAA? 1 : .5;
1937 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001938 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001939 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001940 }
Romain Guy7b631422012-04-04 11:38:54 -07001941
1942 int widthSlot;
1943 int lengthSlot;
1944
Chet Haase5b0200b2011-04-13 17:58:08 -07001945 Vertex lines[verticesCount];
1946 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001947
Chet Haase99585ad2011-05-02 15:00:16 -07001948 AAVertex wLines[verticesCount];
1949 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001950
Romain Guy211370f2012-02-01 16:10:55 -08001951 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001952 setupDrawVertices(vertices);
1953 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001954 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1955 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001956 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001957 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1958 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001959 // We will need to calculate the actual width proportion on each segment for
1960 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07001961 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001962 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1963 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001964 }
1965
Chet Haase99ecdc42011-05-06 12:06:34 -07001966 AAVertex* prevAAVertex = NULL;
1967 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001968
Chet Haase99585ad2011-05-02 15:00:16 -07001969 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001970 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001971
Chet Haase5b0200b2011-04-13 17:58:08 -07001972 for (int i = 0; i < count; i += 4) {
1973 // a = start point, b = end point
1974 vec2 a(points[i], points[i + 1]);
1975 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001976
Chet Haase99585ad2011-05-02 15:00:16 -07001977 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001978 float boundaryLengthProportion = 0;
1979 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001980
Chet Haase5b0200b2011-04-13 17:58:08 -07001981 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001982 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07001983 float x = n.x;
1984 n.x = -n.y;
1985 n.y = x;
1986
Chet Haase8a5cc922011-04-26 07:28:09 -07001987 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001988 if (isAA) {
1989 float wideningFactor;
1990 if (fabs(n.x) >= fabs(n.y)) {
1991 wideningFactor = fabs(1.0f / n.x);
1992 } else {
1993 wideningFactor = fabs(1.0f / n.y);
1994 }
1995 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001996 }
Romain Guy7b631422012-04-04 11:38:54 -07001997
Chet Haase99ecdc42011-05-06 12:06:34 -07001998 if (scaled) {
1999 n.x *= inverseScaleX;
2000 n.y *= inverseScaleY;
2001 }
2002 } else if (scaled) {
2003 // Extend n by .5 pixel on each side, post-transform
2004 vec2 extendedN = n.copyNormalized();
2005 extendedN /= 2;
2006 extendedN.x *= inverseScaleX;
2007 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002008
Chet Haase99ecdc42011-05-06 12:06:34 -07002009 float extendedNLength = extendedN.length();
2010 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002011 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002012 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002013 }
Romain Guy7b631422012-04-04 11:38:54 -07002014
Chet Haase99585ad2011-05-02 15:00:16 -07002015 // aa lines expand the endpoint vertices to encompass the AA boundary
2016 if (isAA) {
2017 vec2 abVector = (b - a);
2018 length = abVector.length();
2019 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002020
Chet Haase99ecdc42011-05-06 12:06:34 -07002021 if (scaled) {
2022 abVector.x *= inverseScaleX;
2023 abVector.y *= inverseScaleY;
2024 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002025 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002026 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002027 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002028 }
Romain Guy7b631422012-04-04 11:38:54 -07002029
Chet Haase99ecdc42011-05-06 12:06:34 -07002030 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002031 a -= abVector;
2032 b += abVector;
2033 }
2034
Chet Haase5b0200b2011-04-13 17:58:08 -07002035 // Four corners of the rectangle defining a thick line
2036 vec2 p1 = a - n;
2037 vec2 p2 = a + n;
2038 vec2 p3 = b + n;
2039 vec2 p4 = b - n;
2040
Chet Haase99585ad2011-05-02 15:00:16 -07002041
Chet Haase5b0200b2011-04-13 17:58:08 -07002042 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2043 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2044 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2045 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2046
Romain Guy04299382012-07-18 17:15:41 -07002047 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002048 if (!isAA) {
2049 if (prevVertex != NULL) {
2050 // Issue two repeat vertices to create degenerate triangles to bridge
2051 // between the previous line and the new one. This is necessary because
2052 // we are creating a single triangle_strip which will contain
2053 // potentially discontinuous line segments.
2054 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2055 Vertex::set(vertices++, p1.x, p1.y);
2056 generatedVerticesCount += 2;
2057 }
Romain Guy7b631422012-04-04 11:38:54 -07002058
Chet Haase5b0200b2011-04-13 17:58:08 -07002059 Vertex::set(vertices++, p1.x, p1.y);
2060 Vertex::set(vertices++, p2.x, p2.y);
2061 Vertex::set(vertices++, p4.x, p4.y);
2062 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002063
Chet Haase5b0200b2011-04-13 17:58:08 -07002064 prevVertex = vertices - 1;
2065 generatedVerticesCount += 4;
2066 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002067 if (!isHairLine && scaled) {
2068 // Must set width proportions per-segment for scaled non-hairlines to use the
2069 // correct AA boundary dimensions
2070 if (boundaryWidthSlot < 0) {
2071 boundaryWidthSlot =
2072 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002073 }
Romain Guy7b631422012-04-04 11:38:54 -07002074
Chet Haase99ecdc42011-05-06 12:06:34 -07002075 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002076 }
Romain Guy7b631422012-04-04 11:38:54 -07002077
Chet Haase99585ad2011-05-02 15:00:16 -07002078 if (boundaryLengthSlot < 0) {
2079 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002080 }
Romain Guy7b631422012-04-04 11:38:54 -07002081
Chet Haase99ecdc42011-05-06 12:06:34 -07002082 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002083
Chet Haase5b0200b2011-04-13 17:58:08 -07002084 if (prevAAVertex != NULL) {
2085 // Issue two repeat vertices to create degenerate triangles to bridge
2086 // between the previous line and the new one. This is necessary because
2087 // we are creating a single triangle_strip which will contain
2088 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002089 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2090 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2091 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002092 generatedVerticesCount += 2;
2093 }
Romain Guy7b631422012-04-04 11:38:54 -07002094
Chet Haase99585ad2011-05-02 15:00:16 -07002095 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2096 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2097 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2098 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002099
Chet Haase5b0200b2011-04-13 17:58:08 -07002100 prevAAVertex = aaVertices - 1;
2101 generatedVerticesCount += 4;
2102 }
Romain Guy7b631422012-04-04 11:38:54 -07002103
Chet Haase99585ad2011-05-02 15:00:16 -07002104 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2105 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2106 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002107 }
2108 }
Romain Guy7b631422012-04-04 11:38:54 -07002109
Chet Haase5b0200b2011-04-13 17:58:08 -07002110 if (generatedVerticesCount > 0) {
2111 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2112 }
Romain Guy7b631422012-04-04 11:38:54 -07002113
2114 if (isAA) {
2115 finishDrawAALine(widthSlot, lengthSlot);
2116 }
Chet Haase48659092012-05-31 15:21:51 -07002117
2118 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002119}
2120
Chet Haase48659092012-05-31 15:21:51 -07002121status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2122 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002123
2124 // TODO: The paint's cap style defines whether the points are square or circular
2125 // TODO: Handle AA for round points
2126
Chet Haase5b0200b2011-04-13 17:58:08 -07002127 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002128 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002129 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002130 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002131 if (isHairLine) {
2132 // Now that we know it's hairline, we can set the effective width, to be used later
2133 strokeWidth = 1.0f;
2134 }
2135 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002136 int alpha;
2137 SkXfermode::Mode mode;
2138 getAlphaAndMode(paint, &alpha, &mode);
2139
2140 int verticesCount = count >> 1;
2141 int generatedVerticesCount = 0;
2142
2143 TextureVertex pointsData[verticesCount];
2144 TextureVertex* vertex = &pointsData[0];
2145
Romain Guy04299382012-07-18 17:15:41 -07002146 // TODO: We should optimize this method to not generate vertices for points
2147 // that lie outside of the clip.
2148 mCaches.enableScissor();
2149
Romain Guyed6fcb02011-03-21 13:11:28 -07002150 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002151 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002152 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002153 setupDrawColor(paint->getColor(), alpha);
2154 setupDrawColorFilter();
2155 setupDrawShader();
2156 setupDrawBlending(mode);
2157 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002158 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002159 setupDrawColorUniforms();
2160 setupDrawColorFilterUniforms();
2161 setupDrawPointUniforms();
2162 setupDrawShaderIdentityUniforms();
2163 setupDrawMesh(vertex);
2164
2165 for (int i = 0; i < count; i += 2) {
2166 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2167 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002168
Chet Haase8a5cc922011-04-26 07:28:09 -07002169 float left = points[i] - halfWidth;
2170 float right = points[i] + halfWidth;
2171 float top = points[i + 1] - halfWidth;
2172 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002173
Chet Haase8a5cc922011-04-26 07:28:09 -07002174 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002175 }
2176
2177 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002178
2179 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002180}
2181
Chet Haase48659092012-05-31 15:21:51 -07002182status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002183 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002184 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002185
Romain Guyae88e5e2010-10-22 17:49:18 -07002186 Rect& clip(*mSnapshot->clipRect);
2187 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002188
Romain Guy3d58c032010-07-14 16:34:53 -07002189 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002190
2191 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002192}
Romain Guy9d5316e2010-06-24 19:30:36 -07002193
Chet Haase48659092012-05-31 15:21:51 -07002194status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2195 SkPaint* paint) {
2196 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002197 const AutoTexture autoCleanup(texture);
2198
2199 const float x = left + texture->left - texture->offset;
2200 const float y = top + texture->top - texture->offset;
2201
2202 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002203
2204 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002205}
2206
Chet Haase48659092012-05-31 15:21:51 -07002207status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002208 float rx, float ry, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002209 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002210
Romain Guya1d3c912011-12-13 14:55:06 -08002211 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002212 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2213 right - left, bottom - top, rx, ry, paint);
Chet Haase48659092012-05-31 15:21:51 -07002214 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002215}
2216
Chet Haase48659092012-05-31 15:21:51 -07002217status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2218 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002219
Romain Guya1d3c912011-12-13 14:55:06 -08002220 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002221 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Chet Haase48659092012-05-31 15:21:51 -07002222 return drawShape(x - radius, y - radius, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002223}
Romain Guy01d58e42011-01-19 21:54:02 -08002224
Chet Haase48659092012-05-31 15:21:51 -07002225status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
2226 SkPaint* paint) {
2227 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002228
Romain Guya1d3c912011-12-13 14:55:06 -08002229 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002230 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002231 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002232}
2233
Chet Haase48659092012-05-31 15:21:51 -07002234status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002235 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002236 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002237
2238 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002239 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002240 }
2241
Romain Guya1d3c912011-12-13 14:55:06 -08002242 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002243 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2244 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002245 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002246}
2247
Chet Haase48659092012-05-31 15:21:51 -07002248status_t OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002249 SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002250 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002251
Romain Guya1d3c912011-12-13 14:55:06 -08002252 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002253 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002254 return drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002255}
2256
Chet Haase48659092012-05-31 15:21:51 -07002257status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002258 if (p->getStyle() != SkPaint::kFill_Style) {
Chet Haase48659092012-05-31 15:21:51 -07002259 return drawRectAsShape(left, top, right, bottom, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002260 }
2261
Romain Guy6926c722010-07-12 20:20:03 -07002262 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002263 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002264 }
2265
Romain Guy026c5e162010-06-28 17:12:22 -07002266 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002267 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002268 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2269 if (!isMode) {
2270 // Assume SRC_OVER
2271 mode = SkXfermode::kSrcOver_Mode;
2272 }
2273 } else {
2274 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002275 }
2276
Romain Guy026c5e162010-06-28 17:12:22 -07002277 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002278 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002279 drawAARect(left, top, right, bottom, color, mode);
2280 } else {
2281 drawColorRect(left, top, right, bottom, color, mode);
2282 }
Chet Haase48659092012-05-31 15:21:51 -07002283
2284 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002285}
Romain Guy9d5316e2010-06-24 19:30:36 -07002286
Raph Levien416a8472012-07-19 22:48:17 -07002287void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2288 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2289 float x, float y) {
2290 mCaches.activeTexture(0);
2291
2292 // NOTE: The drop shadow will not perform gamma correction
2293 // if shader-based correction is enabled
2294 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2295 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2296 paint, text, bytesCount, count, mShadowRadius, positions);
2297 const AutoTexture autoCleanup(shadow);
2298
2299 const float sx = x - shadow->left + mShadowDx;
2300 const float sy = y - shadow->top + mShadowDy;
2301
2302 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2303 int shadowColor = mShadowColor;
2304 if (mShader) {
2305 shadowColor = 0xffffffff;
2306 }
2307
2308 setupDraw();
2309 setupDrawWithTexture(true);
2310 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2311 setupDrawColorFilter();
2312 setupDrawShader();
2313 setupDrawBlending(true, mode);
2314 setupDrawProgram();
2315 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2316 setupDrawTexture(shadow->id);
2317 setupDrawPureColorUniforms();
2318 setupDrawColorFilterUniforms();
2319 setupDrawShaderUniforms();
2320 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2321
2322 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2323}
2324
Chet Haase48659092012-05-31 15:21:51 -07002325status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002326 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002327 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002328 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002329 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002330 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002331
Romain Guy671d6cf2012-01-18 12:39:17 -08002332 // NOTE: Skia does not support perspective transform on drawPosText yet
2333 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002334 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002335 }
2336
2337 float x = 0.0f;
2338 float y = 0.0f;
2339 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2340 if (pureTranslate) {
2341 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2342 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2343 }
2344
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002345 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002346 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2347 paint->getTextSize());
2348
2349 int alpha;
2350 SkXfermode::Mode mode;
2351 getAlphaAndMode(paint, &alpha, &mode);
2352
Raph Levien416a8472012-07-19 22:48:17 -07002353 if (CC_UNLIKELY(mHasShadow)) {
2354 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2355 0.0f, 0.0f);
2356 }
2357
Romain Guy671d6cf2012-01-18 12:39:17 -08002358 // Pick the appropriate texture filtering
2359 bool linearFilter = mSnapshot->transform->changesBounds();
2360 if (pureTranslate && !linearFilter) {
2361 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2362 }
2363
2364 mCaches.activeTexture(0);
2365 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002366 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002367 setupDrawDirtyRegionsDisabled();
2368 setupDrawWithTexture(true);
2369 setupDrawAlpha8Color(paint->getColor(), alpha);
2370 setupDrawColorFilter();
2371 setupDrawShader();
2372 setupDrawBlending(true, mode);
2373 setupDrawProgram();
2374 setupDrawModelView(x, y, x, y, pureTranslate, true);
2375 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2376 setupDrawPureColorUniforms();
2377 setupDrawColorFilterUniforms();
2378 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002379 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002380
2381 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2382 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2383
Romain Guy211370f2012-02-01 16:10:55 -08002384 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002385
2386 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2387 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002388 if (hasActiveLayer) {
2389 if (!pureTranslate) {
2390 mSnapshot->transform->mapRect(bounds);
2391 }
2392 dirtyLayerUnchecked(bounds, getRegion());
2393 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002394 }
Chet Haase48659092012-05-31 15:21:51 -07002395
2396 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002397}
2398
Romain Guyc2525952012-07-27 16:41:22 -07002399status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002400 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002401 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002402 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002403 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002404 }
2405
Chet Haasea1cff502012-02-21 13:43:44 -08002406 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002407 switch (paint->getTextAlign()) {
2408 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002409 x -= length / 2.0f;
2410 break;
2411 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002412 x -= length;
2413 break;
2414 default:
2415 break;
2416 }
2417
Romain Guycac5fd32011-12-01 20:08:50 -08002418 SkPaint::FontMetrics metrics;
2419 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002420 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002421 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002422 }
2423
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002424 const float oldX = x;
2425 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002426 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002427 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002428 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2429 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2430 }
2431
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002432#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002433 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002434 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002435#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002436
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002437 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002438 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002439 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002440
Romain Guy86568192010-12-14 15:55:39 -08002441 int alpha;
2442 SkXfermode::Mode mode;
2443 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002444
Romain Guy211370f2012-02-01 16:10:55 -08002445 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002446 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2447 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002448 }
2449
Romain Guy6620c6d2010-12-06 18:07:02 -08002450 // Pick the appropriate texture filtering
2451 bool linearFilter = mSnapshot->transform->changesBounds();
2452 if (pureTranslate && !linearFilter) {
2453 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2454 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002455
Romain Guy16c88082012-06-11 16:03:47 -07002456 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002457 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002458 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002459 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002460 setupDrawDirtyRegionsDisabled();
2461 setupDrawWithTexture(true);
2462 setupDrawAlpha8Color(paint->getColor(), alpha);
2463 setupDrawColorFilter();
2464 setupDrawShader();
2465 setupDrawBlending(true, mode);
2466 setupDrawProgram();
2467 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002468 // See comment above; the font renderer must use texture unit 0
2469 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002470 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2471 setupDrawPureColorUniforms();
2472 setupDrawColorFilterUniforms();
2473 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002474 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002475
Romain Guy6620c6d2010-12-06 18:07:02 -08002476 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002477 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2478
Romain Guy211370f2012-02-01 16:10:55 -08002479 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002480
Raph Levien996e57c2012-07-23 15:22:52 -07002481 bool status;
Raph Levien8b4072d2012-07-30 15:50:00 -07002482 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
2483 SkPaint paintCopy(*paint);
2484 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2485 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Raph Levien996e57c2012-07-23 15:22:52 -07002486 positions, hasActiveLayer ? &bounds : NULL);
2487 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002488 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2489 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002490 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002491
2492 if (status && hasActiveLayer) {
2493 if (!pureTranslate) {
2494 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002495 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002496 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002497 }
Romain Guy694b5192010-07-21 21:33:20 -07002498
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002499 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002500
2501 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002502}
2503
Chet Haase48659092012-05-31 15:21:51 -07002504status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002505 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002506 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2507 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002508 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002509 }
2510
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002511 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002512 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2513 paint->getTextSize());
2514
2515 int alpha;
2516 SkXfermode::Mode mode;
2517 getAlphaAndMode(paint, &alpha, &mode);
2518
2519 mCaches.activeTexture(0);
2520 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002521 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002522 setupDrawDirtyRegionsDisabled();
2523 setupDrawWithTexture(true);
2524 setupDrawAlpha8Color(paint->getColor(), alpha);
2525 setupDrawColorFilter();
2526 setupDrawShader();
2527 setupDrawBlending(true, mode);
2528 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002529 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002530 setupDrawTexture(fontRenderer.getTexture(true));
2531 setupDrawPureColorUniforms();
2532 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002533 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002534 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002535
Romain Guy97771732012-02-28 18:17:02 -08002536 const Rect* clip = &mSnapshot->getLocalClip();
2537 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 -08002538
Romain Guy97771732012-02-28 18:17:02 -08002539 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002540
2541 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2542 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002543 if (hasActiveLayer) {
2544 mSnapshot->transform->mapRect(bounds);
2545 dirtyLayerUnchecked(bounds, getRegion());
2546 }
Romain Guy97771732012-02-28 18:17:02 -08002547 }
Chet Haase48659092012-05-31 15:21:51 -07002548
2549 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002550}
2551
Chet Haase48659092012-05-31 15:21:51 -07002552status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2553 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002554
Romain Guya1d3c912011-12-13 14:55:06 -08002555 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002556
Romain Guy33f6beb2012-02-16 19:24:51 -08002557 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002558 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002559 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002560 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002561
Romain Guy8b55f372010-08-18 17:10:07 -07002562 const float x = texture->left - texture->offset;
2563 const float y = texture->top - texture->offset;
2564
Romain Guy01d58e42011-01-19 21:54:02 -08002565 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002566
2567 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002568}
2569
Chet Haase48659092012-05-31 15:21:51 -07002570status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guyada830f2011-01-13 12:13:20 -08002571 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Chet Haase48659092012-05-31 15:21:51 -07002572 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002573 }
2574
Romain Guy4ff0cf42012-08-06 14:51:10 -07002575 bool debugLayerUpdate = false;
2576
Romain Guy2bf68f02012-03-02 13:37:47 -08002577 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2578 OpenGLRenderer* renderer = layer->renderer;
2579 Rect& dirty = layer->dirtyRect;
2580
2581 interrupt();
2582 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2583 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002584 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002585 renderer->finish();
2586 resume();
2587
2588 dirty.setEmpty();
2589 layer->deferredUpdateScheduled = false;
2590 layer->renderer = NULL;
2591 layer->displayList = NULL;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002592
2593 debugLayerUpdate = mCaches.debugLayersUpdates;
Romain Guy2bf68f02012-03-02 13:37:47 -08002594 }
2595
Romain Guya1d3c912011-12-13 14:55:06 -08002596 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002597
Romain Guy211370f2012-02-01 16:10:55 -08002598 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002599 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002600 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002601 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002602 const float a = layer->getAlpha() / 255.0f;
2603 SkiaColorFilter *oldFilter = mColorFilter;
2604 mColorFilter = layer->getColorFilter();
Romain Guyf219da52011-01-16 12:54:25 -08002605
Romain Guyc88e3572011-01-22 00:32:12 -08002606 setupDraw();
2607 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002608 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002609 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002610 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002611 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002612 setupDrawPureColorUniforms();
2613 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002614 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002615 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002616 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2617 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002618
Romain Guyd21b6e12011-11-30 20:21:23 -08002619 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002620 setupDrawModelViewTranslate(tx, ty,
2621 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002622 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002623 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002624 setupDrawModelViewTranslate(x, y,
2625 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2626 }
Romain Guyc88e3572011-01-22 00:32:12 -08002627 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002628
Romain Guyc88e3572011-01-22 00:32:12 -08002629 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2630 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002631
Romain Guyc88e3572011-01-22 00:32:12 -08002632 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002633
Chet Haased15ebf22012-09-05 11:40:29 -07002634 mColorFilter = oldFilter;
2635
Romain Guy3a3133d2011-02-01 22:59:58 -08002636#if DEBUG_LAYERS_AS_REGIONS
2637 drawRegionRects(layer->region);
2638#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002639 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002640
2641 if (debugLayerUpdate) {
2642 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2643 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2644 }
Romain Guyf219da52011-01-16 12:54:25 -08002645 }
Chet Haase48659092012-05-31 15:21:51 -07002646
2647 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002648}
2649
Romain Guy6926c722010-07-12 20:20:03 -07002650///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002651// Shaders
2652///////////////////////////////////////////////////////////////////////////////
2653
2654void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002655 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002656}
2657
Romain Guy06f96e22010-07-30 19:18:16 -07002658void OpenGLRenderer::setupShader(SkiaShader* shader) {
2659 mShader = shader;
2660 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002661 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002662 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002663}
2664
Romain Guyd27977d2010-07-14 19:18:51 -07002665///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002666// Color filters
2667///////////////////////////////////////////////////////////////////////////////
2668
2669void OpenGLRenderer::resetColorFilter() {
2670 mColorFilter = NULL;
2671}
2672
2673void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2674 mColorFilter = filter;
2675}
2676
2677///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002678// Drop shadow
2679///////////////////////////////////////////////////////////////////////////////
2680
2681void OpenGLRenderer::resetShadow() {
2682 mHasShadow = false;
2683}
2684
2685void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2686 mHasShadow = true;
2687 mShadowRadius = radius;
2688 mShadowDx = dx;
2689 mShadowDy = dy;
2690 mShadowColor = color;
2691}
2692
2693///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002694// Draw filters
2695///////////////////////////////////////////////////////////////////////////////
2696
2697void OpenGLRenderer::resetPaintFilter() {
2698 mHasDrawFilter = false;
2699}
2700
2701void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2702 mHasDrawFilter = true;
2703 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2704 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2705}
2706
2707SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002708 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002709
2710 uint32_t flags = paint->getFlags();
2711
2712 mFilteredPaint = *paint;
2713 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2714
2715 return &mFilteredPaint;
2716}
2717
2718///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002719// Drawing implementation
2720///////////////////////////////////////////////////////////////////////////////
2721
Romain Guy01d58e42011-01-19 21:54:02 -08002722void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2723 float x, float y, SkPaint* paint) {
2724 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2725 return;
2726 }
2727
2728 int alpha;
2729 SkXfermode::Mode mode;
2730 getAlphaAndMode(paint, &alpha, &mode);
2731
2732 setupDraw();
2733 setupDrawWithTexture(true);
2734 setupDrawAlpha8Color(paint->getColor(), alpha);
2735 setupDrawColorFilter();
2736 setupDrawShader();
2737 setupDrawBlending(true, mode);
2738 setupDrawProgram();
2739 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2740 setupDrawTexture(texture->id);
2741 setupDrawPureColorUniforms();
2742 setupDrawColorFilterUniforms();
2743 setupDrawShaderUniforms();
2744 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2745
2746 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2747
2748 finishDrawTexture();
2749}
2750
Romain Guyf607bdc2010-09-10 19:20:06 -07002751// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002752#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2753#define kStdUnderline_Offset (1.0f / 9.0f)
2754#define kStdUnderline_Thickness (1.0f / 18.0f)
2755
2756void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2757 float x, float y, SkPaint* paint) {
2758 // Handle underline and strike-through
2759 uint32_t flags = paint->getFlags();
2760 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002761 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002762 float underlineWidth = length;
2763 // If length is > 0.0f, we already measured the text for the text alignment
2764 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002765 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002766 }
2767
Romain Guy211370f2012-02-01 16:10:55 -08002768 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002769 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002770 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002771
Raph Levien8b4072d2012-07-30 15:50:00 -07002772 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002773 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002774
Romain Guyf6834472011-01-23 13:32:12 -08002775 int linesCount = 0;
2776 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2777 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2778
2779 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002780 float points[pointsCount];
2781 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002782
2783 if (flags & SkPaint::kUnderlineText_Flag) {
2784 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002785 points[currentPoint++] = left;
2786 points[currentPoint++] = top;
2787 points[currentPoint++] = left + underlineWidth;
2788 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002789 }
2790
2791 if (flags & SkPaint::kStrikeThruText_Flag) {
2792 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002793 points[currentPoint++] = left;
2794 points[currentPoint++] = top;
2795 points[currentPoint++] = left + underlineWidth;
2796 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002797 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002798
Romain Guy726aeba2011-06-01 14:52:00 -07002799 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002800
Romain Guy726aeba2011-06-01 14:52:00 -07002801 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002802 }
2803 }
2804}
2805
Romain Guy026c5e162010-06-28 17:12:22 -07002806void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002807 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002808 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002809 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002810 color |= 0x00ffffff;
2811 }
2812
Romain Guy70ca14e2010-12-13 18:24:33 -08002813 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002814 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002815 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002816 setupDrawShader();
2817 setupDrawColorFilter();
2818 setupDrawBlending(mode);
2819 setupDrawProgram();
2820 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2821 setupDrawColorUniforms();
2822 setupDrawShaderUniforms(ignoreTransform);
2823 setupDrawColorFilterUniforms();
2824 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002825
Romain Guyc95c8d62010-09-17 15:31:32 -07002826 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2827}
2828
Romain Guy82ba8142010-07-09 13:25:56 -07002829void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002830 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002831 int alpha;
2832 SkXfermode::Mode mode;
2833 getAlphaAndMode(paint, &alpha, &mode);
2834
Romain Guyd21b6e12011-11-30 20:21:23 -08002835 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002836
Romain Guy211370f2012-02-01 16:10:55 -08002837 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002838 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2839 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2840
Romain Guyd21b6e12011-11-30 20:21:23 -08002841 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002842 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2843 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2844 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2845 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002846 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002847 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2848 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2849 GL_TRIANGLE_STRIP, gMeshCount);
2850 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002851}
2852
Romain Guybd6b79b2010-06-26 00:13:53 -07002853void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002854 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2855 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002856 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002857}
2858
2859void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002860 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002861 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002862 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002863
Romain Guy746b7402010-10-26 16:27:31 -07002864 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002865 setupDrawWithTexture();
2866 setupDrawColor(alpha, alpha, alpha, alpha);
2867 setupDrawColorFilter();
2868 setupDrawBlending(blend, mode, swapSrcDst);
2869 setupDrawProgram();
2870 if (!dirty) {
2871 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002872 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002873 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002874 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002875 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002876 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002877 }
Romain Guy86568192010-12-14 15:55:39 -08002878 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002879 setupDrawColorFilterUniforms();
2880 setupDrawTexture(texture);
2881 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002882
Romain Guy6820ac82010-09-15 18:11:50 -07002883 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002884
2885 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002886}
2887
Romain Guya5aed0d2010-09-09 14:42:43 -07002888void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002889 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002890 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002891
Romain Guy82ba8142010-07-09 13:25:56 -07002892 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002893 // These blend modes are not supported by OpenGL directly and have
2894 // to be implemented using shaders. Since the shader will perform
2895 // the blending, turn blending off here
2896 // If the blend mode cannot be implemented using shaders, fall
2897 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07002898 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08002899 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002900 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002901 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002902
Romain Guy82bc7a72012-01-03 14:13:39 -08002903 if (mCaches.blend) {
2904 glDisable(GL_BLEND);
2905 mCaches.blend = false;
2906 }
2907
2908 return;
2909 } else {
2910 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002911 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002912 }
2913
2914 if (!mCaches.blend) {
2915 glEnable(GL_BLEND);
2916 }
2917
2918 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2919 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2920
2921 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2922 glBlendFunc(sourceMode, destMode);
2923 mCaches.lastSrcMode = sourceMode;
2924 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002925 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002926 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002927 glDisable(GL_BLEND);
2928 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002929 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002930}
2931
Romain Guy889f8d12010-07-29 14:37:42 -07002932bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002933 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002934 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002935 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002936 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002937 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002938 }
Romain Guy6926c722010-07-12 20:20:03 -07002939 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002940}
2941
Romain Guy026c5e162010-06-28 17:12:22 -07002942void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002943 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002944 TextureVertex::setUV(v++, u1, v1);
2945 TextureVertex::setUV(v++, u2, v1);
2946 TextureVertex::setUV(v++, u1, v2);
2947 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002948}
2949
Chet Haase5c13d892010-10-08 08:37:55 -07002950void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002951 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07002952 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002953}
2954
Romain Guy9d5316e2010-06-24 19:30:36 -07002955}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002956}; // namespace android