blob: 4aefcbaeb62ea831ed87c01e2dc81a97b0a272f6 [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
Romain Guy45e4c3d2012-09-11 17:17:07 -0700176 // If we know that we are going to redraw the entire framebuffer,
177 // perform a discard to let the driver know we don't need to preserve
178 // the back buffer for this frame.
179 if (mCaches.extensions.hasDiscardFramebuffer() &&
180 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
181 const GLenum attachments[] = { getTargetFbo() == 0 ? GL_COLOR_EXT : GL_COLOR_ATTACHMENT0 };
182 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
183 }
184
Romain Guyddf74372012-05-22 14:07:07 -0700185 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800186
Romain Guy6b7bd242010-10-06 19:49:23 -0700187 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700188 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700189 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700190 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700191 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700192 } else {
193 mCaches.resetScissor();
194 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700195
196 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700197}
198
199void OpenGLRenderer::syncState() {
200 glViewport(0, 0, mWidth, mHeight);
201
202 if (mCaches.blend) {
203 glEnable(GL_BLEND);
204 } else {
205 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700206 }
Romain Guybb9524b2010-06-22 18:56:38 -0700207}
208
Romain Guyb025b9c2010-09-16 14:16:48 -0700209void OpenGLRenderer::finish() {
210#if DEBUG_OPENGL
211 GLenum status = GL_NO_ERROR;
212 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000213 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800214 switch (status) {
Romain Guya44a63a2012-04-26 14:05:02 -0700215 case GL_INVALID_ENUM:
216 ALOGE(" GL_INVALID_ENUM");
217 break;
218 case GL_INVALID_VALUE:
219 ALOGE(" GL_INVALID_VALUE");
220 break;
221 case GL_INVALID_OPERATION:
222 ALOGE(" GL_INVALID_OPERATION");
223 break;
Romain Guya07105b2011-01-10 21:14:18 -0800224 case GL_OUT_OF_MEMORY:
Romain Guya44a63a2012-04-26 14:05:02 -0700225 ALOGE(" Out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800226 break;
227 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700228 }
229#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800230#if DEBUG_MEMORY_USAGE
231 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800232#else
233 if (mCaches.getDebugLevel() & kDebugMemory) {
234 mCaches.dumpMemoryUsage();
235 }
Romain Guyc15008e2010-11-10 11:59:15 -0800236#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700237}
238
Romain Guy6c319ca2011-01-11 14:29:25 -0800239void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700240 if (mCaches.currentProgram) {
241 if (mCaches.currentProgram->isInUse()) {
242 mCaches.currentProgram->remove();
243 mCaches.currentProgram = NULL;
244 }
245 }
Romain Guy50c0f092010-10-19 11:42:22 -0700246 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800247 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800248 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800249 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700250}
251
Romain Guy6c319ca2011-01-11 14:29:25 -0800252void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800253 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
254
255 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800256 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
257
Chet Haase80250612012-08-15 13:46:54 -0700258 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700259 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800260 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700261 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700262
Romain Guya1d3c912011-12-13 14:55:06 -0800263 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800264 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700265
Romain Guy50c0f092010-10-19 11:42:22 -0700266 mCaches.blend = true;
267 glEnable(GL_BLEND);
268 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
269 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700270}
271
Romain Guyba6be8a2012-04-23 18:22:09 -0700272void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700273 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700274}
275
276void OpenGLRenderer::attachFunctor(Functor* functor) {
277 mFunctors.add(functor);
278}
279
Romain Guy8f3b8e32012-03-27 16:33:45 -0700280status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
281 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700282 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700283
Romain Guyba6be8a2012-04-23 18:22:09 -0700284 if (count > 0) {
285 SortedVector<Functor*> functors(mFunctors);
286 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700287
Romain Guyba6be8a2012-04-23 18:22:09 -0700288 DrawGlInfo info;
289 info.clipLeft = 0;
290 info.clipTop = 0;
291 info.clipRight = 0;
292 info.clipBottom = 0;
293 info.isLayer = false;
294 info.width = 0;
295 info.height = 0;
296 memset(info.transform, 0, sizeof(float) * 16);
297
298 for (size_t i = 0; i < count; i++) {
299 Functor* f = functors.itemAt(i);
300 result |= (*f)(DrawGlInfo::kModeProcess, &info);
301
Chris Craikc2c95432012-04-25 15:13:52 -0700302 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700303 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
304 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700305 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700306
Chris Craikc2c95432012-04-25 15:13:52 -0700307 if (result & DrawGlInfo::kStatusInvoke) {
308 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700309 }
310 }
Chet Haasebeb8bd02012-09-09 16:13:26 -0700311 // protect against functors binding to other buffers
312 mCaches.unbindMeshBuffer();
313 mCaches.unbindIndicesBuffer();
314 mCaches.activeTexture(0);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700315 }
316
317 return result;
318}
319
320status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800321 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700322 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700323
Romain Guy8a4ac612012-07-17 17:32:48 -0700324 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800325 if (mDirtyClip) {
326 setScissorFromClip();
327 }
Romain Guyd643bb52011-03-01 14:55:21 -0800328
Romain Guy80911b82011-03-16 15:30:12 -0700329 Rect clip(*mSnapshot->clipRect);
330 clip.snapToPixelBoundaries();
331
Romain Guyd643bb52011-03-01 14:55:21 -0800332 // Since we don't know what the functor will draw, let's dirty
333 // tne entire clip region
334 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800335 dirtyLayerUnchecked(clip, getRegion());
336 }
Romain Guyd643bb52011-03-01 14:55:21 -0800337
Romain Guy08aa2cb2011-03-17 11:06:57 -0700338 DrawGlInfo info;
339 info.clipLeft = clip.left;
340 info.clipTop = clip.top;
341 info.clipRight = clip.right;
342 info.clipBottom = clip.bottom;
343 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700344 info.width = getSnapshot()->viewport.getWidth();
345 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700346 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700347
Chet Haase48659092012-05-31 15:21:51 -0700348 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800349
Romain Guy8f3b8e32012-03-27 16:33:45 -0700350 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700351 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800352 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700353
Chris Craik65924a32012-04-05 17:52:11 -0700354 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700355 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700356 }
Romain Guycabfcc12011-03-07 18:06:46 -0800357 }
358
Chet Haasedaf98e92011-01-10 14:10:36 -0800359 resume();
Romain Guy65549432012-03-26 16:45:05 -0700360 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800361}
362
Romain Guyf6a11b82010-06-23 17:47:49 -0700363///////////////////////////////////////////////////////////////////////////////
364// State management
365///////////////////////////////////////////////////////////////////////////////
366
Romain Guybb9524b2010-06-22 18:56:38 -0700367int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700368 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700369}
370
371int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700372 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700373}
374
375void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700376 if (mSaveCount > 1) {
377 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700378 }
Romain Guybb9524b2010-06-22 18:56:38 -0700379}
380
381void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700382 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700383
Romain Guy8fb95422010-08-17 18:38:51 -0700384 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700385 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700386 }
Romain Guybb9524b2010-06-22 18:56:38 -0700387}
388
Romain Guy8aef54f2010-09-01 15:13:49 -0700389int OpenGLRenderer::saveSnapshot(int flags) {
390 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700391 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700392}
393
394bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700395 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700396 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700397 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700398
Romain Guybd6b79b2010-06-26 00:13:53 -0700399 sp<Snapshot> current = mSnapshot;
400 sp<Snapshot> previous = mSnapshot->previous;
401
Romain Guyeb993562010-10-05 18:14:38 -0700402 if (restoreOrtho) {
403 Rect& r = previous->viewport;
404 glViewport(r.left, r.top, r.right, r.bottom);
405 mOrthoMatrix.load(current->orthoMatrix);
406 }
407
Romain Guy8b55f372010-08-18 17:10:07 -0700408 mSaveCount--;
409 mSnapshot = previous;
410
Romain Guy2542d192010-08-18 11:47:12 -0700411 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700412 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700413 }
Romain Guy2542d192010-08-18 11:47:12 -0700414
Romain Guy5ec99242010-11-03 16:19:08 -0700415 if (restoreLayer) {
416 composeLayer(current, previous);
417 }
418
Romain Guy2542d192010-08-18 11:47:12 -0700419 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700420}
421
Romain Guyf6a11b82010-06-23 17:47:49 -0700422///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700423// Layers
424///////////////////////////////////////////////////////////////////////////////
425
426int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700427 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700428 const GLuint previousFbo = mSnapshot->fbo;
429 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700430
Romain Guyaf636eb2010-12-09 17:47:21 -0800431 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700432 int alpha = 255;
433 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700434
Romain Guye45362c2010-11-03 19:58:32 -0700435 if (p) {
436 alpha = p->getAlpha();
437 if (!mCaches.extensions.hasFramebufferFetch()) {
438 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
439 if (!isMode) {
440 // Assume SRC_OVER
441 mode = SkXfermode::kSrcOver_Mode;
442 }
443 } else {
444 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700445 }
446 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700447 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700448 }
Romain Guyd55a8612010-06-28 17:42:46 -0700449
Chet Haased48885a2012-08-28 17:43:28 -0700450 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700451 }
Romain Guyd55a8612010-06-28 17:42:46 -0700452
453 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700454}
455
456int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
457 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700458 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700459 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700460 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700461 SkPaint paint;
462 paint.setAlpha(alpha);
463 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700464 }
Romain Guyd55a8612010-06-28 17:42:46 -0700465}
Romain Guybd6b79b2010-06-26 00:13:53 -0700466
Romain Guy1c740bc2010-09-13 18:00:09 -0700467/**
468 * Layers are viewed by Skia are slightly different than layers in image editing
469 * programs (for instance.) When a layer is created, previously created layers
470 * and the frame buffer still receive every drawing command. For instance, if a
471 * layer is created and a shape intersecting the bounds of the layers and the
472 * framebuffer is draw, the shape will be drawn on both (unless the layer was
473 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
474 *
475 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
476 * texture. Unfortunately, this is inefficient as it requires every primitive to
477 * be drawn n + 1 times, where n is the number of active layers. In practice this
478 * means, for every primitive:
479 * - Switch active frame buffer
480 * - Change viewport, clip and projection matrix
481 * - Issue the drawing
482 *
483 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700484 * To avoid this, layers are implemented in a different way here, at least in the
485 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
486 * is set. When this flag is set we can redirect all drawing operations into a
487 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700488 *
489 * This implementation relies on the frame buffer being at least RGBA 8888. When
490 * a layer is created, only a texture is created, not an FBO. The content of the
491 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700492 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700493 * buffer and drawing continues as normal. This technique therefore treats the
494 * frame buffer as a scratch buffer for the layers.
495 *
496 * To compose the layers back onto the frame buffer, each layer texture
497 * (containing the original frame buffer data) is drawn as a simple quad over
498 * the frame buffer. The trick is that the quad is set as the composition
499 * destination in the blending equation, and the frame buffer becomes the source
500 * of the composition.
501 *
502 * Drawing layers with an alpha value requires an extra step before composition.
503 * An empty quad is drawn over the layer's region in the frame buffer. This quad
504 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
505 * quad is used to multiply the colors in the frame buffer. This is achieved by
506 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
507 * GL_ZERO, GL_SRC_ALPHA.
508 *
509 * Because glCopyTexImage2D() can be slow, an alternative implementation might
510 * be use to draw a single clipped layer. The implementation described above
511 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700512 *
513 * (1) The frame buffer is actually not cleared right away. To allow the GPU
514 * to potentially optimize series of calls to glCopyTexImage2D, the frame
515 * buffer is left untouched until the first drawing operation. Only when
516 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700517 */
Chet Haased48885a2012-08-28 17:43:28 -0700518bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
519 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700520 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700521 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700522
Romain Guyeb993562010-10-05 18:14:38 -0700523 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
524
Romain Guyf607bdc2010-09-10 19:20:06 -0700525 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700526 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700527 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700528 Rect untransformedBounds(bounds);
529 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700530
Chet Haased48885a2012-08-28 17:43:28 -0700531 // Layers only make sense if they are in the framebuffer's bounds
532 if (bounds.intersect(*mSnapshot->clipRect)) {
533 // We cannot work with sub-pixels in this case
534 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700535
Chet Haased48885a2012-08-28 17:43:28 -0700536 // When the layer is not an FBO, we may use glCopyTexImage so we
537 // need to make sure the layer does not extend outside the bounds
538 // of the framebuffer
539 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700540 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700541 } else if (fboLayer) {
542 clip.set(bounds);
543 mat4 inverse;
544 inverse.loadInverse(*mSnapshot->transform);
545 inverse.mapRect(clip);
546 clip.snapToPixelBoundaries();
547 if (clip.intersect(untransformedBounds)) {
548 clip.translate(-left, -top);
549 bounds.set(untransformedBounds);
550 } else {
551 clip.setEmpty();
552 }
Romain Guyad37cd32011-03-15 11:12:25 -0700553 }
Chet Haased48885a2012-08-28 17:43:28 -0700554 } else {
555 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700556 }
Romain Guybf434112010-09-16 14:40:17 -0700557
Romain Guy746b7402010-10-26 16:27:31 -0700558 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700559 bounds.getHeight() > mCaches.maxTextureSize ||
560 (fboLayer && clip.isEmpty())) {
561 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700562 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700563 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700564 }
565
566 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700567 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700568 return false;
569 }
Romain Guyf18fd992010-07-08 11:45:51 -0700570
Romain Guya1d3c912011-12-13 14:55:06 -0800571 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700572 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700573 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700574 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700575 }
576
Romain Guy9ace8f52011-07-07 20:50:11 -0700577 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700578 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700579 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
580 bounds.getWidth() / float(layer->getWidth()), 0.0f);
581 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700582 layer->setBlend(true);
Romain Guydda570202010-07-06 11:39:32 -0700583
Romain Guy8fb95422010-08-17 18:38:51 -0700584 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700585 mSnapshot->flags |= Snapshot::kFlagIsLayer;
586 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700587
Romain Guyeb993562010-10-05 18:14:38 -0700588 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700589 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700590 } else {
591 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700592 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800593 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700594 if (layer->isEmpty()) {
595 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700596 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700597 layer->getWidth(), layer->getHeight(), 0);
598 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800599 } else {
600 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700601 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800602 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700603
Romain Guy54be1cd2011-06-13 19:04:27 -0700604 // Enqueue the buffer coordinates to clear the corresponding region later
605 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700606 }
Romain Guyeb993562010-10-05 18:14:38 -0700607 }
Romain Guyf86ef572010-07-01 11:05:42 -0700608
Romain Guyd55a8612010-06-28 17:42:46 -0700609 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700610}
611
Chet Haased48885a2012-08-28 17:43:28 -0700612bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700613 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700614
Chet Haased48885a2012-08-28 17:43:28 -0700615 mSnapshot->region = &mSnapshot->layer->region;
616 mSnapshot->flags |= Snapshot::kFlagFboTarget;
Romain Guy5b3b3522010-10-27 18:57:51 -0700617
Chet Haased48885a2012-08-28 17:43:28 -0700618 mSnapshot->flags |= Snapshot::kFlagIsFboLayer;
619 mSnapshot->fbo = layer->getFbo();
620 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
621 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
622 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
623 mSnapshot->height = bounds.getHeight();
624 mSnapshot->flags |= Snapshot::kFlagDirtyOrtho;
625 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700626
627 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700628 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
629 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700630
631 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700632 if (layer->isEmpty()) {
633 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
634 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700635 }
636
637 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700638 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700639
640#if DEBUG_LAYERS_AS_REGIONS
641 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
642 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000643 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700644
645 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy5b3b3522010-10-27 18:57:51 -0700646
Chet Haase603f6de2012-09-14 15:31:25 -0700647 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy5b3b3522010-10-27 18:57:51 -0700648
649 return false;
650 }
651#endif
652
653 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700654 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800655 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700656 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700657 glClear(GL_COLOR_BUFFER_BIT);
658
659 dirtyClip();
660
661 // Change the ortho projection
662 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
663 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
664
665 return true;
666}
667
Romain Guy1c740bc2010-09-13 18:00:09 -0700668/**
669 * Read the documentation of createLayer() before doing anything in this method.
670 */
Romain Guy1d83e192010-08-17 11:37:00 -0700671void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
672 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000673 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700674 return;
675 }
676
Romain Guy5b3b3522010-10-27 18:57:51 -0700677 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700678
679 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700680 // Detach the texture from the FBO
681 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
682
Romain Guyeb993562010-10-05 18:14:38 -0700683 // Unbind current FBO and restore previous one
684 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
685 }
686
Romain Guy1d83e192010-08-17 11:37:00 -0700687 Layer* layer = current->layer;
688 const Rect& rect = layer->layer;
689
Romain Guy9ace8f52011-07-07 20:50:11 -0700690 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700691 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700692 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700693 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700694 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700695 }
696
Romain Guy03750a02010-10-18 14:06:08 -0700697 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700698
Romain Guya1d3c912011-12-13 14:55:06 -0800699 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700700
Romain Guy5b3b3522010-10-27 18:57:51 -0700701 // When the layer is stored in an FBO, we can save a bit of fillrate by
702 // drawing only the dirty region
703 if (fboLayer) {
704 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700705 if (layer->getColorFilter()) {
706 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800707 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700708 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700709 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800710 resetColorFilter();
711 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700712 } else if (!rect.isEmpty()) {
713 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
714 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700715 }
Romain Guy8b55f372010-08-18 17:10:07 -0700716
Romain Guyeb993562010-10-05 18:14:38 -0700717 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800718 // Note: No need to use glDiscardFramebufferEXT() since we never
719 // create/compose layers that are not on screen with this
720 // code path
721 // See LayerRenderer::destroyLayer(Layer*)
722
Romain Guyeb993562010-10-05 18:14:38 -0700723 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
724 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700725 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700726 }
727
Romain Guy746b7402010-10-26 16:27:31 -0700728 dirtyClip();
729
Romain Guyeb993562010-10-05 18:14:38 -0700730 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700731 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700732 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700733 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700734 }
735}
736
Romain Guyaa6c24c2011-04-28 18:40:04 -0700737void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700738 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700739
Romain Guy302a9df2011-08-16 13:55:02 -0700740 mat4& transform = layer->getTransform();
741 if (!transform.isIdentity()) {
742 save(0);
743 mSnapshot->transform->multiply(transform);
744 }
745
Romain Guyaa6c24c2011-04-28 18:40:04 -0700746 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700747 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700748 setupDrawWithTexture();
749 } else {
750 setupDrawWithExternalTexture();
751 }
752 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700753 setupDrawColor(alpha, alpha, alpha, alpha);
754 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700755 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700756 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700757 setupDrawPureColorUniforms();
758 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700759 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
760 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700761 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700762 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700763 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700764 if (mSnapshot->transform->isPureTranslate() &&
765 layer->getWidth() == (uint32_t) rect.getWidth() &&
766 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700767 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
768 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
769
Romain Guyd21b6e12011-11-30 20:21:23 -0800770 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700771 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
772 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800773 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700774 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
775 }
776 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700777 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
778
779 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
780
781 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700782
783 if (!transform.isIdentity()) {
784 restore();
785 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700786}
787
Romain Guy5b3b3522010-10-27 18:57:51 -0700788void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700789 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700790 const Rect& texCoords = layer->texCoords;
791 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
792 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700793
Romain Guy9ace8f52011-07-07 20:50:11 -0700794 float x = rect.left;
795 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700796 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700797 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700798 layer->getHeight() == (uint32_t) rect.getHeight();
799
800 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700801 // When we're swapping, the layer is already in screen coordinates
802 if (!swap) {
803 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
804 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
805 }
806
Romain Guyd21b6e12011-11-30 20:21:23 -0800807 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700808 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800809 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700810 }
811
812 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
813 layer->getTexture(), layer->getAlpha() / 255.0f,
814 layer->getMode(), layer->isBlend(),
815 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
816 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700817
Romain Guyaa6c24c2011-04-28 18:40:04 -0700818 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
819 } else {
820 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
821 drawTextureLayer(layer, rect);
822 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
823 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700824}
825
826void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700827 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700828 layer->setRegionAsRect();
829
Romain Guy40667672011-03-18 14:34:03 -0700830 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700831
Romain Guy5b3b3522010-10-27 18:57:51 -0700832 layer->region.clear();
833 return;
834 }
835
Romain Guy8a3957d2011-09-07 17:55:15 -0700836 // TODO: See LayerRenderer.cpp::generateMesh() for important
837 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800838 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700839 size_t count;
840 const android::Rect* rects = layer->region.getArray(&count);
841
Romain Guy9ace8f52011-07-07 20:50:11 -0700842 const float alpha = layer->getAlpha() / 255.0f;
843 const float texX = 1.0f / float(layer->getWidth());
844 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800845 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700846
847 TextureVertex* mesh = mCaches.getRegionMesh();
848 GLsizei numQuads = 0;
849
Romain Guy7230a742011-01-10 22:26:16 -0800850 setupDraw();
851 setupDrawWithTexture();
852 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800853 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700854 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800855 setupDrawProgram();
856 setupDrawDirtyRegionsDisabled();
857 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800858 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700859 setupDrawTexture(layer->getTexture());
860 if (mSnapshot->transform->isPureTranslate()) {
861 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
862 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
863
Romain Guyd21b6e12011-11-30 20:21:23 -0800864 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700865 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
866 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800867 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700868 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
869 }
Romain Guy15bc6432011-12-13 13:11:32 -0800870 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700871
872 for (size_t i = 0; i < count; i++) {
873 const android::Rect* r = &rects[i];
874
875 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800876 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700877 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800878 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700879
880 // TODO: Reject quads outside of the clip
881 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
882 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
883 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
884 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
885
886 numQuads++;
887
888 if (numQuads >= REGION_MESH_QUAD_COUNT) {
889 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
890 numQuads = 0;
891 mesh = mCaches.getRegionMesh();
892 }
893 }
894
895 if (numQuads > 0) {
896 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
897 }
898
Romain Guy7230a742011-01-10 22:26:16 -0800899 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700900
901#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800902 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700903#endif
904
905 layer->region.clear();
906 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700907}
908
Romain Guy3a3133d2011-02-01 22:59:58 -0800909void OpenGLRenderer::drawRegionRects(const Region& region) {
910#if DEBUG_LAYERS_AS_REGIONS
911 size_t count;
912 const android::Rect* rects = region.getArray(&count);
913
914 uint32_t colors[] = {
915 0x7fff0000, 0x7f00ff00,
916 0x7f0000ff, 0x7fff00ff,
917 };
918
919 int offset = 0;
920 int32_t top = rects[0].top;
921
922 for (size_t i = 0; i < count; i++) {
923 if (top != rects[i].top) {
924 offset ^= 0x2;
925 top = rects[i].top;
926 }
927
928 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
929 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
930 SkXfermode::kSrcOver_Mode);
931 }
932#endif
933}
934
Romain Guy5b3b3522010-10-27 18:57:51 -0700935void OpenGLRenderer::dirtyLayer(const float left, const float top,
936 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -0800937 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700938 Rect bounds(left, top, right, bottom);
939 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800940 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700941 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700942}
943
944void OpenGLRenderer::dirtyLayer(const float left, const float top,
945 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -0800946 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700947 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800948 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800949 }
Romain Guy1bd1bad2011-01-14 20:07:20 -0800950}
951
952void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -0800953 if (bounds.intersect(*mSnapshot->clipRect)) {
954 bounds.snapToPixelBoundaries();
955 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
956 if (!dirty.isEmpty()) {
957 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700958 }
959 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700960}
961
Romain Guy54be1cd2011-06-13 19:04:27 -0700962void OpenGLRenderer::clearLayerRegions() {
963 const size_t count = mLayers.size();
964 if (count == 0) return;
965
966 if (!mSnapshot->isIgnored()) {
967 // Doing several glScissor/glClear here can negatively impact
968 // GPUs with a tiler architecture, instead we draw quads with
969 // the Clear blending mode
970
971 // The list contains bounds that have already been clipped
972 // against their initial clip rect, and the current clip
973 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -0700974 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -0700975
976 Vertex mesh[count * 6];
977 Vertex* vertex = mesh;
978
979 for (uint32_t i = 0; i < count; i++) {
980 Rect* bounds = mLayers.itemAt(i);
981
982 Vertex::set(vertex++, bounds->left, bounds->bottom);
983 Vertex::set(vertex++, bounds->left, bounds->top);
984 Vertex::set(vertex++, bounds->right, bounds->top);
985 Vertex::set(vertex++, bounds->left, bounds->bottom);
986 Vertex::set(vertex++, bounds->right, bounds->top);
987 Vertex::set(vertex++, bounds->right, bounds->bottom);
988
989 delete bounds;
990 }
991
992 setupDraw(false);
993 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
994 setupDrawBlending(true, SkXfermode::kClear_Mode);
995 setupDrawProgram();
996 setupDrawPureColorUniforms();
997 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800998 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700999
Romain Guy54be1cd2011-06-13 19:04:27 -07001000 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001001
1002 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001003 } else {
1004 for (uint32_t i = 0; i < count; i++) {
1005 delete mLayers.itemAt(i);
1006 }
1007 }
1008
1009 mLayers.clear();
1010}
1011
Romain Guybd6b79b2010-06-26 00:13:53 -07001012///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001013// Transforms
1014///////////////////////////////////////////////////////////////////////////////
1015
1016void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001017 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001018}
1019
1020void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001021 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001022}
1023
1024void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001025 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001026}
1027
Romain Guy807daf72011-01-18 11:19:19 -08001028void OpenGLRenderer::skew(float sx, float sy) {
1029 mSnapshot->transform->skew(sx, sy);
1030}
1031
Romain Guyf6a11b82010-06-23 17:47:49 -07001032void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001033 if (matrix) {
1034 mSnapshot->transform->load(*matrix);
1035 } else {
1036 mSnapshot->transform->loadIdentity();
1037 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001038}
1039
1040void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001041 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001042}
1043
1044void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001045 SkMatrix transform;
1046 mSnapshot->transform->copyTo(transform);
1047 transform.preConcat(*matrix);
1048 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001049}
1050
1051///////////////////////////////////////////////////////////////////////////////
1052// Clipping
1053///////////////////////////////////////////////////////////////////////////////
1054
Romain Guybb9524b2010-06-22 18:56:38 -07001055void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001056 Rect clip(*mSnapshot->clipRect);
1057 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001058
Romain Guy8a4ac612012-07-17 17:32:48 -07001059 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1060 clip.getWidth(), clip.getHeight())) {
1061 mDirtyClip = false;
1062 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001063}
1064
1065const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001066 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001067}
1068
Romain Guy8a4ac612012-07-17 17:32:48 -07001069bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1070 if (mSnapshot->isIgnored()) {
1071 return true;
1072 }
1073
1074 Rect r(left, top, right, bottom);
1075 mSnapshot->transform->mapRect(r);
1076 r.snapToPixelBoundaries();
1077
1078 Rect clipRect(*mSnapshot->clipRect);
1079 clipRect.snapToPixelBoundaries();
1080
1081 return !clipRect.intersects(r);
1082}
1083
Romain Guyc7d53492010-06-25 13:41:57 -07001084bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001085 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001086 return true;
1087 }
1088
Romain Guy1d83e192010-08-17 11:37:00 -07001089 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001090 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001091 r.snapToPixelBoundaries();
1092
1093 Rect clipRect(*mSnapshot->clipRect);
1094 clipRect.snapToPixelBoundaries();
1095
Romain Guy586cae32012-07-13 15:28:31 -07001096 bool rejected = !clipRect.intersects(r);
1097 if (!isDeferred() && !rejected) {
1098 mCaches.setScissorEnabled(!clipRect.contains(r));
1099 }
1100
1101 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001102}
1103
Romain Guy079ba2c2010-07-16 14:12:24 -07001104bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1105 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001106 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001107 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001108 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001109 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001110}
1111
Chet Haasea23eed82012-04-12 15:19:04 -07001112Rect* OpenGLRenderer::getClipRect() {
1113 return mSnapshot->clipRect;
1114}
1115
Romain Guyf6a11b82010-06-23 17:47:49 -07001116///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001117// Drawing commands
1118///////////////////////////////////////////////////////////////////////////////
1119
Romain Guy54be1cd2011-06-13 19:04:27 -07001120void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001121 // TODO: It would be best if we could do this before quickReject()
1122 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001123 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001124 if (mDirtyClip) {
1125 setScissorFromClip();
1126 }
1127 mDescription.reset();
1128 mSetShaderColor = false;
1129 mColorSet = false;
1130 mColorA = mColorR = mColorG = mColorB = 0.0f;
1131 mTextureUnit = 0;
1132 mTrackDirtyRegions = true;
1133}
1134
1135void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1136 mDescription.hasTexture = true;
1137 mDescription.hasAlpha8Texture = isAlpha8;
1138}
1139
Romain Guyaa6c24c2011-04-28 18:40:04 -07001140void OpenGLRenderer::setupDrawWithExternalTexture() {
1141 mDescription.hasExternalTexture = true;
1142}
1143
Romain Guy15bc6432011-12-13 13:11:32 -08001144void OpenGLRenderer::setupDrawNoTexture() {
1145 mCaches.disbaleTexCoordsVertexArray();
1146}
1147
Chet Haase5b0200b2011-04-13 17:58:08 -07001148void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001149 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001150}
1151
Chris Craik6ebdc112012-08-31 18:24:33 -07001152void OpenGLRenderer::setupDrawAARect() {
1153 mDescription.isAARect = true;
1154}
1155
Romain Guyed6fcb02011-03-21 13:11:28 -07001156void OpenGLRenderer::setupDrawPoint(float pointSize) {
1157 mDescription.isPoint = true;
1158 mDescription.pointSize = pointSize;
1159}
1160
Romain Guy70ca14e2010-12-13 18:24:33 -08001161void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001162 setupDrawColor(color, (color >> 24) & 0xFF);
1163}
1164
1165void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1166 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001167 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1168 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001169 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001170 mColorR = a * ((color >> 16) & 0xFF);
1171 mColorG = a * ((color >> 8) & 0xFF);
1172 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001173 mColorSet = true;
1174 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1175}
1176
Romain Guy86568192010-12-14 15:55:39 -08001177void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1178 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001179 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1180 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001181 const float a = mColorA / 255.0f;
1182 mColorR = a * ((color >> 16) & 0xFF);
1183 mColorG = a * ((color >> 8) & 0xFF);
1184 mColorB = a * ((color ) & 0xFF);
1185 mColorSet = true;
1186 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1187}
1188
Romain Guy41210632012-07-16 17:04:24 -07001189void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1190 mCaches.fontRenderer->describe(mDescription, paint);
1191}
1192
Romain Guy70ca14e2010-12-13 18:24:33 -08001193void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1194 mColorA = a;
1195 mColorR = r;
1196 mColorG = g;
1197 mColorB = b;
1198 mColorSet = true;
1199 mSetShaderColor = mDescription.setColor(r, g, b, a);
1200}
1201
1202void OpenGLRenderer::setupDrawShader() {
1203 if (mShader) {
1204 mShader->describe(mDescription, mCaches.extensions);
1205 }
1206}
1207
1208void OpenGLRenderer::setupDrawColorFilter() {
1209 if (mColorFilter) {
1210 mColorFilter->describe(mDescription, mCaches.extensions);
1211 }
1212}
1213
Romain Guyf09ef512011-05-27 11:43:46 -07001214void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1215 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1216 mColorA = 1.0f;
1217 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001218 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001219 }
1220}
1221
Romain Guy70ca14e2010-12-13 18:24:33 -08001222void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001223 // When the blending mode is kClear_Mode, we need to use a modulate color
1224 // argb=1,0,0,0
1225 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001226 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1227 mDescription, swapSrcDst);
1228}
1229
1230void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001231 // When the blending mode is kClear_Mode, we need to use a modulate color
1232 // argb=1,0,0,0
1233 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001234 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1235 mDescription, swapSrcDst);
1236}
1237
1238void OpenGLRenderer::setupDrawProgram() {
1239 useProgram(mCaches.programCache.get(mDescription));
1240}
1241
1242void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1243 mTrackDirtyRegions = false;
1244}
1245
1246void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1247 bool ignoreTransform) {
1248 mModelView.loadTranslate(left, top, 0.0f);
1249 if (!ignoreTransform) {
1250 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1251 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1252 } else {
1253 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1254 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1255 }
1256}
1257
Chet Haase8a5cc922011-04-26 07:28:09 -07001258void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1259 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001260}
1261
Romain Guy70ca14e2010-12-13 18:24:33 -08001262void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1263 bool ignoreTransform, bool ignoreModelView) {
1264 if (!ignoreModelView) {
1265 mModelView.loadTranslate(left, top, 0.0f);
1266 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001267 } else {
1268 mModelView.loadIdentity();
1269 }
Romain Guy86568192010-12-14 15:55:39 -08001270 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1271 if (!ignoreTransform) {
1272 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1273 if (mTrackDirtyRegions && dirty) {
1274 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1275 }
1276 } else {
1277 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1278 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1279 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001280}
1281
Romain Guyed6fcb02011-03-21 13:11:28 -07001282void OpenGLRenderer::setupDrawPointUniforms() {
1283 int slot = mCaches.currentProgram->getUniform("pointSize");
1284 glUniform1f(slot, mDescription.pointSize);
1285}
1286
Romain Guy70ca14e2010-12-13 18:24:33 -08001287void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001288 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001289 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1290 }
1291}
1292
Romain Guy86568192010-12-14 15:55:39 -08001293void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001294 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001295 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001296 }
1297}
1298
Romain Guy70ca14e2010-12-13 18:24:33 -08001299void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1300 if (mShader) {
1301 if (ignoreTransform) {
1302 mModelView.loadInverse(*mSnapshot->transform);
1303 }
1304 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1305 }
1306}
1307
Romain Guy8d0d4782010-12-14 20:13:35 -08001308void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1309 if (mShader) {
1310 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1311 }
1312}
1313
Romain Guy70ca14e2010-12-13 18:24:33 -08001314void OpenGLRenderer::setupDrawColorFilterUniforms() {
1315 if (mColorFilter) {
1316 mColorFilter->setupProgram(mCaches.currentProgram);
1317 }
1318}
1319
Romain Guy41210632012-07-16 17:04:24 -07001320void OpenGLRenderer::setupDrawTextGammaUniforms() {
1321 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1322}
1323
Romain Guy70ca14e2010-12-13 18:24:33 -08001324void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001325 bool force = mCaches.bindMeshBuffer();
1326 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001327 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001328}
1329
1330void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1331 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001332 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001333 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001334}
1335
Romain Guyaa6c24c2011-04-28 18:40:04 -07001336void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1337 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001338 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001339 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001340}
1341
Romain Guy8f0095c2011-05-02 17:24:22 -07001342void OpenGLRenderer::setupDrawTextureTransform() {
1343 mDescription.hasTextureTransform = true;
1344}
1345
1346void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001347 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1348 GL_FALSE, &transform.data[0]);
1349}
1350
Romain Guy70ca14e2010-12-13 18:24:33 -08001351void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001352 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001353 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001354 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001355 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001356 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001357 }
Romain Guyd71dd362011-12-12 19:03:35 -08001358
Romain Guyf3a910b42011-12-12 20:35:21 -08001359 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001360 if (mCaches.currentProgram->texCoords >= 0) {
1361 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1362 }
1363
1364 mCaches.unbindIndicesBuffer();
1365}
1366
1367void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1368 bool force = mCaches.unbindMeshBuffer();
1369 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1370 if (mCaches.currentProgram->texCoords >= 0) {
1371 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001372 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001373}
1374
Chet Haase5b0200b2011-04-13 17:58:08 -07001375void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001376 bool force = mCaches.unbindMeshBuffer();
1377 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1378 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001379 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001380}
1381
1382/**
1383 * 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 -07001384 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1385 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1386 * attributes (one per vertex) are values from zero to one that tells the fragment
1387 * shader where the fragment is in relation to the line width/length overall; these values are
1388 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1389 * region of the line.
1390 * Note that we only pass down the width values in this setup function. The length coordinates
1391 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001392 */
Chet Haase99585ad2011-05-02 15:00:16 -07001393void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001394 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001395 bool force = mCaches.unbindMeshBuffer();
1396 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1397 vertices, gAAVertexStride);
1398 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001399 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001400
Romain Guy7b631422012-04-04 11:38:54 -07001401 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001402 glEnableVertexAttribArray(widthSlot);
1403 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001404
Romain Guy7b631422012-04-04 11:38:54 -07001405 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001406 glEnableVertexAttribArray(lengthSlot);
1407 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001408
Chet Haase5b0200b2011-04-13 17:58:08 -07001409 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001410 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001411}
1412
1413void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1414 glDisableVertexAttribArray(widthSlot);
1415 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001416}
1417
Romain Guy70ca14e2010-12-13 18:24:33 -08001418void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001419}
1420
1421///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001422// Drawing
1423///////////////////////////////////////////////////////////////////////////////
1424
Chet Haase1271e2c2012-04-20 09:54:27 -07001425status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001426 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001427
Romain Guy0fe478e2010-11-08 12:08:41 -08001428 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1429 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001430 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001431 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001432 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001433
Romain Guy65549432012-03-26 16:45:05 -07001434 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001435}
1436
Chet Haaseed30fd82011-04-22 16:18:45 -07001437void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1438 if (displayList) {
1439 displayList->output(*this, level);
1440 }
1441}
1442
Romain Guya168d732011-03-18 16:50:13 -07001443void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1444 int alpha;
1445 SkXfermode::Mode mode;
1446 getAlphaAndMode(paint, &alpha, &mode);
1447
Romain Guya168d732011-03-18 16:50:13 -07001448 float x = left;
1449 float y = top;
1450
Romain Guye3c26852011-07-25 16:36:01 -07001451 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001452 bool ignoreTransform = false;
1453 if (mSnapshot->transform->isPureTranslate()) {
1454 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1455 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1456 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001457 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001458 } else {
1459 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001460 }
1461
1462 setupDraw();
1463 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001464 if (paint) {
1465 setupDrawAlpha8Color(paint->getColor(), alpha);
1466 }
Romain Guya168d732011-03-18 16:50:13 -07001467 setupDrawColorFilter();
1468 setupDrawShader();
1469 setupDrawBlending(true, mode);
1470 setupDrawProgram();
1471 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001472
Romain Guya168d732011-03-18 16:50:13 -07001473 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001474 texture->setWrap(GL_CLAMP_TO_EDGE);
1475 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001476
Romain Guya168d732011-03-18 16:50:13 -07001477 setupDrawPureColorUniforms();
1478 setupDrawColorFilterUniforms();
1479 setupDrawShaderUniforms();
1480 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1481
1482 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1483
1484 finishDrawTexture();
1485}
1486
Chet Haase48659092012-05-31 15:21:51 -07001487status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001488 const float right = left + bitmap->width();
1489 const float bottom = top + bitmap->height();
1490
1491 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001492 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001493 }
1494
Romain Guya1d3c912011-12-13 14:55:06 -08001495 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001496 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001497 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001498 const AutoTexture autoCleanup(texture);
1499
Romain Guy211370f2012-02-01 16:10:55 -08001500 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001501 drawAlphaBitmap(texture, left, top, paint);
1502 } else {
1503 drawTextureRect(left, top, right, bottom, texture, paint);
1504 }
Chet Haase48659092012-05-31 15:21:51 -07001505
1506 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001507}
1508
Chet Haase48659092012-05-31 15:21:51 -07001509status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001510 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1511 const mat4 transform(*matrix);
1512 transform.mapRect(r);
1513
Romain Guy6926c722010-07-12 20:20:03 -07001514 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001515 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001516 }
1517
Romain Guya1d3c912011-12-13 14:55:06 -08001518 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001519 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001520 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001521 const AutoTexture autoCleanup(texture);
1522
Romain Guy5b3b3522010-10-27 18:57:51 -07001523 // This could be done in a cheaper way, all we need is pass the matrix
1524 // to the vertex shader. The save/restore is a bit overkill.
1525 save(SkCanvas::kMatrix_SaveFlag);
1526 concatMatrix(matrix);
1527 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1528 restore();
Chet Haase48659092012-05-31 15:21:51 -07001529
1530 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001531}
1532
Chet Haase48659092012-05-31 15:21:51 -07001533status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001534 const float right = left + bitmap->width();
1535 const float bottom = top + bitmap->height();
1536
1537 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001538 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001539 }
1540
1541 mCaches.activeTexture(0);
1542 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1543 const AutoTexture autoCleanup(texture);
1544
1545 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001546
1547 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001548}
1549
Chet Haase48659092012-05-31 15:21:51 -07001550status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001551 float* vertices, int* colors, SkPaint* paint) {
1552 // TODO: Do a quickReject
1553 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001554 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001555 }
1556
Romain Guya1d3c912011-12-13 14:55:06 -08001557 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001558 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001559 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001560 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001561
Romain Guyd21b6e12011-11-30 20:21:23 -08001562 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1563 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001564
1565 int alpha;
1566 SkXfermode::Mode mode;
1567 getAlphaAndMode(paint, &alpha, &mode);
1568
Romain Guy5a7b4662011-01-20 19:09:30 -08001569 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001570
Romain Guyb18d2d02011-02-10 15:52:54 -08001571 float left = FLT_MAX;
1572 float top = FLT_MAX;
1573 float right = FLT_MIN;
1574 float bottom = FLT_MIN;
1575
Romain Guy211370f2012-02-01 16:10:55 -08001576 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001577
Romain Guya566b7c2011-01-23 16:36:11 -08001578 // TODO: Support the colors array
1579 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001580 TextureVertex* vertex = mesh;
1581 for (int32_t y = 0; y < meshHeight; y++) {
1582 for (int32_t x = 0; x < meshWidth; x++) {
1583 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1584
1585 float u1 = float(x) / meshWidth;
1586 float u2 = float(x + 1) / meshWidth;
1587 float v1 = float(y) / meshHeight;
1588 float v2 = float(y + 1) / meshHeight;
1589
1590 int ax = i + (meshWidth + 1) * 2;
1591 int ay = ax + 1;
1592 int bx = i;
1593 int by = bx + 1;
1594 int cx = i + 2;
1595 int cy = cx + 1;
1596 int dx = i + (meshWidth + 1) * 2 + 2;
1597 int dy = dx + 1;
1598
1599 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1600 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1601 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1602
1603 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1604 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1605 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001606
Romain Guyb18d2d02011-02-10 15:52:54 -08001607 if (hasActiveLayer) {
1608 // TODO: This could be optimized to avoid unnecessary ops
1609 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1610 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1611 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1612 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1613 }
Romain Guy5a7b4662011-01-20 19:09:30 -08001614 }
1615 }
1616
Romain Guyb18d2d02011-02-10 15:52:54 -08001617 if (hasActiveLayer) {
1618 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1619 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001620
Romain Guy5a7b4662011-01-20 19:09:30 -08001621 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1622 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001623 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001624
1625 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001626}
1627
Chet Haase48659092012-05-31 15:21:51 -07001628status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001629 float srcLeft, float srcTop, float srcRight, float srcBottom,
1630 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001631 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001632 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001633 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001634 }
1635
Romain Guya1d3c912011-12-13 14:55:06 -08001636 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001637 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001638 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001639 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001640
Romain Guy8ba548f2010-06-30 19:21:21 -07001641 const float width = texture->width;
1642 const float height = texture->height;
1643
Romain Guy68169722011-08-22 17:33:33 -07001644 const float u1 = fmax(0.0f, srcLeft / width);
1645 const float v1 = fmax(0.0f, srcTop / height);
1646 const float u2 = fmin(1.0f, srcRight / width);
1647 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001648
Romain Guy03750a02010-10-18 14:06:08 -07001649 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001650 resetDrawTextureTexCoords(u1, v1, u2, v2);
1651
Romain Guy03750a02010-10-18 14:06:08 -07001652 int alpha;
1653 SkXfermode::Mode mode;
1654 getAlphaAndMode(paint, &alpha, &mode);
1655
Romain Guyd21b6e12011-11-30 20:21:23 -08001656 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1657
Romain Guy211370f2012-02-01 16:10:55 -08001658 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001659 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1660 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1661
Romain Guyb5014982011-07-28 15:39:12 -07001662 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001663 // Enable linear filtering if the source rectangle is scaled
1664 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001665 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001666 }
Romain Guyb5014982011-07-28 15:39:12 -07001667
Romain Guyd21b6e12011-11-30 20:21:23 -08001668 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001669 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1670 texture->id, alpha / 255.0f, mode, texture->blend,
1671 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1672 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1673 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001674 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001675 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1676 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1677 GL_TRIANGLE_STRIP, gMeshCount);
1678 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001679
1680 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001681
1682 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001683}
1684
Chet Haase48659092012-05-31 15:21:51 -07001685status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001686 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001687 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001688 int alpha;
1689 SkXfermode::Mode mode;
1690 getAlphaAndModeDirect(paint, &alpha, &mode);
1691
1692 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1693 left, top, right, bottom, alpha, mode);
1694}
1695
1696status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1697 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1698 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001699 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001700 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001701 }
1702
Romain Guybe6f9dc2012-07-16 12:41:17 -07001703 alpha *= mSnapshot->alpha;
1704
Romain Guya1d3c912011-12-13 14:55:06 -08001705 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001706 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001707 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001708 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001709 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1710 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001711
Romain Guy2728f962010-10-08 18:36:15 -07001712 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001713 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001714
Romain Guy211370f2012-02-01 16:10:55 -08001715 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001716 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001717 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001718 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001719 const float offsetX = left + mSnapshot->transform->getTranslateX();
1720 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001721 const size_t count = mesh->quads.size();
1722 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001723 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001724 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001725 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1726 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1727 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001728 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001729 dirtyLayer(left + bounds.left, top + bounds.top,
1730 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001731 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001732 }
1733 }
1734
Romain Guy211370f2012-02-01 16:10:55 -08001735 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001736 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1737 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1738
1739 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1740 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1741 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1742 true, !mesh->hasEmptyQuads);
1743 } else {
1744 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1745 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1746 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1747 true, !mesh->hasEmptyQuads);
1748 }
Romain Guy054dc182010-10-15 17:55:25 -07001749 }
Chet Haase48659092012-05-31 15:21:51 -07001750
1751 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001752}
1753
Chet Haase99ecdc42011-05-06 12:06:34 -07001754/**
Chet Haase858aa932011-05-12 09:06:00 -07001755 * This function uses a similar approach to that of AA lines in the drawLines() function.
Chris Craik6ebdc112012-08-31 18:24:33 -07001756 * We expand the rectangle by a half pixel in screen space on all sides. However, instead of using
1757 * a fragment shader to compute the translucency of the color from its position, we simply use a
1758 * varying parameter to define how far a given pixel is into the region.
Chet Haase858aa932011-05-12 09:06:00 -07001759 */
1760void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001761 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001762 float inverseScaleX = 1.0f;
1763 float inverseScaleY = 1.0f;
Romain Guy04299382012-07-18 17:15:41 -07001764
Chet Haase858aa932011-05-12 09:06:00 -07001765 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001766 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001767 Matrix4 *mat = mSnapshot->transform;
1768 float m00 = mat->data[Matrix4::kScaleX];
1769 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase858aa932011-05-12 09:06:00 -07001770 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07001771 float m11 = mat->data[Matrix4::kScaleY];
Chet Haase858aa932011-05-12 09:06:00 -07001772 float scaleX = sqrt(m00 * m00 + m01 * m01);
1773 float scaleY = sqrt(m10 * m10 + m11 * m11);
1774 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1775 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1776 }
1777
Chet Haase858aa932011-05-12 09:06:00 -07001778 float boundarySizeX = .5 * inverseScaleX;
1779 float boundarySizeY = .5 * inverseScaleY;
1780
Chris Craik6ebdc112012-08-31 18:24:33 -07001781 float innerLeft = left + boundarySizeX;
1782 float innerRight = right - boundarySizeX;
1783 float innerTop = top + boundarySizeY;
1784 float innerBottom = bottom - boundarySizeY;
1785
Chet Haase858aa932011-05-12 09:06:00 -07001786 // Adjust the rect by the AA boundary padding
1787 left -= boundarySizeX;
1788 right += boundarySizeX;
1789 top -= boundarySizeY;
1790 bottom += boundarySizeY;
1791
Chet Haase858aa932011-05-12 09:06:00 -07001792 if (!quickReject(left, top, right, bottom)) {
Romain Guy04299382012-07-18 17:15:41 -07001793 setupDraw();
1794 setupDrawNoTexture();
Chris Craik6ebdc112012-08-31 18:24:33 -07001795 setupDrawAARect();
Romain Guy04299382012-07-18 17:15:41 -07001796 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1797 setupDrawColorFilter();
1798 setupDrawShader();
1799 setupDrawBlending(true, mode);
1800 setupDrawProgram();
1801 setupDrawModelViewIdentity(true);
1802 setupDrawColorUniforms();
1803 setupDrawColorFilterUniforms();
1804 setupDrawShaderIdentityUniforms();
1805
Chris Craik6ebdc112012-08-31 18:24:33 -07001806 AlphaVertex rects[14];
1807 AlphaVertex* aVertices = &rects[0];
1808 void* alphaCoords = ((GLbyte*) aVertices) + gVertexAlphaOffset;
Romain Guy04299382012-07-18 17:15:41 -07001809
Chris Craik6ebdc112012-08-31 18:24:33 -07001810 bool force = mCaches.unbindMeshBuffer();
1811 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1812 aVertices, gAlphaVertexStride);
1813 mCaches.resetTexCoordsVertexPointer();
1814 mCaches.unbindIndicesBuffer();
Romain Guy04299382012-07-18 17:15:41 -07001815
Chris Craik6ebdc112012-08-31 18:24:33 -07001816 int alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
1817 glEnableVertexAttribArray(alphaSlot);
1818 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Romain Guy04299382012-07-18 17:15:41 -07001819
Chris Craik6ebdc112012-08-31 18:24:33 -07001820 // draw left
1821 AlphaVertex::set(aVertices++, left, bottom, 0);
1822 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1823 AlphaVertex::set(aVertices++, left, top, 0);
1824 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001825
Chris Craik6ebdc112012-08-31 18:24:33 -07001826 // draw top
1827 AlphaVertex::set(aVertices++, right, top, 0);
1828 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001829
Chris Craik6ebdc112012-08-31 18:24:33 -07001830 // draw right
1831 AlphaVertex::set(aVertices++, right, bottom, 0);
1832 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1833
1834 // draw bottom
1835 AlphaVertex::set(aVertices++, left, bottom, 0);
1836 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1837
1838 // draw inner rect (repeating last vertex to create degenerate bridge triangles)
1839 // TODO: also consider drawing the inner rect without the blending-forced shader, if
1840 // blending is expensive. Note: can't use drawColorRect() since it doesn't use vertex
1841 // buffers like below, resulting in slightly different transformed coordinates.
1842 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1843 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
1844 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1845 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
1846
Chet Haase858aa932011-05-12 09:06:00 -07001847 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guy7b631422012-04-04 11:38:54 -07001848
Chris Craik6ebdc112012-08-31 18:24:33 -07001849 glDrawArrays(GL_TRIANGLE_STRIP, 0, 14);
Romain Guy04299382012-07-18 17:15:41 -07001850
Chris Craik6ebdc112012-08-31 18:24:33 -07001851 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07001852 }
Chet Haase858aa932011-05-12 09:06:00 -07001853}
1854
1855/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001856 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1857 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1858 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1859 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1860 * of the line. Hairlines are more involved because we need to account for transform scaling
1861 * to end up with a one-pixel-wide line in screen space..
1862 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1863 * in combination with values that we calculate and pass down in this method. The basic approach
1864 * is that the quad we create contains both the core line area plus a bounding area in which
1865 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1866 * proportion of the width and the length of a given segment is represented by the boundary
1867 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1868 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1869 * on the inside). This ends up giving the result we want, with pixels that are completely
1870 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1871 * how far into the boundary region they are, which is determined by shader interpolation.
1872 */
Chet Haase48659092012-05-31 15:21:51 -07001873status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1874 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07001875
1876 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001877 // We use half the stroke width here because we're going to position the quad
1878 // corner vertices half of the width away from the line endpoints
1879 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001880 // A stroke width of 0 has a special meaning in Skia:
1881 // it draws a line 1 px wide regardless of current transform
1882 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001883
Chet Haase99ecdc42011-05-06 12:06:34 -07001884 float inverseScaleX = 1.0f;
1885 float inverseScaleY = 1.0f;
1886 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001887
Chet Haase8a5cc922011-04-26 07:28:09 -07001888 int alpha;
1889 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001890
Chet Haase8a5cc922011-04-26 07:28:09 -07001891 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001892 int verticesCount = count;
1893 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001894 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001895 verticesCount += (count - 4);
1896 }
1897
1898 if (isHairLine || isAA) {
1899 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1900 // the line on the screen should always be one pixel wide regardless of scale. For
1901 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001902 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001903 Matrix4 *mat = mSnapshot->transform;
1904 float m00 = mat->data[Matrix4::kScaleX];
1905 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07001906 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07001907 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07001908
Romain Guy211370f2012-02-01 16:10:55 -08001909 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1910 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001911
Chet Haase99ecdc42011-05-06 12:06:34 -07001912 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1913 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001914
Chet Haase99ecdc42011-05-06 12:06:34 -07001915 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1916 scaled = true;
1917 }
1918 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001919 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001920
1921 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07001922
1923 mCaches.enableScissor();
1924
Chet Haase8a5cc922011-04-26 07:28:09 -07001925 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001926 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001927 if (isAA) {
1928 setupDrawAALine();
1929 }
1930 setupDrawColor(paint->getColor(), alpha);
1931 setupDrawColorFilter();
1932 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001933 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001934 setupDrawProgram();
1935 setupDrawModelViewIdentity(true);
1936 setupDrawColorUniforms();
1937 setupDrawColorFilterUniforms();
1938 setupDrawShaderIdentityUniforms();
1939
1940 if (isHairLine) {
1941 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001942 halfStrokeWidth = isAA? 1 : .5;
1943 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001944 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001945 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001946 }
Romain Guy7b631422012-04-04 11:38:54 -07001947
1948 int widthSlot;
1949 int lengthSlot;
1950
Chet Haase5b0200b2011-04-13 17:58:08 -07001951 Vertex lines[verticesCount];
1952 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001953
Chet Haase99585ad2011-05-02 15:00:16 -07001954 AAVertex wLines[verticesCount];
1955 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001956
Romain Guy211370f2012-02-01 16:10:55 -08001957 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001958 setupDrawVertices(vertices);
1959 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001960 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1961 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001962 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001963 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1964 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001965 // We will need to calculate the actual width proportion on each segment for
1966 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07001967 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001968 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1969 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001970 }
1971
Chet Haase99ecdc42011-05-06 12:06:34 -07001972 AAVertex* prevAAVertex = NULL;
1973 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001974
Chet Haase99585ad2011-05-02 15:00:16 -07001975 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001976 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001977
Chet Haase5b0200b2011-04-13 17:58:08 -07001978 for (int i = 0; i < count; i += 4) {
1979 // a = start point, b = end point
1980 vec2 a(points[i], points[i + 1]);
1981 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001982
Chet Haase99585ad2011-05-02 15:00:16 -07001983 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001984 float boundaryLengthProportion = 0;
1985 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001986
Chet Haase5b0200b2011-04-13 17:58:08 -07001987 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001988 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07001989 float x = n.x;
1990 n.x = -n.y;
1991 n.y = x;
1992
Chet Haase8a5cc922011-04-26 07:28:09 -07001993 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001994 if (isAA) {
1995 float wideningFactor;
1996 if (fabs(n.x) >= fabs(n.y)) {
1997 wideningFactor = fabs(1.0f / n.x);
1998 } else {
1999 wideningFactor = fabs(1.0f / n.y);
2000 }
2001 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002002 }
Romain Guy7b631422012-04-04 11:38:54 -07002003
Chet Haase99ecdc42011-05-06 12:06:34 -07002004 if (scaled) {
2005 n.x *= inverseScaleX;
2006 n.y *= inverseScaleY;
2007 }
2008 } else if (scaled) {
2009 // Extend n by .5 pixel on each side, post-transform
2010 vec2 extendedN = n.copyNormalized();
2011 extendedN /= 2;
2012 extendedN.x *= inverseScaleX;
2013 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002014
Chet Haase99ecdc42011-05-06 12:06:34 -07002015 float extendedNLength = extendedN.length();
2016 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002017 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002018 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002019 }
Romain Guy7b631422012-04-04 11:38:54 -07002020
Chet Haase99585ad2011-05-02 15:00:16 -07002021 // aa lines expand the endpoint vertices to encompass the AA boundary
2022 if (isAA) {
2023 vec2 abVector = (b - a);
2024 length = abVector.length();
2025 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002026
Chet Haase99ecdc42011-05-06 12:06:34 -07002027 if (scaled) {
2028 abVector.x *= inverseScaleX;
2029 abVector.y *= inverseScaleY;
2030 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002031 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002032 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002033 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002034 }
Romain Guy7b631422012-04-04 11:38:54 -07002035
Chet Haase99ecdc42011-05-06 12:06:34 -07002036 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002037 a -= abVector;
2038 b += abVector;
2039 }
2040
Chet Haase5b0200b2011-04-13 17:58:08 -07002041 // Four corners of the rectangle defining a thick line
2042 vec2 p1 = a - n;
2043 vec2 p2 = a + n;
2044 vec2 p3 = b + n;
2045 vec2 p4 = b - n;
2046
Chet Haase99585ad2011-05-02 15:00:16 -07002047
Chet Haase5b0200b2011-04-13 17:58:08 -07002048 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2049 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2050 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2051 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2052
Romain Guy04299382012-07-18 17:15:41 -07002053 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002054 if (!isAA) {
2055 if (prevVertex != NULL) {
2056 // Issue two repeat vertices to create degenerate triangles to bridge
2057 // between the previous line and the new one. This is necessary because
2058 // we are creating a single triangle_strip which will contain
2059 // potentially discontinuous line segments.
2060 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2061 Vertex::set(vertices++, p1.x, p1.y);
2062 generatedVerticesCount += 2;
2063 }
Romain Guy7b631422012-04-04 11:38:54 -07002064
Chet Haase5b0200b2011-04-13 17:58:08 -07002065 Vertex::set(vertices++, p1.x, p1.y);
2066 Vertex::set(vertices++, p2.x, p2.y);
2067 Vertex::set(vertices++, p4.x, p4.y);
2068 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002069
Chet Haase5b0200b2011-04-13 17:58:08 -07002070 prevVertex = vertices - 1;
2071 generatedVerticesCount += 4;
2072 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002073 if (!isHairLine && scaled) {
2074 // Must set width proportions per-segment for scaled non-hairlines to use the
2075 // correct AA boundary dimensions
2076 if (boundaryWidthSlot < 0) {
2077 boundaryWidthSlot =
2078 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002079 }
Romain Guy7b631422012-04-04 11:38:54 -07002080
Chet Haase99ecdc42011-05-06 12:06:34 -07002081 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002082 }
Romain Guy7b631422012-04-04 11:38:54 -07002083
Chet Haase99585ad2011-05-02 15:00:16 -07002084 if (boundaryLengthSlot < 0) {
2085 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002086 }
Romain Guy7b631422012-04-04 11:38:54 -07002087
Chet Haase99ecdc42011-05-06 12:06:34 -07002088 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002089
Chet Haase5b0200b2011-04-13 17:58:08 -07002090 if (prevAAVertex != NULL) {
2091 // Issue two repeat vertices to create degenerate triangles to bridge
2092 // between the previous line and the new one. This is necessary because
2093 // we are creating a single triangle_strip which will contain
2094 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002095 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2096 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2097 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002098 generatedVerticesCount += 2;
2099 }
Romain Guy7b631422012-04-04 11:38:54 -07002100
Chet Haase99585ad2011-05-02 15:00:16 -07002101 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2102 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2103 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2104 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002105
Chet Haase5b0200b2011-04-13 17:58:08 -07002106 prevAAVertex = aaVertices - 1;
2107 generatedVerticesCount += 4;
2108 }
Romain Guy7b631422012-04-04 11:38:54 -07002109
Chet Haase99585ad2011-05-02 15:00:16 -07002110 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2111 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2112 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002113 }
2114 }
Romain Guy7b631422012-04-04 11:38:54 -07002115
Chet Haase5b0200b2011-04-13 17:58:08 -07002116 if (generatedVerticesCount > 0) {
2117 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2118 }
Romain Guy7b631422012-04-04 11:38:54 -07002119
2120 if (isAA) {
2121 finishDrawAALine(widthSlot, lengthSlot);
2122 }
Chet Haase48659092012-05-31 15:21:51 -07002123
2124 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002125}
2126
Chet Haase48659092012-05-31 15:21:51 -07002127status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2128 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002129
2130 // TODO: The paint's cap style defines whether the points are square or circular
2131 // TODO: Handle AA for round points
2132
Chet Haase5b0200b2011-04-13 17:58:08 -07002133 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002134 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002135 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002136 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002137 if (isHairLine) {
2138 // Now that we know it's hairline, we can set the effective width, to be used later
2139 strokeWidth = 1.0f;
2140 }
2141 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002142 int alpha;
2143 SkXfermode::Mode mode;
2144 getAlphaAndMode(paint, &alpha, &mode);
2145
2146 int verticesCount = count >> 1;
2147 int generatedVerticesCount = 0;
2148
2149 TextureVertex pointsData[verticesCount];
2150 TextureVertex* vertex = &pointsData[0];
2151
Romain Guy04299382012-07-18 17:15:41 -07002152 // TODO: We should optimize this method to not generate vertices for points
2153 // that lie outside of the clip.
2154 mCaches.enableScissor();
2155
Romain Guyed6fcb02011-03-21 13:11:28 -07002156 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002157 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002158 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002159 setupDrawColor(paint->getColor(), alpha);
2160 setupDrawColorFilter();
2161 setupDrawShader();
2162 setupDrawBlending(mode);
2163 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002164 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002165 setupDrawColorUniforms();
2166 setupDrawColorFilterUniforms();
2167 setupDrawPointUniforms();
2168 setupDrawShaderIdentityUniforms();
2169 setupDrawMesh(vertex);
2170
2171 for (int i = 0; i < count; i += 2) {
2172 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2173 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002174
Chet Haase8a5cc922011-04-26 07:28:09 -07002175 float left = points[i] - halfWidth;
2176 float right = points[i] + halfWidth;
2177 float top = points[i + 1] - halfWidth;
2178 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002179
Chet Haase8a5cc922011-04-26 07:28:09 -07002180 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002181 }
2182
2183 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002184
2185 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002186}
2187
Chet Haase48659092012-05-31 15:21:51 -07002188status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002189 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002190 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002191
Romain Guyae88e5e2010-10-22 17:49:18 -07002192 Rect& clip(*mSnapshot->clipRect);
2193 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002194
Romain Guy3d58c032010-07-14 16:34:53 -07002195 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002196
2197 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002198}
Romain Guy9d5316e2010-06-24 19:30:36 -07002199
Chet Haase48659092012-05-31 15:21:51 -07002200status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2201 SkPaint* paint) {
2202 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002203 const AutoTexture autoCleanup(texture);
2204
2205 const float x = left + texture->left - texture->offset;
2206 const float y = top + texture->top - texture->offset;
2207
2208 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002209
2210 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002211}
2212
Chet Haase48659092012-05-31 15:21:51 -07002213status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002214 float rx, float ry, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002215 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002216
Romain Guya1d3c912011-12-13 14:55:06 -08002217 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002218 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2219 right - left, bottom - top, rx, ry, paint);
Chet Haase48659092012-05-31 15:21:51 -07002220 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002221}
2222
Chet Haase48659092012-05-31 15:21:51 -07002223status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2224 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002225
Romain Guya1d3c912011-12-13 14:55:06 -08002226 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002227 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Chet Haase48659092012-05-31 15:21:51 -07002228 return drawShape(x - radius, y - radius, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002229}
Romain Guy01d58e42011-01-19 21:54:02 -08002230
Chet Haase48659092012-05-31 15:21:51 -07002231status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
2232 SkPaint* paint) {
2233 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002234
Romain Guya1d3c912011-12-13 14:55:06 -08002235 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002236 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002237 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002238}
2239
Chet Haase48659092012-05-31 15:21:51 -07002240status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002241 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002242 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002243
2244 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002245 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002246 }
2247
Romain Guya1d3c912011-12-13 14:55:06 -08002248 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002249 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2250 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002251 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002252}
2253
Chet Haase48659092012-05-31 15:21:51 -07002254status_t OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002255 SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002256 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002257
Romain Guya1d3c912011-12-13 14:55:06 -08002258 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002259 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002260 return drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002261}
2262
Chet Haase48659092012-05-31 15:21:51 -07002263status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002264 if (p->getStyle() != SkPaint::kFill_Style) {
Chet Haase48659092012-05-31 15:21:51 -07002265 return drawRectAsShape(left, top, right, bottom, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002266 }
2267
Romain Guy6926c722010-07-12 20:20:03 -07002268 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002269 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002270 }
2271
Romain Guy026c5e162010-06-28 17:12:22 -07002272 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002273 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002274 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2275 if (!isMode) {
2276 // Assume SRC_OVER
2277 mode = SkXfermode::kSrcOver_Mode;
2278 }
2279 } else {
2280 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002281 }
2282
Romain Guy026c5e162010-06-28 17:12:22 -07002283 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002284 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002285 drawAARect(left, top, right, bottom, color, mode);
2286 } else {
2287 drawColorRect(left, top, right, bottom, color, mode);
2288 }
Chet Haase48659092012-05-31 15:21:51 -07002289
2290 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002291}
Romain Guy9d5316e2010-06-24 19:30:36 -07002292
Raph Levien416a8472012-07-19 22:48:17 -07002293void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2294 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2295 float x, float y) {
2296 mCaches.activeTexture(0);
2297
2298 // NOTE: The drop shadow will not perform gamma correction
2299 // if shader-based correction is enabled
2300 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2301 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2302 paint, text, bytesCount, count, mShadowRadius, positions);
2303 const AutoTexture autoCleanup(shadow);
2304
2305 const float sx = x - shadow->left + mShadowDx;
2306 const float sy = y - shadow->top + mShadowDy;
2307
2308 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2309 int shadowColor = mShadowColor;
2310 if (mShader) {
2311 shadowColor = 0xffffffff;
2312 }
2313
2314 setupDraw();
2315 setupDrawWithTexture(true);
2316 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2317 setupDrawColorFilter();
2318 setupDrawShader();
2319 setupDrawBlending(true, mode);
2320 setupDrawProgram();
2321 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2322 setupDrawTexture(shadow->id);
2323 setupDrawPureColorUniforms();
2324 setupDrawColorFilterUniforms();
2325 setupDrawShaderUniforms();
2326 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2327
2328 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2329}
2330
Chet Haase48659092012-05-31 15:21:51 -07002331status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002332 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002333 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002334 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002335 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002336 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002337
Romain Guy671d6cf2012-01-18 12:39:17 -08002338 // NOTE: Skia does not support perspective transform on drawPosText yet
2339 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002340 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002341 }
2342
2343 float x = 0.0f;
2344 float y = 0.0f;
2345 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2346 if (pureTranslate) {
2347 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2348 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2349 }
2350
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002351 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002352 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2353 paint->getTextSize());
2354
2355 int alpha;
2356 SkXfermode::Mode mode;
2357 getAlphaAndMode(paint, &alpha, &mode);
2358
Raph Levien416a8472012-07-19 22:48:17 -07002359 if (CC_UNLIKELY(mHasShadow)) {
2360 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2361 0.0f, 0.0f);
2362 }
2363
Romain Guy671d6cf2012-01-18 12:39:17 -08002364 // Pick the appropriate texture filtering
2365 bool linearFilter = mSnapshot->transform->changesBounds();
2366 if (pureTranslate && !linearFilter) {
2367 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2368 }
2369
2370 mCaches.activeTexture(0);
2371 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002372 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002373 setupDrawDirtyRegionsDisabled();
2374 setupDrawWithTexture(true);
2375 setupDrawAlpha8Color(paint->getColor(), alpha);
2376 setupDrawColorFilter();
2377 setupDrawShader();
2378 setupDrawBlending(true, mode);
2379 setupDrawProgram();
2380 setupDrawModelView(x, y, x, y, pureTranslate, true);
2381 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2382 setupDrawPureColorUniforms();
2383 setupDrawColorFilterUniforms();
2384 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002385 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002386
2387 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2388 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2389
Romain Guy211370f2012-02-01 16:10:55 -08002390 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002391
2392 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2393 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002394 if (hasActiveLayer) {
2395 if (!pureTranslate) {
2396 mSnapshot->transform->mapRect(bounds);
2397 }
2398 dirtyLayerUnchecked(bounds, getRegion());
2399 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002400 }
Chet Haase48659092012-05-31 15:21:51 -07002401
2402 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002403}
2404
Romain Guyc2525952012-07-27 16:41:22 -07002405status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002406 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002407 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002408 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002409 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002410 }
2411
Chet Haasea1cff502012-02-21 13:43:44 -08002412 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002413 switch (paint->getTextAlign()) {
2414 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002415 x -= length / 2.0f;
2416 break;
2417 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002418 x -= length;
2419 break;
2420 default:
2421 break;
2422 }
2423
Romain Guycac5fd32011-12-01 20:08:50 -08002424 SkPaint::FontMetrics metrics;
2425 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002426 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002427 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002428 }
2429
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002430 const float oldX = x;
2431 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002432 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002433 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002434 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2435 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2436 }
2437
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002438#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002439 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002440 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002441#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002442
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002443 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002444 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002445 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002446
Romain Guy86568192010-12-14 15:55:39 -08002447 int alpha;
2448 SkXfermode::Mode mode;
2449 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002450
Romain Guy211370f2012-02-01 16:10:55 -08002451 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002452 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2453 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002454 }
2455
Romain Guy6620c6d2010-12-06 18:07:02 -08002456 // Pick the appropriate texture filtering
2457 bool linearFilter = mSnapshot->transform->changesBounds();
2458 if (pureTranslate && !linearFilter) {
2459 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2460 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002461
Romain Guy16c88082012-06-11 16:03:47 -07002462 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002463 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002464 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002465 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002466 setupDrawDirtyRegionsDisabled();
2467 setupDrawWithTexture(true);
2468 setupDrawAlpha8Color(paint->getColor(), alpha);
2469 setupDrawColorFilter();
2470 setupDrawShader();
2471 setupDrawBlending(true, mode);
2472 setupDrawProgram();
2473 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002474 // See comment above; the font renderer must use texture unit 0
2475 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002476 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2477 setupDrawPureColorUniforms();
2478 setupDrawColorFilterUniforms();
2479 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002480 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002481
Romain Guy6620c6d2010-12-06 18:07:02 -08002482 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002483 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2484
Romain Guy211370f2012-02-01 16:10:55 -08002485 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002486
Raph Levien996e57c2012-07-23 15:22:52 -07002487 bool status;
Raph Levien8b4072d2012-07-30 15:50:00 -07002488 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
2489 SkPaint paintCopy(*paint);
2490 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2491 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Raph Levien996e57c2012-07-23 15:22:52 -07002492 positions, hasActiveLayer ? &bounds : NULL);
2493 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002494 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2495 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002496 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002497
2498 if (status && hasActiveLayer) {
2499 if (!pureTranslate) {
2500 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002501 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002502 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002503 }
Romain Guy694b5192010-07-21 21:33:20 -07002504
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002505 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002506
2507 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002508}
2509
Chet Haase48659092012-05-31 15:21:51 -07002510status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002511 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002512 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2513 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002514 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002515 }
2516
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002517 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002518 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2519 paint->getTextSize());
2520
2521 int alpha;
2522 SkXfermode::Mode mode;
2523 getAlphaAndMode(paint, &alpha, &mode);
2524
2525 mCaches.activeTexture(0);
2526 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002527 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002528 setupDrawDirtyRegionsDisabled();
2529 setupDrawWithTexture(true);
2530 setupDrawAlpha8Color(paint->getColor(), alpha);
2531 setupDrawColorFilter();
2532 setupDrawShader();
2533 setupDrawBlending(true, mode);
2534 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002535 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002536 setupDrawTexture(fontRenderer.getTexture(true));
2537 setupDrawPureColorUniforms();
2538 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002539 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002540 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002541
Romain Guy97771732012-02-28 18:17:02 -08002542 const Rect* clip = &mSnapshot->getLocalClip();
2543 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 -08002544
Romain Guy97771732012-02-28 18:17:02 -08002545 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002546
2547 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2548 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002549 if (hasActiveLayer) {
2550 mSnapshot->transform->mapRect(bounds);
2551 dirtyLayerUnchecked(bounds, getRegion());
2552 }
Romain Guy97771732012-02-28 18:17:02 -08002553 }
Chet Haase48659092012-05-31 15:21:51 -07002554
2555 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002556}
2557
Chet Haase48659092012-05-31 15:21:51 -07002558status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2559 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002560
Romain Guya1d3c912011-12-13 14:55:06 -08002561 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002562
Romain Guy33f6beb2012-02-16 19:24:51 -08002563 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002564 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002565 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002566 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002567
Romain Guy8b55f372010-08-18 17:10:07 -07002568 const float x = texture->left - texture->offset;
2569 const float y = texture->top - texture->offset;
2570
Romain Guy01d58e42011-01-19 21:54:02 -08002571 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002572
2573 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002574}
2575
Chet Haase48659092012-05-31 15:21:51 -07002576status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guyada830f2011-01-13 12:13:20 -08002577 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Chet Haase48659092012-05-31 15:21:51 -07002578 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002579 }
2580
Romain Guy4ff0cf42012-08-06 14:51:10 -07002581 bool debugLayerUpdate = false;
2582
Romain Guy2bf68f02012-03-02 13:37:47 -08002583 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2584 OpenGLRenderer* renderer = layer->renderer;
2585 Rect& dirty = layer->dirtyRect;
2586
2587 interrupt();
2588 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2589 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002590 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002591 renderer->finish();
2592 resume();
2593
2594 dirty.setEmpty();
2595 layer->deferredUpdateScheduled = false;
2596 layer->renderer = NULL;
2597 layer->displayList = NULL;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002598
2599 debugLayerUpdate = mCaches.debugLayersUpdates;
Romain Guy2bf68f02012-03-02 13:37:47 -08002600 }
2601
Romain Guya1d3c912011-12-13 14:55:06 -08002602 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002603
Romain Guy211370f2012-02-01 16:10:55 -08002604 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002605 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002606 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002607 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002608 const float a = layer->getAlpha() / 255.0f;
2609 SkiaColorFilter *oldFilter = mColorFilter;
2610 mColorFilter = layer->getColorFilter();
Romain Guyf219da52011-01-16 12:54:25 -08002611
Romain Guyc88e3572011-01-22 00:32:12 -08002612 setupDraw();
2613 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002614 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002615 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002616 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002617 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002618 setupDrawPureColorUniforms();
2619 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002620 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002621 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002622 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2623 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002624
Romain Guyd21b6e12011-11-30 20:21:23 -08002625 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002626 setupDrawModelViewTranslate(tx, ty,
2627 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002628 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002629 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002630 setupDrawModelViewTranslate(x, y,
2631 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2632 }
Romain Guyc88e3572011-01-22 00:32:12 -08002633 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002634
Romain Guyc88e3572011-01-22 00:32:12 -08002635 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2636 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002637
Romain Guyc88e3572011-01-22 00:32:12 -08002638 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002639
Chet Haased15ebf22012-09-05 11:40:29 -07002640 mColorFilter = oldFilter;
2641
Romain Guy3a3133d2011-02-01 22:59:58 -08002642#if DEBUG_LAYERS_AS_REGIONS
2643 drawRegionRects(layer->region);
2644#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002645 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002646
2647 if (debugLayerUpdate) {
2648 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2649 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2650 }
Romain Guyf219da52011-01-16 12:54:25 -08002651 }
Chet Haase48659092012-05-31 15:21:51 -07002652
2653 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002654}
2655
Romain Guy6926c722010-07-12 20:20:03 -07002656///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002657// Shaders
2658///////////////////////////////////////////////////////////////////////////////
2659
2660void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002661 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002662}
2663
Romain Guy06f96e22010-07-30 19:18:16 -07002664void OpenGLRenderer::setupShader(SkiaShader* shader) {
2665 mShader = shader;
2666 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002667 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002668 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002669}
2670
Romain Guyd27977d2010-07-14 19:18:51 -07002671///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002672// Color filters
2673///////////////////////////////////////////////////////////////////////////////
2674
2675void OpenGLRenderer::resetColorFilter() {
2676 mColorFilter = NULL;
2677}
2678
2679void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2680 mColorFilter = filter;
2681}
2682
2683///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002684// Drop shadow
2685///////////////////////////////////////////////////////////////////////////////
2686
2687void OpenGLRenderer::resetShadow() {
2688 mHasShadow = false;
2689}
2690
2691void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2692 mHasShadow = true;
2693 mShadowRadius = radius;
2694 mShadowDx = dx;
2695 mShadowDy = dy;
2696 mShadowColor = color;
2697}
2698
2699///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002700// Draw filters
2701///////////////////////////////////////////////////////////////////////////////
2702
2703void OpenGLRenderer::resetPaintFilter() {
2704 mHasDrawFilter = false;
2705}
2706
2707void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2708 mHasDrawFilter = true;
2709 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2710 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2711}
2712
2713SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002714 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002715
2716 uint32_t flags = paint->getFlags();
2717
2718 mFilteredPaint = *paint;
2719 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2720
2721 return &mFilteredPaint;
2722}
2723
2724///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002725// Drawing implementation
2726///////////////////////////////////////////////////////////////////////////////
2727
Romain Guy01d58e42011-01-19 21:54:02 -08002728void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2729 float x, float y, SkPaint* paint) {
2730 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2731 return;
2732 }
2733
2734 int alpha;
2735 SkXfermode::Mode mode;
2736 getAlphaAndMode(paint, &alpha, &mode);
2737
2738 setupDraw();
2739 setupDrawWithTexture(true);
2740 setupDrawAlpha8Color(paint->getColor(), alpha);
2741 setupDrawColorFilter();
2742 setupDrawShader();
2743 setupDrawBlending(true, mode);
2744 setupDrawProgram();
2745 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2746 setupDrawTexture(texture->id);
2747 setupDrawPureColorUniforms();
2748 setupDrawColorFilterUniforms();
2749 setupDrawShaderUniforms();
2750 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2751
2752 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2753
2754 finishDrawTexture();
2755}
2756
Romain Guyf607bdc2010-09-10 19:20:06 -07002757// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002758#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2759#define kStdUnderline_Offset (1.0f / 9.0f)
2760#define kStdUnderline_Thickness (1.0f / 18.0f)
2761
2762void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2763 float x, float y, SkPaint* paint) {
2764 // Handle underline and strike-through
2765 uint32_t flags = paint->getFlags();
2766 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002767 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002768 float underlineWidth = length;
2769 // If length is > 0.0f, we already measured the text for the text alignment
2770 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002771 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002772 }
2773
Romain Guy211370f2012-02-01 16:10:55 -08002774 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002775 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002776 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002777
Raph Levien8b4072d2012-07-30 15:50:00 -07002778 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002779 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002780
Romain Guyf6834472011-01-23 13:32:12 -08002781 int linesCount = 0;
2782 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2783 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2784
2785 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002786 float points[pointsCount];
2787 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002788
2789 if (flags & SkPaint::kUnderlineText_Flag) {
2790 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002791 points[currentPoint++] = left;
2792 points[currentPoint++] = top;
2793 points[currentPoint++] = left + underlineWidth;
2794 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002795 }
2796
2797 if (flags & SkPaint::kStrikeThruText_Flag) {
2798 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002799 points[currentPoint++] = left;
2800 points[currentPoint++] = top;
2801 points[currentPoint++] = left + underlineWidth;
2802 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002803 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002804
Romain Guy726aeba2011-06-01 14:52:00 -07002805 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002806
Romain Guy726aeba2011-06-01 14:52:00 -07002807 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002808 }
2809 }
2810}
2811
Romain Guy026c5e162010-06-28 17:12:22 -07002812void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002813 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002814 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002815 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002816 color |= 0x00ffffff;
2817 }
2818
Romain Guy70ca14e2010-12-13 18:24:33 -08002819 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002820 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002821 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002822 setupDrawShader();
2823 setupDrawColorFilter();
2824 setupDrawBlending(mode);
2825 setupDrawProgram();
2826 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2827 setupDrawColorUniforms();
2828 setupDrawShaderUniforms(ignoreTransform);
2829 setupDrawColorFilterUniforms();
2830 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002831
Romain Guyc95c8d62010-09-17 15:31:32 -07002832 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2833}
2834
Romain Guy82ba8142010-07-09 13:25:56 -07002835void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002836 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002837 int alpha;
2838 SkXfermode::Mode mode;
2839 getAlphaAndMode(paint, &alpha, &mode);
2840
Romain Guyd21b6e12011-11-30 20:21:23 -08002841 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002842
Romain Guy211370f2012-02-01 16:10:55 -08002843 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002844 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2845 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2846
Romain Guyd21b6e12011-11-30 20:21:23 -08002847 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002848 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2849 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2850 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2851 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002852 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002853 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2854 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2855 GL_TRIANGLE_STRIP, gMeshCount);
2856 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002857}
2858
Romain Guybd6b79b2010-06-26 00:13:53 -07002859void OpenGLRenderer::drawTextureRect(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) {
2861 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002862 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002863}
2864
2865void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002866 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002867 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002868 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002869
Romain Guy746b7402010-10-26 16:27:31 -07002870 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002871 setupDrawWithTexture();
2872 setupDrawColor(alpha, alpha, alpha, alpha);
2873 setupDrawColorFilter();
2874 setupDrawBlending(blend, mode, swapSrcDst);
2875 setupDrawProgram();
2876 if (!dirty) {
2877 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002878 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002879 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002880 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002881 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002882 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002883 }
Romain Guy86568192010-12-14 15:55:39 -08002884 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002885 setupDrawColorFilterUniforms();
2886 setupDrawTexture(texture);
2887 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002888
Romain Guy6820ac82010-09-15 18:11:50 -07002889 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002890
2891 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002892}
2893
Romain Guya5aed0d2010-09-09 14:42:43 -07002894void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002895 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002896 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002897
Romain Guy82ba8142010-07-09 13:25:56 -07002898 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002899 // These blend modes are not supported by OpenGL directly and have
2900 // to be implemented using shaders. Since the shader will perform
2901 // the blending, turn blending off here
2902 // If the blend mode cannot be implemented using shaders, fall
2903 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07002904 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08002905 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002906 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002907 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002908
Romain Guy82bc7a72012-01-03 14:13:39 -08002909 if (mCaches.blend) {
2910 glDisable(GL_BLEND);
2911 mCaches.blend = false;
2912 }
2913
2914 return;
2915 } else {
2916 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002917 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002918 }
2919
2920 if (!mCaches.blend) {
2921 glEnable(GL_BLEND);
2922 }
2923
2924 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2925 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2926
2927 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2928 glBlendFunc(sourceMode, destMode);
2929 mCaches.lastSrcMode = sourceMode;
2930 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002931 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002932 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002933 glDisable(GL_BLEND);
2934 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002935 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002936}
2937
Romain Guy889f8d12010-07-29 14:37:42 -07002938bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002939 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002940 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002941 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002942 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002943 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002944 }
Romain Guy6926c722010-07-12 20:20:03 -07002945 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002946}
2947
Romain Guy026c5e162010-06-28 17:12:22 -07002948void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002949 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002950 TextureVertex::setUV(v++, u1, v1);
2951 TextureVertex::setUV(v++, u2, v1);
2952 TextureVertex::setUV(v++, u1, v2);
2953 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002954}
2955
Chet Haase5c13d892010-10-08 08:37:55 -07002956void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002957 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07002958 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002959}
2960
Romain Guy9d5316e2010-06-24 19:30:36 -07002961}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002962}; // namespace android