blob: e0320850fca47d7fc73449ebd79bd94ea14b9516 [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
Romain Guy759ea802010-09-16 20:49:46 -070042#define RAD_TO_DEG (180.0f / 3.14159265f)
43#define MIN_ANGLE 0.001f
44
Romain Guy9d5316e2010-06-24 19:30:36 -070045///////////////////////////////////////////////////////////////////////////////
46// Globals
47///////////////////////////////////////////////////////////////////////////////
48
Romain Guy026c5e162010-06-28 17:12:22 -070049// This array is never used directly but used as a memcpy source in the
50// OpenGLRenderer constructor
Romain Guyac670c02010-07-27 17:39:27 -070051static const TextureVertex gMeshVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070052 FV(0.0f, 0.0f, 0.0f, 0.0f),
53 FV(1.0f, 0.0f, 1.0f, 0.0f),
54 FV(0.0f, 1.0f, 0.0f, 1.0f),
55 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070056};
Romain Guyac670c02010-07-27 17:39:27 -070057static const GLsizei gMeshStride = sizeof(TextureVertex);
58static const GLsizei gMeshCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070059
Romain Guy889f8d12010-07-29 14:37:42 -070060/**
61 * Structure mapping Skia xfermodes to OpenGL blending factors.
62 */
63struct Blender {
64 SkXfermode::Mode mode;
65 GLenum src;
66 GLenum dst;
67}; // struct Blender
68
Romain Guy026c5e162010-06-28 17:12:22 -070069// In this array, the index of each Blender equals the value of the first
70// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
71static const Blender gBlends[] = {
72 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
73 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
74 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
75 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
76 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
77 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
78 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
79 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
80 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
82 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
83 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
84};
Romain Guye4d01122010-06-16 18:44:05 -070085
Romain Guy87a76572010-09-13 18:11:21 -070086// This array contains the swapped version of each SkXfermode. For instance
87// this array's SrcOver blending mode is actually DstOver. You can refer to
88// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070089static const Blender gBlendsSwap[] = {
90 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
91 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
92 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
93 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
94 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
96 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
97 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
98 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
99 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
100 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
101 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
102};
103
Romain Guy889f8d12010-07-29 14:37:42 -0700104static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -0700105 GL_TEXTURE0,
106 GL_TEXTURE1,
107 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700108};
109
Romain Guyf6a11b82010-06-23 17:47:49 -0700110///////////////////////////////////////////////////////////////////////////////
111// Constructors/destructor
112///////////////////////////////////////////////////////////////////////////////
113
Romain Guyfb8b7632010-08-23 21:05:08 -0700114OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy85bf02f2010-06-22 13:11:24 -0700115 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700116
Romain Guy06f96e22010-07-30 19:18:16 -0700117 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700118 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700119 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700120
Romain Guyac670c02010-07-27 17:39:27 -0700121 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
122
Romain Guyae5575b2010-07-29 18:48:04 -0700123 mFirstSnapshot = new Snapshot;
124
125 GLint maxTextureUnits;
126 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
127 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Romain Guy889f8d12010-07-29 14:37:42 -0700128 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
129 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700130
131 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Romain Guye4d01122010-06-16 18:44:05 -0700132}
133
Romain Guy85bf02f2010-06-22 13:11:24 -0700134OpenGLRenderer::~OpenGLRenderer() {
135 LOGD("Destroy OpenGLRenderer");
Romain Guy29d89972010-09-22 16:10:57 -0700136 // The context has already been destroyed at this point, do not call
137 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700138}
139
Romain Guyf6a11b82010-06-23 17:47:49 -0700140///////////////////////////////////////////////////////////////////////////////
141// Setup
142///////////////////////////////////////////////////////////////////////////////
143
Romain Guy85bf02f2010-06-22 13:11:24 -0700144void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700145 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700146 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700147
148 mWidth = width;
149 mHeight = height;
Romain Guye4d01122010-06-16 18:44:05 -0700150}
151
Romain Guy85bf02f2010-06-22 13:11:24 -0700152void OpenGLRenderer::prepare() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700153 mSnapshot = new Snapshot(mFirstSnapshot,
154 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy8fb95422010-08-17 18:38:51 -0700155 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700156
Romain Guyfb8b7632010-08-23 21:05:08 -0700157 glViewport(0, 0, mWidth, mHeight);
158
Romain Guyd90f23e2010-09-09 11:47:54 -0700159 glDisable(GL_DITHER);
Romain Guy08ae3172010-06-21 19:35:50 -0700160 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700161
Romain Guy08ae3172010-06-21 19:35:50 -0700162 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
163 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700164
Romain Guy08ae3172010-06-21 19:35:50 -0700165 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700166 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700167
Romain Guyb82da652010-07-30 11:36:12 -0700168 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700169}
170
Romain Guyb025b9c2010-09-16 14:16:48 -0700171void OpenGLRenderer::finish() {
172#if DEBUG_OPENGL
173 GLenum status = GL_NO_ERROR;
174 while ((status = glGetError()) != GL_NO_ERROR) {
175 LOGD("GL error from OpenGLRenderer: 0x%x", status);
176 }
177#endif
178}
179
Romain Guyda8532c2010-08-31 11:50:35 -0700180void OpenGLRenderer::acquireContext() {
181 if (mCaches.currentProgram) {
182 if (mCaches.currentProgram->isInUse()) {
183 mCaches.currentProgram->remove();
184 mCaches.currentProgram = NULL;
185 }
186 }
187}
188
189void OpenGLRenderer::releaseContext() {
Romain Guyf607bdc2010-09-10 19:20:06 -0700190 glViewport(0, 0, mWidth, mHeight);
Romain Guyda8532c2010-08-31 11:50:35 -0700191
192 glEnable(GL_SCISSOR_TEST);
193 setScissorFromClip();
194
Romain Guyf607bdc2010-09-10 19:20:06 -0700195 glDisable(GL_DITHER);
196
197 glBindFramebuffer(GL_FRAMEBUFFER, 0);
198
Romain Guyda8532c2010-08-31 11:50:35 -0700199 if (mCaches.blend) {
200 glEnable(GL_BLEND);
201 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
Romain Guyf607bdc2010-09-10 19:20:06 -0700202 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700203 } else {
204 glDisable(GL_BLEND);
205 }
206}
207
Romain Guyf6a11b82010-06-23 17:47:49 -0700208///////////////////////////////////////////////////////////////////////////////
209// State management
210///////////////////////////////////////////////////////////////////////////////
211
Romain Guybb9524b2010-06-22 18:56:38 -0700212int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700213 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700214}
215
216int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700217 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700218}
219
220void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700221 if (mSaveCount > 1) {
222 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700223 }
Romain Guybb9524b2010-06-22 18:56:38 -0700224}
225
226void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700227 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700228
Romain Guy8fb95422010-08-17 18:38:51 -0700229 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700230 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700231 }
Romain Guybb9524b2010-06-22 18:56:38 -0700232}
233
Romain Guy8aef54f2010-09-01 15:13:49 -0700234int OpenGLRenderer::saveSnapshot(int flags) {
235 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700236 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700237}
238
239bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700240 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700241 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guybb9524b2010-06-22 18:56:38 -0700242
Romain Guybd6b79b2010-06-26 00:13:53 -0700243 sp<Snapshot> current = mSnapshot;
244 sp<Snapshot> previous = mSnapshot->previous;
245
Romain Guy8b55f372010-08-18 17:10:07 -0700246 mSaveCount--;
247 mSnapshot = previous;
248
Romain Guybd6b79b2010-06-26 00:13:53 -0700249 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700250 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700251 }
252
Romain Guy2542d192010-08-18 11:47:12 -0700253 if (restoreClip) {
254 setScissorFromClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700255 }
Romain Guy2542d192010-08-18 11:47:12 -0700256
257 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700258}
259
Romain Guyf6a11b82010-06-23 17:47:49 -0700260///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700261// Layers
262///////////////////////////////////////////////////////////////////////////////
263
264int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
265 const SkPaint* p, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700266 int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700267
268 int alpha = 255;
269 SkXfermode::Mode mode;
270
271 if (p) {
272 alpha = p->getAlpha();
Romain Guya5aed0d2010-09-09 14:42:43 -0700273 if (!mExtensions.hasFramebufferFetch()) {
274 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
275 if (!isMode) {
276 // Assume SRC_OVER
277 mode = SkXfermode::kSrcOver_Mode;
278 }
279 } else {
280 mode = getXfermode(p->getXfermode());
Romain Guyd55a8612010-06-28 17:42:46 -0700281 }
282 } else {
283 mode = SkXfermode::kSrcOver_Mode;
284 }
285
Romain Guyf607bdc2010-09-10 19:20:06 -0700286 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700287
288 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700289}
290
291int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
292 int alpha, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700293 if (alpha == 0xff) {
294 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700295 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700296 SkPaint paint;
297 paint.setAlpha(alpha);
298 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700299 }
Romain Guyd55a8612010-06-28 17:42:46 -0700300}
Romain Guybd6b79b2010-06-26 00:13:53 -0700301
Romain Guy1c740bc2010-09-13 18:00:09 -0700302/**
303 * Layers are viewed by Skia are slightly different than layers in image editing
304 * programs (for instance.) When a layer is created, previously created layers
305 * and the frame buffer still receive every drawing command. For instance, if a
306 * layer is created and a shape intersecting the bounds of the layers and the
307 * framebuffer is draw, the shape will be drawn on both (unless the layer was
308 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
309 *
310 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
311 * texture. Unfortunately, this is inefficient as it requires every primitive to
312 * be drawn n + 1 times, where n is the number of active layers. In practice this
313 * means, for every primitive:
314 * - Switch active frame buffer
315 * - Change viewport, clip and projection matrix
316 * - Issue the drawing
317 *
318 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
319 * To avoid this, layers are implemented in a different way here.
320 *
321 * This implementation relies on the frame buffer being at least RGBA 8888. When
322 * a layer is created, only a texture is created, not an FBO. The content of the
323 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700324 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700325 * buffer and drawing continues as normal. This technique therefore treats the
326 * frame buffer as a scratch buffer for the layers.
327 *
328 * To compose the layers back onto the frame buffer, each layer texture
329 * (containing the original frame buffer data) is drawn as a simple quad over
330 * the frame buffer. The trick is that the quad is set as the composition
331 * destination in the blending equation, and the frame buffer becomes the source
332 * of the composition.
333 *
334 * Drawing layers with an alpha value requires an extra step before composition.
335 * An empty quad is drawn over the layer's region in the frame buffer. This quad
336 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
337 * quad is used to multiply the colors in the frame buffer. This is achieved by
338 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
339 * GL_ZERO, GL_SRC_ALPHA.
340 *
341 * Because glCopyTexImage2D() can be slow, an alternative implementation might
342 * be use to draw a single clipped layer. The implementation described above
343 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700344 *
345 * (1) The frame buffer is actually not cleared right away. To allow the GPU
346 * to potentially optimize series of calls to glCopyTexImage2D, the frame
347 * buffer is left untouched until the first drawing operation. Only when
348 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700349 */
Romain Guyd55a8612010-06-28 17:42:46 -0700350bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
351 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700352 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700353 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700354
Romain Guyf607bdc2010-09-10 19:20:06 -0700355 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700356 Rect bounds(left, top, right, bottom);
Romain Guyf607bdc2010-09-10 19:20:06 -0700357 mSnapshot->transform->mapRect(bounds);
Romain Guy8aef54f2010-09-01 15:13:49 -0700358
Romain Guy8411f332010-09-13 17:27:57 -0700359 // Layers only make sense if they are in the framebuffer's bounds
360 bounds.intersect(*mSnapshot->clipRect);
Romain Guybf434112010-09-16 14:40:17 -0700361 bounds.snapToPixelBoundaries();
362
Romain Guyb025b9c2010-09-16 14:16:48 -0700363 if (bounds.isEmpty() || bounds.getWidth() > mMaxTextureSize ||
364 bounds.getHeight() > mMaxTextureSize) {
365 return false;
366 }
Romain Guyf18fd992010-07-08 11:45:51 -0700367
Romain Guy8411f332010-09-13 17:27:57 -0700368 LayerSize size(bounds.getWidth(), bounds.getHeight());
Romain Guyf607bdc2010-09-10 19:20:06 -0700369 Layer* layer = mCaches.layerCache.get(size);
Romain Guydda570202010-07-06 11:39:32 -0700370 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700371 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700372 }
373
Romain Guydda570202010-07-06 11:39:32 -0700374 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700375 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700376 layer->layer.set(bounds);
Romain Guydda570202010-07-06 11:39:32 -0700377
Romain Guy8fb95422010-08-17 18:38:51 -0700378 // Save the layer in the snapshot
379 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700380 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700381
Romain Guyf607bdc2010-09-10 19:20:06 -0700382 // Copy the framebuffer into the layer
383 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy38c85b92010-09-22 22:48:20 -0700384
385 if (layer->empty) {
386 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
387 bounds.getWidth(), bounds.getHeight(), 0);
388 layer->empty = false;
389 } else {
390 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, mHeight - bounds.bottom,
391 bounds.getWidth(), bounds.getHeight());
392 }
Romain Guyf607bdc2010-09-10 19:20:06 -0700393
Romain Guyf607bdc2010-09-10 19:20:06 -0700394 if (flags & SkCanvas::kClipToLayer_SaveFlag) {
Romain Guy86942302010-09-12 13:02:16 -0700395 if (mSnapshot->clipTransformed(bounds)) setScissorFromClip();
Romain Guyf607bdc2010-09-10 19:20:06 -0700396 }
397
Romain Guy86942302010-09-12 13:02:16 -0700398 // Enqueue the buffer coordinates to clear the corresponding region later
399 mLayers.push(new Rect(bounds));
Romain Guyf86ef572010-07-01 11:05:42 -0700400
Romain Guyd55a8612010-06-28 17:42:46 -0700401 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700402}
403
Romain Guy1c740bc2010-09-13 18:00:09 -0700404/**
405 * Read the documentation of createLayer() before doing anything in this method.
406 */
Romain Guy1d83e192010-08-17 11:37:00 -0700407void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
408 if (!current->layer) {
409 LOGE("Attempting to compose a layer that does not exist");
410 return;
411 }
412
Romain Guy1d83e192010-08-17 11:37:00 -0700413 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700414 const Rect& clip = *previous->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700415 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700416
417 Layer* layer = current->layer;
418 const Rect& rect = layer->layer;
419
Romain Guyf607bdc2010-09-10 19:20:06 -0700420 if (layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700421 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700422 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guyf607bdc2010-09-10 19:20:06 -0700423 }
424
425 // Layers are already drawn with a top-left origin, don't flip the texture
Romain Guy8b55f372010-08-18 17:10:07 -0700426 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
427
Romain Guyf607bdc2010-09-10 19:20:06 -0700428 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
429 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
Romain Guy6820ac82010-09-15 18:11:50 -0700430 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, true, true);
Romain Guy1d83e192010-08-17 11:37:00 -0700431
Romain Guy8b55f372010-08-18 17:10:07 -0700432 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
433
Romain Guy1d83e192010-08-17 11:37:00 -0700434 LayerSize size(rect.getWidth(), rect.getHeight());
435 // Failing to add the layer to the cache should happen only if the
436 // layer is too large
Romain Guyfb8b7632010-08-23 21:05:08 -0700437 if (!mCaches.layerCache.put(size, layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700438 LAYER_LOGD("Deleting layer");
439
Romain Guy1d83e192010-08-17 11:37:00 -0700440 glDeleteTextures(1, &layer->texture);
441
442 delete layer;
443 }
444}
445
Romain Guy86942302010-09-12 13:02:16 -0700446void OpenGLRenderer::clearLayerRegions() {
447 if (mLayers.size() == 0) return;
448
449 for (uint32_t i = 0; i < mLayers.size(); i++) {
450 Rect* bounds = mLayers.itemAt(i);
451
452 // Clear the framebuffer where the layer will draw
453 glScissor(bounds->left, mHeight - bounds->bottom,
454 bounds->getWidth(), bounds->getHeight());
455 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
456 glClear(GL_COLOR_BUFFER_BIT);
457
458 delete bounds;
459 }
460 mLayers.clear();
461
462 // Restore the clip
463 setScissorFromClip();
464}
465
Romain Guybd6b79b2010-06-26 00:13:53 -0700466///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700467// Transforms
468///////////////////////////////////////////////////////////////////////////////
469
470void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700471 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700472}
473
474void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700475 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700476}
477
478void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700479 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700480}
481
482void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700483 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700484}
485
486void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700487 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700488}
489
490void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700491 mat4 m(*matrix);
Romain Guy8aef54f2010-09-01 15:13:49 -0700492 mSnapshot->transform->multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700493}
494
495///////////////////////////////////////////////////////////////////////////////
496// Clipping
497///////////////////////////////////////////////////////////////////////////////
498
Romain Guybb9524b2010-06-22 18:56:38 -0700499void OpenGLRenderer::setScissorFromClip() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700500 const Rect& clip = *mSnapshot->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700501 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700502}
503
504const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700505 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700506}
507
Romain Guyc7d53492010-06-25 13:41:57 -0700508bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy1d83e192010-08-17 11:37:00 -0700509 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700510 mSnapshot->transform->mapRect(r);
511 return !mSnapshot->clipRect->intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700512}
513
Romain Guy079ba2c2010-07-16 14:12:24 -0700514bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
515 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700516 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700517 setScissorFromClip();
518 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700519 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700520}
521
Romain Guyf6a11b82010-06-23 17:47:49 -0700522///////////////////////////////////////////////////////////////////////////////
523// Drawing
524///////////////////////////////////////////////////////////////////////////////
525
Romain Guyc1396e92010-06-30 17:56:19 -0700526void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700527 const float right = left + bitmap->width();
528 const float bottom = top + bitmap->height();
529
530 if (quickReject(left, top, right, bottom)) {
531 return;
532 }
533
Romain Guy61c8c9c2010-08-09 20:48:09 -0700534 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700535 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700536 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700537 const AutoTexture autoCleanup(texture);
538
Romain Guy6926c722010-07-12 20:20:03 -0700539 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700540}
541
Romain Guyf86ef572010-07-01 11:05:42 -0700542void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
543 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
544 const mat4 transform(*matrix);
545 transform.mapRect(r);
546
Romain Guy6926c722010-07-12 20:20:03 -0700547 if (quickReject(r.left, r.top, r.right, r.bottom)) {
548 return;
549 }
550
Romain Guy61c8c9c2010-08-09 20:48:09 -0700551 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700552 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700553 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700554 const AutoTexture autoCleanup(texture);
555
Romain Guy82ba8142010-07-09 13:25:56 -0700556 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700557}
558
Romain Guy8ba548f2010-06-30 19:21:21 -0700559void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
560 float srcLeft, float srcTop, float srcRight, float srcBottom,
561 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700562 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700563 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
564 return;
565 }
566
Romain Guy61c8c9c2010-08-09 20:48:09 -0700567 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700568 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700569 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700570 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700571
Romain Guy8ba548f2010-06-30 19:21:21 -0700572 const float width = texture->width;
573 const float height = texture->height;
574
575 const float u1 = srcLeft / width;
576 const float v1 = srcTop / height;
577 const float u2 = srcRight / width;
578 const float v2 = srcBottom / height;
579
580 resetDrawTextureTexCoords(u1, v1, u2, v2);
581
Romain Guy82ba8142010-07-09 13:25:56 -0700582 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700583
584 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
585}
586
Romain Guydeba7852010-07-07 17:54:48 -0700587void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
588 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700589 if (quickReject(left, top, right, bottom)) {
590 return;
591 }
592
Romain Guy61c8c9c2010-08-09 20:48:09 -0700593 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700594 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700595 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700596 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700597
598 int alpha;
599 SkXfermode::Mode mode;
600 getAlphaAndMode(paint, &alpha, &mode);
601
Romain Guyfb8b7632010-08-23 21:05:08 -0700602 Patch* mesh = mCaches.patchCache.get(patch);
Romain Guy759ea802010-09-16 20:49:46 -0700603 mesh->updateVertices(bitmap->width(), bitmap->height(),left, top, right, bottom,
Romain Guyfb5e23c2010-07-09 13:52:56 -0700604 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700605
606 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
607 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700608 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
609 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy6820ac82010-09-15 18:11:50 -0700610 &mesh->vertices[0].texture[0], GL_TRIANGLES, mesh->verticesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700611}
612
Romain Guy759ea802010-09-16 20:49:46 -0700613void OpenGLRenderer::drawLines(float* points, int count, const SkPaint* paint) {
614 int alpha;
615 SkXfermode::Mode mode;
616 getAlphaAndMode(paint, &alpha, &mode);
617
618 uint32_t color = paint->getColor();
619 const GLfloat a = alpha / 255.0f;
620 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
621 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
622 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
623
Romain Guyc95c8d62010-09-17 15:31:32 -0700624 const bool isAA = paint->isAntiAlias();
625 if (isAA) {
626 GLuint textureUnit = 0;
Romain Guy29d89972010-09-22 16:10:57 -0700627 setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
628 mode, false, true, mCaches.line.getVertices(), mCaches.line.getTexCoords());
Romain Guyc95c8d62010-09-17 15:31:32 -0700629 } else {
630 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false);
631 }
632
633 const float strokeWidth = paint->getStrokeWidth();
Romain Guy29d89972010-09-22 16:10:57 -0700634 const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount;
Romain Guyc95c8d62010-09-17 15:31:32 -0700635 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
Romain Guy759ea802010-09-16 20:49:46 -0700636
637 for (int i = 0; i < count; i += 4) {
638 float tx = 0.0f;
639 float ty = 0.0f;
640
Romain Guyc95c8d62010-09-17 15:31:32 -0700641 if (isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700642 mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3],
Romain Guyc95c8d62010-09-17 15:31:32 -0700643 strokeWidth, tx, ty);
644 } else {
Romain Guyb5ab4172010-09-17 15:36:56 -0700645 ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f;
Romain Guyc95c8d62010-09-17 15:31:32 -0700646 }
Romain Guy759ea802010-09-16 20:49:46 -0700647
648 const float dx = points[i + 2] - points[i];
649 const float dy = points[i + 3] - points[i + 1];
650 const float mag = sqrtf(dx * dx + dy * dy);
651 const float angle = acos(dx / mag);
652
653 mModelView.loadTranslate(points[i], points[i + 1], 0.0f);
654 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) {
655 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f);
656 }
657 mModelView.translate(tx, ty, 0.0f);
Romain Guyc95c8d62010-09-17 15:31:32 -0700658 if (!isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700659 float length = mCaches.line.getLength(points[i], points[i + 1],
660 points[i + 2], points[i + 3]);
Romain Guyc95c8d62010-09-17 15:31:32 -0700661 mModelView.scale(length, strokeWidth, 1.0f);
662 }
Romain Guy759ea802010-09-16 20:49:46 -0700663 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
664
665 if (mShader) {
666 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot);
667 }
668
Romain Guyc95c8d62010-09-17 15:31:32 -0700669 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy759ea802010-09-16 20:49:46 -0700670 }
671
Romain Guy29d89972010-09-22 16:10:57 -0700672 if (isAA) {
673 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
674 }
Romain Guy759ea802010-09-16 20:49:46 -0700675}
676
Romain Guy85bf02f2010-06-22 13:11:24 -0700677void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700678 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700679 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700680}
Romain Guy9d5316e2010-06-24 19:30:36 -0700681
Romain Guybd6b79b2010-06-26 00:13:53 -0700682void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700683 if (quickReject(left, top, right, bottom)) {
684 return;
685 }
686
Romain Guy026c5e162010-06-28 17:12:22 -0700687 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700688 if (!mExtensions.hasFramebufferFetch()) {
689 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
690 if (!isMode) {
691 // Assume SRC_OVER
692 mode = SkXfermode::kSrcOver_Mode;
693 }
694 } else {
695 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700696 }
697
698 // Skia draws using the color's alpha channel if < 255
699 // Otherwise, it uses the paint's alpha
700 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700701 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700702 color |= p->getAlpha() << 24;
703 }
704
705 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700706}
Romain Guy9d5316e2010-06-24 19:30:36 -0700707
Romain Guye8e62a42010-07-23 18:55:21 -0700708void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
709 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700710 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700711 return;
712 }
Romain Guyf607bdc2010-09-10 19:20:06 -0700713 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700714
Romain Guya674ab72010-08-10 17:26:42 -0700715 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700716 switch (paint->getTextAlign()) {
717 case SkPaint::kCenter_Align:
718 length = paint->measureText(text, bytesCount);
719 x -= length / 2.0f;
720 break;
721 case SkPaint::kRight_Align:
722 length = paint->measureText(text, bytesCount);
723 x -= length;
724 break;
725 default:
726 break;
727 }
728
Romain Guy694b5192010-07-21 21:33:20 -0700729 int alpha;
730 SkXfermode::Mode mode;
731 getAlphaAndMode(paint, &alpha, &mode);
732
Romain Guy2542d192010-08-18 11:47:12 -0700733 uint32_t color = paint->getColor();
734 const GLfloat a = alpha / 255.0f;
735 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
736 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
737 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
738
Romain Guyb45c0c92010-08-26 20:35:23 -0700739 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
740 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700741 paint->getTextSize());
Romain Guy1e45aae2010-08-13 19:39:53 -0700742 if (mHasShadow) {
743 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700744 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700745 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700746 count, mShadowRadius);
747 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700748
Romain Guy2542d192010-08-18 11:47:12 -0700749 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700750
751 // Draw the mesh
752 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700753 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700754 }
755
Romain Guy06f96e22010-07-30 19:18:16 -0700756 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700757 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700758
Romain Guyb45c0c92010-08-26 20:35:23 -0700759 setupTextureAlpha8(fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
Romain Guy0a417492010-08-16 20:26:20 -0700760 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700761
Romain Guy09147fb2010-07-22 13:08:20 -0700762 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy86942302010-09-12 13:02:16 -0700763 clearLayerRegions();
Romain Guyb45c0c92010-08-26 20:35:23 -0700764 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700765
766 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700767 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700768
Romain Guy0a417492010-08-16 20:26:20 -0700769 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -0700770}
771
Romain Guy7fbcc042010-08-04 15:40:07 -0700772void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
773 GLuint textureUnit = 0;
774 glActiveTexture(gTextureUnits[textureUnit]);
775
Romain Guyfb8b7632010-08-23 21:05:08 -0700776 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700777 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700778 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700779
Romain Guy8b55f372010-08-18 17:10:07 -0700780 const float x = texture->left - texture->offset;
781 const float y = texture->top - texture->offset;
782
783 if (quickReject(x, y, x + texture->width, y + texture->height)) {
784 return;
785 }
786
Romain Guy7fbcc042010-08-04 15:40:07 -0700787 int alpha;
788 SkXfermode::Mode mode;
789 getAlphaAndMode(paint, &alpha, &mode);
790
791 uint32_t color = paint->getColor();
792 const GLfloat a = alpha / 255.0f;
793 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
794 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
795 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
796
Romain Guy0a417492010-08-16 20:26:20 -0700797 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
798
Romain Guy86942302010-09-12 13:02:16 -0700799 clearLayerRegions();
800
Romain Guy0a417492010-08-16 20:26:20 -0700801 // Draw the mesh
802 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700803 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700804}
805
Romain Guy6926c722010-07-12 20:20:03 -0700806///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700807// Shaders
808///////////////////////////////////////////////////////////////////////////////
809
810void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700811 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700812}
813
Romain Guy06f96e22010-07-30 19:18:16 -0700814void OpenGLRenderer::setupShader(SkiaShader* shader) {
815 mShader = shader;
816 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700817 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700818 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700819}
820
Romain Guyd27977d2010-07-14 19:18:51 -0700821///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700822// Color filters
823///////////////////////////////////////////////////////////////////////////////
824
825void OpenGLRenderer::resetColorFilter() {
826 mColorFilter = NULL;
827}
828
829void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
830 mColorFilter = filter;
831}
832
833///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700834// Drop shadow
835///////////////////////////////////////////////////////////////////////////////
836
837void OpenGLRenderer::resetShadow() {
838 mHasShadow = false;
839}
840
841void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
842 mHasShadow = true;
843 mShadowRadius = radius;
844 mShadowDx = dx;
845 mShadowDy = dy;
846 mShadowColor = color;
847}
848
849///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700850// Drawing implementation
851///////////////////////////////////////////////////////////////////////////////
852
Romain Guy0a417492010-08-16 20:26:20 -0700853void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700854 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700855 const float sx = x - texture->left + mShadowDx;
856 const float sy = y - texture->top + mShadowDy;
857
Romain Guy2542d192010-08-18 11:47:12 -0700858 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
859 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700860 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
861 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
862 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
863
864 GLuint textureUnit = 0;
865 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
866}
867
868void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
869 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
870 bool transforms, bool applyFilters) {
871 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -0700872 x, y, r, g, b, a, mode, transforms, applyFilters,
873 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
Romain Guy0a417492010-08-16 20:26:20 -0700874}
875
876void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
877 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
878 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy759ea802010-09-16 20:49:46 -0700879 setupTextureAlpha8(texture, width, height, textureUnit,
880 x, y, r, g, b, a, mode, transforms, applyFilters,
881 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
882}
883
884void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
885 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
886 SkXfermode::Mode mode, bool transforms, bool applyFilters,
887 GLvoid* vertices, GLvoid* texCoords) {
Romain Guy0a417492010-08-16 20:26:20 -0700888 // Describe the required shaders
889 ProgramDescription description;
890 description.hasTexture = true;
891 description.hasAlpha8Texture = true;
892
893 if (applyFilters) {
894 if (mShader) {
895 mShader->describe(description, mExtensions);
896 }
897 if (mColorFilter) {
898 mColorFilter->describe(description, mExtensions);
899 }
900 }
901
Romain Guya5aed0d2010-09-09 14:42:43 -0700902 // Setup the blending mode
903 chooseBlending(true, mode, description);
904
Romain Guy0a417492010-08-16 20:26:20 -0700905 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700906 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700907
Romain Guy0a417492010-08-16 20:26:20 -0700908 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -0700909 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700910
Romain Guyfb8b7632010-08-23 21:05:08 -0700911 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -0700912 glEnableVertexAttribArray(texCoordsSlot);
913
914 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700915 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -0700916 gMeshStride, vertices);
Romain Guy0a417492010-08-16 20:26:20 -0700917 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -0700918 gMeshStride, texCoords);
Romain Guy0a417492010-08-16 20:26:20 -0700919
920 // Setup uniforms
921 if (transforms) {
922 mModelView.loadTranslate(x, y, 0.0f);
923 mModelView.scale(width, height, 1.0f);
924 } else {
925 mModelView.loadIdentity();
926 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700927 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyfb8b7632010-08-23 21:05:08 -0700928 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -0700929
930 textureUnit++;
931 if (applyFilters) {
932 // Setup attributes and uniforms required by the shaders
933 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700934 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700935 }
936 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700937 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -0700938 }
939 }
940}
941
Romain Guyf607bdc2010-09-10 19:20:06 -0700942// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -0700943#define kStdStrikeThru_Offset (-6.0f / 21.0f)
944#define kStdUnderline_Offset (1.0f / 9.0f)
945#define kStdUnderline_Thickness (1.0f / 18.0f)
946
947void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
948 float x, float y, SkPaint* paint) {
949 // Handle underline and strike-through
950 uint32_t flags = paint->getFlags();
951 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
952 float underlineWidth = length;
953 // If length is > 0.0f, we already measured the text for the text alignment
954 if (length <= 0.0f) {
955 underlineWidth = paint->measureText(text, bytesCount);
956 }
957
958 float offsetX = 0;
959 switch (paint->getTextAlign()) {
960 case SkPaint::kCenter_Align:
961 offsetX = underlineWidth * 0.5f;
962 break;
963 case SkPaint::kRight_Align:
964 offsetX = underlineWidth;
965 break;
966 default:
967 break;
968 }
969
970 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -0700971 const float textSize = paint->getTextSize();
972 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -0700973
Romain Guye20ecbd2010-09-22 19:49:04 -0700974 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -0700975 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -0700976
977 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
978 float points[pointsCount];
979 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -0700980
981 if (flags & SkPaint::kUnderlineText_Flag) {
982 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -0700983 points[currentPoint++] = left;
984 points[currentPoint++] = top;
985 points[currentPoint++] = left + underlineWidth;
986 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -0700987 }
988
989 if (flags & SkPaint::kStrikeThruText_Flag) {
990 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -0700991 points[currentPoint++] = left;
992 points[currentPoint++] = top;
993 points[currentPoint++] = left + underlineWidth;
994 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -0700995 }
Romain Guye20ecbd2010-09-22 19:49:04 -0700996
997 SkPaint linesPaint(*paint);
998 linesPaint.setStrokeWidth(strokeWidth);
999
1000 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001001 }
1002 }
1003}
1004
Romain Guy026c5e162010-06-28 17:12:22 -07001005void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001006 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001007 clearLayerRegions();
1008
Romain Guyd27977d2010-07-14 19:18:51 -07001009 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001010 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001011 color |= 0x00ffffff;
1012 }
1013
1014 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -07001015 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -07001016 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -07001017 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1018 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1019 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1020
Romain Guyc95c8d62010-09-17 15:31:32 -07001021 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform);
1022
1023 // Draw the mesh
1024 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1025}
1026
1027void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom,
1028 float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy06f96e22010-07-30 19:18:16 -07001029 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -07001030
Romain Guy06f96e22010-07-30 19:18:16 -07001031 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -07001032 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -07001033 if (mShader) {
1034 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -07001035 }
Romain Guydb1938e2010-08-02 18:50:22 -07001036 if (mColorFilter) {
1037 mColorFilter->describe(description, mExtensions);
1038 }
Romain Guyd27977d2010-07-14 19:18:51 -07001039
Romain Guy1c740bc2010-09-13 18:00:09 -07001040 // Setup the blending mode
Romain Guyc95c8d62010-09-17 15:31:32 -07001041 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -07001042
Romain Guy06f96e22010-07-30 19:18:16 -07001043 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001044 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -07001045
1046 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -07001047 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -07001048 gMeshStride, &mMeshVertices[0].position[0]);
1049
1050 // Setup uniforms
1051 mModelView.loadTranslate(left, top, 0.0f);
1052 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -07001053 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001054 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -07001055 } else {
1056 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -07001057 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -07001058 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001059 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -07001060
Romain Guy06f96e22010-07-30 19:18:16 -07001061 // Setup attributes and uniforms required by the shaders
1062 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001063 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001064 }
Romain Guydb1938e2010-08-02 18:50:22 -07001065 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001066 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001067 }
Romain Guyd27977d2010-07-14 19:18:51 -07001068}
1069
Romain Guy82ba8142010-07-09 13:25:56 -07001070void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001071 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001072 int alpha;
1073 SkXfermode::Mode mode;
1074 getAlphaAndMode(paint, &alpha, &mode);
1075
Romain Guy6820ac82010-09-15 18:11:50 -07001076 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1077 texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1078 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -07001079}
1080
Romain Guybd6b79b2010-06-26 00:13:53 -07001081void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001082 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1083 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001084 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1085 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001086}
1087
1088void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001089 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001090 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guyf607bdc2010-09-10 19:20:06 -07001091 bool swapSrcDst, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001092 clearLayerRegions();
1093
Romain Guy889f8d12010-07-29 14:37:42 -07001094 ProgramDescription description;
1095 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -07001096 if (mColorFilter) {
1097 mColorFilter->describe(description, mExtensions);
1098 }
Romain Guy889f8d12010-07-29 14:37:42 -07001099
Romain Guybd6b79b2010-06-26 00:13:53 -07001100 mModelView.loadTranslate(left, top, 0.0f);
1101 mModelView.scale(right - left, bottom - top, 1.0f);
1102
Romain Guyf607bdc2010-09-10 19:20:06 -07001103 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001104
Romain Guyfb8b7632010-08-23 21:05:08 -07001105 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001106 if (!ignoreTransform) {
1107 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1108 } else {
1109 mat4 m;
1110 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
1111 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001112
Romain Guy889f8d12010-07-29 14:37:42 -07001113 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -07001114 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001115 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001116
Romain Guya9794742010-07-13 11:37:54 -07001117 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -07001118 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -07001119
Romain Guy889f8d12010-07-29 14:37:42 -07001120 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001121 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001122 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -07001123 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001124 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001125 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001126
Romain Guydb1938e2010-08-02 18:50:22 -07001127 // Color filter
1128 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001129 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001130 }
1131
Romain Guy6820ac82010-09-15 18:11:50 -07001132 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001133 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001134}
1135
Romain Guya5aed0d2010-09-09 14:42:43 -07001136void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001137 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001138 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1139 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001140 if (mode < SkXfermode::kPlus_Mode) {
1141 if (!mCaches.blend) {
1142 glEnable(GL_BLEND);
1143 }
Romain Guy82ba8142010-07-09 13:25:56 -07001144
Romain Guyf607bdc2010-09-10 19:20:06 -07001145 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1146 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001147
Romain Guya5aed0d2010-09-09 14:42:43 -07001148 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1149 glBlendFunc(sourceMode, destMode);
1150 mCaches.lastSrcMode = sourceMode;
1151 mCaches.lastDstMode = destMode;
1152 }
1153 } else {
1154 // These blend modes are not supported by OpenGL directly and have
1155 // to be implemented using shaders. Since the shader will perform
1156 // the blending, turn blending off here
1157 if (mExtensions.hasFramebufferFetch()) {
1158 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001159 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001160 }
1161
1162 if (mCaches.blend) {
1163 glDisable(GL_BLEND);
1164 }
1165 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001166 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001167 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001168 glDisable(GL_BLEND);
1169 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001170 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001171}
1172
Romain Guy889f8d12010-07-29 14:37:42 -07001173bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001174 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001175 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001176 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001177 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001178 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001179 }
Romain Guy6926c722010-07-12 20:20:03 -07001180 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001181}
1182
Romain Guy026c5e162010-06-28 17:12:22 -07001183void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001184 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001185 TextureVertex::setUV(v++, u1, v1);
1186 TextureVertex::setUV(v++, u2, v1);
1187 TextureVertex::setUV(v++, u1, v2);
1188 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001189}
1190
1191void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1192 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001193 if (!mExtensions.hasFramebufferFetch()) {
1194 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1195 if (!isMode) {
1196 // Assume SRC_OVER
1197 *mode = SkXfermode::kSrcOver_Mode;
1198 }
1199 } else {
1200 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001201 }
1202
1203 // Skia draws using the color's alpha channel if < 255
1204 // Otherwise, it uses the paint's alpha
1205 int color = paint->getColor();
1206 *alpha = (color >> 24) & 0xFF;
1207 if (*alpha == 255) {
1208 *alpha = paint->getAlpha();
1209 }
1210 } else {
1211 *mode = SkXfermode::kSrcOver_Mode;
1212 *alpha = 255;
1213 }
Romain Guy026c5e162010-06-28 17:12:22 -07001214}
1215
Romain Guya5aed0d2010-09-09 14:42:43 -07001216SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1217 if (mode == NULL) {
1218 return SkXfermode::kSrcOver_Mode;
1219 }
1220 return mode->fMode;
1221}
1222
Romain Guy889f8d12010-07-29 14:37:42 -07001223void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1224 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001225 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001226 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1227 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1228}
1229
Romain Guy9d5316e2010-06-24 19:30:36 -07001230}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001231}; // namespace android