blob: f187d3ec3d9c17e9c555e76975fced2bb53a5914 [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>
27
Romain Guy85bf02f2010-06-22 13:11:24 -070028#include "OpenGLRenderer.h"
Romain Guye4d01122010-06-16 18:44:05 -070029
30namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070031namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Defines
35///////////////////////////////////////////////////////////////////////////////
36
Romain Guy06f96e22010-07-30 19:18:16 -070037#define REQUIRED_TEXTURE_UNITS_COUNT 3
38
Romain Guydda570202010-07-06 11:39:32 -070039// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070040#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070041
42///////////////////////////////////////////////////////////////////////////////
43// Globals
44///////////////////////////////////////////////////////////////////////////////
45
Romain Guy026c5e162010-06-28 17:12:22 -070046// This array is never used directly but used as a memcpy source in the
47// OpenGLRenderer constructor
Romain Guyac670c02010-07-27 17:39:27 -070048static const TextureVertex gMeshVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070049 FV(0.0f, 0.0f, 0.0f, 0.0f),
50 FV(1.0f, 0.0f, 1.0f, 0.0f),
51 FV(0.0f, 1.0f, 0.0f, 1.0f),
52 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070053};
Romain Guyac670c02010-07-27 17:39:27 -070054static const GLsizei gMeshStride = sizeof(TextureVertex);
55static const GLsizei gMeshCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070056
Romain Guy889f8d12010-07-29 14:37:42 -070057/**
58 * Structure mapping Skia xfermodes to OpenGL blending factors.
59 */
60struct Blender {
61 SkXfermode::Mode mode;
62 GLenum src;
63 GLenum dst;
64}; // struct Blender
65
Romain Guy026c5e162010-06-28 17:12:22 -070066// In this array, the index of each Blender equals the value of the first
67// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
68static const Blender gBlends[] = {
69 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
70 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
71 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
72 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
73 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
74 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
75 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
76 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
80 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
81};
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[] = {
87 { SkXfermode::kClear_Mode, GL_ZERO, 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};
100
Romain Guy889f8d12010-07-29 14:37:42 -0700101static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -0700102 GL_TEXTURE0,
103 GL_TEXTURE1,
104 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700105};
106
Romain Guyf6a11b82010-06-23 17:47:49 -0700107///////////////////////////////////////////////////////////////////////////////
108// Constructors/destructor
109///////////////////////////////////////////////////////////////////////////////
110
Romain Guyfb8b7632010-08-23 21:05:08 -0700111OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy85bf02f2010-06-22 13:11:24 -0700112 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700113
Romain Guy06f96e22010-07-30 19:18:16 -0700114 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700115 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700116 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700117
Romain Guyac670c02010-07-27 17:39:27 -0700118 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
119
Romain Guyae5575b2010-07-29 18:48:04 -0700120 mFirstSnapshot = new Snapshot;
121
122 GLint maxTextureUnits;
123 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
124 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Romain Guy889f8d12010-07-29 14:37:42 -0700125 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
126 }
Romain Guye4d01122010-06-16 18:44:05 -0700127}
128
Romain Guy85bf02f2010-06-22 13:11:24 -0700129OpenGLRenderer::~OpenGLRenderer() {
130 LOGD("Destroy OpenGLRenderer");
Romain Guye4d01122010-06-16 18:44:05 -0700131}
132
Romain Guyf6a11b82010-06-23 17:47:49 -0700133///////////////////////////////////////////////////////////////////////////////
134// Setup
135///////////////////////////////////////////////////////////////////////////////
136
Romain Guy85bf02f2010-06-22 13:11:24 -0700137void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700138 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700139 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700140
141 mWidth = width;
142 mHeight = height;
Romain Guye4d01122010-06-16 18:44:05 -0700143}
144
Romain Guy85bf02f2010-06-22 13:11:24 -0700145void OpenGLRenderer::prepare() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700146 mSnapshot = new Snapshot(mFirstSnapshot,
147 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy8fb95422010-08-17 18:38:51 -0700148 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700149
Romain Guyfb8b7632010-08-23 21:05:08 -0700150 glViewport(0, 0, mWidth, mHeight);
151
Romain Guyd90f23e2010-09-09 11:47:54 -0700152 glDisable(GL_DITHER);
Romain Guy08ae3172010-06-21 19:35:50 -0700153 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700154
Romain Guy08ae3172010-06-21 19:35:50 -0700155 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
156 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700157
Romain Guy08ae3172010-06-21 19:35:50 -0700158 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700159 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700160
Romain Guyb82da652010-07-30 11:36:12 -0700161 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700162}
163
Romain Guyda8532c2010-08-31 11:50:35 -0700164void OpenGLRenderer::acquireContext() {
165 if (mCaches.currentProgram) {
166 if (mCaches.currentProgram->isInUse()) {
167 mCaches.currentProgram->remove();
168 mCaches.currentProgram = NULL;
169 }
170 }
171}
172
173void OpenGLRenderer::releaseContext() {
Romain Guyf607bdc2010-09-10 19:20:06 -0700174 glViewport(0, 0, mWidth, mHeight);
Romain Guyda8532c2010-08-31 11:50:35 -0700175
176 glEnable(GL_SCISSOR_TEST);
177 setScissorFromClip();
178
Romain Guyf607bdc2010-09-10 19:20:06 -0700179 glDisable(GL_DITHER);
180
181 glBindFramebuffer(GL_FRAMEBUFFER, 0);
182
Romain Guyda8532c2010-08-31 11:50:35 -0700183 if (mCaches.blend) {
184 glEnable(GL_BLEND);
185 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
Romain Guyf607bdc2010-09-10 19:20:06 -0700186 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700187 } else {
188 glDisable(GL_BLEND);
189 }
190}
191
Romain Guyf6a11b82010-06-23 17:47:49 -0700192///////////////////////////////////////////////////////////////////////////////
193// State management
194///////////////////////////////////////////////////////////////////////////////
195
Romain Guybb9524b2010-06-22 18:56:38 -0700196int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700197 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700198}
199
200int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700201 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700202}
203
204void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700205 if (mSaveCount > 1) {
206 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700207 }
Romain Guybb9524b2010-06-22 18:56:38 -0700208}
209
210void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700211 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700212
Romain Guy8fb95422010-08-17 18:38:51 -0700213 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700214 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700215 }
Romain Guybb9524b2010-06-22 18:56:38 -0700216}
217
Romain Guy8aef54f2010-09-01 15:13:49 -0700218int OpenGLRenderer::saveSnapshot(int flags) {
219 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700220 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700221}
222
223bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700224 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700225 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guybb9524b2010-06-22 18:56:38 -0700226
Romain Guybd6b79b2010-06-26 00:13:53 -0700227 sp<Snapshot> current = mSnapshot;
228 sp<Snapshot> previous = mSnapshot->previous;
229
Romain Guy8b55f372010-08-18 17:10:07 -0700230 mSaveCount--;
231 mSnapshot = previous;
232
Romain Guybd6b79b2010-06-26 00:13:53 -0700233 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700234 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700235 }
236
Romain Guy2542d192010-08-18 11:47:12 -0700237 if (restoreClip) {
238 setScissorFromClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700239 }
Romain Guy2542d192010-08-18 11:47:12 -0700240
241 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700242}
243
Romain Guyf6a11b82010-06-23 17:47:49 -0700244///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700245// Layers
246///////////////////////////////////////////////////////////////////////////////
247
248int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
249 const SkPaint* p, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700250 int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700251
252 int alpha = 255;
253 SkXfermode::Mode mode;
254
255 if (p) {
256 alpha = p->getAlpha();
Romain Guya5aed0d2010-09-09 14:42:43 -0700257 if (!mExtensions.hasFramebufferFetch()) {
258 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
259 if (!isMode) {
260 // Assume SRC_OVER
261 mode = SkXfermode::kSrcOver_Mode;
262 }
263 } else {
264 mode = getXfermode(p->getXfermode());
Romain Guyd55a8612010-06-28 17:42:46 -0700265 }
266 } else {
267 mode = SkXfermode::kSrcOver_Mode;
268 }
269
Romain Guyf607bdc2010-09-10 19:20:06 -0700270 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700271
272 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700273}
274
275int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
276 int alpha, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700277 if (alpha == 0xff) {
278 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700279 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700280 SkPaint paint;
281 paint.setAlpha(alpha);
282 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700283 }
Romain Guyd55a8612010-06-28 17:42:46 -0700284}
Romain Guybd6b79b2010-06-26 00:13:53 -0700285
Romain Guy1c740bc2010-09-13 18:00:09 -0700286/**
287 * Layers are viewed by Skia are slightly different than layers in image editing
288 * programs (for instance.) When a layer is created, previously created layers
289 * and the frame buffer still receive every drawing command. For instance, if a
290 * layer is created and a shape intersecting the bounds of the layers and the
291 * framebuffer is draw, the shape will be drawn on both (unless the layer was
292 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
293 *
294 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
295 * texture. Unfortunately, this is inefficient as it requires every primitive to
296 * be drawn n + 1 times, where n is the number of active layers. In practice this
297 * means, for every primitive:
298 * - Switch active frame buffer
299 * - Change viewport, clip and projection matrix
300 * - Issue the drawing
301 *
302 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
303 * To avoid this, layers are implemented in a different way here.
304 *
305 * This implementation relies on the frame buffer being at least RGBA 8888. When
306 * a layer is created, only a texture is created, not an FBO. The content of the
307 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700308 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700309 * buffer and drawing continues as normal. This technique therefore treats the
310 * frame buffer as a scratch buffer for the layers.
311 *
312 * To compose the layers back onto the frame buffer, each layer texture
313 * (containing the original frame buffer data) is drawn as a simple quad over
314 * the frame buffer. The trick is that the quad is set as the composition
315 * destination in the blending equation, and the frame buffer becomes the source
316 * of the composition.
317 *
318 * Drawing layers with an alpha value requires an extra step before composition.
319 * An empty quad is drawn over the layer's region in the frame buffer. This quad
320 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
321 * quad is used to multiply the colors in the frame buffer. This is achieved by
322 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
323 * GL_ZERO, GL_SRC_ALPHA.
324 *
325 * Because glCopyTexImage2D() can be slow, an alternative implementation might
326 * be use to draw a single clipped layer. The implementation described above
327 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700328 *
329 * (1) The frame buffer is actually not cleared right away. To allow the GPU
330 * to potentially optimize series of calls to glCopyTexImage2D, the frame
331 * buffer is left untouched until the first drawing operation. Only when
332 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700333 */
Romain Guyd55a8612010-06-28 17:42:46 -0700334bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
335 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700336 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700337 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700338
Romain Guyf607bdc2010-09-10 19:20:06 -0700339 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700340 Rect bounds(left, top, right, bottom);
Romain Guyf607bdc2010-09-10 19:20:06 -0700341 mSnapshot->transform->mapRect(bounds);
Romain Guy8aef54f2010-09-01 15:13:49 -0700342
Romain Guy8411f332010-09-13 17:27:57 -0700343 // Layers only make sense if they are in the framebuffer's bounds
344 bounds.intersect(*mSnapshot->clipRect);
Romain Guy81ab0462010-09-13 17:32:34 -0700345 if (bounds.isEmpty()) return false;
Romain Guyf18fd992010-07-08 11:45:51 -0700346
Romain Guy8411f332010-09-13 17:27:57 -0700347 LayerSize size(bounds.getWidth(), bounds.getHeight());
Romain Guyf607bdc2010-09-10 19:20:06 -0700348 Layer* layer = mCaches.layerCache.get(size);
Romain Guydda570202010-07-06 11:39:32 -0700349 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700350 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700351 }
352
Romain Guydda570202010-07-06 11:39:32 -0700353 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700354 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700355 layer->layer.set(bounds);
Romain Guydda570202010-07-06 11:39:32 -0700356
Romain Guy8fb95422010-08-17 18:38:51 -0700357 // Save the layer in the snapshot
358 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700359 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700360
Romain Guyf607bdc2010-09-10 19:20:06 -0700361 // Copy the framebuffer into the layer
362 glBindTexture(GL_TEXTURE_2D, layer->texture);
363 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
364 bounds.getWidth(), bounds.getHeight(), 0);
365
Romain Guyf607bdc2010-09-10 19:20:06 -0700366 if (flags & SkCanvas::kClipToLayer_SaveFlag) {
Romain Guy86942302010-09-12 13:02:16 -0700367 if (mSnapshot->clipTransformed(bounds)) setScissorFromClip();
Romain Guyf607bdc2010-09-10 19:20:06 -0700368 }
369
Romain Guy86942302010-09-12 13:02:16 -0700370 // Enqueue the buffer coordinates to clear the corresponding region later
371 mLayers.push(new Rect(bounds));
Romain Guyf86ef572010-07-01 11:05:42 -0700372
Romain Guyd55a8612010-06-28 17:42:46 -0700373 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700374}
375
Romain Guy1c740bc2010-09-13 18:00:09 -0700376/**
377 * Read the documentation of createLayer() before doing anything in this method.
378 */
Romain Guy1d83e192010-08-17 11:37:00 -0700379void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
380 if (!current->layer) {
381 LOGE("Attempting to compose a layer that does not exist");
382 return;
383 }
384
Romain Guy1d83e192010-08-17 11:37:00 -0700385 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700386 const Rect& clip = *previous->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700387 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700388
389 Layer* layer = current->layer;
390 const Rect& rect = layer->layer;
391
Romain Guyf607bdc2010-09-10 19:20:06 -0700392 if (layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700393 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700394 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guyf607bdc2010-09-10 19:20:06 -0700395 }
396
397 // Layers are already drawn with a top-left origin, don't flip the texture
Romain Guy8b55f372010-08-18 17:10:07 -0700398 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
399
Romain Guyf607bdc2010-09-10 19:20:06 -0700400 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
401 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
Romain Guy6820ac82010-09-15 18:11:50 -0700402 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, true, true);
Romain Guy1d83e192010-08-17 11:37:00 -0700403
Romain Guy8b55f372010-08-18 17:10:07 -0700404 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
405
Romain Guy1d83e192010-08-17 11:37:00 -0700406 LayerSize size(rect.getWidth(), rect.getHeight());
407 // Failing to add the layer to the cache should happen only if the
408 // layer is too large
Romain Guyfb8b7632010-08-23 21:05:08 -0700409 if (!mCaches.layerCache.put(size, layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700410 LAYER_LOGD("Deleting layer");
411
Romain Guy1d83e192010-08-17 11:37:00 -0700412 glDeleteTextures(1, &layer->texture);
413
414 delete layer;
415 }
416}
417
Romain Guy86942302010-09-12 13:02:16 -0700418void OpenGLRenderer::clearLayerRegions() {
419 if (mLayers.size() == 0) return;
420
421 for (uint32_t i = 0; i < mLayers.size(); i++) {
422 Rect* bounds = mLayers.itemAt(i);
423
424 // Clear the framebuffer where the layer will draw
425 glScissor(bounds->left, mHeight - bounds->bottom,
426 bounds->getWidth(), bounds->getHeight());
427 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
428 glClear(GL_COLOR_BUFFER_BIT);
429
430 delete bounds;
431 }
432 mLayers.clear();
433
434 // Restore the clip
435 setScissorFromClip();
436}
437
Romain Guybd6b79b2010-06-26 00:13:53 -0700438///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700439// Transforms
440///////////////////////////////////////////////////////////////////////////////
441
442void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700443 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700444}
445
446void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700447 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700448}
449
450void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700451 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700452}
453
454void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700455 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700456}
457
458void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700459 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700460}
461
462void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700463 mat4 m(*matrix);
Romain Guy8aef54f2010-09-01 15:13:49 -0700464 mSnapshot->transform->multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700465}
466
467///////////////////////////////////////////////////////////////////////////////
468// Clipping
469///////////////////////////////////////////////////////////////////////////////
470
Romain Guybb9524b2010-06-22 18:56:38 -0700471void OpenGLRenderer::setScissorFromClip() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700472 const Rect& clip = *mSnapshot->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700473 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700474}
475
476const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700477 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700478}
479
Romain Guyc7d53492010-06-25 13:41:57 -0700480bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy1d83e192010-08-17 11:37:00 -0700481 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700482 mSnapshot->transform->mapRect(r);
483 return !mSnapshot->clipRect->intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700484}
485
Romain Guy079ba2c2010-07-16 14:12:24 -0700486bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
487 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700488 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700489 setScissorFromClip();
490 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700491 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700492}
493
Romain Guyf6a11b82010-06-23 17:47:49 -0700494///////////////////////////////////////////////////////////////////////////////
495// Drawing
496///////////////////////////////////////////////////////////////////////////////
497
Romain Guyc1396e92010-06-30 17:56:19 -0700498void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700499 const float right = left + bitmap->width();
500 const float bottom = top + bitmap->height();
501
502 if (quickReject(left, top, right, bottom)) {
503 return;
504 }
505
Romain Guy61c8c9c2010-08-09 20:48:09 -0700506 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700507 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700508 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700509 const AutoTexture autoCleanup(texture);
510
Romain Guy6926c722010-07-12 20:20:03 -0700511 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700512}
513
Romain Guyf86ef572010-07-01 11:05:42 -0700514void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
515 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
516 const mat4 transform(*matrix);
517 transform.mapRect(r);
518
Romain Guy6926c722010-07-12 20:20:03 -0700519 if (quickReject(r.left, r.top, r.right, r.bottom)) {
520 return;
521 }
522
Romain Guy61c8c9c2010-08-09 20:48:09 -0700523 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700524 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700525 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700526 const AutoTexture autoCleanup(texture);
527
Romain Guy82ba8142010-07-09 13:25:56 -0700528 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700529}
530
Romain Guy8ba548f2010-06-30 19:21:21 -0700531void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
532 float srcLeft, float srcTop, float srcRight, float srcBottom,
533 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700534 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700535 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
536 return;
537 }
538
Romain Guy61c8c9c2010-08-09 20:48:09 -0700539 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700540 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700541 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700542 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700543
Romain Guy8ba548f2010-06-30 19:21:21 -0700544 const float width = texture->width;
545 const float height = texture->height;
546
547 const float u1 = srcLeft / width;
548 const float v1 = srcTop / height;
549 const float u2 = srcRight / width;
550 const float v2 = srcBottom / height;
551
552 resetDrawTextureTexCoords(u1, v1, u2, v2);
553
Romain Guy82ba8142010-07-09 13:25:56 -0700554 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700555
556 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
557}
558
Romain Guydeba7852010-07-07 17:54:48 -0700559void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
560 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700561 if (quickReject(left, top, right, bottom)) {
562 return;
563 }
564
Romain Guy61c8c9c2010-08-09 20:48:09 -0700565 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700566 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700567 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700568 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700569
570 int alpha;
571 SkXfermode::Mode mode;
572 getAlphaAndMode(paint, &alpha, &mode);
573
Romain Guyfb8b7632010-08-23 21:05:08 -0700574 Patch* mesh = mCaches.patchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700575 mesh->updateVertices(bitmap, left, top, right, bottom,
576 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700577
578 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
579 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700580 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
581 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy6820ac82010-09-15 18:11:50 -0700582 &mesh->vertices[0].texture[0], GL_TRIANGLES, mesh->verticesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700583}
584
Romain Guy85bf02f2010-06-22 13:11:24 -0700585void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700586 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700587 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700588}
Romain Guy9d5316e2010-06-24 19:30:36 -0700589
Romain Guybd6b79b2010-06-26 00:13:53 -0700590void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700591 if (quickReject(left, top, right, bottom)) {
592 return;
593 }
594
Romain Guy026c5e162010-06-28 17:12:22 -0700595 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700596 if (!mExtensions.hasFramebufferFetch()) {
597 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
598 if (!isMode) {
599 // Assume SRC_OVER
600 mode = SkXfermode::kSrcOver_Mode;
601 }
602 } else {
603 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700604 }
605
606 // Skia draws using the color's alpha channel if < 255
607 // Otherwise, it uses the paint's alpha
608 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700609 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700610 color |= p->getAlpha() << 24;
611 }
612
613 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700614}
Romain Guy9d5316e2010-06-24 19:30:36 -0700615
Romain Guye8e62a42010-07-23 18:55:21 -0700616void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
617 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700618 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700619 return;
620 }
Romain Guyf607bdc2010-09-10 19:20:06 -0700621 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700622
Romain Guya674ab72010-08-10 17:26:42 -0700623 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700624 switch (paint->getTextAlign()) {
625 case SkPaint::kCenter_Align:
626 length = paint->measureText(text, bytesCount);
627 x -= length / 2.0f;
628 break;
629 case SkPaint::kRight_Align:
630 length = paint->measureText(text, bytesCount);
631 x -= length;
632 break;
633 default:
634 break;
635 }
636
Romain Guy694b5192010-07-21 21:33:20 -0700637 int alpha;
638 SkXfermode::Mode mode;
639 getAlphaAndMode(paint, &alpha, &mode);
640
Romain Guy2542d192010-08-18 11:47:12 -0700641 uint32_t color = paint->getColor();
642 const GLfloat a = alpha / 255.0f;
643 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
644 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
645 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
646
Romain Guyb45c0c92010-08-26 20:35:23 -0700647 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
648 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700649 paint->getTextSize());
Romain Guy1e45aae2010-08-13 19:39:53 -0700650 if (mHasShadow) {
651 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700652 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700653 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700654 count, mShadowRadius);
655 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700656
Romain Guy2542d192010-08-18 11:47:12 -0700657 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700658
659 // Draw the mesh
660 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700661 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700662 }
663
Romain Guy06f96e22010-07-30 19:18:16 -0700664 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700665 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700666
Romain Guyb45c0c92010-08-26 20:35:23 -0700667 setupTextureAlpha8(fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
Romain Guy0a417492010-08-16 20:26:20 -0700668 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700669
Romain Guy09147fb2010-07-22 13:08:20 -0700670 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy86942302010-09-12 13:02:16 -0700671 clearLayerRegions();
Romain Guyb45c0c92010-08-26 20:35:23 -0700672 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700673
674 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700675 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700676
Romain Guy0a417492010-08-16 20:26:20 -0700677 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -0700678}
679
Romain Guy7fbcc042010-08-04 15:40:07 -0700680void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
681 GLuint textureUnit = 0;
682 glActiveTexture(gTextureUnits[textureUnit]);
683
Romain Guyfb8b7632010-08-23 21:05:08 -0700684 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700685 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700686 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700687
Romain Guy8b55f372010-08-18 17:10:07 -0700688 const float x = texture->left - texture->offset;
689 const float y = texture->top - texture->offset;
690
691 if (quickReject(x, y, x + texture->width, y + texture->height)) {
692 return;
693 }
694
Romain Guy7fbcc042010-08-04 15:40:07 -0700695 int alpha;
696 SkXfermode::Mode mode;
697 getAlphaAndMode(paint, &alpha, &mode);
698
699 uint32_t color = paint->getColor();
700 const GLfloat a = alpha / 255.0f;
701 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
702 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
703 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
704
Romain Guy0a417492010-08-16 20:26:20 -0700705 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
706
Romain Guy86942302010-09-12 13:02:16 -0700707 clearLayerRegions();
708
Romain Guy0a417492010-08-16 20:26:20 -0700709 // Draw the mesh
710 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700711 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700712}
713
Romain Guy6926c722010-07-12 20:20:03 -0700714///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700715// Shaders
716///////////////////////////////////////////////////////////////////////////////
717
718void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700719 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700720}
721
Romain Guy06f96e22010-07-30 19:18:16 -0700722void OpenGLRenderer::setupShader(SkiaShader* shader) {
723 mShader = shader;
724 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700725 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700726 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700727}
728
Romain Guyd27977d2010-07-14 19:18:51 -0700729///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700730// Color filters
731///////////////////////////////////////////////////////////////////////////////
732
733void OpenGLRenderer::resetColorFilter() {
734 mColorFilter = NULL;
735}
736
737void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
738 mColorFilter = filter;
739}
740
741///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700742// Drop shadow
743///////////////////////////////////////////////////////////////////////////////
744
745void OpenGLRenderer::resetShadow() {
746 mHasShadow = false;
747}
748
749void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
750 mHasShadow = true;
751 mShadowRadius = radius;
752 mShadowDx = dx;
753 mShadowDy = dy;
754 mShadowColor = color;
755}
756
757///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700758// Drawing implementation
759///////////////////////////////////////////////////////////////////////////////
760
Romain Guy0a417492010-08-16 20:26:20 -0700761void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700762 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700763 const float sx = x - texture->left + mShadowDx;
764 const float sy = y - texture->top + mShadowDy;
765
Romain Guy2542d192010-08-18 11:47:12 -0700766 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
767 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700768 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
769 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
770 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
771
772 GLuint textureUnit = 0;
773 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
774}
775
776void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
777 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
778 bool transforms, bool applyFilters) {
779 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
780 x, y, r, g, b, a, mode, transforms, applyFilters);
781}
782
783void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
784 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
785 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
786 // Describe the required shaders
787 ProgramDescription description;
788 description.hasTexture = true;
789 description.hasAlpha8Texture = true;
790
791 if (applyFilters) {
792 if (mShader) {
793 mShader->describe(description, mExtensions);
794 }
795 if (mColorFilter) {
796 mColorFilter->describe(description, mExtensions);
797 }
798 }
799
Romain Guya5aed0d2010-09-09 14:42:43 -0700800 // Setup the blending mode
801 chooseBlending(true, mode, description);
802
Romain Guy0a417492010-08-16 20:26:20 -0700803 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700804 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700805
Romain Guy0a417492010-08-16 20:26:20 -0700806 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -0700807 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700808
Romain Guyfb8b7632010-08-23 21:05:08 -0700809 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -0700810 glEnableVertexAttribArray(texCoordsSlot);
811
812 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700813 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy0a417492010-08-16 20:26:20 -0700814 gMeshStride, &mMeshVertices[0].position[0]);
815 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
816 gMeshStride, &mMeshVertices[0].texture[0]);
817
818 // Setup uniforms
819 if (transforms) {
820 mModelView.loadTranslate(x, y, 0.0f);
821 mModelView.scale(width, height, 1.0f);
822 } else {
823 mModelView.loadIdentity();
824 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700825 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyfb8b7632010-08-23 21:05:08 -0700826 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -0700827
828 textureUnit++;
829 if (applyFilters) {
830 // Setup attributes and uniforms required by the shaders
831 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700832 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700833 }
834 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700835 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -0700836 }
837 }
838}
839
Romain Guyf607bdc2010-09-10 19:20:06 -0700840// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -0700841#define kStdStrikeThru_Offset (-6.0f / 21.0f)
842#define kStdUnderline_Offset (1.0f / 9.0f)
843#define kStdUnderline_Thickness (1.0f / 18.0f)
844
845void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
846 float x, float y, SkPaint* paint) {
847 // Handle underline and strike-through
848 uint32_t flags = paint->getFlags();
849 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
850 float underlineWidth = length;
851 // If length is > 0.0f, we already measured the text for the text alignment
852 if (length <= 0.0f) {
853 underlineWidth = paint->measureText(text, bytesCount);
854 }
855
856 float offsetX = 0;
857 switch (paint->getTextAlign()) {
858 case SkPaint::kCenter_Align:
859 offsetX = underlineWidth * 0.5f;
860 break;
861 case SkPaint::kRight_Align:
862 offsetX = underlineWidth;
863 break;
864 default:
865 break;
866 }
867
868 if (underlineWidth > 0.0f) {
869 float textSize = paint->getTextSize();
870 float height = textSize * kStdUnderline_Thickness;
871
872 float left = x - offsetX;
873 float top = 0.0f;
874 float right = left + underlineWidth;
875 float bottom = 0.0f;
876
877 if (flags & SkPaint::kUnderlineText_Flag) {
878 top = y + textSize * kStdUnderline_Offset;
879 bottom = top + height;
880 drawRect(left, top, right, bottom, paint);
881 }
882
883 if (flags & SkPaint::kStrikeThruText_Flag) {
884 top = y + textSize * kStdStrikeThru_Offset;
885 bottom = top + height;
886 drawRect(left, top, right, bottom, paint);
887 }
888 }
889 }
890}
891
Romain Guy026c5e162010-06-28 17:12:22 -0700892void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700893 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -0700894 clearLayerRegions();
895
Romain Guyd27977d2010-07-14 19:18:51 -0700896 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700897 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700898 color |= 0x00ffffff;
899 }
900
901 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700902 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700903 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700904 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
905 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
906 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
907
Romain Guy06f96e22010-07-30 19:18:16 -0700908 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700909
Romain Guy06f96e22010-07-30 19:18:16 -0700910 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700911 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700912 if (mShader) {
913 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700914 }
Romain Guydb1938e2010-08-02 18:50:22 -0700915 if (mColorFilter) {
916 mColorFilter->describe(description, mExtensions);
917 }
Romain Guyd27977d2010-07-14 19:18:51 -0700918
Romain Guy1c740bc2010-09-13 18:00:09 -0700919 // Setup the blending mode
920 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -0700921
Romain Guy06f96e22010-07-30 19:18:16 -0700922 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700923 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -0700924
925 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700926 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -0700927 gMeshStride, &mMeshVertices[0].position[0]);
928
929 // Setup uniforms
930 mModelView.loadTranslate(left, top, 0.0f);
931 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700932 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700933 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700934 } else {
935 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -0700936 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700937 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700938 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700939
Romain Guy06f96e22010-07-30 19:18:16 -0700940 // Setup attributes and uniforms required by the shaders
941 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700942 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700943 }
Romain Guydb1938e2010-08-02 18:50:22 -0700944 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700945 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700946 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700947
Romain Guy06f96e22010-07-30 19:18:16 -0700948 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700949 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700950}
951
Romain Guy82ba8142010-07-09 13:25:56 -0700952void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700953 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700954 int alpha;
955 SkXfermode::Mode mode;
956 getAlphaAndMode(paint, &alpha, &mode);
957
Romain Guy6820ac82010-09-15 18:11:50 -0700958 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
959 texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
960 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -0700961}
962
Romain Guybd6b79b2010-06-26 00:13:53 -0700963void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700964 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
965 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy6820ac82010-09-15 18:11:50 -0700966 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
967 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700968}
969
970void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700971 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -0700972 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guyf607bdc2010-09-10 19:20:06 -0700973 bool swapSrcDst, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -0700974 clearLayerRegions();
975
Romain Guy889f8d12010-07-29 14:37:42 -0700976 ProgramDescription description;
977 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700978 if (mColorFilter) {
979 mColorFilter->describe(description, mExtensions);
980 }
Romain Guy889f8d12010-07-29 14:37:42 -0700981
Romain Guybd6b79b2010-06-26 00:13:53 -0700982 mModelView.loadTranslate(left, top, 0.0f);
983 mModelView.scale(right - left, bottom - top, 1.0f);
984
Romain Guyf607bdc2010-09-10 19:20:06 -0700985 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -0700986
Romain Guyfb8b7632010-08-23 21:05:08 -0700987 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -0700988 if (!ignoreTransform) {
989 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
990 } else {
991 mat4 m;
992 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
993 }
Romain Guybd6b79b2010-06-26 00:13:53 -0700994
Romain Guy889f8d12010-07-29 14:37:42 -0700995 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700996 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700997 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700998
Romain Guya9794742010-07-13 11:37:54 -0700999 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -07001000 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -07001001
Romain Guy889f8d12010-07-29 14:37:42 -07001002 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001003 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001004 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -07001005 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001006 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001007 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001008
Romain Guydb1938e2010-08-02 18:50:22 -07001009 // Color filter
1010 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001011 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001012 }
1013
Romain Guy6820ac82010-09-15 18:11:50 -07001014 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001015 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001016}
1017
Romain Guya5aed0d2010-09-09 14:42:43 -07001018void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001019 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001020 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1021 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001022 if (mode < SkXfermode::kPlus_Mode) {
1023 if (!mCaches.blend) {
1024 glEnable(GL_BLEND);
1025 }
Romain Guy82ba8142010-07-09 13:25:56 -07001026
Romain Guyf607bdc2010-09-10 19:20:06 -07001027 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1028 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001029
Romain Guya5aed0d2010-09-09 14:42:43 -07001030 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1031 glBlendFunc(sourceMode, destMode);
1032 mCaches.lastSrcMode = sourceMode;
1033 mCaches.lastDstMode = destMode;
1034 }
1035 } else {
1036 // These blend modes are not supported by OpenGL directly and have
1037 // to be implemented using shaders. Since the shader will perform
1038 // the blending, turn blending off here
1039 if (mExtensions.hasFramebufferFetch()) {
1040 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001041 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001042 }
1043
1044 if (mCaches.blend) {
1045 glDisable(GL_BLEND);
1046 }
1047 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001048 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001049 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001050 glDisable(GL_BLEND);
1051 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001052 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001053}
1054
Romain Guy889f8d12010-07-29 14:37:42 -07001055bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001056 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001057 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001058 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001059 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001060 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001061 }
Romain Guy6926c722010-07-12 20:20:03 -07001062 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001063}
1064
Romain Guy026c5e162010-06-28 17:12:22 -07001065void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001066 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001067 TextureVertex::setUV(v++, u1, v1);
1068 TextureVertex::setUV(v++, u2, v1);
1069 TextureVertex::setUV(v++, u1, v2);
1070 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001071}
1072
1073void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1074 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001075 if (!mExtensions.hasFramebufferFetch()) {
1076 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1077 if (!isMode) {
1078 // Assume SRC_OVER
1079 *mode = SkXfermode::kSrcOver_Mode;
1080 }
1081 } else {
1082 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001083 }
1084
1085 // Skia draws using the color's alpha channel if < 255
1086 // Otherwise, it uses the paint's alpha
1087 int color = paint->getColor();
1088 *alpha = (color >> 24) & 0xFF;
1089 if (*alpha == 255) {
1090 *alpha = paint->getAlpha();
1091 }
1092 } else {
1093 *mode = SkXfermode::kSrcOver_Mode;
1094 *alpha = 255;
1095 }
Romain Guy026c5e162010-06-28 17:12:22 -07001096}
1097
Romain Guya5aed0d2010-09-09 14:42:43 -07001098SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1099 if (mode == NULL) {
1100 return SkXfermode::kSrcOver_Mode;
1101 }
1102 return mode->fMode;
1103}
1104
Romain Guy889f8d12010-07-29 14:37:42 -07001105void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1106 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001107 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001108 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1109 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1110}
1111
Romain Guy9d5316e2010-06-24 19:30:36 -07001112}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001113}; // namespace android