blob: e3790f5cab75f593b81ad155ec397fa2a4ee9c74 [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>
Romain Guye2d345e2010-09-24 18:39:22 -070027#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070028
Romain Guy85bf02f2010-06-22 13:11:24 -070029#include "OpenGLRenderer.h"
Romain Guye4d01122010-06-16 18:44:05 -070030
31namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070032namespace uirenderer {
33
34///////////////////////////////////////////////////////////////////////////////
35// Defines
36///////////////////////////////////////////////////////////////////////////////
37
Romain Guy06f96e22010-07-30 19:18:16 -070038#define REQUIRED_TEXTURE_UNITS_COUNT 3
39
Romain Guydda570202010-07-06 11:39:32 -070040// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070041#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070042
Romain Guy759ea802010-09-16 20:49:46 -070043#define RAD_TO_DEG (180.0f / 3.14159265f)
44#define MIN_ANGLE 0.001f
45
Romain Guy9d5316e2010-06-24 19:30:36 -070046///////////////////////////////////////////////////////////////////////////////
47// Globals
48///////////////////////////////////////////////////////////////////////////////
49
Romain Guy026c5e162010-06-28 17:12:22 -070050// This array is never used directly but used as a memcpy source in the
51// OpenGLRenderer constructor
Romain Guyac670c02010-07-27 17:39:27 -070052static const TextureVertex gMeshVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070053 FV(0.0f, 0.0f, 0.0f, 0.0f),
54 FV(1.0f, 0.0f, 1.0f, 0.0f),
55 FV(0.0f, 1.0f, 0.0f, 1.0f),
56 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070057};
Romain Guyac670c02010-07-27 17:39:27 -070058static const GLsizei gMeshStride = sizeof(TextureVertex);
59static const GLsizei gMeshCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070060
Romain Guy889f8d12010-07-29 14:37:42 -070061/**
62 * Structure mapping Skia xfermodes to OpenGL blending factors.
63 */
64struct Blender {
65 SkXfermode::Mode mode;
66 GLenum src;
67 GLenum dst;
68}; // struct Blender
69
Romain Guy026c5e162010-06-28 17:12:22 -070070// In this array, the index of each Blender equals the value of the first
71// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
72static const Blender gBlends[] = {
73 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
74 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
75 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
76 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
77 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
78 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
79 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
80 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
81 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
82 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
84 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
85};
Romain Guye4d01122010-06-16 18:44:05 -070086
Romain Guy87a76572010-09-13 18:11:21 -070087// This array contains the swapped version of each SkXfermode. For instance
88// this array's SrcOver blending mode is actually DstOver. You can refer to
89// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070090static const Blender gBlendsSwap[] = {
91 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
92 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
93 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
94 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
95 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
96 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
97 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
98 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
99 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
100 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
101 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
103};
104
Romain Guy889f8d12010-07-29 14:37:42 -0700105static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -0700106 GL_TEXTURE0,
107 GL_TEXTURE1,
108 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700109};
110
Romain Guyf6a11b82010-06-23 17:47:49 -0700111///////////////////////////////////////////////////////////////////////////////
112// Constructors/destructor
113///////////////////////////////////////////////////////////////////////////////
114
Romain Guyfb8b7632010-08-23 21:05:08 -0700115OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700116 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700117 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700118 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700119
Romain Guyac670c02010-07-27 17:39:27 -0700120 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
121
Romain Guyae5575b2010-07-29 18:48:04 -0700122 mFirstSnapshot = new Snapshot;
123
124 GLint maxTextureUnits;
125 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
126 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Romain Guy889f8d12010-07-29 14:37:42 -0700127 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
128 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700129
130 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Romain Guye4d01122010-06-16 18:44:05 -0700131}
132
Romain Guy85bf02f2010-06-22 13:11:24 -0700133OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700134 // The context has already been destroyed at this point, do not call
135 // GL APIs. All GL state should be kept in Caches.h
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 Guy0bb56672010-10-01 00:25:02 -0700366 glActiveTexture(GL_TEXTURE0);
367
Romain Guy8411f332010-09-13 17:27:57 -0700368 LayerSize size(bounds.getWidth(), bounds.getHeight());
Romain Guyf607bdc2010-09-10 19:20:06 -0700369 Layer* layer = mCaches.layerCache.get(size);
Romain Guydda570202010-07-06 11:39:32 -0700370 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700371 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700372 }
373
Romain Guydda570202010-07-06 11:39:32 -0700374 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700375 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700376 layer->layer.set(bounds);
Romain Guydda570202010-07-06 11:39:32 -0700377
Romain Guy8fb95422010-08-17 18:38:51 -0700378 // Save the layer in the snapshot
379 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700380 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700381
Romain Guyf607bdc2010-09-10 19:20:06 -0700382 // Copy the framebuffer into the layer
383 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy38c85b92010-09-22 22:48:20 -0700384
Romain Guy0bb56672010-10-01 00:25:02 -0700385 // TODO: Workaround for b/3054204
386 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
387 bounds.getWidth(), bounds.getHeight(), 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700388
Romain Guy0bb56672010-10-01 00:25:02 -0700389 // TODO: Waiting for b/3054204 to be fixed
390// if (layer->empty) {
391// glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
392// bounds.getWidth(), bounds.getHeight(), 0);
393// layer->empty = false;
394// } else {
395// glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, mHeight - bounds.bottom,
396// bounds.getWidth(), bounds.getHeight());
397// }
398
399 if (flags & SkCanvas::kClipToLayer_SaveFlag && mSnapshot->clipTransformed(bounds)) {
400 setScissorFromClip();
Romain Guyf607bdc2010-09-10 19:20:06 -0700401 }
402
Romain Guy86942302010-09-12 13:02:16 -0700403 // Enqueue the buffer coordinates to clear the corresponding region later
404 mLayers.push(new Rect(bounds));
Romain Guyf86ef572010-07-01 11:05:42 -0700405
Romain Guyd55a8612010-06-28 17:42:46 -0700406 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700407}
408
Romain Guy1c740bc2010-09-13 18:00:09 -0700409/**
410 * Read the documentation of createLayer() before doing anything in this method.
411 */
Romain Guy1d83e192010-08-17 11:37:00 -0700412void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
413 if (!current->layer) {
414 LOGE("Attempting to compose a layer that does not exist");
415 return;
416 }
417
Romain Guy1d83e192010-08-17 11:37:00 -0700418 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700419 const Rect& clip = *previous->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700420 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700421
422 Layer* layer = current->layer;
423 const Rect& rect = layer->layer;
424
Romain Guyf607bdc2010-09-10 19:20:06 -0700425 if (layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700426 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700427 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guyf607bdc2010-09-10 19:20:06 -0700428 }
429
430 // Layers are already drawn with a top-left origin, don't flip the texture
Romain Guy8b55f372010-08-18 17:10:07 -0700431 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
432
Romain Guyf607bdc2010-09-10 19:20:06 -0700433 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
434 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
Romain Guy6820ac82010-09-15 18:11:50 -0700435 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, true, true);
Romain Guy1d83e192010-08-17 11:37:00 -0700436
Romain Guy8b55f372010-08-18 17:10:07 -0700437 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
438
Romain Guy1d83e192010-08-17 11:37:00 -0700439 LayerSize size(rect.getWidth(), rect.getHeight());
440 // Failing to add the layer to the cache should happen only if the
441 // layer is too large
Romain Guyfb8b7632010-08-23 21:05:08 -0700442 if (!mCaches.layerCache.put(size, layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700443 LAYER_LOGD("Deleting layer");
444
Romain Guy1d83e192010-08-17 11:37:00 -0700445 glDeleteTextures(1, &layer->texture);
446
447 delete layer;
448 }
449}
450
Romain Guy86942302010-09-12 13:02:16 -0700451void OpenGLRenderer::clearLayerRegions() {
452 if (mLayers.size() == 0) return;
453
454 for (uint32_t i = 0; i < mLayers.size(); i++) {
455 Rect* bounds = mLayers.itemAt(i);
456
457 // Clear the framebuffer where the layer will draw
458 glScissor(bounds->left, mHeight - bounds->bottom,
459 bounds->getWidth(), bounds->getHeight());
460 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
461 glClear(GL_COLOR_BUFFER_BIT);
462
463 delete bounds;
464 }
465 mLayers.clear();
466
467 // Restore the clip
468 setScissorFromClip();
469}
470
Romain Guybd6b79b2010-06-26 00:13:53 -0700471///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700472// Transforms
473///////////////////////////////////////////////////////////////////////////////
474
475void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700476 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700477}
478
479void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700480 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700481}
482
483void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700484 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700485}
486
487void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700488 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700489}
490
491void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700492 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700493}
494
495void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700496 mat4 m(*matrix);
Romain Guy8aef54f2010-09-01 15:13:49 -0700497 mSnapshot->transform->multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700498}
499
500///////////////////////////////////////////////////////////////////////////////
501// Clipping
502///////////////////////////////////////////////////////////////////////////////
503
Romain Guybb9524b2010-06-22 18:56:38 -0700504void OpenGLRenderer::setScissorFromClip() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700505 const Rect& clip = *mSnapshot->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700506 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700507}
508
509const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700510 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700511}
512
Romain Guyc7d53492010-06-25 13:41:57 -0700513bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy1d83e192010-08-17 11:37:00 -0700514 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700515 mSnapshot->transform->mapRect(r);
516 return !mSnapshot->clipRect->intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700517}
518
Romain Guy079ba2c2010-07-16 14:12:24 -0700519bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
520 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700521 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700522 setScissorFromClip();
523 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700524 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700525}
526
Romain Guyf6a11b82010-06-23 17:47:49 -0700527///////////////////////////////////////////////////////////////////////////////
528// Drawing
529///////////////////////////////////////////////////////////////////////////////
530
Romain Guyc1396e92010-06-30 17:56:19 -0700531void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700532 const float right = left + bitmap->width();
533 const float bottom = top + bitmap->height();
534
535 if (quickReject(left, top, right, bottom)) {
536 return;
537 }
538
Romain Guy61c8c9c2010-08-09 20:48:09 -0700539 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700540 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700541 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700542 const AutoTexture autoCleanup(texture);
543
Romain Guy6926c722010-07-12 20:20:03 -0700544 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700545}
546
Romain Guyf86ef572010-07-01 11:05:42 -0700547void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
548 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
549 const mat4 transform(*matrix);
550 transform.mapRect(r);
551
Romain Guy6926c722010-07-12 20:20:03 -0700552 if (quickReject(r.left, r.top, r.right, r.bottom)) {
553 return;
554 }
555
Romain Guy61c8c9c2010-08-09 20:48:09 -0700556 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700557 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700558 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700559 const AutoTexture autoCleanup(texture);
560
Romain Guy82ba8142010-07-09 13:25:56 -0700561 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700562}
563
Romain Guy8ba548f2010-06-30 19:21:21 -0700564void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
565 float srcLeft, float srcTop, float srcRight, float srcBottom,
566 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700567 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700568 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
569 return;
570 }
571
Romain Guy61c8c9c2010-08-09 20:48:09 -0700572 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700573 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700574 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700575 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700576
Romain Guy8ba548f2010-06-30 19:21:21 -0700577 const float width = texture->width;
578 const float height = texture->height;
579
580 const float u1 = srcLeft / width;
581 const float v1 = srcTop / height;
582 const float u2 = srcRight / width;
583 const float v2 = srcBottom / height;
584
585 resetDrawTextureTexCoords(u1, v1, u2, v2);
586
Romain Guy82ba8142010-07-09 13:25:56 -0700587 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700588
589 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
590}
591
Romain Guy4aa90572010-09-26 18:40:37 -0700592void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
593 uint32_t width, uint32_t height, float left, float top, float right, float bottom,
594 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700595 if (quickReject(left, top, right, bottom)) {
596 return;
597 }
598
Romain Guy61c8c9c2010-08-09 20:48:09 -0700599 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700600 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700601 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700602 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700603
604 int alpha;
605 SkXfermode::Mode mode;
606 getAlphaAndMode(paint, &alpha, &mode);
607
Romain Guy4aa90572010-09-26 18:40:37 -0700608 Patch* mesh = mCaches.patchCache.get(width, height);
Romain Guy759ea802010-09-16 20:49:46 -0700609 mesh->updateVertices(bitmap->width(), bitmap->height(),left, top, right, bottom,
Romain Guy4aa90572010-09-26 18:40:37 -0700610 xDivs, yDivs, width, height);
Romain Guyf7f93552010-07-08 19:17:03 -0700611
612 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
613 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700614 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
615 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy6820ac82010-09-15 18:11:50 -0700616 &mesh->vertices[0].texture[0], GL_TRIANGLES, mesh->verticesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700617}
618
Romain Guy759ea802010-09-16 20:49:46 -0700619void OpenGLRenderer::drawLines(float* points, int count, const SkPaint* paint) {
620 int alpha;
621 SkXfermode::Mode mode;
622 getAlphaAndMode(paint, &alpha, &mode);
623
624 uint32_t color = paint->getColor();
625 const GLfloat a = alpha / 255.0f;
626 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
627 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
628 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
629
Romain Guyc95c8d62010-09-17 15:31:32 -0700630 const bool isAA = paint->isAntiAlias();
631 if (isAA) {
632 GLuint textureUnit = 0;
Romain Guy29d89972010-09-22 16:10:57 -0700633 setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
634 mode, false, true, mCaches.line.getVertices(), mCaches.line.getTexCoords());
Romain Guyc95c8d62010-09-17 15:31:32 -0700635 } else {
636 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false);
637 }
638
639 const float strokeWidth = paint->getStrokeWidth();
Romain Guy29d89972010-09-22 16:10:57 -0700640 const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount;
Romain Guyc95c8d62010-09-17 15:31:32 -0700641 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
Romain Guy759ea802010-09-16 20:49:46 -0700642
643 for (int i = 0; i < count; i += 4) {
644 float tx = 0.0f;
645 float ty = 0.0f;
646
Romain Guyc95c8d62010-09-17 15:31:32 -0700647 if (isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700648 mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3],
Romain Guyc95c8d62010-09-17 15:31:32 -0700649 strokeWidth, tx, ty);
650 } else {
Romain Guyb5ab4172010-09-17 15:36:56 -0700651 ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f;
Romain Guyc95c8d62010-09-17 15:31:32 -0700652 }
Romain Guy759ea802010-09-16 20:49:46 -0700653
654 const float dx = points[i + 2] - points[i];
655 const float dy = points[i + 3] - points[i + 1];
656 const float mag = sqrtf(dx * dx + dy * dy);
657 const float angle = acos(dx / mag);
658
659 mModelView.loadTranslate(points[i], points[i + 1], 0.0f);
660 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) {
661 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f);
662 }
663 mModelView.translate(tx, ty, 0.0f);
Romain Guyc95c8d62010-09-17 15:31:32 -0700664 if (!isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700665 float length = mCaches.line.getLength(points[i], points[i + 1],
666 points[i + 2], points[i + 3]);
Romain Guyc95c8d62010-09-17 15:31:32 -0700667 mModelView.scale(length, strokeWidth, 1.0f);
668 }
Romain Guy759ea802010-09-16 20:49:46 -0700669 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
670
671 if (mShader) {
672 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot);
673 }
674
Romain Guyc95c8d62010-09-17 15:31:32 -0700675 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy759ea802010-09-16 20:49:46 -0700676 }
677
Romain Guy29d89972010-09-22 16:10:57 -0700678 if (isAA) {
679 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
680 }
Romain Guy759ea802010-09-16 20:49:46 -0700681}
682
Romain Guy85bf02f2010-06-22 13:11:24 -0700683void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700684 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700685 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700686}
Romain Guy9d5316e2010-06-24 19:30:36 -0700687
Romain Guybd6b79b2010-06-26 00:13:53 -0700688void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700689 if (quickReject(left, top, right, bottom)) {
690 return;
691 }
692
Romain Guy026c5e162010-06-28 17:12:22 -0700693 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700694 if (!mExtensions.hasFramebufferFetch()) {
695 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
696 if (!isMode) {
697 // Assume SRC_OVER
698 mode = SkXfermode::kSrcOver_Mode;
699 }
700 } else {
701 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700702 }
703
704 // Skia draws using the color's alpha channel if < 255
705 // Otherwise, it uses the paint's alpha
706 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700707 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700708 color |= p->getAlpha() << 24;
709 }
710
711 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700712}
Romain Guy9d5316e2010-06-24 19:30:36 -0700713
Romain Guye8e62a42010-07-23 18:55:21 -0700714void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
715 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700716 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700717 return;
718 }
Romain Guye2d345e2010-09-24 18:39:22 -0700719
Romain Guyf607bdc2010-09-10 19:20:06 -0700720 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700721
Romain Guya674ab72010-08-10 17:26:42 -0700722 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700723 switch (paint->getTextAlign()) {
724 case SkPaint::kCenter_Align:
725 length = paint->measureText(text, bytesCount);
726 x -= length / 2.0f;
727 break;
728 case SkPaint::kRight_Align:
729 length = paint->measureText(text, bytesCount);
730 x -= length;
731 break;
732 default:
733 break;
734 }
735
Romain Guy694b5192010-07-21 21:33:20 -0700736 int alpha;
737 SkXfermode::Mode mode;
738 getAlphaAndMode(paint, &alpha, &mode);
739
Romain Guy2542d192010-08-18 11:47:12 -0700740 uint32_t color = paint->getColor();
741 const GLfloat a = alpha / 255.0f;
742 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
743 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
744 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
745
Romain Guyb45c0c92010-08-26 20:35:23 -0700746 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
747 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700748 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -0700749
Romain Guy1e45aae2010-08-13 19:39:53 -0700750 if (mHasShadow) {
751 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700752 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700753 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700754 count, mShadowRadius);
755 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700756
Romain Guy2542d192010-08-18 11:47:12 -0700757 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700758
759 // Draw the mesh
760 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700761 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700762 }
763
Romain Guy06f96e22010-07-30 19:18:16 -0700764 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700765 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700766
Romain Guye8cb9c142010-10-04 14:14:11 -0700767 // Assume that the modelView matrix does not force scales, rotates, etc.
768 const bool linearFilter = mSnapshot->transform->changesBounds();
769 setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit,
770 x, y, r, g, b, a, mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700771
Romain Guy09147fb2010-07-22 13:08:20 -0700772 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy86942302010-09-12 13:02:16 -0700773 clearLayerRegions();
Romain Guyb45c0c92010-08-26 20:35:23 -0700774 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700775
776 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700777 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700778
Romain Guy0a417492010-08-16 20:26:20 -0700779 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -0700780}
781
Romain Guy7fbcc042010-08-04 15:40:07 -0700782void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
783 GLuint textureUnit = 0;
784 glActiveTexture(gTextureUnits[textureUnit]);
785
Romain Guyfb8b7632010-08-23 21:05:08 -0700786 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700787 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700788 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700789
Romain Guy8b55f372010-08-18 17:10:07 -0700790 const float x = texture->left - texture->offset;
791 const float y = texture->top - texture->offset;
792
793 if (quickReject(x, y, x + texture->width, y + texture->height)) {
794 return;
795 }
796
Romain Guy7fbcc042010-08-04 15:40:07 -0700797 int alpha;
798 SkXfermode::Mode mode;
799 getAlphaAndMode(paint, &alpha, &mode);
800
801 uint32_t color = paint->getColor();
802 const GLfloat a = alpha / 255.0f;
803 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
804 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
805 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
806
Romain Guy0a417492010-08-16 20:26:20 -0700807 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
808
Romain Guy86942302010-09-12 13:02:16 -0700809 clearLayerRegions();
810
Romain Guy0a417492010-08-16 20:26:20 -0700811 // Draw the mesh
812 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700813 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700814}
815
Romain Guy6926c722010-07-12 20:20:03 -0700816///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700817// Shaders
818///////////////////////////////////////////////////////////////////////////////
819
820void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700821 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700822}
823
Romain Guy06f96e22010-07-30 19:18:16 -0700824void OpenGLRenderer::setupShader(SkiaShader* shader) {
825 mShader = shader;
826 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700827 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700828 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700829}
830
Romain Guyd27977d2010-07-14 19:18:51 -0700831///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700832// Color filters
833///////////////////////////////////////////////////////////////////////////////
834
835void OpenGLRenderer::resetColorFilter() {
836 mColorFilter = NULL;
837}
838
839void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
840 mColorFilter = filter;
841}
842
843///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700844// Drop shadow
845///////////////////////////////////////////////////////////////////////////////
846
847void OpenGLRenderer::resetShadow() {
848 mHasShadow = false;
849}
850
851void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
852 mHasShadow = true;
853 mShadowRadius = radius;
854 mShadowDx = dx;
855 mShadowDy = dy;
856 mShadowColor = color;
857}
858
859///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700860// Drawing implementation
861///////////////////////////////////////////////////////////////////////////////
862
Romain Guy0a417492010-08-16 20:26:20 -0700863void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700864 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700865 const float sx = x - texture->left + mShadowDx;
866 const float sy = y - texture->top + mShadowDy;
867
Romain Guy2542d192010-08-18 11:47:12 -0700868 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
869 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700870 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
871 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
872 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
873
874 GLuint textureUnit = 0;
875 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
876}
877
878void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
879 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
880 bool transforms, bool applyFilters) {
881 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -0700882 x, y, r, g, b, a, mode, transforms, applyFilters,
883 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
Romain Guy0a417492010-08-16 20:26:20 -0700884}
885
886void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
887 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
888 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy759ea802010-09-16 20:49:46 -0700889 setupTextureAlpha8(texture, width, height, textureUnit,
890 x, y, r, g, b, a, mode, transforms, applyFilters,
891 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
892}
893
894void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
895 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
896 SkXfermode::Mode mode, bool transforms, bool applyFilters,
897 GLvoid* vertices, GLvoid* texCoords) {
Romain Guy0a417492010-08-16 20:26:20 -0700898 // Describe the required shaders
899 ProgramDescription description;
900 description.hasTexture = true;
901 description.hasAlpha8Texture = true;
902
903 if (applyFilters) {
904 if (mShader) {
905 mShader->describe(description, mExtensions);
906 }
907 if (mColorFilter) {
908 mColorFilter->describe(description, mExtensions);
909 }
910 }
911
Romain Guya5aed0d2010-09-09 14:42:43 -0700912 // Setup the blending mode
913 chooseBlending(true, mode, description);
914
Romain Guy0a417492010-08-16 20:26:20 -0700915 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700916 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700917
Romain Guy0a417492010-08-16 20:26:20 -0700918 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -0700919 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700920
Romain Guyfb8b7632010-08-23 21:05:08 -0700921 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -0700922 glEnableVertexAttribArray(texCoordsSlot);
923
924 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700925 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -0700926 gMeshStride, vertices);
Romain Guy0a417492010-08-16 20:26:20 -0700927 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -0700928 gMeshStride, texCoords);
Romain Guy0a417492010-08-16 20:26:20 -0700929
930 // Setup uniforms
931 if (transforms) {
932 mModelView.loadTranslate(x, y, 0.0f);
933 mModelView.scale(width, height, 1.0f);
934 } else {
935 mModelView.loadIdentity();
936 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700937 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyfb8b7632010-08-23 21:05:08 -0700938 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -0700939
940 textureUnit++;
941 if (applyFilters) {
942 // Setup attributes and uniforms required by the shaders
943 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700944 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700945 }
946 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700947 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -0700948 }
949 }
950}
951
Romain Guyf607bdc2010-09-10 19:20:06 -0700952// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -0700953#define kStdStrikeThru_Offset (-6.0f / 21.0f)
954#define kStdUnderline_Offset (1.0f / 9.0f)
955#define kStdUnderline_Thickness (1.0f / 18.0f)
956
957void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
958 float x, float y, SkPaint* paint) {
959 // Handle underline and strike-through
960 uint32_t flags = paint->getFlags();
961 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
962 float underlineWidth = length;
963 // If length is > 0.0f, we already measured the text for the text alignment
964 if (length <= 0.0f) {
965 underlineWidth = paint->measureText(text, bytesCount);
966 }
967
968 float offsetX = 0;
969 switch (paint->getTextAlign()) {
970 case SkPaint::kCenter_Align:
971 offsetX = underlineWidth * 0.5f;
972 break;
973 case SkPaint::kRight_Align:
974 offsetX = underlineWidth;
975 break;
976 default:
977 break;
978 }
979
980 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -0700981 const float textSize = paint->getTextSize();
982 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -0700983
Romain Guye20ecbd2010-09-22 19:49:04 -0700984 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -0700985 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -0700986
987 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
988 float points[pointsCount];
989 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -0700990
991 if (flags & SkPaint::kUnderlineText_Flag) {
992 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -0700993 points[currentPoint++] = left;
994 points[currentPoint++] = top;
995 points[currentPoint++] = left + underlineWidth;
996 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -0700997 }
998
999 if (flags & SkPaint::kStrikeThruText_Flag) {
1000 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001001 points[currentPoint++] = left;
1002 points[currentPoint++] = top;
1003 points[currentPoint++] = left + underlineWidth;
1004 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001005 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001006
1007 SkPaint linesPaint(*paint);
1008 linesPaint.setStrokeWidth(strokeWidth);
1009
1010 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001011 }
1012 }
1013}
1014
Romain Guy026c5e162010-06-28 17:12:22 -07001015void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001016 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001017 clearLayerRegions();
1018
Romain Guyd27977d2010-07-14 19:18:51 -07001019 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001020 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001021 color |= 0x00ffffff;
1022 }
1023
1024 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -07001025 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -07001026 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -07001027 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1028 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1029 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1030
Romain Guyc95c8d62010-09-17 15:31:32 -07001031 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform);
1032
1033 // Draw the mesh
1034 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1035}
1036
1037void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom,
1038 float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy06f96e22010-07-30 19:18:16 -07001039 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -07001040
Romain Guy06f96e22010-07-30 19:18:16 -07001041 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -07001042 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -07001043 if (mShader) {
1044 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -07001045 }
Romain Guydb1938e2010-08-02 18:50:22 -07001046 if (mColorFilter) {
1047 mColorFilter->describe(description, mExtensions);
1048 }
Romain Guyd27977d2010-07-14 19:18:51 -07001049
Romain Guy1c740bc2010-09-13 18:00:09 -07001050 // Setup the blending mode
Romain Guyc95c8d62010-09-17 15:31:32 -07001051 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -07001052
Romain Guy06f96e22010-07-30 19:18:16 -07001053 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001054 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -07001055
1056 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -07001057 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -07001058 gMeshStride, &mMeshVertices[0].position[0]);
1059
1060 // Setup uniforms
1061 mModelView.loadTranslate(left, top, 0.0f);
1062 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -07001063 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001064 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -07001065 } else {
1066 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -07001067 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -07001068 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001069 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -07001070
Romain Guy06f96e22010-07-30 19:18:16 -07001071 // Setup attributes and uniforms required by the shaders
1072 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001073 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001074 }
Romain Guydb1938e2010-08-02 18:50:22 -07001075 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001076 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001077 }
Romain Guyd27977d2010-07-14 19:18:51 -07001078}
1079
Romain Guy82ba8142010-07-09 13:25:56 -07001080void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001081 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001082 int alpha;
1083 SkXfermode::Mode mode;
1084 getAlphaAndMode(paint, &alpha, &mode);
1085
Romain Guy6820ac82010-09-15 18:11:50 -07001086 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1087 texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1088 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -07001089}
1090
Romain Guybd6b79b2010-06-26 00:13:53 -07001091void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001092 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1093 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001094 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1095 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001096}
1097
1098void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001099 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001100 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guyf607bdc2010-09-10 19:20:06 -07001101 bool swapSrcDst, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001102 clearLayerRegions();
1103
Romain Guy889f8d12010-07-29 14:37:42 -07001104 ProgramDescription description;
1105 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -07001106 if (mColorFilter) {
1107 mColorFilter->describe(description, mExtensions);
1108 }
Romain Guy889f8d12010-07-29 14:37:42 -07001109
Romain Guybd6b79b2010-06-26 00:13:53 -07001110 mModelView.loadTranslate(left, top, 0.0f);
1111 mModelView.scale(right - left, bottom - top, 1.0f);
1112
Romain Guyf607bdc2010-09-10 19:20:06 -07001113 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001114
Romain Guyfb8b7632010-08-23 21:05:08 -07001115 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001116 if (!ignoreTransform) {
1117 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1118 } else {
1119 mat4 m;
1120 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
1121 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001122
Romain Guy889f8d12010-07-29 14:37:42 -07001123 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -07001124 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001125 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001126
Romain Guya9794742010-07-13 11:37:54 -07001127 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -07001128 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -07001129
Romain Guy889f8d12010-07-29 14:37:42 -07001130 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001131 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001132 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -07001133 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001134 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001135 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001136
Romain Guydb1938e2010-08-02 18:50:22 -07001137 // Color filter
1138 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001139 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001140 }
1141
Romain Guy6820ac82010-09-15 18:11:50 -07001142 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001143 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001144}
1145
Romain Guya5aed0d2010-09-09 14:42:43 -07001146void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001147 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001148 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1149 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001150 if (mode < SkXfermode::kPlus_Mode) {
1151 if (!mCaches.blend) {
1152 glEnable(GL_BLEND);
1153 }
Romain Guy82ba8142010-07-09 13:25:56 -07001154
Romain Guyf607bdc2010-09-10 19:20:06 -07001155 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1156 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001157
Romain Guya5aed0d2010-09-09 14:42:43 -07001158 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1159 glBlendFunc(sourceMode, destMode);
1160 mCaches.lastSrcMode = sourceMode;
1161 mCaches.lastDstMode = destMode;
1162 }
1163 } else {
1164 // These blend modes are not supported by OpenGL directly and have
1165 // to be implemented using shaders. Since the shader will perform
1166 // the blending, turn blending off here
1167 if (mExtensions.hasFramebufferFetch()) {
1168 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001169 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001170 }
1171
1172 if (mCaches.blend) {
1173 glDisable(GL_BLEND);
1174 }
1175 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001176 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001177 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001178 glDisable(GL_BLEND);
1179 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001180 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001181}
1182
Romain Guy889f8d12010-07-29 14:37:42 -07001183bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001184 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001185 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001186 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001187 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001188 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001189 }
Romain Guy6926c722010-07-12 20:20:03 -07001190 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001191}
1192
Romain Guy026c5e162010-06-28 17:12:22 -07001193void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001194 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001195 TextureVertex::setUV(v++, u1, v1);
1196 TextureVertex::setUV(v++, u2, v1);
1197 TextureVertex::setUV(v++, u1, v2);
1198 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001199}
1200
1201void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1202 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001203 if (!mExtensions.hasFramebufferFetch()) {
1204 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1205 if (!isMode) {
1206 // Assume SRC_OVER
1207 *mode = SkXfermode::kSrcOver_Mode;
1208 }
1209 } else {
1210 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001211 }
1212
1213 // Skia draws using the color's alpha channel if < 255
1214 // Otherwise, it uses the paint's alpha
1215 int color = paint->getColor();
1216 *alpha = (color >> 24) & 0xFF;
1217 if (*alpha == 255) {
1218 *alpha = paint->getAlpha();
1219 }
1220 } else {
1221 *mode = SkXfermode::kSrcOver_Mode;
1222 *alpha = 255;
1223 }
Romain Guy026c5e162010-06-28 17:12:22 -07001224}
1225
Romain Guya5aed0d2010-09-09 14:42:43 -07001226SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1227 if (mode == NULL) {
1228 return SkXfermode::kSrcOver_Mode;
1229 }
1230 return mode->fMode;
1231}
1232
Romain Guy889f8d12010-07-29 14:37:42 -07001233void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1234 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001235 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001236 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1237 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1238}
1239
Romain Guy9d5316e2010-06-24 19:30:36 -07001240}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001241}; // namespace android