blob: 02448e8d5261201ebdf57eba2d40d139ade9cbe7 [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 Guy9ace8f52011-07-07 20:50:11 -0700646 layer->deleteTexture();
647 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700648
649 delete layer;
650
651 return false;
652 }
653#endif
654
655 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700656 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800657 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700658 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700659 glClear(GL_COLOR_BUFFER_BIT);
660
661 dirtyClip();
662
663 // Change the ortho projection
664 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
665 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
666
667 return true;
668}
669
Romain Guy1c740bc2010-09-13 18:00:09 -0700670/**
671 * Read the documentation of createLayer() before doing anything in this method.
672 */
Romain Guy1d83e192010-08-17 11:37:00 -0700673void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
674 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000675 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700676 return;
677 }
678
Romain Guy5b3b3522010-10-27 18:57:51 -0700679 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700680
681 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700682 // Detach the texture from the FBO
683 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
684
Romain Guyeb993562010-10-05 18:14:38 -0700685 // Unbind current FBO and restore previous one
686 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
687 }
688
Romain Guy1d83e192010-08-17 11:37:00 -0700689 Layer* layer = current->layer;
690 const Rect& rect = layer->layer;
691
Romain Guy9ace8f52011-07-07 20:50:11 -0700692 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700693 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700694 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700695 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700696 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700697 }
698
Romain Guy03750a02010-10-18 14:06:08 -0700699 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700700
Romain Guya1d3c912011-12-13 14:55:06 -0800701 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700702
Romain Guy5b3b3522010-10-27 18:57:51 -0700703 // When the layer is stored in an FBO, we can save a bit of fillrate by
704 // drawing only the dirty region
705 if (fboLayer) {
706 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700707 if (layer->getColorFilter()) {
708 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800709 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700710 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700711 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800712 resetColorFilter();
713 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700714 } else if (!rect.isEmpty()) {
715 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
716 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700717 }
Romain Guy8b55f372010-08-18 17:10:07 -0700718
Romain Guyeb993562010-10-05 18:14:38 -0700719 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800720 // Note: No need to use glDiscardFramebufferEXT() since we never
721 // create/compose layers that are not on screen with this
722 // code path
723 // See LayerRenderer::destroyLayer(Layer*)
724
Romain Guyeb993562010-10-05 18:14:38 -0700725 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
726 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700727 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700728 }
729
Romain Guy746b7402010-10-26 16:27:31 -0700730 dirtyClip();
731
Romain Guyeb993562010-10-05 18:14:38 -0700732 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700733 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700734 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700735 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700736 delete layer;
737 }
738}
739
Romain Guyaa6c24c2011-04-28 18:40:04 -0700740void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700741 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700742
Romain Guy302a9df2011-08-16 13:55:02 -0700743 mat4& transform = layer->getTransform();
744 if (!transform.isIdentity()) {
745 save(0);
746 mSnapshot->transform->multiply(transform);
747 }
748
Romain Guyaa6c24c2011-04-28 18:40:04 -0700749 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700750 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700751 setupDrawWithTexture();
752 } else {
753 setupDrawWithExternalTexture();
754 }
755 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700756 setupDrawColor(alpha, alpha, alpha, alpha);
757 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700758 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700759 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700760 setupDrawPureColorUniforms();
761 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700762 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
763 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700764 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700765 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700766 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700767 if (mSnapshot->transform->isPureTranslate() &&
768 layer->getWidth() == (uint32_t) rect.getWidth() &&
769 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700770 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
771 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
772
Romain Guyd21b6e12011-11-30 20:21:23 -0800773 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700774 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
775 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800776 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700777 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
778 }
779 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700780 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
781
782 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
783
784 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700785
786 if (!transform.isIdentity()) {
787 restore();
788 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700789}
790
Romain Guy5b3b3522010-10-27 18:57:51 -0700791void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700792 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700793 const Rect& texCoords = layer->texCoords;
794 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
795 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700796
Romain Guy9ace8f52011-07-07 20:50:11 -0700797 float x = rect.left;
798 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700799 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700800 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700801 layer->getHeight() == (uint32_t) rect.getHeight();
802
803 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700804 // When we're swapping, the layer is already in screen coordinates
805 if (!swap) {
806 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
807 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
808 }
809
Romain Guyd21b6e12011-11-30 20:21:23 -0800810 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700811 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800812 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700813 }
814
815 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
816 layer->getTexture(), layer->getAlpha() / 255.0f,
817 layer->getMode(), layer->isBlend(),
818 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
819 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700820
Romain Guyaa6c24c2011-04-28 18:40:04 -0700821 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
822 } else {
823 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
824 drawTextureLayer(layer, rect);
825 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
826 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700827}
828
829void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700830 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700831 layer->setRegionAsRect();
832
Romain Guy40667672011-03-18 14:34:03 -0700833 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700834
Romain Guy5b3b3522010-10-27 18:57:51 -0700835 layer->region.clear();
836 return;
837 }
838
Romain Guy8a3957d2011-09-07 17:55:15 -0700839 // TODO: See LayerRenderer.cpp::generateMesh() for important
840 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800841 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700842 size_t count;
843 const android::Rect* rects = layer->region.getArray(&count);
844
Romain Guy9ace8f52011-07-07 20:50:11 -0700845 const float alpha = layer->getAlpha() / 255.0f;
846 const float texX = 1.0f / float(layer->getWidth());
847 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800848 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700849
850 TextureVertex* mesh = mCaches.getRegionMesh();
851 GLsizei numQuads = 0;
852
Romain Guy7230a742011-01-10 22:26:16 -0800853 setupDraw();
854 setupDrawWithTexture();
855 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800856 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700857 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800858 setupDrawProgram();
859 setupDrawDirtyRegionsDisabled();
860 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800861 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700862 setupDrawTexture(layer->getTexture());
863 if (mSnapshot->transform->isPureTranslate()) {
864 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
865 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
866
Romain Guyd21b6e12011-11-30 20:21:23 -0800867 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700868 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
869 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800870 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700871 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
872 }
Romain Guy15bc6432011-12-13 13:11:32 -0800873 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700874
875 for (size_t i = 0; i < count; i++) {
876 const android::Rect* r = &rects[i];
877
878 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800879 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700880 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800881 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700882
883 // TODO: Reject quads outside of the clip
884 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
885 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
886 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
887 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
888
889 numQuads++;
890
891 if (numQuads >= REGION_MESH_QUAD_COUNT) {
892 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
893 numQuads = 0;
894 mesh = mCaches.getRegionMesh();
895 }
896 }
897
898 if (numQuads > 0) {
899 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
900 }
901
Romain Guy7230a742011-01-10 22:26:16 -0800902 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700903
904#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800905 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700906#endif
907
908 layer->region.clear();
909 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700910}
911
Romain Guy3a3133d2011-02-01 22:59:58 -0800912void OpenGLRenderer::drawRegionRects(const Region& region) {
913#if DEBUG_LAYERS_AS_REGIONS
914 size_t count;
915 const android::Rect* rects = region.getArray(&count);
916
917 uint32_t colors[] = {
918 0x7fff0000, 0x7f00ff00,
919 0x7f0000ff, 0x7fff00ff,
920 };
921
922 int offset = 0;
923 int32_t top = rects[0].top;
924
925 for (size_t i = 0; i < count; i++) {
926 if (top != rects[i].top) {
927 offset ^= 0x2;
928 top = rects[i].top;
929 }
930
931 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
932 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
933 SkXfermode::kSrcOver_Mode);
934 }
935#endif
936}
937
Romain Guy5b3b3522010-10-27 18:57:51 -0700938void OpenGLRenderer::dirtyLayer(const float left, const float top,
939 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -0800940 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700941 Rect bounds(left, top, right, bottom);
942 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800943 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700944 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700945}
946
947void OpenGLRenderer::dirtyLayer(const float left, const float top,
948 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -0800949 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700950 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800951 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800952 }
Romain Guy1bd1bad2011-01-14 20:07:20 -0800953}
954
955void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -0800956 if (bounds.intersect(*mSnapshot->clipRect)) {
957 bounds.snapToPixelBoundaries();
958 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
959 if (!dirty.isEmpty()) {
960 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700961 }
962 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700963}
964
Romain Guy54be1cd2011-06-13 19:04:27 -0700965void OpenGLRenderer::clearLayerRegions() {
966 const size_t count = mLayers.size();
967 if (count == 0) return;
968
969 if (!mSnapshot->isIgnored()) {
970 // Doing several glScissor/glClear here can negatively impact
971 // GPUs with a tiler architecture, instead we draw quads with
972 // the Clear blending mode
973
974 // The list contains bounds that have already been clipped
975 // against their initial clip rect, and the current clip
976 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -0700977 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -0700978
979 Vertex mesh[count * 6];
980 Vertex* vertex = mesh;
981
982 for (uint32_t i = 0; i < count; i++) {
983 Rect* bounds = mLayers.itemAt(i);
984
985 Vertex::set(vertex++, bounds->left, bounds->bottom);
986 Vertex::set(vertex++, bounds->left, bounds->top);
987 Vertex::set(vertex++, bounds->right, bounds->top);
988 Vertex::set(vertex++, bounds->left, bounds->bottom);
989 Vertex::set(vertex++, bounds->right, bounds->top);
990 Vertex::set(vertex++, bounds->right, bounds->bottom);
991
992 delete bounds;
993 }
994
995 setupDraw(false);
996 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
997 setupDrawBlending(true, SkXfermode::kClear_Mode);
998 setupDrawProgram();
999 setupDrawPureColorUniforms();
1000 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001001 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001002
Romain Guy54be1cd2011-06-13 19:04:27 -07001003 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001004
1005 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001006 } else {
1007 for (uint32_t i = 0; i < count; i++) {
1008 delete mLayers.itemAt(i);
1009 }
1010 }
1011
1012 mLayers.clear();
1013}
1014
Romain Guybd6b79b2010-06-26 00:13:53 -07001015///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001016// Transforms
1017///////////////////////////////////////////////////////////////////////////////
1018
1019void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001020 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001021}
1022
1023void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001024 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001025}
1026
1027void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001028 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001029}
1030
Romain Guy807daf72011-01-18 11:19:19 -08001031void OpenGLRenderer::skew(float sx, float sy) {
1032 mSnapshot->transform->skew(sx, sy);
1033}
1034
Romain Guyf6a11b82010-06-23 17:47:49 -07001035void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001036 if (matrix) {
1037 mSnapshot->transform->load(*matrix);
1038 } else {
1039 mSnapshot->transform->loadIdentity();
1040 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001041}
1042
1043void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001044 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001045}
1046
1047void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001048 SkMatrix transform;
1049 mSnapshot->transform->copyTo(transform);
1050 transform.preConcat(*matrix);
1051 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001052}
1053
1054///////////////////////////////////////////////////////////////////////////////
1055// Clipping
1056///////////////////////////////////////////////////////////////////////////////
1057
Romain Guybb9524b2010-06-22 18:56:38 -07001058void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001059 Rect clip(*mSnapshot->clipRect);
1060 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001061
Romain Guy8a4ac612012-07-17 17:32:48 -07001062 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1063 clip.getWidth(), clip.getHeight())) {
1064 mDirtyClip = false;
1065 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001066}
1067
1068const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001069 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001070}
1071
Romain Guy8a4ac612012-07-17 17:32:48 -07001072bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1073 if (mSnapshot->isIgnored()) {
1074 return true;
1075 }
1076
1077 Rect r(left, top, right, bottom);
1078 mSnapshot->transform->mapRect(r);
1079 r.snapToPixelBoundaries();
1080
1081 Rect clipRect(*mSnapshot->clipRect);
1082 clipRect.snapToPixelBoundaries();
1083
1084 return !clipRect.intersects(r);
1085}
1086
Romain Guyc7d53492010-06-25 13:41:57 -07001087bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001088 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001089 return true;
1090 }
1091
Romain Guy1d83e192010-08-17 11:37:00 -07001092 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001093 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001094 r.snapToPixelBoundaries();
1095
1096 Rect clipRect(*mSnapshot->clipRect);
1097 clipRect.snapToPixelBoundaries();
1098
Romain Guy586cae32012-07-13 15:28:31 -07001099 bool rejected = !clipRect.intersects(r);
1100 if (!isDeferred() && !rejected) {
1101 mCaches.setScissorEnabled(!clipRect.contains(r));
1102 }
1103
1104 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001105}
1106
Romain Guy079ba2c2010-07-16 14:12:24 -07001107bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1108 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001109 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001110 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001111 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001112 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001113}
1114
Chet Haasea23eed82012-04-12 15:19:04 -07001115Rect* OpenGLRenderer::getClipRect() {
1116 return mSnapshot->clipRect;
1117}
1118
Romain Guyf6a11b82010-06-23 17:47:49 -07001119///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001120// Drawing commands
1121///////////////////////////////////////////////////////////////////////////////
1122
Romain Guy54be1cd2011-06-13 19:04:27 -07001123void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001124 // TODO: It would be best if we could do this before quickReject()
1125 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001126 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001127 if (mDirtyClip) {
1128 setScissorFromClip();
1129 }
1130 mDescription.reset();
1131 mSetShaderColor = false;
1132 mColorSet = false;
1133 mColorA = mColorR = mColorG = mColorB = 0.0f;
1134 mTextureUnit = 0;
1135 mTrackDirtyRegions = true;
1136}
1137
1138void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1139 mDescription.hasTexture = true;
1140 mDescription.hasAlpha8Texture = isAlpha8;
1141}
1142
Romain Guyaa6c24c2011-04-28 18:40:04 -07001143void OpenGLRenderer::setupDrawWithExternalTexture() {
1144 mDescription.hasExternalTexture = true;
1145}
1146
Romain Guy15bc6432011-12-13 13:11:32 -08001147void OpenGLRenderer::setupDrawNoTexture() {
1148 mCaches.disbaleTexCoordsVertexArray();
1149}
1150
Chet Haase5b0200b2011-04-13 17:58:08 -07001151void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001152 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001153}
1154
Chris Craik6ebdc112012-08-31 18:24:33 -07001155void OpenGLRenderer::setupDrawAARect() {
1156 mDescription.isAARect = true;
1157}
1158
Romain Guyed6fcb02011-03-21 13:11:28 -07001159void OpenGLRenderer::setupDrawPoint(float pointSize) {
1160 mDescription.isPoint = true;
1161 mDescription.pointSize = pointSize;
1162}
1163
Romain Guy70ca14e2010-12-13 18:24:33 -08001164void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001165 setupDrawColor(color, (color >> 24) & 0xFF);
1166}
1167
1168void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1169 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001170 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1171 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001172 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001173 mColorR = a * ((color >> 16) & 0xFF);
1174 mColorG = a * ((color >> 8) & 0xFF);
1175 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001176 mColorSet = true;
1177 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1178}
1179
Romain Guy86568192010-12-14 15:55:39 -08001180void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1181 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001182 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1183 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001184 const float a = mColorA / 255.0f;
1185 mColorR = a * ((color >> 16) & 0xFF);
1186 mColorG = a * ((color >> 8) & 0xFF);
1187 mColorB = a * ((color ) & 0xFF);
1188 mColorSet = true;
1189 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1190}
1191
Romain Guy41210632012-07-16 17:04:24 -07001192void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1193 mCaches.fontRenderer->describe(mDescription, paint);
1194}
1195
Romain Guy70ca14e2010-12-13 18:24:33 -08001196void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1197 mColorA = a;
1198 mColorR = r;
1199 mColorG = g;
1200 mColorB = b;
1201 mColorSet = true;
1202 mSetShaderColor = mDescription.setColor(r, g, b, a);
1203}
1204
1205void OpenGLRenderer::setupDrawShader() {
1206 if (mShader) {
1207 mShader->describe(mDescription, mCaches.extensions);
1208 }
1209}
1210
1211void OpenGLRenderer::setupDrawColorFilter() {
1212 if (mColorFilter) {
1213 mColorFilter->describe(mDescription, mCaches.extensions);
1214 }
1215}
1216
Romain Guyf09ef512011-05-27 11:43:46 -07001217void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1218 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1219 mColorA = 1.0f;
1220 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001221 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001222 }
1223}
1224
Romain Guy70ca14e2010-12-13 18:24:33 -08001225void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001226 // When the blending mode is kClear_Mode, we need to use a modulate color
1227 // argb=1,0,0,0
1228 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001229 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1230 mDescription, swapSrcDst);
1231}
1232
1233void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001234 // When the blending mode is kClear_Mode, we need to use a modulate color
1235 // argb=1,0,0,0
1236 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001237 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1238 mDescription, swapSrcDst);
1239}
1240
1241void OpenGLRenderer::setupDrawProgram() {
1242 useProgram(mCaches.programCache.get(mDescription));
1243}
1244
1245void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1246 mTrackDirtyRegions = false;
1247}
1248
1249void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1250 bool ignoreTransform) {
1251 mModelView.loadTranslate(left, top, 0.0f);
1252 if (!ignoreTransform) {
1253 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1254 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1255 } else {
1256 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1257 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1258 }
1259}
1260
Chet Haase8a5cc922011-04-26 07:28:09 -07001261void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1262 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001263}
1264
Romain Guy70ca14e2010-12-13 18:24:33 -08001265void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1266 bool ignoreTransform, bool ignoreModelView) {
1267 if (!ignoreModelView) {
1268 mModelView.loadTranslate(left, top, 0.0f);
1269 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001270 } else {
1271 mModelView.loadIdentity();
1272 }
Romain Guy86568192010-12-14 15:55:39 -08001273 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1274 if (!ignoreTransform) {
1275 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1276 if (mTrackDirtyRegions && dirty) {
1277 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1278 }
1279 } else {
1280 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1281 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1282 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001283}
1284
Romain Guyed6fcb02011-03-21 13:11:28 -07001285void OpenGLRenderer::setupDrawPointUniforms() {
1286 int slot = mCaches.currentProgram->getUniform("pointSize");
1287 glUniform1f(slot, mDescription.pointSize);
1288}
1289
Romain Guy70ca14e2010-12-13 18:24:33 -08001290void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001291 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001292 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1293 }
1294}
1295
Romain Guy86568192010-12-14 15:55:39 -08001296void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001297 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001298 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001299 }
1300}
1301
Romain Guy70ca14e2010-12-13 18:24:33 -08001302void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1303 if (mShader) {
1304 if (ignoreTransform) {
1305 mModelView.loadInverse(*mSnapshot->transform);
1306 }
1307 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1308 }
1309}
1310
Romain Guy8d0d4782010-12-14 20:13:35 -08001311void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1312 if (mShader) {
1313 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1314 }
1315}
1316
Romain Guy70ca14e2010-12-13 18:24:33 -08001317void OpenGLRenderer::setupDrawColorFilterUniforms() {
1318 if (mColorFilter) {
1319 mColorFilter->setupProgram(mCaches.currentProgram);
1320 }
1321}
1322
Romain Guy41210632012-07-16 17:04:24 -07001323void OpenGLRenderer::setupDrawTextGammaUniforms() {
1324 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1325}
1326
Romain Guy70ca14e2010-12-13 18:24:33 -08001327void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001328 bool force = mCaches.bindMeshBuffer();
1329 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001330 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001331}
1332
1333void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1334 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001335 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001336 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001337}
1338
Romain Guyaa6c24c2011-04-28 18:40:04 -07001339void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1340 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001341 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001342 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001343}
1344
Romain Guy8f0095c2011-05-02 17:24:22 -07001345void OpenGLRenderer::setupDrawTextureTransform() {
1346 mDescription.hasTextureTransform = true;
1347}
1348
1349void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001350 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1351 GL_FALSE, &transform.data[0]);
1352}
1353
Romain Guy70ca14e2010-12-13 18:24:33 -08001354void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001355 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001356 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001357 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001358 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001359 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001360 }
Romain Guyd71dd362011-12-12 19:03:35 -08001361
Romain Guyf3a910b42011-12-12 20:35:21 -08001362 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001363 if (mCaches.currentProgram->texCoords >= 0) {
1364 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1365 }
1366
1367 mCaches.unbindIndicesBuffer();
1368}
1369
1370void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1371 bool force = mCaches.unbindMeshBuffer();
1372 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1373 if (mCaches.currentProgram->texCoords >= 0) {
1374 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001375 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001376}
1377
Chet Haase5b0200b2011-04-13 17:58:08 -07001378void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001379 bool force = mCaches.unbindMeshBuffer();
1380 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1381 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001382 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001383}
1384
1385/**
1386 * 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 -07001387 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1388 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1389 * attributes (one per vertex) are values from zero to one that tells the fragment
1390 * shader where the fragment is in relation to the line width/length overall; these values are
1391 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1392 * region of the line.
1393 * Note that we only pass down the width values in this setup function. The length coordinates
1394 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001395 */
Chet Haase99585ad2011-05-02 15:00:16 -07001396void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001397 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001398 bool force = mCaches.unbindMeshBuffer();
1399 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1400 vertices, gAAVertexStride);
1401 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001402 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001403
Romain Guy7b631422012-04-04 11:38:54 -07001404 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001405 glEnableVertexAttribArray(widthSlot);
1406 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001407
Romain Guy7b631422012-04-04 11:38:54 -07001408 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001409 glEnableVertexAttribArray(lengthSlot);
1410 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001411
Chet Haase5b0200b2011-04-13 17:58:08 -07001412 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001413 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001414}
1415
1416void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1417 glDisableVertexAttribArray(widthSlot);
1418 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001419}
1420
Romain Guy70ca14e2010-12-13 18:24:33 -08001421void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001422}
1423
1424///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001425// Drawing
1426///////////////////////////////////////////////////////////////////////////////
1427
Chet Haase1271e2c2012-04-20 09:54:27 -07001428status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001429 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001430
Romain Guy0fe478e2010-11-08 12:08:41 -08001431 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1432 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001433 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001434 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001435 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001436
Romain Guy65549432012-03-26 16:45:05 -07001437 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001438}
1439
Chet Haaseed30fd82011-04-22 16:18:45 -07001440void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1441 if (displayList) {
1442 displayList->output(*this, level);
1443 }
1444}
1445
Romain Guya168d732011-03-18 16:50:13 -07001446void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1447 int alpha;
1448 SkXfermode::Mode mode;
1449 getAlphaAndMode(paint, &alpha, &mode);
1450
Romain Guya168d732011-03-18 16:50:13 -07001451 float x = left;
1452 float y = top;
1453
Romain Guye3c26852011-07-25 16:36:01 -07001454 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001455 bool ignoreTransform = false;
1456 if (mSnapshot->transform->isPureTranslate()) {
1457 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1458 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1459 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001460 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001461 } else {
1462 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001463 }
1464
1465 setupDraw();
1466 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001467 if (paint) {
1468 setupDrawAlpha8Color(paint->getColor(), alpha);
1469 }
Romain Guya168d732011-03-18 16:50:13 -07001470 setupDrawColorFilter();
1471 setupDrawShader();
1472 setupDrawBlending(true, mode);
1473 setupDrawProgram();
1474 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001475
Romain Guya168d732011-03-18 16:50:13 -07001476 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001477 texture->setWrap(GL_CLAMP_TO_EDGE);
1478 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001479
Romain Guya168d732011-03-18 16:50:13 -07001480 setupDrawPureColorUniforms();
1481 setupDrawColorFilterUniforms();
1482 setupDrawShaderUniforms();
1483 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1484
1485 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1486
1487 finishDrawTexture();
1488}
1489
Chet Haase48659092012-05-31 15:21:51 -07001490status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001491 const float right = left + bitmap->width();
1492 const float bottom = top + bitmap->height();
1493
1494 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001495 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001496 }
1497
Romain Guya1d3c912011-12-13 14:55:06 -08001498 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001499 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001500 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001501 const AutoTexture autoCleanup(texture);
1502
Romain Guy211370f2012-02-01 16:10:55 -08001503 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001504 drawAlphaBitmap(texture, left, top, paint);
1505 } else {
1506 drawTextureRect(left, top, right, bottom, texture, paint);
1507 }
Chet Haase48659092012-05-31 15:21:51 -07001508
1509 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001510}
1511
Chet Haase48659092012-05-31 15:21:51 -07001512status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001513 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1514 const mat4 transform(*matrix);
1515 transform.mapRect(r);
1516
Romain Guy6926c722010-07-12 20:20:03 -07001517 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001518 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001519 }
1520
Romain Guya1d3c912011-12-13 14:55:06 -08001521 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001522 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001523 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001524 const AutoTexture autoCleanup(texture);
1525
Romain Guy5b3b3522010-10-27 18:57:51 -07001526 // This could be done in a cheaper way, all we need is pass the matrix
1527 // to the vertex shader. The save/restore is a bit overkill.
1528 save(SkCanvas::kMatrix_SaveFlag);
1529 concatMatrix(matrix);
1530 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1531 restore();
Chet Haase48659092012-05-31 15:21:51 -07001532
1533 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001534}
1535
Chet Haase48659092012-05-31 15:21:51 -07001536status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001537 const float right = left + bitmap->width();
1538 const float bottom = top + bitmap->height();
1539
1540 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001541 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001542 }
1543
1544 mCaches.activeTexture(0);
1545 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1546 const AutoTexture autoCleanup(texture);
1547
1548 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001549
1550 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001551}
1552
Chet Haase48659092012-05-31 15:21:51 -07001553status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001554 float* vertices, int* colors, SkPaint* paint) {
1555 // TODO: Do a quickReject
1556 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001557 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001558 }
1559
Romain Guya1d3c912011-12-13 14:55:06 -08001560 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001561 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001562 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001563 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001564
Romain Guyd21b6e12011-11-30 20:21:23 -08001565 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1566 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001567
1568 int alpha;
1569 SkXfermode::Mode mode;
1570 getAlphaAndMode(paint, &alpha, &mode);
1571
Romain Guy5a7b4662011-01-20 19:09:30 -08001572 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001573
Romain Guyb18d2d02011-02-10 15:52:54 -08001574 float left = FLT_MAX;
1575 float top = FLT_MAX;
1576 float right = FLT_MIN;
1577 float bottom = FLT_MIN;
1578
Romain Guy211370f2012-02-01 16:10:55 -08001579 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001580
Romain Guya566b7c2011-01-23 16:36:11 -08001581 // TODO: Support the colors array
1582 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001583 TextureVertex* vertex = mesh;
1584 for (int32_t y = 0; y < meshHeight; y++) {
1585 for (int32_t x = 0; x < meshWidth; x++) {
1586 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1587
1588 float u1 = float(x) / meshWidth;
1589 float u2 = float(x + 1) / meshWidth;
1590 float v1 = float(y) / meshHeight;
1591 float v2 = float(y + 1) / meshHeight;
1592
1593 int ax = i + (meshWidth + 1) * 2;
1594 int ay = ax + 1;
1595 int bx = i;
1596 int by = bx + 1;
1597 int cx = i + 2;
1598 int cy = cx + 1;
1599 int dx = i + (meshWidth + 1) * 2 + 2;
1600 int dy = dx + 1;
1601
1602 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1603 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1604 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1605
1606 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1607 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1608 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001609
Romain Guyb18d2d02011-02-10 15:52:54 -08001610 if (hasActiveLayer) {
1611 // TODO: This could be optimized to avoid unnecessary ops
1612 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1613 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1614 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1615 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1616 }
Romain Guy5a7b4662011-01-20 19:09:30 -08001617 }
1618 }
1619
Romain Guyb18d2d02011-02-10 15:52:54 -08001620 if (hasActiveLayer) {
1621 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1622 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001623
Romain Guy5a7b4662011-01-20 19:09:30 -08001624 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1625 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001626 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001627
1628 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001629}
1630
Chet Haase48659092012-05-31 15:21:51 -07001631status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001632 float srcLeft, float srcTop, float srcRight, float srcBottom,
1633 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001634 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001635 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001636 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001637 }
1638
Romain Guya1d3c912011-12-13 14:55:06 -08001639 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001640 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001641 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001642 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001643
Romain Guy8ba548f2010-06-30 19:21:21 -07001644 const float width = texture->width;
1645 const float height = texture->height;
1646
Romain Guy68169722011-08-22 17:33:33 -07001647 const float u1 = fmax(0.0f, srcLeft / width);
1648 const float v1 = fmax(0.0f, srcTop / height);
1649 const float u2 = fmin(1.0f, srcRight / width);
1650 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001651
Romain Guy03750a02010-10-18 14:06:08 -07001652 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001653 resetDrawTextureTexCoords(u1, v1, u2, v2);
1654
Romain Guy03750a02010-10-18 14:06:08 -07001655 int alpha;
1656 SkXfermode::Mode mode;
1657 getAlphaAndMode(paint, &alpha, &mode);
1658
Romain Guyd21b6e12011-11-30 20:21:23 -08001659 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1660
Romain Guy211370f2012-02-01 16:10:55 -08001661 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001662 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1663 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1664
Romain Guyb5014982011-07-28 15:39:12 -07001665 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001666 // Enable linear filtering if the source rectangle is scaled
1667 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001668 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001669 }
Romain Guyb5014982011-07-28 15:39:12 -07001670
Romain Guyd21b6e12011-11-30 20:21:23 -08001671 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001672 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1673 texture->id, alpha / 255.0f, mode, texture->blend,
1674 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1675 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1676 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001677 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001678 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1679 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1680 GL_TRIANGLE_STRIP, gMeshCount);
1681 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001682
1683 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001684
1685 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001686}
1687
Chet Haase48659092012-05-31 15:21:51 -07001688status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001689 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001690 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001691 int alpha;
1692 SkXfermode::Mode mode;
1693 getAlphaAndModeDirect(paint, &alpha, &mode);
1694
1695 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1696 left, top, right, bottom, alpha, mode);
1697}
1698
1699status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1700 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1701 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001702 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001703 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001704 }
1705
Romain Guybe6f9dc2012-07-16 12:41:17 -07001706 alpha *= mSnapshot->alpha;
1707
Romain Guya1d3c912011-12-13 14:55:06 -08001708 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001709 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001710 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001711 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001712 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1713 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001714
Romain Guy2728f962010-10-08 18:36:15 -07001715 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001716 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001717
Romain Guy211370f2012-02-01 16:10:55 -08001718 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001719 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001720 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001721 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001722 const float offsetX = left + mSnapshot->transform->getTranslateX();
1723 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001724 const size_t count = mesh->quads.size();
1725 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001726 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001727 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001728 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1729 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1730 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001731 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001732 dirtyLayer(left + bounds.left, top + bounds.top,
1733 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001734 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001735 }
1736 }
1737
Romain Guy211370f2012-02-01 16:10:55 -08001738 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001739 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1740 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1741
1742 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1743 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1744 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1745 true, !mesh->hasEmptyQuads);
1746 } else {
1747 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1748 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1749 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1750 true, !mesh->hasEmptyQuads);
1751 }
Romain Guy054dc182010-10-15 17:55:25 -07001752 }
Chet Haase48659092012-05-31 15:21:51 -07001753
1754 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001755}
1756
Chet Haase99ecdc42011-05-06 12:06:34 -07001757/**
Chet Haase858aa932011-05-12 09:06:00 -07001758 * This function uses a similar approach to that of AA lines in the drawLines() function.
Chris Craik6ebdc112012-08-31 18:24:33 -07001759 * We expand the rectangle by a half pixel in screen space on all sides. However, instead of using
1760 * a fragment shader to compute the translucency of the color from its position, we simply use a
1761 * varying parameter to define how far a given pixel is into the region.
Chet Haase858aa932011-05-12 09:06:00 -07001762 */
1763void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001764 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001765 float inverseScaleX = 1.0f;
1766 float inverseScaleY = 1.0f;
Romain Guy04299382012-07-18 17:15:41 -07001767
Chet Haase858aa932011-05-12 09:06:00 -07001768 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001769 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001770 Matrix4 *mat = mSnapshot->transform;
1771 float m00 = mat->data[Matrix4::kScaleX];
1772 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase858aa932011-05-12 09:06:00 -07001773 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07001774 float m11 = mat->data[Matrix4::kScaleY];
Chet Haase858aa932011-05-12 09:06:00 -07001775 float scaleX = sqrt(m00 * m00 + m01 * m01);
1776 float scaleY = sqrt(m10 * m10 + m11 * m11);
1777 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1778 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1779 }
1780
Chet Haase858aa932011-05-12 09:06:00 -07001781 float boundarySizeX = .5 * inverseScaleX;
1782 float boundarySizeY = .5 * inverseScaleY;
1783
Chris Craik6ebdc112012-08-31 18:24:33 -07001784 float innerLeft = left + boundarySizeX;
1785 float innerRight = right - boundarySizeX;
1786 float innerTop = top + boundarySizeY;
1787 float innerBottom = bottom - boundarySizeY;
1788
Chet Haase858aa932011-05-12 09:06:00 -07001789 // Adjust the rect by the AA boundary padding
1790 left -= boundarySizeX;
1791 right += boundarySizeX;
1792 top -= boundarySizeY;
1793 bottom += boundarySizeY;
1794
Chet Haase858aa932011-05-12 09:06:00 -07001795 if (!quickReject(left, top, right, bottom)) {
Romain Guy04299382012-07-18 17:15:41 -07001796 setupDraw();
1797 setupDrawNoTexture();
Chris Craik6ebdc112012-08-31 18:24:33 -07001798 setupDrawAARect();
Romain Guy04299382012-07-18 17:15:41 -07001799 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1800 setupDrawColorFilter();
1801 setupDrawShader();
1802 setupDrawBlending(true, mode);
1803 setupDrawProgram();
1804 setupDrawModelViewIdentity(true);
1805 setupDrawColorUniforms();
1806 setupDrawColorFilterUniforms();
1807 setupDrawShaderIdentityUniforms();
1808
Chris Craik6ebdc112012-08-31 18:24:33 -07001809 AlphaVertex rects[14];
1810 AlphaVertex* aVertices = &rects[0];
1811 void* alphaCoords = ((GLbyte*) aVertices) + gVertexAlphaOffset;
Romain Guy04299382012-07-18 17:15:41 -07001812
Chris Craik6ebdc112012-08-31 18:24:33 -07001813 bool force = mCaches.unbindMeshBuffer();
1814 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1815 aVertices, gAlphaVertexStride);
1816 mCaches.resetTexCoordsVertexPointer();
1817 mCaches.unbindIndicesBuffer();
Romain Guy04299382012-07-18 17:15:41 -07001818
Chris Craik6ebdc112012-08-31 18:24:33 -07001819 int alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
1820 glEnableVertexAttribArray(alphaSlot);
1821 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Romain Guy04299382012-07-18 17:15:41 -07001822
Chris Craik6ebdc112012-08-31 18:24:33 -07001823 // draw left
1824 AlphaVertex::set(aVertices++, left, bottom, 0);
1825 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1826 AlphaVertex::set(aVertices++, left, top, 0);
1827 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001828
Chris Craik6ebdc112012-08-31 18:24:33 -07001829 // draw top
1830 AlphaVertex::set(aVertices++, right, top, 0);
1831 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001832
Chris Craik6ebdc112012-08-31 18:24:33 -07001833 // draw right
1834 AlphaVertex::set(aVertices++, right, bottom, 0);
1835 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1836
1837 // draw bottom
1838 AlphaVertex::set(aVertices++, left, bottom, 0);
1839 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1840
1841 // draw inner rect (repeating last vertex to create degenerate bridge triangles)
1842 // TODO: also consider drawing the inner rect without the blending-forced shader, if
1843 // blending is expensive. Note: can't use drawColorRect() since it doesn't use vertex
1844 // buffers like below, resulting in slightly different transformed coordinates.
1845 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1846 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
1847 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1848 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
1849
Chet Haase858aa932011-05-12 09:06:00 -07001850 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guy7b631422012-04-04 11:38:54 -07001851
Chris Craik6ebdc112012-08-31 18:24:33 -07001852 glDrawArrays(GL_TRIANGLE_STRIP, 0, 14);
Romain Guy04299382012-07-18 17:15:41 -07001853
Chris Craik6ebdc112012-08-31 18:24:33 -07001854 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07001855 }
Chet Haase858aa932011-05-12 09:06:00 -07001856}
1857
1858/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001859 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1860 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1861 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1862 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1863 * of the line. Hairlines are more involved because we need to account for transform scaling
1864 * to end up with a one-pixel-wide line in screen space..
1865 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1866 * in combination with values that we calculate and pass down in this method. The basic approach
1867 * is that the quad we create contains both the core line area plus a bounding area in which
1868 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1869 * proportion of the width and the length of a given segment is represented by the boundary
1870 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1871 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1872 * on the inside). This ends up giving the result we want, with pixels that are completely
1873 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1874 * how far into the boundary region they are, which is determined by shader interpolation.
1875 */
Chet Haase48659092012-05-31 15:21:51 -07001876status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1877 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07001878
1879 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001880 // We use half the stroke width here because we're going to position the quad
1881 // corner vertices half of the width away from the line endpoints
1882 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001883 // A stroke width of 0 has a special meaning in Skia:
1884 // it draws a line 1 px wide regardless of current transform
1885 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001886
Chet Haase99ecdc42011-05-06 12:06:34 -07001887 float inverseScaleX = 1.0f;
1888 float inverseScaleY = 1.0f;
1889 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001890
Chet Haase8a5cc922011-04-26 07:28:09 -07001891 int alpha;
1892 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001893
Chet Haase8a5cc922011-04-26 07:28:09 -07001894 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001895 int verticesCount = count;
1896 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001897 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001898 verticesCount += (count - 4);
1899 }
1900
1901 if (isHairLine || isAA) {
1902 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1903 // the line on the screen should always be one pixel wide regardless of scale. For
1904 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001905 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001906 Matrix4 *mat = mSnapshot->transform;
1907 float m00 = mat->data[Matrix4::kScaleX];
1908 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07001909 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07001910 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07001911
Romain Guy211370f2012-02-01 16:10:55 -08001912 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1913 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001914
Chet Haase99ecdc42011-05-06 12:06:34 -07001915 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1916 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001917
Chet Haase99ecdc42011-05-06 12:06:34 -07001918 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1919 scaled = true;
1920 }
1921 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001922 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001923
1924 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07001925
1926 mCaches.enableScissor();
1927
Chet Haase8a5cc922011-04-26 07:28:09 -07001928 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001929 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001930 if (isAA) {
1931 setupDrawAALine();
1932 }
1933 setupDrawColor(paint->getColor(), alpha);
1934 setupDrawColorFilter();
1935 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001936 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001937 setupDrawProgram();
1938 setupDrawModelViewIdentity(true);
1939 setupDrawColorUniforms();
1940 setupDrawColorFilterUniforms();
1941 setupDrawShaderIdentityUniforms();
1942
1943 if (isHairLine) {
1944 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001945 halfStrokeWidth = isAA? 1 : .5;
1946 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001947 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001948 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001949 }
Romain Guy7b631422012-04-04 11:38:54 -07001950
1951 int widthSlot;
1952 int lengthSlot;
1953
Chet Haase5b0200b2011-04-13 17:58:08 -07001954 Vertex lines[verticesCount];
1955 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001956
Chet Haase99585ad2011-05-02 15:00:16 -07001957 AAVertex wLines[verticesCount];
1958 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001959
Romain Guy211370f2012-02-01 16:10:55 -08001960 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001961 setupDrawVertices(vertices);
1962 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001963 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1964 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001965 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001966 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1967 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001968 // We will need to calculate the actual width proportion on each segment for
1969 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07001970 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001971 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1972 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001973 }
1974
Chet Haase99ecdc42011-05-06 12:06:34 -07001975 AAVertex* prevAAVertex = NULL;
1976 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001977
Chet Haase99585ad2011-05-02 15:00:16 -07001978 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001979 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001980
Chet Haase5b0200b2011-04-13 17:58:08 -07001981 for (int i = 0; i < count; i += 4) {
1982 // a = start point, b = end point
1983 vec2 a(points[i], points[i + 1]);
1984 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001985
Chet Haase99585ad2011-05-02 15:00:16 -07001986 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001987 float boundaryLengthProportion = 0;
1988 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001989
Chet Haase5b0200b2011-04-13 17:58:08 -07001990 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001991 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07001992 float x = n.x;
1993 n.x = -n.y;
1994 n.y = x;
1995
Chet Haase8a5cc922011-04-26 07:28:09 -07001996 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001997 if (isAA) {
1998 float wideningFactor;
1999 if (fabs(n.x) >= fabs(n.y)) {
2000 wideningFactor = fabs(1.0f / n.x);
2001 } else {
2002 wideningFactor = fabs(1.0f / n.y);
2003 }
2004 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002005 }
Romain Guy7b631422012-04-04 11:38:54 -07002006
Chet Haase99ecdc42011-05-06 12:06:34 -07002007 if (scaled) {
2008 n.x *= inverseScaleX;
2009 n.y *= inverseScaleY;
2010 }
2011 } else if (scaled) {
2012 // Extend n by .5 pixel on each side, post-transform
2013 vec2 extendedN = n.copyNormalized();
2014 extendedN /= 2;
2015 extendedN.x *= inverseScaleX;
2016 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002017
Chet Haase99ecdc42011-05-06 12:06:34 -07002018 float extendedNLength = extendedN.length();
2019 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002020 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002021 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002022 }
Romain Guy7b631422012-04-04 11:38:54 -07002023
Chet Haase99585ad2011-05-02 15:00:16 -07002024 // aa lines expand the endpoint vertices to encompass the AA boundary
2025 if (isAA) {
2026 vec2 abVector = (b - a);
2027 length = abVector.length();
2028 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002029
Chet Haase99ecdc42011-05-06 12:06:34 -07002030 if (scaled) {
2031 abVector.x *= inverseScaleX;
2032 abVector.y *= inverseScaleY;
2033 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002034 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002035 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002036 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002037 }
Romain Guy7b631422012-04-04 11:38:54 -07002038
Chet Haase99ecdc42011-05-06 12:06:34 -07002039 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002040 a -= abVector;
2041 b += abVector;
2042 }
2043
Chet Haase5b0200b2011-04-13 17:58:08 -07002044 // Four corners of the rectangle defining a thick line
2045 vec2 p1 = a - n;
2046 vec2 p2 = a + n;
2047 vec2 p3 = b + n;
2048 vec2 p4 = b - n;
2049
Chet Haase99585ad2011-05-02 15:00:16 -07002050
Chet Haase5b0200b2011-04-13 17:58:08 -07002051 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2052 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2053 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2054 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2055
Romain Guy04299382012-07-18 17:15:41 -07002056 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002057 if (!isAA) {
2058 if (prevVertex != NULL) {
2059 // Issue two repeat vertices to create degenerate triangles to bridge
2060 // between the previous line and the new one. This is necessary because
2061 // we are creating a single triangle_strip which will contain
2062 // potentially discontinuous line segments.
2063 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2064 Vertex::set(vertices++, p1.x, p1.y);
2065 generatedVerticesCount += 2;
2066 }
Romain Guy7b631422012-04-04 11:38:54 -07002067
Chet Haase5b0200b2011-04-13 17:58:08 -07002068 Vertex::set(vertices++, p1.x, p1.y);
2069 Vertex::set(vertices++, p2.x, p2.y);
2070 Vertex::set(vertices++, p4.x, p4.y);
2071 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002072
Chet Haase5b0200b2011-04-13 17:58:08 -07002073 prevVertex = vertices - 1;
2074 generatedVerticesCount += 4;
2075 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002076 if (!isHairLine && scaled) {
2077 // Must set width proportions per-segment for scaled non-hairlines to use the
2078 // correct AA boundary dimensions
2079 if (boundaryWidthSlot < 0) {
2080 boundaryWidthSlot =
2081 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002082 }
Romain Guy7b631422012-04-04 11:38:54 -07002083
Chet Haase99ecdc42011-05-06 12:06:34 -07002084 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002085 }
Romain Guy7b631422012-04-04 11:38:54 -07002086
Chet Haase99585ad2011-05-02 15:00:16 -07002087 if (boundaryLengthSlot < 0) {
2088 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002089 }
Romain Guy7b631422012-04-04 11:38:54 -07002090
Chet Haase99ecdc42011-05-06 12:06:34 -07002091 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002092
Chet Haase5b0200b2011-04-13 17:58:08 -07002093 if (prevAAVertex != NULL) {
2094 // Issue two repeat vertices to create degenerate triangles to bridge
2095 // between the previous line and the new one. This is necessary because
2096 // we are creating a single triangle_strip which will contain
2097 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002098 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2099 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2100 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002101 generatedVerticesCount += 2;
2102 }
Romain Guy7b631422012-04-04 11:38:54 -07002103
Chet Haase99585ad2011-05-02 15:00:16 -07002104 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2105 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2106 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2107 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002108
Chet Haase5b0200b2011-04-13 17:58:08 -07002109 prevAAVertex = aaVertices - 1;
2110 generatedVerticesCount += 4;
2111 }
Romain Guy7b631422012-04-04 11:38:54 -07002112
Chet Haase99585ad2011-05-02 15:00:16 -07002113 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2114 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2115 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002116 }
2117 }
Romain Guy7b631422012-04-04 11:38:54 -07002118
Chet Haase5b0200b2011-04-13 17:58:08 -07002119 if (generatedVerticesCount > 0) {
2120 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2121 }
Romain Guy7b631422012-04-04 11:38:54 -07002122
2123 if (isAA) {
2124 finishDrawAALine(widthSlot, lengthSlot);
2125 }
Chet Haase48659092012-05-31 15:21:51 -07002126
2127 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002128}
2129
Chet Haase48659092012-05-31 15:21:51 -07002130status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2131 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002132
2133 // TODO: The paint's cap style defines whether the points are square or circular
2134 // TODO: Handle AA for round points
2135
Chet Haase5b0200b2011-04-13 17:58:08 -07002136 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002137 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002138 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002139 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002140 if (isHairLine) {
2141 // Now that we know it's hairline, we can set the effective width, to be used later
2142 strokeWidth = 1.0f;
2143 }
2144 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002145 int alpha;
2146 SkXfermode::Mode mode;
2147 getAlphaAndMode(paint, &alpha, &mode);
2148
2149 int verticesCount = count >> 1;
2150 int generatedVerticesCount = 0;
2151
2152 TextureVertex pointsData[verticesCount];
2153 TextureVertex* vertex = &pointsData[0];
2154
Romain Guy04299382012-07-18 17:15:41 -07002155 // TODO: We should optimize this method to not generate vertices for points
2156 // that lie outside of the clip.
2157 mCaches.enableScissor();
2158
Romain Guyed6fcb02011-03-21 13:11:28 -07002159 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002160 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002161 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002162 setupDrawColor(paint->getColor(), alpha);
2163 setupDrawColorFilter();
2164 setupDrawShader();
2165 setupDrawBlending(mode);
2166 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002167 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002168 setupDrawColorUniforms();
2169 setupDrawColorFilterUniforms();
2170 setupDrawPointUniforms();
2171 setupDrawShaderIdentityUniforms();
2172 setupDrawMesh(vertex);
2173
2174 for (int i = 0; i < count; i += 2) {
2175 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2176 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002177
Chet Haase8a5cc922011-04-26 07:28:09 -07002178 float left = points[i] - halfWidth;
2179 float right = points[i] + halfWidth;
2180 float top = points[i + 1] - halfWidth;
2181 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002182
Chet Haase8a5cc922011-04-26 07:28:09 -07002183 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002184 }
2185
2186 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002187
2188 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002189}
2190
Chet Haase48659092012-05-31 15:21:51 -07002191status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002192 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002193 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002194
Romain Guyae88e5e2010-10-22 17:49:18 -07002195 Rect& clip(*mSnapshot->clipRect);
2196 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002197
Romain Guy3d58c032010-07-14 16:34:53 -07002198 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002199
2200 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002201}
Romain Guy9d5316e2010-06-24 19:30:36 -07002202
Chet Haase48659092012-05-31 15:21:51 -07002203status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2204 SkPaint* paint) {
2205 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002206 const AutoTexture autoCleanup(texture);
2207
2208 const float x = left + texture->left - texture->offset;
2209 const float y = top + texture->top - texture->offset;
2210
2211 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002212
2213 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002214}
2215
Chet Haase48659092012-05-31 15:21:51 -07002216status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002217 float rx, float ry, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002218 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002219
Romain Guya1d3c912011-12-13 14:55:06 -08002220 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002221 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2222 right - left, bottom - top, rx, ry, paint);
Chet Haase48659092012-05-31 15:21:51 -07002223 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002224}
2225
Chet Haase48659092012-05-31 15:21:51 -07002226status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2227 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002228
Romain Guya1d3c912011-12-13 14:55:06 -08002229 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002230 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Chet Haase48659092012-05-31 15:21:51 -07002231 return drawShape(x - radius, y - radius, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002232}
Romain Guy01d58e42011-01-19 21:54:02 -08002233
Chet Haase48659092012-05-31 15:21:51 -07002234status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
2235 SkPaint* paint) {
2236 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002237
Romain Guya1d3c912011-12-13 14:55:06 -08002238 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002239 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002240 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002241}
2242
Chet Haase48659092012-05-31 15:21:51 -07002243status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002244 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002245 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002246
2247 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002248 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002249 }
2250
Romain Guya1d3c912011-12-13 14:55:06 -08002251 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002252 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2253 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002254 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002255}
2256
Chet Haase48659092012-05-31 15:21:51 -07002257status_t OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002258 SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002259 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002260
Romain Guya1d3c912011-12-13 14:55:06 -08002261 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002262 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002263 return drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002264}
2265
Chet Haase48659092012-05-31 15:21:51 -07002266status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002267 if (p->getStyle() != SkPaint::kFill_Style) {
Chet Haase48659092012-05-31 15:21:51 -07002268 return drawRectAsShape(left, top, right, bottom, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002269 }
2270
Romain Guy6926c722010-07-12 20:20:03 -07002271 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002272 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002273 }
2274
Romain Guy026c5e162010-06-28 17:12:22 -07002275 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002276 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002277 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2278 if (!isMode) {
2279 // Assume SRC_OVER
2280 mode = SkXfermode::kSrcOver_Mode;
2281 }
2282 } else {
2283 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002284 }
2285
Romain Guy026c5e162010-06-28 17:12:22 -07002286 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002287 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002288 drawAARect(left, top, right, bottom, color, mode);
2289 } else {
2290 drawColorRect(left, top, right, bottom, color, mode);
2291 }
Chet Haase48659092012-05-31 15:21:51 -07002292
2293 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002294}
Romain Guy9d5316e2010-06-24 19:30:36 -07002295
Raph Levien416a8472012-07-19 22:48:17 -07002296void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2297 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2298 float x, float y) {
2299 mCaches.activeTexture(0);
2300
2301 // NOTE: The drop shadow will not perform gamma correction
2302 // if shader-based correction is enabled
2303 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2304 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2305 paint, text, bytesCount, count, mShadowRadius, positions);
2306 const AutoTexture autoCleanup(shadow);
2307
2308 const float sx = x - shadow->left + mShadowDx;
2309 const float sy = y - shadow->top + mShadowDy;
2310
2311 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2312 int shadowColor = mShadowColor;
2313 if (mShader) {
2314 shadowColor = 0xffffffff;
2315 }
2316
2317 setupDraw();
2318 setupDrawWithTexture(true);
2319 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2320 setupDrawColorFilter();
2321 setupDrawShader();
2322 setupDrawBlending(true, mode);
2323 setupDrawProgram();
2324 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2325 setupDrawTexture(shadow->id);
2326 setupDrawPureColorUniforms();
2327 setupDrawColorFilterUniforms();
2328 setupDrawShaderUniforms();
2329 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2330
2331 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2332}
2333
Chet Haase48659092012-05-31 15:21:51 -07002334status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002335 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002336 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002337 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002338 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002339 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002340
Romain Guy671d6cf2012-01-18 12:39:17 -08002341 // NOTE: Skia does not support perspective transform on drawPosText yet
2342 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002343 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002344 }
2345
2346 float x = 0.0f;
2347 float y = 0.0f;
2348 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2349 if (pureTranslate) {
2350 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2351 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2352 }
2353
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002354 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002355 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2356 paint->getTextSize());
2357
2358 int alpha;
2359 SkXfermode::Mode mode;
2360 getAlphaAndMode(paint, &alpha, &mode);
2361
Raph Levien416a8472012-07-19 22:48:17 -07002362 if (CC_UNLIKELY(mHasShadow)) {
2363 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2364 0.0f, 0.0f);
2365 }
2366
Romain Guy671d6cf2012-01-18 12:39:17 -08002367 // Pick the appropriate texture filtering
2368 bool linearFilter = mSnapshot->transform->changesBounds();
2369 if (pureTranslate && !linearFilter) {
2370 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2371 }
2372
2373 mCaches.activeTexture(0);
2374 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002375 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002376 setupDrawDirtyRegionsDisabled();
2377 setupDrawWithTexture(true);
2378 setupDrawAlpha8Color(paint->getColor(), alpha);
2379 setupDrawColorFilter();
2380 setupDrawShader();
2381 setupDrawBlending(true, mode);
2382 setupDrawProgram();
2383 setupDrawModelView(x, y, x, y, pureTranslate, true);
2384 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2385 setupDrawPureColorUniforms();
2386 setupDrawColorFilterUniforms();
2387 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002388 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002389
2390 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2391 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2392
Romain Guy211370f2012-02-01 16:10:55 -08002393 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002394
2395 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2396 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002397 if (hasActiveLayer) {
2398 if (!pureTranslate) {
2399 mSnapshot->transform->mapRect(bounds);
2400 }
2401 dirtyLayerUnchecked(bounds, getRegion());
2402 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002403 }
Chet Haase48659092012-05-31 15:21:51 -07002404
2405 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002406}
2407
Romain Guyc2525952012-07-27 16:41:22 -07002408status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002409 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002410 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002411 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002412 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002413 }
2414
Chet Haasea1cff502012-02-21 13:43:44 -08002415 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002416 switch (paint->getTextAlign()) {
2417 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002418 x -= length / 2.0f;
2419 break;
2420 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002421 x -= length;
2422 break;
2423 default:
2424 break;
2425 }
2426
Romain Guycac5fd32011-12-01 20:08:50 -08002427 SkPaint::FontMetrics metrics;
2428 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002429 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002430 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002431 }
2432
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002433 const float oldX = x;
2434 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002435 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002436 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002437 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2438 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2439 }
2440
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002441#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002442 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002443 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002444#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002445
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002446 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002447 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002448 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002449
Romain Guy86568192010-12-14 15:55:39 -08002450 int alpha;
2451 SkXfermode::Mode mode;
2452 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002453
Romain Guy211370f2012-02-01 16:10:55 -08002454 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002455 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2456 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002457 }
2458
Romain Guy6620c6d2010-12-06 18:07:02 -08002459 // Pick the appropriate texture filtering
2460 bool linearFilter = mSnapshot->transform->changesBounds();
2461 if (pureTranslate && !linearFilter) {
2462 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2463 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002464
Romain Guy16c88082012-06-11 16:03:47 -07002465 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002466 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002467 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002468 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002469 setupDrawDirtyRegionsDisabled();
2470 setupDrawWithTexture(true);
2471 setupDrawAlpha8Color(paint->getColor(), alpha);
2472 setupDrawColorFilter();
2473 setupDrawShader();
2474 setupDrawBlending(true, mode);
2475 setupDrawProgram();
2476 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002477 // See comment above; the font renderer must use texture unit 0
2478 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002479 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2480 setupDrawPureColorUniforms();
2481 setupDrawColorFilterUniforms();
2482 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002483 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002484
Romain Guy6620c6d2010-12-06 18:07:02 -08002485 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002486 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2487
Romain Guy211370f2012-02-01 16:10:55 -08002488 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002489
Raph Levien996e57c2012-07-23 15:22:52 -07002490 bool status;
Raph Levien8b4072d2012-07-30 15:50:00 -07002491 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
2492 SkPaint paintCopy(*paint);
2493 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2494 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Raph Levien996e57c2012-07-23 15:22:52 -07002495 positions, hasActiveLayer ? &bounds : NULL);
2496 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002497 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2498 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002499 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002500
2501 if (status && hasActiveLayer) {
2502 if (!pureTranslate) {
2503 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002504 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002505 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002506 }
Romain Guy694b5192010-07-21 21:33:20 -07002507
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002508 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002509
2510 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002511}
2512
Chet Haase48659092012-05-31 15:21:51 -07002513status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002514 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002515 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2516 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002517 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002518 }
2519
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002520 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002521 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2522 paint->getTextSize());
2523
2524 int alpha;
2525 SkXfermode::Mode mode;
2526 getAlphaAndMode(paint, &alpha, &mode);
2527
2528 mCaches.activeTexture(0);
2529 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002530 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002531 setupDrawDirtyRegionsDisabled();
2532 setupDrawWithTexture(true);
2533 setupDrawAlpha8Color(paint->getColor(), alpha);
2534 setupDrawColorFilter();
2535 setupDrawShader();
2536 setupDrawBlending(true, mode);
2537 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002538 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002539 setupDrawTexture(fontRenderer.getTexture(true));
2540 setupDrawPureColorUniforms();
2541 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002542 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002543 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002544
Romain Guy97771732012-02-28 18:17:02 -08002545 const Rect* clip = &mSnapshot->getLocalClip();
2546 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 -08002547
Romain Guy97771732012-02-28 18:17:02 -08002548 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002549
2550 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2551 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002552 if (hasActiveLayer) {
2553 mSnapshot->transform->mapRect(bounds);
2554 dirtyLayerUnchecked(bounds, getRegion());
2555 }
Romain Guy97771732012-02-28 18:17:02 -08002556 }
Chet Haase48659092012-05-31 15:21:51 -07002557
2558 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002559}
2560
Chet Haase48659092012-05-31 15:21:51 -07002561status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2562 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002563
Romain Guya1d3c912011-12-13 14:55:06 -08002564 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002565
Romain Guy33f6beb2012-02-16 19:24:51 -08002566 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002567 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002568 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002569 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002570
Romain Guy8b55f372010-08-18 17:10:07 -07002571 const float x = texture->left - texture->offset;
2572 const float y = texture->top - texture->offset;
2573
Romain Guy01d58e42011-01-19 21:54:02 -08002574 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002575
2576 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002577}
2578
Chet Haase48659092012-05-31 15:21:51 -07002579status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guyada830f2011-01-13 12:13:20 -08002580 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Chet Haase48659092012-05-31 15:21:51 -07002581 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002582 }
2583
Romain Guy4ff0cf42012-08-06 14:51:10 -07002584 bool debugLayerUpdate = false;
2585
Romain Guy2bf68f02012-03-02 13:37:47 -08002586 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2587 OpenGLRenderer* renderer = layer->renderer;
2588 Rect& dirty = layer->dirtyRect;
2589
2590 interrupt();
2591 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2592 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002593 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002594 renderer->finish();
2595 resume();
2596
2597 dirty.setEmpty();
2598 layer->deferredUpdateScheduled = false;
2599 layer->renderer = NULL;
2600 layer->displayList = NULL;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002601
2602 debugLayerUpdate = mCaches.debugLayersUpdates;
Romain Guy2bf68f02012-03-02 13:37:47 -08002603 }
2604
Romain Guya1d3c912011-12-13 14:55:06 -08002605 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002606
Romain Guy211370f2012-02-01 16:10:55 -08002607 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002608 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002609 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002610 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002611 const float a = layer->getAlpha() / 255.0f;
2612 SkiaColorFilter *oldFilter = mColorFilter;
2613 mColorFilter = layer->getColorFilter();
Romain Guyf219da52011-01-16 12:54:25 -08002614
Romain Guyc88e3572011-01-22 00:32:12 -08002615 setupDraw();
2616 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002617 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002618 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002619 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002620 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002621 setupDrawPureColorUniforms();
2622 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002623 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002624 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002625 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2626 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002627
Romain Guyd21b6e12011-11-30 20:21:23 -08002628 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002629 setupDrawModelViewTranslate(tx, ty,
2630 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002631 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002632 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002633 setupDrawModelViewTranslate(x, y,
2634 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2635 }
Romain Guyc88e3572011-01-22 00:32:12 -08002636 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002637
Romain Guyc88e3572011-01-22 00:32:12 -08002638 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2639 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002640
Romain Guyc88e3572011-01-22 00:32:12 -08002641 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002642
Chet Haased15ebf22012-09-05 11:40:29 -07002643 mColorFilter = oldFilter;
2644
Romain Guy3a3133d2011-02-01 22:59:58 -08002645#if DEBUG_LAYERS_AS_REGIONS
2646 drawRegionRects(layer->region);
2647#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002648 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002649
2650 if (debugLayerUpdate) {
2651 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2652 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2653 }
Romain Guyf219da52011-01-16 12:54:25 -08002654 }
Chet Haase48659092012-05-31 15:21:51 -07002655
2656 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002657}
2658
Romain Guy6926c722010-07-12 20:20:03 -07002659///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002660// Shaders
2661///////////////////////////////////////////////////////////////////////////////
2662
2663void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002664 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002665}
2666
Romain Guy06f96e22010-07-30 19:18:16 -07002667void OpenGLRenderer::setupShader(SkiaShader* shader) {
2668 mShader = shader;
2669 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002670 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002671 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002672}
2673
Romain Guyd27977d2010-07-14 19:18:51 -07002674///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002675// Color filters
2676///////////////////////////////////////////////////////////////////////////////
2677
2678void OpenGLRenderer::resetColorFilter() {
2679 mColorFilter = NULL;
2680}
2681
2682void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2683 mColorFilter = filter;
2684}
2685
2686///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002687// Drop shadow
2688///////////////////////////////////////////////////////////////////////////////
2689
2690void OpenGLRenderer::resetShadow() {
2691 mHasShadow = false;
2692}
2693
2694void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2695 mHasShadow = true;
2696 mShadowRadius = radius;
2697 mShadowDx = dx;
2698 mShadowDy = dy;
2699 mShadowColor = color;
2700}
2701
2702///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002703// Draw filters
2704///////////////////////////////////////////////////////////////////////////////
2705
2706void OpenGLRenderer::resetPaintFilter() {
2707 mHasDrawFilter = false;
2708}
2709
2710void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2711 mHasDrawFilter = true;
2712 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2713 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2714}
2715
2716SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002717 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002718
2719 uint32_t flags = paint->getFlags();
2720
2721 mFilteredPaint = *paint;
2722 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2723
2724 return &mFilteredPaint;
2725}
2726
2727///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002728// Drawing implementation
2729///////////////////////////////////////////////////////////////////////////////
2730
Romain Guy01d58e42011-01-19 21:54:02 -08002731void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2732 float x, float y, SkPaint* paint) {
2733 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2734 return;
2735 }
2736
2737 int alpha;
2738 SkXfermode::Mode mode;
2739 getAlphaAndMode(paint, &alpha, &mode);
2740
2741 setupDraw();
2742 setupDrawWithTexture(true);
2743 setupDrawAlpha8Color(paint->getColor(), alpha);
2744 setupDrawColorFilter();
2745 setupDrawShader();
2746 setupDrawBlending(true, mode);
2747 setupDrawProgram();
2748 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2749 setupDrawTexture(texture->id);
2750 setupDrawPureColorUniforms();
2751 setupDrawColorFilterUniforms();
2752 setupDrawShaderUniforms();
2753 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2754
2755 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2756
2757 finishDrawTexture();
2758}
2759
Romain Guyf607bdc2010-09-10 19:20:06 -07002760// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002761#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2762#define kStdUnderline_Offset (1.0f / 9.0f)
2763#define kStdUnderline_Thickness (1.0f / 18.0f)
2764
2765void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2766 float x, float y, SkPaint* paint) {
2767 // Handle underline and strike-through
2768 uint32_t flags = paint->getFlags();
2769 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002770 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002771 float underlineWidth = length;
2772 // If length is > 0.0f, we already measured the text for the text alignment
2773 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002774 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002775 }
2776
Romain Guy211370f2012-02-01 16:10:55 -08002777 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002778 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002779 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002780
Raph Levien8b4072d2012-07-30 15:50:00 -07002781 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002782 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002783
Romain Guyf6834472011-01-23 13:32:12 -08002784 int linesCount = 0;
2785 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2786 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2787
2788 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002789 float points[pointsCount];
2790 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002791
2792 if (flags & SkPaint::kUnderlineText_Flag) {
2793 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002794 points[currentPoint++] = left;
2795 points[currentPoint++] = top;
2796 points[currentPoint++] = left + underlineWidth;
2797 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002798 }
2799
2800 if (flags & SkPaint::kStrikeThruText_Flag) {
2801 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002802 points[currentPoint++] = left;
2803 points[currentPoint++] = top;
2804 points[currentPoint++] = left + underlineWidth;
2805 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002806 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002807
Romain Guy726aeba2011-06-01 14:52:00 -07002808 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002809
Romain Guy726aeba2011-06-01 14:52:00 -07002810 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002811 }
2812 }
2813}
2814
Romain Guy026c5e162010-06-28 17:12:22 -07002815void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002816 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002817 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002818 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002819 color |= 0x00ffffff;
2820 }
2821
Romain Guy70ca14e2010-12-13 18:24:33 -08002822 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002823 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002824 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002825 setupDrawShader();
2826 setupDrawColorFilter();
2827 setupDrawBlending(mode);
2828 setupDrawProgram();
2829 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2830 setupDrawColorUniforms();
2831 setupDrawShaderUniforms(ignoreTransform);
2832 setupDrawColorFilterUniforms();
2833 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002834
Romain Guyc95c8d62010-09-17 15:31:32 -07002835 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2836}
2837
Romain Guy82ba8142010-07-09 13:25:56 -07002838void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002839 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002840 int alpha;
2841 SkXfermode::Mode mode;
2842 getAlphaAndMode(paint, &alpha, &mode);
2843
Romain Guyd21b6e12011-11-30 20:21:23 -08002844 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002845
Romain Guy211370f2012-02-01 16:10:55 -08002846 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002847 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2848 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2849
Romain Guyd21b6e12011-11-30 20:21:23 -08002850 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002851 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2852 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2853 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2854 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002855 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002856 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2857 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2858 GL_TRIANGLE_STRIP, gMeshCount);
2859 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002860}
2861
Romain Guybd6b79b2010-06-26 00:13:53 -07002862void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002863 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2864 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002865 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002866}
2867
2868void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002869 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002870 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002871 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002872
Romain Guy746b7402010-10-26 16:27:31 -07002873 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002874 setupDrawWithTexture();
2875 setupDrawColor(alpha, alpha, alpha, alpha);
2876 setupDrawColorFilter();
2877 setupDrawBlending(blend, mode, swapSrcDst);
2878 setupDrawProgram();
2879 if (!dirty) {
2880 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002881 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002882 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002883 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002884 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002885 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002886 }
Romain Guy86568192010-12-14 15:55:39 -08002887 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002888 setupDrawColorFilterUniforms();
2889 setupDrawTexture(texture);
2890 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002891
Romain Guy6820ac82010-09-15 18:11:50 -07002892 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002893
2894 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002895}
2896
Romain Guya5aed0d2010-09-09 14:42:43 -07002897void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002898 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002899 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002900
Romain Guy82ba8142010-07-09 13:25:56 -07002901 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002902 // These blend modes are not supported by OpenGL directly and have
2903 // to be implemented using shaders. Since the shader will perform
2904 // the blending, turn blending off here
2905 // If the blend mode cannot be implemented using shaders, fall
2906 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07002907 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08002908 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002909 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002910 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002911
Romain Guy82bc7a72012-01-03 14:13:39 -08002912 if (mCaches.blend) {
2913 glDisable(GL_BLEND);
2914 mCaches.blend = false;
2915 }
2916
2917 return;
2918 } else {
2919 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002920 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002921 }
2922
2923 if (!mCaches.blend) {
2924 glEnable(GL_BLEND);
2925 }
2926
2927 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2928 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2929
2930 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2931 glBlendFunc(sourceMode, destMode);
2932 mCaches.lastSrcMode = sourceMode;
2933 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002934 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002935 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002936 glDisable(GL_BLEND);
2937 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002938 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002939}
2940
Romain Guy889f8d12010-07-29 14:37:42 -07002941bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002942 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002943 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002944 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002945 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002946 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002947 }
Romain Guy6926c722010-07-12 20:20:03 -07002948 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002949}
2950
Romain Guy026c5e162010-06-28 17:12:22 -07002951void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002952 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002953 TextureVertex::setUV(v++, u1, v1);
2954 TextureVertex::setUV(v++, u2, v1);
2955 TextureVertex::setUV(v++, u1, v2);
2956 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002957}
2958
Chet Haase5c13d892010-10-08 08:37:55 -07002959void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002960 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07002961 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002962}
2963
Romain Guy9d5316e2010-06-24 19:30:36 -07002964}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002965}; // namespace android