blob: eb85169e9471ec34b9645a28f3f0e769c2b6da1d [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
Romain Guyc95c8d62010-09-17 15:31:32 -0700615 const bool isAA = paint->isAntiAlias();
616 if (isAA) {
617 GLuint textureUnit = 0;
618 setupTextureAlpha8(mLine.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
619 mode, false, true, mLine.getVertices(), mLine.getTexCoords());
620 } else {
621 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false);
622 }
623
624 const float strokeWidth = paint->getStrokeWidth();
625 const GLsizei elementsCount = isAA ? mLine.getElementsCount() : gMeshCount;
626 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
Romain Guy759ea802010-09-16 20:49:46 -0700627
628 for (int i = 0; i < count; i += 4) {
629 float tx = 0.0f;
630 float ty = 0.0f;
631
Romain Guyc95c8d62010-09-17 15:31:32 -0700632 if (isAA) {
633 mLine.update(points[i], points[i + 1], points[i + 2], points[i + 3],
634 strokeWidth, tx, ty);
635 } else {
636 ty = -strokeWidth * 0.5f;
637 }
Romain Guy759ea802010-09-16 20:49:46 -0700638
639 const float dx = points[i + 2] - points[i];
640 const float dy = points[i + 3] - points[i + 1];
641 const float mag = sqrtf(dx * dx + dy * dy);
642 const float angle = acos(dx / mag);
643
644 mModelView.loadTranslate(points[i], points[i + 1], 0.0f);
645 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) {
646 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f);
647 }
648 mModelView.translate(tx, ty, 0.0f);
Romain Guyc95c8d62010-09-17 15:31:32 -0700649 if (!isAA) {
650 float length = mLine.getLength(points[i], points[i + 1], points[i + 2], points[i + 3]);
651 mModelView.scale(length, strokeWidth, 1.0f);
652 }
Romain Guy759ea802010-09-16 20:49:46 -0700653 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
654
655 if (mShader) {
656 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot);
657 }
658
Romain Guyc95c8d62010-09-17 15:31:32 -0700659 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy759ea802010-09-16 20:49:46 -0700660 }
661
662 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
663}
664
Romain Guy85bf02f2010-06-22 13:11:24 -0700665void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700666 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700667 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700668}
Romain Guy9d5316e2010-06-24 19:30:36 -0700669
Romain Guybd6b79b2010-06-26 00:13:53 -0700670void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700671 if (quickReject(left, top, right, bottom)) {
672 return;
673 }
674
Romain Guy026c5e162010-06-28 17:12:22 -0700675 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700676 if (!mExtensions.hasFramebufferFetch()) {
677 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
678 if (!isMode) {
679 // Assume SRC_OVER
680 mode = SkXfermode::kSrcOver_Mode;
681 }
682 } else {
683 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700684 }
685
686 // Skia draws using the color's alpha channel if < 255
687 // Otherwise, it uses the paint's alpha
688 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700689 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700690 color |= p->getAlpha() << 24;
691 }
692
693 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700694}
Romain Guy9d5316e2010-06-24 19:30:36 -0700695
Romain Guye8e62a42010-07-23 18:55:21 -0700696void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
697 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700698 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700699 return;
700 }
Romain Guyf607bdc2010-09-10 19:20:06 -0700701 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700702
Romain Guya674ab72010-08-10 17:26:42 -0700703 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700704 switch (paint->getTextAlign()) {
705 case SkPaint::kCenter_Align:
706 length = paint->measureText(text, bytesCount);
707 x -= length / 2.0f;
708 break;
709 case SkPaint::kRight_Align:
710 length = paint->measureText(text, bytesCount);
711 x -= length;
712 break;
713 default:
714 break;
715 }
716
Romain Guy694b5192010-07-21 21:33:20 -0700717 int alpha;
718 SkXfermode::Mode mode;
719 getAlphaAndMode(paint, &alpha, &mode);
720
Romain Guy2542d192010-08-18 11:47:12 -0700721 uint32_t color = paint->getColor();
722 const GLfloat a = alpha / 255.0f;
723 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
724 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
725 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
726
Romain Guyb45c0c92010-08-26 20:35:23 -0700727 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
728 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700729 paint->getTextSize());
Romain Guy1e45aae2010-08-13 19:39:53 -0700730 if (mHasShadow) {
731 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700732 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700733 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700734 count, mShadowRadius);
735 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700736
Romain Guy2542d192010-08-18 11:47:12 -0700737 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700738
739 // Draw the mesh
740 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700741 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700742 }
743
Romain Guy06f96e22010-07-30 19:18:16 -0700744 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700745 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700746
Romain Guyb45c0c92010-08-26 20:35:23 -0700747 setupTextureAlpha8(fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
Romain Guy0a417492010-08-16 20:26:20 -0700748 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700749
Romain Guy09147fb2010-07-22 13:08:20 -0700750 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy86942302010-09-12 13:02:16 -0700751 clearLayerRegions();
Romain Guyb45c0c92010-08-26 20:35:23 -0700752 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700753
754 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700755 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700756
Romain Guy0a417492010-08-16 20:26:20 -0700757 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -0700758}
759
Romain Guy7fbcc042010-08-04 15:40:07 -0700760void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
761 GLuint textureUnit = 0;
762 glActiveTexture(gTextureUnits[textureUnit]);
763
Romain Guyfb8b7632010-08-23 21:05:08 -0700764 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700765 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700766 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700767
Romain Guy8b55f372010-08-18 17:10:07 -0700768 const float x = texture->left - texture->offset;
769 const float y = texture->top - texture->offset;
770
771 if (quickReject(x, y, x + texture->width, y + texture->height)) {
772 return;
773 }
774
Romain Guy7fbcc042010-08-04 15:40:07 -0700775 int alpha;
776 SkXfermode::Mode mode;
777 getAlphaAndMode(paint, &alpha, &mode);
778
779 uint32_t color = paint->getColor();
780 const GLfloat a = alpha / 255.0f;
781 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
782 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
783 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
784
Romain Guy0a417492010-08-16 20:26:20 -0700785 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
786
Romain Guy86942302010-09-12 13:02:16 -0700787 clearLayerRegions();
788
Romain Guy0a417492010-08-16 20:26:20 -0700789 // Draw the mesh
790 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700791 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700792}
793
Romain Guy6926c722010-07-12 20:20:03 -0700794///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700795// Shaders
796///////////////////////////////////////////////////////////////////////////////
797
798void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700799 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700800}
801
Romain Guy06f96e22010-07-30 19:18:16 -0700802void OpenGLRenderer::setupShader(SkiaShader* shader) {
803 mShader = shader;
804 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700805 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700806 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700807}
808
Romain Guyd27977d2010-07-14 19:18:51 -0700809///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700810// Color filters
811///////////////////////////////////////////////////////////////////////////////
812
813void OpenGLRenderer::resetColorFilter() {
814 mColorFilter = NULL;
815}
816
817void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
818 mColorFilter = filter;
819}
820
821///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700822// Drop shadow
823///////////////////////////////////////////////////////////////////////////////
824
825void OpenGLRenderer::resetShadow() {
826 mHasShadow = false;
827}
828
829void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
830 mHasShadow = true;
831 mShadowRadius = radius;
832 mShadowDx = dx;
833 mShadowDy = dy;
834 mShadowColor = color;
835}
836
837///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700838// Drawing implementation
839///////////////////////////////////////////////////////////////////////////////
840
Romain Guy0a417492010-08-16 20:26:20 -0700841void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700842 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700843 const float sx = x - texture->left + mShadowDx;
844 const float sy = y - texture->top + mShadowDy;
845
Romain Guy2542d192010-08-18 11:47:12 -0700846 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
847 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700848 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
849 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
850 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
851
852 GLuint textureUnit = 0;
853 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
854}
855
856void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
857 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
858 bool transforms, bool applyFilters) {
859 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -0700860 x, y, r, g, b, a, mode, transforms, applyFilters,
861 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
Romain Guy0a417492010-08-16 20:26:20 -0700862}
863
864void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
865 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
866 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy759ea802010-09-16 20:49:46 -0700867 setupTextureAlpha8(texture, width, height, textureUnit,
868 x, y, r, g, b, a, mode, transforms, applyFilters,
869 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
870}
871
872void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
873 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
874 SkXfermode::Mode mode, bool transforms, bool applyFilters,
875 GLvoid* vertices, GLvoid* texCoords) {
Romain Guy0a417492010-08-16 20:26:20 -0700876 // Describe the required shaders
877 ProgramDescription description;
878 description.hasTexture = true;
879 description.hasAlpha8Texture = true;
880
881 if (applyFilters) {
882 if (mShader) {
883 mShader->describe(description, mExtensions);
884 }
885 if (mColorFilter) {
886 mColorFilter->describe(description, mExtensions);
887 }
888 }
889
Romain Guya5aed0d2010-09-09 14:42:43 -0700890 // Setup the blending mode
891 chooseBlending(true, mode, description);
892
Romain Guy0a417492010-08-16 20:26:20 -0700893 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700894 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700895
Romain Guy0a417492010-08-16 20:26:20 -0700896 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -0700897 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700898
Romain Guyfb8b7632010-08-23 21:05:08 -0700899 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -0700900 glEnableVertexAttribArray(texCoordsSlot);
901
902 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700903 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -0700904 gMeshStride, vertices);
Romain Guy0a417492010-08-16 20:26:20 -0700905 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -0700906 gMeshStride, texCoords);
Romain Guy0a417492010-08-16 20:26:20 -0700907
908 // Setup uniforms
909 if (transforms) {
910 mModelView.loadTranslate(x, y, 0.0f);
911 mModelView.scale(width, height, 1.0f);
912 } else {
913 mModelView.loadIdentity();
914 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700915 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyfb8b7632010-08-23 21:05:08 -0700916 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -0700917
918 textureUnit++;
919 if (applyFilters) {
920 // Setup attributes and uniforms required by the shaders
921 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700922 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700923 }
924 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700925 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -0700926 }
927 }
928}
929
Romain Guyf607bdc2010-09-10 19:20:06 -0700930// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -0700931#define kStdStrikeThru_Offset (-6.0f / 21.0f)
932#define kStdUnderline_Offset (1.0f / 9.0f)
933#define kStdUnderline_Thickness (1.0f / 18.0f)
934
935void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
936 float x, float y, SkPaint* paint) {
937 // Handle underline and strike-through
938 uint32_t flags = paint->getFlags();
939 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
940 float underlineWidth = length;
941 // If length is > 0.0f, we already measured the text for the text alignment
942 if (length <= 0.0f) {
943 underlineWidth = paint->measureText(text, bytesCount);
944 }
945
946 float offsetX = 0;
947 switch (paint->getTextAlign()) {
948 case SkPaint::kCenter_Align:
949 offsetX = underlineWidth * 0.5f;
950 break;
951 case SkPaint::kRight_Align:
952 offsetX = underlineWidth;
953 break;
954 default:
955 break;
956 }
957
958 if (underlineWidth > 0.0f) {
959 float textSize = paint->getTextSize();
960 float height = textSize * kStdUnderline_Thickness;
961
962 float left = x - offsetX;
963 float top = 0.0f;
964 float right = left + underlineWidth;
965 float bottom = 0.0f;
966
967 if (flags & SkPaint::kUnderlineText_Flag) {
968 top = y + textSize * kStdUnderline_Offset;
969 bottom = top + height;
970 drawRect(left, top, right, bottom, paint);
971 }
972
973 if (flags & SkPaint::kStrikeThruText_Flag) {
974 top = y + textSize * kStdStrikeThru_Offset;
975 bottom = top + height;
976 drawRect(left, top, right, bottom, paint);
977 }
978 }
979 }
980}
981
Romain Guy026c5e162010-06-28 17:12:22 -0700982void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700983 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -0700984 clearLayerRegions();
985
Romain Guyd27977d2010-07-14 19:18:51 -0700986 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700987 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700988 color |= 0x00ffffff;
989 }
990
991 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700992 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700993 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700994 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
995 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
996 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
997
Romain Guyc95c8d62010-09-17 15:31:32 -0700998 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform);
999
1000 // Draw the mesh
1001 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1002}
1003
1004void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom,
1005 float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy06f96e22010-07-30 19:18:16 -07001006 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -07001007
Romain Guy06f96e22010-07-30 19:18:16 -07001008 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -07001009 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -07001010 if (mShader) {
1011 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -07001012 }
Romain Guydb1938e2010-08-02 18:50:22 -07001013 if (mColorFilter) {
1014 mColorFilter->describe(description, mExtensions);
1015 }
Romain Guyd27977d2010-07-14 19:18:51 -07001016
Romain Guy1c740bc2010-09-13 18:00:09 -07001017 // Setup the blending mode
Romain Guyc95c8d62010-09-17 15:31:32 -07001018 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -07001019
Romain Guy06f96e22010-07-30 19:18:16 -07001020 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001021 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -07001022
1023 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -07001024 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -07001025 gMeshStride, &mMeshVertices[0].position[0]);
1026
1027 // Setup uniforms
1028 mModelView.loadTranslate(left, top, 0.0f);
1029 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -07001030 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001031 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -07001032 } else {
1033 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -07001034 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -07001035 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001036 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -07001037
Romain Guy06f96e22010-07-30 19:18:16 -07001038 // Setup attributes and uniforms required by the shaders
1039 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001040 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001041 }
Romain Guydb1938e2010-08-02 18:50:22 -07001042 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001043 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001044 }
Romain Guyd27977d2010-07-14 19:18:51 -07001045}
1046
Romain Guy82ba8142010-07-09 13:25:56 -07001047void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001048 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001049 int alpha;
1050 SkXfermode::Mode mode;
1051 getAlphaAndMode(paint, &alpha, &mode);
1052
Romain Guy6820ac82010-09-15 18:11:50 -07001053 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1054 texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1055 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -07001056}
1057
Romain Guybd6b79b2010-06-26 00:13:53 -07001058void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001059 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1060 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001061 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1062 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001063}
1064
1065void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001066 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001067 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guyf607bdc2010-09-10 19:20:06 -07001068 bool swapSrcDst, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001069 clearLayerRegions();
1070
Romain Guy889f8d12010-07-29 14:37:42 -07001071 ProgramDescription description;
1072 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -07001073 if (mColorFilter) {
1074 mColorFilter->describe(description, mExtensions);
1075 }
Romain Guy889f8d12010-07-29 14:37:42 -07001076
Romain Guybd6b79b2010-06-26 00:13:53 -07001077 mModelView.loadTranslate(left, top, 0.0f);
1078 mModelView.scale(right - left, bottom - top, 1.0f);
1079
Romain Guyf607bdc2010-09-10 19:20:06 -07001080 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001081
Romain Guyfb8b7632010-08-23 21:05:08 -07001082 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001083 if (!ignoreTransform) {
1084 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1085 } else {
1086 mat4 m;
1087 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
1088 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001089
Romain Guy889f8d12010-07-29 14:37:42 -07001090 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -07001091 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001092 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001093
Romain Guya9794742010-07-13 11:37:54 -07001094 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -07001095 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -07001096
Romain Guy889f8d12010-07-29 14:37:42 -07001097 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001098 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001099 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -07001100 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001101 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001102 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001103
Romain Guydb1938e2010-08-02 18:50:22 -07001104 // Color filter
1105 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001106 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001107 }
1108
Romain Guy6820ac82010-09-15 18:11:50 -07001109 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001110 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001111}
1112
Romain Guya5aed0d2010-09-09 14:42:43 -07001113void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001114 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001115 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1116 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001117 if (mode < SkXfermode::kPlus_Mode) {
1118 if (!mCaches.blend) {
1119 glEnable(GL_BLEND);
1120 }
Romain Guy82ba8142010-07-09 13:25:56 -07001121
Romain Guyf607bdc2010-09-10 19:20:06 -07001122 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1123 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001124
Romain Guya5aed0d2010-09-09 14:42:43 -07001125 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1126 glBlendFunc(sourceMode, destMode);
1127 mCaches.lastSrcMode = sourceMode;
1128 mCaches.lastDstMode = destMode;
1129 }
1130 } else {
1131 // These blend modes are not supported by OpenGL directly and have
1132 // to be implemented using shaders. Since the shader will perform
1133 // the blending, turn blending off here
1134 if (mExtensions.hasFramebufferFetch()) {
1135 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001136 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001137 }
1138
1139 if (mCaches.blend) {
1140 glDisable(GL_BLEND);
1141 }
1142 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001143 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001144 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001145 glDisable(GL_BLEND);
1146 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001147 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001148}
1149
Romain Guy889f8d12010-07-29 14:37:42 -07001150bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001151 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001152 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001153 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001154 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001155 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001156 }
Romain Guy6926c722010-07-12 20:20:03 -07001157 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001158}
1159
Romain Guy026c5e162010-06-28 17:12:22 -07001160void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001161 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001162 TextureVertex::setUV(v++, u1, v1);
1163 TextureVertex::setUV(v++, u2, v1);
1164 TextureVertex::setUV(v++, u1, v2);
1165 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001166}
1167
1168void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1169 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001170 if (!mExtensions.hasFramebufferFetch()) {
1171 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1172 if (!isMode) {
1173 // Assume SRC_OVER
1174 *mode = SkXfermode::kSrcOver_Mode;
1175 }
1176 } else {
1177 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001178 }
1179
1180 // Skia draws using the color's alpha channel if < 255
1181 // Otherwise, it uses the paint's alpha
1182 int color = paint->getColor();
1183 *alpha = (color >> 24) & 0xFF;
1184 if (*alpha == 255) {
1185 *alpha = paint->getAlpha();
1186 }
1187 } else {
1188 *mode = SkXfermode::kSrcOver_Mode;
1189 *alpha = 255;
1190 }
Romain Guy026c5e162010-06-28 17:12:22 -07001191}
1192
Romain Guya5aed0d2010-09-09 14:42:43 -07001193SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1194 if (mode == NULL) {
1195 return SkXfermode::kSrcOver_Mode;
1196 }
1197 return mode->fMode;
1198}
1199
Romain Guy889f8d12010-07-29 14:37:42 -07001200void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1201 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001202 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001203 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1204 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1205}
1206
Romain Guy9d5316e2010-06-24 19:30:36 -07001207}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001208}; // namespace android