blob: ecc02fa66aed1df8c2666a5827fbcd3c244d4907 [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 Guye4d01122010-06-16 18:44:05 -0700136}
137
Romain Guyf6a11b82010-06-23 17:47:49 -0700138///////////////////////////////////////////////////////////////////////////////
139// Setup
140///////////////////////////////////////////////////////////////////////////////
141
Romain Guy85bf02f2010-06-22 13:11:24 -0700142void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700143 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700144 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700145
146 mWidth = width;
147 mHeight = height;
Romain Guye4d01122010-06-16 18:44:05 -0700148}
149
Romain Guy85bf02f2010-06-22 13:11:24 -0700150void OpenGLRenderer::prepare() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700151 mSnapshot = new Snapshot(mFirstSnapshot,
152 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy8fb95422010-08-17 18:38:51 -0700153 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700154
Romain Guyfb8b7632010-08-23 21:05:08 -0700155 glViewport(0, 0, mWidth, mHeight);
156
Romain Guyd90f23e2010-09-09 11:47:54 -0700157 glDisable(GL_DITHER);
Romain Guy08ae3172010-06-21 19:35:50 -0700158 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700159
Romain Guy08ae3172010-06-21 19:35:50 -0700160 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
161 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700162
Romain Guy08ae3172010-06-21 19:35:50 -0700163 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700164 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700165
Romain Guyb82da652010-07-30 11:36:12 -0700166 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700167}
168
Romain Guyb025b9c2010-09-16 14:16:48 -0700169void OpenGLRenderer::finish() {
170#if DEBUG_OPENGL
171 GLenum status = GL_NO_ERROR;
172 while ((status = glGetError()) != GL_NO_ERROR) {
173 LOGD("GL error from OpenGLRenderer: 0x%x", status);
174 }
175#endif
176}
177
Romain Guyda8532c2010-08-31 11:50:35 -0700178void OpenGLRenderer::acquireContext() {
179 if (mCaches.currentProgram) {
180 if (mCaches.currentProgram->isInUse()) {
181 mCaches.currentProgram->remove();
182 mCaches.currentProgram = NULL;
183 }
184 }
185}
186
187void OpenGLRenderer::releaseContext() {
Romain Guyf607bdc2010-09-10 19:20:06 -0700188 glViewport(0, 0, mWidth, mHeight);
Romain Guyda8532c2010-08-31 11:50:35 -0700189
190 glEnable(GL_SCISSOR_TEST);
191 setScissorFromClip();
192
Romain Guyf607bdc2010-09-10 19:20:06 -0700193 glDisable(GL_DITHER);
194
195 glBindFramebuffer(GL_FRAMEBUFFER, 0);
196
Romain Guyda8532c2010-08-31 11:50:35 -0700197 if (mCaches.blend) {
198 glEnable(GL_BLEND);
199 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
Romain Guyf607bdc2010-09-10 19:20:06 -0700200 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700201 } else {
202 glDisable(GL_BLEND);
203 }
204}
205
Romain Guyf6a11b82010-06-23 17:47:49 -0700206///////////////////////////////////////////////////////////////////////////////
207// State management
208///////////////////////////////////////////////////////////////////////////////
209
Romain Guybb9524b2010-06-22 18:56:38 -0700210int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700211 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700212}
213
214int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700215 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700216}
217
218void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700219 if (mSaveCount > 1) {
220 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700221 }
Romain Guybb9524b2010-06-22 18:56:38 -0700222}
223
224void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700225 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700226
Romain Guy8fb95422010-08-17 18:38:51 -0700227 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700228 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700229 }
Romain Guybb9524b2010-06-22 18:56:38 -0700230}
231
Romain Guy8aef54f2010-09-01 15:13:49 -0700232int OpenGLRenderer::saveSnapshot(int flags) {
233 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700234 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700235}
236
237bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700238 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700239 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guybb9524b2010-06-22 18:56:38 -0700240
Romain Guybd6b79b2010-06-26 00:13:53 -0700241 sp<Snapshot> current = mSnapshot;
242 sp<Snapshot> previous = mSnapshot->previous;
243
Romain Guy8b55f372010-08-18 17:10:07 -0700244 mSaveCount--;
245 mSnapshot = previous;
246
Romain Guybd6b79b2010-06-26 00:13:53 -0700247 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700248 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700249 }
250
Romain Guy2542d192010-08-18 11:47:12 -0700251 if (restoreClip) {
252 setScissorFromClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700253 }
Romain Guy2542d192010-08-18 11:47:12 -0700254
255 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700256}
257
Romain Guyf6a11b82010-06-23 17:47:49 -0700258///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700259// Layers
260///////////////////////////////////////////////////////////////////////////////
261
262int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
263 const SkPaint* p, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700264 int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700265
266 int alpha = 255;
267 SkXfermode::Mode mode;
268
269 if (p) {
270 alpha = p->getAlpha();
Romain Guya5aed0d2010-09-09 14:42:43 -0700271 if (!mExtensions.hasFramebufferFetch()) {
272 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
273 if (!isMode) {
274 // Assume SRC_OVER
275 mode = SkXfermode::kSrcOver_Mode;
276 }
277 } else {
278 mode = getXfermode(p->getXfermode());
Romain Guyd55a8612010-06-28 17:42:46 -0700279 }
280 } else {
281 mode = SkXfermode::kSrcOver_Mode;
282 }
283
Romain Guyf607bdc2010-09-10 19:20:06 -0700284 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700285
286 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700287}
288
289int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
290 int alpha, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700291 if (alpha == 0xff) {
292 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700293 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700294 SkPaint paint;
295 paint.setAlpha(alpha);
296 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700297 }
Romain Guyd55a8612010-06-28 17:42:46 -0700298}
Romain Guybd6b79b2010-06-26 00:13:53 -0700299
Romain Guy1c740bc2010-09-13 18:00:09 -0700300/**
301 * Layers are viewed by Skia are slightly different than layers in image editing
302 * programs (for instance.) When a layer is created, previously created layers
303 * and the frame buffer still receive every drawing command. For instance, if a
304 * layer is created and a shape intersecting the bounds of the layers and the
305 * framebuffer is draw, the shape will be drawn on both (unless the layer was
306 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
307 *
308 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
309 * texture. Unfortunately, this is inefficient as it requires every primitive to
310 * be drawn n + 1 times, where n is the number of active layers. In practice this
311 * means, for every primitive:
312 * - Switch active frame buffer
313 * - Change viewport, clip and projection matrix
314 * - Issue the drawing
315 *
316 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
317 * To avoid this, layers are implemented in a different way here.
318 *
319 * This implementation relies on the frame buffer being at least RGBA 8888. When
320 * a layer is created, only a texture is created, not an FBO. The content of the
321 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700322 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700323 * buffer and drawing continues as normal. This technique therefore treats the
324 * frame buffer as a scratch buffer for the layers.
325 *
326 * To compose the layers back onto the frame buffer, each layer texture
327 * (containing the original frame buffer data) is drawn as a simple quad over
328 * the frame buffer. The trick is that the quad is set as the composition
329 * destination in the blending equation, and the frame buffer becomes the source
330 * of the composition.
331 *
332 * Drawing layers with an alpha value requires an extra step before composition.
333 * An empty quad is drawn over the layer's region in the frame buffer. This quad
334 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
335 * quad is used to multiply the colors in the frame buffer. This is achieved by
336 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
337 * GL_ZERO, GL_SRC_ALPHA.
338 *
339 * Because glCopyTexImage2D() can be slow, an alternative implementation might
340 * be use to draw a single clipped layer. The implementation described above
341 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700342 *
343 * (1) The frame buffer is actually not cleared right away. To allow the GPU
344 * to potentially optimize series of calls to glCopyTexImage2D, the frame
345 * buffer is left untouched until the first drawing operation. Only when
346 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700347 */
Romain Guyd55a8612010-06-28 17:42:46 -0700348bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
349 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700350 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700351 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700352
Romain Guyf607bdc2010-09-10 19:20:06 -0700353 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700354 Rect bounds(left, top, right, bottom);
Romain Guyf607bdc2010-09-10 19:20:06 -0700355 mSnapshot->transform->mapRect(bounds);
Romain Guy8aef54f2010-09-01 15:13:49 -0700356
Romain Guy8411f332010-09-13 17:27:57 -0700357 // Layers only make sense if they are in the framebuffer's bounds
358 bounds.intersect(*mSnapshot->clipRect);
Romain Guybf434112010-09-16 14:40:17 -0700359 bounds.snapToPixelBoundaries();
360
Romain Guyb025b9c2010-09-16 14:16:48 -0700361 if (bounds.isEmpty() || bounds.getWidth() > mMaxTextureSize ||
362 bounds.getHeight() > mMaxTextureSize) {
363 return false;
364 }
Romain Guyf18fd992010-07-08 11:45:51 -0700365
Romain Guy8411f332010-09-13 17:27:57 -0700366 LayerSize size(bounds.getWidth(), bounds.getHeight());
Romain Guyf607bdc2010-09-10 19:20:06 -0700367 Layer* layer = mCaches.layerCache.get(size);
Romain Guydda570202010-07-06 11:39:32 -0700368 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700369 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700370 }
371
Romain Guydda570202010-07-06 11:39:32 -0700372 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700373 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700374 layer->layer.set(bounds);
Romain Guydda570202010-07-06 11:39:32 -0700375
Romain Guy8fb95422010-08-17 18:38:51 -0700376 // Save the layer in the snapshot
377 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700378 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700379
Romain Guyf607bdc2010-09-10 19:20:06 -0700380 // Copy the framebuffer into the layer
381 glBindTexture(GL_TEXTURE_2D, layer->texture);
382 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
383 bounds.getWidth(), bounds.getHeight(), 0);
384
Romain Guyf607bdc2010-09-10 19:20:06 -0700385 if (flags & SkCanvas::kClipToLayer_SaveFlag) {
Romain Guy86942302010-09-12 13:02:16 -0700386 if (mSnapshot->clipTransformed(bounds)) setScissorFromClip();
Romain Guyf607bdc2010-09-10 19:20:06 -0700387 }
388
Romain Guy86942302010-09-12 13:02:16 -0700389 // Enqueue the buffer coordinates to clear the corresponding region later
390 mLayers.push(new Rect(bounds));
Romain Guyf86ef572010-07-01 11:05:42 -0700391
Romain Guyd55a8612010-06-28 17:42:46 -0700392 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700393}
394
Romain Guy1c740bc2010-09-13 18:00:09 -0700395/**
396 * Read the documentation of createLayer() before doing anything in this method.
397 */
Romain Guy1d83e192010-08-17 11:37:00 -0700398void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
399 if (!current->layer) {
400 LOGE("Attempting to compose a layer that does not exist");
401 return;
402 }
403
Romain Guy1d83e192010-08-17 11:37:00 -0700404 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700405 const Rect& clip = *previous->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700406 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700407
408 Layer* layer = current->layer;
409 const Rect& rect = layer->layer;
410
Romain Guyf607bdc2010-09-10 19:20:06 -0700411 if (layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700412 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700413 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guyf607bdc2010-09-10 19:20:06 -0700414 }
415
416 // Layers are already drawn with a top-left origin, don't flip the texture
Romain Guy8b55f372010-08-18 17:10:07 -0700417 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
418
Romain Guyf607bdc2010-09-10 19:20:06 -0700419 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
420 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
Romain Guy6820ac82010-09-15 18:11:50 -0700421 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, true, true);
Romain Guy1d83e192010-08-17 11:37:00 -0700422
Romain Guy8b55f372010-08-18 17:10:07 -0700423 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
424
Romain Guy1d83e192010-08-17 11:37:00 -0700425 LayerSize size(rect.getWidth(), rect.getHeight());
426 // Failing to add the layer to the cache should happen only if the
427 // layer is too large
Romain Guyfb8b7632010-08-23 21:05:08 -0700428 if (!mCaches.layerCache.put(size, layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700429 LAYER_LOGD("Deleting layer");
430
Romain Guy1d83e192010-08-17 11:37:00 -0700431 glDeleteTextures(1, &layer->texture);
432
433 delete layer;
434 }
435}
436
Romain Guy86942302010-09-12 13:02:16 -0700437void OpenGLRenderer::clearLayerRegions() {
438 if (mLayers.size() == 0) return;
439
440 for (uint32_t i = 0; i < mLayers.size(); i++) {
441 Rect* bounds = mLayers.itemAt(i);
442
443 // Clear the framebuffer where the layer will draw
444 glScissor(bounds->left, mHeight - bounds->bottom,
445 bounds->getWidth(), bounds->getHeight());
446 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
447 glClear(GL_COLOR_BUFFER_BIT);
448
449 delete bounds;
450 }
451 mLayers.clear();
452
453 // Restore the clip
454 setScissorFromClip();
455}
456
Romain Guybd6b79b2010-06-26 00:13:53 -0700457///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700458// Transforms
459///////////////////////////////////////////////////////////////////////////////
460
461void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700462 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700463}
464
465void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700466 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700467}
468
469void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700470 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700471}
472
473void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700474 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700475}
476
477void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700478 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700479}
480
481void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700482 mat4 m(*matrix);
Romain Guy8aef54f2010-09-01 15:13:49 -0700483 mSnapshot->transform->multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700484}
485
486///////////////////////////////////////////////////////////////////////////////
487// Clipping
488///////////////////////////////////////////////////////////////////////////////
489
Romain Guybb9524b2010-06-22 18:56:38 -0700490void OpenGLRenderer::setScissorFromClip() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700491 const Rect& clip = *mSnapshot->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700492 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700493}
494
495const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700496 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700497}
498
Romain Guyc7d53492010-06-25 13:41:57 -0700499bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy1d83e192010-08-17 11:37:00 -0700500 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700501 mSnapshot->transform->mapRect(r);
502 return !mSnapshot->clipRect->intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700503}
504
Romain Guy079ba2c2010-07-16 14:12:24 -0700505bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
506 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700507 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700508 setScissorFromClip();
509 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700510 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700511}
512
Romain Guyf6a11b82010-06-23 17:47:49 -0700513///////////////////////////////////////////////////////////////////////////////
514// Drawing
515///////////////////////////////////////////////////////////////////////////////
516
Romain Guyc1396e92010-06-30 17:56:19 -0700517void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700518 const float right = left + bitmap->width();
519 const float bottom = top + bitmap->height();
520
521 if (quickReject(left, top, right, bottom)) {
522 return;
523 }
524
Romain Guy61c8c9c2010-08-09 20:48:09 -0700525 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700526 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700527 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700528 const AutoTexture autoCleanup(texture);
529
Romain Guy6926c722010-07-12 20:20:03 -0700530 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700531}
532
Romain Guyf86ef572010-07-01 11:05:42 -0700533void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
534 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
535 const mat4 transform(*matrix);
536 transform.mapRect(r);
537
Romain Guy6926c722010-07-12 20:20:03 -0700538 if (quickReject(r.left, r.top, r.right, r.bottom)) {
539 return;
540 }
541
Romain Guy61c8c9c2010-08-09 20:48:09 -0700542 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700543 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700544 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700545 const AutoTexture autoCleanup(texture);
546
Romain Guy82ba8142010-07-09 13:25:56 -0700547 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700548}
549
Romain Guy8ba548f2010-06-30 19:21:21 -0700550void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
551 float srcLeft, float srcTop, float srcRight, float srcBottom,
552 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700553 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700554 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
555 return;
556 }
557
Romain Guy61c8c9c2010-08-09 20:48:09 -0700558 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700559 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700560 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700561 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700562
Romain Guy8ba548f2010-06-30 19:21:21 -0700563 const float width = texture->width;
564 const float height = texture->height;
565
566 const float u1 = srcLeft / width;
567 const float v1 = srcTop / height;
568 const float u2 = srcRight / width;
569 const float v2 = srcBottom / height;
570
571 resetDrawTextureTexCoords(u1, v1, u2, v2);
572
Romain Guy82ba8142010-07-09 13:25:56 -0700573 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700574
575 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
576}
577
Romain Guydeba7852010-07-07 17:54:48 -0700578void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
579 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700580 if (quickReject(left, top, right, bottom)) {
581 return;
582 }
583
Romain Guy61c8c9c2010-08-09 20:48:09 -0700584 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700585 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700586 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700587 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700588
589 int alpha;
590 SkXfermode::Mode mode;
591 getAlphaAndMode(paint, &alpha, &mode);
592
Romain Guyfb8b7632010-08-23 21:05:08 -0700593 Patch* mesh = mCaches.patchCache.get(patch);
Romain Guy759ea802010-09-16 20:49:46 -0700594 mesh->updateVertices(bitmap->width(), bitmap->height(),left, top, right, bottom,
Romain Guyfb5e23c2010-07-09 13:52:56 -0700595 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700596
597 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
598 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700599 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
600 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy6820ac82010-09-15 18:11:50 -0700601 &mesh->vertices[0].texture[0], GL_TRIANGLES, mesh->verticesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700602}
603
Romain Guy759ea802010-09-16 20:49:46 -0700604void OpenGLRenderer::drawLines(float* points, int count, const SkPaint* paint) {
605 int alpha;
606 SkXfermode::Mode mode;
607 getAlphaAndMode(paint, &alpha, &mode);
608
609 uint32_t color = paint->getColor();
610 const GLfloat a = alpha / 255.0f;
611 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
612 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
613 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
614
615 GLuint textureUnit = 0;
616 setupTextureAlpha8(mLine.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
617 mode, false, true, mLine.getVertices(), mLine.getTexCoords());
618
619 for (int i = 0; i < count; i += 4) {
620 float tx = 0.0f;
621 float ty = 0.0f;
622
623 mLine.update(points[i], points[i + 1], points[i + 2], points[i + 3],
624 paint->getStrokeWidth(), tx, ty);
625
626 const float dx = points[i + 2] - points[i];
627 const float dy = points[i + 3] - points[i + 1];
628 const float mag = sqrtf(dx * dx + dy * dy);
629 const float angle = acos(dx / mag);
630
631 mModelView.loadTranslate(points[i], points[i + 1], 0.0f);
632 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) {
633 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f);
634 }
635 mModelView.translate(tx, ty, 0.0f);
636 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
637
638 if (mShader) {
639 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot);
640 }
641
642 glDrawArrays(GL_TRIANGLES, 0, mLine.getElementsCount());
643 }
644
645 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
646}
647
Romain Guy85bf02f2010-06-22 13:11:24 -0700648void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700649 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700650 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700651}
Romain Guy9d5316e2010-06-24 19:30:36 -0700652
Romain Guybd6b79b2010-06-26 00:13:53 -0700653void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700654 if (quickReject(left, top, right, bottom)) {
655 return;
656 }
657
Romain Guy026c5e162010-06-28 17:12:22 -0700658 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700659 if (!mExtensions.hasFramebufferFetch()) {
660 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
661 if (!isMode) {
662 // Assume SRC_OVER
663 mode = SkXfermode::kSrcOver_Mode;
664 }
665 } else {
666 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700667 }
668
669 // Skia draws using the color's alpha channel if < 255
670 // Otherwise, it uses the paint's alpha
671 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700672 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700673 color |= p->getAlpha() << 24;
674 }
675
676 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700677}
Romain Guy9d5316e2010-06-24 19:30:36 -0700678
Romain Guye8e62a42010-07-23 18:55:21 -0700679void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
680 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700681 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700682 return;
683 }
Romain Guyf607bdc2010-09-10 19:20:06 -0700684 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700685
Romain Guya674ab72010-08-10 17:26:42 -0700686 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700687 switch (paint->getTextAlign()) {
688 case SkPaint::kCenter_Align:
689 length = paint->measureText(text, bytesCount);
690 x -= length / 2.0f;
691 break;
692 case SkPaint::kRight_Align:
693 length = paint->measureText(text, bytesCount);
694 x -= length;
695 break;
696 default:
697 break;
698 }
699
Romain Guy694b5192010-07-21 21:33:20 -0700700 int alpha;
701 SkXfermode::Mode mode;
702 getAlphaAndMode(paint, &alpha, &mode);
703
Romain Guy2542d192010-08-18 11:47:12 -0700704 uint32_t color = paint->getColor();
705 const GLfloat a = alpha / 255.0f;
706 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
707 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
708 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
709
Romain Guyb45c0c92010-08-26 20:35:23 -0700710 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
711 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700712 paint->getTextSize());
Romain Guy1e45aae2010-08-13 19:39:53 -0700713 if (mHasShadow) {
714 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700715 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700716 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700717 count, mShadowRadius);
718 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700719
Romain Guy2542d192010-08-18 11:47:12 -0700720 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700721
722 // Draw the mesh
723 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700724 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700725 }
726
Romain Guy06f96e22010-07-30 19:18:16 -0700727 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700728 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700729
Romain Guyb45c0c92010-08-26 20:35:23 -0700730 setupTextureAlpha8(fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
Romain Guy0a417492010-08-16 20:26:20 -0700731 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700732
Romain Guy09147fb2010-07-22 13:08:20 -0700733 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy86942302010-09-12 13:02:16 -0700734 clearLayerRegions();
Romain Guyb45c0c92010-08-26 20:35:23 -0700735 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700736
737 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700738 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700739
Romain Guy0a417492010-08-16 20:26:20 -0700740 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -0700741}
742
Romain Guy7fbcc042010-08-04 15:40:07 -0700743void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
744 GLuint textureUnit = 0;
745 glActiveTexture(gTextureUnits[textureUnit]);
746
Romain Guyfb8b7632010-08-23 21:05:08 -0700747 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700748 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700749 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700750
Romain Guy8b55f372010-08-18 17:10:07 -0700751 const float x = texture->left - texture->offset;
752 const float y = texture->top - texture->offset;
753
754 if (quickReject(x, y, x + texture->width, y + texture->height)) {
755 return;
756 }
757
Romain Guy7fbcc042010-08-04 15:40:07 -0700758 int alpha;
759 SkXfermode::Mode mode;
760 getAlphaAndMode(paint, &alpha, &mode);
761
762 uint32_t color = paint->getColor();
763 const GLfloat a = alpha / 255.0f;
764 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
765 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
766 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
767
Romain Guy0a417492010-08-16 20:26:20 -0700768 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
769
Romain Guy86942302010-09-12 13:02:16 -0700770 clearLayerRegions();
771
Romain Guy0a417492010-08-16 20:26:20 -0700772 // Draw the mesh
773 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700774 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700775}
776
Romain Guy6926c722010-07-12 20:20:03 -0700777///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700778// Shaders
779///////////////////////////////////////////////////////////////////////////////
780
781void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700782 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700783}
784
Romain Guy06f96e22010-07-30 19:18:16 -0700785void OpenGLRenderer::setupShader(SkiaShader* shader) {
786 mShader = shader;
787 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700788 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700789 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700790}
791
Romain Guyd27977d2010-07-14 19:18:51 -0700792///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700793// Color filters
794///////////////////////////////////////////////////////////////////////////////
795
796void OpenGLRenderer::resetColorFilter() {
797 mColorFilter = NULL;
798}
799
800void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
801 mColorFilter = filter;
802}
803
804///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700805// Drop shadow
806///////////////////////////////////////////////////////////////////////////////
807
808void OpenGLRenderer::resetShadow() {
809 mHasShadow = false;
810}
811
812void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
813 mHasShadow = true;
814 mShadowRadius = radius;
815 mShadowDx = dx;
816 mShadowDy = dy;
817 mShadowColor = color;
818}
819
820///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700821// Drawing implementation
822///////////////////////////////////////////////////////////////////////////////
823
Romain Guy0a417492010-08-16 20:26:20 -0700824void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700825 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700826 const float sx = x - texture->left + mShadowDx;
827 const float sy = y - texture->top + mShadowDy;
828
Romain Guy2542d192010-08-18 11:47:12 -0700829 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
830 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700831 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
832 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
833 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
834
835 GLuint textureUnit = 0;
836 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
837}
838
839void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
840 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
841 bool transforms, bool applyFilters) {
842 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -0700843 x, y, r, g, b, a, mode, transforms, applyFilters,
844 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
Romain Guy0a417492010-08-16 20:26:20 -0700845}
846
847void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
848 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
849 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy759ea802010-09-16 20:49:46 -0700850 setupTextureAlpha8(texture, width, height, textureUnit,
851 x, y, r, g, b, a, mode, transforms, applyFilters,
852 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
853}
854
855void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
856 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
857 SkXfermode::Mode mode, bool transforms, bool applyFilters,
858 GLvoid* vertices, GLvoid* texCoords) {
Romain Guy0a417492010-08-16 20:26:20 -0700859 // Describe the required shaders
860 ProgramDescription description;
861 description.hasTexture = true;
862 description.hasAlpha8Texture = true;
863
864 if (applyFilters) {
865 if (mShader) {
866 mShader->describe(description, mExtensions);
867 }
868 if (mColorFilter) {
869 mColorFilter->describe(description, mExtensions);
870 }
871 }
872
Romain Guya5aed0d2010-09-09 14:42:43 -0700873 // Setup the blending mode
874 chooseBlending(true, mode, description);
875
Romain Guy0a417492010-08-16 20:26:20 -0700876 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700877 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700878
Romain Guy0a417492010-08-16 20:26:20 -0700879 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -0700880 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700881
Romain Guyfb8b7632010-08-23 21:05:08 -0700882 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -0700883 glEnableVertexAttribArray(texCoordsSlot);
884
885 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700886 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -0700887 gMeshStride, vertices);
Romain Guy0a417492010-08-16 20:26:20 -0700888 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -0700889 gMeshStride, texCoords);
Romain Guy0a417492010-08-16 20:26:20 -0700890
891 // Setup uniforms
892 if (transforms) {
893 mModelView.loadTranslate(x, y, 0.0f);
894 mModelView.scale(width, height, 1.0f);
895 } else {
896 mModelView.loadIdentity();
897 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700898 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyfb8b7632010-08-23 21:05:08 -0700899 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -0700900
901 textureUnit++;
902 if (applyFilters) {
903 // Setup attributes and uniforms required by the shaders
904 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700905 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700906 }
907 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700908 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -0700909 }
910 }
911}
912
Romain Guyf607bdc2010-09-10 19:20:06 -0700913// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -0700914#define kStdStrikeThru_Offset (-6.0f / 21.0f)
915#define kStdUnderline_Offset (1.0f / 9.0f)
916#define kStdUnderline_Thickness (1.0f / 18.0f)
917
918void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
919 float x, float y, SkPaint* paint) {
920 // Handle underline and strike-through
921 uint32_t flags = paint->getFlags();
922 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
923 float underlineWidth = length;
924 // If length is > 0.0f, we already measured the text for the text alignment
925 if (length <= 0.0f) {
926 underlineWidth = paint->measureText(text, bytesCount);
927 }
928
929 float offsetX = 0;
930 switch (paint->getTextAlign()) {
931 case SkPaint::kCenter_Align:
932 offsetX = underlineWidth * 0.5f;
933 break;
934 case SkPaint::kRight_Align:
935 offsetX = underlineWidth;
936 break;
937 default:
938 break;
939 }
940
941 if (underlineWidth > 0.0f) {
942 float textSize = paint->getTextSize();
943 float height = textSize * kStdUnderline_Thickness;
944
945 float left = x - offsetX;
946 float top = 0.0f;
947 float right = left + underlineWidth;
948 float bottom = 0.0f;
949
950 if (flags & SkPaint::kUnderlineText_Flag) {
951 top = y + textSize * kStdUnderline_Offset;
952 bottom = top + height;
953 drawRect(left, top, right, bottom, paint);
954 }
955
956 if (flags & SkPaint::kStrikeThruText_Flag) {
957 top = y + textSize * kStdStrikeThru_Offset;
958 bottom = top + height;
959 drawRect(left, top, right, bottom, paint);
960 }
961 }
962 }
963}
964
Romain Guy026c5e162010-06-28 17:12:22 -0700965void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700966 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -0700967 clearLayerRegions();
968
Romain Guyd27977d2010-07-14 19:18:51 -0700969 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700970 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700971 color |= 0x00ffffff;
972 }
973
974 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700975 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700976 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700977 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
978 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
979 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
980
Romain Guy06f96e22010-07-30 19:18:16 -0700981 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700982
Romain Guy06f96e22010-07-30 19:18:16 -0700983 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700984 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700985 if (mShader) {
986 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700987 }
Romain Guydb1938e2010-08-02 18:50:22 -0700988 if (mColorFilter) {
989 mColorFilter->describe(description, mExtensions);
990 }
Romain Guyd27977d2010-07-14 19:18:51 -0700991
Romain Guy1c740bc2010-09-13 18:00:09 -0700992 // Setup the blending mode
993 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -0700994
Romain Guy06f96e22010-07-30 19:18:16 -0700995 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700996 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -0700997
998 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700999 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -07001000 gMeshStride, &mMeshVertices[0].position[0]);
1001
1002 // Setup uniforms
1003 mModelView.loadTranslate(left, top, 0.0f);
1004 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -07001005 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001006 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -07001007 } else {
1008 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -07001009 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -07001010 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001011 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -07001012
Romain Guy06f96e22010-07-30 19:18:16 -07001013 // Setup attributes and uniforms required by the shaders
1014 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001015 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001016 }
Romain Guydb1938e2010-08-02 18:50:22 -07001017 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001018 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001019 }
Romain Guyc0ac1932010-07-19 18:43:02 -07001020
Romain Guy06f96e22010-07-30 19:18:16 -07001021 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -07001022 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -07001023}
1024
Romain Guy82ba8142010-07-09 13:25:56 -07001025void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001026 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001027 int alpha;
1028 SkXfermode::Mode mode;
1029 getAlphaAndMode(paint, &alpha, &mode);
1030
Romain Guy6820ac82010-09-15 18:11:50 -07001031 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1032 texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1033 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -07001034}
1035
Romain Guybd6b79b2010-06-26 00:13:53 -07001036void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001037 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1038 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001039 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1040 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001041}
1042
1043void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001044 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001045 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guyf607bdc2010-09-10 19:20:06 -07001046 bool swapSrcDst, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001047 clearLayerRegions();
1048
Romain Guy889f8d12010-07-29 14:37:42 -07001049 ProgramDescription description;
1050 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -07001051 if (mColorFilter) {
1052 mColorFilter->describe(description, mExtensions);
1053 }
Romain Guy889f8d12010-07-29 14:37:42 -07001054
Romain Guybd6b79b2010-06-26 00:13:53 -07001055 mModelView.loadTranslate(left, top, 0.0f);
1056 mModelView.scale(right - left, bottom - top, 1.0f);
1057
Romain Guyf607bdc2010-09-10 19:20:06 -07001058 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001059
Romain Guyfb8b7632010-08-23 21:05:08 -07001060 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001061 if (!ignoreTransform) {
1062 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1063 } else {
1064 mat4 m;
1065 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
1066 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001067
Romain Guy889f8d12010-07-29 14:37:42 -07001068 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -07001069 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001070 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001071
Romain Guya9794742010-07-13 11:37:54 -07001072 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -07001073 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -07001074
Romain Guy889f8d12010-07-29 14:37:42 -07001075 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001076 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001077 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -07001078 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001079 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001080 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001081
Romain Guydb1938e2010-08-02 18:50:22 -07001082 // Color filter
1083 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001084 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001085 }
1086
Romain Guy6820ac82010-09-15 18:11:50 -07001087 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001088 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001089}
1090
Romain Guya5aed0d2010-09-09 14:42:43 -07001091void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001092 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001093 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1094 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001095 if (mode < SkXfermode::kPlus_Mode) {
1096 if (!mCaches.blend) {
1097 glEnable(GL_BLEND);
1098 }
Romain Guy82ba8142010-07-09 13:25:56 -07001099
Romain Guyf607bdc2010-09-10 19:20:06 -07001100 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1101 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001102
Romain Guya5aed0d2010-09-09 14:42:43 -07001103 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1104 glBlendFunc(sourceMode, destMode);
1105 mCaches.lastSrcMode = sourceMode;
1106 mCaches.lastDstMode = destMode;
1107 }
1108 } else {
1109 // These blend modes are not supported by OpenGL directly and have
1110 // to be implemented using shaders. Since the shader will perform
1111 // the blending, turn blending off here
1112 if (mExtensions.hasFramebufferFetch()) {
1113 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001114 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001115 }
1116
1117 if (mCaches.blend) {
1118 glDisable(GL_BLEND);
1119 }
1120 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001121 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001122 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001123 glDisable(GL_BLEND);
1124 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001125 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001126}
1127
Romain Guy889f8d12010-07-29 14:37:42 -07001128bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001129 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001130 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001131 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001132 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001133 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001134 }
Romain Guy6926c722010-07-12 20:20:03 -07001135 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001136}
1137
Romain Guy026c5e162010-06-28 17:12:22 -07001138void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001139 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001140 TextureVertex::setUV(v++, u1, v1);
1141 TextureVertex::setUV(v++, u2, v1);
1142 TextureVertex::setUV(v++, u1, v2);
1143 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001144}
1145
1146void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1147 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001148 if (!mExtensions.hasFramebufferFetch()) {
1149 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1150 if (!isMode) {
1151 // Assume SRC_OVER
1152 *mode = SkXfermode::kSrcOver_Mode;
1153 }
1154 } else {
1155 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001156 }
1157
1158 // Skia draws using the color's alpha channel if < 255
1159 // Otherwise, it uses the paint's alpha
1160 int color = paint->getColor();
1161 *alpha = (color >> 24) & 0xFF;
1162 if (*alpha == 255) {
1163 *alpha = paint->getAlpha();
1164 }
1165 } else {
1166 *mode = SkXfermode::kSrcOver_Mode;
1167 *alpha = 255;
1168 }
Romain Guy026c5e162010-06-28 17:12:22 -07001169}
1170
Romain Guya5aed0d2010-09-09 14:42:43 -07001171SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1172 if (mode == NULL) {
1173 return SkXfermode::kSrcOver_Mode;
1174 }
1175 return mode->fMode;
1176}
1177
Romain Guy889f8d12010-07-29 14:37:42 -07001178void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1179 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001180 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001181 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1182 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1183}
1184
Romain Guy9d5316e2010-06-24 19:30:36 -07001185}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001186}; // namespace android