blob: 32595e4a55aebc8d6a8eb23f8a1026cbbfd538a8 [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 Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070027#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070028
Romain Guy08aa2cb2011-03-17 11:06:57 -070029#include <private/hwui/DrawGlInfo.h>
30
Romain Guy5b3b3522010-10-27 18:57:51 -070031#include <ui/Rect.h>
32
Romain Guy85bf02f2010-06-22 13:11:24 -070033#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080034#include "DisplayListRenderer.h"
Romain Guya957eea2010-12-08 18:34:42 -080035#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070036
37namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070038namespace uirenderer {
39
40///////////////////////////////////////////////////////////////////////////////
41// Defines
42///////////////////////////////////////////////////////////////////////////////
43
Romain Guy759ea802010-09-16 20:49:46 -070044#define RAD_TO_DEG (180.0f / 3.14159265f)
45#define MIN_ANGLE 0.001f
46
Romain Guydbc26d22010-10-11 17:58:29 -070047// TODO: This should be set in properties
48#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
49
Romain Guy9d5316e2010-06-24 19:30:36 -070050///////////////////////////////////////////////////////////////////////////////
51// Globals
52///////////////////////////////////////////////////////////////////////////////
53
Romain Guy889f8d12010-07-29 14:37:42 -070054/**
55 * Structure mapping Skia xfermodes to OpenGL blending factors.
56 */
57struct Blender {
58 SkXfermode::Mode mode;
59 GLenum src;
60 GLenum dst;
61}; // struct Blender
62
Romain Guy026c5e162010-06-28 17:12:22 -070063// In this array, the index of each Blender equals the value of the first
64// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
65static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070066 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
67 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
68 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
69 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
70 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
71 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
72 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
73 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
74 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
75 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
76 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
77 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
79 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
80 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070081};
Romain Guye4d01122010-06-16 18:44:05 -070082
Romain Guy87a76572010-09-13 18:11:21 -070083// This array contains the swapped version of each SkXfermode. For instance
84// this array's SrcOver blending mode is actually DstOver. You can refer to
85// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070086static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070087 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
88 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
89 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
90 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
91 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
92 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
93 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
94 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
96 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
97 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
98 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
99 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
100 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
101 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700102};
103
Romain Guy889f8d12010-07-29 14:37:42 -0700104static const GLenum gTextureUnits[] = {
Romain Guyb146b122010-12-15 17:06:45 -0800105 GL_TEXTURE0,
106 GL_TEXTURE1,
107 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700108};
109
Romain Guyf6a11b82010-06-23 17:47:49 -0700110///////////////////////////////////////////////////////////////////////////////
111// Constructors/destructor
112///////////////////////////////////////////////////////////////////////////////
113
Romain Guyfb8b7632010-08-23 21:05:08 -0700114OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700115 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700116 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700117 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700118
Romain Guyac670c02010-07-27 17:39:27 -0700119 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
120
Romain Guyae5575b2010-07-29 18:48:04 -0700121 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700122}
123
Romain Guy85bf02f2010-06-22 13:11:24 -0700124OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700125 // The context has already been destroyed at this point, do not call
126 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700127}
128
Romain Guyf6a11b82010-06-23 17:47:49 -0700129///////////////////////////////////////////////////////////////////////////////
130// Setup
131///////////////////////////////////////////////////////////////////////////////
132
Romain Guy85bf02f2010-06-22 13:11:24 -0700133void OpenGLRenderer::setViewport(int width, int height) {
Romain Guyf2fc4602011-07-19 15:20:03 -0700134 glDisable(GL_DITHER);
Romain Guy08ae3172010-06-21 19:35:50 -0700135 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700136 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700137
138 mWidth = width;
139 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700140
141 mFirstSnapshot->height = height;
142 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700143
144 mDirtyClip = false;
Romain Guye4d01122010-06-16 18:44:05 -0700145}
146
Romain Guy6b7bd242010-10-06 19:49:23 -0700147void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800148 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
149}
150
151void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800152 mCaches.clearGarbage();
153
Romain Guy8aef54f2010-09-01 15:13:49 -0700154 mSnapshot = new Snapshot(mFirstSnapshot,
155 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800156 mSnapshot->fbo = getTargetFbo();
157
Romain Guy8fb95422010-08-17 18:38:51 -0700158 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700159
Romain Guyfb8b7632010-08-23 21:05:08 -0700160 glViewport(0, 0, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700161
Romain Guy7d7b5492011-01-24 16:33:45 -0800162 glEnable(GL_SCISSOR_TEST);
163 glScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
164 mSnapshot->setClip(left, top, right, bottom);
165
Romain Guy6b7bd242010-10-06 19:49:23 -0700166 if (!opaque) {
167 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
168 glClear(GL_COLOR_BUFFER_BIT);
169 }
Romain Guybb9524b2010-06-22 18:56:38 -0700170}
171
Romain Guyb025b9c2010-09-16 14:16:48 -0700172void OpenGLRenderer::finish() {
173#if DEBUG_OPENGL
174 GLenum status = GL_NO_ERROR;
175 while ((status = glGetError()) != GL_NO_ERROR) {
176 LOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800177 switch (status) {
178 case GL_OUT_OF_MEMORY:
179 LOGE(" OpenGLRenderer is out of memory!");
180 break;
181 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700182 }
183#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800184#if DEBUG_MEMORY_USAGE
185 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800186#else
187 if (mCaches.getDebugLevel() & kDebugMemory) {
188 mCaches.dumpMemoryUsage();
189 }
Romain Guyc15008e2010-11-10 11:59:15 -0800190#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700191}
192
Romain Guy6c319ca2011-01-11 14:29:25 -0800193void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700194 if (mCaches.currentProgram) {
195 if (mCaches.currentProgram->isInUse()) {
196 mCaches.currentProgram->remove();
197 mCaches.currentProgram = NULL;
198 }
199 }
Romain Guy50c0f092010-10-19 11:42:22 -0700200 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700201}
202
Romain Guy6c319ca2011-01-11 14:29:25 -0800203void OpenGLRenderer::resume() {
Romain Guyeb993562010-10-05 18:14:38 -0700204 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700205
206 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700207 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700208
Romain Guyf607bdc2010-09-10 19:20:06 -0700209 glDisable(GL_DITHER);
210
Romain Guy42f3a4b2011-01-19 13:42:26 -0800211 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy03750a02010-10-18 14:06:08 -0700212 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700213
Romain Guy50c0f092010-10-19 11:42:22 -0700214 mCaches.blend = true;
215 glEnable(GL_BLEND);
216 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
217 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700218}
219
Romain Guycabfcc12011-03-07 18:06:46 -0800220bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800221 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800222 if (mDirtyClip) {
223 setScissorFromClip();
224 }
Romain Guyd643bb52011-03-01 14:55:21 -0800225
Romain Guy80911b82011-03-16 15:30:12 -0700226 Rect clip(*mSnapshot->clipRect);
227 clip.snapToPixelBoundaries();
228
Romain Guyd643bb52011-03-01 14:55:21 -0800229#if RENDER_LAYERS_AS_REGIONS
230 // Since we don't know what the functor will draw, let's dirty
231 // tne entire clip region
232 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800233 dirtyLayerUnchecked(clip, getRegion());
234 }
235#endif
236
Romain Guy08aa2cb2011-03-17 11:06:57 -0700237 DrawGlInfo info;
238 info.clipLeft = clip.left;
239 info.clipTop = clip.top;
240 info.clipRight = clip.right;
241 info.clipBottom = clip.bottom;
242 info.isLayer = hasLayer();
243 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700244
Romain Guy08aa2cb2011-03-17 11:06:57 -0700245 status_t result = (*functor)(0, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800246
247 if (result != 0) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700248 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800249 dirty.unionWith(localDirty);
250 }
251
Chet Haasedaf98e92011-01-10 14:10:36 -0800252 resume();
Romain Guycabfcc12011-03-07 18:06:46 -0800253 return result != 0;
Chet Haasedaf98e92011-01-10 14:10:36 -0800254}
255
Romain Guyf6a11b82010-06-23 17:47:49 -0700256///////////////////////////////////////////////////////////////////////////////
257// State management
258///////////////////////////////////////////////////////////////////////////////
259
Romain Guybb9524b2010-06-22 18:56:38 -0700260int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700261 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700262}
263
264int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700265 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700266}
267
268void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700269 if (mSaveCount > 1) {
270 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700271 }
Romain Guybb9524b2010-06-22 18:56:38 -0700272}
273
274void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700275 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700276
Romain Guy8fb95422010-08-17 18:38:51 -0700277 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700278 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700279 }
Romain Guybb9524b2010-06-22 18:56:38 -0700280}
281
Romain Guy8aef54f2010-09-01 15:13:49 -0700282int OpenGLRenderer::saveSnapshot(int flags) {
283 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700284 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700285}
286
287bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700288 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700289 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700290 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700291
Romain Guybd6b79b2010-06-26 00:13:53 -0700292 sp<Snapshot> current = mSnapshot;
293 sp<Snapshot> previous = mSnapshot->previous;
294
Romain Guyeb993562010-10-05 18:14:38 -0700295 if (restoreOrtho) {
296 Rect& r = previous->viewport;
297 glViewport(r.left, r.top, r.right, r.bottom);
298 mOrthoMatrix.load(current->orthoMatrix);
299 }
300
Romain Guy8b55f372010-08-18 17:10:07 -0700301 mSaveCount--;
302 mSnapshot = previous;
303
Romain Guy2542d192010-08-18 11:47:12 -0700304 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700305 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700306 }
Romain Guy2542d192010-08-18 11:47:12 -0700307
Romain Guy5ec99242010-11-03 16:19:08 -0700308 if (restoreLayer) {
309 composeLayer(current, previous);
310 }
311
Romain Guy2542d192010-08-18 11:47:12 -0700312 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700313}
314
Romain Guyf6a11b82010-06-23 17:47:49 -0700315///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700316// Layers
317///////////////////////////////////////////////////////////////////////////////
318
319int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700320 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700321 const GLuint previousFbo = mSnapshot->fbo;
322 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700323
Romain Guyaf636eb2010-12-09 17:47:21 -0800324 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700325 int alpha = 255;
326 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700327
Romain Guye45362c2010-11-03 19:58:32 -0700328 if (p) {
329 alpha = p->getAlpha();
330 if (!mCaches.extensions.hasFramebufferFetch()) {
331 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
332 if (!isMode) {
333 // Assume SRC_OVER
334 mode = SkXfermode::kSrcOver_Mode;
335 }
336 } else {
337 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700338 }
339 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700340 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700341 }
Romain Guyd55a8612010-06-28 17:42:46 -0700342
Romain Guydbc26d22010-10-11 17:58:29 -0700343 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
344 }
Romain Guyd55a8612010-06-28 17:42:46 -0700345
346 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700347}
348
349int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
350 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700351 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700352 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700353 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700354 SkPaint paint;
355 paint.setAlpha(alpha);
356 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700357 }
Romain Guyd55a8612010-06-28 17:42:46 -0700358}
Romain Guybd6b79b2010-06-26 00:13:53 -0700359
Romain Guy1c740bc2010-09-13 18:00:09 -0700360/**
361 * Layers are viewed by Skia are slightly different than layers in image editing
362 * programs (for instance.) When a layer is created, previously created layers
363 * and the frame buffer still receive every drawing command. For instance, if a
364 * layer is created and a shape intersecting the bounds of the layers and the
365 * framebuffer is draw, the shape will be drawn on both (unless the layer was
366 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
367 *
368 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
369 * texture. Unfortunately, this is inefficient as it requires every primitive to
370 * be drawn n + 1 times, where n is the number of active layers. In practice this
371 * means, for every primitive:
372 * - Switch active frame buffer
373 * - Change viewport, clip and projection matrix
374 * - Issue the drawing
375 *
376 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700377 * To avoid this, layers are implemented in a different way here, at least in the
378 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
379 * is set. When this flag is set we can redirect all drawing operations into a
380 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700381 *
382 * This implementation relies on the frame buffer being at least RGBA 8888. When
383 * a layer is created, only a texture is created, not an FBO. The content of the
384 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700385 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700386 * buffer and drawing continues as normal. This technique therefore treats the
387 * frame buffer as a scratch buffer for the layers.
388 *
389 * To compose the layers back onto the frame buffer, each layer texture
390 * (containing the original frame buffer data) is drawn as a simple quad over
391 * the frame buffer. The trick is that the quad is set as the composition
392 * destination in the blending equation, and the frame buffer becomes the source
393 * of the composition.
394 *
395 * Drawing layers with an alpha value requires an extra step before composition.
396 * An empty quad is drawn over the layer's region in the frame buffer. This quad
397 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
398 * quad is used to multiply the colors in the frame buffer. This is achieved by
399 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
400 * GL_ZERO, GL_SRC_ALPHA.
401 *
402 * Because glCopyTexImage2D() can be slow, an alternative implementation might
403 * be use to draw a single clipped layer. The implementation described above
404 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700405 *
406 * (1) The frame buffer is actually not cleared right away. To allow the GPU
407 * to potentially optimize series of calls to glCopyTexImage2D, the frame
408 * buffer is left untouched until the first drawing operation. Only when
409 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700410 */
Romain Guyd55a8612010-06-28 17:42:46 -0700411bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700412 float right, float bottom, int alpha, SkXfermode::Mode mode,
413 int flags, GLuint previousFbo) {
414 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700415 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700416
Romain Guyeb993562010-10-05 18:14:38 -0700417 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
418
Romain Guyf607bdc2010-09-10 19:20:06 -0700419 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700420 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700421 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700422 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700423
Romain Guyeb993562010-10-05 18:14:38 -0700424 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700425 if (bounds.intersect(*snapshot->clipRect)) {
426 // We cannot work with sub-pixels in this case
427 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700428
Romain Guyad37cd32011-03-15 11:12:25 -0700429 // When the layer is not an FBO, we may use glCopyTexImage so we
430 // need to make sure the layer does not extend outside the bounds
431 // of the framebuffer
432 if (!bounds.intersect(snapshot->previous->viewport)) {
433 bounds.setEmpty();
434 }
435 } else {
436 bounds.setEmpty();
437 }
Romain Guyeb993562010-10-05 18:14:38 -0700438 }
Romain Guybf434112010-09-16 14:40:17 -0700439
Romain Guy746b7402010-10-26 16:27:31 -0700440 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
441 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800442 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700443 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700444 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700445 }
446
447 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800448 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700449 return false;
450 }
Romain Guyf18fd992010-07-08 11:45:51 -0700451
Romain Guy5b3b3522010-10-27 18:57:51 -0700452 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700453 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700454 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700455 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700456 }
457
Romain Guy9ace8f52011-07-07 20:50:11 -0700458 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700459 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700460 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
461 bounds.getWidth() / float(layer->getWidth()), 0.0f);
462 layer->setColorFilter(mColorFilter);
Romain Guydda57022010-07-06 11:39:32 -0700463
Romain Guy8fb95422010-08-17 18:38:51 -0700464 // Save the layer in the snapshot
465 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700466 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700467
Romain Guyeb993562010-10-05 18:14:38 -0700468 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700469 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700470 } else {
471 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700472 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800473 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700474 if (layer->isEmpty()) {
475 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
476 bounds.left, snapshot->height - bounds.bottom,
477 layer->getWidth(), layer->getHeight(), 0);
478 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800479 } else {
480 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
481 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
482 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700483
Romain Guy54be1cd2011-06-13 19:04:27 -0700484 // Enqueue the buffer coordinates to clear the corresponding region later
485 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700486 }
Romain Guyeb993562010-10-05 18:14:38 -0700487 }
Romain Guyf86ef572010-07-01 11:05:42 -0700488
Romain Guyd55a8612010-06-28 17:42:46 -0700489 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700490}
491
Romain Guy5b3b3522010-10-27 18:57:51 -0700492bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
493 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700494 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700495
496#if RENDER_LAYERS_AS_REGIONS
497 snapshot->region = &snapshot->layer->region;
498 snapshot->flags |= Snapshot::kFlagFboTarget;
499#endif
500
501 Rect clip(bounds);
502 snapshot->transform->mapRect(clip);
503 clip.intersect(*snapshot->clipRect);
504 clip.snapToPixelBoundaries();
505 clip.intersect(snapshot->previous->viewport);
506
507 mat4 inverse;
508 inverse.loadInverse(*mSnapshot->transform);
509
510 inverse.mapRect(clip);
511 clip.snapToPixelBoundaries();
512 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700513 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700514
515 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700516 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700517 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700518 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
519 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
520 snapshot->height = bounds.getHeight();
521 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
522 snapshot->orthoMatrix.load(mOrthoMatrix);
523
524 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700525 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
526 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700527
528 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700529 if (layer->isEmpty()) {
530 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
531 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700532 }
533
534 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700535 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700536
537#if DEBUG_LAYERS_AS_REGIONS
538 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
539 if (status != GL_FRAMEBUFFER_COMPLETE) {
540 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
541
542 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700543 layer->deleteTexture();
544 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700545
546 delete layer;
547
548 return false;
549 }
550#endif
551
552 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
553 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
554 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
555 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
556 glClear(GL_COLOR_BUFFER_BIT);
557
558 dirtyClip();
559
560 // Change the ortho projection
561 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
562 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
563
564 return true;
565}
566
Romain Guy1c740bc2010-09-13 18:00:09 -0700567/**
568 * Read the documentation of createLayer() before doing anything in this method.
569 */
Romain Guy1d83e192010-08-17 11:37:00 -0700570void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
571 if (!current->layer) {
572 LOGE("Attempting to compose a layer that does not exist");
573 return;
574 }
575
Romain Guy5b3b3522010-10-27 18:57:51 -0700576 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700577
578 if (fboLayer) {
579 // Unbind current FBO and restore previous one
580 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
581 }
582
Romain Guy1d83e192010-08-17 11:37:00 -0700583 Layer* layer = current->layer;
584 const Rect& rect = layer->layer;
585
Romain Guy9ace8f52011-07-07 20:50:11 -0700586 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700587 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700588 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700589 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700590 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700591 }
592
Romain Guy03750a02010-10-18 14:06:08 -0700593 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700594
Romain Guy746b7402010-10-26 16:27:31 -0700595 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700596
Romain Guy5b3b3522010-10-27 18:57:51 -0700597 // When the layer is stored in an FBO, we can save a bit of fillrate by
598 // drawing only the dirty region
599 if (fboLayer) {
600 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700601 if (layer->getColorFilter()) {
602 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800603 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700604 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700605 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800606 resetColorFilter();
607 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700608 } else if (!rect.isEmpty()) {
609 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
610 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700611 }
Romain Guy8b55f372010-08-18 17:10:07 -0700612
Romain Guyeb993562010-10-05 18:14:38 -0700613 if (fboLayer) {
614 // Detach the texture from the FBO
615 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
616 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
617 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
618
619 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
620 mCaches.fboCache.put(current->fbo);
621 }
622
Romain Guy746b7402010-10-26 16:27:31 -0700623 dirtyClip();
624
Romain Guyeb993562010-10-05 18:14:38 -0700625 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700626 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700627 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700628 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700629 delete layer;
630 }
631}
632
Romain Guyaa6c24c2011-04-28 18:40:04 -0700633void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700634 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700635
Romain Guy302a9df2011-08-16 13:55:02 -0700636 mat4& transform = layer->getTransform();
637 if (!transform.isIdentity()) {
638 save(0);
639 mSnapshot->transform->multiply(transform);
640 }
641
Romain Guyaa6c24c2011-04-28 18:40:04 -0700642 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700643 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700644 setupDrawWithTexture();
645 } else {
646 setupDrawWithExternalTexture();
647 }
648 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700649 setupDrawColor(alpha, alpha, alpha, alpha);
650 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700651 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700652 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700653 setupDrawPureColorUniforms();
654 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700655 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
656 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700657 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700658 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700659 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700660 if (mSnapshot->transform->isPureTranslate() &&
661 layer->getWidth() == (uint32_t) rect.getWidth() &&
662 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700663 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
664 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
665
666 layer->setFilter(GL_NEAREST, GL_NEAREST);
667 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
668 } else {
669 layer->setFilter(GL_LINEAR, GL_LINEAR);
670 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
671 }
672 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700673 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
674
675 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
676
677 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700678
679 if (!transform.isIdentity()) {
680 restore();
681 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700682}
683
Romain Guy5b3b3522010-10-27 18:57:51 -0700684void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700685 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700686 const Rect& texCoords = layer->texCoords;
687 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
688 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700689
Romain Guy9ace8f52011-07-07 20:50:11 -0700690 float x = rect.left;
691 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700692 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700693 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700694 layer->getHeight() == (uint32_t) rect.getHeight();
695
696 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700697 // When we're swapping, the layer is already in screen coordinates
698 if (!swap) {
699 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
700 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
701 }
702
703 layer->setFilter(GL_NEAREST, GL_NEAREST, true);
704 } else {
705 layer->setFilter(GL_LINEAR, GL_LINEAR, true);
706 }
707
708 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
709 layer->getTexture(), layer->getAlpha() / 255.0f,
710 layer->getMode(), layer->isBlend(),
711 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
712 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700713
Romain Guyaa6c24c2011-04-28 18:40:04 -0700714 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
715 } else {
716 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
717 drawTextureLayer(layer, rect);
718 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
719 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700720}
721
722void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
723#if RENDER_LAYERS_AS_REGIONS
724 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700725 layer->setRegionAsRect();
726
Romain Guy40667672011-03-18 14:34:03 -0700727 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700728
Romain Guy5b3b3522010-10-27 18:57:51 -0700729 layer->region.clear();
730 return;
731 }
732
Romain Guy8a3957d2011-09-07 17:55:15 -0700733 // TODO: See LayerRenderer.cpp::generateMesh() for important
734 // information about this implementation
Romain Guy5b3b3522010-10-27 18:57:51 -0700735 if (!layer->region.isEmpty()) {
736 size_t count;
737 const android::Rect* rects = layer->region.getArray(&count);
738
Romain Guy9ace8f52011-07-07 20:50:11 -0700739 const float alpha = layer->getAlpha() / 255.0f;
740 const float texX = 1.0f / float(layer->getWidth());
741 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800742 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700743
744 TextureVertex* mesh = mCaches.getRegionMesh();
745 GLsizei numQuads = 0;
746
Romain Guy7230a742011-01-10 22:26:16 -0800747 setupDraw();
748 setupDrawWithTexture();
749 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800750 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700751 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800752 setupDrawProgram();
753 setupDrawDirtyRegionsDisabled();
754 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800755 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700756 setupDrawTexture(layer->getTexture());
757 if (mSnapshot->transform->isPureTranslate()) {
758 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
759 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
760
761 layer->setFilter(GL_NEAREST, GL_NEAREST);
762 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
763 } else {
764 layer->setFilter(GL_LINEAR, GL_LINEAR);
765 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
766 }
Romain Guy7230a742011-01-10 22:26:16 -0800767 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700768
769 for (size_t i = 0; i < count; i++) {
770 const android::Rect* r = &rects[i];
771
772 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800773 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700774 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800775 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700776
777 // TODO: Reject quads outside of the clip
778 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
779 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
780 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
781 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
782
783 numQuads++;
784
785 if (numQuads >= REGION_MESH_QUAD_COUNT) {
786 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
787 numQuads = 0;
788 mesh = mCaches.getRegionMesh();
789 }
790 }
791
792 if (numQuads > 0) {
793 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
794 }
795
796 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800797 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700798
799#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800800 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700801#endif
802
803 layer->region.clear();
804 }
805#else
806 composeLayerRect(layer, rect);
807#endif
808}
809
Romain Guy3a3133d2011-02-01 22:59:58 -0800810void OpenGLRenderer::drawRegionRects(const Region& region) {
811#if DEBUG_LAYERS_AS_REGIONS
812 size_t count;
813 const android::Rect* rects = region.getArray(&count);
814
815 uint32_t colors[] = {
816 0x7fff0000, 0x7f00ff00,
817 0x7f0000ff, 0x7fff00ff,
818 };
819
820 int offset = 0;
821 int32_t top = rects[0].top;
822
823 for (size_t i = 0; i < count; i++) {
824 if (top != rects[i].top) {
825 offset ^= 0x2;
826 top = rects[i].top;
827 }
828
829 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
830 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
831 SkXfermode::kSrcOver_Mode);
832 }
833#endif
834}
835
Romain Guy5b3b3522010-10-27 18:57:51 -0700836void OpenGLRenderer::dirtyLayer(const float left, const float top,
837 const float right, const float bottom, const mat4 transform) {
838#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800839 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700840 Rect bounds(left, top, right, bottom);
841 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800842 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700843 }
844#endif
845}
846
847void OpenGLRenderer::dirtyLayer(const float left, const float top,
848 const float right, const float bottom) {
849#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800850 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700851 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800852 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800853 }
854#endif
855}
856
857void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
858#if RENDER_LAYERS_AS_REGIONS
859 if (bounds.intersect(*mSnapshot->clipRect)) {
860 bounds.snapToPixelBoundaries();
861 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
862 if (!dirty.isEmpty()) {
863 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700864 }
865 }
866#endif
867}
868
Romain Guy54be1cd2011-06-13 19:04:27 -0700869void OpenGLRenderer::clearLayerRegions() {
870 const size_t count = mLayers.size();
871 if (count == 0) return;
872
873 if (!mSnapshot->isIgnored()) {
874 // Doing several glScissor/glClear here can negatively impact
875 // GPUs with a tiler architecture, instead we draw quads with
876 // the Clear blending mode
877
878 // The list contains bounds that have already been clipped
879 // against their initial clip rect, and the current clip
880 // is likely different so we need to disable clipping here
881 glDisable(GL_SCISSOR_TEST);
882
883 Vertex mesh[count * 6];
884 Vertex* vertex = mesh;
885
886 for (uint32_t i = 0; i < count; i++) {
887 Rect* bounds = mLayers.itemAt(i);
888
889 Vertex::set(vertex++, bounds->left, bounds->bottom);
890 Vertex::set(vertex++, bounds->left, bounds->top);
891 Vertex::set(vertex++, bounds->right, bounds->top);
892 Vertex::set(vertex++, bounds->left, bounds->bottom);
893 Vertex::set(vertex++, bounds->right, bounds->top);
894 Vertex::set(vertex++, bounds->right, bounds->bottom);
895
896 delete bounds;
897 }
898
899 setupDraw(false);
900 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
901 setupDrawBlending(true, SkXfermode::kClear_Mode);
902 setupDrawProgram();
903 setupDrawPureColorUniforms();
904 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
905
906 mCaches.unbindMeshBuffer();
907 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
908 gVertexStride, &mesh[0].position[0]);
909 glDrawArrays(GL_TRIANGLES, 0, count * 6);
910
911 glEnable(GL_SCISSOR_TEST);
912 } else {
913 for (uint32_t i = 0; i < count; i++) {
914 delete mLayers.itemAt(i);
915 }
916 }
917
918 mLayers.clear();
919}
920
Romain Guybd6b79b2010-06-26 00:13:53 -0700921///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700922// Transforms
923///////////////////////////////////////////////////////////////////////////////
924
925void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700926 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700927}
928
929void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700930 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700931}
932
933void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700934 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700935}
936
Romain Guy807daf72011-01-18 11:19:19 -0800937void OpenGLRenderer::skew(float sx, float sy) {
938 mSnapshot->transform->skew(sx, sy);
939}
940
Romain Guyf6a11b82010-06-23 17:47:49 -0700941void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700942 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700943}
944
945void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700946 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700947}
948
949void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700950 SkMatrix transform;
951 mSnapshot->transform->copyTo(transform);
952 transform.preConcat(*matrix);
953 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700954}
955
956///////////////////////////////////////////////////////////////////////////////
957// Clipping
958///////////////////////////////////////////////////////////////////////////////
959
Romain Guybb9524b2010-06-22 18:56:38 -0700960void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700961 Rect clip(*mSnapshot->clipRect);
962 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700963 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700964 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700965}
966
967const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700968 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700969}
970
Romain Guyc7d53492010-06-25 13:41:57 -0700971bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800972 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700973 return true;
974 }
975
Romain Guy1d83e192010-08-17 11:37:00 -0700976 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700977 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700978 r.snapToPixelBoundaries();
979
980 Rect clipRect(*mSnapshot->clipRect);
981 clipRect.snapToPixelBoundaries();
982
983 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700984}
985
Romain Guy079ba2c2010-07-16 14:12:24 -0700986bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
987 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700988 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700989 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700990 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700991 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700992}
993
Romain Guyf6a11b82010-06-23 17:47:49 -0700994///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800995// Drawing commands
996///////////////////////////////////////////////////////////////////////////////
997
Romain Guy54be1cd2011-06-13 19:04:27 -0700998void OpenGLRenderer::setupDraw(bool clear) {
999 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001000 if (mDirtyClip) {
1001 setScissorFromClip();
1002 }
1003 mDescription.reset();
1004 mSetShaderColor = false;
1005 mColorSet = false;
1006 mColorA = mColorR = mColorG = mColorB = 0.0f;
1007 mTextureUnit = 0;
1008 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -08001009 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -08001010}
1011
1012void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1013 mDescription.hasTexture = true;
1014 mDescription.hasAlpha8Texture = isAlpha8;
1015}
1016
Romain Guyaa6c24c2011-04-28 18:40:04 -07001017void OpenGLRenderer::setupDrawWithExternalTexture() {
1018 mDescription.hasExternalTexture = true;
1019}
1020
Chet Haase5b0200b2011-04-13 17:58:08 -07001021void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001022 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001023}
1024
Romain Guyed6fcb02011-03-21 13:11:28 -07001025void OpenGLRenderer::setupDrawPoint(float pointSize) {
1026 mDescription.isPoint = true;
1027 mDescription.pointSize = pointSize;
1028}
1029
Romain Guy70ca14e2010-12-13 18:24:33 -08001030void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001031 setupDrawColor(color, (color >> 24) & 0xFF);
1032}
1033
1034void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1035 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001036 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1037 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001038 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001039 mColorR = a * ((color >> 16) & 0xFF);
1040 mColorG = a * ((color >> 8) & 0xFF);
1041 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001042 mColorSet = true;
1043 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1044}
1045
Romain Guy86568192010-12-14 15:55:39 -08001046void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1047 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001048 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1049 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001050 const float a = mColorA / 255.0f;
1051 mColorR = a * ((color >> 16) & 0xFF);
1052 mColorG = a * ((color >> 8) & 0xFF);
1053 mColorB = a * ((color ) & 0xFF);
1054 mColorSet = true;
1055 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1056}
1057
Romain Guy70ca14e2010-12-13 18:24:33 -08001058void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1059 mColorA = a;
1060 mColorR = r;
1061 mColorG = g;
1062 mColorB = b;
1063 mColorSet = true;
1064 mSetShaderColor = mDescription.setColor(r, g, b, a);
1065}
1066
Romain Guy86568192010-12-14 15:55:39 -08001067void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1068 mColorA = a;
1069 mColorR = r;
1070 mColorG = g;
1071 mColorB = b;
1072 mColorSet = true;
1073 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1074}
1075
Romain Guy70ca14e2010-12-13 18:24:33 -08001076void OpenGLRenderer::setupDrawShader() {
1077 if (mShader) {
1078 mShader->describe(mDescription, mCaches.extensions);
1079 }
1080}
1081
1082void OpenGLRenderer::setupDrawColorFilter() {
1083 if (mColorFilter) {
1084 mColorFilter->describe(mDescription, mCaches.extensions);
1085 }
1086}
1087
Romain Guyf09ef512011-05-27 11:43:46 -07001088void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1089 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1090 mColorA = 1.0f;
1091 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001092 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001093 }
1094}
1095
Romain Guy70ca14e2010-12-13 18:24:33 -08001096void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001097 // When the blending mode is kClear_Mode, we need to use a modulate color
1098 // argb=1,0,0,0
1099 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001100 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1101 mDescription, swapSrcDst);
1102}
1103
1104void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001105 // When the blending mode is kClear_Mode, we need to use a modulate color
1106 // argb=1,0,0,0
1107 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001108 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1109 mDescription, swapSrcDst);
1110}
1111
1112void OpenGLRenderer::setupDrawProgram() {
1113 useProgram(mCaches.programCache.get(mDescription));
1114}
1115
1116void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1117 mTrackDirtyRegions = false;
1118}
1119
1120void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1121 bool ignoreTransform) {
1122 mModelView.loadTranslate(left, top, 0.0f);
1123 if (!ignoreTransform) {
1124 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1125 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1126 } else {
1127 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1128 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1129 }
1130}
1131
Chet Haase8a5cc922011-04-26 07:28:09 -07001132void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1133 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001134}
1135
Romain Guy70ca14e2010-12-13 18:24:33 -08001136void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1137 bool ignoreTransform, bool ignoreModelView) {
1138 if (!ignoreModelView) {
1139 mModelView.loadTranslate(left, top, 0.0f);
1140 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001141 } else {
1142 mModelView.loadIdentity();
1143 }
Romain Guy86568192010-12-14 15:55:39 -08001144 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1145 if (!ignoreTransform) {
1146 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1147 if (mTrackDirtyRegions && dirty) {
1148 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1149 }
1150 } else {
1151 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1152 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1153 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001154}
1155
Romain Guyed6fcb02011-03-21 13:11:28 -07001156void OpenGLRenderer::setupDrawPointUniforms() {
1157 int slot = mCaches.currentProgram->getUniform("pointSize");
1158 glUniform1f(slot, mDescription.pointSize);
1159}
1160
Romain Guy70ca14e2010-12-13 18:24:33 -08001161void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001162 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001163 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1164 }
1165}
1166
Romain Guy86568192010-12-14 15:55:39 -08001167void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001168 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001169 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001170 }
1171}
1172
Romain Guy70ca14e2010-12-13 18:24:33 -08001173void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1174 if (mShader) {
1175 if (ignoreTransform) {
1176 mModelView.loadInverse(*mSnapshot->transform);
1177 }
1178 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1179 }
1180}
1181
Romain Guy8d0d4782010-12-14 20:13:35 -08001182void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1183 if (mShader) {
1184 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1185 }
1186}
1187
Romain Guy70ca14e2010-12-13 18:24:33 -08001188void OpenGLRenderer::setupDrawColorFilterUniforms() {
1189 if (mColorFilter) {
1190 mColorFilter->setupProgram(mCaches.currentProgram);
1191 }
1192}
1193
1194void OpenGLRenderer::setupDrawSimpleMesh() {
1195 mCaches.bindMeshBuffer();
1196 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1197 gMeshStride, 0);
1198}
1199
1200void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1201 bindTexture(texture);
1202 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1203
1204 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1205 glEnableVertexAttribArray(mTexCoordsSlot);
1206}
1207
Romain Guyaa6c24c2011-04-28 18:40:04 -07001208void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1209 bindExternalTexture(texture);
1210 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1211
1212 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1213 glEnableVertexAttribArray(mTexCoordsSlot);
1214}
1215
Romain Guy8f0095c2011-05-02 17:24:22 -07001216void OpenGLRenderer::setupDrawTextureTransform() {
1217 mDescription.hasTextureTransform = true;
1218}
1219
1220void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001221 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1222 GL_FALSE, &transform.data[0]);
1223}
1224
Romain Guy70ca14e2010-12-13 18:24:33 -08001225void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1226 if (!vertices) {
1227 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1228 } else {
1229 mCaches.unbindMeshBuffer();
1230 }
1231 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1232 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001233 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001234 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1235 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001236}
1237
Chet Haase5b0200b2011-04-13 17:58:08 -07001238void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
1239 mCaches.unbindMeshBuffer();
1240 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1241 gVertexStride, vertices);
1242}
1243
1244/**
1245 * 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 -07001246 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1247 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1248 * attributes (one per vertex) are values from zero to one that tells the fragment
1249 * shader where the fragment is in relation to the line width/length overall; these values are
1250 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1251 * region of the line.
1252 * Note that we only pass down the width values in this setup function. The length coordinates
1253 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001254 */
Chet Haase99585ad2011-05-02 15:00:16 -07001255void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001256 GLvoid* lengthCoords, float boundaryWidthProportion) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001257 mCaches.unbindMeshBuffer();
1258 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Chet Haase99585ad2011-05-02 15:00:16 -07001259 gAAVertexStride, vertices);
1260 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1261 glEnableVertexAttribArray(widthSlot);
1262 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
1263 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1264 glEnableVertexAttribArray(lengthSlot);
1265 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Chet Haase5b0200b2011-04-13 17:58:08 -07001266 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001267 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07001268 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001269 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001270 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001271}
1272
Romain Guy70ca14e2010-12-13 18:24:33 -08001273void OpenGLRenderer::finishDrawTexture() {
1274 glDisableVertexAttribArray(mTexCoordsSlot);
1275}
1276
1277///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001278// Drawing
1279///////////////////////////////////////////////////////////////////////////////
1280
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001281bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1282 Rect& dirty, uint32_t level) {
1283 if (quickReject(0.0f, 0.0f, width, height)) {
1284 return false;
1285 }
1286
Romain Guy0fe478e2010-11-08 12:08:41 -08001287 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1288 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001289 if (displayList && displayList->isRenderable()) {
Romain Guycabfcc12011-03-07 18:06:46 -08001290 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001291 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001292
Chet Haasedaf98e92011-01-10 14:10:36 -08001293 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001294}
1295
Chet Haaseed30fd82011-04-22 16:18:45 -07001296void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1297 if (displayList) {
1298 displayList->output(*this, level);
1299 }
1300}
1301
Romain Guya168d732011-03-18 16:50:13 -07001302void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1303 int alpha;
1304 SkXfermode::Mode mode;
1305 getAlphaAndMode(paint, &alpha, &mode);
1306
Romain Guya168d732011-03-18 16:50:13 -07001307 float x = left;
1308 float y = top;
1309
Romain Guye3c26852011-07-25 16:36:01 -07001310 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001311 bool ignoreTransform = false;
1312 if (mSnapshot->transform->isPureTranslate()) {
1313 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1314 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1315 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001316 filter = GL_NEAREST;
Romain Guya168d732011-03-18 16:50:13 -07001317 }
1318
1319 setupDraw();
1320 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001321 if (paint) {
1322 setupDrawAlpha8Color(paint->getColor(), alpha);
1323 }
Romain Guya168d732011-03-18 16:50:13 -07001324 setupDrawColorFilter();
1325 setupDrawShader();
1326 setupDrawBlending(true, mode);
1327 setupDrawProgram();
1328 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001329
Romain Guya168d732011-03-18 16:50:13 -07001330 setupDrawTexture(texture->id);
Romain Guye3c26852011-07-25 16:36:01 -07001331 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1332 texture->setFilter(filter, filter);
1333
Romain Guya168d732011-03-18 16:50:13 -07001334 setupDrawPureColorUniforms();
1335 setupDrawColorFilterUniforms();
1336 setupDrawShaderUniforms();
1337 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1338
1339 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1340
1341 finishDrawTexture();
1342}
1343
Chet Haase5c13d892010-10-08 08:37:55 -07001344void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001345 const float right = left + bitmap->width();
1346 const float bottom = top + bitmap->height();
1347
1348 if (quickReject(left, top, right, bottom)) {
1349 return;
1350 }
1351
Romain Guy86568192010-12-14 15:55:39 -08001352 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001353 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001354 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001355 const AutoTexture autoCleanup(texture);
1356
Romain Guya168d732011-03-18 16:50:13 -07001357 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1358 drawAlphaBitmap(texture, left, top, paint);
1359 } else {
1360 drawTextureRect(left, top, right, bottom, texture, paint);
1361 }
Romain Guyce0537b2010-06-29 21:05:21 -07001362}
1363
Chet Haase5c13d892010-10-08 08:37:55 -07001364void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001365 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1366 const mat4 transform(*matrix);
1367 transform.mapRect(r);
1368
Romain Guy6926c722010-07-12 20:20:03 -07001369 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1370 return;
1371 }
1372
Romain Guy86568192010-12-14 15:55:39 -08001373 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001374 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001375 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001376 const AutoTexture autoCleanup(texture);
1377
Romain Guy5b3b3522010-10-27 18:57:51 -07001378 // This could be done in a cheaper way, all we need is pass the matrix
1379 // to the vertex shader. The save/restore is a bit overkill.
1380 save(SkCanvas::kMatrix_SaveFlag);
1381 concatMatrix(matrix);
1382 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1383 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001384}
1385
Romain Guy5a7b4662011-01-20 19:09:30 -08001386void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1387 float* vertices, int* colors, SkPaint* paint) {
1388 // TODO: Do a quickReject
1389 if (!vertices || mSnapshot->isIgnored()) {
1390 return;
1391 }
1392
1393 glActiveTexture(gTextureUnits[0]);
1394 Texture* texture = mCaches.textureCache.get(bitmap);
1395 if (!texture) return;
1396 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001397
1398 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
1399 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001400
1401 int alpha;
1402 SkXfermode::Mode mode;
1403 getAlphaAndMode(paint, &alpha, &mode);
1404
Romain Guy5a7b4662011-01-20 19:09:30 -08001405 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001406
Romain Guyb18d2d02011-02-10 15:52:54 -08001407 float left = FLT_MAX;
1408 float top = FLT_MAX;
1409 float right = FLT_MIN;
1410 float bottom = FLT_MIN;
1411
1412#if RENDER_LAYERS_AS_REGIONS
1413 bool hasActiveLayer = hasLayer();
1414#else
1415 bool hasActiveLayer = false;
1416#endif
1417
Romain Guya566b7c2011-01-23 16:36:11 -08001418 // TODO: Support the colors array
1419 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001420 TextureVertex* vertex = mesh;
1421 for (int32_t y = 0; y < meshHeight; y++) {
1422 for (int32_t x = 0; x < meshWidth; x++) {
1423 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1424
1425 float u1 = float(x) / meshWidth;
1426 float u2 = float(x + 1) / meshWidth;
1427 float v1 = float(y) / meshHeight;
1428 float v2 = float(y + 1) / meshHeight;
1429
1430 int ax = i + (meshWidth + 1) * 2;
1431 int ay = ax + 1;
1432 int bx = i;
1433 int by = bx + 1;
1434 int cx = i + 2;
1435 int cy = cx + 1;
1436 int dx = i + (meshWidth + 1) * 2 + 2;
1437 int dy = dx + 1;
1438
1439 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1440 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1441 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1442
1443 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1444 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1445 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001446
1447#if RENDER_LAYERS_AS_REGIONS
1448 if (hasActiveLayer) {
1449 // TODO: This could be optimized to avoid unnecessary ops
1450 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1451 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1452 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1453 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1454 }
1455#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001456 }
1457 }
1458
Romain Guyb18d2d02011-02-10 15:52:54 -08001459#if RENDER_LAYERS_AS_REGIONS
1460 if (hasActiveLayer) {
1461 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1462 }
1463#endif
1464
Romain Guy5a7b4662011-01-20 19:09:30 -08001465 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1466 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001467 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001468}
1469
Romain Guy8ba548f2010-06-30 19:21:21 -07001470void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1471 float srcLeft, float srcTop, float srcRight, float srcBottom,
1472 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001473 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001474 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1475 return;
1476 }
1477
Romain Guy746b7402010-10-26 16:27:31 -07001478 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001479 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001480 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001481 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001482 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
Romain Guy8ba548f2010-06-30 19:21:21 -07001483
Romain Guy8ba548f2010-06-30 19:21:21 -07001484 const float width = texture->width;
1485 const float height = texture->height;
1486
Romain Guy68169722011-08-22 17:33:33 -07001487 const float u1 = fmax(0.0f, srcLeft / width);
1488 const float v1 = fmax(0.0f, srcTop / height);
1489 const float u2 = fmin(1.0f, srcRight / width);
1490 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001491
Romain Guy03750a02010-10-18 14:06:08 -07001492 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001493 resetDrawTextureTexCoords(u1, v1, u2, v2);
1494
Romain Guy03750a02010-10-18 14:06:08 -07001495 int alpha;
1496 SkXfermode::Mode mode;
1497 getAlphaAndMode(paint, &alpha, &mode);
1498
Romain Guy6620c6d2010-12-06 18:07:02 -08001499 if (mSnapshot->transform->isPureTranslate()) {
1500 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1501 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1502
Romain Guyb5014982011-07-28 15:39:12 -07001503 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001504 // Enable linear filtering if the source rectangle is scaled
1505 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyb5014982011-07-28 15:39:12 -07001506 filter = GL_LINEAR;
1507 }
1508 texture->setFilter(filter, filter, true);
1509
Romain Guy6620c6d2010-12-06 18:07:02 -08001510 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1511 texture->id, alpha / 255.0f, mode, texture->blend,
1512 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1513 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1514 } else {
Romain Guye3c26852011-07-25 16:36:01 -07001515 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guyb5014982011-07-28 15:39:12 -07001516
Romain Guy6620c6d2010-12-06 18:07:02 -08001517 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1518 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1519 GL_TRIANGLE_STRIP, gMeshCount);
1520 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001521
1522 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1523}
1524
Romain Guy4aa90572010-09-26 18:40:37 -07001525void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001526 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001527 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001528 if (quickReject(left, top, right, bottom)) {
1529 return;
1530 }
1531
Romain Guy746b7402010-10-26 16:27:31 -07001532 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001533 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001534 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001535 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001536 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
1537 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001538
1539 int alpha;
1540 SkXfermode::Mode mode;
1541 getAlphaAndMode(paint, &alpha, &mode);
1542
Romain Guy2728f962010-10-08 18:36:15 -07001543 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001544 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001545
Romain Guya5ef39a2010-12-03 16:48:20 -08001546 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001547 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001548#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001549 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001550 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001551 const float offsetX = left + mSnapshot->transform->getTranslateX();
1552 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001553 const size_t count = mesh->quads.size();
1554 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001555 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001556 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001557 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1558 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1559 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001560 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001561 dirtyLayer(left + bounds.left, top + bounds.top,
1562 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001563 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001564 }
1565 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001566#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001567
Romain Guy6620c6d2010-12-06 18:07:02 -08001568 if (pureTranslate) {
1569 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1570 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1571
1572 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1573 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1574 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1575 true, !mesh->hasEmptyQuads);
1576 } else {
1577 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1578 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1579 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1580 true, !mesh->hasEmptyQuads);
1581 }
Romain Guy054dc182010-10-15 17:55:25 -07001582 }
Romain Guyf7f93552010-07-08 19:17:03 -07001583}
1584
Chet Haase99ecdc42011-05-06 12:06:34 -07001585/**
Chet Haase858aa932011-05-12 09:06:00 -07001586 * This function uses a similar approach to that of AA lines in the drawLines() function.
1587 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1588 * shader to compute the translucency of the color, determined by whether a given pixel is
1589 * within that boundary region and how far into the region it is.
1590 */
1591void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001592 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001593 float inverseScaleX = 1.0f;
1594 float inverseScaleY = 1.0f;
1595 // The quad that we use needs to account for scaling.
1596 if (!mSnapshot->transform->isPureTranslate()) {
1597 Matrix4 *mat = mSnapshot->transform;
1598 float m00 = mat->data[Matrix4::kScaleX];
1599 float m01 = mat->data[Matrix4::kSkewY];
1600 float m02 = mat->data[2];
1601 float m10 = mat->data[Matrix4::kSkewX];
1602 float m11 = mat->data[Matrix4::kScaleX];
1603 float m12 = mat->data[6];
1604 float scaleX = sqrt(m00 * m00 + m01 * m01);
1605 float scaleY = sqrt(m10 * m10 + m11 * m11);
1606 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1607 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1608 }
1609
1610 setupDraw();
1611 setupDrawAALine();
1612 setupDrawColor(color);
1613 setupDrawColorFilter();
1614 setupDrawShader();
1615 setupDrawBlending(true, mode);
1616 setupDrawProgram();
1617 setupDrawModelViewIdentity(true);
1618 setupDrawColorUniforms();
1619 setupDrawColorFilterUniforms();
1620 setupDrawShaderIdentityUniforms();
1621
1622 AAVertex rects[4];
1623 AAVertex* aaVertices = &rects[0];
1624 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1625 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1626
1627 float boundarySizeX = .5 * inverseScaleX;
1628 float boundarySizeY = .5 * inverseScaleY;
1629
1630 // Adjust the rect by the AA boundary padding
1631 left -= boundarySizeX;
1632 right += boundarySizeX;
1633 top -= boundarySizeY;
1634 bottom += boundarySizeY;
1635
1636 float width = right - left;
1637 float height = bottom - top;
1638
1639 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1640 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1641 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1642 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1643 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1644 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1645 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1646
1647 if (!quickReject(left, top, right, bottom)) {
1648 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1649 AAVertex::set(aaVertices++, left, top, 1, 0);
1650 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1651 AAVertex::set(aaVertices++, right, top, 0, 0);
1652 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1653 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1654 }
1655}
1656
1657/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001658 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1659 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1660 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1661 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1662 * of the line. Hairlines are more involved because we need to account for transform scaling
1663 * to end up with a one-pixel-wide line in screen space..
1664 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1665 * in combination with values that we calculate and pass down in this method. The basic approach
1666 * is that the quad we create contains both the core line area plus a bounding area in which
1667 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1668 * proportion of the width and the length of a given segment is represented by the boundary
1669 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1670 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1671 * on the inside). This ends up giving the result we want, with pixels that are completely
1672 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1673 * how far into the boundary region they are, which is determined by shader interpolation.
1674 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001675void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1676 if (mSnapshot->isIgnored()) return;
1677
1678 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001679 // We use half the stroke width here because we're going to position the quad
1680 // corner vertices half of the width away from the line endpoints
1681 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001682 // A stroke width of 0 has a special meaning in Skia:
1683 // it draws a line 1 px wide regardless of current transform
1684 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001685 float inverseScaleX = 1.0f;
1686 float inverseScaleY = 1.0f;
1687 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001688 int alpha;
1689 SkXfermode::Mode mode;
1690 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001691 int verticesCount = count;
1692 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001693 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001694 verticesCount += (count - 4);
1695 }
1696
1697 if (isHairLine || isAA) {
1698 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1699 // the line on the screen should always be one pixel wide regardless of scale. For
1700 // AA lines, we only want one pixel of translucent boundary around the quad.
1701 if (!mSnapshot->transform->isPureTranslate()) {
1702 Matrix4 *mat = mSnapshot->transform;
1703 float m00 = mat->data[Matrix4::kScaleX];
1704 float m01 = mat->data[Matrix4::kSkewY];
1705 float m02 = mat->data[2];
1706 float m10 = mat->data[Matrix4::kSkewX];
1707 float m11 = mat->data[Matrix4::kScaleX];
1708 float m12 = mat->data[6];
1709 float scaleX = sqrt(m00*m00 + m01*m01);
1710 float scaleY = sqrt(m10*m10 + m11*m11);
1711 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1712 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1713 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1714 scaled = true;
1715 }
1716 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001717 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001718
1719 getAlphaAndMode(paint, &alpha, &mode);
1720 setupDraw();
1721 if (isAA) {
1722 setupDrawAALine();
1723 }
1724 setupDrawColor(paint->getColor(), alpha);
1725 setupDrawColorFilter();
1726 setupDrawShader();
1727 if (isAA) {
1728 setupDrawBlending(true, mode);
1729 } else {
1730 setupDrawBlending(mode);
1731 }
1732 setupDrawProgram();
1733 setupDrawModelViewIdentity(true);
1734 setupDrawColorUniforms();
1735 setupDrawColorFilterUniforms();
1736 setupDrawShaderIdentityUniforms();
1737
1738 if (isHairLine) {
1739 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001740 halfStrokeWidth = isAA? 1 : .5;
1741 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001742 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001743 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001744 }
1745 Vertex lines[verticesCount];
1746 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001747 AAVertex wLines[verticesCount];
1748 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001749 if (!isAA) {
1750 setupDrawVertices(vertices);
1751 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001752 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1753 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001754 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001755 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1756 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001757 // We will need to calculate the actual width proportion on each segment for
1758 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1759 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1760 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001761 }
1762
Chet Haase99ecdc42011-05-06 12:06:34 -07001763 AAVertex* prevAAVertex = NULL;
1764 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001765
Chet Haase99585ad2011-05-02 15:00:16 -07001766 int boundaryLengthSlot = -1;
1767 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001768 int boundaryWidthSlot = -1;
1769 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001770 for (int i = 0; i < count; i += 4) {
1771 // a = start point, b = end point
1772 vec2 a(points[i], points[i + 1]);
1773 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001774 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001775 float boundaryLengthProportion = 0;
1776 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001777
Chet Haase5b0200b2011-04-13 17:58:08 -07001778 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001779 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001780 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001781 if (isAA) {
1782 float wideningFactor;
1783 if (fabs(n.x) >= fabs(n.y)) {
1784 wideningFactor = fabs(1.0f / n.x);
1785 } else {
1786 wideningFactor = fabs(1.0f / n.y);
1787 }
1788 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001789 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001790 if (scaled) {
1791 n.x *= inverseScaleX;
1792 n.y *= inverseScaleY;
1793 }
1794 } else if (scaled) {
1795 // Extend n by .5 pixel on each side, post-transform
1796 vec2 extendedN = n.copyNormalized();
1797 extendedN /= 2;
1798 extendedN.x *= inverseScaleX;
1799 extendedN.y *= inverseScaleY;
1800 float extendedNLength = extendedN.length();
1801 // We need to set this value on the shader prior to drawing
1802 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1803 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001804 }
1805 float x = n.x;
1806 n.x = -n.y;
1807 n.y = x;
1808
Chet Haase99585ad2011-05-02 15:00:16 -07001809 // aa lines expand the endpoint vertices to encompass the AA boundary
1810 if (isAA) {
1811 vec2 abVector = (b - a);
1812 length = abVector.length();
1813 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001814 if (scaled) {
1815 abVector.x *= inverseScaleX;
1816 abVector.y *= inverseScaleY;
1817 float abLength = abVector.length();
1818 boundaryLengthProportion = abLength / (length + abLength);
1819 } else {
1820 boundaryLengthProportion = .5 / (length + 1);
1821 }
1822 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001823 a -= abVector;
1824 b += abVector;
1825 }
1826
Chet Haase5b0200b2011-04-13 17:58:08 -07001827 // Four corners of the rectangle defining a thick line
1828 vec2 p1 = a - n;
1829 vec2 p2 = a + n;
1830 vec2 p3 = b + n;
1831 vec2 p4 = b - n;
1832
Chet Haase99585ad2011-05-02 15:00:16 -07001833
Chet Haase5b0200b2011-04-13 17:58:08 -07001834 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1835 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1836 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1837 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1838
1839 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001840 if (!isAA) {
1841 if (prevVertex != NULL) {
1842 // Issue two repeat vertices to create degenerate triangles to bridge
1843 // between the previous line and the new one. This is necessary because
1844 // we are creating a single triangle_strip which will contain
1845 // potentially discontinuous line segments.
1846 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1847 Vertex::set(vertices++, p1.x, p1.y);
1848 generatedVerticesCount += 2;
1849 }
1850 Vertex::set(vertices++, p1.x, p1.y);
1851 Vertex::set(vertices++, p2.x, p2.y);
1852 Vertex::set(vertices++, p4.x, p4.y);
1853 Vertex::set(vertices++, p3.x, p3.y);
1854 prevVertex = vertices - 1;
1855 generatedVerticesCount += 4;
1856 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001857 if (!isHairLine && scaled) {
1858 // Must set width proportions per-segment for scaled non-hairlines to use the
1859 // correct AA boundary dimensions
1860 if (boundaryWidthSlot < 0) {
1861 boundaryWidthSlot =
1862 mCaches.currentProgram->getUniform("boundaryWidth");
1863 inverseBoundaryWidthSlot =
1864 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1865 }
1866 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1867 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1868 }
Chet Haase99585ad2011-05-02 15:00:16 -07001869 if (boundaryLengthSlot < 0) {
1870 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1871 inverseBoundaryLengthSlot =
1872 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1873 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001874 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1875 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001876
Chet Haase5b0200b2011-04-13 17:58:08 -07001877 if (prevAAVertex != NULL) {
1878 // Issue two repeat vertices to create degenerate triangles to bridge
1879 // between the previous line and the new one. This is necessary because
1880 // we are creating a single triangle_strip which will contain
1881 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001882 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1883 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1884 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001885 generatedVerticesCount += 2;
1886 }
Chet Haase99585ad2011-05-02 15:00:16 -07001887 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1888 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1889 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1890 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001891 prevAAVertex = aaVertices - 1;
1892 generatedVerticesCount += 4;
1893 }
Chet Haase99585ad2011-05-02 15:00:16 -07001894 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1895 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1896 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001897 }
1898 }
1899 if (generatedVerticesCount > 0) {
1900 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1901 }
1902}
1903
Romain Guyed6fcb02011-03-21 13:11:28 -07001904void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1905 if (mSnapshot->isIgnored()) return;
1906
1907 // TODO: The paint's cap style defines whether the points are square or circular
1908 // TODO: Handle AA for round points
1909
Chet Haase5b0200b2011-04-13 17:58:08 -07001910 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001911 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001912 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001913 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001914 if (isHairLine) {
1915 // Now that we know it's hairline, we can set the effective width, to be used later
1916 strokeWidth = 1.0f;
1917 }
1918 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001919 int alpha;
1920 SkXfermode::Mode mode;
1921 getAlphaAndMode(paint, &alpha, &mode);
1922
1923 int verticesCount = count >> 1;
1924 int generatedVerticesCount = 0;
1925
1926 TextureVertex pointsData[verticesCount];
1927 TextureVertex* vertex = &pointsData[0];
1928
1929 setupDraw();
Chet Haase8a5cc922011-04-26 07:28:09 -07001930 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001931 setupDrawColor(paint->getColor(), alpha);
1932 setupDrawColorFilter();
1933 setupDrawShader();
1934 setupDrawBlending(mode);
1935 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001936 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001937 setupDrawColorUniforms();
1938 setupDrawColorFilterUniforms();
1939 setupDrawPointUniforms();
1940 setupDrawShaderIdentityUniforms();
1941 setupDrawMesh(vertex);
1942
1943 for (int i = 0; i < count; i += 2) {
1944 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1945 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001946 float left = points[i] - halfWidth;
1947 float right = points[i] + halfWidth;
1948 float top = points[i + 1] - halfWidth;
1949 float bottom = points [i + 1] + halfWidth;
1950 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001951 }
1952
1953 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1954}
1955
Romain Guy85bf02f2010-06-22 13:11:24 -07001956void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001957 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001958 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001959
Romain Guyae88e5e2010-10-22 17:49:18 -07001960 Rect& clip(*mSnapshot->clipRect);
1961 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001962
Romain Guy3d58c032010-07-14 16:34:53 -07001963 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001964}
Romain Guy9d5316e2010-06-24 19:30:36 -07001965
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001966void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001967 if (!texture) return;
1968 const AutoTexture autoCleanup(texture);
1969
1970 const float x = left + texture->left - texture->offset;
1971 const float y = top + texture->top - texture->offset;
1972
1973 drawPathTexture(texture, x, y, paint);
1974}
1975
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001976void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1977 float rx, float ry, SkPaint* paint) {
1978 if (mSnapshot->isIgnored()) return;
1979
1980 glActiveTexture(gTextureUnits[0]);
1981 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1982 right - left, bottom - top, rx, ry, paint);
1983 drawShape(left, top, texture, paint);
1984}
1985
Romain Guy01d58e42011-01-19 21:54:02 -08001986void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1987 if (mSnapshot->isIgnored()) return;
1988
1989 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001990 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001991 drawShape(x - radius, y - radius, texture, paint);
1992}
Romain Guy01d58e42011-01-19 21:54:02 -08001993
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001994void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1995 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001996
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001997 glActiveTexture(gTextureUnits[0]);
1998 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1999 drawShape(left, top, texture, paint);
2000}
2001
Romain Guy8b2f5262011-01-23 16:15:02 -08002002void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2003 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2004 if (mSnapshot->isIgnored()) return;
2005
2006 if (fabs(sweepAngle) >= 360.0f) {
2007 drawOval(left, top, right, bottom, paint);
2008 return;
2009 }
2010
2011 glActiveTexture(gTextureUnits[0]);
2012 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2013 startAngle, sweepAngle, useCenter, paint);
2014 drawShape(left, top, texture, paint);
2015}
2016
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002017void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2018 SkPaint* paint) {
2019 if (mSnapshot->isIgnored()) return;
2020
2021 glActiveTexture(gTextureUnits[0]);
2022 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2023 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002024}
2025
Chet Haase5c13d892010-10-08 08:37:55 -07002026void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002027 if (p->getStyle() != SkPaint::kFill_Style) {
2028 drawRectAsShape(left, top, right, bottom, p);
2029 return;
2030 }
2031
Romain Guy6926c722010-07-12 20:20:03 -07002032 if (quickReject(left, top, right, bottom)) {
2033 return;
2034 }
2035
Romain Guy026c5e162010-06-28 17:12:22 -07002036 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002037 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002038 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2039 if (!isMode) {
2040 // Assume SRC_OVER
2041 mode = SkXfermode::kSrcOver_Mode;
2042 }
2043 } else {
2044 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002045 }
2046
Romain Guy026c5e162010-06-28 17:12:22 -07002047 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002048 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002049 drawAARect(left, top, right, bottom, color, mode);
2050 } else {
2051 drawColorRect(left, top, right, bottom, color, mode);
2052 }
Romain Guyc7d53492010-06-25 13:41:57 -07002053}
Romain Guy9d5316e2010-06-24 19:30:36 -07002054
Romain Guye8e62a42010-07-23 18:55:21 -07002055void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
2056 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08002057 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07002058 return;
2059 }
Romain Guyaf636eb2010-12-09 17:47:21 -08002060 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07002061
Romain Guy67ffc362011-06-03 18:50:11 -07002062 // TODO: We should probably make a copy of the paint instead of modifying
2063 // it; modifying the paint will change its generationID the first
2064 // time, which might impact caches. More investigation needed to
2065 // see if it matters.
2066 // If we make a copy, then drawTextDecorations() should *not* make
2067 // its own copy as it does right now.
Romain Guyf607bdc2010-09-10 19:20:06 -07002068 paint->setAntiAlias(true);
Romain Guy67ffc362011-06-03 18:50:11 -07002069#if RENDER_TEXT_AS_GLYPHS
2070 paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
2071#endif
Romain Guye8e62a42010-07-23 18:55:21 -07002072
Romain Guya674ab72010-08-10 17:26:42 -07002073 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07002074 switch (paint->getTextAlign()) {
2075 case SkPaint::kCenter_Align:
2076 length = paint->measureText(text, bytesCount);
2077 x -= length / 2.0f;
2078 break;
2079 case SkPaint::kRight_Align:
2080 length = paint->measureText(text, bytesCount);
2081 x -= length;
2082 break;
2083 default:
2084 break;
2085 }
2086
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002087 const float oldX = x;
2088 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002089 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2090 if (pureTranslate) {
2091 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2092 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2093 }
2094
Romain Guyb45c0c92010-08-26 20:35:23 -07002095 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2096 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002097 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002098
Romain Guy86568192010-12-14 15:55:39 -08002099 int alpha;
2100 SkXfermode::Mode mode;
2101 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002102
Romain Guy1e45aae2010-08-13 19:39:53 -07002103 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07002104 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002105 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2106 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002107 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002108
Romain Guy740bf2b2011-04-26 15:33:10 -07002109 const float sx = oldX - shadow->left + mShadowDx;
2110 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002111
Romain Guy86568192010-12-14 15:55:39 -08002112 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002113 int shadowColor = mShadowColor;
2114 if (mShader) {
2115 shadowColor = 0xffffffff;
2116 }
Romain Guy86568192010-12-14 15:55:39 -08002117
2118 glActiveTexture(gTextureUnits[0]);
2119 setupDraw();
2120 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002121 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2122 setupDrawColorFilter();
2123 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002124 setupDrawBlending(true, mode);
2125 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002126 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002127 setupDrawTexture(shadow->id);
2128 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002129 setupDrawColorFilterUniforms();
2130 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002131 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2132
Romain Guy0a417492010-08-16 20:26:20 -07002133 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy740bf2b2011-04-26 15:33:10 -07002134
Romain Guy86568192010-12-14 15:55:39 -08002135 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07002136 }
2137
Romain Guyb146b122010-12-15 17:06:45 -08002138 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
2139 return;
2140 }
2141
Romain Guy6620c6d2010-12-06 18:07:02 -08002142 // Pick the appropriate texture filtering
2143 bool linearFilter = mSnapshot->transform->changesBounds();
2144 if (pureTranslate && !linearFilter) {
2145 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2146 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002147
Romain Guy86568192010-12-14 15:55:39 -08002148 glActiveTexture(gTextureUnits[0]);
2149 setupDraw();
2150 setupDrawDirtyRegionsDisabled();
2151 setupDrawWithTexture(true);
2152 setupDrawAlpha8Color(paint->getColor(), alpha);
2153 setupDrawColorFilter();
2154 setupDrawShader();
2155 setupDrawBlending(true, mode);
2156 setupDrawProgram();
2157 setupDrawModelView(x, y, x, y, pureTranslate, true);
2158 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2159 setupDrawPureColorUniforms();
2160 setupDrawColorFilterUniforms();
2161 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002162
Romain Guy6620c6d2010-12-06 18:07:02 -08002163 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002164 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2165
2166#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002167 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002168#else
Romain Guyf219da52011-01-16 12:54:25 -08002169 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002170#endif
Romain Guy03750a02010-10-18 14:06:08 -07002171 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002172
2173 // Tell font renderer the locations of position and texture coord
2174 // attributes so it can bind its data properly
2175 int positionSlot = mCaches.currentProgram->position;
2176 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08002177 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002178 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002179#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002180 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002181 if (!pureTranslate) {
2182 mSnapshot->transform->mapRect(bounds);
2183 }
Romain Guyf219da52011-01-16 12:54:25 -08002184 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002185 }
2186#endif
2187 }
Romain Guy694b5192010-07-21 21:33:20 -07002188
2189 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07002190 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07002191
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002192 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002193}
2194
Romain Guy7fbcc042010-08-04 15:40:07 -07002195void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002196 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002197
Romain Guy01d58e42011-01-19 21:54:02 -08002198 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07002199
Romain Guyfb8b7632010-08-23 21:05:08 -07002200 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07002201 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002202 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002203
Romain Guy8b55f372010-08-18 17:10:07 -07002204 const float x = texture->left - texture->offset;
2205 const float y = texture->top - texture->offset;
2206
Romain Guy01d58e42011-01-19 21:54:02 -08002207 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002208}
2209
Romain Guyada830f2011-01-13 12:13:20 -08002210void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2211 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002212 return;
2213 }
2214
2215 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08002216
2217 int alpha;
2218 SkXfermode::Mode mode;
2219 getAlphaAndMode(paint, &alpha, &mode);
2220
Romain Guy9ace8f52011-07-07 20:50:11 -07002221 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002222
Romain Guyf219da52011-01-16 12:54:25 -08002223#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002224 if (!layer->region.isEmpty()) {
2225 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002226 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002227 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002228 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002229 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002230
Romain Guyc88e3572011-01-22 00:32:12 -08002231 setupDraw();
2232 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002233 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002234 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002235 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002236 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002237 setupDrawPureColorUniforms();
2238 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002239 setupDrawTexture(layer->getTexture());
2240 if (mSnapshot->transform->isPureTranslate()) {
2241 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2242 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2243
2244 layer->setFilter(GL_NEAREST, GL_NEAREST);
2245 setupDrawModelViewTranslate(x, y,
2246 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2247 } else {
2248 layer->setFilter(GL_LINEAR, GL_LINEAR);
2249 setupDrawModelViewTranslate(x, y,
2250 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2251 }
Romain Guyc88e3572011-01-22 00:32:12 -08002252 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002253
Romain Guyc88e3572011-01-22 00:32:12 -08002254 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2255 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002256
Romain Guyc88e3572011-01-22 00:32:12 -08002257 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002258
2259#if DEBUG_LAYERS_AS_REGIONS
2260 drawRegionRects(layer->region);
2261#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002262 }
Romain Guyf219da52011-01-16 12:54:25 -08002263 }
2264#else
Romain Guyada830f2011-01-13 12:13:20 -08002265 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2266 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002267#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002268}
2269
Romain Guy6926c722010-07-12 20:20:03 -07002270///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002271// Shaders
2272///////////////////////////////////////////////////////////////////////////////
2273
2274void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002275 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002276}
2277
Romain Guy06f96e22010-07-30 19:18:16 -07002278void OpenGLRenderer::setupShader(SkiaShader* shader) {
2279 mShader = shader;
2280 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002281 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002282 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002283}
2284
Romain Guyd27977d2010-07-14 19:18:51 -07002285///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002286// Color filters
2287///////////////////////////////////////////////////////////////////////////////
2288
2289void OpenGLRenderer::resetColorFilter() {
2290 mColorFilter = NULL;
2291}
2292
2293void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2294 mColorFilter = filter;
2295}
2296
2297///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002298// Drop shadow
2299///////////////////////////////////////////////////////////////////////////////
2300
2301void OpenGLRenderer::resetShadow() {
2302 mHasShadow = false;
2303}
2304
2305void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2306 mHasShadow = true;
2307 mShadowRadius = radius;
2308 mShadowDx = dx;
2309 mShadowDy = dy;
2310 mShadowColor = color;
2311}
2312
2313///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002314// Drawing implementation
2315///////////////////////////////////////////////////////////////////////////////
2316
Romain Guy01d58e42011-01-19 21:54:02 -08002317void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2318 float x, float y, SkPaint* paint) {
2319 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2320 return;
2321 }
2322
2323 int alpha;
2324 SkXfermode::Mode mode;
2325 getAlphaAndMode(paint, &alpha, &mode);
2326
2327 setupDraw();
2328 setupDrawWithTexture(true);
2329 setupDrawAlpha8Color(paint->getColor(), alpha);
2330 setupDrawColorFilter();
2331 setupDrawShader();
2332 setupDrawBlending(true, mode);
2333 setupDrawProgram();
2334 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2335 setupDrawTexture(texture->id);
2336 setupDrawPureColorUniforms();
2337 setupDrawColorFilterUniforms();
2338 setupDrawShaderUniforms();
2339 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2340
2341 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2342
2343 finishDrawTexture();
2344}
2345
Romain Guyf607bdc2010-09-10 19:20:06 -07002346// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002347#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2348#define kStdUnderline_Offset (1.0f / 9.0f)
2349#define kStdUnderline_Thickness (1.0f / 18.0f)
2350
2351void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2352 float x, float y, SkPaint* paint) {
2353 // Handle underline and strike-through
2354 uint32_t flags = paint->getFlags();
2355 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002356 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002357 float underlineWidth = length;
2358 // If length is > 0.0f, we already measured the text for the text alignment
2359 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002360 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002361 }
2362
2363 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002364 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002365 case SkPaint::kCenter_Align:
2366 offsetX = underlineWidth * 0.5f;
2367 break;
2368 case SkPaint::kRight_Align:
2369 offsetX = underlineWidth;
2370 break;
2371 default:
2372 break;
2373 }
2374
2375 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002376 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002377 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002378
Romain Guye20ecbd2010-09-22 19:49:04 -07002379 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002380 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002381
Romain Guyf6834472011-01-23 13:32:12 -08002382 int linesCount = 0;
2383 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2384 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2385
2386 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002387 float points[pointsCount];
2388 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002389
2390 if (flags & SkPaint::kUnderlineText_Flag) {
2391 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002392 points[currentPoint++] = left;
2393 points[currentPoint++] = top;
2394 points[currentPoint++] = left + underlineWidth;
2395 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002396 }
2397
2398 if (flags & SkPaint::kStrikeThruText_Flag) {
2399 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002400 points[currentPoint++] = left;
2401 points[currentPoint++] = top;
2402 points[currentPoint++] = left + underlineWidth;
2403 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002404 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002405
Romain Guy726aeba2011-06-01 14:52:00 -07002406 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002407
Romain Guy726aeba2011-06-01 14:52:00 -07002408 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002409 }
2410 }
2411}
2412
Romain Guy026c5e162010-06-28 17:12:22 -07002413void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002414 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002415 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002416 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002417 color |= 0x00ffffff;
2418 }
2419
Romain Guy70ca14e2010-12-13 18:24:33 -08002420 setupDraw();
2421 setupDrawColor(color);
2422 setupDrawShader();
2423 setupDrawColorFilter();
2424 setupDrawBlending(mode);
2425 setupDrawProgram();
2426 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2427 setupDrawColorUniforms();
2428 setupDrawShaderUniforms(ignoreTransform);
2429 setupDrawColorFilterUniforms();
2430 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002431
Romain Guyc95c8d62010-09-17 15:31:32 -07002432 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2433}
2434
Romain Guy82ba8142010-07-09 13:25:56 -07002435void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002436 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002437 int alpha;
2438 SkXfermode::Mode mode;
2439 getAlphaAndMode(paint, &alpha, &mode);
2440
Romain Guye3c26852011-07-25 16:36:01 -07002441 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002442
Romain Guy6620c6d2010-12-06 18:07:02 -08002443 if (mSnapshot->transform->isPureTranslate()) {
2444 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2445 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2446
Romain Guye3c26852011-07-25 16:36:01 -07002447 texture->setFilter(GL_NEAREST, GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002448 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2449 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2450 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2451 } else {
Romain Guye3c26852011-07-25 16:36:01 -07002452 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002453 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2454 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2455 GL_TRIANGLE_STRIP, gMeshCount);
2456 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002457}
2458
Romain Guybd6b79b2010-06-26 00:13:53 -07002459void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002460 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2461 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002462 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002463}
2464
2465void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002466 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002467 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002468 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002469
Romain Guy746b7402010-10-26 16:27:31 -07002470 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002471 setupDrawWithTexture();
2472 setupDrawColor(alpha, alpha, alpha, alpha);
2473 setupDrawColorFilter();
2474 setupDrawBlending(blend, mode, swapSrcDst);
2475 setupDrawProgram();
2476 if (!dirty) {
2477 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002478 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002479 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002480 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002481 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002482 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002483 }
Romain Guy86568192010-12-14 15:55:39 -08002484 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002485 setupDrawColorFilterUniforms();
2486 setupDrawTexture(texture);
2487 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002488
Romain Guy6820ac82010-09-15 18:11:50 -07002489 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002490
2491 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002492}
2493
Romain Guya5aed0d2010-09-09 14:42:43 -07002494void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002495 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002496 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2497 if (blend) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002498 if (mode <= SkXfermode::kScreen_Mode) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002499 if (!mCaches.blend) {
2500 glEnable(GL_BLEND);
2501 }
Romain Guy82ba8142010-07-09 13:25:56 -07002502
Romain Guyf607bdc2010-09-10 19:20:06 -07002503 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2504 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07002505
Romain Guya5aed0d2010-09-09 14:42:43 -07002506 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2507 glBlendFunc(sourceMode, destMode);
2508 mCaches.lastSrcMode = sourceMode;
2509 mCaches.lastDstMode = destMode;
2510 }
2511 } else {
2512 // These blend modes are not supported by OpenGL directly and have
2513 // to be implemented using shaders. Since the shader will perform
2514 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07002515 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002516 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002517 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002518 }
2519
2520 if (mCaches.blend) {
2521 glDisable(GL_BLEND);
2522 }
2523 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07002524 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002525 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002526 glDisable(GL_BLEND);
2527 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002528 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002529}
2530
Romain Guy889f8d12010-07-29 14:37:42 -07002531bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002532 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002533 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002534 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002535 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002536 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002537 }
Romain Guy6926c722010-07-12 20:20:03 -07002538 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002539}
2540
Romain Guy026c5e162010-06-28 17:12:22 -07002541void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002542 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002543 TextureVertex::setUV(v++, u1, v1);
2544 TextureVertex::setUV(v++, u2, v1);
2545 TextureVertex::setUV(v++, u1, v2);
2546 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002547}
2548
Chet Haase5c13d892010-10-08 08:37:55 -07002549void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002550 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002551 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002552
2553 // Skia draws using the color's alpha channel if < 255
2554 // Otherwise, it uses the paint's alpha
2555 int color = paint->getColor();
2556 *alpha = (color >> 24) & 0xFF;
2557 if (*alpha == 255) {
2558 *alpha = paint->getAlpha();
2559 }
2560 } else {
2561 *mode = SkXfermode::kSrcOver_Mode;
2562 *alpha = 255;
2563 }
Romain Guy026c5e162010-06-28 17:12:22 -07002564}
2565
Romain Guya5aed0d2010-09-09 14:42:43 -07002566SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002567 SkXfermode::Mode resultMode;
2568 if (!SkXfermode::AsMode(mode, &resultMode)) {
2569 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002570 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002571 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002572}
2573
Romain Guy9d5316e2010-06-24 19:30:36 -07002574}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002575}; // namespace android