blob: 2a8b32c4721ae839ddbdecd7ea57a2523a3699fe [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 Guydbc26d22010-10-11 17:58:29 -070048// TODO: This should be set in properties
49#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
50
Romain Guyd21b6e12011-11-30 20:21:23 -080051#define FILTER(paint) (paint && paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
52
Romain Guy9d5316e2010-06-24 19:30:36 -070053///////////////////////////////////////////////////////////////////////////////
54// Globals
55///////////////////////////////////////////////////////////////////////////////
56
Romain Guy889f8d12010-07-29 14:37:42 -070057/**
58 * Structure mapping Skia xfermodes to OpenGL blending factors.
59 */
60struct Blender {
61 SkXfermode::Mode mode;
62 GLenum src;
63 GLenum dst;
64}; // struct Blender
65
Romain Guy026c5e162010-06-28 17:12:22 -070066// In this array, the index of each Blender equals the value of the first
67// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
68static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070069 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
70 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
71 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
72 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
73 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
74 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
75 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
76 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
80 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
82 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
83 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070084};
Romain Guye4d01122010-06-16 18:44:05 -070085
Romain Guy87a76572010-09-13 18:11:21 -070086// This array contains the swapped version of each SkXfermode. For instance
87// this array's SrcOver blending mode is actually DstOver. You can refer to
88// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070089static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070090 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
92 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
93 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
94 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
96 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
97 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
98 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
99 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
100 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
101 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
103 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
104 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700105};
106
Romain Guyf6a11b82010-06-23 17:47:49 -0700107///////////////////////////////////////////////////////////////////////////////
108// Constructors/destructor
109///////////////////////////////////////////////////////////////////////////////
110
Romain Guyfb8b7632010-08-23 21:05:08 -0700111OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700112 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700113 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700114 mHasShadow = false;
Romain Guy5ff9df62012-01-23 17:09:05 -0800115 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700116
Romain Guyac670c02010-07-27 17:39:27 -0700117 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
118
Romain Guyae5575b2010-07-29 18:48:04 -0700119 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700120}
121
Romain Guy85bf02f2010-06-22 13:11:24 -0700122OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700123 // The context has already been destroyed at this point, do not call
124 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700125}
126
Romain Guyf6a11b82010-06-23 17:47:49 -0700127///////////////////////////////////////////////////////////////////////////////
Romain Guy13631f32012-01-30 17:41:55 -0800128// Debug
129///////////////////////////////////////////////////////////////////////////////
130
131void OpenGLRenderer::startMark(const char* name) const {
132 mCaches.startMark(0, name);
133}
134
135void OpenGLRenderer::endMark() const {
136 mCaches.endMark();
137}
138
139///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700140// Setup
141///////////////////////////////////////////////////////////////////////////////
142
Romain Guy530041d2012-01-25 18:56:29 -0800143uint32_t OpenGLRenderer::getStencilSize() {
144 return STENCIL_BUFFER_SIZE;
145}
146
Romain Guy49c5fc02012-05-15 11:10:01 -0700147bool OpenGLRenderer::isDeferred() {
148 return false;
149}
150
Romain Guy85bf02f2010-06-22 13:11:24 -0700151void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700152 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700153
154 mWidth = width;
155 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700156
157 mFirstSnapshot->height = height;
158 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700159
Romain Guy3e263fa2011-12-12 16:47:48 -0800160 glDisable(GL_DITHER);
Romain Guy39d252a2011-12-12 18:14:06 -0800161 glEnable(GL_SCISSOR_TEST);
Romain Guy3e263fa2011-12-12 16:47:48 -0800162 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800163
Romain Guy3e263fa2011-12-12 16:47:48 -0800164 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700165}
166
Romain Guy6b7bd242010-10-06 19:49:23 -0700167void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800168 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
169}
170
171void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800172 mCaches.clearGarbage();
173
Romain Guy8aef54f2010-09-01 15:13:49 -0700174 mSnapshot = new Snapshot(mFirstSnapshot,
175 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800176 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700177 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700178
Romain Guy7d7b5492011-01-24 16:33:45 -0800179 mSnapshot->setClip(left, top, right, bottom);
Romain Guyddf74372012-05-22 14:07:07 -0700180 mDirtyClip = opaque;
181
182 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800183
Romain Guy6b7bd242010-10-06 19:49:23 -0700184 if (!opaque) {
Romain Guyddf74372012-05-22 14:07:07 -0700185 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700186 glClear(GL_COLOR_BUFFER_BIT);
Romain Guyddf74372012-05-22 14:07:07 -0700187 } else {
188 mCaches.resetScissor();
189 }
190}
191
192void OpenGLRenderer::syncState() {
193 glViewport(0, 0, mWidth, mHeight);
194
195 if (mCaches.blend) {
196 glEnable(GL_BLEND);
197 } else {
198 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700199 }
Romain Guybb9524b2010-06-22 18:56:38 -0700200}
201
Romain Guyb025b9c2010-09-16 14:16:48 -0700202void OpenGLRenderer::finish() {
203#if DEBUG_OPENGL
204 GLenum status = GL_NO_ERROR;
205 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000206 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800207 switch (status) {
Romain Guya44a63a2012-04-26 14:05:02 -0700208 case GL_INVALID_ENUM:
209 ALOGE(" GL_INVALID_ENUM");
210 break;
211 case GL_INVALID_VALUE:
212 ALOGE(" GL_INVALID_VALUE");
213 break;
214 case GL_INVALID_OPERATION:
215 ALOGE(" GL_INVALID_OPERATION");
216 break;
Romain Guya07105b2011-01-10 21:14:18 -0800217 case GL_OUT_OF_MEMORY:
Romain Guya44a63a2012-04-26 14:05:02 -0700218 ALOGE(" Out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800219 break;
220 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700221 }
222#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800223#if DEBUG_MEMORY_USAGE
224 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800225#else
226 if (mCaches.getDebugLevel() & kDebugMemory) {
227 mCaches.dumpMemoryUsage();
228 }
Romain Guyc15008e2010-11-10 11:59:15 -0800229#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700230}
231
Romain Guy6c319ca2011-01-11 14:29:25 -0800232void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700233 if (mCaches.currentProgram) {
234 if (mCaches.currentProgram->isInUse()) {
235 mCaches.currentProgram->remove();
236 mCaches.currentProgram = NULL;
237 }
238 }
Romain Guy50c0f092010-10-19 11:42:22 -0700239 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800240 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800241 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800242 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700243}
244
Romain Guy6c319ca2011-01-11 14:29:25 -0800245void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800246 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
247
248 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800249 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
250
Romain Guyda8532c2010-08-31 11:50:35 -0700251 glEnable(GL_SCISSOR_TEST);
Romain Guy82bc7a72012-01-03 14:13:39 -0800252 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700253 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700254
Romain Guya1d3c912011-12-13 14:55:06 -0800255 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800256 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700257
Romain Guy50c0f092010-10-19 11:42:22 -0700258 mCaches.blend = true;
259 glEnable(GL_BLEND);
260 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
261 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700262}
263
Romain Guyba6be8a2012-04-23 18:22:09 -0700264void OpenGLRenderer::detachFunctor(Functor* functor) {
265 mFunctors.remove(functor);
266}
267
268void OpenGLRenderer::attachFunctor(Functor* functor) {
269 mFunctors.add(functor);
270}
271
Romain Guy8f3b8e32012-03-27 16:33:45 -0700272status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
273 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700274 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700275
Romain Guyba6be8a2012-04-23 18:22:09 -0700276 if (count > 0) {
277 SortedVector<Functor*> functors(mFunctors);
278 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700279
Romain Guyba6be8a2012-04-23 18:22:09 -0700280 DrawGlInfo info;
281 info.clipLeft = 0;
282 info.clipTop = 0;
283 info.clipRight = 0;
284 info.clipBottom = 0;
285 info.isLayer = false;
286 info.width = 0;
287 info.height = 0;
288 memset(info.transform, 0, sizeof(float) * 16);
289
290 for (size_t i = 0; i < count; i++) {
291 Functor* f = functors.itemAt(i);
292 result |= (*f)(DrawGlInfo::kModeProcess, &info);
293
Chris Craikc2c95432012-04-25 15:13:52 -0700294 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700295 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
296 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700297 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700298
Chris Craikc2c95432012-04-25 15:13:52 -0700299 if (result & DrawGlInfo::kStatusInvoke) {
300 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700301 }
302 }
303 }
304
Romain Guyc189ef52012-04-25 20:02:53 -0700305 mCaches.activeTexture(0);
306
Romain Guy8f3b8e32012-03-27 16:33:45 -0700307 return result;
308}
309
310status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800311 interrupt();
Chris Craik9e080122012-05-22 16:00:19 -0700312 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700313
Romain Guyf90f8172011-01-25 22:53:24 -0800314 if (mDirtyClip) {
315 setScissorFromClip();
316 }
Romain Guyd643bb52011-03-01 14:55:21 -0800317
Romain Guy80911b82011-03-16 15:30:12 -0700318 Rect clip(*mSnapshot->clipRect);
319 clip.snapToPixelBoundaries();
320
Romain Guyd643bb52011-03-01 14:55:21 -0800321#if RENDER_LAYERS_AS_REGIONS
322 // Since we don't know what the functor will draw, let's dirty
323 // tne entire clip region
324 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800325 dirtyLayerUnchecked(clip, getRegion());
326 }
327#endif
328
Romain Guy08aa2cb2011-03-17 11:06:57 -0700329 DrawGlInfo info;
330 info.clipLeft = clip.left;
331 info.clipTop = clip.top;
332 info.clipRight = clip.right;
333 info.clipBottom = clip.bottom;
334 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700335 info.width = getSnapshot()->viewport.getWidth();
336 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700337 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700338
Romain Guy8f3b8e32012-03-27 16:33:45 -0700339 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800340
Romain Guy8f3b8e32012-03-27 16:33:45 -0700341 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700342 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800343 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700344
Chris Craik65924a32012-04-05 17:52:11 -0700345 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700346 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700347 }
Romain Guycabfcc12011-03-07 18:06:46 -0800348 }
349
Chet Haasedaf98e92011-01-10 14:10:36 -0800350 resume();
Romain Guy65549432012-03-26 16:45:05 -0700351 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800352}
353
Romain Guyf6a11b82010-06-23 17:47:49 -0700354///////////////////////////////////////////////////////////////////////////////
355// State management
356///////////////////////////////////////////////////////////////////////////////
357
Romain Guybb9524b2010-06-22 18:56:38 -0700358int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700359 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700360}
361
362int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700363 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700364}
365
366void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700367 if (mSaveCount > 1) {
368 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700369 }
Romain Guybb9524b2010-06-22 18:56:38 -0700370}
371
372void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700373 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700374
Romain Guy8fb95422010-08-17 18:38:51 -0700375 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700376 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700377 }
Romain Guybb9524b2010-06-22 18:56:38 -0700378}
379
Romain Guy8aef54f2010-09-01 15:13:49 -0700380int OpenGLRenderer::saveSnapshot(int flags) {
381 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700382 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700383}
384
385bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700386 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700387 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700388 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700389
Romain Guybd6b79b2010-06-26 00:13:53 -0700390 sp<Snapshot> current = mSnapshot;
391 sp<Snapshot> previous = mSnapshot->previous;
392
Romain Guyeb993562010-10-05 18:14:38 -0700393 if (restoreOrtho) {
394 Rect& r = previous->viewport;
395 glViewport(r.left, r.top, r.right, r.bottom);
396 mOrthoMatrix.load(current->orthoMatrix);
397 }
398
Romain Guy8b55f372010-08-18 17:10:07 -0700399 mSaveCount--;
400 mSnapshot = previous;
401
Romain Guy2542d192010-08-18 11:47:12 -0700402 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700403 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700404 }
Romain Guy2542d192010-08-18 11:47:12 -0700405
Romain Guy5ec99242010-11-03 16:19:08 -0700406 if (restoreLayer) {
407 composeLayer(current, previous);
408 }
409
Romain Guy2542d192010-08-18 11:47:12 -0700410 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700411}
412
Romain Guyf6a11b82010-06-23 17:47:49 -0700413///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700414// Layers
415///////////////////////////////////////////////////////////////////////////////
416
417int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700418 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700419 const GLuint previousFbo = mSnapshot->fbo;
420 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700421
Romain Guyaf636eb2010-12-09 17:47:21 -0800422 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700423 int alpha = 255;
424 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700425
Romain Guye45362c2010-11-03 19:58:32 -0700426 if (p) {
427 alpha = p->getAlpha();
428 if (!mCaches.extensions.hasFramebufferFetch()) {
429 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
430 if (!isMode) {
431 // Assume SRC_OVER
432 mode = SkXfermode::kSrcOver_Mode;
433 }
434 } else {
435 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700436 }
437 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700438 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700439 }
Romain Guyd55a8612010-06-28 17:42:46 -0700440
Romain Guydbc26d22010-10-11 17:58:29 -0700441 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
442 }
Romain Guyd55a8612010-06-28 17:42:46 -0700443
444 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700445}
446
447int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
448 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700449 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700450 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700451 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700452 SkPaint paint;
453 paint.setAlpha(alpha);
454 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700455 }
Romain Guyd55a8612010-06-28 17:42:46 -0700456}
Romain Guybd6b79b2010-06-26 00:13:53 -0700457
Romain Guy1c740bc2010-09-13 18:00:09 -0700458/**
459 * Layers are viewed by Skia are slightly different than layers in image editing
460 * programs (for instance.) When a layer is created, previously created layers
461 * and the frame buffer still receive every drawing command. For instance, if a
462 * layer is created and a shape intersecting the bounds of the layers and the
463 * framebuffer is draw, the shape will be drawn on both (unless the layer was
464 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
465 *
466 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
467 * texture. Unfortunately, this is inefficient as it requires every primitive to
468 * be drawn n + 1 times, where n is the number of active layers. In practice this
469 * means, for every primitive:
470 * - Switch active frame buffer
471 * - Change viewport, clip and projection matrix
472 * - Issue the drawing
473 *
474 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700475 * To avoid this, layers are implemented in a different way here, at least in the
476 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
477 * is set. When this flag is set we can redirect all drawing operations into a
478 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700479 *
480 * This implementation relies on the frame buffer being at least RGBA 8888. When
481 * a layer is created, only a texture is created, not an FBO. The content of the
482 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700483 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700484 * buffer and drawing continues as normal. This technique therefore treats the
485 * frame buffer as a scratch buffer for the layers.
486 *
487 * To compose the layers back onto the frame buffer, each layer texture
488 * (containing the original frame buffer data) is drawn as a simple quad over
489 * the frame buffer. The trick is that the quad is set as the composition
490 * destination in the blending equation, and the frame buffer becomes the source
491 * of the composition.
492 *
493 * Drawing layers with an alpha value requires an extra step before composition.
494 * An empty quad is drawn over the layer's region in the frame buffer. This quad
495 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
496 * quad is used to multiply the colors in the frame buffer. This is achieved by
497 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
498 * GL_ZERO, GL_SRC_ALPHA.
499 *
500 * Because glCopyTexImage2D() can be slow, an alternative implementation might
501 * be use to draw a single clipped layer. The implementation described above
502 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700503 *
504 * (1) The frame buffer is actually not cleared right away. To allow the GPU
505 * to potentially optimize series of calls to glCopyTexImage2D, the frame
506 * buffer is left untouched until the first drawing operation. Only when
507 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700508 */
Romain Guyd55a8612010-06-28 17:42:46 -0700509bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700510 float right, float bottom, int alpha, SkXfermode::Mode mode,
511 int flags, GLuint previousFbo) {
512 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700513 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700514
Romain Guyeb993562010-10-05 18:14:38 -0700515 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
516
Romain Guyf607bdc2010-09-10 19:20:06 -0700517 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700518 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700519 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700520 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700521
Romain Guyeb993562010-10-05 18:14:38 -0700522 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700523 if (bounds.intersect(*snapshot->clipRect)) {
524 // We cannot work with sub-pixels in this case
525 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700526
Romain Guyad37cd32011-03-15 11:12:25 -0700527 // When the layer is not an FBO, we may use glCopyTexImage so we
528 // need to make sure the layer does not extend outside the bounds
529 // of the framebuffer
530 if (!bounds.intersect(snapshot->previous->viewport)) {
531 bounds.setEmpty();
532 }
533 } else {
534 bounds.setEmpty();
535 }
Romain Guyeb993562010-10-05 18:14:38 -0700536 }
Romain Guybf434112010-09-16 14:40:17 -0700537
Romain Guy746b7402010-10-26 16:27:31 -0700538 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
539 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800540 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700541 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700542 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700543 }
544
545 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800546 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700547 return false;
548 }
Romain Guyf18fd992010-07-08 11:45:51 -0700549
Romain Guya1d3c912011-12-13 14:55:06 -0800550 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700551 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700552 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700553 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700554 }
555
Romain Guy9ace8f52011-07-07 20:50:11 -0700556 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700557 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700558 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
559 bounds.getWidth() / float(layer->getWidth()), 0.0f);
560 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700561 layer->setBlend(true);
Romain Guydda570202010-07-06 11:39:32 -0700562
Romain Guy8fb95422010-08-17 18:38:51 -0700563 // Save the layer in the snapshot
564 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700565 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700566
Romain Guyeb993562010-10-05 18:14:38 -0700567 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700568 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700569 } else {
570 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700571 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800572 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700573 if (layer->isEmpty()) {
574 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
575 bounds.left, snapshot->height - bounds.bottom,
576 layer->getWidth(), layer->getHeight(), 0);
577 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800578 } else {
579 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
580 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
581 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700582
Romain Guy54be1cd2011-06-13 19:04:27 -0700583 // Enqueue the buffer coordinates to clear the corresponding region later
584 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700585 }
Romain Guyeb993562010-10-05 18:14:38 -0700586 }
Romain Guyf86ef572010-07-01 11:05:42 -0700587
Romain Guyd55a8612010-06-28 17:42:46 -0700588 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700589}
590
Romain Guy5b3b3522010-10-27 18:57:51 -0700591bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
592 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700593 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700594
595#if RENDER_LAYERS_AS_REGIONS
596 snapshot->region = &snapshot->layer->region;
597 snapshot->flags |= Snapshot::kFlagFboTarget;
598#endif
599
600 Rect clip(bounds);
601 snapshot->transform->mapRect(clip);
602 clip.intersect(*snapshot->clipRect);
603 clip.snapToPixelBoundaries();
604 clip.intersect(snapshot->previous->viewport);
605
606 mat4 inverse;
607 inverse.loadInverse(*mSnapshot->transform);
608
609 inverse.mapRect(clip);
610 clip.snapToPixelBoundaries();
611 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700612 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700613
614 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700615 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700616 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700617 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
618 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
619 snapshot->height = bounds.getHeight();
620 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
621 snapshot->orthoMatrix.load(mOrthoMatrix);
622
623 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700624 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
625 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700626
627 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700628 if (layer->isEmpty()) {
629 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
630 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700631 }
632
633 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700634 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700635
636#if DEBUG_LAYERS_AS_REGIONS
637 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
638 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000639 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700640
641 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700642 layer->deleteTexture();
643 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700644
645 delete layer;
646
647 return false;
648 }
649#endif
650
651 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800652 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700653 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700654 glClear(GL_COLOR_BUFFER_BIT);
655
656 dirtyClip();
657
658 // Change the ortho projection
659 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
660 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
661
662 return true;
663}
664
Romain Guy1c740bc2010-09-13 18:00:09 -0700665/**
666 * Read the documentation of createLayer() before doing anything in this method.
667 */
Romain Guy1d83e192010-08-17 11:37:00 -0700668void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
669 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000670 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700671 return;
672 }
673
Romain Guy5b3b3522010-10-27 18:57:51 -0700674 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700675
676 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700677 // Detach the texture from the FBO
678 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
679
Romain Guyeb993562010-10-05 18:14:38 -0700680 // Unbind current FBO and restore previous one
681 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
682 }
683
Romain Guy1d83e192010-08-17 11:37:00 -0700684 Layer* layer = current->layer;
685 const Rect& rect = layer->layer;
686
Romain Guy9ace8f52011-07-07 20:50:11 -0700687 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700688 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700689 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700690 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700691 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700692 }
693
Romain Guy03750a02010-10-18 14:06:08 -0700694 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700695
Romain Guya1d3c912011-12-13 14:55:06 -0800696 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700697
Romain Guy5b3b3522010-10-27 18:57:51 -0700698 // When the layer is stored in an FBO, we can save a bit of fillrate by
699 // drawing only the dirty region
700 if (fboLayer) {
701 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700702 if (layer->getColorFilter()) {
703 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800704 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700705 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700706 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800707 resetColorFilter();
708 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700709 } else if (!rect.isEmpty()) {
710 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
711 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700712 }
Romain Guy8b55f372010-08-18 17:10:07 -0700713
Romain Guyeb993562010-10-05 18:14:38 -0700714 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800715 // Note: No need to use glDiscardFramebufferEXT() since we never
716 // create/compose layers that are not on screen with this
717 // code path
718 // See LayerRenderer::destroyLayer(Layer*)
719
Romain Guyeb993562010-10-05 18:14:38 -0700720 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
721 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700722 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700723 }
724
Romain Guy746b7402010-10-26 16:27:31 -0700725 dirtyClip();
726
Romain Guyeb993562010-10-05 18:14:38 -0700727 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700728 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700729 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700730 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700731 delete layer;
732 }
733}
734
Romain Guyaa6c24c2011-04-28 18:40:04 -0700735void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700736 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700737
Romain Guy302a9df2011-08-16 13:55:02 -0700738 mat4& transform = layer->getTransform();
739 if (!transform.isIdentity()) {
740 save(0);
741 mSnapshot->transform->multiply(transform);
742 }
743
Romain Guyaa6c24c2011-04-28 18:40:04 -0700744 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700745 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700746 setupDrawWithTexture();
747 } else {
748 setupDrawWithExternalTexture();
749 }
750 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700751 setupDrawColor(alpha, alpha, alpha, alpha);
752 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700753 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700754 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700755 setupDrawPureColorUniforms();
756 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700757 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
758 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700759 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700760 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700761 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700762 if (mSnapshot->transform->isPureTranslate() &&
763 layer->getWidth() == (uint32_t) rect.getWidth() &&
764 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700765 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
766 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
767
Romain Guyd21b6e12011-11-30 20:21:23 -0800768 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700769 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
770 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800771 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700772 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
773 }
774 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700775 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
776
777 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
778
779 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700780
781 if (!transform.isIdentity()) {
782 restore();
783 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700784}
785
Romain Guy5b3b3522010-10-27 18:57:51 -0700786void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700787 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700788 const Rect& texCoords = layer->texCoords;
789 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
790 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700791
Romain Guy9ace8f52011-07-07 20:50:11 -0700792 float x = rect.left;
793 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700794 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700795 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700796 layer->getHeight() == (uint32_t) rect.getHeight();
797
798 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700799 // When we're swapping, the layer is already in screen coordinates
800 if (!swap) {
801 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
802 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
803 }
804
Romain Guyd21b6e12011-11-30 20:21:23 -0800805 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700806 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800807 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700808 }
809
810 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
811 layer->getTexture(), layer->getAlpha() / 255.0f,
812 layer->getMode(), layer->isBlend(),
813 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
814 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700815
Romain Guyaa6c24c2011-04-28 18:40:04 -0700816 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
817 } else {
818 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
819 drawTextureLayer(layer, rect);
820 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
821 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700822}
823
824void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
825#if RENDER_LAYERS_AS_REGIONS
826 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700827 layer->setRegionAsRect();
828
Romain Guy40667672011-03-18 14:34:03 -0700829 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700830
Romain Guy5b3b3522010-10-27 18:57:51 -0700831 layer->region.clear();
832 return;
833 }
834
Romain Guy8a3957d2011-09-07 17:55:15 -0700835 // TODO: See LayerRenderer.cpp::generateMesh() for important
836 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800837 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700838 size_t count;
839 const android::Rect* rects = layer->region.getArray(&count);
840
Romain Guy9ace8f52011-07-07 20:50:11 -0700841 const float alpha = layer->getAlpha() / 255.0f;
842 const float texX = 1.0f / float(layer->getWidth());
843 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800844 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700845
846 TextureVertex* mesh = mCaches.getRegionMesh();
847 GLsizei numQuads = 0;
848
Romain Guy7230a742011-01-10 22:26:16 -0800849 setupDraw();
850 setupDrawWithTexture();
851 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800852 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700853 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800854 setupDrawProgram();
855 setupDrawDirtyRegionsDisabled();
856 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800857 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700858 setupDrawTexture(layer->getTexture());
859 if (mSnapshot->transform->isPureTranslate()) {
860 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
861 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
862
Romain Guyd21b6e12011-11-30 20:21:23 -0800863 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700864 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
865 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800866 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700867 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
868 }
Romain Guy15bc6432011-12-13 13:11:32 -0800869 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700870
871 for (size_t i = 0; i < count; i++) {
872 const android::Rect* r = &rects[i];
873
874 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800875 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700876 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800877 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700878
879 // TODO: Reject quads outside of the clip
880 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
881 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
882 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
883 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
884
885 numQuads++;
886
887 if (numQuads >= REGION_MESH_QUAD_COUNT) {
888 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
889 numQuads = 0;
890 mesh = mCaches.getRegionMesh();
891 }
892 }
893
894 if (numQuads > 0) {
895 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
896 }
897
Romain Guy7230a742011-01-10 22:26:16 -0800898 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700899
900#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800901 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700902#endif
903
904 layer->region.clear();
905 }
906#else
907 composeLayerRect(layer, rect);
908#endif
909}
910
Romain Guy3a3133d2011-02-01 22:59:58 -0800911void OpenGLRenderer::drawRegionRects(const Region& region) {
912#if DEBUG_LAYERS_AS_REGIONS
913 size_t count;
914 const android::Rect* rects = region.getArray(&count);
915
916 uint32_t colors[] = {
917 0x7fff0000, 0x7f00ff00,
918 0x7f0000ff, 0x7fff00ff,
919 };
920
921 int offset = 0;
922 int32_t top = rects[0].top;
923
924 for (size_t i = 0; i < count; i++) {
925 if (top != rects[i].top) {
926 offset ^= 0x2;
927 top = rects[i].top;
928 }
929
930 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
931 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
932 SkXfermode::kSrcOver_Mode);
933 }
934#endif
935}
936
Romain Guy5b3b3522010-10-27 18:57:51 -0700937void OpenGLRenderer::dirtyLayer(const float left, const float top,
938 const float right, const float bottom, const mat4 transform) {
939#if RENDER_LAYERS_AS_REGIONS
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 }
945#endif
946}
947
948void OpenGLRenderer::dirtyLayer(const float left, const float top,
949 const float right, const float bottom) {
950#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800951 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700952 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800953 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800954 }
955#endif
956}
957
958void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
959#if RENDER_LAYERS_AS_REGIONS
960 if (bounds.intersect(*mSnapshot->clipRect)) {
961 bounds.snapToPixelBoundaries();
962 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
963 if (!dirty.isEmpty()) {
964 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700965 }
966 }
967#endif
968}
969
Romain Guy54be1cd2011-06-13 19:04:27 -0700970void OpenGLRenderer::clearLayerRegions() {
971 const size_t count = mLayers.size();
972 if (count == 0) return;
973
974 if (!mSnapshot->isIgnored()) {
975 // Doing several glScissor/glClear here can negatively impact
976 // GPUs with a tiler architecture, instead we draw quads with
977 // the Clear blending mode
978
979 // The list contains bounds that have already been clipped
980 // against their initial clip rect, and the current clip
981 // is likely different so we need to disable clipping here
982 glDisable(GL_SCISSOR_TEST);
983
984 Vertex mesh[count * 6];
985 Vertex* vertex = mesh;
986
987 for (uint32_t i = 0; i < count; i++) {
988 Rect* bounds = mLayers.itemAt(i);
989
990 Vertex::set(vertex++, bounds->left, bounds->bottom);
991 Vertex::set(vertex++, bounds->left, bounds->top);
992 Vertex::set(vertex++, bounds->right, bounds->top);
993 Vertex::set(vertex++, bounds->left, bounds->bottom);
994 Vertex::set(vertex++, bounds->right, bounds->top);
995 Vertex::set(vertex++, bounds->right, bounds->bottom);
996
997 delete bounds;
998 }
999
1000 setupDraw(false);
1001 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1002 setupDrawBlending(true, SkXfermode::kClear_Mode);
1003 setupDrawProgram();
1004 setupDrawPureColorUniforms();
1005 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001006 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001007
Romain Guy54be1cd2011-06-13 19:04:27 -07001008 glDrawArrays(GL_TRIANGLES, 0, count * 6);
1009
1010 glEnable(GL_SCISSOR_TEST);
1011 } else {
1012 for (uint32_t i = 0; i < count; i++) {
1013 delete mLayers.itemAt(i);
1014 }
1015 }
1016
1017 mLayers.clear();
1018}
1019
Romain Guybd6b79b2010-06-26 00:13:53 -07001020///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001021// Transforms
1022///////////////////////////////////////////////////////////////////////////////
1023
1024void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001025 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001026}
1027
1028void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001029 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001030}
1031
1032void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001033 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001034}
1035
Romain Guy807daf72011-01-18 11:19:19 -08001036void OpenGLRenderer::skew(float sx, float sy) {
1037 mSnapshot->transform->skew(sx, sy);
1038}
1039
Romain Guyf6a11b82010-06-23 17:47:49 -07001040void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001041 if (matrix) {
1042 mSnapshot->transform->load(*matrix);
1043 } else {
1044 mSnapshot->transform->loadIdentity();
1045 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001046}
1047
1048void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001049 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001050}
1051
1052void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001053 SkMatrix transform;
1054 mSnapshot->transform->copyTo(transform);
1055 transform.preConcat(*matrix);
1056 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001057}
1058
1059///////////////////////////////////////////////////////////////////////////////
1060// Clipping
1061///////////////////////////////////////////////////////////////////////////////
1062
Romain Guybb9524b2010-06-22 18:56:38 -07001063void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001064 Rect clip(*mSnapshot->clipRect);
1065 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001066
1067 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1068 clip.getWidth(), clip.getHeight());
1069
Romain Guy746b7402010-10-26 16:27:31 -07001070 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -07001071}
1072
1073const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001074 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001075}
1076
Romain Guyc7d53492010-06-25 13:41:57 -07001077bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001078 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001079 return true;
1080 }
1081
Romain Guy1d83e192010-08-17 11:37:00 -07001082 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001083 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001084 r.snapToPixelBoundaries();
1085
1086 Rect clipRect(*mSnapshot->clipRect);
1087 clipRect.snapToPixelBoundaries();
1088
1089 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -07001090}
1091
Romain Guy079ba2c2010-07-16 14:12:24 -07001092bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1093 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001094 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001095 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001096 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001097 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001098}
1099
Chet Haasea23eed82012-04-12 15:19:04 -07001100Rect* OpenGLRenderer::getClipRect() {
1101 return mSnapshot->clipRect;
1102}
1103
Romain Guyf6a11b82010-06-23 17:47:49 -07001104///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001105// Drawing commands
1106///////////////////////////////////////////////////////////////////////////////
1107
Romain Guy54be1cd2011-06-13 19:04:27 -07001108void OpenGLRenderer::setupDraw(bool clear) {
1109 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001110 if (mDirtyClip) {
1111 setScissorFromClip();
1112 }
1113 mDescription.reset();
1114 mSetShaderColor = false;
1115 mColorSet = false;
1116 mColorA = mColorR = mColorG = mColorB = 0.0f;
1117 mTextureUnit = 0;
1118 mTrackDirtyRegions = true;
1119}
1120
1121void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1122 mDescription.hasTexture = true;
1123 mDescription.hasAlpha8Texture = isAlpha8;
1124}
1125
Romain Guyaa6c24c2011-04-28 18:40:04 -07001126void OpenGLRenderer::setupDrawWithExternalTexture() {
1127 mDescription.hasExternalTexture = true;
1128}
1129
Romain Guy15bc6432011-12-13 13:11:32 -08001130void OpenGLRenderer::setupDrawNoTexture() {
1131 mCaches.disbaleTexCoordsVertexArray();
1132}
1133
Chet Haase5b0200b2011-04-13 17:58:08 -07001134void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001135 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001136}
1137
Romain Guyed6fcb02011-03-21 13:11:28 -07001138void OpenGLRenderer::setupDrawPoint(float pointSize) {
1139 mDescription.isPoint = true;
1140 mDescription.pointSize = pointSize;
1141}
1142
Romain Guy70ca14e2010-12-13 18:24:33 -08001143void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001144 setupDrawColor(color, (color >> 24) & 0xFF);
1145}
1146
1147void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1148 mColorA = alpha / 255.0f;
Chet Haasedb8c9a62012-03-21 18:54:18 -07001149 mColorA *= mSnapshot->alpha;
Chet Haase6cfdf452011-04-22 16:42:10 -07001150 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1151 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001152 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001153 mColorR = a * ((color >> 16) & 0xFF);
1154 mColorG = a * ((color >> 8) & 0xFF);
1155 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001156 mColorSet = true;
1157 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1158}
1159
Romain Guy86568192010-12-14 15:55:39 -08001160void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1161 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001162 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1163 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001164 const float a = mColorA / 255.0f;
1165 mColorR = a * ((color >> 16) & 0xFF);
1166 mColorG = a * ((color >> 8) & 0xFF);
1167 mColorB = a * ((color ) & 0xFF);
1168 mColorSet = true;
1169 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1170}
1171
Romain Guy70ca14e2010-12-13 18:24:33 -08001172void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1173 mColorA = a;
1174 mColorR = r;
1175 mColorG = g;
1176 mColorB = b;
1177 mColorSet = true;
1178 mSetShaderColor = mDescription.setColor(r, g, b, a);
1179}
1180
Romain Guy86568192010-12-14 15:55:39 -08001181void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1182 mColorA = a;
1183 mColorR = r;
1184 mColorG = g;
1185 mColorB = b;
1186 mColorSet = true;
1187 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1188}
1189
Romain Guy70ca14e2010-12-13 18:24:33 -08001190void OpenGLRenderer::setupDrawShader() {
1191 if (mShader) {
1192 mShader->describe(mDescription, mCaches.extensions);
1193 }
1194}
1195
1196void OpenGLRenderer::setupDrawColorFilter() {
1197 if (mColorFilter) {
1198 mColorFilter->describe(mDescription, mCaches.extensions);
1199 }
1200}
1201
Romain Guyf09ef512011-05-27 11:43:46 -07001202void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1203 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1204 mColorA = 1.0f;
1205 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001206 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001207 }
1208}
1209
Romain Guy70ca14e2010-12-13 18:24:33 -08001210void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001211 // When the blending mode is kClear_Mode, we need to use a modulate color
1212 // argb=1,0,0,0
1213 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001214 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1215 mDescription, swapSrcDst);
1216}
1217
1218void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001219 // When the blending mode is kClear_Mode, we need to use a modulate color
1220 // argb=1,0,0,0
1221 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001222 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1223 mDescription, swapSrcDst);
1224}
1225
1226void OpenGLRenderer::setupDrawProgram() {
1227 useProgram(mCaches.programCache.get(mDescription));
1228}
1229
1230void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1231 mTrackDirtyRegions = false;
1232}
1233
1234void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1235 bool ignoreTransform) {
1236 mModelView.loadTranslate(left, top, 0.0f);
1237 if (!ignoreTransform) {
1238 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1239 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1240 } else {
1241 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1242 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1243 }
1244}
1245
Chet Haase8a5cc922011-04-26 07:28:09 -07001246void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1247 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001248}
1249
Romain Guy70ca14e2010-12-13 18:24:33 -08001250void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1251 bool ignoreTransform, bool ignoreModelView) {
1252 if (!ignoreModelView) {
1253 mModelView.loadTranslate(left, top, 0.0f);
1254 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001255 } else {
1256 mModelView.loadIdentity();
1257 }
Romain Guy86568192010-12-14 15:55:39 -08001258 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1259 if (!ignoreTransform) {
1260 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1261 if (mTrackDirtyRegions && dirty) {
1262 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1263 }
1264 } else {
1265 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1266 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1267 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001268}
1269
Romain Guyed6fcb02011-03-21 13:11:28 -07001270void OpenGLRenderer::setupDrawPointUniforms() {
1271 int slot = mCaches.currentProgram->getUniform("pointSize");
1272 glUniform1f(slot, mDescription.pointSize);
1273}
1274
Romain Guy70ca14e2010-12-13 18:24:33 -08001275void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001276 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001277 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1278 }
1279}
1280
Romain Guy86568192010-12-14 15:55:39 -08001281void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001282 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001283 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001284 }
1285}
1286
Romain Guy70ca14e2010-12-13 18:24:33 -08001287void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1288 if (mShader) {
1289 if (ignoreTransform) {
1290 mModelView.loadInverse(*mSnapshot->transform);
1291 }
1292 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1293 }
1294}
1295
Romain Guy8d0d4782010-12-14 20:13:35 -08001296void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1297 if (mShader) {
1298 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1299 }
1300}
1301
Romain Guy70ca14e2010-12-13 18:24:33 -08001302void OpenGLRenderer::setupDrawColorFilterUniforms() {
1303 if (mColorFilter) {
1304 mColorFilter->setupProgram(mCaches.currentProgram);
1305 }
1306}
1307
1308void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001309 bool force = mCaches.bindMeshBuffer();
1310 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001311 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001312}
1313
1314void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1315 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001316 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001317 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001318}
1319
Romain Guyaa6c24c2011-04-28 18:40:04 -07001320void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1321 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001322 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001323 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001324}
1325
Romain Guy8f0095c2011-05-02 17:24:22 -07001326void OpenGLRenderer::setupDrawTextureTransform() {
1327 mDescription.hasTextureTransform = true;
1328}
1329
1330void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001331 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1332 GL_FALSE, &transform.data[0]);
1333}
1334
Romain Guy70ca14e2010-12-13 18:24:33 -08001335void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001336 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001337 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001338 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001339 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001340 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001341 }
Romain Guyd71dd362011-12-12 19:03:35 -08001342
Romain Guyf3a910b42011-12-12 20:35:21 -08001343 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001344 if (mCaches.currentProgram->texCoords >= 0) {
1345 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1346 }
1347
1348 mCaches.unbindIndicesBuffer();
1349}
1350
1351void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1352 bool force = mCaches.unbindMeshBuffer();
1353 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1354 if (mCaches.currentProgram->texCoords >= 0) {
1355 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001356 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001357}
1358
Chet Haase5b0200b2011-04-13 17:58:08 -07001359void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001360 bool force = mCaches.unbindMeshBuffer();
1361 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1362 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001363 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001364}
1365
1366/**
1367 * 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 -07001368 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1369 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1370 * attributes (one per vertex) are values from zero to one that tells the fragment
1371 * shader where the fragment is in relation to the line width/length overall; these values are
1372 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1373 * region of the line.
1374 * Note that we only pass down the width values in this setup function. The length coordinates
1375 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001376 */
Chet Haase99585ad2011-05-02 15:00:16 -07001377void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001378 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001379 bool force = mCaches.unbindMeshBuffer();
1380 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1381 vertices, gAAVertexStride);
1382 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001383 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001384
Romain Guy7b631422012-04-04 11:38:54 -07001385 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001386 glEnableVertexAttribArray(widthSlot);
1387 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001388
Romain Guy7b631422012-04-04 11:38:54 -07001389 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001390 glEnableVertexAttribArray(lengthSlot);
1391 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001392
Chet Haase5b0200b2011-04-13 17:58:08 -07001393 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001394 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001395
Chet Haase99585ad2011-05-02 15:00:16 -07001396 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001397 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Romain Guy7b631422012-04-04 11:38:54 -07001398 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion);
1399}
1400
1401void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1402 glDisableVertexAttribArray(widthSlot);
1403 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001404}
1405
Romain Guy70ca14e2010-12-13 18:24:33 -08001406void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001407}
1408
1409///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001410// Drawing
1411///////////////////////////////////////////////////////////////////////////////
1412
Chet Haase1271e2c2012-04-20 09:54:27 -07001413status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001414 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001415
Romain Guy0fe478e2010-11-08 12:08:41 -08001416 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1417 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001418 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001419 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001420 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001421
Romain Guy65549432012-03-26 16:45:05 -07001422 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001423}
1424
Chet Haaseed30fd82011-04-22 16:18:45 -07001425void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1426 if (displayList) {
1427 displayList->output(*this, level);
1428 }
1429}
1430
Romain Guya168d732011-03-18 16:50:13 -07001431void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1432 int alpha;
1433 SkXfermode::Mode mode;
1434 getAlphaAndMode(paint, &alpha, &mode);
1435
Romain Guya168d732011-03-18 16:50:13 -07001436 float x = left;
1437 float y = top;
1438
Romain Guye3c26852011-07-25 16:36:01 -07001439 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001440 bool ignoreTransform = false;
1441 if (mSnapshot->transform->isPureTranslate()) {
1442 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1443 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1444 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001445 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001446 } else {
1447 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001448 }
1449
1450 setupDraw();
1451 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001452 if (paint) {
1453 setupDrawAlpha8Color(paint->getColor(), alpha);
1454 }
Romain Guya168d732011-03-18 16:50:13 -07001455 setupDrawColorFilter();
1456 setupDrawShader();
1457 setupDrawBlending(true, mode);
1458 setupDrawProgram();
1459 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001460
Romain Guya168d732011-03-18 16:50:13 -07001461 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001462 texture->setWrap(GL_CLAMP_TO_EDGE);
1463 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001464
Romain Guya168d732011-03-18 16:50:13 -07001465 setupDrawPureColorUniforms();
1466 setupDrawColorFilterUniforms();
1467 setupDrawShaderUniforms();
1468 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1469
1470 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1471
1472 finishDrawTexture();
1473}
1474
Chet Haase5c13d892010-10-08 08:37:55 -07001475void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001476 const float right = left + bitmap->width();
1477 const float bottom = top + bitmap->height();
1478
1479 if (quickReject(left, top, right, bottom)) {
1480 return;
1481 }
1482
Romain Guya1d3c912011-12-13 14:55:06 -08001483 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001484 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001485 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001486 const AutoTexture autoCleanup(texture);
1487
Romain Guy211370f2012-02-01 16:10:55 -08001488 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001489 drawAlphaBitmap(texture, left, top, paint);
1490 } else {
1491 drawTextureRect(left, top, right, bottom, texture, paint);
1492 }
Romain Guyce0537b2010-06-29 21:05:21 -07001493}
1494
Chet Haase5c13d892010-10-08 08:37:55 -07001495void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001496 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1497 const mat4 transform(*matrix);
1498 transform.mapRect(r);
1499
Romain Guy6926c722010-07-12 20:20:03 -07001500 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1501 return;
1502 }
1503
Romain Guya1d3c912011-12-13 14:55:06 -08001504 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001505 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001506 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001507 const AutoTexture autoCleanup(texture);
1508
Romain Guy5b3b3522010-10-27 18:57:51 -07001509 // This could be done in a cheaper way, all we need is pass the matrix
1510 // to the vertex shader. The save/restore is a bit overkill.
1511 save(SkCanvas::kMatrix_SaveFlag);
1512 concatMatrix(matrix);
1513 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1514 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001515}
1516
Romain Guye651cc62012-05-14 19:44:40 -07001517void OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
1518 const float right = left + bitmap->width();
1519 const float bottom = top + bitmap->height();
1520
1521 if (quickReject(left, top, right, bottom)) {
1522 return;
1523 }
1524
1525 mCaches.activeTexture(0);
1526 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1527 const AutoTexture autoCleanup(texture);
1528
1529 drawTextureRect(left, top, right, bottom, texture, paint);
1530}
1531
Romain Guy5a7b4662011-01-20 19:09:30 -08001532void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1533 float* vertices, int* colors, SkPaint* paint) {
1534 // TODO: Do a quickReject
1535 if (!vertices || mSnapshot->isIgnored()) {
1536 return;
1537 }
1538
Romain Guya1d3c912011-12-13 14:55:06 -08001539 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001540 Texture* texture = mCaches.textureCache.get(bitmap);
1541 if (!texture) return;
1542 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001543
Romain Guyd21b6e12011-11-30 20:21:23 -08001544 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1545 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001546
1547 int alpha;
1548 SkXfermode::Mode mode;
1549 getAlphaAndMode(paint, &alpha, &mode);
1550
Romain Guy5a7b4662011-01-20 19:09:30 -08001551 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001552
Romain Guyb18d2d02011-02-10 15:52:54 -08001553 float left = FLT_MAX;
1554 float top = FLT_MAX;
1555 float right = FLT_MIN;
1556 float bottom = FLT_MIN;
1557
1558#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001559 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001560#else
Romain Guy211370f2012-02-01 16:10:55 -08001561 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001562#endif
1563
Romain Guya566b7c2011-01-23 16:36:11 -08001564 // TODO: Support the colors array
1565 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001566 TextureVertex* vertex = mesh;
1567 for (int32_t y = 0; y < meshHeight; y++) {
1568 for (int32_t x = 0; x < meshWidth; x++) {
1569 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1570
1571 float u1 = float(x) / meshWidth;
1572 float u2 = float(x + 1) / meshWidth;
1573 float v1 = float(y) / meshHeight;
1574 float v2 = float(y + 1) / meshHeight;
1575
1576 int ax = i + (meshWidth + 1) * 2;
1577 int ay = ax + 1;
1578 int bx = i;
1579 int by = bx + 1;
1580 int cx = i + 2;
1581 int cy = cx + 1;
1582 int dx = i + (meshWidth + 1) * 2 + 2;
1583 int dy = dx + 1;
1584
1585 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1586 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1587 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1588
1589 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1590 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1591 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001592
1593#if RENDER_LAYERS_AS_REGIONS
1594 if (hasActiveLayer) {
1595 // TODO: This could be optimized to avoid unnecessary ops
1596 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1597 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1598 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1599 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1600 }
1601#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001602 }
1603 }
1604
Romain Guyb18d2d02011-02-10 15:52:54 -08001605#if RENDER_LAYERS_AS_REGIONS
1606 if (hasActiveLayer) {
1607 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1608 }
1609#endif
1610
Romain Guy5a7b4662011-01-20 19:09:30 -08001611 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1612 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001613 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001614}
1615
Romain Guy8ba548f2010-06-30 19:21:21 -07001616void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1617 float srcLeft, float srcTop, float srcRight, float srcBottom,
1618 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001619 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001620 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1621 return;
1622 }
1623
Romain Guya1d3c912011-12-13 14:55:06 -08001624 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001625 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001626 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001627 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001628
Romain Guy8ba548f2010-06-30 19:21:21 -07001629 const float width = texture->width;
1630 const float height = texture->height;
1631
Romain Guy68169722011-08-22 17:33:33 -07001632 const float u1 = fmax(0.0f, srcLeft / width);
1633 const float v1 = fmax(0.0f, srcTop / height);
1634 const float u2 = fmin(1.0f, srcRight / width);
1635 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001636
Romain Guy03750a02010-10-18 14:06:08 -07001637 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001638 resetDrawTextureTexCoords(u1, v1, u2, v2);
1639
Romain Guy03750a02010-10-18 14:06:08 -07001640 int alpha;
1641 SkXfermode::Mode mode;
1642 getAlphaAndMode(paint, &alpha, &mode);
1643
Romain Guyd21b6e12011-11-30 20:21:23 -08001644 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1645
Romain Guy211370f2012-02-01 16:10:55 -08001646 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001647 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1648 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1649
Romain Guyb5014982011-07-28 15:39:12 -07001650 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001651 // Enable linear filtering if the source rectangle is scaled
1652 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001653 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001654 }
Romain Guyb5014982011-07-28 15:39:12 -07001655
Romain Guyd21b6e12011-11-30 20:21:23 -08001656 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001657 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1658 texture->id, alpha / 255.0f, mode, texture->blend,
1659 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1660 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1661 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001662 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001663 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1664 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1665 GL_TRIANGLE_STRIP, gMeshCount);
1666 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001667
1668 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1669}
1670
Romain Guy4aa90572010-09-26 18:40:37 -07001671void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001672 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001673 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001674 if (quickReject(left, top, right, bottom)) {
1675 return;
1676 }
1677
Romain Guya1d3c912011-12-13 14:55:06 -08001678 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001679 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001680 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001681 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001682 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1683 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001684
1685 int alpha;
1686 SkXfermode::Mode mode;
1687 getAlphaAndMode(paint, &alpha, &mode);
1688
Romain Guy2728f962010-10-08 18:36:15 -07001689 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001690 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001691
Romain Guy211370f2012-02-01 16:10:55 -08001692 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001693 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001694#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001695 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001696 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001697 const float offsetX = left + mSnapshot->transform->getTranslateX();
1698 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001699 const size_t count = mesh->quads.size();
1700 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001701 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001702 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001703 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1704 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1705 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001706 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001707 dirtyLayer(left + bounds.left, top + bounds.top,
1708 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001709 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001710 }
1711 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001712#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001713
Romain Guy211370f2012-02-01 16:10:55 -08001714 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001715 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1716 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1717
1718 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1719 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1720 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1721 true, !mesh->hasEmptyQuads);
1722 } else {
1723 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1724 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1725 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1726 true, !mesh->hasEmptyQuads);
1727 }
Romain Guy054dc182010-10-15 17:55:25 -07001728 }
Romain Guyf7f93552010-07-08 19:17:03 -07001729}
1730
Chet Haase99ecdc42011-05-06 12:06:34 -07001731/**
Chet Haase858aa932011-05-12 09:06:00 -07001732 * This function uses a similar approach to that of AA lines in the drawLines() function.
1733 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1734 * shader to compute the translucency of the color, determined by whether a given pixel is
1735 * within that boundary region and how far into the region it is.
1736 */
1737void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001738 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001739 float inverseScaleX = 1.0f;
1740 float inverseScaleY = 1.0f;
1741 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001742 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001743 Matrix4 *mat = mSnapshot->transform;
1744 float m00 = mat->data[Matrix4::kScaleX];
1745 float m01 = mat->data[Matrix4::kSkewY];
1746 float m02 = mat->data[2];
1747 float m10 = mat->data[Matrix4::kSkewX];
1748 float m11 = mat->data[Matrix4::kScaleX];
1749 float m12 = mat->data[6];
1750 float scaleX = sqrt(m00 * m00 + m01 * m01);
1751 float scaleY = sqrt(m10 * m10 + m11 * m11);
1752 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1753 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1754 }
1755
1756 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001757 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001758 setupDrawAALine();
1759 setupDrawColor(color);
1760 setupDrawColorFilter();
1761 setupDrawShader();
1762 setupDrawBlending(true, mode);
1763 setupDrawProgram();
1764 setupDrawModelViewIdentity(true);
1765 setupDrawColorUniforms();
1766 setupDrawColorFilterUniforms();
1767 setupDrawShaderIdentityUniforms();
1768
1769 AAVertex rects[4];
1770 AAVertex* aaVertices = &rects[0];
1771 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1772 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1773
1774 float boundarySizeX = .5 * inverseScaleX;
1775 float boundarySizeY = .5 * inverseScaleY;
1776
1777 // Adjust the rect by the AA boundary padding
1778 left -= boundarySizeX;
1779 right += boundarySizeX;
1780 top -= boundarySizeY;
1781 bottom += boundarySizeY;
1782
1783 float width = right - left;
1784 float height = bottom - top;
1785
Romain Guy7b631422012-04-04 11:38:54 -07001786 int widthSlot;
1787 int lengthSlot;
1788
Chet Haase858aa932011-05-12 09:06:00 -07001789 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1790 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001791 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1792 boundaryWidthProportion, widthSlot, lengthSlot);
1793
Chet Haase858aa932011-05-12 09:06:00 -07001794 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1795 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1796 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001797 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion));
Chet Haase858aa932011-05-12 09:06:00 -07001798
1799 if (!quickReject(left, top, right, bottom)) {
1800 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1801 AAVertex::set(aaVertices++, left, top, 1, 0);
1802 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1803 AAVertex::set(aaVertices++, right, top, 0, 0);
1804 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1805 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1806 }
Romain Guy7b631422012-04-04 11:38:54 -07001807
1808 finishDrawAALine(widthSlot, lengthSlot);
Chet Haase858aa932011-05-12 09:06:00 -07001809}
1810
1811/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001812 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1813 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1814 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1815 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1816 * of the line. Hairlines are more involved because we need to account for transform scaling
1817 * to end up with a one-pixel-wide line in screen space..
1818 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1819 * in combination with values that we calculate and pass down in this method. The basic approach
1820 * is that the quad we create contains both the core line area plus a bounding area in which
1821 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1822 * proportion of the width and the length of a given segment is represented by the boundary
1823 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1824 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1825 * on the inside). This ends up giving the result we want, with pixels that are completely
1826 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1827 * how far into the boundary region they are, which is determined by shader interpolation.
1828 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001829void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1830 if (mSnapshot->isIgnored()) return;
1831
1832 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001833 // We use half the stroke width here because we're going to position the quad
1834 // corner vertices half of the width away from the line endpoints
1835 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001836 // A stroke width of 0 has a special meaning in Skia:
1837 // it draws a line 1 px wide regardless of current transform
1838 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001839
Chet Haase99ecdc42011-05-06 12:06:34 -07001840 float inverseScaleX = 1.0f;
1841 float inverseScaleY = 1.0f;
1842 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001843
Chet Haase8a5cc922011-04-26 07:28:09 -07001844 int alpha;
1845 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001846
Chet Haase8a5cc922011-04-26 07:28:09 -07001847 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001848 int verticesCount = count;
1849 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001850 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001851 verticesCount += (count - 4);
1852 }
1853
1854 if (isHairLine || isAA) {
1855 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1856 // the line on the screen should always be one pixel wide regardless of scale. For
1857 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001858 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001859 Matrix4 *mat = mSnapshot->transform;
1860 float m00 = mat->data[Matrix4::kScaleX];
1861 float m01 = mat->data[Matrix4::kSkewY];
1862 float m02 = mat->data[2];
1863 float m10 = mat->data[Matrix4::kSkewX];
1864 float m11 = mat->data[Matrix4::kScaleX];
1865 float m12 = mat->data[6];
Romain Guy7b631422012-04-04 11:38:54 -07001866
Romain Guy211370f2012-02-01 16:10:55 -08001867 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1868 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001869
Chet Haase99ecdc42011-05-06 12:06:34 -07001870 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1871 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001872
Chet Haase99ecdc42011-05-06 12:06:34 -07001873 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1874 scaled = true;
1875 }
1876 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001877 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001878
1879 getAlphaAndMode(paint, &alpha, &mode);
1880 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001881 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001882 if (isAA) {
1883 setupDrawAALine();
1884 }
1885 setupDrawColor(paint->getColor(), alpha);
1886 setupDrawColorFilter();
1887 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001888 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001889 setupDrawProgram();
1890 setupDrawModelViewIdentity(true);
1891 setupDrawColorUniforms();
1892 setupDrawColorFilterUniforms();
1893 setupDrawShaderIdentityUniforms();
1894
1895 if (isHairLine) {
1896 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001897 halfStrokeWidth = isAA? 1 : .5;
1898 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001899 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001900 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001901 }
Romain Guy7b631422012-04-04 11:38:54 -07001902
1903 int widthSlot;
1904 int lengthSlot;
1905
Chet Haase5b0200b2011-04-13 17:58:08 -07001906 Vertex lines[verticesCount];
1907 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001908
Chet Haase99585ad2011-05-02 15:00:16 -07001909 AAVertex wLines[verticesCount];
1910 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001911
Romain Guy211370f2012-02-01 16:10:55 -08001912 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001913 setupDrawVertices(vertices);
1914 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001915 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1916 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001917 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001918 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1919 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001920 // We will need to calculate the actual width proportion on each segment for
1921 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1922 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001923 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1924 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001925 }
1926
Chet Haase99ecdc42011-05-06 12:06:34 -07001927 AAVertex* prevAAVertex = NULL;
1928 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001929
Chet Haase99585ad2011-05-02 15:00:16 -07001930 int boundaryLengthSlot = -1;
1931 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001932 int boundaryWidthSlot = -1;
1933 int inverseBoundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001934
Chet Haase5b0200b2011-04-13 17:58:08 -07001935 for (int i = 0; i < count; i += 4) {
1936 // a = start point, b = end point
1937 vec2 a(points[i], points[i + 1]);
1938 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001939
Chet Haase99585ad2011-05-02 15:00:16 -07001940 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001941 float boundaryLengthProportion = 0;
1942 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001943
Chet Haase5b0200b2011-04-13 17:58:08 -07001944 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001945 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001946 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001947 if (isAA) {
1948 float wideningFactor;
1949 if (fabs(n.x) >= fabs(n.y)) {
1950 wideningFactor = fabs(1.0f / n.x);
1951 } else {
1952 wideningFactor = fabs(1.0f / n.y);
1953 }
1954 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001955 }
Romain Guy7b631422012-04-04 11:38:54 -07001956
Chet Haase99ecdc42011-05-06 12:06:34 -07001957 if (scaled) {
1958 n.x *= inverseScaleX;
1959 n.y *= inverseScaleY;
1960 }
1961 } else if (scaled) {
1962 // Extend n by .5 pixel on each side, post-transform
1963 vec2 extendedN = n.copyNormalized();
1964 extendedN /= 2;
1965 extendedN.x *= inverseScaleX;
1966 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07001967
Chet Haase99ecdc42011-05-06 12:06:34 -07001968 float extendedNLength = extendedN.length();
1969 // We need to set this value on the shader prior to drawing
1970 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1971 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001972 }
Romain Guy7b631422012-04-04 11:38:54 -07001973
Chet Haase5b0200b2011-04-13 17:58:08 -07001974 float x = n.x;
1975 n.x = -n.y;
1976 n.y = x;
1977
Chet Haase99585ad2011-05-02 15:00:16 -07001978 // aa lines expand the endpoint vertices to encompass the AA boundary
1979 if (isAA) {
1980 vec2 abVector = (b - a);
1981 length = abVector.length();
1982 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07001983
Chet Haase99ecdc42011-05-06 12:06:34 -07001984 if (scaled) {
1985 abVector.x *= inverseScaleX;
1986 abVector.y *= inverseScaleY;
1987 float abLength = abVector.length();
1988 boundaryLengthProportion = abLength / (length + abLength);
1989 } else {
1990 boundaryLengthProportion = .5 / (length + 1);
1991 }
Romain Guy7b631422012-04-04 11:38:54 -07001992
Chet Haase99ecdc42011-05-06 12:06:34 -07001993 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001994 a -= abVector;
1995 b += abVector;
1996 }
1997
Chet Haase5b0200b2011-04-13 17:58:08 -07001998 // Four corners of the rectangle defining a thick line
1999 vec2 p1 = a - n;
2000 vec2 p2 = a + n;
2001 vec2 p3 = b + n;
2002 vec2 p4 = b - n;
2003
Chet Haase99585ad2011-05-02 15:00:16 -07002004
Chet Haase5b0200b2011-04-13 17:58:08 -07002005 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2006 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2007 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2008 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2009
2010 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002011 if (!isAA) {
2012 if (prevVertex != NULL) {
2013 // Issue two repeat vertices to create degenerate triangles to bridge
2014 // between the previous line and the new one. This is necessary because
2015 // we are creating a single triangle_strip which will contain
2016 // potentially discontinuous line segments.
2017 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2018 Vertex::set(vertices++, p1.x, p1.y);
2019 generatedVerticesCount += 2;
2020 }
Romain Guy7b631422012-04-04 11:38:54 -07002021
Chet Haase5b0200b2011-04-13 17:58:08 -07002022 Vertex::set(vertices++, p1.x, p1.y);
2023 Vertex::set(vertices++, p2.x, p2.y);
2024 Vertex::set(vertices++, p4.x, p4.y);
2025 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002026
Chet Haase5b0200b2011-04-13 17:58:08 -07002027 prevVertex = vertices - 1;
2028 generatedVerticesCount += 4;
2029 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002030 if (!isHairLine && scaled) {
2031 // Must set width proportions per-segment for scaled non-hairlines to use the
2032 // correct AA boundary dimensions
2033 if (boundaryWidthSlot < 0) {
2034 boundaryWidthSlot =
2035 mCaches.currentProgram->getUniform("boundaryWidth");
2036 inverseBoundaryWidthSlot =
2037 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
2038 }
Romain Guy7b631422012-04-04 11:38:54 -07002039
Chet Haase99ecdc42011-05-06 12:06:34 -07002040 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
2041 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
2042 }
Romain Guy7b631422012-04-04 11:38:54 -07002043
Chet Haase99585ad2011-05-02 15:00:16 -07002044 if (boundaryLengthSlot < 0) {
2045 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
2046 inverseBoundaryLengthSlot =
2047 mCaches.currentProgram->getUniform("inverseBoundaryLength");
2048 }
Romain Guy7b631422012-04-04 11:38:54 -07002049
Chet Haase99ecdc42011-05-06 12:06:34 -07002050 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
2051 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07002052
Chet Haase5b0200b2011-04-13 17:58:08 -07002053 if (prevAAVertex != NULL) {
2054 // Issue two repeat vertices to create degenerate triangles to bridge
2055 // between the previous line and the new one. This is necessary because
2056 // we are creating a single triangle_strip which will contain
2057 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002058 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2059 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2060 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002061 generatedVerticesCount += 2;
2062 }
Romain Guy7b631422012-04-04 11:38:54 -07002063
Chet Haase99585ad2011-05-02 15:00:16 -07002064 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2065 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2066 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2067 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002068
Chet Haase5b0200b2011-04-13 17:58:08 -07002069 prevAAVertex = aaVertices - 1;
2070 generatedVerticesCount += 4;
2071 }
Romain Guy7b631422012-04-04 11:38:54 -07002072
Chet Haase99585ad2011-05-02 15:00:16 -07002073 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2074 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2075 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002076 }
2077 }
Romain Guy7b631422012-04-04 11:38:54 -07002078
Chet Haase5b0200b2011-04-13 17:58:08 -07002079 if (generatedVerticesCount > 0) {
2080 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2081 }
Romain Guy7b631422012-04-04 11:38:54 -07002082
2083 if (isAA) {
2084 finishDrawAALine(widthSlot, lengthSlot);
2085 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002086}
2087
Romain Guyed6fcb02011-03-21 13:11:28 -07002088void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2089 if (mSnapshot->isIgnored()) return;
2090
2091 // TODO: The paint's cap style defines whether the points are square or circular
2092 // TODO: Handle AA for round points
2093
Chet Haase5b0200b2011-04-13 17:58:08 -07002094 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002095 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002096 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002097 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002098 if (isHairLine) {
2099 // Now that we know it's hairline, we can set the effective width, to be used later
2100 strokeWidth = 1.0f;
2101 }
2102 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002103 int alpha;
2104 SkXfermode::Mode mode;
2105 getAlphaAndMode(paint, &alpha, &mode);
2106
2107 int verticesCount = count >> 1;
2108 int generatedVerticesCount = 0;
2109
2110 TextureVertex pointsData[verticesCount];
2111 TextureVertex* vertex = &pointsData[0];
2112
2113 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002114 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002115 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002116 setupDrawColor(paint->getColor(), alpha);
2117 setupDrawColorFilter();
2118 setupDrawShader();
2119 setupDrawBlending(mode);
2120 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002121 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002122 setupDrawColorUniforms();
2123 setupDrawColorFilterUniforms();
2124 setupDrawPointUniforms();
2125 setupDrawShaderIdentityUniforms();
2126 setupDrawMesh(vertex);
2127
2128 for (int i = 0; i < count; i += 2) {
2129 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2130 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002131
Chet Haase8a5cc922011-04-26 07:28:09 -07002132 float left = points[i] - halfWidth;
2133 float right = points[i] + halfWidth;
2134 float top = points[i + 1] - halfWidth;
2135 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002136
Chet Haase8a5cc922011-04-26 07:28:09 -07002137 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002138 }
2139
2140 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
2141}
2142
Romain Guy85bf02f2010-06-22 13:11:24 -07002143void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002144 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08002145 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002146
Romain Guyae88e5e2010-10-22 17:49:18 -07002147 Rect& clip(*mSnapshot->clipRect);
2148 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002149
Romain Guy3d58c032010-07-14 16:34:53 -07002150 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07002151}
Romain Guy9d5316e2010-06-24 19:30:36 -07002152
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002153void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08002154 if (!texture) return;
2155 const AutoTexture autoCleanup(texture);
2156
2157 const float x = left + texture->left - texture->offset;
2158 const float y = top + texture->top - texture->offset;
2159
2160 drawPathTexture(texture, x, y, paint);
2161}
2162
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002163void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2164 float rx, float ry, SkPaint* paint) {
2165 if (mSnapshot->isIgnored()) return;
2166
Romain Guya1d3c912011-12-13 14:55:06 -08002167 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002168 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2169 right - left, bottom - top, rx, ry, paint);
2170 drawShape(left, top, texture, paint);
2171}
2172
Romain Guy01d58e42011-01-19 21:54:02 -08002173void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2174 if (mSnapshot->isIgnored()) return;
2175
Romain Guya1d3c912011-12-13 14:55:06 -08002176 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002177 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002178 drawShape(x - radius, y - radius, texture, paint);
2179}
Romain Guy01d58e42011-01-19 21:54:02 -08002180
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002181void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2182 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002183
Romain Guya1d3c912011-12-13 14:55:06 -08002184 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002185 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2186 drawShape(left, top, texture, paint);
2187}
2188
Romain Guy8b2f5262011-01-23 16:15:02 -08002189void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2190 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2191 if (mSnapshot->isIgnored()) return;
2192
2193 if (fabs(sweepAngle) >= 360.0f) {
2194 drawOval(left, top, right, bottom, paint);
2195 return;
2196 }
2197
Romain Guya1d3c912011-12-13 14:55:06 -08002198 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002199 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2200 startAngle, sweepAngle, useCenter, paint);
2201 drawShape(left, top, texture, paint);
2202}
2203
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002204void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2205 SkPaint* paint) {
2206 if (mSnapshot->isIgnored()) return;
2207
Romain Guya1d3c912011-12-13 14:55:06 -08002208 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002209 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2210 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002211}
2212
Chet Haase5c13d892010-10-08 08:37:55 -07002213void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002214 if (p->getStyle() != SkPaint::kFill_Style) {
2215 drawRectAsShape(left, top, right, bottom, p);
2216 return;
2217 }
2218
Romain Guy6926c722010-07-12 20:20:03 -07002219 if (quickReject(left, top, right, bottom)) {
2220 return;
2221 }
2222
Romain Guy026c5e162010-06-28 17:12:22 -07002223 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002224 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002225 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2226 if (!isMode) {
2227 // Assume SRC_OVER
2228 mode = SkXfermode::kSrcOver_Mode;
2229 }
2230 } else {
2231 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002232 }
2233
Romain Guy026c5e162010-06-28 17:12:22 -07002234 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002235 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002236 drawAARect(left, top, right, bottom, color, mode);
2237 } else {
2238 drawColorRect(left, top, right, bottom, color, mode);
2239 }
Romain Guyc7d53492010-06-25 13:41:57 -07002240}
Romain Guy9d5316e2010-06-24 19:30:36 -07002241
Romain Guyeb9a5362012-01-17 17:39:26 -08002242void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
2243 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002244 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2245 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guyeb9a5362012-01-17 17:39:26 -08002246 return;
2247 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002248
Romain Guy671d6cf2012-01-18 12:39:17 -08002249 // NOTE: Skia does not support perspective transform on drawPosText yet
2250 if (!mSnapshot->transform->isSimple()) {
2251 return;
2252 }
2253
2254 float x = 0.0f;
2255 float y = 0.0f;
2256 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2257 if (pureTranslate) {
2258 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2259 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2260 }
2261
2262 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2263 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2264 paint->getTextSize());
2265
2266 int alpha;
2267 SkXfermode::Mode mode;
2268 getAlphaAndMode(paint, &alpha, &mode);
2269
2270 // Pick the appropriate texture filtering
2271 bool linearFilter = mSnapshot->transform->changesBounds();
2272 if (pureTranslate && !linearFilter) {
2273 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2274 }
2275
2276 mCaches.activeTexture(0);
2277 setupDraw();
2278 setupDrawDirtyRegionsDisabled();
2279 setupDrawWithTexture(true);
2280 setupDrawAlpha8Color(paint->getColor(), alpha);
2281 setupDrawColorFilter();
2282 setupDrawShader();
2283 setupDrawBlending(true, mode);
2284 setupDrawProgram();
2285 setupDrawModelView(x, y, x, y, pureTranslate, true);
2286 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2287 setupDrawPureColorUniforms();
2288 setupDrawColorFilterUniforms();
2289 setupDrawShaderUniforms(pureTranslate);
2290
2291 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2292 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2293
2294#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002295 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002296#else
Romain Guy211370f2012-02-01 16:10:55 -08002297 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002298#endif
2299
2300 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2301 positions, hasActiveLayer ? &bounds : NULL)) {
2302#if RENDER_LAYERS_AS_REGIONS
2303 if (hasActiveLayer) {
2304 if (!pureTranslate) {
2305 mSnapshot->transform->mapRect(bounds);
2306 }
2307 dirtyLayerUnchecked(bounds, getRegion());
2308 }
2309#endif
2310 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002311}
2312
Romain Guye8e62a42010-07-23 18:55:21 -07002313void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002314 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002315 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2316 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07002317 return;
2318 }
2319
Chet Haasea1cff502012-02-21 13:43:44 -08002320 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002321 switch (paint->getTextAlign()) {
2322 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002323 x -= length / 2.0f;
2324 break;
2325 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002326 x -= length;
2327 break;
2328 default:
2329 break;
2330 }
2331
Romain Guycac5fd32011-12-01 20:08:50 -08002332 SkPaint::FontMetrics metrics;
2333 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002334 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Romain Guycac5fd32011-12-01 20:08:50 -08002335 return;
2336 }
2337
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002338 const float oldX = x;
2339 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002340 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002341 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002342 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2343 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2344 }
2345
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002346#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002347 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002348#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002349
2350 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002351 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002352 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002353
Romain Guy86568192010-12-14 15:55:39 -08002354 int alpha;
2355 SkXfermode::Mode mode;
2356 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002357
Romain Guy211370f2012-02-01 16:10:55 -08002358 if (CC_UNLIKELY(mHasShadow)) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002359 mCaches.activeTexture(0);
2360
Romain Guyb45c0c92010-08-26 20:35:23 -07002361 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002362 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2363 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002364 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002365
Romain Guy740bf2b2011-04-26 15:33:10 -07002366 const float sx = oldX - shadow->left + mShadowDx;
2367 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002368
Romain Guy86568192010-12-14 15:55:39 -08002369 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002370 int shadowColor = mShadowColor;
2371 if (mShader) {
2372 shadowColor = 0xffffffff;
2373 }
Romain Guy86568192010-12-14 15:55:39 -08002374
Romain Guy86568192010-12-14 15:55:39 -08002375 setupDraw();
2376 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002377 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2378 setupDrawColorFilter();
2379 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002380 setupDrawBlending(true, mode);
2381 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002382 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002383 setupDrawTexture(shadow->id);
2384 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002385 setupDrawColorFilterUniforms();
2386 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002387 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2388
Romain Guy0a417492010-08-16 20:26:20 -07002389 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002390 }
2391
Romain Guy6620c6d2010-12-06 18:07:02 -08002392 // Pick the appropriate texture filtering
2393 bool linearFilter = mSnapshot->transform->changesBounds();
2394 if (pureTranslate && !linearFilter) {
2395 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2396 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002397
Romain Guya1d3c912011-12-13 14:55:06 -08002398 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002399 setupDraw();
2400 setupDrawDirtyRegionsDisabled();
2401 setupDrawWithTexture(true);
2402 setupDrawAlpha8Color(paint->getColor(), alpha);
2403 setupDrawColorFilter();
2404 setupDrawShader();
2405 setupDrawBlending(true, mode);
2406 setupDrawProgram();
2407 setupDrawModelView(x, y, x, y, pureTranslate, true);
2408 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2409 setupDrawPureColorUniforms();
2410 setupDrawColorFilterUniforms();
2411 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002412
Romain Guy6620c6d2010-12-06 18:07:02 -08002413 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002414 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2415
2416#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002417 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002418#else
Romain Guy211370f2012-02-01 16:10:55 -08002419 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002420#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002421
Romain Guy6620c6d2010-12-06 18:07:02 -08002422 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002423 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002424#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002425 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002426 if (!pureTranslate) {
2427 mSnapshot->transform->mapRect(bounds);
2428 }
Romain Guyf219da52011-01-16 12:54:25 -08002429 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002430 }
2431#endif
2432 }
Romain Guy694b5192010-07-21 21:33:20 -07002433
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002434 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002435}
2436
Romain Guy325740f2012-02-24 16:48:34 -08002437void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
2438 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002439 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2440 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
2441 return;
2442 }
2443
Romain Guy03d58522012-02-24 17:54:07 -08002444 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2445 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2446 paint->getTextSize());
2447
2448 int alpha;
2449 SkXfermode::Mode mode;
2450 getAlphaAndMode(paint, &alpha, &mode);
2451
2452 mCaches.activeTexture(0);
2453 setupDraw();
2454 setupDrawDirtyRegionsDisabled();
2455 setupDrawWithTexture(true);
2456 setupDrawAlpha8Color(paint->getColor(), alpha);
2457 setupDrawColorFilter();
2458 setupDrawShader();
2459 setupDrawBlending(true, mode);
2460 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002461 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002462 setupDrawTexture(fontRenderer.getTexture(true));
2463 setupDrawPureColorUniforms();
2464 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002465 setupDrawShaderUniforms(false);
Romain Guy03d58522012-02-24 17:54:07 -08002466
Romain Guy97771732012-02-28 18:17:02 -08002467 const Rect* clip = &mSnapshot->getLocalClip();
2468 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 -08002469
Romain Guy97771732012-02-28 18:17:02 -08002470#if RENDER_LAYERS_AS_REGIONS
2471 const bool hasActiveLayer = hasLayer();
2472#else
2473 const bool hasActiveLayer = false;
2474#endif
2475
2476 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2477 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2478#if RENDER_LAYERS_AS_REGIONS
2479 if (hasActiveLayer) {
2480 mSnapshot->transform->mapRect(bounds);
2481 dirtyLayerUnchecked(bounds, getRegion());
2482 }
2483#endif
2484 }
Romain Guy325740f2012-02-24 16:48:34 -08002485}
2486
Romain Guy7fbcc042010-08-04 15:40:07 -07002487void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002488 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002489
Romain Guya1d3c912011-12-13 14:55:06 -08002490 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002491
Romain Guy33f6beb2012-02-16 19:24:51 -08002492 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002493 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002494 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002495 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002496
Romain Guy8b55f372010-08-18 17:10:07 -07002497 const float x = texture->left - texture->offset;
2498 const float y = texture->top - texture->offset;
2499
Romain Guy01d58e42011-01-19 21:54:02 -08002500 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002501}
2502
Romain Guyada830f2011-01-13 12:13:20 -08002503void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2504 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002505 return;
2506 }
2507
Romain Guy2bf68f02012-03-02 13:37:47 -08002508 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2509 OpenGLRenderer* renderer = layer->renderer;
2510 Rect& dirty = layer->dirtyRect;
2511
2512 interrupt();
2513 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2514 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002515 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002516 renderer->finish();
2517 resume();
2518
2519 dirty.setEmpty();
2520 layer->deferredUpdateScheduled = false;
2521 layer->renderer = NULL;
2522 layer->displayList = NULL;
2523 }
2524
Romain Guya1d3c912011-12-13 14:55:06 -08002525 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002526
2527 int alpha;
2528 SkXfermode::Mode mode;
2529 getAlphaAndMode(paint, &alpha, &mode);
2530
Romain Guy9ace8f52011-07-07 20:50:11 -07002531 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002532
Romain Guyf219da52011-01-16 12:54:25 -08002533#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002534 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002535 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002536 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002537 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002538 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002539 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002540
Romain Guyc88e3572011-01-22 00:32:12 -08002541 setupDraw();
2542 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002543 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002544 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002545 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002546 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002547 setupDrawPureColorUniforms();
2548 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002549 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002550 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002551 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2552 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2553
Romain Guyd21b6e12011-11-30 20:21:23 -08002554 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002555 setupDrawModelViewTranslate(x, y,
2556 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2557 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002558 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002559 setupDrawModelViewTranslate(x, y,
2560 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2561 }
Romain Guyc88e3572011-01-22 00:32:12 -08002562 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002563
Romain Guyc88e3572011-01-22 00:32:12 -08002564 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2565 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002566
Romain Guyc88e3572011-01-22 00:32:12 -08002567 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002568
2569#if DEBUG_LAYERS_AS_REGIONS
2570 drawRegionRects(layer->region);
2571#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002572 }
Romain Guyf219da52011-01-16 12:54:25 -08002573 }
2574#else
Romain Guyada830f2011-01-13 12:13:20 -08002575 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2576 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002577#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002578}
2579
Romain Guy6926c722010-07-12 20:20:03 -07002580///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002581// Shaders
2582///////////////////////////////////////////////////////////////////////////////
2583
2584void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002585 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002586}
2587
Romain Guy06f96e22010-07-30 19:18:16 -07002588void OpenGLRenderer::setupShader(SkiaShader* shader) {
2589 mShader = shader;
2590 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002591 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002592 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002593}
2594
Romain Guyd27977d2010-07-14 19:18:51 -07002595///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002596// Color filters
2597///////////////////////////////////////////////////////////////////////////////
2598
2599void OpenGLRenderer::resetColorFilter() {
2600 mColorFilter = NULL;
2601}
2602
2603void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2604 mColorFilter = filter;
2605}
2606
2607///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002608// Drop shadow
2609///////////////////////////////////////////////////////////////////////////////
2610
2611void OpenGLRenderer::resetShadow() {
2612 mHasShadow = false;
2613}
2614
2615void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2616 mHasShadow = true;
2617 mShadowRadius = radius;
2618 mShadowDx = dx;
2619 mShadowDy = dy;
2620 mShadowColor = color;
2621}
2622
2623///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002624// Draw filters
2625///////////////////////////////////////////////////////////////////////////////
2626
2627void OpenGLRenderer::resetPaintFilter() {
2628 mHasDrawFilter = false;
2629}
2630
2631void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2632 mHasDrawFilter = true;
2633 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2634 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2635}
2636
2637SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002638 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002639
2640 uint32_t flags = paint->getFlags();
2641
2642 mFilteredPaint = *paint;
2643 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2644
2645 return &mFilteredPaint;
2646}
2647
2648///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002649// Drawing implementation
2650///////////////////////////////////////////////////////////////////////////////
2651
Romain Guy01d58e42011-01-19 21:54:02 -08002652void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2653 float x, float y, SkPaint* paint) {
2654 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2655 return;
2656 }
2657
2658 int alpha;
2659 SkXfermode::Mode mode;
2660 getAlphaAndMode(paint, &alpha, &mode);
2661
2662 setupDraw();
2663 setupDrawWithTexture(true);
2664 setupDrawAlpha8Color(paint->getColor(), alpha);
2665 setupDrawColorFilter();
2666 setupDrawShader();
2667 setupDrawBlending(true, mode);
2668 setupDrawProgram();
2669 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2670 setupDrawTexture(texture->id);
2671 setupDrawPureColorUniforms();
2672 setupDrawColorFilterUniforms();
2673 setupDrawShaderUniforms();
2674 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2675
2676 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2677
2678 finishDrawTexture();
2679}
2680
Romain Guyf607bdc2010-09-10 19:20:06 -07002681// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002682#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2683#define kStdUnderline_Offset (1.0f / 9.0f)
2684#define kStdUnderline_Thickness (1.0f / 18.0f)
2685
2686void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2687 float x, float y, SkPaint* paint) {
2688 // Handle underline and strike-through
2689 uint32_t flags = paint->getFlags();
2690 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002691 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002692 float underlineWidth = length;
2693 // If length is > 0.0f, we already measured the text for the text alignment
2694 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002695 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002696 }
2697
2698 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002699 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002700 case SkPaint::kCenter_Align:
2701 offsetX = underlineWidth * 0.5f;
2702 break;
2703 case SkPaint::kRight_Align:
2704 offsetX = underlineWidth;
2705 break;
2706 default:
2707 break;
2708 }
2709
Romain Guy211370f2012-02-01 16:10:55 -08002710 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002711 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002712 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002713
Romain Guye20ecbd2010-09-22 19:49:04 -07002714 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002715 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002716
Romain Guyf6834472011-01-23 13:32:12 -08002717 int linesCount = 0;
2718 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2719 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2720
2721 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002722 float points[pointsCount];
2723 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002724
2725 if (flags & SkPaint::kUnderlineText_Flag) {
2726 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002727 points[currentPoint++] = left;
2728 points[currentPoint++] = top;
2729 points[currentPoint++] = left + underlineWidth;
2730 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002731 }
2732
2733 if (flags & SkPaint::kStrikeThruText_Flag) {
2734 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002735 points[currentPoint++] = left;
2736 points[currentPoint++] = top;
2737 points[currentPoint++] = left + underlineWidth;
2738 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002739 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002740
Romain Guy726aeba2011-06-01 14:52:00 -07002741 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002742
Romain Guy726aeba2011-06-01 14:52:00 -07002743 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002744 }
2745 }
2746}
2747
Romain Guy026c5e162010-06-28 17:12:22 -07002748void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002749 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002750 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002751 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002752 color |= 0x00ffffff;
2753 }
2754
Romain Guy70ca14e2010-12-13 18:24:33 -08002755 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002756 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002757 setupDrawColor(color);
2758 setupDrawShader();
2759 setupDrawColorFilter();
2760 setupDrawBlending(mode);
2761 setupDrawProgram();
2762 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2763 setupDrawColorUniforms();
2764 setupDrawShaderUniforms(ignoreTransform);
2765 setupDrawColorFilterUniforms();
2766 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002767
Romain Guyc95c8d62010-09-17 15:31:32 -07002768 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2769}
2770
Romain Guy82ba8142010-07-09 13:25:56 -07002771void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002772 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002773 int alpha;
2774 SkXfermode::Mode mode;
2775 getAlphaAndMode(paint, &alpha, &mode);
2776
Romain Guyd21b6e12011-11-30 20:21:23 -08002777 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002778
Romain Guy211370f2012-02-01 16:10:55 -08002779 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002780 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2781 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2782
Romain Guyd21b6e12011-11-30 20:21:23 -08002783 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002784 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2785 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2786 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2787 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002788 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002789 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2790 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2791 GL_TRIANGLE_STRIP, gMeshCount);
2792 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002793}
2794
Romain Guybd6b79b2010-06-26 00:13:53 -07002795void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002796 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2797 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002798 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002799}
2800
2801void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002802 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002803 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002804 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002805
Romain Guy746b7402010-10-26 16:27:31 -07002806 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002807 setupDrawWithTexture();
2808 setupDrawColor(alpha, alpha, alpha, alpha);
2809 setupDrawColorFilter();
2810 setupDrawBlending(blend, mode, swapSrcDst);
2811 setupDrawProgram();
2812 if (!dirty) {
2813 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002814 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002815 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002816 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002817 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002818 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002819 }
Romain Guy86568192010-12-14 15:55:39 -08002820 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002821 setupDrawColorFilterUniforms();
2822 setupDrawTexture(texture);
2823 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002824
Romain Guy6820ac82010-09-15 18:11:50 -07002825 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002826
2827 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002828}
2829
Romain Guya5aed0d2010-09-09 14:42:43 -07002830void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002831 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002832 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002833
Romain Guy82ba8142010-07-09 13:25:56 -07002834 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002835 // These blend modes are not supported by OpenGL directly and have
2836 // to be implemented using shaders. Since the shader will perform
2837 // the blending, turn blending off here
2838 // If the blend mode cannot be implemented using shaders, fall
2839 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002840 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2841 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002842 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002843 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002844
Romain Guy82bc7a72012-01-03 14:13:39 -08002845 if (mCaches.blend) {
2846 glDisable(GL_BLEND);
2847 mCaches.blend = false;
2848 }
2849
2850 return;
2851 } else {
2852 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002853 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002854 }
2855
2856 if (!mCaches.blend) {
2857 glEnable(GL_BLEND);
2858 }
2859
2860 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2861 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2862
2863 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2864 glBlendFunc(sourceMode, destMode);
2865 mCaches.lastSrcMode = sourceMode;
2866 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002867 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002868 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002869 glDisable(GL_BLEND);
2870 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002871 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002872}
2873
Romain Guy889f8d12010-07-29 14:37:42 -07002874bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002875 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002876 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002877 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002878 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002879 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002880 }
Romain Guy6926c722010-07-12 20:20:03 -07002881 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002882}
2883
Romain Guy026c5e162010-06-28 17:12:22 -07002884void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002885 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002886 TextureVertex::setUV(v++, u1, v1);
2887 TextureVertex::setUV(v++, u2, v1);
2888 TextureVertex::setUV(v++, u1, v2);
2889 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002890}
2891
Chet Haase5c13d892010-10-08 08:37:55 -07002892void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002893 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002894 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002895
2896 // Skia draws using the color's alpha channel if < 255
2897 // Otherwise, it uses the paint's alpha
2898 int color = paint->getColor();
2899 *alpha = (color >> 24) & 0xFF;
2900 if (*alpha == 255) {
2901 *alpha = paint->getAlpha();
2902 }
2903 } else {
2904 *mode = SkXfermode::kSrcOver_Mode;
2905 *alpha = 255;
2906 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07002907 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002908}
2909
Romain Guya5aed0d2010-09-09 14:42:43 -07002910SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002911 SkXfermode::Mode resultMode;
2912 if (!SkXfermode::AsMode(mode, &resultMode)) {
2913 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002914 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002915 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002916}
2917
Romain Guy9d5316e2010-06-24 19:30:36 -07002918}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002919}; // namespace android