blob: b16713109f126daf2603fbb3b7882aaf99f0db2a [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 Guy759ea802010-09-16 20:49:46 -070038#define RAD_TO_DEG (180.0f / 3.14159265f)
39#define MIN_ANGLE 0.001f
40
Romain Guydbc26d22010-10-11 17:58:29 -070041// TODO: This should be set in properties
42#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
43
Romain Guy9d5316e2010-06-24 19:30:36 -070044///////////////////////////////////////////////////////////////////////////////
45// Globals
46///////////////////////////////////////////////////////////////////////////////
47
Romain Guy889f8d12010-07-29 14:37:42 -070048/**
49 * Structure mapping Skia xfermodes to OpenGL blending factors.
50 */
51struct Blender {
52 SkXfermode::Mode mode;
53 GLenum src;
54 GLenum dst;
55}; // struct Blender
56
Romain Guy026c5e162010-06-28 17:12:22 -070057// In this array, the index of each Blender equals the value of the first
58// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
59static const Blender gBlends[] = {
60 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
61 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
62 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
63 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
64 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
65 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
66 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
67 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
68 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
69 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
70 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
71 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
72};
Romain Guye4d01122010-06-16 18:44:05 -070073
Romain Guy87a76572010-09-13 18:11:21 -070074// This array contains the swapped version of each SkXfermode. For instance
75// this array's SrcOver blending mode is actually DstOver. You can refer to
76// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070077static const Blender gBlendsSwap[] = {
78 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
79 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
80 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
81 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
82 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
84 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
85 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
86 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
87 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
88 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
89 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
90};
91
Romain Guy889f8d12010-07-29 14:37:42 -070092static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -070093 GL_TEXTURE0,
94 GL_TEXTURE1,
95 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -070096};
97
Romain Guyf6a11b82010-06-23 17:47:49 -070098///////////////////////////////////////////////////////////////////////////////
99// Constructors/destructor
100///////////////////////////////////////////////////////////////////////////////
101
Romain Guyfb8b7632010-08-23 21:05:08 -0700102OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700103 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700104 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700105 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700106
Romain Guyac670c02010-07-27 17:39:27 -0700107 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
108
Romain Guyae5575b2010-07-29 18:48:04 -0700109 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700110}
111
Romain Guy85bf02f2010-06-22 13:11:24 -0700112OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700113 // The context has already been destroyed at this point, do not call
114 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700115}
116
Romain Guyf6a11b82010-06-23 17:47:49 -0700117///////////////////////////////////////////////////////////////////////////////
118// Setup
119///////////////////////////////////////////////////////////////////////////////
120
Romain Guy85bf02f2010-06-22 13:11:24 -0700121void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700122 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700123 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700124
125 mWidth = width;
126 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700127
128 mFirstSnapshot->height = height;
129 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700130
131 mDirtyClip = false;
Romain Guye4d01122010-06-16 18:44:05 -0700132}
133
Romain Guy6b7bd242010-10-06 19:49:23 -0700134void OpenGLRenderer::prepare(bool opaque) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700135 mSnapshot = new Snapshot(mFirstSnapshot,
136 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy8fb95422010-08-17 18:38:51 -0700137 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700138
Romain Guyfb8b7632010-08-23 21:05:08 -0700139 glViewport(0, 0, mWidth, mHeight);
140
Romain Guyd90f23e2010-09-09 11:47:54 -0700141 glDisable(GL_DITHER);
Romain Guybb9524b2010-06-22 18:56:38 -0700142
Romain Guy6b7bd242010-10-06 19:49:23 -0700143 if (!opaque) {
Romain Guy746b7402010-10-26 16:27:31 -0700144 glDisable(GL_SCISSOR_TEST);
Romain Guy6b7bd242010-10-06 19:49:23 -0700145 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
146 glClear(GL_COLOR_BUFFER_BIT);
Romain Guy746b7402010-10-26 16:27:31 -0700147 glEnable(GL_SCISSOR_TEST);
148 } else {
149 glEnable(GL_SCISSOR_TEST);
150 glScissor(0, 0, mWidth, mHeight);
151 dirtyClip();
Romain Guy6b7bd242010-10-06 19:49:23 -0700152 }
Romain Guybb9524b2010-06-22 18:56:38 -0700153
Romain Guyb82da652010-07-30 11:36:12 -0700154 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700155}
156
Romain Guyb025b9c2010-09-16 14:16:48 -0700157void OpenGLRenderer::finish() {
158#if DEBUG_OPENGL
159 GLenum status = GL_NO_ERROR;
160 while ((status = glGetError()) != GL_NO_ERROR) {
161 LOGD("GL error from OpenGLRenderer: 0x%x", status);
162 }
163#endif
164}
165
Romain Guyda8532c2010-08-31 11:50:35 -0700166void OpenGLRenderer::acquireContext() {
167 if (mCaches.currentProgram) {
168 if (mCaches.currentProgram->isInUse()) {
169 mCaches.currentProgram->remove();
170 mCaches.currentProgram = NULL;
171 }
172 }
Romain Guy50c0f092010-10-19 11:42:22 -0700173 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700174}
175
176void OpenGLRenderer::releaseContext() {
Romain Guyeb993562010-10-05 18:14:38 -0700177 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700178
179 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700180 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700181
Romain Guyf607bdc2010-09-10 19:20:06 -0700182 glDisable(GL_DITHER);
183
184 glBindFramebuffer(GL_FRAMEBUFFER, 0);
Romain Guy03750a02010-10-18 14:06:08 -0700185 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700186
Romain Guy50c0f092010-10-19 11:42:22 -0700187 mCaches.blend = true;
188 glEnable(GL_BLEND);
189 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
190 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700191}
192
Romain Guyf6a11b82010-06-23 17:47:49 -0700193///////////////////////////////////////////////////////////////////////////////
194// State management
195///////////////////////////////////////////////////////////////////////////////
196
Romain Guybb9524b2010-06-22 18:56:38 -0700197int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700198 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700199}
200
201int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700202 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700203}
204
205void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700206 if (mSaveCount > 1) {
207 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700208 }
Romain Guybb9524b2010-06-22 18:56:38 -0700209}
210
211void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700212 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700213
Romain Guy8fb95422010-08-17 18:38:51 -0700214 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700215 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700216 }
Romain Guybb9524b2010-06-22 18:56:38 -0700217}
218
Romain Guy8aef54f2010-09-01 15:13:49 -0700219int OpenGLRenderer::saveSnapshot(int flags) {
220 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700221 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700222}
223
224bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700225 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700226 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700227 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700228
Romain Guybd6b79b2010-06-26 00:13:53 -0700229 sp<Snapshot> current = mSnapshot;
230 sp<Snapshot> previous = mSnapshot->previous;
231
Romain Guyeb993562010-10-05 18:14:38 -0700232 if (restoreOrtho) {
233 Rect& r = previous->viewport;
234 glViewport(r.left, r.top, r.right, r.bottom);
235 mOrthoMatrix.load(current->orthoMatrix);
236 }
237
Romain Guy8b55f372010-08-18 17:10:07 -0700238 mSaveCount--;
239 mSnapshot = previous;
240
Romain Guybd6b79b2010-06-26 00:13:53 -0700241 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700242 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700243 }
244
Romain Guy2542d192010-08-18 11:47:12 -0700245 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700246 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700247 }
Romain Guy2542d192010-08-18 11:47:12 -0700248
249 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700250}
251
Romain Guyf6a11b82010-06-23 17:47:49 -0700252///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700253// Layers
254///////////////////////////////////////////////////////////////////////////////
255
256int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700257 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700258 const GLuint previousFbo = mSnapshot->fbo;
259 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700260
261 int alpha = 255;
262 SkXfermode::Mode mode;
263
264 if (p) {
265 alpha = p->getAlpha();
Romain Guy746b7402010-10-26 16:27:31 -0700266 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700267 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
268 if (!isMode) {
269 // Assume SRC_OVER
270 mode = SkXfermode::kSrcOver_Mode;
271 }
272 } else {
273 mode = getXfermode(p->getXfermode());
Romain Guyd55a8612010-06-28 17:42:46 -0700274 }
275 } else {
276 mode = SkXfermode::kSrcOver_Mode;
277 }
278
Romain Guydbc26d22010-10-11 17:58:29 -0700279 if (!mSnapshot->previous->invisible) {
280 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
281 }
Romain Guyd55a8612010-06-28 17:42:46 -0700282
283 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700284}
285
286int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
287 int alpha, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700288 if (alpha == 0xff) {
289 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700290 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700291 SkPaint paint;
292 paint.setAlpha(alpha);
293 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700294 }
Romain Guyd55a8612010-06-28 17:42:46 -0700295}
Romain Guybd6b79b2010-06-26 00:13:53 -0700296
Romain Guy1c740bc2010-09-13 18:00:09 -0700297/**
298 * Layers are viewed by Skia are slightly different than layers in image editing
299 * programs (for instance.) When a layer is created, previously created layers
300 * and the frame buffer still receive every drawing command. For instance, if a
301 * layer is created and a shape intersecting the bounds of the layers and the
302 * framebuffer is draw, the shape will be drawn on both (unless the layer was
303 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
304 *
305 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
306 * texture. Unfortunately, this is inefficient as it requires every primitive to
307 * be drawn n + 1 times, where n is the number of active layers. In practice this
308 * means, for every primitive:
309 * - Switch active frame buffer
310 * - Change viewport, clip and projection matrix
311 * - Issue the drawing
312 *
313 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700314 * To avoid this, layers are implemented in a different way here, at least in the
315 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
316 * is set. When this flag is set we can redirect all drawing operations into a
317 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700318 *
319 * This implementation relies on the frame buffer being at least RGBA 8888. When
320 * a layer is created, only a texture is created, not an FBO. The content of the
321 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700322 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700323 * buffer and drawing continues as normal. This technique therefore treats the
324 * frame buffer as a scratch buffer for the layers.
325 *
326 * To compose the layers back onto the frame buffer, each layer texture
327 * (containing the original frame buffer data) is drawn as a simple quad over
328 * the frame buffer. The trick is that the quad is set as the composition
329 * destination in the blending equation, and the frame buffer becomes the source
330 * of the composition.
331 *
332 * Drawing layers with an alpha value requires an extra step before composition.
333 * An empty quad is drawn over the layer's region in the frame buffer. This quad
334 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
335 * quad is used to multiply the colors in the frame buffer. This is achieved by
336 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
337 * GL_ZERO, GL_SRC_ALPHA.
338 *
339 * Because glCopyTexImage2D() can be slow, an alternative implementation might
340 * be use to draw a single clipped layer. The implementation described above
341 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700342 *
343 * (1) The frame buffer is actually not cleared right away. To allow the GPU
344 * to potentially optimize series of calls to glCopyTexImage2D, the frame
345 * buffer is left untouched until the first drawing operation. Only when
346 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700347 */
Romain Guyd55a8612010-06-28 17:42:46 -0700348bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700349 float right, float bottom, int alpha, SkXfermode::Mode mode,
350 int flags, GLuint previousFbo) {
351 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700352 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700353
Romain Guyeb993562010-10-05 18:14:38 -0700354 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
355
Romain Guyf607bdc2010-09-10 19:20:06 -0700356 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700357 Rect bounds(left, top, right, bottom);
Romain Guyae88e5e2010-10-22 17:49:18 -0700358 if (fboLayer) {
359 // Clear the previous layer regions before we change the viewport
360 clearLayerRegions();
361 } else {
Romain Guyeb993562010-10-05 18:14:38 -0700362 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700363
Romain Guyeb993562010-10-05 18:14:38 -0700364 // Layers only make sense if they are in the framebuffer's bounds
Romain Guy58ae6db2010-10-22 11:16:37 -0700365 bounds.intersect(*snapshot->clipRect);
Romain Guyae517592010-10-22 10:40:27 -0700366
Romain Guyae88e5e2010-10-22 17:49:18 -0700367 // We cannot work with sub-pixels in this case
368 bounds.snapToPixelBoundaries();
369
Romain Guyae517592010-10-22 10:40:27 -0700370 // When the layer is not an FBO, we may use glCopyTexImage so we
371 // need to make sure the layer does not extend outside the bounds
372 // of the framebuffer
373 bounds.intersect(snapshot->previous->viewport);
Romain Guyeb993562010-10-05 18:14:38 -0700374 }
Romain Guybf434112010-09-16 14:40:17 -0700375
Romain Guy746b7402010-10-26 16:27:31 -0700376 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
377 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guydbc26d22010-10-11 17:58:29 -0700378 snapshot->invisible = true;
379 } else {
380 // TODO: Should take the mode into account
Romain Guyd2a1ff02010-10-14 14:46:44 -0700381 snapshot->invisible = snapshot->previous->invisible ||
382 (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700383 }
384
385 // Bail out if we won't draw in this snapshot
386 if (snapshot->invisible) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700387 return false;
388 }
Romain Guyf18fd992010-07-08 11:45:51 -0700389
Romain Guy0bb56672010-10-01 00:25:02 -0700390 glActiveTexture(GL_TEXTURE0);
391
Romain Guy8550c4c2010-10-08 15:49:53 -0700392 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700393 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700394 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700395 }
396
Romain Guydda57022010-07-06 11:39:32 -0700397 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700398 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700399 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700400 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
401 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guydda57022010-07-06 11:39:32 -0700402
Romain Guy8fb95422010-08-17 18:38:51 -0700403 // Save the layer in the snapshot
404 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700405 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700406
Romain Guyeb993562010-10-05 18:14:38 -0700407 if (fboLayer) {
408 layer->fbo = mCaches.fboCache.get();
Romain Guy38c85b92010-09-22 22:48:20 -0700409
Romain Guyeb993562010-10-05 18:14:38 -0700410 snapshot->flags |= Snapshot::kFlagIsFboLayer;
411 snapshot->fbo = layer->fbo;
412 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
413 snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
414 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
415 snapshot->height = bounds.getHeight();
416 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
417 snapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf607bdc2010-09-10 19:20:06 -0700418
Romain Guyeb993562010-10-05 18:14:38 -0700419 // Bind texture to FBO
420 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
421 glBindTexture(GL_TEXTURE_2D, layer->texture);
422
423 // Initialize the texture if needed
424 if (layer->empty) {
425 layer->empty = false;
Romain Guy8550c4c2010-10-08 15:49:53 -0700426 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
Romain Guyeb993562010-10-05 18:14:38 -0700427 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
428 }
429
430 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
431 layer->texture, 0);
432
Romain Guye9108052010-10-12 18:15:42 -0700433#if DEBUG_LAYERS
Romain Guyeb993562010-10-05 18:14:38 -0700434 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
435 if (status != GL_FRAMEBUFFER_COMPLETE) {
436 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
437
438 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
439 glDeleteTextures(1, &layer->texture);
440 mCaches.fboCache.put(layer->fbo);
441
442 delete layer;
443
444 return false;
445 }
Romain Guye9108052010-10-12 18:15:42 -0700446#endif
Romain Guyeb993562010-10-05 18:14:38 -0700447
448 // Clear the FBO
Romain Guy93d23612010-10-13 18:26:36 -0700449 glScissor(0.0f, 0.0f, bounds.getWidth() + 1.0f, bounds.getHeight() + 1.0f);
Romain Guyeb993562010-10-05 18:14:38 -0700450 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
451 glClear(GL_COLOR_BUFFER_BIT);
Romain Guyeb07af62010-10-12 18:40:36 -0700452
Romain Guy746b7402010-10-26 16:27:31 -0700453 dirtyClip();
Romain Guyeb993562010-10-05 18:14:38 -0700454
455 // Change the ortho projection
456 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
457 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
458 } else {
459 // Copy the framebuffer into the layer
460 glBindTexture(GL_TEXTURE_2D, layer->texture);
461
Romain Guyae88e5e2010-10-22 17:49:18 -0700462 if (layer->empty) {
463 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
464 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
465 layer->empty = false;
466 } else {
467 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
468 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
469 }
Romain Guyeb993562010-10-05 18:14:38 -0700470
471 // Enqueue the buffer coordinates to clear the corresponding region later
472 mLayers.push(new Rect(bounds));
473 }
Romain Guyf86ef572010-07-01 11:05:42 -0700474
Romain Guyd55a8612010-06-28 17:42:46 -0700475 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700476}
477
Romain Guy1c740bc2010-09-13 18:00:09 -0700478/**
479 * Read the documentation of createLayer() before doing anything in this method.
480 */
Romain Guy1d83e192010-08-17 11:37:00 -0700481void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
482 if (!current->layer) {
483 LOGE("Attempting to compose a layer that does not exist");
484 return;
485 }
486
Romain Guyeb993562010-10-05 18:14:38 -0700487 const bool fboLayer = current->flags & SkCanvas::kClipToLayer_SaveFlag;
488
489 if (fboLayer) {
490 // Unbind current FBO and restore previous one
491 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
492 }
493
Romain Guy1d83e192010-08-17 11:37:00 -0700494 // Restore the clip from the previous snapshot
Romain Guyae88e5e2010-10-22 17:49:18 -0700495 Rect& clip(*previous->clipRect);
496 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700497 glScissor(clip.left, previous->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700498
499 Layer* layer = current->layer;
500 const Rect& rect = layer->layer;
501
Romain Guyeb993562010-10-05 18:14:38 -0700502 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700503 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700504 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guyf607bdc2010-09-10 19:20:06 -0700505 }
506
Romain Guy8550c4c2010-10-08 15:49:53 -0700507 const Rect& texCoords = layer->texCoords;
Romain Guy03750a02010-10-18 14:06:08 -0700508 mCaches.unbindMeshBuffer();
Romain Guy8550c4c2010-10-08 15:49:53 -0700509 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
Romain Guy8b55f372010-08-18 17:10:07 -0700510
Romain Guy746b7402010-10-26 16:27:31 -0700511 glActiveTexture(gTextureUnits[0]);
Romain Guyeb993562010-10-05 18:14:38 -0700512 if (fboLayer) {
Romain Guy03750a02010-10-18 14:06:08 -0700513 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
514 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
515 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyeb993562010-10-05 18:14:38 -0700516 } else {
517 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
518 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
519 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, true, true);
520 }
Romain Guy1d83e192010-08-17 11:37:00 -0700521
Romain Guy8b55f372010-08-18 17:10:07 -0700522 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
523
Romain Guyeb993562010-10-05 18:14:38 -0700524 if (fboLayer) {
525 // Detach the texture from the FBO
526 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
527 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
528 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
529
530 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
531 mCaches.fboCache.put(current->fbo);
532 }
533
Romain Guy746b7402010-10-26 16:27:31 -0700534 dirtyClip();
535
Romain Guyeb993562010-10-05 18:14:38 -0700536 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700537 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700538 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700539 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700540 delete layer;
541 }
542}
543
Romain Guy746b7402010-10-26 16:27:31 -0700544void OpenGLRenderer::setupDraw() {
545 clearLayerRegions();
546 if (mDirtyClip) {
547 setScissorFromClip();
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
Romain Guyd2a1ff02010-10-14 14:46:44 -0700558 glScissor(bounds->left, mSnapshot->height - bounds->bottom,
Romain Guy86942302010-09-12 13:02:16 -0700559 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
Romain Guy746b7402010-10-26 16:27:31 -0700568 dirtyClip();
Romain Guy86942302010-09-12 13:02:16 -0700569}
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
Romain Guy41030da2010-10-13 13:40:37 -0700591const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700592 if (mSnapshot->fbo != 0) {
593 return &mSnapshot->transform->data[0];
594 }
595 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700596}
597
Romain Guyf6a11b82010-06-23 17:47:49 -0700598void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700599 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700600}
601
602void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700603 SkMatrix transform;
604 mSnapshot->transform->copyTo(transform);
605 transform.preConcat(*matrix);
606 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700607}
608
609///////////////////////////////////////////////////////////////////////////////
610// Clipping
611///////////////////////////////////////////////////////////////////////////////
612
Romain Guybb9524b2010-06-22 18:56:38 -0700613void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700614 Rect clip(*mSnapshot->clipRect);
615 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700616 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700617 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700618}
619
620const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700621 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700622}
623
Romain Guyc7d53492010-06-25 13:41:57 -0700624bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guydbc26d22010-10-11 17:58:29 -0700625 if (mSnapshot->invisible) {
626 return true;
627 }
628
Romain Guy1d83e192010-08-17 11:37:00 -0700629 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700630 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700631 r.snapToPixelBoundaries();
632
633 Rect clipRect(*mSnapshot->clipRect);
634 clipRect.snapToPixelBoundaries();
635
636 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700637}
638
Romain Guy079ba2c2010-07-16 14:12:24 -0700639bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
640 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700641 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700642 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700643 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700644 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700645}
646
Romain Guyf6a11b82010-06-23 17:47:49 -0700647///////////////////////////////////////////////////////////////////////////////
648// Drawing
649///////////////////////////////////////////////////////////////////////////////
650
Chet Haase5c13d892010-10-08 08:37:55 -0700651void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700652 const float right = left + bitmap->width();
653 const float bottom = top + bitmap->height();
654
655 if (quickReject(left, top, right, bottom)) {
656 return;
657 }
658
Romain Guy61c8c9c2010-08-09 20:48:09 -0700659 glActiveTexture(GL_TEXTURE0);
Romain Guy8164c2d2010-10-25 18:03:28 -0700660 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700661 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700662 const AutoTexture autoCleanup(texture);
663
Romain Guy6926c722010-07-12 20:20:03 -0700664 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700665}
666
Chet Haase5c13d892010-10-08 08:37:55 -0700667void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -0700668 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
669 const mat4 transform(*matrix);
670 transform.mapRect(r);
671
Romain Guy6926c722010-07-12 20:20:03 -0700672 if (quickReject(r.left, r.top, r.right, r.bottom)) {
673 return;
674 }
675
Romain Guy61c8c9c2010-08-09 20:48:09 -0700676 glActiveTexture(GL_TEXTURE0);
Romain Guy8164c2d2010-10-25 18:03:28 -0700677 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);
680
Romain Guy82ba8142010-07-09 13:25:56 -0700681 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700682}
683
Romain Guy8ba548f2010-06-30 19:21:21 -0700684void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
685 float srcLeft, float srcTop, float srcRight, float srcBottom,
686 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700687 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700688 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
689 return;
690 }
691
Romain Guy746b7402010-10-26 16:27:31 -0700692 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -0700693 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700694 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700695 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -0700696 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -0700697
Romain Guy8ba548f2010-06-30 19:21:21 -0700698 const float width = texture->width;
699 const float height = texture->height;
700
701 const float u1 = srcLeft / width;
702 const float v1 = srcTop / height;
703 const float u2 = srcRight / width;
704 const float v2 = srcBottom / height;
705
Romain Guy03750a02010-10-18 14:06:08 -0700706 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -0700707 resetDrawTextureTexCoords(u1, v1, u2, v2);
708
Romain Guy03750a02010-10-18 14:06:08 -0700709 int alpha;
710 SkXfermode::Mode mode;
711 getAlphaAndMode(paint, &alpha, &mode);
712
713 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
714 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
715 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy8ba548f2010-06-30 19:21:21 -0700716
717 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
718}
719
Romain Guy4aa90572010-09-26 18:40:37 -0700720void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -0700721 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -0700722 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700723 if (quickReject(left, top, right, bottom)) {
724 return;
725 }
726
Romain Guy746b7402010-10-26 16:27:31 -0700727 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -0700728 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700729 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700730 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -0700731 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -0700732
733 int alpha;
734 SkXfermode::Mode mode;
735 getAlphaAndMode(paint, &alpha, &mode);
736
Romain Guy2728f962010-10-08 18:36:15 -0700737 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -0700738 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -0700739
Romain Guy054dc182010-10-15 17:55:25 -0700740 if (mesh) {
741 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
742 // patch mesh already defines the final size
743 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
Romain Guy03750a02010-10-18 14:06:08 -0700744 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
745 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer);
Romain Guy054dc182010-10-15 17:55:25 -0700746 }
Romain Guyf7f93552010-07-08 19:17:03 -0700747}
748
Chet Haase5c13d892010-10-08 08:37:55 -0700749void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -0700750 if (mSnapshot->invisible) return;
751
Romain Guy759ea802010-09-16 20:49:46 -0700752 int alpha;
753 SkXfermode::Mode mode;
754 getAlphaAndMode(paint, &alpha, &mode);
755
756 uint32_t color = paint->getColor();
757 const GLfloat a = alpha / 255.0f;
758 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
759 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
760 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
761
Romain Guyc95c8d62010-09-17 15:31:32 -0700762 const bool isAA = paint->isAntiAlias();
763 if (isAA) {
764 GLuint textureUnit = 0;
Romain Guy746b7402010-10-26 16:27:31 -0700765 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy29d89972010-09-22 16:10:57 -0700766 setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
Romain Guy03750a02010-10-18 14:06:08 -0700767 mode, false, true, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
768 mCaches.line.getMeshBuffer());
Romain Guyc95c8d62010-09-17 15:31:32 -0700769 } else {
770 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false);
771 }
772
773 const float strokeWidth = paint->getStrokeWidth();
Romain Guy29d89972010-09-22 16:10:57 -0700774 const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount;
Romain Guyc95c8d62010-09-17 15:31:32 -0700775 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
Romain Guy759ea802010-09-16 20:49:46 -0700776
777 for (int i = 0; i < count; i += 4) {
778 float tx = 0.0f;
779 float ty = 0.0f;
780
Romain Guyc95c8d62010-09-17 15:31:32 -0700781 if (isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700782 mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3],
Romain Guyc95c8d62010-09-17 15:31:32 -0700783 strokeWidth, tx, ty);
784 } else {
Romain Guyb5ab4172010-09-17 15:36:56 -0700785 ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f;
Romain Guyc95c8d62010-09-17 15:31:32 -0700786 }
Romain Guy759ea802010-09-16 20:49:46 -0700787
788 const float dx = points[i + 2] - points[i];
789 const float dy = points[i + 3] - points[i + 1];
790 const float mag = sqrtf(dx * dx + dy * dy);
791 const float angle = acos(dx / mag);
792
793 mModelView.loadTranslate(points[i], points[i + 1], 0.0f);
794 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) {
795 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f);
796 }
797 mModelView.translate(tx, ty, 0.0f);
Romain Guyc95c8d62010-09-17 15:31:32 -0700798 if (!isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700799 float length = mCaches.line.getLength(points[i], points[i + 1],
800 points[i + 2], points[i + 3]);
Romain Guyc95c8d62010-09-17 15:31:32 -0700801 mModelView.scale(length, strokeWidth, 1.0f);
802 }
Romain Guy759ea802010-09-16 20:49:46 -0700803 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
804
805 if (mShader) {
806 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot);
807 }
808
Romain Guyc95c8d62010-09-17 15:31:32 -0700809 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy759ea802010-09-16 20:49:46 -0700810 }
811
Romain Guy29d89972010-09-22 16:10:57 -0700812 if (isAA) {
813 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
814 }
Romain Guy759ea802010-09-16 20:49:46 -0700815}
816
Romain Guy85bf02f2010-06-22 13:11:24 -0700817void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guyae88e5e2010-10-22 17:49:18 -0700818 Rect& clip(*mSnapshot->clipRect);
819 clip.snapToPixelBoundaries();
Romain Guy3d58c032010-07-14 16:34:53 -0700820 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700821}
Romain Guy9d5316e2010-06-24 19:30:36 -0700822
Chet Haase5c13d892010-10-08 08:37:55 -0700823void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700824 if (quickReject(left, top, right, bottom)) {
825 return;
826 }
827
Romain Guy026c5e162010-06-28 17:12:22 -0700828 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -0700829 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700830 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
831 if (!isMode) {
832 // Assume SRC_OVER
833 mode = SkXfermode::kSrcOver_Mode;
834 }
835 } else {
836 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700837 }
838
839 // Skia draws using the color's alpha channel if < 255
840 // Otherwise, it uses the paint's alpha
841 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700842 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700843 color |= p->getAlpha() << 24;
844 }
845
846 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700847}
Romain Guy9d5316e2010-06-24 19:30:36 -0700848
Romain Guye8e62a42010-07-23 18:55:21 -0700849void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
850 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700851 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700852 return;
853 }
Romain Guydbc26d22010-10-11 17:58:29 -0700854 if (mSnapshot->invisible) return;
Romain Guye2d345e2010-09-24 18:39:22 -0700855
Romain Guyf607bdc2010-09-10 19:20:06 -0700856 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700857
Romain Guya674ab72010-08-10 17:26:42 -0700858 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700859 switch (paint->getTextAlign()) {
860 case SkPaint::kCenter_Align:
861 length = paint->measureText(text, bytesCount);
862 x -= length / 2.0f;
863 break;
864 case SkPaint::kRight_Align:
865 length = paint->measureText(text, bytesCount);
866 x -= length;
867 break;
868 default:
869 break;
870 }
871
Romain Guy694b5192010-07-21 21:33:20 -0700872 int alpha;
873 SkXfermode::Mode mode;
874 getAlphaAndMode(paint, &alpha, &mode);
875
Romain Guy2542d192010-08-18 11:47:12 -0700876 uint32_t color = paint->getColor();
877 const GLfloat a = alpha / 255.0f;
878 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
879 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
880 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
881
Romain Guyb45c0c92010-08-26 20:35:23 -0700882 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
883 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700884 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -0700885
Romain Guy746b7402010-10-26 16:27:31 -0700886 setupDraw();
Romain Guy9d13fe252010-10-15 16:06:03 -0700887
Romain Guy1e45aae2010-08-13 19:39:53 -0700888 if (mHasShadow) {
889 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700890 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700891 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700892 count, mShadowRadius);
893 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700894
Romain Guy2542d192010-08-18 11:47:12 -0700895 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700896
897 // Draw the mesh
898 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700899 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700900 }
901
Romain Guy06f96e22010-07-30 19:18:16 -0700902 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700903 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700904
Romain Guye8cb9c142010-10-04 14:14:11 -0700905 // Assume that the modelView matrix does not force scales, rotates, etc.
906 const bool linearFilter = mSnapshot->transform->changesBounds();
907 setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit,
Romain Guy03750a02010-10-18 14:06:08 -0700908 x, y, r, g, b, a, mode, false, true, NULL, NULL);
Romain Guy06f96e22010-07-30 19:18:16 -0700909
Romain Guy09147fb2010-07-22 13:08:20 -0700910 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy9d13fe252010-10-15 16:06:03 -0700911
Romain Guy03750a02010-10-18 14:06:08 -0700912 mCaches.unbindMeshBuffer();
Romain Guyb45c0c92010-08-26 20:35:23 -0700913 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700914
915 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700916 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700917
Romain Guy0a417492010-08-16 20:26:20 -0700918 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -0700919}
920
Romain Guy7fbcc042010-08-04 15:40:07 -0700921void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -0700922 if (mSnapshot->invisible) return;
923
Romain Guy7fbcc042010-08-04 15:40:07 -0700924 GLuint textureUnit = 0;
925 glActiveTexture(gTextureUnits[textureUnit]);
926
Romain Guyfb8b7632010-08-23 21:05:08 -0700927 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700928 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700929 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700930
Romain Guy8b55f372010-08-18 17:10:07 -0700931 const float x = texture->left - texture->offset;
932 const float y = texture->top - texture->offset;
933
934 if (quickReject(x, y, x + texture->width, y + texture->height)) {
935 return;
936 }
937
Romain Guy7fbcc042010-08-04 15:40:07 -0700938 int alpha;
939 SkXfermode::Mode mode;
940 getAlphaAndMode(paint, &alpha, &mode);
941
942 uint32_t color = paint->getColor();
943 const GLfloat a = alpha / 255.0f;
944 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
945 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
946 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
947
Romain Guy0a417492010-08-16 20:26:20 -0700948 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
949
Romain Guy746b7402010-10-26 16:27:31 -0700950 setupDraw();
Romain Guy86942302010-09-12 13:02:16 -0700951
Romain Guy0a417492010-08-16 20:26:20 -0700952 // Draw the mesh
953 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700954 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700955}
956
Romain Guy6926c722010-07-12 20:20:03 -0700957///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700958// Shaders
959///////////////////////////////////////////////////////////////////////////////
960
961void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700962 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700963}
964
Romain Guy06f96e22010-07-30 19:18:16 -0700965void OpenGLRenderer::setupShader(SkiaShader* shader) {
966 mShader = shader;
967 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700968 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700969 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700970}
971
Romain Guyd27977d2010-07-14 19:18:51 -0700972///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700973// Color filters
974///////////////////////////////////////////////////////////////////////////////
975
976void OpenGLRenderer::resetColorFilter() {
977 mColorFilter = NULL;
978}
979
980void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
981 mColorFilter = filter;
982}
983
984///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700985// Drop shadow
986///////////////////////////////////////////////////////////////////////////////
987
988void OpenGLRenderer::resetShadow() {
989 mHasShadow = false;
990}
991
992void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
993 mHasShadow = true;
994 mShadowRadius = radius;
995 mShadowDx = dx;
996 mShadowDy = dy;
997 mShadowColor = color;
998}
999
1000///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001001// Drawing implementation
1002///////////////////////////////////////////////////////////////////////////////
1003
Romain Guy0a417492010-08-16 20:26:20 -07001004void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -07001005 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -07001006 const float sx = x - texture->left + mShadowDx;
1007 const float sy = y - texture->top + mShadowDy;
1008
Romain Guy2542d192010-08-18 11:47:12 -07001009 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1010 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -07001011 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
1012 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
1013 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
1014
1015 GLuint textureUnit = 0;
1016 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
1017}
1018
1019void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
1020 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
1021 bool transforms, bool applyFilters) {
1022 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -07001023 x, y, r, g, b, a, mode, transforms, applyFilters,
Romain Guy03750a02010-10-18 14:06:08 -07001024 (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy0a417492010-08-16 20:26:20 -07001025}
1026
1027void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1028 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1029 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy03750a02010-10-18 14:06:08 -07001030 setupTextureAlpha8(texture, width, height, textureUnit, x, y, r, g, b, a, mode,
1031 transforms, applyFilters, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy759ea802010-09-16 20:49:46 -07001032}
1033
1034void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1035 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1036 SkXfermode::Mode mode, bool transforms, bool applyFilters,
Romain Guy03750a02010-10-18 14:06:08 -07001037 GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guy0a417492010-08-16 20:26:20 -07001038 // Describe the required shaders
1039 ProgramDescription description;
1040 description.hasTexture = true;
1041 description.hasAlpha8Texture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001042 const bool setColor = description.setAlpha8Color(r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -07001043
1044 if (applyFilters) {
1045 if (mShader) {
Romain Guy746b7402010-10-26 16:27:31 -07001046 mShader->describe(description, mCaches.extensions);
Romain Guy0a417492010-08-16 20:26:20 -07001047 }
1048 if (mColorFilter) {
Romain Guy746b7402010-10-26 16:27:31 -07001049 mColorFilter->describe(description, mCaches.extensions);
Romain Guy0a417492010-08-16 20:26:20 -07001050 }
1051 }
1052
Romain Guya5aed0d2010-09-09 14:42:43 -07001053 // Setup the blending mode
1054 chooseBlending(true, mode, description);
1055
Romain Guy0a417492010-08-16 20:26:20 -07001056 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001057 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -07001058
Romain Guy746b7402010-10-26 16:27:31 -07001059 bindTexture(texture);
Romain Guyfb8b7632010-08-23 21:05:08 -07001060 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001061
Romain Guyfb8b7632010-08-23 21:05:08 -07001062 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -07001063 glEnableVertexAttribArray(texCoordsSlot);
1064
Romain Guy03750a02010-10-18 14:06:08 -07001065 if (texCoords) {
1066 // Setup attributes
1067 if (!vertices) {
1068 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1069 } else {
1070 mCaches.unbindMeshBuffer();
1071 }
1072 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1073 gMeshStride, vertices);
1074 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1075 }
Romain Guy0a417492010-08-16 20:26:20 -07001076
1077 // Setup uniforms
1078 if (transforms) {
1079 mModelView.loadTranslate(x, y, 0.0f);
1080 mModelView.scale(width, height, 1.0f);
1081 } else {
1082 mModelView.loadIdentity();
1083 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001084 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy707b2f72010-10-11 16:34:59 -07001085 if (setColor) {
1086 mCaches.currentProgram->setColor(r, g, b, a);
1087 }
Romain Guy0a417492010-08-16 20:26:20 -07001088
1089 textureUnit++;
1090 if (applyFilters) {
1091 // Setup attributes and uniforms required by the shaders
1092 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001093 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001094 }
1095 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001096 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -07001097 }
1098 }
1099}
1100
Romain Guyf607bdc2010-09-10 19:20:06 -07001101// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001102#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1103#define kStdUnderline_Offset (1.0f / 9.0f)
1104#define kStdUnderline_Thickness (1.0f / 18.0f)
1105
1106void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1107 float x, float y, SkPaint* paint) {
1108 // Handle underline and strike-through
1109 uint32_t flags = paint->getFlags();
1110 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1111 float underlineWidth = length;
1112 // If length is > 0.0f, we already measured the text for the text alignment
1113 if (length <= 0.0f) {
1114 underlineWidth = paint->measureText(text, bytesCount);
1115 }
1116
1117 float offsetX = 0;
1118 switch (paint->getTextAlign()) {
1119 case SkPaint::kCenter_Align:
1120 offsetX = underlineWidth * 0.5f;
1121 break;
1122 case SkPaint::kRight_Align:
1123 offsetX = underlineWidth;
1124 break;
1125 default:
1126 break;
1127 }
1128
1129 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001130 const float textSize = paint->getTextSize();
1131 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -07001132
Romain Guye20ecbd2010-09-22 19:49:04 -07001133 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001134 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001135
1136 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
1137 float points[pointsCount];
1138 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001139
1140 if (flags & SkPaint::kUnderlineText_Flag) {
1141 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001142 points[currentPoint++] = left;
1143 points[currentPoint++] = top;
1144 points[currentPoint++] = left + underlineWidth;
1145 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001146 }
1147
1148 if (flags & SkPaint::kStrikeThruText_Flag) {
1149 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001150 points[currentPoint++] = left;
1151 points[currentPoint++] = top;
1152 points[currentPoint++] = left + underlineWidth;
1153 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001154 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001155
1156 SkPaint linesPaint(*paint);
1157 linesPaint.setStrokeWidth(strokeWidth);
1158
1159 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001160 }
1161 }
1162}
1163
Romain Guy026c5e162010-06-28 17:12:22 -07001164void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001165 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy746b7402010-10-26 16:27:31 -07001166 setupDraw();
Romain Guy86942302010-09-12 13:02:16 -07001167
Romain Guyd27977d2010-07-14 19:18:51 -07001168 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001169 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001170 color |= 0x00ffffff;
1171 }
1172
1173 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -07001174 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -07001175 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -07001176 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1177 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1178 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1179
Romain Guyc95c8d62010-09-17 15:31:32 -07001180 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform);
1181
1182 // Draw the mesh
1183 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1184}
1185
1186void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom,
1187 float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy06f96e22010-07-30 19:18:16 -07001188 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -07001189
Romain Guy06f96e22010-07-30 19:18:16 -07001190 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -07001191 ProgramDescription description;
Romain Guy707b2f72010-10-11 16:34:59 -07001192 const bool setColor = description.setColor(r, g, b, a);
1193
Romain Guy06f96e22010-07-30 19:18:16 -07001194 if (mShader) {
Romain Guy746b7402010-10-26 16:27:31 -07001195 mShader->describe(description, mCaches.extensions);
Romain Guy6926c722010-07-12 20:20:03 -07001196 }
Romain Guydb1938e2010-08-02 18:50:22 -07001197 if (mColorFilter) {
Romain Guy746b7402010-10-26 16:27:31 -07001198 mColorFilter->describe(description, mCaches.extensions);
Romain Guydb1938e2010-08-02 18:50:22 -07001199 }
Romain Guyd27977d2010-07-14 19:18:51 -07001200
Romain Guy1c740bc2010-09-13 18:00:09 -07001201 // Setup the blending mode
Romain Guyc95c8d62010-09-17 15:31:32 -07001202 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -07001203
Romain Guy06f96e22010-07-30 19:18:16 -07001204 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001205 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -07001206
1207 // Setup attributes
Romain Guy03750a02010-10-18 14:06:08 -07001208 mCaches.bindMeshBuffer();
Romain Guyfb8b7632010-08-23 21:05:08 -07001209 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy03750a02010-10-18 14:06:08 -07001210 gMeshStride, 0);
Romain Guy06f96e22010-07-30 19:18:16 -07001211
1212 // Setup uniforms
1213 mModelView.loadTranslate(left, top, 0.0f);
1214 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -07001215 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001216 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -07001217 } else {
1218 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -07001219 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -07001220 }
Romain Guy707b2f72010-10-11 16:34:59 -07001221 mCaches.currentProgram->setColor(r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -07001222
Romain Guy06f96e22010-07-30 19:18:16 -07001223 // Setup attributes and uniforms required by the shaders
1224 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001225 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001226 }
Romain Guydb1938e2010-08-02 18:50:22 -07001227 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001228 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001229 }
Romain Guyd27977d2010-07-14 19:18:51 -07001230}
1231
Romain Guy82ba8142010-07-09 13:25:56 -07001232void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001233 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001234 int alpha;
1235 SkXfermode::Mode mode;
1236 getAlphaAndMode(paint, &alpha, &mode);
1237
Romain Guy8164c2d2010-10-25 18:03:28 -07001238 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1239
Romain Guy6820ac82010-09-15 18:11:50 -07001240 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy03750a02010-10-18 14:06:08 -07001241 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy6820ac82010-09-15 18:11:50 -07001242 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -07001243}
1244
Romain Guybd6b79b2010-06-26 00:13:53 -07001245void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001246 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1247 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001248 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001249}
1250
1251void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001252 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001253 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy03750a02010-10-18 14:06:08 -07001254 bool swapSrcDst, bool ignoreTransform, GLuint vbo) {
Romain Guy746b7402010-10-26 16:27:31 -07001255 setupDraw();
Romain Guy86942302010-09-12 13:02:16 -07001256
Romain Guy889f8d12010-07-29 14:37:42 -07001257 ProgramDescription description;
1258 description.hasTexture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001259 const bool setColor = description.setColor(alpha, alpha, alpha, alpha);
Romain Guydb1938e2010-08-02 18:50:22 -07001260 if (mColorFilter) {
Romain Guy746b7402010-10-26 16:27:31 -07001261 mColorFilter->describe(description, mCaches.extensions);
Romain Guydb1938e2010-08-02 18:50:22 -07001262 }
Romain Guy889f8d12010-07-29 14:37:42 -07001263
Romain Guybd6b79b2010-06-26 00:13:53 -07001264 mModelView.loadTranslate(left, top, 0.0f);
1265 mModelView.scale(right - left, bottom - top, 1.0f);
1266
Romain Guyf607bdc2010-09-10 19:20:06 -07001267 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001268
Romain Guyfb8b7632010-08-23 21:05:08 -07001269 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001270 if (!ignoreTransform) {
1271 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1272 } else {
1273 mat4 m;
1274 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
1275 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001276
Romain Guy889f8d12010-07-29 14:37:42 -07001277 // Texture
Romain Guy8164c2d2010-10-25 18:03:28 -07001278 bindTexture(texture);
Romain Guyfb8b7632010-08-23 21:05:08 -07001279 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001280
Romain Guya9794742010-07-13 11:37:54 -07001281 // Always premultiplied
Romain Guy707b2f72010-10-11 16:34:59 -07001282 if (setColor) {
1283 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha);
1284 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001285
Romain Guy889f8d12010-07-29 14:37:42 -07001286 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001287 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001288 glEnableVertexAttribArray(texCoordsSlot);
Romain Guy03750a02010-10-18 14:06:08 -07001289
1290 if (!vertices) {
1291 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1292 } else {
1293 mCaches.unbindMeshBuffer();
1294 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001295 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001296 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001297 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001298
Romain Guydb1938e2010-08-02 18:50:22 -07001299 // Color filter
1300 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001301 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001302 }
1303
Romain Guy6820ac82010-09-15 18:11:50 -07001304 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001305 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001306}
1307
Romain Guya5aed0d2010-09-09 14:42:43 -07001308void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001309 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001310 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1311 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001312 if (mode < SkXfermode::kPlus_Mode) {
1313 if (!mCaches.blend) {
1314 glEnable(GL_BLEND);
1315 }
Romain Guy82ba8142010-07-09 13:25:56 -07001316
Romain Guyf607bdc2010-09-10 19:20:06 -07001317 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1318 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001319
Romain Guya5aed0d2010-09-09 14:42:43 -07001320 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1321 glBlendFunc(sourceMode, destMode);
1322 mCaches.lastSrcMode = sourceMode;
1323 mCaches.lastDstMode = destMode;
1324 }
1325 } else {
1326 // These blend modes are not supported by OpenGL directly and have
1327 // to be implemented using shaders. Since the shader will perform
1328 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001329 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001330 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001331 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001332 }
1333
1334 if (mCaches.blend) {
1335 glDisable(GL_BLEND);
1336 }
1337 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001338 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001339 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001340 glDisable(GL_BLEND);
1341 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001342 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001343}
1344
Romain Guy889f8d12010-07-29 14:37:42 -07001345bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001346 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001347 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001348 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001349 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001350 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001351 }
Romain Guy6926c722010-07-12 20:20:03 -07001352 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001353}
1354
Romain Guy026c5e162010-06-28 17:12:22 -07001355void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001356 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001357 TextureVertex::setUV(v++, u1, v1);
1358 TextureVertex::setUV(v++, u2, v1);
1359 TextureVertex::setUV(v++, u1, v2);
1360 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001361}
1362
Chet Haase5c13d892010-10-08 08:37:55 -07001363void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001364 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07001365 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001366 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1367 if (!isMode) {
1368 // Assume SRC_OVER
1369 *mode = SkXfermode::kSrcOver_Mode;
1370 }
1371 } else {
1372 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001373 }
1374
1375 // Skia draws using the color's alpha channel if < 255
1376 // Otherwise, it uses the paint's alpha
1377 int color = paint->getColor();
1378 *alpha = (color >> 24) & 0xFF;
1379 if (*alpha == 255) {
1380 *alpha = paint->getAlpha();
1381 }
1382 } else {
1383 *mode = SkXfermode::kSrcOver_Mode;
1384 *alpha = 255;
1385 }
Romain Guy026c5e162010-06-28 17:12:22 -07001386}
1387
Romain Guya5aed0d2010-09-09 14:42:43 -07001388SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1389 if (mode == NULL) {
1390 return SkXfermode::kSrcOver_Mode;
1391 }
1392 return mode->fMode;
1393}
1394
Romain Guy746b7402010-10-26 16:27:31 -07001395void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07001396 bool bound = false;
1397 if (wrapS != texture->wrapS) {
1398 glBindTexture(GL_TEXTURE_2D, texture->id);
1399 bound = true;
1400 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1401 texture->wrapS = wrapS;
1402 }
1403 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001404 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001405 glBindTexture(GL_TEXTURE_2D, texture->id);
1406 }
Romain Guy8164c2d2010-10-25 18:03:28 -07001407 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1408 texture->wrapT = wrapT;
1409 }
Romain Guya1db5742010-07-20 13:09:13 -07001410}
1411
Romain Guy9d5316e2010-06-24 19:30:36 -07001412}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001413}; // namespace android