blob: 287601092f20dda49660c8f3a8d5a16d6e6df063 [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 Guyeb993562010-10-05 18:14:38 -0700428 // Bind texture to FBO
429 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
430 glBindTexture(GL_TEXTURE_2D, layer->texture);
431
432 // Initialize the texture if needed
433 if (layer->empty) {
434 layer->empty = false;
Romain Guy8550c4c2010-10-08 15:49:53 -0700435 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
Romain Guyeb993562010-10-05 18:14:38 -0700436 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
437 }
438
439 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
440 layer->texture, 0);
441
Romain Guye9108052010-10-12 18:15:42 -0700442#if DEBUG_LAYERS
Romain Guyeb993562010-10-05 18:14:38 -0700443 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
444 if (status != GL_FRAMEBUFFER_COMPLETE) {
445 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
446
447 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
448 glDeleteTextures(1, &layer->texture);
449 mCaches.fboCache.put(layer->fbo);
450
451 delete layer;
452
453 return false;
454 }
Romain Guye9108052010-10-12 18:15:42 -0700455#endif
Romain Guyeb993562010-10-05 18:14:38 -0700456
457 // Clear the FBO
Romain Guy93d23612010-10-13 18:26:36 -0700458 glScissor(0.0f, 0.0f, bounds.getWidth() + 1.0f, bounds.getHeight() + 1.0f);
Romain Guyeb993562010-10-05 18:14:38 -0700459 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
460 glClear(GL_COLOR_BUFFER_BIT);
Romain Guyeb07af62010-10-12 18:40:36 -0700461
462 setScissorFromClip();
Romain Guyeb993562010-10-05 18:14:38 -0700463
464 // Change the ortho projection
465 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
466 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
467 } else {
468 // Copy the framebuffer into the layer
469 glBindTexture(GL_TEXTURE_2D, layer->texture);
470
Romain Guyc00972b2010-10-12 11:31:07 -0700471 if (layer->empty) {
472 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
473 layer->width, layer->height, 0);
474 layer->empty = false;
475 } else {
476 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, mHeight - bounds.bottom,
477 bounds.getWidth(), bounds.getHeight());
478 }
Romain Guyeb993562010-10-05 18:14:38 -0700479
480 // Enqueue the buffer coordinates to clear the corresponding region later
481 mLayers.push(new Rect(bounds));
482 }
Romain Guyf86ef572010-07-01 11:05:42 -0700483
Romain Guyd55a8612010-06-28 17:42:46 -0700484 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700485}
486
Romain Guy1c740bc2010-09-13 18:00:09 -0700487/**
488 * Read the documentation of createLayer() before doing anything in this method.
489 */
Romain Guy1d83e192010-08-17 11:37:00 -0700490void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
491 if (!current->layer) {
492 LOGE("Attempting to compose a layer that does not exist");
493 return;
494 }
495
Romain Guyeb993562010-10-05 18:14:38 -0700496 const bool fboLayer = current->flags & SkCanvas::kClipToLayer_SaveFlag;
497
498 if (fboLayer) {
499 // Unbind current FBO and restore previous one
500 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
501 }
502
Romain Guy1d83e192010-08-17 11:37:00 -0700503 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700504 const Rect& clip = *previous->clipRect;
Romain Guyeb993562010-10-05 18:14:38 -0700505 glScissor(clip.left, previous->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700506
507 Layer* layer = current->layer;
508 const Rect& rect = layer->layer;
509
Romain Guyeb993562010-10-05 18:14:38 -0700510 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700511 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700512 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guyf607bdc2010-09-10 19:20:06 -0700513 }
514
Romain Guy8550c4c2010-10-08 15:49:53 -0700515 const Rect& texCoords = layer->texCoords;
516 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
Romain Guy8b55f372010-08-18 17:10:07 -0700517
Romain Guyeb993562010-10-05 18:14:38 -0700518 if (fboLayer) {
519 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
520 layer->texture, layer->alpha / 255.0f, layer->mode, layer->blend);
521 } else {
522 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
523 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
524 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, true, true);
525 }
Romain Guy1d83e192010-08-17 11:37:00 -0700526
Romain Guy8b55f372010-08-18 17:10:07 -0700527 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
528
Romain Guyeb993562010-10-05 18:14:38 -0700529 if (fboLayer) {
530 // Detach the texture from the FBO
531 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
532 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
533 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
534
535 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
536 mCaches.fboCache.put(current->fbo);
537 }
538
Romain Guyeb993562010-10-05 18:14:38 -0700539 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700540 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700541 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700542 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700543 delete layer;
544 }
545}
546
Romain Guy86942302010-09-12 13:02:16 -0700547void OpenGLRenderer::clearLayerRegions() {
Romain Guydbc26d22010-10-11 17:58:29 -0700548 if (mLayers.size() == 0 || mSnapshot->invisible) return;
Romain Guy86942302010-09-12 13:02:16 -0700549
550 for (uint32_t i = 0; i < mLayers.size(); i++) {
551 Rect* bounds = mLayers.itemAt(i);
552
553 // Clear the framebuffer where the layer will draw
554 glScissor(bounds->left, mHeight - bounds->bottom,
555 bounds->getWidth(), bounds->getHeight());
556 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
557 glClear(GL_COLOR_BUFFER_BIT);
558
559 delete bounds;
560 }
561 mLayers.clear();
562
563 // Restore the clip
564 setScissorFromClip();
565}
566
Romain Guybd6b79b2010-06-26 00:13:53 -0700567///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700568// Transforms
569///////////////////////////////////////////////////////////////////////////////
570
571void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700572 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700573}
574
575void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700576 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700577}
578
579void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700580 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700581}
582
583void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700584 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700585}
586
Romain Guy41030da2010-10-13 13:40:37 -0700587const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700588 if (mSnapshot->fbo != 0) {
589 return &mSnapshot->transform->data[0];
590 }
591 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700592}
593
Romain Guyf6a11b82010-06-23 17:47:49 -0700594void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700595 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700596}
597
598void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700599 mat4 m(*matrix);
Romain Guy8aef54f2010-09-01 15:13:49 -0700600 mSnapshot->transform->multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700601}
602
603///////////////////////////////////////////////////////////////////////////////
604// Clipping
605///////////////////////////////////////////////////////////////////////////////
606
Romain Guybb9524b2010-06-22 18:56:38 -0700607void OpenGLRenderer::setScissorFromClip() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700608 const Rect& clip = *mSnapshot->clipRect;
Romain Guyeb993562010-10-05 18:14:38 -0700609 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700610}
611
612const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700613 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700614}
615
Romain Guyc7d53492010-06-25 13:41:57 -0700616bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guydbc26d22010-10-11 17:58:29 -0700617 if (mSnapshot->invisible) {
618 return true;
619 }
620
Romain Guy1d83e192010-08-17 11:37:00 -0700621 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700622 mSnapshot->transform->mapRect(r);
623 return !mSnapshot->clipRect->intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700624}
625
Romain Guy079ba2c2010-07-16 14:12:24 -0700626bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
627 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700628 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700629 setScissorFromClip();
630 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700631 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700632}
633
Romain Guyf6a11b82010-06-23 17:47:49 -0700634///////////////////////////////////////////////////////////////////////////////
635// Drawing
636///////////////////////////////////////////////////////////////////////////////
637
Romain Guyc1396e92010-06-30 17:56:19 -0700638void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700639 const float right = left + bitmap->width();
640 const float bottom = top + bitmap->height();
641
642 if (quickReject(left, top, right, bottom)) {
643 return;
644 }
645
Romain Guy61c8c9c2010-08-09 20:48:09 -0700646 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700647 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700648 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700649 const AutoTexture autoCleanup(texture);
650
Romain Guy6926c722010-07-12 20:20:03 -0700651 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700652}
653
Romain Guyf86ef572010-07-01 11:05:42 -0700654void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
655 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
656 const mat4 transform(*matrix);
657 transform.mapRect(r);
658
Romain Guy6926c722010-07-12 20:20:03 -0700659 if (quickReject(r.left, r.top, r.right, r.bottom)) {
660 return;
661 }
662
Romain Guy61c8c9c2010-08-09 20:48:09 -0700663 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700664 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700665 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700666 const AutoTexture autoCleanup(texture);
667
Romain Guy82ba8142010-07-09 13:25:56 -0700668 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700669}
670
Romain Guy8ba548f2010-06-30 19:21:21 -0700671void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
672 float srcLeft, float srcTop, float srcRight, float srcBottom,
673 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700674 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700675 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
676 return;
677 }
678
Romain Guy61c8c9c2010-08-09 20:48:09 -0700679 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700680 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700681 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700682 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700683
Romain Guy8ba548f2010-06-30 19:21:21 -0700684 const float width = texture->width;
685 const float height = texture->height;
686
687 const float u1 = srcLeft / width;
688 const float v1 = srcTop / height;
689 const float u2 = srcRight / width;
690 const float v2 = srcBottom / height;
691
692 resetDrawTextureTexCoords(u1, v1, u2, v2);
693
Romain Guy82ba8142010-07-09 13:25:56 -0700694 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700695
696 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
697}
698
Romain Guy4aa90572010-09-26 18:40:37 -0700699void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -0700700 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
701 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700702 if (quickReject(left, top, right, bottom)) {
703 return;
704 }
705
Romain Guy61c8c9c2010-08-09 20:48:09 -0700706 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700707 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700708 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700709 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700710
711 int alpha;
712 SkXfermode::Mode mode;
713 getAlphaAndMode(paint, &alpha, &mode);
714
Romain Guy2728f962010-10-08 18:36:15 -0700715 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -0700716 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -0700717
718 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
719 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700720 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
721 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy6820ac82010-09-15 18:11:50 -0700722 &mesh->vertices[0].texture[0], GL_TRIANGLES, mesh->verticesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700723}
724
Romain Guy759ea802010-09-16 20:49:46 -0700725void OpenGLRenderer::drawLines(float* points, int count, const SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -0700726 if (mSnapshot->invisible) return;
727
Romain Guy759ea802010-09-16 20:49:46 -0700728 int alpha;
729 SkXfermode::Mode mode;
730 getAlphaAndMode(paint, &alpha, &mode);
731
732 uint32_t color = paint->getColor();
733 const GLfloat a = alpha / 255.0f;
734 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
735 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
736 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
737
Romain Guyc95c8d62010-09-17 15:31:32 -0700738 const bool isAA = paint->isAntiAlias();
739 if (isAA) {
740 GLuint textureUnit = 0;
Romain Guy29d89972010-09-22 16:10:57 -0700741 setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
742 mode, false, true, mCaches.line.getVertices(), mCaches.line.getTexCoords());
Romain Guyc95c8d62010-09-17 15:31:32 -0700743 } else {
744 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false);
745 }
746
747 const float strokeWidth = paint->getStrokeWidth();
Romain Guy29d89972010-09-22 16:10:57 -0700748 const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount;
Romain Guyc95c8d62010-09-17 15:31:32 -0700749 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
Romain Guy759ea802010-09-16 20:49:46 -0700750
751 for (int i = 0; i < count; i += 4) {
752 float tx = 0.0f;
753 float ty = 0.0f;
754
Romain Guyc95c8d62010-09-17 15:31:32 -0700755 if (isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700756 mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3],
Romain Guyc95c8d62010-09-17 15:31:32 -0700757 strokeWidth, tx, ty);
758 } else {
Romain Guyb5ab4172010-09-17 15:36:56 -0700759 ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f;
Romain Guyc95c8d62010-09-17 15:31:32 -0700760 }
Romain Guy759ea802010-09-16 20:49:46 -0700761
762 const float dx = points[i + 2] - points[i];
763 const float dy = points[i + 3] - points[i + 1];
764 const float mag = sqrtf(dx * dx + dy * dy);
765 const float angle = acos(dx / mag);
766
767 mModelView.loadTranslate(points[i], points[i + 1], 0.0f);
768 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) {
769 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f);
770 }
771 mModelView.translate(tx, ty, 0.0f);
Romain Guyc95c8d62010-09-17 15:31:32 -0700772 if (!isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700773 float length = mCaches.line.getLength(points[i], points[i + 1],
774 points[i + 2], points[i + 3]);
Romain Guyc95c8d62010-09-17 15:31:32 -0700775 mModelView.scale(length, strokeWidth, 1.0f);
776 }
Romain Guy759ea802010-09-16 20:49:46 -0700777 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
778
779 if (mShader) {
780 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot);
781 }
782
Romain Guyc95c8d62010-09-17 15:31:32 -0700783 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy759ea802010-09-16 20:49:46 -0700784 }
785
Romain Guy29d89972010-09-22 16:10:57 -0700786 if (isAA) {
787 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
788 }
Romain Guy759ea802010-09-16 20:49:46 -0700789}
790
Romain Guy85bf02f2010-06-22 13:11:24 -0700791void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700792 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700793 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700794}
Romain Guy9d5316e2010-06-24 19:30:36 -0700795
Romain Guybd6b79b2010-06-26 00:13:53 -0700796void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700797 if (quickReject(left, top, right, bottom)) {
798 return;
799 }
800
Romain Guy026c5e162010-06-28 17:12:22 -0700801 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700802 if (!mExtensions.hasFramebufferFetch()) {
803 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
804 if (!isMode) {
805 // Assume SRC_OVER
806 mode = SkXfermode::kSrcOver_Mode;
807 }
808 } else {
809 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700810 }
811
812 // Skia draws using the color's alpha channel if < 255
813 // Otherwise, it uses the paint's alpha
814 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700815 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700816 color |= p->getAlpha() << 24;
817 }
818
819 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700820}
Romain Guy9d5316e2010-06-24 19:30:36 -0700821
Romain Guye8e62a42010-07-23 18:55:21 -0700822void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
823 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700824 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700825 return;
826 }
Romain Guydbc26d22010-10-11 17:58:29 -0700827 if (mSnapshot->invisible) return;
Romain Guye2d345e2010-09-24 18:39:22 -0700828
Romain Guyf607bdc2010-09-10 19:20:06 -0700829 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700830
Romain Guya674ab72010-08-10 17:26:42 -0700831 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700832 switch (paint->getTextAlign()) {
833 case SkPaint::kCenter_Align:
834 length = paint->measureText(text, bytesCount);
835 x -= length / 2.0f;
836 break;
837 case SkPaint::kRight_Align:
838 length = paint->measureText(text, bytesCount);
839 x -= length;
840 break;
841 default:
842 break;
843 }
844
Romain Guy694b5192010-07-21 21:33:20 -0700845 int alpha;
846 SkXfermode::Mode mode;
847 getAlphaAndMode(paint, &alpha, &mode);
848
Romain Guy2542d192010-08-18 11:47:12 -0700849 uint32_t color = paint->getColor();
850 const GLfloat a = alpha / 255.0f;
851 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
852 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
853 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
854
Romain Guyb45c0c92010-08-26 20:35:23 -0700855 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
856 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700857 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -0700858
Romain Guy1e45aae2010-08-13 19:39:53 -0700859 if (mHasShadow) {
860 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700861 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700862 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700863 count, mShadowRadius);
864 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700865
Romain Guy2542d192010-08-18 11:47:12 -0700866 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700867
868 // Draw the mesh
869 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700870 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700871 }
872
Romain Guy06f96e22010-07-30 19:18:16 -0700873 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700874 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700875
Romain Guye8cb9c142010-10-04 14:14:11 -0700876 // Assume that the modelView matrix does not force scales, rotates, etc.
877 const bool linearFilter = mSnapshot->transform->changesBounds();
878 setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit,
879 x, y, r, g, b, a, mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700880
Romain Guy09147fb2010-07-22 13:08:20 -0700881 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy86942302010-09-12 13:02:16 -0700882 clearLayerRegions();
Romain Guyb45c0c92010-08-26 20:35:23 -0700883 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700884
885 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700886 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700887
Romain Guy0a417492010-08-16 20:26:20 -0700888 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -0700889}
890
Romain Guy7fbcc042010-08-04 15:40:07 -0700891void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -0700892 if (mSnapshot->invisible) return;
893
Romain Guy7fbcc042010-08-04 15:40:07 -0700894 GLuint textureUnit = 0;
895 glActiveTexture(gTextureUnits[textureUnit]);
896
Romain Guyfb8b7632010-08-23 21:05:08 -0700897 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700898 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700899 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700900
Romain Guy8b55f372010-08-18 17:10:07 -0700901 const float x = texture->left - texture->offset;
902 const float y = texture->top - texture->offset;
903
904 if (quickReject(x, y, x + texture->width, y + texture->height)) {
905 return;
906 }
907
Romain Guy7fbcc042010-08-04 15:40:07 -0700908 int alpha;
909 SkXfermode::Mode mode;
910 getAlphaAndMode(paint, &alpha, &mode);
911
912 uint32_t color = paint->getColor();
913 const GLfloat a = alpha / 255.0f;
914 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
915 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
916 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
917
Romain Guy0a417492010-08-16 20:26:20 -0700918 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
919
Romain Guy86942302010-09-12 13:02:16 -0700920 clearLayerRegions();
921
Romain Guy0a417492010-08-16 20:26:20 -0700922 // Draw the mesh
923 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700924 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700925}
926
Romain Guy6926c722010-07-12 20:20:03 -0700927///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700928// Shaders
929///////////////////////////////////////////////////////////////////////////////
930
931void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700932 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700933}
934
Romain Guy06f96e22010-07-30 19:18:16 -0700935void OpenGLRenderer::setupShader(SkiaShader* shader) {
936 mShader = shader;
937 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700938 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700939 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700940}
941
Romain Guyd27977d2010-07-14 19:18:51 -0700942///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700943// Color filters
944///////////////////////////////////////////////////////////////////////////////
945
946void OpenGLRenderer::resetColorFilter() {
947 mColorFilter = NULL;
948}
949
950void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
951 mColorFilter = filter;
952}
953
954///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700955// Drop shadow
956///////////////////////////////////////////////////////////////////////////////
957
958void OpenGLRenderer::resetShadow() {
959 mHasShadow = false;
960}
961
962void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
963 mHasShadow = true;
964 mShadowRadius = radius;
965 mShadowDx = dx;
966 mShadowDy = dy;
967 mShadowColor = color;
968}
969
970///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700971// Drawing implementation
972///////////////////////////////////////////////////////////////////////////////
973
Romain Guy0a417492010-08-16 20:26:20 -0700974void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700975 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700976 const float sx = x - texture->left + mShadowDx;
977 const float sy = y - texture->top + mShadowDy;
978
Romain Guy2542d192010-08-18 11:47:12 -0700979 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
980 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700981 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
982 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
983 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
984
985 GLuint textureUnit = 0;
986 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
987}
988
989void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
990 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
991 bool transforms, bool applyFilters) {
992 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -0700993 x, y, r, g, b, a, mode, transforms, applyFilters,
994 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
Romain Guy0a417492010-08-16 20:26:20 -0700995}
996
997void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
998 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
999 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy759ea802010-09-16 20:49:46 -07001000 setupTextureAlpha8(texture, width, height, textureUnit,
1001 x, y, r, g, b, a, mode, transforms, applyFilters,
1002 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1003}
1004
1005void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1006 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1007 SkXfermode::Mode mode, bool transforms, bool applyFilters,
1008 GLvoid* vertices, GLvoid* texCoords) {
Romain Guy0a417492010-08-16 20:26:20 -07001009 // Describe the required shaders
1010 ProgramDescription description;
1011 description.hasTexture = true;
1012 description.hasAlpha8Texture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001013 const bool setColor = description.setAlpha8Color(r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -07001014
1015 if (applyFilters) {
1016 if (mShader) {
1017 mShader->describe(description, mExtensions);
1018 }
1019 if (mColorFilter) {
1020 mColorFilter->describe(description, mExtensions);
1021 }
1022 }
1023
Romain Guya5aed0d2010-09-09 14:42:43 -07001024 // Setup the blending mode
1025 chooseBlending(true, mode, description);
1026
Romain Guy0a417492010-08-16 20:26:20 -07001027 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001028 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -07001029
Romain Guy0a417492010-08-16 20:26:20 -07001030 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -07001031 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001032
Romain Guyfb8b7632010-08-23 21:05:08 -07001033 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -07001034 glEnableVertexAttribArray(texCoordsSlot);
1035
1036 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -07001037 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -07001038 gMeshStride, vertices);
Romain Guy0a417492010-08-16 20:26:20 -07001039 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -07001040 gMeshStride, texCoords);
Romain Guy0a417492010-08-16 20:26:20 -07001041
1042 // Setup uniforms
1043 if (transforms) {
1044 mModelView.loadTranslate(x, y, 0.0f);
1045 mModelView.scale(width, height, 1.0f);
1046 } else {
1047 mModelView.loadIdentity();
1048 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001049 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy707b2f72010-10-11 16:34:59 -07001050 if (setColor) {
1051 mCaches.currentProgram->setColor(r, g, b, a);
1052 }
Romain Guy0a417492010-08-16 20:26:20 -07001053
1054 textureUnit++;
1055 if (applyFilters) {
1056 // Setup attributes and uniforms required by the shaders
1057 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001058 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001059 }
1060 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001061 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -07001062 }
1063 }
1064}
1065
Romain Guyf607bdc2010-09-10 19:20:06 -07001066// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001067#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1068#define kStdUnderline_Offset (1.0f / 9.0f)
1069#define kStdUnderline_Thickness (1.0f / 18.0f)
1070
1071void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1072 float x, float y, SkPaint* paint) {
1073 // Handle underline and strike-through
1074 uint32_t flags = paint->getFlags();
1075 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1076 float underlineWidth = length;
1077 // If length is > 0.0f, we already measured the text for the text alignment
1078 if (length <= 0.0f) {
1079 underlineWidth = paint->measureText(text, bytesCount);
1080 }
1081
1082 float offsetX = 0;
1083 switch (paint->getTextAlign()) {
1084 case SkPaint::kCenter_Align:
1085 offsetX = underlineWidth * 0.5f;
1086 break;
1087 case SkPaint::kRight_Align:
1088 offsetX = underlineWidth;
1089 break;
1090 default:
1091 break;
1092 }
1093
1094 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001095 const float textSize = paint->getTextSize();
1096 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -07001097
Romain Guye20ecbd2010-09-22 19:49:04 -07001098 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001099 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001100
1101 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
1102 float points[pointsCount];
1103 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001104
1105 if (flags & SkPaint::kUnderlineText_Flag) {
1106 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001107 points[currentPoint++] = left;
1108 points[currentPoint++] = top;
1109 points[currentPoint++] = left + underlineWidth;
1110 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001111 }
1112
1113 if (flags & SkPaint::kStrikeThruText_Flag) {
1114 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001115 points[currentPoint++] = left;
1116 points[currentPoint++] = top;
1117 points[currentPoint++] = left + underlineWidth;
1118 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001119 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001120
1121 SkPaint linesPaint(*paint);
1122 linesPaint.setStrokeWidth(strokeWidth);
1123
1124 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001125 }
1126 }
1127}
1128
Romain Guy026c5e162010-06-28 17:12:22 -07001129void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001130 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001131 clearLayerRegions();
1132
Romain Guyd27977d2010-07-14 19:18:51 -07001133 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001134 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001135 color |= 0x00ffffff;
1136 }
1137
1138 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -07001139 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -07001140 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -07001141 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1142 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1143 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1144
Romain Guyc95c8d62010-09-17 15:31:32 -07001145 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform);
1146
1147 // Draw the mesh
1148 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1149}
1150
1151void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom,
1152 float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy06f96e22010-07-30 19:18:16 -07001153 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -07001154
Romain Guy06f96e22010-07-30 19:18:16 -07001155 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -07001156 ProgramDescription description;
Romain Guy707b2f72010-10-11 16:34:59 -07001157 const bool setColor = description.setColor(r, g, b, a);
1158
Romain Guy06f96e22010-07-30 19:18:16 -07001159 if (mShader) {
1160 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -07001161 }
Romain Guydb1938e2010-08-02 18:50:22 -07001162 if (mColorFilter) {
1163 mColorFilter->describe(description, mExtensions);
1164 }
Romain Guyd27977d2010-07-14 19:18:51 -07001165
Romain Guy1c740bc2010-09-13 18:00:09 -07001166 // Setup the blending mode
Romain Guyc95c8d62010-09-17 15:31:32 -07001167 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -07001168
Romain Guy06f96e22010-07-30 19:18:16 -07001169 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001170 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -07001171
1172 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -07001173 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -07001174 gMeshStride, &mMeshVertices[0].position[0]);
1175
1176 // Setup uniforms
1177 mModelView.loadTranslate(left, top, 0.0f);
1178 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -07001179 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001180 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -07001181 } else {
1182 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -07001183 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -07001184 }
Romain Guy707b2f72010-10-11 16:34:59 -07001185 mCaches.currentProgram->setColor(r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -07001186
Romain Guy06f96e22010-07-30 19:18:16 -07001187 // Setup attributes and uniforms required by the shaders
1188 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001189 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001190 }
Romain Guydb1938e2010-08-02 18:50:22 -07001191 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001192 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001193 }
Romain Guyd27977d2010-07-14 19:18:51 -07001194}
1195
Romain Guy82ba8142010-07-09 13:25:56 -07001196void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001197 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001198 int alpha;
1199 SkXfermode::Mode mode;
1200 getAlphaAndMode(paint, &alpha, &mode);
1201
Romain Guy6820ac82010-09-15 18:11:50 -07001202 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1203 texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1204 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -07001205}
1206
Romain Guybd6b79b2010-06-26 00:13:53 -07001207void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001208 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1209 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001210 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1211 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001212}
1213
1214void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001215 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001216 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guyf607bdc2010-09-10 19:20:06 -07001217 bool swapSrcDst, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001218 clearLayerRegions();
1219
Romain Guy889f8d12010-07-29 14:37:42 -07001220 ProgramDescription description;
1221 description.hasTexture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001222 const bool setColor = description.setColor(alpha, alpha, alpha, alpha);
Romain Guydb1938e2010-08-02 18:50:22 -07001223 if (mColorFilter) {
1224 mColorFilter->describe(description, mExtensions);
1225 }
Romain Guy889f8d12010-07-29 14:37:42 -07001226
Romain Guybd6b79b2010-06-26 00:13:53 -07001227 mModelView.loadTranslate(left, top, 0.0f);
1228 mModelView.scale(right - left, bottom - top, 1.0f);
1229
Romain Guyf607bdc2010-09-10 19:20:06 -07001230 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001231
Romain Guyfb8b7632010-08-23 21:05:08 -07001232 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001233 if (!ignoreTransform) {
1234 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1235 } else {
1236 mat4 m;
1237 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
1238 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001239
Romain Guy889f8d12010-07-29 14:37:42 -07001240 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -07001241 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001242 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001243
Romain Guya9794742010-07-13 11:37:54 -07001244 // Always premultiplied
Romain Guy707b2f72010-10-11 16:34:59 -07001245 if (setColor) {
1246 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha);
1247 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001248
Romain Guy889f8d12010-07-29 14:37:42 -07001249 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001250 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001251 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -07001252 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001253 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001254 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001255
Romain Guydb1938e2010-08-02 18:50:22 -07001256 // Color filter
1257 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001258 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001259 }
1260
Romain Guy6820ac82010-09-15 18:11:50 -07001261 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001262 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001263}
1264
Romain Guya5aed0d2010-09-09 14:42:43 -07001265void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001266 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001267 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1268 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001269 if (mode < SkXfermode::kPlus_Mode) {
1270 if (!mCaches.blend) {
1271 glEnable(GL_BLEND);
1272 }
Romain Guy82ba8142010-07-09 13:25:56 -07001273
Romain Guyf607bdc2010-09-10 19:20:06 -07001274 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1275 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001276
Romain Guya5aed0d2010-09-09 14:42:43 -07001277 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1278 glBlendFunc(sourceMode, destMode);
1279 mCaches.lastSrcMode = sourceMode;
1280 mCaches.lastDstMode = destMode;
1281 }
1282 } else {
1283 // These blend modes are not supported by OpenGL directly and have
1284 // to be implemented using shaders. Since the shader will perform
1285 // the blending, turn blending off here
1286 if (mExtensions.hasFramebufferFetch()) {
1287 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001288 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001289 }
1290
1291 if (mCaches.blend) {
1292 glDisable(GL_BLEND);
1293 }
1294 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001295 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001296 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001297 glDisable(GL_BLEND);
1298 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001299 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001300}
1301
Romain Guy889f8d12010-07-29 14:37:42 -07001302bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001303 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001304 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001305 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001306 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001307 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001308 }
Romain Guy6926c722010-07-12 20:20:03 -07001309 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001310}
1311
Romain Guy026c5e162010-06-28 17:12:22 -07001312void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001313 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001314 TextureVertex::setUV(v++, u1, v1);
1315 TextureVertex::setUV(v++, u2, v1);
1316 TextureVertex::setUV(v++, u1, v2);
1317 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001318}
1319
1320void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1321 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001322 if (!mExtensions.hasFramebufferFetch()) {
1323 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1324 if (!isMode) {
1325 // Assume SRC_OVER
1326 *mode = SkXfermode::kSrcOver_Mode;
1327 }
1328 } else {
1329 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001330 }
1331
1332 // Skia draws using the color's alpha channel if < 255
1333 // Otherwise, it uses the paint's alpha
1334 int color = paint->getColor();
1335 *alpha = (color >> 24) & 0xFF;
1336 if (*alpha == 255) {
1337 *alpha = paint->getAlpha();
1338 }
1339 } else {
1340 *mode = SkXfermode::kSrcOver_Mode;
1341 *alpha = 255;
1342 }
Romain Guy026c5e162010-06-28 17:12:22 -07001343}
1344
Romain Guya5aed0d2010-09-09 14:42:43 -07001345SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1346 if (mode == NULL) {
1347 return SkXfermode::kSrcOver_Mode;
1348 }
1349 return mode->fMode;
1350}
1351
Romain Guy889f8d12010-07-29 14:37:42 -07001352void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1353 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001354 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001355 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1356 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1357}
1358
Romain Guy9d5316e2010-06-24 19:30:36 -07001359}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001360}; // namespace android