blob: 57de43af0baa0e2ebe202bb2e54a99d08821299d [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 Guydbc26d22010-10-11 17:58:29 -070046// TODO: This should be set in properties
47#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
48
Romain Guy9d5316e2010-06-24 19:30:36 -070049///////////////////////////////////////////////////////////////////////////////
50// Globals
51///////////////////////////////////////////////////////////////////////////////
52
Romain Guy026c5e162010-06-28 17:12:22 -070053// This array is never used directly but used as a memcpy source in the
54// OpenGLRenderer constructor
Romain Guyac670c02010-07-27 17:39:27 -070055static const TextureVertex gMeshVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070056 FV(0.0f, 0.0f, 0.0f, 0.0f),
57 FV(1.0f, 0.0f, 1.0f, 0.0f),
58 FV(0.0f, 1.0f, 0.0f, 1.0f),
59 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070060};
Romain Guyac670c02010-07-27 17:39:27 -070061static const GLsizei gMeshStride = sizeof(TextureVertex);
62static const GLsizei gMeshCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070063
Romain Guy889f8d12010-07-29 14:37:42 -070064/**
65 * Structure mapping Skia xfermodes to OpenGL blending factors.
66 */
67struct Blender {
68 SkXfermode::Mode mode;
69 GLenum src;
70 GLenum dst;
71}; // struct Blender
72
Romain Guy026c5e162010-06-28 17:12:22 -070073// In this array, the index of each Blender equals the value of the first
74// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
75static const Blender gBlends[] = {
76 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
77 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
78 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
79 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
81 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
82 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
83 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
84 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
85 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
86 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
87 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
88};
Romain Guye4d01122010-06-16 18:44:05 -070089
Romain Guy87a76572010-09-13 18:11:21 -070090// This array contains the swapped version of each SkXfermode. For instance
91// this array's SrcOver blending mode is actually DstOver. You can refer to
92// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070093static const Blender gBlendsSwap[] = {
94 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
95 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
96 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
97 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
98 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
99 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
100 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
101 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
103 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
104 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
105 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
106};
107
Romain Guy889f8d12010-07-29 14:37:42 -0700108static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -0700109 GL_TEXTURE0,
110 GL_TEXTURE1,
111 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700112};
113
Romain Guyf6a11b82010-06-23 17:47:49 -0700114///////////////////////////////////////////////////////////////////////////////
115// Constructors/destructor
116///////////////////////////////////////////////////////////////////////////////
117
Romain Guyfb8b7632010-08-23 21:05:08 -0700118OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700119 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700120 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700121 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700122
Romain Guyac670c02010-07-27 17:39:27 -0700123 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
124
Romain Guyae5575b2010-07-29 18:48:04 -0700125 mFirstSnapshot = new Snapshot;
126
127 GLint maxTextureUnits;
128 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
129 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Romain Guy889f8d12010-07-29 14:37:42 -0700130 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
131 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700132
133 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Romain Guye4d01122010-06-16 18:44:05 -0700134}
135
Romain Guy85bf02f2010-06-22 13:11:24 -0700136OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700137 // The context has already been destroyed at this point, do not call
138 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700139}
140
Romain Guyf6a11b82010-06-23 17:47:49 -0700141///////////////////////////////////////////////////////////////////////////////
142// Setup
143///////////////////////////////////////////////////////////////////////////////
144
Romain Guy85bf02f2010-06-22 13:11:24 -0700145void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700146 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700147 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700148
149 mWidth = width;
150 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700151
152 mFirstSnapshot->height = height;
153 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700154}
155
Romain Guy6b7bd242010-10-06 19:49:23 -0700156void OpenGLRenderer::prepare(bool opaque) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700157 mSnapshot = new Snapshot(mFirstSnapshot,
158 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy8fb95422010-08-17 18:38:51 -0700159 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700160
Romain Guyfb8b7632010-08-23 21:05:08 -0700161 glViewport(0, 0, mWidth, mHeight);
162
Romain Guyd90f23e2010-09-09 11:47:54 -0700163 glDisable(GL_DITHER);
Romain Guy08ae3172010-06-21 19:35:50 -0700164 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700165
Romain Guy6b7bd242010-10-06 19:49:23 -0700166 if (!opaque) {
167 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
168 glClear(GL_COLOR_BUFFER_BIT);
169 }
Romain Guybb9524b2010-06-22 18:56:38 -0700170
Romain Guy08ae3172010-06-21 19:35:50 -0700171 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700172 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700173
Romain Guyb82da652010-07-30 11:36:12 -0700174 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700175}
176
Romain Guyb025b9c2010-09-16 14:16:48 -0700177void OpenGLRenderer::finish() {
178#if DEBUG_OPENGL
179 GLenum status = GL_NO_ERROR;
180 while ((status = glGetError()) != GL_NO_ERROR) {
181 LOGD("GL error from OpenGLRenderer: 0x%x", status);
182 }
183#endif
184}
185
Romain Guyda8532c2010-08-31 11:50:35 -0700186void OpenGLRenderer::acquireContext() {
187 if (mCaches.currentProgram) {
188 if (mCaches.currentProgram->isInUse()) {
189 mCaches.currentProgram->remove();
190 mCaches.currentProgram = NULL;
191 }
192 }
193}
194
195void OpenGLRenderer::releaseContext() {
Romain Guyeb993562010-10-05 18:14:38 -0700196 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700197
198 glEnable(GL_SCISSOR_TEST);
199 setScissorFromClip();
200
Romain Guyf607bdc2010-09-10 19:20:06 -0700201 glDisable(GL_DITHER);
202
203 glBindFramebuffer(GL_FRAMEBUFFER, 0);
204
Romain Guyda8532c2010-08-31 11:50:35 -0700205 if (mCaches.blend) {
206 glEnable(GL_BLEND);
207 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
Romain Guyf607bdc2010-09-10 19:20:06 -0700208 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700209 } else {
210 glDisable(GL_BLEND);
211 }
212}
213
Romain Guyf6a11b82010-06-23 17:47:49 -0700214///////////////////////////////////////////////////////////////////////////////
215// State management
216///////////////////////////////////////////////////////////////////////////////
217
Romain Guybb9524b2010-06-22 18:56:38 -0700218int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700219 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700220}
221
222int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700223 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700224}
225
226void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700227 if (mSaveCount > 1) {
228 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700229 }
Romain Guybb9524b2010-06-22 18:56:38 -0700230}
231
232void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700233 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700234
Romain Guy8fb95422010-08-17 18:38:51 -0700235 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700236 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700237 }
Romain Guybb9524b2010-06-22 18:56:38 -0700238}
239
Romain Guy8aef54f2010-09-01 15:13:49 -0700240int OpenGLRenderer::saveSnapshot(int flags) {
241 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700242 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700243}
244
245bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700246 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700247 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700248 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700249
Romain Guybd6b79b2010-06-26 00:13:53 -0700250 sp<Snapshot> current = mSnapshot;
251 sp<Snapshot> previous = mSnapshot->previous;
252
Romain Guyeb993562010-10-05 18:14:38 -0700253 if (restoreOrtho) {
254 Rect& r = previous->viewport;
255 glViewport(r.left, r.top, r.right, r.bottom);
256 mOrthoMatrix.load(current->orthoMatrix);
257 }
258
Romain Guy8b55f372010-08-18 17:10:07 -0700259 mSaveCount--;
260 mSnapshot = previous;
261
Romain Guybd6b79b2010-06-26 00:13:53 -0700262 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700263 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700264 }
265
Romain Guy2542d192010-08-18 11:47:12 -0700266 if (restoreClip) {
267 setScissorFromClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700268 }
Romain Guy2542d192010-08-18 11:47:12 -0700269
270 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700271}
272
Romain Guyf6a11b82010-06-23 17:47:49 -0700273///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700274// Layers
275///////////////////////////////////////////////////////////////////////////////
276
277int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
278 const SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700279 const GLuint previousFbo = mSnapshot->fbo;
280 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700281
282 int alpha = 255;
283 SkXfermode::Mode mode;
284
285 if (p) {
286 alpha = p->getAlpha();
Romain Guya5aed0d2010-09-09 14:42:43 -0700287 if (!mExtensions.hasFramebufferFetch()) {
288 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
289 if (!isMode) {
290 // Assume SRC_OVER
291 mode = SkXfermode::kSrcOver_Mode;
292 }
293 } else {
294 mode = getXfermode(p->getXfermode());
Romain Guyd55a8612010-06-28 17:42:46 -0700295 }
296 } else {
297 mode = SkXfermode::kSrcOver_Mode;
298 }
299
Romain Guydbc26d22010-10-11 17:58:29 -0700300 if (!mSnapshot->previous->invisible) {
301 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
302 }
Romain Guyd55a8612010-06-28 17:42:46 -0700303
304 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700305}
306
307int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
308 int alpha, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700309 if (alpha == 0xff) {
310 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700311 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700312 SkPaint paint;
313 paint.setAlpha(alpha);
314 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700315 }
Romain Guyd55a8612010-06-28 17:42:46 -0700316}
Romain Guybd6b79b2010-06-26 00:13:53 -0700317
Romain Guy1c740bc2010-09-13 18:00:09 -0700318/**
319 * Layers are viewed by Skia are slightly different than layers in image editing
320 * programs (for instance.) When a layer is created, previously created layers
321 * and the frame buffer still receive every drawing command. For instance, if a
322 * layer is created and a shape intersecting the bounds of the layers and the
323 * framebuffer is draw, the shape will be drawn on both (unless the layer was
324 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
325 *
326 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
327 * texture. Unfortunately, this is inefficient as it requires every primitive to
328 * be drawn n + 1 times, where n is the number of active layers. In practice this
329 * means, for every primitive:
330 * - Switch active frame buffer
331 * - Change viewport, clip and projection matrix
332 * - Issue the drawing
333 *
334 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700335 * To avoid this, layers are implemented in a different way here, at least in the
336 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
337 * is set. When this flag is set we can redirect all drawing operations into a
338 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700339 *
340 * This implementation relies on the frame buffer being at least RGBA 8888. When
341 * a layer is created, only a texture is created, not an FBO. The content of the
342 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700343 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700344 * buffer and drawing continues as normal. This technique therefore treats the
345 * frame buffer as a scratch buffer for the layers.
346 *
347 * To compose the layers back onto the frame buffer, each layer texture
348 * (containing the original frame buffer data) is drawn as a simple quad over
349 * the frame buffer. The trick is that the quad is set as the composition
350 * destination in the blending equation, and the frame buffer becomes the source
351 * of the composition.
352 *
353 * Drawing layers with an alpha value requires an extra step before composition.
354 * An empty quad is drawn over the layer's region in the frame buffer. This quad
355 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
356 * quad is used to multiply the colors in the frame buffer. This is achieved by
357 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
358 * GL_ZERO, GL_SRC_ALPHA.
359 *
360 * Because glCopyTexImage2D() can be slow, an alternative implementation might
361 * be use to draw a single clipped layer. The implementation described above
362 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700363 *
364 * (1) The frame buffer is actually not cleared right away. To allow the GPU
365 * to potentially optimize series of calls to glCopyTexImage2D, the frame
366 * buffer is left untouched until the first drawing operation. Only when
367 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700368 */
Romain Guyd55a8612010-06-28 17:42:46 -0700369bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700370 float right, float bottom, int alpha, SkXfermode::Mode mode,
371 int flags, GLuint previousFbo) {
372 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700373 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700374
Romain Guyeb993562010-10-05 18:14:38 -0700375 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
376
Romain Guyf607bdc2010-09-10 19:20:06 -0700377 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700378 Rect bounds(left, top, right, bottom);
Romain Guyeb993562010-10-05 18:14:38 -0700379 if (!fboLayer) {
380 mSnapshot->transform->mapRect(bounds);
381 // Layers only make sense if they are in the framebuffer's bounds
382 bounds.intersect(*mSnapshot->clipRect);
383 bounds.snapToPixelBoundaries();
384 }
Romain Guybf434112010-09-16 14:40:17 -0700385
Romain Guyb025b9c2010-09-16 14:16:48 -0700386 if (bounds.isEmpty() || bounds.getWidth() > mMaxTextureSize ||
387 bounds.getHeight() > mMaxTextureSize) {
Romain Guydbc26d22010-10-11 17:58:29 -0700388 snapshot->invisible = true;
389 } else {
390 // TODO: Should take the mode into account
391 snapshot->invisible = snapshot->previous->invisible || alpha <= ALPHA_THRESHOLD;
392 }
393
394 // Bail out if we won't draw in this snapshot
395 if (snapshot->invisible) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700396 return false;
397 }
Romain Guyf18fd992010-07-08 11:45:51 -0700398
Romain Guy0bb56672010-10-01 00:25:02 -0700399 glActiveTexture(GL_TEXTURE0);
400
Romain Guy8550c4c2010-10-08 15:49:53 -0700401 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700402 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700403 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700404 }
405
Romain Guydda570202010-07-06 11:39:32 -0700406 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700407 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700408 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700409 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
410 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guydda570202010-07-06 11:39:32 -0700411
Romain Guy8fb95422010-08-17 18:38:51 -0700412 // Save the layer in the snapshot
413 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700414 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700415
Romain Guyeb993562010-10-05 18:14:38 -0700416 if (fboLayer) {
417 layer->fbo = mCaches.fboCache.get();
Romain Guy38c85b92010-09-22 22:48:20 -0700418
Romain Guyeb993562010-10-05 18:14:38 -0700419 snapshot->flags |= Snapshot::kFlagIsFboLayer;
420 snapshot->fbo = layer->fbo;
421 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
422 snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
423 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
424 snapshot->height = bounds.getHeight();
425 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
426 snapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf607bdc2010-09-10 19:20:06 -0700427
Romain Guy0bb56672010-10-01 00:25:02 -0700428 setScissorFromClip();
Romain Guyf607bdc2010-09-10 19:20:06 -0700429
Romain Guyeb993562010-10-05 18:14:38 -0700430 // Bind texture to FBO
431 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
432 glBindTexture(GL_TEXTURE_2D, layer->texture);
433
434 // Initialize the texture if needed
435 if (layer->empty) {
436 layer->empty = false;
Romain Guy8550c4c2010-10-08 15:49:53 -0700437 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
Romain Guyeb993562010-10-05 18:14:38 -0700438 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
439 }
440
441 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
442 layer->texture, 0);
443
444 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
445 if (status != GL_FRAMEBUFFER_COMPLETE) {
446 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
447
448 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
449 glDeleteTextures(1, &layer->texture);
450 mCaches.fboCache.put(layer->fbo);
451
452 delete layer;
453
454 return false;
455 }
456
457 // Clear the FBO
458 glDisable(GL_SCISSOR_TEST);
459 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
460 glClear(GL_COLOR_BUFFER_BIT);
461 glEnable(GL_SCISSOR_TEST);
462
463 // Change the ortho projection
464 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
465 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
466 } else {
467 // Copy the framebuffer into the layer
468 glBindTexture(GL_TEXTURE_2D, layer->texture);
469
470 // TODO: Workaround for b/3054204
471 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
Romain Guy8550c4c2010-10-08 15:49:53 -0700472 layer->width, layer->height, 0);
Romain Guyeb993562010-10-05 18:14:38 -0700473
474 // TODO: Waiting for b/3054204 to be fixed
475 // if (layer->empty) {
476 // glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
Romain Guy8550c4c2010-10-08 15:49:53 -0700477 // layer->width, layer->height, 0);
Romain Guyeb993562010-10-05 18:14:38 -0700478 // layer->empty = false;
479 // } else {
480 // glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, mHeight - bounds.bottom,
481 // bounds.getWidth(), bounds.getHeight());
482 // }
483
484 // Enqueue the buffer coordinates to clear the corresponding region later
485 mLayers.push(new Rect(bounds));
486 }
Romain Guyf86ef572010-07-01 11:05:42 -0700487
Romain Guyd55a8612010-06-28 17:42:46 -0700488 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700489}
490
Romain Guy1c740bc2010-09-13 18:00:09 -0700491/**
492 * Read the documentation of createLayer() before doing anything in this method.
493 */
Romain Guy1d83e192010-08-17 11:37:00 -0700494void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
495 if (!current->layer) {
496 LOGE("Attempting to compose a layer that does not exist");
497 return;
498 }
499
Romain Guyeb993562010-10-05 18:14:38 -0700500 const bool fboLayer = current->flags & SkCanvas::kClipToLayer_SaveFlag;
501
502 if (fboLayer) {
503 // Unbind current FBO and restore previous one
504 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
505 }
506
Romain Guy1d83e192010-08-17 11:37:00 -0700507 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700508 const Rect& clip = *previous->clipRect;
Romain Guyeb993562010-10-05 18:14:38 -0700509 glScissor(clip.left, previous->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700510
511 Layer* layer = current->layer;
512 const Rect& rect = layer->layer;
513
Romain Guyeb993562010-10-05 18:14:38 -0700514 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700515 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700516 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guyf607bdc2010-09-10 19:20:06 -0700517 }
518
Romain Guy8550c4c2010-10-08 15:49:53 -0700519 const Rect& texCoords = layer->texCoords;
520 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
Romain Guy8b55f372010-08-18 17:10:07 -0700521
Romain Guyeb993562010-10-05 18:14:38 -0700522 if (fboLayer) {
523 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
524 layer->texture, layer->alpha / 255.0f, layer->mode, layer->blend);
525 } else {
526 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
527 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
528 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, true, true);
529 }
Romain Guy1d83e192010-08-17 11:37:00 -0700530
Romain Guy8b55f372010-08-18 17:10:07 -0700531 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
532
Romain Guyeb993562010-10-05 18:14:38 -0700533 if (fboLayer) {
534 // Detach the texture from the FBO
535 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
536 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
537 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
538
539 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
540 mCaches.fboCache.put(current->fbo);
541 }
542
Romain Guyeb993562010-10-05 18:14:38 -0700543 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700544 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700545 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700546 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700547 delete layer;
548 }
549}
550
Romain Guy86942302010-09-12 13:02:16 -0700551void OpenGLRenderer::clearLayerRegions() {
Romain Guydbc26d22010-10-11 17:58:29 -0700552 if (mLayers.size() == 0 || mSnapshot->invisible) return;
Romain Guy86942302010-09-12 13:02:16 -0700553
554 for (uint32_t i = 0; i < mLayers.size(); i++) {
555 Rect* bounds = mLayers.itemAt(i);
556
557 // Clear the framebuffer where the layer will draw
558 glScissor(bounds->left, mHeight - bounds->bottom,
559 bounds->getWidth(), bounds->getHeight());
560 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
561 glClear(GL_COLOR_BUFFER_BIT);
562
563 delete bounds;
564 }
565 mLayers.clear();
566
567 // Restore the clip
568 setScissorFromClip();
569}
570
Romain Guybd6b79b2010-06-26 00:13:53 -0700571///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700572// Transforms
573///////////////////////////////////////////////////////////////////////////////
574
575void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700576 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700577}
578
579void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700580 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700581}
582
583void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700584 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700585}
586
587void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700588 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700589}
590
591void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700592 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700593}
594
595void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700596 mat4 m(*matrix);
Romain Guy8aef54f2010-09-01 15:13:49 -0700597 mSnapshot->transform->multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700598}
599
600///////////////////////////////////////////////////////////////////////////////
601// Clipping
602///////////////////////////////////////////////////////////////////////////////
603
Romain Guybb9524b2010-06-22 18:56:38 -0700604void OpenGLRenderer::setScissorFromClip() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700605 const Rect& clip = *mSnapshot->clipRect;
Romain Guyeb993562010-10-05 18:14:38 -0700606 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700607}
608
609const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700610 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700611}
612
Romain Guyc7d53492010-06-25 13:41:57 -0700613bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guydbc26d22010-10-11 17:58:29 -0700614 if (mSnapshot->invisible) {
615 return true;
616 }
617
Romain Guy1d83e192010-08-17 11:37:00 -0700618 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700619 mSnapshot->transform->mapRect(r);
620 return !mSnapshot->clipRect->intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700621}
622
Romain Guy079ba2c2010-07-16 14:12:24 -0700623bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
624 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700625 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700626 setScissorFromClip();
627 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700628 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700629}
630
Romain Guyf6a11b82010-06-23 17:47:49 -0700631///////////////////////////////////////////////////////////////////////////////
632// Drawing
633///////////////////////////////////////////////////////////////////////////////
634
Romain Guyc1396e92010-06-30 17:56:19 -0700635void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700636 const float right = left + bitmap->width();
637 const float bottom = top + bitmap->height();
638
639 if (quickReject(left, top, right, bottom)) {
640 return;
641 }
642
Romain Guy61c8c9c2010-08-09 20:48:09 -0700643 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700644 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700645 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700646 const AutoTexture autoCleanup(texture);
647
Romain Guy6926c722010-07-12 20:20:03 -0700648 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700649}
650
Romain Guyf86ef572010-07-01 11:05:42 -0700651void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
652 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
653 const mat4 transform(*matrix);
654 transform.mapRect(r);
655
Romain Guy6926c722010-07-12 20:20:03 -0700656 if (quickReject(r.left, r.top, r.right, r.bottom)) {
657 return;
658 }
659
Romain Guy61c8c9c2010-08-09 20:48:09 -0700660 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700661 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700662 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700663 const AutoTexture autoCleanup(texture);
664
Romain Guy82ba8142010-07-09 13:25:56 -0700665 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700666}
667
Romain Guy8ba548f2010-06-30 19:21:21 -0700668void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
669 float srcLeft, float srcTop, float srcRight, float srcBottom,
670 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700671 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700672 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
673 return;
674 }
675
Romain Guy61c8c9c2010-08-09 20:48:09 -0700676 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700677 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700678 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700679 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700680
Romain Guy8ba548f2010-06-30 19:21:21 -0700681 const float width = texture->width;
682 const float height = texture->height;
683
684 const float u1 = srcLeft / width;
685 const float v1 = srcTop / height;
686 const float u2 = srcRight / width;
687 const float v2 = srcBottom / height;
688
689 resetDrawTextureTexCoords(u1, v1, u2, v2);
690
Romain Guy82ba8142010-07-09 13:25:56 -0700691 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700692
693 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
694}
695
Romain Guy4aa90572010-09-26 18:40:37 -0700696void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
697 uint32_t width, uint32_t height, float left, float top, float right, float bottom,
698 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700699 if (quickReject(left, top, right, bottom)) {
700 return;
701 }
702
Romain Guy61c8c9c2010-08-09 20:48:09 -0700703 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700704 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700705 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700706 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700707
708 int alpha;
709 SkXfermode::Mode mode;
710 getAlphaAndMode(paint, &alpha, &mode);
711
Romain Guy2728f962010-10-08 18:36:15 -0700712 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
713 right - left, bottom - top, xDivs, yDivs, width, height);
Romain Guyf7f93552010-07-08 19:17:03 -0700714
715 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
716 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700717 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
718 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy6820ac82010-09-15 18:11:50 -0700719 &mesh->vertices[0].texture[0], GL_TRIANGLES, mesh->verticesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700720}
721
Romain Guy759ea802010-09-16 20:49:46 -0700722void OpenGLRenderer::drawLines(float* points, int count, const SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -0700723 if (mSnapshot->invisible) return;
724
Romain Guy759ea802010-09-16 20:49:46 -0700725 int alpha;
726 SkXfermode::Mode mode;
727 getAlphaAndMode(paint, &alpha, &mode);
728
729 uint32_t color = paint->getColor();
730 const GLfloat a = alpha / 255.0f;
731 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
732 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
733 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
734
Romain Guyc95c8d62010-09-17 15:31:32 -0700735 const bool isAA = paint->isAntiAlias();
736 if (isAA) {
737 GLuint textureUnit = 0;
Romain Guy29d89972010-09-22 16:10:57 -0700738 setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
739 mode, false, true, mCaches.line.getVertices(), mCaches.line.getTexCoords());
Romain Guyc95c8d62010-09-17 15:31:32 -0700740 } else {
741 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false);
742 }
743
744 const float strokeWidth = paint->getStrokeWidth();
Romain Guy29d89972010-09-22 16:10:57 -0700745 const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount;
Romain Guyc95c8d62010-09-17 15:31:32 -0700746 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
Romain Guy759ea802010-09-16 20:49:46 -0700747
748 for (int i = 0; i < count; i += 4) {
749 float tx = 0.0f;
750 float ty = 0.0f;
751
Romain Guyc95c8d62010-09-17 15:31:32 -0700752 if (isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700753 mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3],
Romain Guyc95c8d62010-09-17 15:31:32 -0700754 strokeWidth, tx, ty);
755 } else {
Romain Guyb5ab4172010-09-17 15:36:56 -0700756 ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f;
Romain Guyc95c8d62010-09-17 15:31:32 -0700757 }
Romain Guy759ea802010-09-16 20:49:46 -0700758
759 const float dx = points[i + 2] - points[i];
760 const float dy = points[i + 3] - points[i + 1];
761 const float mag = sqrtf(dx * dx + dy * dy);
762 const float angle = acos(dx / mag);
763
764 mModelView.loadTranslate(points[i], points[i + 1], 0.0f);
765 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) {
766 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f);
767 }
768 mModelView.translate(tx, ty, 0.0f);
Romain Guyc95c8d62010-09-17 15:31:32 -0700769 if (!isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700770 float length = mCaches.line.getLength(points[i], points[i + 1],
771 points[i + 2], points[i + 3]);
Romain Guyc95c8d62010-09-17 15:31:32 -0700772 mModelView.scale(length, strokeWidth, 1.0f);
773 }
Romain Guy759ea802010-09-16 20:49:46 -0700774 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
775
776 if (mShader) {
777 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot);
778 }
779
Romain Guyc95c8d62010-09-17 15:31:32 -0700780 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy759ea802010-09-16 20:49:46 -0700781 }
782
Romain Guy29d89972010-09-22 16:10:57 -0700783 if (isAA) {
784 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
785 }
Romain Guy759ea802010-09-16 20:49:46 -0700786}
787
Romain Guy85bf02f2010-06-22 13:11:24 -0700788void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700789 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700790 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700791}
Romain Guy9d5316e2010-06-24 19:30:36 -0700792
Romain Guybd6b79b2010-06-26 00:13:53 -0700793void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700794 if (quickReject(left, top, right, bottom)) {
795 return;
796 }
797
Romain Guy026c5e162010-06-28 17:12:22 -0700798 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700799 if (!mExtensions.hasFramebufferFetch()) {
800 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
801 if (!isMode) {
802 // Assume SRC_OVER
803 mode = SkXfermode::kSrcOver_Mode;
804 }
805 } else {
806 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700807 }
808
809 // Skia draws using the color's alpha channel if < 255
810 // Otherwise, it uses the paint's alpha
811 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700812 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700813 color |= p->getAlpha() << 24;
814 }
815
816 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700817}
Romain Guy9d5316e2010-06-24 19:30:36 -0700818
Romain Guye8e62a42010-07-23 18:55:21 -0700819void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
820 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700821 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700822 return;
823 }
Romain Guydbc26d22010-10-11 17:58:29 -0700824 if (mSnapshot->invisible) return;
Romain Guye2d345e2010-09-24 18:39:22 -0700825
Romain Guyf607bdc2010-09-10 19:20:06 -0700826 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700827
Romain Guya674ab72010-08-10 17:26:42 -0700828 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700829 switch (paint->getTextAlign()) {
830 case SkPaint::kCenter_Align:
831 length = paint->measureText(text, bytesCount);
832 x -= length / 2.0f;
833 break;
834 case SkPaint::kRight_Align:
835 length = paint->measureText(text, bytesCount);
836 x -= length;
837 break;
838 default:
839 break;
840 }
841
Romain Guy694b5192010-07-21 21:33:20 -0700842 int alpha;
843 SkXfermode::Mode mode;
844 getAlphaAndMode(paint, &alpha, &mode);
845
Romain Guy2542d192010-08-18 11:47:12 -0700846 uint32_t color = paint->getColor();
847 const GLfloat a = alpha / 255.0f;
848 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
849 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
850 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
851
Romain Guyb45c0c92010-08-26 20:35:23 -0700852 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
853 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700854 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -0700855
Romain Guy1e45aae2010-08-13 19:39:53 -0700856 if (mHasShadow) {
857 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700858 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700859 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700860 count, mShadowRadius);
861 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700862
Romain Guy2542d192010-08-18 11:47:12 -0700863 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700864
865 // Draw the mesh
866 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700867 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700868 }
869
Romain Guy06f96e22010-07-30 19:18:16 -0700870 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700871 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700872
Romain Guye8cb9c142010-10-04 14:14:11 -0700873 // Assume that the modelView matrix does not force scales, rotates, etc.
874 const bool linearFilter = mSnapshot->transform->changesBounds();
875 setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit,
876 x, y, r, g, b, a, mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700877
Romain Guy09147fb2010-07-22 13:08:20 -0700878 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy86942302010-09-12 13:02:16 -0700879 clearLayerRegions();
Romain Guyb45c0c92010-08-26 20:35:23 -0700880 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700881
882 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700883 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700884
Romain Guy0a417492010-08-16 20:26:20 -0700885 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -0700886}
887
Romain Guy7fbcc042010-08-04 15:40:07 -0700888void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -0700889 if (mSnapshot->invisible) return;
890
Romain Guy7fbcc042010-08-04 15:40:07 -0700891 GLuint textureUnit = 0;
892 glActiveTexture(gTextureUnits[textureUnit]);
893
Romain Guyfb8b7632010-08-23 21:05:08 -0700894 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700895 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700896 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700897
Romain Guy8b55f372010-08-18 17:10:07 -0700898 const float x = texture->left - texture->offset;
899 const float y = texture->top - texture->offset;
900
901 if (quickReject(x, y, x + texture->width, y + texture->height)) {
902 return;
903 }
904
Romain Guy7fbcc042010-08-04 15:40:07 -0700905 int alpha;
906 SkXfermode::Mode mode;
907 getAlphaAndMode(paint, &alpha, &mode);
908
909 uint32_t color = paint->getColor();
910 const GLfloat a = alpha / 255.0f;
911 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
912 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
913 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
914
Romain Guy0a417492010-08-16 20:26:20 -0700915 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
916
Romain Guy86942302010-09-12 13:02:16 -0700917 clearLayerRegions();
918
Romain Guy0a417492010-08-16 20:26:20 -0700919 // Draw the mesh
920 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700921 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700922}
923
Romain Guy6926c722010-07-12 20:20:03 -0700924///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700925// Shaders
926///////////////////////////////////////////////////////////////////////////////
927
928void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700929 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700930}
931
Romain Guy06f96e22010-07-30 19:18:16 -0700932void OpenGLRenderer::setupShader(SkiaShader* shader) {
933 mShader = shader;
934 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700935 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700936 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700937}
938
Romain Guyd27977d2010-07-14 19:18:51 -0700939///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700940// Color filters
941///////////////////////////////////////////////////////////////////////////////
942
943void OpenGLRenderer::resetColorFilter() {
944 mColorFilter = NULL;
945}
946
947void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
948 mColorFilter = filter;
949}
950
951///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700952// Drop shadow
953///////////////////////////////////////////////////////////////////////////////
954
955void OpenGLRenderer::resetShadow() {
956 mHasShadow = false;
957}
958
959void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
960 mHasShadow = true;
961 mShadowRadius = radius;
962 mShadowDx = dx;
963 mShadowDy = dy;
964 mShadowColor = color;
965}
966
967///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700968// Drawing implementation
969///////////////////////////////////////////////////////////////////////////////
970
Romain Guy0a417492010-08-16 20:26:20 -0700971void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700972 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700973 const float sx = x - texture->left + mShadowDx;
974 const float sy = y - texture->top + mShadowDy;
975
Romain Guy2542d192010-08-18 11:47:12 -0700976 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
977 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700978 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
979 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
980 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
981
982 GLuint textureUnit = 0;
983 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
984}
985
986void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
987 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
988 bool transforms, bool applyFilters) {
989 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -0700990 x, y, r, g, b, a, mode, transforms, applyFilters,
991 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
Romain Guy0a417492010-08-16 20:26:20 -0700992}
993
994void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
995 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
996 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy759ea802010-09-16 20:49:46 -0700997 setupTextureAlpha8(texture, width, height, textureUnit,
998 x, y, r, g, b, a, mode, transforms, applyFilters,
999 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1000}
1001
1002void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1003 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1004 SkXfermode::Mode mode, bool transforms, bool applyFilters,
1005 GLvoid* vertices, GLvoid* texCoords) {
Romain Guy0a417492010-08-16 20:26:20 -07001006 // Describe the required shaders
1007 ProgramDescription description;
1008 description.hasTexture = true;
1009 description.hasAlpha8Texture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001010 const bool setColor = description.setAlpha8Color(r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -07001011
1012 if (applyFilters) {
1013 if (mShader) {
1014 mShader->describe(description, mExtensions);
1015 }
1016 if (mColorFilter) {
1017 mColorFilter->describe(description, mExtensions);
1018 }
1019 }
1020
Romain Guya5aed0d2010-09-09 14:42:43 -07001021 // Setup the blending mode
1022 chooseBlending(true, mode, description);
1023
Romain Guy0a417492010-08-16 20:26:20 -07001024 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001025 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -07001026
Romain Guy0a417492010-08-16 20:26:20 -07001027 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -07001028 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001029
Romain Guyfb8b7632010-08-23 21:05:08 -07001030 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -07001031 glEnableVertexAttribArray(texCoordsSlot);
1032
1033 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -07001034 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -07001035 gMeshStride, vertices);
Romain Guy0a417492010-08-16 20:26:20 -07001036 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -07001037 gMeshStride, texCoords);
Romain Guy0a417492010-08-16 20:26:20 -07001038
1039 // Setup uniforms
1040 if (transforms) {
1041 mModelView.loadTranslate(x, y, 0.0f);
1042 mModelView.scale(width, height, 1.0f);
1043 } else {
1044 mModelView.loadIdentity();
1045 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001046 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy707b2f72010-10-11 16:34:59 -07001047 if (setColor) {
1048 mCaches.currentProgram->setColor(r, g, b, a);
1049 }
Romain Guy0a417492010-08-16 20:26:20 -07001050
1051 textureUnit++;
1052 if (applyFilters) {
1053 // Setup attributes and uniforms required by the shaders
1054 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001055 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001056 }
1057 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001058 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -07001059 }
1060 }
1061}
1062
Romain Guyf607bdc2010-09-10 19:20:06 -07001063// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001064#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1065#define kStdUnderline_Offset (1.0f / 9.0f)
1066#define kStdUnderline_Thickness (1.0f / 18.0f)
1067
1068void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1069 float x, float y, SkPaint* paint) {
1070 // Handle underline and strike-through
1071 uint32_t flags = paint->getFlags();
1072 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1073 float underlineWidth = length;
1074 // If length is > 0.0f, we already measured the text for the text alignment
1075 if (length <= 0.0f) {
1076 underlineWidth = paint->measureText(text, bytesCount);
1077 }
1078
1079 float offsetX = 0;
1080 switch (paint->getTextAlign()) {
1081 case SkPaint::kCenter_Align:
1082 offsetX = underlineWidth * 0.5f;
1083 break;
1084 case SkPaint::kRight_Align:
1085 offsetX = underlineWidth;
1086 break;
1087 default:
1088 break;
1089 }
1090
1091 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001092 const float textSize = paint->getTextSize();
1093 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -07001094
Romain Guye20ecbd2010-09-22 19:49:04 -07001095 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001096 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001097
1098 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
1099 float points[pointsCount];
1100 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001101
1102 if (flags & SkPaint::kUnderlineText_Flag) {
1103 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001104 points[currentPoint++] = left;
1105 points[currentPoint++] = top;
1106 points[currentPoint++] = left + underlineWidth;
1107 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001108 }
1109
1110 if (flags & SkPaint::kStrikeThruText_Flag) {
1111 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001112 points[currentPoint++] = left;
1113 points[currentPoint++] = top;
1114 points[currentPoint++] = left + underlineWidth;
1115 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001116 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001117
1118 SkPaint linesPaint(*paint);
1119 linesPaint.setStrokeWidth(strokeWidth);
1120
1121 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001122 }
1123 }
1124}
1125
Romain Guy026c5e162010-06-28 17:12:22 -07001126void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001127 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001128 clearLayerRegions();
1129
Romain Guyd27977d2010-07-14 19:18:51 -07001130 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001131 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001132 color |= 0x00ffffff;
1133 }
1134
1135 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -07001136 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -07001137 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -07001138 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1139 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1140 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1141
Romain Guyc95c8d62010-09-17 15:31:32 -07001142 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform);
1143
1144 // Draw the mesh
1145 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1146}
1147
1148void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom,
1149 float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy06f96e22010-07-30 19:18:16 -07001150 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -07001151
Romain Guy06f96e22010-07-30 19:18:16 -07001152 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -07001153 ProgramDescription description;
Romain Guy707b2f72010-10-11 16:34:59 -07001154 const bool setColor = description.setColor(r, g, b, a);
1155
Romain Guy06f96e22010-07-30 19:18:16 -07001156 if (mShader) {
1157 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -07001158 }
Romain Guydb1938e2010-08-02 18:50:22 -07001159 if (mColorFilter) {
1160 mColorFilter->describe(description, mExtensions);
1161 }
Romain Guyd27977d2010-07-14 19:18:51 -07001162
Romain Guy1c740bc2010-09-13 18:00:09 -07001163 // Setup the blending mode
Romain Guyc95c8d62010-09-17 15:31:32 -07001164 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -07001165
Romain Guy06f96e22010-07-30 19:18:16 -07001166 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001167 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -07001168
1169 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -07001170 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -07001171 gMeshStride, &mMeshVertices[0].position[0]);
1172
1173 // Setup uniforms
1174 mModelView.loadTranslate(left, top, 0.0f);
1175 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -07001176 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001177 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -07001178 } else {
1179 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -07001180 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -07001181 }
Romain Guy707b2f72010-10-11 16:34:59 -07001182 mCaches.currentProgram->setColor(r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -07001183
Romain Guy06f96e22010-07-30 19:18:16 -07001184 // Setup attributes and uniforms required by the shaders
1185 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001186 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001187 }
Romain Guydb1938e2010-08-02 18:50:22 -07001188 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001189 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001190 }
Romain Guyd27977d2010-07-14 19:18:51 -07001191}
1192
Romain Guy82ba8142010-07-09 13:25:56 -07001193void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001194 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001195 int alpha;
1196 SkXfermode::Mode mode;
1197 getAlphaAndMode(paint, &alpha, &mode);
1198
Romain Guy6820ac82010-09-15 18:11:50 -07001199 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1200 texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1201 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -07001202}
1203
Romain Guybd6b79b2010-06-26 00:13:53 -07001204void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001205 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1206 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001207 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1208 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001209}
1210
1211void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001212 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001213 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guyf607bdc2010-09-10 19:20:06 -07001214 bool swapSrcDst, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001215 clearLayerRegions();
1216
Romain Guy889f8d12010-07-29 14:37:42 -07001217 ProgramDescription description;
1218 description.hasTexture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001219 const bool setColor = description.setColor(alpha, alpha, alpha, alpha);
Romain Guydb1938e2010-08-02 18:50:22 -07001220 if (mColorFilter) {
1221 mColorFilter->describe(description, mExtensions);
1222 }
Romain Guy889f8d12010-07-29 14:37:42 -07001223
Romain Guybd6b79b2010-06-26 00:13:53 -07001224 mModelView.loadTranslate(left, top, 0.0f);
1225 mModelView.scale(right - left, bottom - top, 1.0f);
1226
Romain Guyf607bdc2010-09-10 19:20:06 -07001227 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001228
Romain Guyfb8b7632010-08-23 21:05:08 -07001229 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001230 if (!ignoreTransform) {
1231 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1232 } else {
1233 mat4 m;
1234 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
1235 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001236
Romain Guy889f8d12010-07-29 14:37:42 -07001237 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -07001238 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001239 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001240
Romain Guya9794742010-07-13 11:37:54 -07001241 // Always premultiplied
Romain Guy707b2f72010-10-11 16:34:59 -07001242 if (setColor) {
1243 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha);
1244 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001245
Romain Guy889f8d12010-07-29 14:37:42 -07001246 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001247 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001248 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -07001249 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001250 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001251 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001252
Romain Guydb1938e2010-08-02 18:50:22 -07001253 // Color filter
1254 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001255 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001256 }
1257
Romain Guy6820ac82010-09-15 18:11:50 -07001258 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001259 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001260}
1261
Romain Guya5aed0d2010-09-09 14:42:43 -07001262void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001263 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001264 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1265 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001266 if (mode < SkXfermode::kPlus_Mode) {
1267 if (!mCaches.blend) {
1268 glEnable(GL_BLEND);
1269 }
Romain Guy82ba8142010-07-09 13:25:56 -07001270
Romain Guyf607bdc2010-09-10 19:20:06 -07001271 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1272 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001273
Romain Guya5aed0d2010-09-09 14:42:43 -07001274 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1275 glBlendFunc(sourceMode, destMode);
1276 mCaches.lastSrcMode = sourceMode;
1277 mCaches.lastDstMode = destMode;
1278 }
1279 } else {
1280 // These blend modes are not supported by OpenGL directly and have
1281 // to be implemented using shaders. Since the shader will perform
1282 // the blending, turn blending off here
1283 if (mExtensions.hasFramebufferFetch()) {
1284 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001285 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001286 }
1287
1288 if (mCaches.blend) {
1289 glDisable(GL_BLEND);
1290 }
1291 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001292 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001293 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001294 glDisable(GL_BLEND);
1295 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001296 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001297}
1298
Romain Guy889f8d12010-07-29 14:37:42 -07001299bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001300 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001301 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001302 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001303 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001304 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001305 }
Romain Guy6926c722010-07-12 20:20:03 -07001306 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001307}
1308
Romain Guy026c5e162010-06-28 17:12:22 -07001309void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001310 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001311 TextureVertex::setUV(v++, u1, v1);
1312 TextureVertex::setUV(v++, u2, v1);
1313 TextureVertex::setUV(v++, u1, v2);
1314 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001315}
1316
1317void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1318 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001319 if (!mExtensions.hasFramebufferFetch()) {
1320 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1321 if (!isMode) {
1322 // Assume SRC_OVER
1323 *mode = SkXfermode::kSrcOver_Mode;
1324 }
1325 } else {
1326 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001327 }
1328
1329 // Skia draws using the color's alpha channel if < 255
1330 // Otherwise, it uses the paint's alpha
1331 int color = paint->getColor();
1332 *alpha = (color >> 24) & 0xFF;
1333 if (*alpha == 255) {
1334 *alpha = paint->getAlpha();
1335 }
1336 } else {
1337 *mode = SkXfermode::kSrcOver_Mode;
1338 *alpha = 255;
1339 }
Romain Guy026c5e162010-06-28 17:12:22 -07001340}
1341
Romain Guya5aed0d2010-09-09 14:42:43 -07001342SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1343 if (mode == NULL) {
1344 return SkXfermode::kSrcOver_Mode;
1345 }
1346 return mode->fMode;
1347}
1348
Romain Guy889f8d12010-07-29 14:37:42 -07001349void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1350 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001351 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001352 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1353 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1354}
1355
Romain Guy9d5316e2010-06-24 19:30:36 -07001356}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001357}; // namespace android