blob: a65edcd9d7068ab78feda9b1ea9d79434cd8b7fd [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 Guydda57022010-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 Guyf607bdc2010-09-10 19:20:06 -070083static const Blender gBlendsSwap[] = {
84 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
85 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
86 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
87 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
88 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
89 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
90 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
92 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
93 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
94 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
96};
97
Romain Guy889f8d12010-07-29 14:37:42 -070098static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -070099 GL_TEXTURE0,
100 GL_TEXTURE1,
101 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700102};
103
Romain Guyf6a11b82010-06-23 17:47:49 -0700104///////////////////////////////////////////////////////////////////////////////
105// Constructors/destructor
106///////////////////////////////////////////////////////////////////////////////
107
Romain Guyfb8b7632010-08-23 21:05:08 -0700108OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy85bf02f2010-06-22 13:11:24 -0700109 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700110
Romain Guy06f96e22010-07-30 19:18:16 -0700111 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700112 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700113 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700114
Romain Guyac670c02010-07-27 17:39:27 -0700115 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
116
Romain Guyae5575b2010-07-29 18:48:04 -0700117 mFirstSnapshot = new Snapshot;
118
119 GLint maxTextureUnits;
120 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
121 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Romain Guy889f8d12010-07-29 14:37:42 -0700122 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
123 }
Romain Guye4d01122010-06-16 18:44:05 -0700124}
125
Romain Guy85bf02f2010-06-22 13:11:24 -0700126OpenGLRenderer::~OpenGLRenderer() {
127 LOGD("Destroy OpenGLRenderer");
Romain Guye4d01122010-06-16 18:44:05 -0700128}
129
Romain Guyf6a11b82010-06-23 17:47:49 -0700130///////////////////////////////////////////////////////////////////////////////
131// Setup
132///////////////////////////////////////////////////////////////////////////////
133
Romain Guy85bf02f2010-06-22 13:11:24 -0700134void OpenGLRenderer::setViewport(int width, int height) {
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 Guye4d01122010-06-16 18:44:05 -0700140}
141
Romain Guy85bf02f2010-06-22 13:11:24 -0700142void OpenGLRenderer::prepare() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700143 mSnapshot = new Snapshot(mFirstSnapshot,
144 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy8fb95422010-08-17 18:38:51 -0700145 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700146
Romain Guyfb8b7632010-08-23 21:05:08 -0700147 glViewport(0, 0, mWidth, mHeight);
148
Romain Guyd90f23e2010-09-09 11:47:54 -0700149 glDisable(GL_DITHER);
Romain Guy08ae3172010-06-21 19:35:50 -0700150 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700151
Romain Guy08ae3172010-06-21 19:35:50 -0700152 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
153 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700154
Romain Guy08ae3172010-06-21 19:35:50 -0700155 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700156 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700157
Romain Guyb82da652010-07-30 11:36:12 -0700158 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700159}
160
Romain Guyda8532c2010-08-31 11:50:35 -0700161void OpenGLRenderer::acquireContext() {
162 if (mCaches.currentProgram) {
163 if (mCaches.currentProgram->isInUse()) {
164 mCaches.currentProgram->remove();
165 mCaches.currentProgram = NULL;
166 }
167 }
168}
169
170void OpenGLRenderer::releaseContext() {
Romain Guyf607bdc2010-09-10 19:20:06 -0700171 glViewport(0, 0, mWidth, mHeight);
Romain Guyda8532c2010-08-31 11:50:35 -0700172
173 glEnable(GL_SCISSOR_TEST);
174 setScissorFromClip();
175
Romain Guyf607bdc2010-09-10 19:20:06 -0700176 glDisable(GL_DITHER);
177
178 glBindFramebuffer(GL_FRAMEBUFFER, 0);
179
Romain Guyda8532c2010-08-31 11:50:35 -0700180 if (mCaches.blend) {
181 glEnable(GL_BLEND);
182 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
Romain Guyf607bdc2010-09-10 19:20:06 -0700183 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700184 } else {
185 glDisable(GL_BLEND);
186 }
187}
188
Romain Guyf6a11b82010-06-23 17:47:49 -0700189///////////////////////////////////////////////////////////////////////////////
190// State management
191///////////////////////////////////////////////////////////////////////////////
192
Romain Guybb9524b2010-06-22 18:56:38 -0700193int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700194 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700195}
196
197int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700198 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700199}
200
201void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700202 if (mSaveCount > 1) {
203 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700204 }
Romain Guybb9524b2010-06-22 18:56:38 -0700205}
206
207void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700208 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700209
Romain Guy8fb95422010-08-17 18:38:51 -0700210 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700211 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700212 }
Romain Guybb9524b2010-06-22 18:56:38 -0700213}
214
Romain Guy8aef54f2010-09-01 15:13:49 -0700215int OpenGLRenderer::saveSnapshot(int flags) {
216 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700217 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700218}
219
220bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700221 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700222 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guybb9524b2010-06-22 18:56:38 -0700223
Romain Guybd6b79b2010-06-26 00:13:53 -0700224 sp<Snapshot> current = mSnapshot;
225 sp<Snapshot> previous = mSnapshot->previous;
226
Romain Guy8b55f372010-08-18 17:10:07 -0700227 mSaveCount--;
228 mSnapshot = previous;
229
Romain Guybd6b79b2010-06-26 00:13:53 -0700230 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700231 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700232 }
233
Romain Guy2542d192010-08-18 11:47:12 -0700234 if (restoreClip) {
235 setScissorFromClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700236 }
Romain Guy2542d192010-08-18 11:47:12 -0700237
238 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700239}
240
Romain Guyf6a11b82010-06-23 17:47:49 -0700241///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700242// Layers
243///////////////////////////////////////////////////////////////////////////////
244
245int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
246 const SkPaint* p, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700247 int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700248
249 int alpha = 255;
250 SkXfermode::Mode mode;
251
252 if (p) {
253 alpha = p->getAlpha();
Romain Guya5aed0d2010-09-09 14:42:43 -0700254 if (!mExtensions.hasFramebufferFetch()) {
255 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
256 if (!isMode) {
257 // Assume SRC_OVER
258 mode = SkXfermode::kSrcOver_Mode;
259 }
260 } else {
261 mode = getXfermode(p->getXfermode());
Romain Guyd55a8612010-06-28 17:42:46 -0700262 }
263 } else {
264 mode = SkXfermode::kSrcOver_Mode;
265 }
266
Romain Guyf607bdc2010-09-10 19:20:06 -0700267 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700268
269 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700270}
271
272int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
273 int alpha, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700274 if (alpha == 0xff) {
275 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700276 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700277 SkPaint paint;
278 paint.setAlpha(alpha);
279 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700280 }
Romain Guyd55a8612010-06-28 17:42:46 -0700281}
Romain Guybd6b79b2010-06-26 00:13:53 -0700282
Romain Guy1c740bc2010-09-13 18:00:09 -0700283/**
284 * Layers are viewed by Skia are slightly different than layers in image editing
285 * programs (for instance.) When a layer is created, previously created layers
286 * and the frame buffer still receive every drawing command. For instance, if a
287 * layer is created and a shape intersecting the bounds of the layers and the
288 * framebuffer is draw, the shape will be drawn on both (unless the layer was
289 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
290 *
291 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
292 * texture. Unfortunately, this is inefficient as it requires every primitive to
293 * be drawn n + 1 times, where n is the number of active layers. In practice this
294 * means, for every primitive:
295 * - Switch active frame buffer
296 * - Change viewport, clip and projection matrix
297 * - Issue the drawing
298 *
299 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
300 * To avoid this, layers are implemented in a different way here.
301 *
302 * This implementation relies on the frame buffer being at least RGBA 8888. When
303 * a layer is created, only a texture is created, not an FBO. The content of the
304 * frame buffer contained within the layer's bounds is copied into this texture
305 * using glCopyTexImage2D(). The layer's region is then cleared in the frame
306 * buffer and drawing continues as normal. This technique therefore treats the
307 * frame buffer as a scratch buffer for the layers.
308 *
309 * To compose the layers back onto the frame buffer, each layer texture
310 * (containing the original frame buffer data) is drawn as a simple quad over
311 * the frame buffer. The trick is that the quad is set as the composition
312 * destination in the blending equation, and the frame buffer becomes the source
313 * of the composition.
314 *
315 * Drawing layers with an alpha value requires an extra step before composition.
316 * An empty quad is drawn over the layer's region in the frame buffer. This quad
317 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
318 * quad is used to multiply the colors in the frame buffer. This is achieved by
319 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
320 * GL_ZERO, GL_SRC_ALPHA.
321 *
322 * Because glCopyTexImage2D() can be slow, an alternative implementation might
323 * be use to draw a single clipped layer. The implementation described above
324 * is correct in every case.
325 */
Romain Guyd55a8612010-06-28 17:42:46 -0700326bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
327 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700328 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700329 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700330
Romain Guyf607bdc2010-09-10 19:20:06 -0700331 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700332 Rect bounds(left, top, right, bottom);
Romain Guyf607bdc2010-09-10 19:20:06 -0700333 mSnapshot->transform->mapRect(bounds);
Romain Guy8aef54f2010-09-01 15:13:49 -0700334
Romain Guy8411f332010-09-13 17:27:57 -0700335 // Layers only make sense if they are in the framebuffer's bounds
336 bounds.intersect(*mSnapshot->clipRect);
Romain Guy81ab0462010-09-13 17:32:34 -0700337 if (bounds.isEmpty()) return false;
Romain Guyf18fd992010-07-08 11:45:51 -0700338
Romain Guy8411f332010-09-13 17:27:57 -0700339 LayerSize size(bounds.getWidth(), bounds.getHeight());
Romain Guyf607bdc2010-09-10 19:20:06 -0700340 Layer* layer = mCaches.layerCache.get(size);
Romain Guydda57022010-07-06 11:39:32 -0700341 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700342 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700343 }
344
Romain Guydda57022010-07-06 11:39:32 -0700345 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700346 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700347 layer->layer.set(bounds);
Romain Guydda57022010-07-06 11:39:32 -0700348
Romain Guy8fb95422010-08-17 18:38:51 -0700349 // Save the layer in the snapshot
350 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700351 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700352
Romain Guyf607bdc2010-09-10 19:20:06 -0700353 // Copy the framebuffer into the layer
354 glBindTexture(GL_TEXTURE_2D, layer->texture);
355 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
356 bounds.getWidth(), bounds.getHeight(), 0);
357
Romain Guyf607bdc2010-09-10 19:20:06 -0700358 if (flags & SkCanvas::kClipToLayer_SaveFlag) {
Romain Guy86942302010-09-12 13:02:16 -0700359 if (mSnapshot->clipTransformed(bounds)) setScissorFromClip();
Romain Guyf607bdc2010-09-10 19:20:06 -0700360 }
361
Romain Guy86942302010-09-12 13:02:16 -0700362 // Enqueue the buffer coordinates to clear the corresponding region later
363 mLayers.push(new Rect(bounds));
Romain Guyf86ef572010-07-01 11:05:42 -0700364
Romain Guyd55a8612010-06-28 17:42:46 -0700365 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700366}
367
Romain Guy1c740bc2010-09-13 18:00:09 -0700368/**
369 * Read the documentation of createLayer() before doing anything in this method.
370 */
Romain Guy1d83e192010-08-17 11:37:00 -0700371void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
372 if (!current->layer) {
373 LOGE("Attempting to compose a layer that does not exist");
374 return;
375 }
376
Romain Guy1d83e192010-08-17 11:37:00 -0700377 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700378 const Rect& clip = *previous->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700379 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700380
381 Layer* layer = current->layer;
382 const Rect& rect = layer->layer;
383
Romain Guyf607bdc2010-09-10 19:20:06 -0700384 if (layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700385 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700386 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guyf607bdc2010-09-10 19:20:06 -0700387 }
388
389 // Layers are already drawn with a top-left origin, don't flip the texture
Romain Guy8b55f372010-08-18 17:10:07 -0700390 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
391
Romain Guyf607bdc2010-09-10 19:20:06 -0700392 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
393 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
394 &mMeshVertices[0].texture[0], NULL, 0, true, true);
Romain Guy1d83e192010-08-17 11:37:00 -0700395
Romain Guy8b55f372010-08-18 17:10:07 -0700396 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
397
Romain Guy1d83e192010-08-17 11:37:00 -0700398 LayerSize size(rect.getWidth(), rect.getHeight());
399 // Failing to add the layer to the cache should happen only if the
400 // layer is too large
Romain Guyfb8b7632010-08-23 21:05:08 -0700401 if (!mCaches.layerCache.put(size, layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700402 LAYER_LOGD("Deleting layer");
403
Romain Guy1d83e192010-08-17 11:37:00 -0700404 glDeleteTextures(1, &layer->texture);
405
406 delete layer;
407 }
408}
409
Romain Guy86942302010-09-12 13:02:16 -0700410void OpenGLRenderer::clearLayerRegions() {
411 if (mLayers.size() == 0) return;
412
413 for (uint32_t i = 0; i < mLayers.size(); i++) {
414 Rect* bounds = mLayers.itemAt(i);
415
416 // Clear the framebuffer where the layer will draw
417 glScissor(bounds->left, mHeight - bounds->bottom,
418 bounds->getWidth(), bounds->getHeight());
419 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
420 glClear(GL_COLOR_BUFFER_BIT);
421
422 delete bounds;
423 }
424 mLayers.clear();
425
426 // Restore the clip
427 setScissorFromClip();
428}
429
Romain Guybd6b79b2010-06-26 00:13:53 -0700430///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700431// Transforms
432///////////////////////////////////////////////////////////////////////////////
433
434void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700435 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700436}
437
438void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700439 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700440}
441
442void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700443 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700444}
445
446void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700447 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700448}
449
450void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700451 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700452}
453
454void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700455 mat4 m(*matrix);
Romain Guy8aef54f2010-09-01 15:13:49 -0700456 mSnapshot->transform->multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700457}
458
459///////////////////////////////////////////////////////////////////////////////
460// Clipping
461///////////////////////////////////////////////////////////////////////////////
462
Romain Guybb9524b2010-06-22 18:56:38 -0700463void OpenGLRenderer::setScissorFromClip() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700464 const Rect& clip = *mSnapshot->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700465 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700466}
467
468const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700469 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700470}
471
Romain Guyc7d53492010-06-25 13:41:57 -0700472bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy1d83e192010-08-17 11:37:00 -0700473 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700474 mSnapshot->transform->mapRect(r);
475 return !mSnapshot->clipRect->intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700476}
477
Romain Guy079ba2c2010-07-16 14:12:24 -0700478bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
479 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700480 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700481 setScissorFromClip();
482 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700483 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700484}
485
Romain Guyf6a11b82010-06-23 17:47:49 -0700486///////////////////////////////////////////////////////////////////////////////
487// Drawing
488///////////////////////////////////////////////////////////////////////////////
489
Romain Guyc1396e92010-06-30 17:56:19 -0700490void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700491 const float right = left + bitmap->width();
492 const float bottom = top + bitmap->height();
493
494 if (quickReject(left, top, right, bottom)) {
495 return;
496 }
497
Romain Guy61c8c9c2010-08-09 20:48:09 -0700498 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700499 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700500 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700501 const AutoTexture autoCleanup(texture);
502
Romain Guy6926c722010-07-12 20:20:03 -0700503 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700504}
505
Romain Guyf86ef572010-07-01 11:05:42 -0700506void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
507 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
508 const mat4 transform(*matrix);
509 transform.mapRect(r);
510
Romain Guy6926c722010-07-12 20:20:03 -0700511 if (quickReject(r.left, r.top, r.right, r.bottom)) {
512 return;
513 }
514
Romain Guy61c8c9c2010-08-09 20:48:09 -0700515 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700516 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700517 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700518 const AutoTexture autoCleanup(texture);
519
Romain Guy82ba8142010-07-09 13:25:56 -0700520 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700521}
522
Romain Guy8ba548f2010-06-30 19:21:21 -0700523void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
524 float srcLeft, float srcTop, float srcRight, float srcBottom,
525 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700526 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700527 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
528 return;
529 }
530
Romain Guy61c8c9c2010-08-09 20:48:09 -0700531 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700532 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700533 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700534 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700535
Romain Guy8ba548f2010-06-30 19:21:21 -0700536 const float width = texture->width;
537 const float height = texture->height;
538
539 const float u1 = srcLeft / width;
540 const float v1 = srcTop / height;
541 const float u2 = srcRight / width;
542 const float v2 = srcBottom / height;
543
544 resetDrawTextureTexCoords(u1, v1, u2, v2);
545
Romain Guy82ba8142010-07-09 13:25:56 -0700546 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700547
548 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
549}
550
Romain Guydeba7852010-07-07 17:54:48 -0700551void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
552 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700553 if (quickReject(left, top, right, bottom)) {
554 return;
555 }
556
Romain Guy61c8c9c2010-08-09 20:48:09 -0700557 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700558 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700559 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700560 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700561
562 int alpha;
563 SkXfermode::Mode mode;
564 getAlphaAndMode(paint, &alpha, &mode);
565
Romain Guyfb8b7632010-08-23 21:05:08 -0700566 Patch* mesh = mCaches.patchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700567 mesh->updateVertices(bitmap, left, top, right, bottom,
568 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guy8411f332010-09-13 17:27:57 -0700569 mesh->dump();
Romain Guyf7f93552010-07-08 19:17:03 -0700570
571 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
572 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700573 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
574 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700575 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700576}
577
Romain Guy85bf02f2010-06-22 13:11:24 -0700578void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700579 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700580 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700581}
Romain Guy9d5316e2010-06-24 19:30:36 -0700582
Romain Guybd6b79b2010-06-26 00:13:53 -0700583void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700584 if (quickReject(left, top, right, bottom)) {
585 return;
586 }
587
Romain Guy026c5e162010-06-28 17:12:22 -0700588 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700589 if (!mExtensions.hasFramebufferFetch()) {
590 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
591 if (!isMode) {
592 // Assume SRC_OVER
593 mode = SkXfermode::kSrcOver_Mode;
594 }
595 } else {
596 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700597 }
598
599 // Skia draws using the color's alpha channel if < 255
600 // Otherwise, it uses the paint's alpha
601 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700602 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700603 color |= p->getAlpha() << 24;
604 }
605
606 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700607}
Romain Guy9d5316e2010-06-24 19:30:36 -0700608
Romain Guye8e62a42010-07-23 18:55:21 -0700609void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
610 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700611 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700612 return;
613 }
Romain Guyf607bdc2010-09-10 19:20:06 -0700614 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700615
Romain Guya674ab72010-08-10 17:26:42 -0700616 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700617 switch (paint->getTextAlign()) {
618 case SkPaint::kCenter_Align:
619 length = paint->measureText(text, bytesCount);
620 x -= length / 2.0f;
621 break;
622 case SkPaint::kRight_Align:
623 length = paint->measureText(text, bytesCount);
624 x -= length;
625 break;
626 default:
627 break;
628 }
629
Romain Guy694b5192010-07-21 21:33:20 -0700630 int alpha;
631 SkXfermode::Mode mode;
632 getAlphaAndMode(paint, &alpha, &mode);
633
Romain Guy2542d192010-08-18 11:47:12 -0700634 uint32_t color = paint->getColor();
635 const GLfloat a = alpha / 255.0f;
636 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
637 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
638 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
639
Romain Guyb45c0c92010-08-26 20:35:23 -0700640 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
641 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700642 paint->getTextSize());
Romain Guy1e45aae2010-08-13 19:39:53 -0700643 if (mHasShadow) {
644 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700645 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700646 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700647 count, mShadowRadius);
648 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700649
Romain Guy2542d192010-08-18 11:47:12 -0700650 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700651
652 // Draw the mesh
653 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700654 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700655 }
656
Romain Guy06f96e22010-07-30 19:18:16 -0700657 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700658 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700659
Romain Guyb45c0c92010-08-26 20:35:23 -0700660 setupTextureAlpha8(fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
Romain Guy0a417492010-08-16 20:26:20 -0700661 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700662
Romain Guy09147fb2010-07-22 13:08:20 -0700663 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy86942302010-09-12 13:02:16 -0700664 clearLayerRegions();
Romain Guyb45c0c92010-08-26 20:35:23 -0700665 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700666
667 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700668 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700669
Romain Guy0a417492010-08-16 20:26:20 -0700670 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -0700671}
672
Romain Guy7fbcc042010-08-04 15:40:07 -0700673void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
674 GLuint textureUnit = 0;
675 glActiveTexture(gTextureUnits[textureUnit]);
676
Romain Guyfb8b7632010-08-23 21:05:08 -0700677 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700678 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700679 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700680
Romain Guy8b55f372010-08-18 17:10:07 -0700681 const float x = texture->left - texture->offset;
682 const float y = texture->top - texture->offset;
683
684 if (quickReject(x, y, x + texture->width, y + texture->height)) {
685 return;
686 }
687
Romain Guy7fbcc042010-08-04 15:40:07 -0700688 int alpha;
689 SkXfermode::Mode mode;
690 getAlphaAndMode(paint, &alpha, &mode);
691
692 uint32_t color = paint->getColor();
693 const GLfloat a = alpha / 255.0f;
694 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
695 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
696 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
697
Romain Guy0a417492010-08-16 20:26:20 -0700698 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
699
Romain Guy86942302010-09-12 13:02:16 -0700700 clearLayerRegions();
701
Romain Guy0a417492010-08-16 20:26:20 -0700702 // Draw the mesh
703 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700704 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700705}
706
Romain Guy6926c722010-07-12 20:20:03 -0700707///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700708// Shaders
709///////////////////////////////////////////////////////////////////////////////
710
711void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700712 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700713}
714
Romain Guy06f96e22010-07-30 19:18:16 -0700715void OpenGLRenderer::setupShader(SkiaShader* shader) {
716 mShader = shader;
717 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700718 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700719 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700720}
721
Romain Guyd27977d2010-07-14 19:18:51 -0700722///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700723// Color filters
724///////////////////////////////////////////////////////////////////////////////
725
726void OpenGLRenderer::resetColorFilter() {
727 mColorFilter = NULL;
728}
729
730void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
731 mColorFilter = filter;
732}
733
734///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700735// Drop shadow
736///////////////////////////////////////////////////////////////////////////////
737
738void OpenGLRenderer::resetShadow() {
739 mHasShadow = false;
740}
741
742void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
743 mHasShadow = true;
744 mShadowRadius = radius;
745 mShadowDx = dx;
746 mShadowDy = dy;
747 mShadowColor = color;
748}
749
750///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700751// Drawing implementation
752///////////////////////////////////////////////////////////////////////////////
753
Romain Guy0a417492010-08-16 20:26:20 -0700754void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700755 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700756 const float sx = x - texture->left + mShadowDx;
757 const float sy = y - texture->top + mShadowDy;
758
Romain Guy2542d192010-08-18 11:47:12 -0700759 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
760 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700761 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
762 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
763 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
764
765 GLuint textureUnit = 0;
766 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
767}
768
769void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
770 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
771 bool transforms, bool applyFilters) {
772 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
773 x, y, r, g, b, a, mode, transforms, applyFilters);
774}
775
776void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
777 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
778 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
779 // Describe the required shaders
780 ProgramDescription description;
781 description.hasTexture = true;
782 description.hasAlpha8Texture = true;
783
784 if (applyFilters) {
785 if (mShader) {
786 mShader->describe(description, mExtensions);
787 }
788 if (mColorFilter) {
789 mColorFilter->describe(description, mExtensions);
790 }
791 }
792
Romain Guya5aed0d2010-09-09 14:42:43 -0700793 // Setup the blending mode
794 chooseBlending(true, mode, description);
795
Romain Guy0a417492010-08-16 20:26:20 -0700796 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700797 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700798
Romain Guy0a417492010-08-16 20:26:20 -0700799 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -0700800 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700801
Romain Guyfb8b7632010-08-23 21:05:08 -0700802 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -0700803 glEnableVertexAttribArray(texCoordsSlot);
804
805 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700806 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy0a417492010-08-16 20:26:20 -0700807 gMeshStride, &mMeshVertices[0].position[0]);
808 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
809 gMeshStride, &mMeshVertices[0].texture[0]);
810
811 // Setup uniforms
812 if (transforms) {
813 mModelView.loadTranslate(x, y, 0.0f);
814 mModelView.scale(width, height, 1.0f);
815 } else {
816 mModelView.loadIdentity();
817 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700818 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyfb8b7632010-08-23 21:05:08 -0700819 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -0700820
821 textureUnit++;
822 if (applyFilters) {
823 // Setup attributes and uniforms required by the shaders
824 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700825 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700826 }
827 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700828 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -0700829 }
830 }
831}
832
Romain Guyf607bdc2010-09-10 19:20:06 -0700833// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -0700834#define kStdStrikeThru_Offset (-6.0f / 21.0f)
835#define kStdUnderline_Offset (1.0f / 9.0f)
836#define kStdUnderline_Thickness (1.0f / 18.0f)
837
838void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
839 float x, float y, SkPaint* paint) {
840 // Handle underline and strike-through
841 uint32_t flags = paint->getFlags();
842 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
843 float underlineWidth = length;
844 // If length is > 0.0f, we already measured the text for the text alignment
845 if (length <= 0.0f) {
846 underlineWidth = paint->measureText(text, bytesCount);
847 }
848
849 float offsetX = 0;
850 switch (paint->getTextAlign()) {
851 case SkPaint::kCenter_Align:
852 offsetX = underlineWidth * 0.5f;
853 break;
854 case SkPaint::kRight_Align:
855 offsetX = underlineWidth;
856 break;
857 default:
858 break;
859 }
860
861 if (underlineWidth > 0.0f) {
862 float textSize = paint->getTextSize();
863 float height = textSize * kStdUnderline_Thickness;
864
865 float left = x - offsetX;
866 float top = 0.0f;
867 float right = left + underlineWidth;
868 float bottom = 0.0f;
869
870 if (flags & SkPaint::kUnderlineText_Flag) {
871 top = y + textSize * kStdUnderline_Offset;
872 bottom = top + height;
873 drawRect(left, top, right, bottom, paint);
874 }
875
876 if (flags & SkPaint::kStrikeThruText_Flag) {
877 top = y + textSize * kStdStrikeThru_Offset;
878 bottom = top + height;
879 drawRect(left, top, right, bottom, paint);
880 }
881 }
882 }
883}
884
Romain Guy026c5e162010-06-28 17:12:22 -0700885void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700886 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -0700887 clearLayerRegions();
888
Romain Guyd27977d2010-07-14 19:18:51 -0700889 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700890 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700891 color |= 0x00ffffff;
892 }
893
894 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700895 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700896 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700897 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
898 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
899 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
900
Romain Guy06f96e22010-07-30 19:18:16 -0700901 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700902
Romain Guy06f96e22010-07-30 19:18:16 -0700903 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700904 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700905 if (mShader) {
906 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700907 }
Romain Guydb1938e2010-08-02 18:50:22 -0700908 if (mColorFilter) {
909 mColorFilter->describe(description, mExtensions);
910 }
Romain Guyd27977d2010-07-14 19:18:51 -0700911
Romain Guy1c740bc2010-09-13 18:00:09 -0700912 // Setup the blending mode
913 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -0700914
Romain Guy06f96e22010-07-30 19:18:16 -0700915 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700916 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -0700917
918 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700919 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -0700920 gMeshStride, &mMeshVertices[0].position[0]);
921
922 // Setup uniforms
923 mModelView.loadTranslate(left, top, 0.0f);
924 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700925 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700926 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700927 } else {
928 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -0700929 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700930 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700931 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700932
Romain Guy06f96e22010-07-30 19:18:16 -0700933 // Setup attributes and uniforms required by the shaders
934 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700935 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700936 }
Romain Guydb1938e2010-08-02 18:50:22 -0700937 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700938 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700939 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700940
Romain Guy06f96e22010-07-30 19:18:16 -0700941 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700942 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700943}
944
Romain Guy82ba8142010-07-09 13:25:56 -0700945void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700946 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700947 int alpha;
948 SkXfermode::Mode mode;
949 getAlphaAndMode(paint, &alpha, &mode);
950
Romain Guya9794742010-07-13 11:37:54 -0700951 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700952 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700953}
954
Romain Guybd6b79b2010-06-26 00:13:53 -0700955void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700956 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
957 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700958 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700959}
960
961void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700962 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf607bdc2010-09-10 19:20:06 -0700963 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount,
964 bool swapSrcDst, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -0700965 clearLayerRegions();
966
Romain Guy889f8d12010-07-29 14:37:42 -0700967 ProgramDescription description;
968 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700969 if (mColorFilter) {
970 mColorFilter->describe(description, mExtensions);
971 }
Romain Guy889f8d12010-07-29 14:37:42 -0700972
Romain Guybd6b79b2010-06-26 00:13:53 -0700973 mModelView.loadTranslate(left, top, 0.0f);
974 mModelView.scale(right - left, bottom - top, 1.0f);
975
Romain Guyf607bdc2010-09-10 19:20:06 -0700976 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -0700977
Romain Guyfb8b7632010-08-23 21:05:08 -0700978 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -0700979 if (!ignoreTransform) {
980 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
981 } else {
982 mat4 m;
983 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
984 }
Romain Guybd6b79b2010-06-26 00:13:53 -0700985
Romain Guy889f8d12010-07-29 14:37:42 -0700986 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700987 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700988 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700989
Romain Guya9794742010-07-13 11:37:54 -0700990 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -0700991 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700992
Romain Guy889f8d12010-07-29 14:37:42 -0700993 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -0700994 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -0700995 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -0700996 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700997 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700998 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700999
Romain Guydb1938e2010-08-02 18:50:22 -07001000 // Color filter
1001 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001002 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001003 }
1004
Romain Guyf7f93552010-07-08 19:17:03 -07001005 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -07001006 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001007 } else {
1008 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
1009 }
Romain Guy889f8d12010-07-29 14:37:42 -07001010 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001011}
1012
Romain Guya5aed0d2010-09-09 14:42:43 -07001013void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001014 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001015 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1016 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001017 if (mode < SkXfermode::kPlus_Mode) {
1018 if (!mCaches.blend) {
1019 glEnable(GL_BLEND);
1020 }
Romain Guy82ba8142010-07-09 13:25:56 -07001021
Romain Guyf607bdc2010-09-10 19:20:06 -07001022 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1023 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001024
Romain Guya5aed0d2010-09-09 14:42:43 -07001025 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1026 glBlendFunc(sourceMode, destMode);
1027 mCaches.lastSrcMode = sourceMode;
1028 mCaches.lastDstMode = destMode;
1029 }
1030 } else {
1031 // These blend modes are not supported by OpenGL directly and have
1032 // to be implemented using shaders. Since the shader will perform
1033 // the blending, turn blending off here
1034 if (mExtensions.hasFramebufferFetch()) {
1035 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001036 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001037 }
1038
1039 if (mCaches.blend) {
1040 glDisable(GL_BLEND);
1041 }
1042 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001043 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001044 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001045 glDisable(GL_BLEND);
1046 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001047 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001048}
1049
Romain Guy889f8d12010-07-29 14:37:42 -07001050bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001051 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001052 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001053 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001054 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001055 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001056 }
Romain Guy6926c722010-07-12 20:20:03 -07001057 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001058}
1059
Romain Guy026c5e162010-06-28 17:12:22 -07001060void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001061 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001062 TextureVertex::setUV(v++, u1, v1);
1063 TextureVertex::setUV(v++, u2, v1);
1064 TextureVertex::setUV(v++, u1, v2);
1065 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001066}
1067
1068void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1069 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001070 if (!mExtensions.hasFramebufferFetch()) {
1071 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1072 if (!isMode) {
1073 // Assume SRC_OVER
1074 *mode = SkXfermode::kSrcOver_Mode;
1075 }
1076 } else {
1077 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001078 }
1079
1080 // Skia draws using the color's alpha channel if < 255
1081 // Otherwise, it uses the paint's alpha
1082 int color = paint->getColor();
1083 *alpha = (color >> 24) & 0xFF;
1084 if (*alpha == 255) {
1085 *alpha = paint->getAlpha();
1086 }
1087 } else {
1088 *mode = SkXfermode::kSrcOver_Mode;
1089 *alpha = 255;
1090 }
Romain Guy026c5e162010-06-28 17:12:22 -07001091}
1092
Romain Guya5aed0d2010-09-09 14:42:43 -07001093SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1094 if (mode == NULL) {
1095 return SkXfermode::kSrcOver_Mode;
1096 }
1097 return mode->fMode;
1098}
1099
Romain Guy889f8d12010-07-29 14:37:42 -07001100void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1101 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001102 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1104 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1105}
1106
Romain Guy9d5316e2010-06-24 19:30:36 -07001107}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001108}; // namespace android