blob: e62a693bda477408b6f466de1fd3e5256e09bc20 [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
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 Guydda57022010-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 Guydda57022010-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 Guydda57022010-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 Guydda57022010-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);
384 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
385 bounds.getWidth(), bounds.getHeight(), 0);
386
Romain Guyf607bdc2010-09-10 19:20:06 -0700387 if (flags & SkCanvas::kClipToLayer_SaveFlag) {
Romain Guy86942302010-09-12 13:02:16 -0700388 if (mSnapshot->clipTransformed(bounds)) setScissorFromClip();
Romain Guyf607bdc2010-09-10 19:20:06 -0700389 }
390
Romain Guy86942302010-09-12 13:02:16 -0700391 // Enqueue the buffer coordinates to clear the corresponding region later
392 mLayers.push(new Rect(bounds));
Romain Guyf86ef572010-07-01 11:05:42 -0700393
Romain Guyd55a8612010-06-28 17:42:46 -0700394 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700395}
396
Romain Guy1c740bc2010-09-13 18:00:09 -0700397/**
398 * Read the documentation of createLayer() before doing anything in this method.
399 */
Romain Guy1d83e192010-08-17 11:37:00 -0700400void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
401 if (!current->layer) {
402 LOGE("Attempting to compose a layer that does not exist");
403 return;
404 }
405
Romain Guy1d83e192010-08-17 11:37:00 -0700406 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700407 const Rect& clip = *previous->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700408 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700409
410 Layer* layer = current->layer;
411 const Rect& rect = layer->layer;
412
Romain Guyf607bdc2010-09-10 19:20:06 -0700413 if (layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700414 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700415 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guyf607bdc2010-09-10 19:20:06 -0700416 }
417
418 // Layers are already drawn with a top-left origin, don't flip the texture
Romain Guy8b55f372010-08-18 17:10:07 -0700419 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
420
Romain Guyf607bdc2010-09-10 19:20:06 -0700421 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
422 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
Romain Guy6820ac82010-09-15 18:11:50 -0700423 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, true, true);
Romain Guy1d83e192010-08-17 11:37:00 -0700424
Romain Guy8b55f372010-08-18 17:10:07 -0700425 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
426
Romain Guy1d83e192010-08-17 11:37:00 -0700427 LayerSize size(rect.getWidth(), rect.getHeight());
428 // Failing to add the layer to the cache should happen only if the
429 // layer is too large
Romain Guyfb8b7632010-08-23 21:05:08 -0700430 if (!mCaches.layerCache.put(size, layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700431 LAYER_LOGD("Deleting layer");
432
Romain Guy1d83e192010-08-17 11:37:00 -0700433 glDeleteTextures(1, &layer->texture);
434
435 delete layer;
436 }
437}
438
Romain Guy86942302010-09-12 13:02:16 -0700439void OpenGLRenderer::clearLayerRegions() {
440 if (mLayers.size() == 0) return;
441
442 for (uint32_t i = 0; i < mLayers.size(); i++) {
443 Rect* bounds = mLayers.itemAt(i);
444
445 // Clear the framebuffer where the layer will draw
446 glScissor(bounds->left, mHeight - bounds->bottom,
447 bounds->getWidth(), bounds->getHeight());
448 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
449 glClear(GL_COLOR_BUFFER_BIT);
450
451 delete bounds;
452 }
453 mLayers.clear();
454
455 // Restore the clip
456 setScissorFromClip();
457}
458
Romain Guybd6b79b2010-06-26 00:13:53 -0700459///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700460// Transforms
461///////////////////////////////////////////////////////////////////////////////
462
463void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700464 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700465}
466
467void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700468 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700469}
470
471void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700472 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700473}
474
475void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700476 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700477}
478
479void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700480 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700481}
482
483void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700484 mat4 m(*matrix);
Romain Guy8aef54f2010-09-01 15:13:49 -0700485 mSnapshot->transform->multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700486}
487
488///////////////////////////////////////////////////////////////////////////////
489// Clipping
490///////////////////////////////////////////////////////////////////////////////
491
Romain Guybb9524b2010-06-22 18:56:38 -0700492void OpenGLRenderer::setScissorFromClip() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700493 const Rect& clip = *mSnapshot->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700494 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700495}
496
497const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700498 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700499}
500
Romain Guyc7d53492010-06-25 13:41:57 -0700501bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy1d83e192010-08-17 11:37:00 -0700502 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700503 mSnapshot->transform->mapRect(r);
504 return !mSnapshot->clipRect->intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700505}
506
Romain Guy079ba2c2010-07-16 14:12:24 -0700507bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
508 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700509 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700510 setScissorFromClip();
511 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700512 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700513}
514
Romain Guyf6a11b82010-06-23 17:47:49 -0700515///////////////////////////////////////////////////////////////////////////////
516// Drawing
517///////////////////////////////////////////////////////////////////////////////
518
Romain Guyc1396e92010-06-30 17:56:19 -0700519void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700520 const float right = left + bitmap->width();
521 const float bottom = top + bitmap->height();
522
523 if (quickReject(left, top, right, bottom)) {
524 return;
525 }
526
Romain Guy61c8c9c2010-08-09 20:48:09 -0700527 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700528 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700529 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700530 const AutoTexture autoCleanup(texture);
531
Romain Guy6926c722010-07-12 20:20:03 -0700532 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700533}
534
Romain Guyf86ef572010-07-01 11:05:42 -0700535void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
536 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
537 const mat4 transform(*matrix);
538 transform.mapRect(r);
539
Romain Guy6926c722010-07-12 20:20:03 -0700540 if (quickReject(r.left, r.top, r.right, r.bottom)) {
541 return;
542 }
543
Romain Guy61c8c9c2010-08-09 20:48:09 -0700544 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700545 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700546 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700547 const AutoTexture autoCleanup(texture);
548
Romain Guy82ba8142010-07-09 13:25:56 -0700549 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700550}
551
Romain Guy8ba548f2010-06-30 19:21:21 -0700552void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
553 float srcLeft, float srcTop, float srcRight, float srcBottom,
554 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700555 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700556 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
557 return;
558 }
559
Romain Guy61c8c9c2010-08-09 20:48:09 -0700560 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700561 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700562 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700563 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700564
Romain Guy8ba548f2010-06-30 19:21:21 -0700565 const float width = texture->width;
566 const float height = texture->height;
567
568 const float u1 = srcLeft / width;
569 const float v1 = srcTop / height;
570 const float u2 = srcRight / width;
571 const float v2 = srcBottom / height;
572
573 resetDrawTextureTexCoords(u1, v1, u2, v2);
574
Romain Guy82ba8142010-07-09 13:25:56 -0700575 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700576
577 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
578}
579
Romain Guydeba7852010-07-07 17:54:48 -0700580void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
581 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700582 if (quickReject(left, top, right, bottom)) {
583 return;
584 }
585
Romain Guy61c8c9c2010-08-09 20:48:09 -0700586 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700587 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700588 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700589 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700590
591 int alpha;
592 SkXfermode::Mode mode;
593 getAlphaAndMode(paint, &alpha, &mode);
594
Romain Guyfb8b7632010-08-23 21:05:08 -0700595 Patch* mesh = mCaches.patchCache.get(patch);
Romain Guy759ea802010-09-16 20:49:46 -0700596 mesh->updateVertices(bitmap->width(), bitmap->height(),left, top, right, bottom,
Romain Guyfb5e23c2010-07-09 13:52:56 -0700597 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700598
599 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
600 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700601 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
602 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy6820ac82010-09-15 18:11:50 -0700603 &mesh->vertices[0].texture[0], GL_TRIANGLES, mesh->verticesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700604}
605
Romain Guy759ea802010-09-16 20:49:46 -0700606void OpenGLRenderer::drawLines(float* points, int count, const SkPaint* paint) {
607 int alpha;
608 SkXfermode::Mode mode;
609 getAlphaAndMode(paint, &alpha, &mode);
610
611 uint32_t color = paint->getColor();
612 const GLfloat a = alpha / 255.0f;
613 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
614 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
615 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
616
Romain Guyc95c8d62010-09-17 15:31:32 -0700617 const bool isAA = paint->isAntiAlias();
618 if (isAA) {
619 GLuint textureUnit = 0;
Romain Guy29d89972010-09-22 16:10:57 -0700620 setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
621 mode, false, true, mCaches.line.getVertices(), mCaches.line.getTexCoords());
Romain Guyc95c8d62010-09-17 15:31:32 -0700622 } else {
623 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false);
624 }
625
626 const float strokeWidth = paint->getStrokeWidth();
Romain Guy29d89972010-09-22 16:10:57 -0700627 const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount;
Romain Guyc95c8d62010-09-17 15:31:32 -0700628 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
Romain Guy759ea802010-09-16 20:49:46 -0700629
630 for (int i = 0; i < count; i += 4) {
631 float tx = 0.0f;
632 float ty = 0.0f;
633
Romain Guyc95c8d62010-09-17 15:31:32 -0700634 if (isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700635 mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3],
Romain Guyc95c8d62010-09-17 15:31:32 -0700636 strokeWidth, tx, ty);
637 } else {
Romain Guyb5ab4172010-09-17 15:36:56 -0700638 ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f;
Romain Guyc95c8d62010-09-17 15:31:32 -0700639 }
Romain Guy759ea802010-09-16 20:49:46 -0700640
641 const float dx = points[i + 2] - points[i];
642 const float dy = points[i + 3] - points[i + 1];
643 const float mag = sqrtf(dx * dx + dy * dy);
644 const float angle = acos(dx / mag);
645
646 mModelView.loadTranslate(points[i], points[i + 1], 0.0f);
647 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) {
648 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f);
649 }
650 mModelView.translate(tx, ty, 0.0f);
Romain Guyc95c8d62010-09-17 15:31:32 -0700651 if (!isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700652 float length = mCaches.line.getLength(points[i], points[i + 1],
653 points[i + 2], points[i + 3]);
Romain Guyc95c8d62010-09-17 15:31:32 -0700654 mModelView.scale(length, strokeWidth, 1.0f);
655 }
Romain Guy759ea802010-09-16 20:49:46 -0700656 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
657
658 if (mShader) {
659 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot);
660 }
661
Romain Guyc95c8d62010-09-17 15:31:32 -0700662 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy759ea802010-09-16 20:49:46 -0700663 }
664
Romain Guy29d89972010-09-22 16:10:57 -0700665 if (isAA) {
666 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
667 }
Romain Guy759ea802010-09-16 20:49:46 -0700668}
669
Romain Guy85bf02f2010-06-22 13:11:24 -0700670void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700671 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700672 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700673}
Romain Guy9d5316e2010-06-24 19:30:36 -0700674
Romain Guybd6b79b2010-06-26 00:13:53 -0700675void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700676 if (quickReject(left, top, right, bottom)) {
677 return;
678 }
679
Romain Guy026c5e162010-06-28 17:12:22 -0700680 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700681 if (!mExtensions.hasFramebufferFetch()) {
682 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
683 if (!isMode) {
684 // Assume SRC_OVER
685 mode = SkXfermode::kSrcOver_Mode;
686 }
687 } else {
688 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700689 }
690
691 // Skia draws using the color's alpha channel if < 255
692 // Otherwise, it uses the paint's alpha
693 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700694 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700695 color |= p->getAlpha() << 24;
696 }
697
698 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700699}
Romain Guy9d5316e2010-06-24 19:30:36 -0700700
Romain Guye8e62a42010-07-23 18:55:21 -0700701void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
702 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700703 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700704 return;
705 }
Romain Guyf607bdc2010-09-10 19:20:06 -0700706 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700707
Romain Guya674ab72010-08-10 17:26:42 -0700708 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700709 switch (paint->getTextAlign()) {
710 case SkPaint::kCenter_Align:
711 length = paint->measureText(text, bytesCount);
712 x -= length / 2.0f;
713 break;
714 case SkPaint::kRight_Align:
715 length = paint->measureText(text, bytesCount);
716 x -= length;
717 break;
718 default:
719 break;
720 }
721
Romain Guy694b5192010-07-21 21:33:20 -0700722 int alpha;
723 SkXfermode::Mode mode;
724 getAlphaAndMode(paint, &alpha, &mode);
725
Romain Guy2542d192010-08-18 11:47:12 -0700726 uint32_t color = paint->getColor();
727 const GLfloat a = alpha / 255.0f;
728 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
729 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
730 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
731
Romain Guyb45c0c92010-08-26 20:35:23 -0700732 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
733 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700734 paint->getTextSize());
Romain Guy1e45aae2010-08-13 19:39:53 -0700735 if (mHasShadow) {
736 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700737 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700738 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700739 count, mShadowRadius);
740 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700741
Romain Guy2542d192010-08-18 11:47:12 -0700742 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700743
744 // Draw the mesh
745 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700746 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700747 }
748
Romain Guy06f96e22010-07-30 19:18:16 -0700749 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700750 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700751
Romain Guyb45c0c92010-08-26 20:35:23 -0700752 setupTextureAlpha8(fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
Romain Guy0a417492010-08-16 20:26:20 -0700753 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700754
Romain Guy09147fb2010-07-22 13:08:20 -0700755 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy86942302010-09-12 13:02:16 -0700756 clearLayerRegions();
Romain Guyb45c0c92010-08-26 20:35:23 -0700757 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700758
759 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700760 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700761
Romain Guy0a417492010-08-16 20:26:20 -0700762 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -0700763}
764
Romain Guy7fbcc042010-08-04 15:40:07 -0700765void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
766 GLuint textureUnit = 0;
767 glActiveTexture(gTextureUnits[textureUnit]);
768
Romain Guyfb8b7632010-08-23 21:05:08 -0700769 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700770 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700771 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700772
Romain Guy8b55f372010-08-18 17:10:07 -0700773 const float x = texture->left - texture->offset;
774 const float y = texture->top - texture->offset;
775
776 if (quickReject(x, y, x + texture->width, y + texture->height)) {
777 return;
778 }
779
Romain Guy7fbcc042010-08-04 15:40:07 -0700780 int alpha;
781 SkXfermode::Mode mode;
782 getAlphaAndMode(paint, &alpha, &mode);
783
784 uint32_t color = paint->getColor();
785 const GLfloat a = alpha / 255.0f;
786 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
787 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
788 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
789
Romain Guy0a417492010-08-16 20:26:20 -0700790 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
791
Romain Guy86942302010-09-12 13:02:16 -0700792 clearLayerRegions();
793
Romain Guy0a417492010-08-16 20:26:20 -0700794 // Draw the mesh
795 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700796 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700797}
798
Romain Guy6926c722010-07-12 20:20:03 -0700799///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700800// Shaders
801///////////////////////////////////////////////////////////////////////////////
802
803void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700804 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700805}
806
Romain Guy06f96e22010-07-30 19:18:16 -0700807void OpenGLRenderer::setupShader(SkiaShader* shader) {
808 mShader = shader;
809 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700810 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700811 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700812}
813
Romain Guyd27977d2010-07-14 19:18:51 -0700814///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700815// Color filters
816///////////////////////////////////////////////////////////////////////////////
817
818void OpenGLRenderer::resetColorFilter() {
819 mColorFilter = NULL;
820}
821
822void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
823 mColorFilter = filter;
824}
825
826///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700827// Drop shadow
828///////////////////////////////////////////////////////////////////////////////
829
830void OpenGLRenderer::resetShadow() {
831 mHasShadow = false;
832}
833
834void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
835 mHasShadow = true;
836 mShadowRadius = radius;
837 mShadowDx = dx;
838 mShadowDy = dy;
839 mShadowColor = color;
840}
841
842///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700843// Drawing implementation
844///////////////////////////////////////////////////////////////////////////////
845
Romain Guy0a417492010-08-16 20:26:20 -0700846void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700847 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700848 const float sx = x - texture->left + mShadowDx;
849 const float sy = y - texture->top + mShadowDy;
850
Romain Guy2542d192010-08-18 11:47:12 -0700851 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
852 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700853 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
854 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
855 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
856
857 GLuint textureUnit = 0;
858 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
859}
860
861void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
862 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
863 bool transforms, bool applyFilters) {
864 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -0700865 x, y, r, g, b, a, mode, transforms, applyFilters,
866 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
Romain Guy0a417492010-08-16 20:26:20 -0700867}
868
869void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
870 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
871 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy759ea802010-09-16 20:49:46 -0700872 setupTextureAlpha8(texture, width, height, textureUnit,
873 x, y, r, g, b, a, mode, transforms, applyFilters,
874 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
875}
876
877void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
878 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
879 SkXfermode::Mode mode, bool transforms, bool applyFilters,
880 GLvoid* vertices, GLvoid* texCoords) {
Romain Guy0a417492010-08-16 20:26:20 -0700881 // Describe the required shaders
882 ProgramDescription description;
883 description.hasTexture = true;
884 description.hasAlpha8Texture = true;
885
886 if (applyFilters) {
887 if (mShader) {
888 mShader->describe(description, mExtensions);
889 }
890 if (mColorFilter) {
891 mColorFilter->describe(description, mExtensions);
892 }
893 }
894
Romain Guya5aed0d2010-09-09 14:42:43 -0700895 // Setup the blending mode
896 chooseBlending(true, mode, description);
897
Romain Guy0a417492010-08-16 20:26:20 -0700898 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700899 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700900
Romain Guy0a417492010-08-16 20:26:20 -0700901 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -0700902 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700903
Romain Guyfb8b7632010-08-23 21:05:08 -0700904 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -0700905 glEnableVertexAttribArray(texCoordsSlot);
906
907 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700908 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -0700909 gMeshStride, vertices);
Romain Guy0a417492010-08-16 20:26:20 -0700910 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -0700911 gMeshStride, texCoords);
Romain Guy0a417492010-08-16 20:26:20 -0700912
913 // Setup uniforms
914 if (transforms) {
915 mModelView.loadTranslate(x, y, 0.0f);
916 mModelView.scale(width, height, 1.0f);
917 } else {
918 mModelView.loadIdentity();
919 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700920 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyfb8b7632010-08-23 21:05:08 -0700921 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -0700922
923 textureUnit++;
924 if (applyFilters) {
925 // Setup attributes and uniforms required by the shaders
926 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700927 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700928 }
929 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700930 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -0700931 }
932 }
933}
934
Romain Guyf607bdc2010-09-10 19:20:06 -0700935// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -0700936#define kStdStrikeThru_Offset (-6.0f / 21.0f)
937#define kStdUnderline_Offset (1.0f / 9.0f)
938#define kStdUnderline_Thickness (1.0f / 18.0f)
939
940void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
941 float x, float y, SkPaint* paint) {
942 // Handle underline and strike-through
943 uint32_t flags = paint->getFlags();
944 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
945 float underlineWidth = length;
946 // If length is > 0.0f, we already measured the text for the text alignment
947 if (length <= 0.0f) {
948 underlineWidth = paint->measureText(text, bytesCount);
949 }
950
951 float offsetX = 0;
952 switch (paint->getTextAlign()) {
953 case SkPaint::kCenter_Align:
954 offsetX = underlineWidth * 0.5f;
955 break;
956 case SkPaint::kRight_Align:
957 offsetX = underlineWidth;
958 break;
959 default:
960 break;
961 }
962
963 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -0700964 const float textSize = paint->getTextSize();
965 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -0700966
Romain Guye20ecbd2010-09-22 19:49:04 -0700967 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -0700968 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -0700969
970 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
971 float points[pointsCount];
972 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -0700973
974 if (flags & SkPaint::kUnderlineText_Flag) {
975 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -0700976 points[currentPoint++] = left;
977 points[currentPoint++] = top;
978 points[currentPoint++] = left + underlineWidth;
979 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -0700980 }
981
982 if (flags & SkPaint::kStrikeThruText_Flag) {
983 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -0700984 points[currentPoint++] = left;
985 points[currentPoint++] = top;
986 points[currentPoint++] = left + underlineWidth;
987 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -0700988 }
Romain Guye20ecbd2010-09-22 19:49:04 -0700989
990 SkPaint linesPaint(*paint);
991 linesPaint.setStrokeWidth(strokeWidth);
992
993 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -0700994 }
995 }
996}
997
Romain Guy026c5e162010-06-28 17:12:22 -0700998void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700999 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001000 clearLayerRegions();
1001
Romain Guyd27977d2010-07-14 19:18:51 -07001002 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001003 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001004 color |= 0x00ffffff;
1005 }
1006
1007 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -07001008 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -07001009 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -07001010 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1011 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1012 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1013
Romain Guyc95c8d62010-09-17 15:31:32 -07001014 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform);
1015
1016 // Draw the mesh
1017 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1018}
1019
1020void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom,
1021 float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy06f96e22010-07-30 19:18:16 -07001022 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -07001023
Romain Guy06f96e22010-07-30 19:18:16 -07001024 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -07001025 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -07001026 if (mShader) {
1027 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -07001028 }
Romain Guydb1938e2010-08-02 18:50:22 -07001029 if (mColorFilter) {
1030 mColorFilter->describe(description, mExtensions);
1031 }
Romain Guyd27977d2010-07-14 19:18:51 -07001032
Romain Guy1c740bc2010-09-13 18:00:09 -07001033 // Setup the blending mode
Romain Guyc95c8d62010-09-17 15:31:32 -07001034 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -07001035
Romain Guy06f96e22010-07-30 19:18:16 -07001036 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001037 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -07001038
1039 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -07001040 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -07001041 gMeshStride, &mMeshVertices[0].position[0]);
1042
1043 // Setup uniforms
1044 mModelView.loadTranslate(left, top, 0.0f);
1045 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -07001046 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001047 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -07001048 } else {
1049 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -07001050 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -07001051 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001052 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -07001053
Romain Guy06f96e22010-07-30 19:18:16 -07001054 // Setup attributes and uniforms required by the shaders
1055 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001056 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001057 }
Romain Guydb1938e2010-08-02 18:50:22 -07001058 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001059 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001060 }
Romain Guyd27977d2010-07-14 19:18:51 -07001061}
1062
Romain Guy82ba8142010-07-09 13:25:56 -07001063void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001064 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001065 int alpha;
1066 SkXfermode::Mode mode;
1067 getAlphaAndMode(paint, &alpha, &mode);
1068
Romain Guy6820ac82010-09-15 18:11:50 -07001069 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1070 texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1071 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -07001072}
1073
Romain Guybd6b79b2010-06-26 00:13:53 -07001074void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001075 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1076 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001077 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1078 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001079}
1080
1081void OpenGLRenderer::drawTextureMesh(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,
Romain Guy6820ac82010-09-15 18:11:50 -07001083 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guyf607bdc2010-09-10 19:20:06 -07001084 bool swapSrcDst, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001085 clearLayerRegions();
1086
Romain Guy889f8d12010-07-29 14:37:42 -07001087 ProgramDescription description;
1088 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -07001089 if (mColorFilter) {
1090 mColorFilter->describe(description, mExtensions);
1091 }
Romain Guy889f8d12010-07-29 14:37:42 -07001092
Romain Guybd6b79b2010-06-26 00:13:53 -07001093 mModelView.loadTranslate(left, top, 0.0f);
1094 mModelView.scale(right - left, bottom - top, 1.0f);
1095
Romain Guyf607bdc2010-09-10 19:20:06 -07001096 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001097
Romain Guyfb8b7632010-08-23 21:05:08 -07001098 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001099 if (!ignoreTransform) {
1100 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1101 } else {
1102 mat4 m;
1103 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
1104 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001105
Romain Guy889f8d12010-07-29 14:37:42 -07001106 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -07001107 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001108 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001109
Romain Guya9794742010-07-13 11:37:54 -07001110 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -07001111 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -07001112
Romain Guy889f8d12010-07-29 14:37:42 -07001113 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001114 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001115 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -07001116 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001117 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001118 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001119
Romain Guydb1938e2010-08-02 18:50:22 -07001120 // Color filter
1121 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001122 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001123 }
1124
Romain Guy6820ac82010-09-15 18:11:50 -07001125 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001126 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001127}
1128
Romain Guya5aed0d2010-09-09 14:42:43 -07001129void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001130 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001131 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1132 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001133 if (mode < SkXfermode::kPlus_Mode) {
1134 if (!mCaches.blend) {
1135 glEnable(GL_BLEND);
1136 }
Romain Guy82ba8142010-07-09 13:25:56 -07001137
Romain Guyf607bdc2010-09-10 19:20:06 -07001138 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1139 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001140
Romain Guya5aed0d2010-09-09 14:42:43 -07001141 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1142 glBlendFunc(sourceMode, destMode);
1143 mCaches.lastSrcMode = sourceMode;
1144 mCaches.lastDstMode = destMode;
1145 }
1146 } else {
1147 // These blend modes are not supported by OpenGL directly and have
1148 // to be implemented using shaders. Since the shader will perform
1149 // the blending, turn blending off here
1150 if (mExtensions.hasFramebufferFetch()) {
1151 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001152 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001153 }
1154
1155 if (mCaches.blend) {
1156 glDisable(GL_BLEND);
1157 }
1158 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001159 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001160 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001161 glDisable(GL_BLEND);
1162 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001163 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001164}
1165
Romain Guy889f8d12010-07-29 14:37:42 -07001166bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001167 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001168 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001169 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001170 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001171 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001172 }
Romain Guy6926c722010-07-12 20:20:03 -07001173 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001174}
1175
Romain Guy026c5e162010-06-28 17:12:22 -07001176void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001177 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001178 TextureVertex::setUV(v++, u1, v1);
1179 TextureVertex::setUV(v++, u2, v1);
1180 TextureVertex::setUV(v++, u1, v2);
1181 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001182}
1183
1184void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1185 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001186 if (!mExtensions.hasFramebufferFetch()) {
1187 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1188 if (!isMode) {
1189 // Assume SRC_OVER
1190 *mode = SkXfermode::kSrcOver_Mode;
1191 }
1192 } else {
1193 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001194 }
1195
1196 // Skia draws using the color's alpha channel if < 255
1197 // Otherwise, it uses the paint's alpha
1198 int color = paint->getColor();
1199 *alpha = (color >> 24) & 0xFF;
1200 if (*alpha == 255) {
1201 *alpha = paint->getAlpha();
1202 }
1203 } else {
1204 *mode = SkXfermode::kSrcOver_Mode;
1205 *alpha = 255;
1206 }
Romain Guy026c5e162010-06-28 17:12:22 -07001207}
1208
Romain Guya5aed0d2010-09-09 14:42:43 -07001209SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1210 if (mode == NULL) {
1211 return SkXfermode::kSrcOver_Mode;
1212 }
1213 return mode->fMode;
1214}
1215
Romain Guy889f8d12010-07-29 14:37:42 -07001216void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1217 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001218 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001219 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1220 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1221}
1222
Romain Guy9d5316e2010-06-24 19:30:36 -07001223}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001224}; // namespace android