blob: 7495a068f50cf85dfefc5e1bd5c15eda6c783a64 [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;
110
Romain Guyb025b9c2010-09-16 14:16:48 -0700111 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Romain Guye4d01122010-06-16 18:44:05 -0700112}
113
Romain Guy85bf02f2010-06-22 13:11:24 -0700114OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700115 // The context has already been destroyed at this point, do not call
116 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700117}
118
Romain Guyf6a11b82010-06-23 17:47:49 -0700119///////////////////////////////////////////////////////////////////////////////
120// Setup
121///////////////////////////////////////////////////////////////////////////////
122
Romain Guy85bf02f2010-06-22 13:11:24 -0700123void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700124 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700125 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700126
127 mWidth = width;
128 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700129
130 mFirstSnapshot->height = height;
131 mFirstSnapshot->viewport.set(0, 0, width, height);
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 Guy08ae3172010-06-21 19:35:50 -0700142 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700143
Romain Guy6b7bd242010-10-06 19:49:23 -0700144 if (!opaque) {
145 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
146 glClear(GL_COLOR_BUFFER_BIT);
147 }
Romain Guybb9524b2010-06-22 18:56:38 -0700148
Romain Guy08ae3172010-06-21 19:35:50 -0700149 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700150 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700151
Romain Guyb82da652010-07-30 11:36:12 -0700152 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700153}
154
Romain Guyb025b9c2010-09-16 14:16:48 -0700155void OpenGLRenderer::finish() {
156#if DEBUG_OPENGL
157 GLenum status = GL_NO_ERROR;
158 while ((status = glGetError()) != GL_NO_ERROR) {
159 LOGD("GL error from OpenGLRenderer: 0x%x", status);
160 }
161#endif
162}
163
Romain Guyda8532c2010-08-31 11:50:35 -0700164void OpenGLRenderer::acquireContext() {
165 if (mCaches.currentProgram) {
166 if (mCaches.currentProgram->isInUse()) {
167 mCaches.currentProgram->remove();
168 mCaches.currentProgram = NULL;
169 }
170 }
Romain Guy50c0f092010-10-19 11:42:22 -0700171 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700172}
173
174void OpenGLRenderer::releaseContext() {
Romain Guyeb993562010-10-05 18:14:38 -0700175 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700176
177 glEnable(GL_SCISSOR_TEST);
178 setScissorFromClip();
179
Romain Guyf607bdc2010-09-10 19:20:06 -0700180 glDisable(GL_DITHER);
181
182 glBindFramebuffer(GL_FRAMEBUFFER, 0);
Romain Guy03750a02010-10-18 14:06:08 -0700183 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700184
Romain Guy50c0f092010-10-19 11:42:22 -0700185 mCaches.blend = true;
186 glEnable(GL_BLEND);
187 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
188 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700189}
190
Romain Guyf6a11b82010-06-23 17:47:49 -0700191///////////////////////////////////////////////////////////////////////////////
192// State management
193///////////////////////////////////////////////////////////////////////////////
194
Romain Guybb9524b2010-06-22 18:56:38 -0700195int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700196 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700197}
198
199int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700200 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700201}
202
203void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700204 if (mSaveCount > 1) {
205 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700206 }
Romain Guybb9524b2010-06-22 18:56:38 -0700207}
208
209void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700210 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700211
Romain Guy8fb95422010-08-17 18:38:51 -0700212 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700213 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700214 }
Romain Guybb9524b2010-06-22 18:56:38 -0700215}
216
Romain Guy8aef54f2010-09-01 15:13:49 -0700217int OpenGLRenderer::saveSnapshot(int flags) {
218 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700219 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700220}
221
222bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700223 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700224 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700225 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700226
Romain Guybd6b79b2010-06-26 00:13:53 -0700227 sp<Snapshot> current = mSnapshot;
228 sp<Snapshot> previous = mSnapshot->previous;
229
Romain Guyeb993562010-10-05 18:14:38 -0700230 if (restoreOrtho) {
231 Rect& r = previous->viewport;
232 glViewport(r.left, r.top, r.right, r.bottom);
233 mOrthoMatrix.load(current->orthoMatrix);
234 }
235
Romain Guy8b55f372010-08-18 17:10:07 -0700236 mSaveCount--;
237 mSnapshot = previous;
238
Romain Guybd6b79b2010-06-26 00:13:53 -0700239 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700240 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700241 }
242
Romain Guy2542d192010-08-18 11:47:12 -0700243 if (restoreClip) {
244 setScissorFromClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700245 }
Romain Guy2542d192010-08-18 11:47:12 -0700246
247 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700248}
249
Romain Guyf6a11b82010-06-23 17:47:49 -0700250///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700251// Layers
252///////////////////////////////////////////////////////////////////////////////
253
254int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700255 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700256 const GLuint previousFbo = mSnapshot->fbo;
257 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700258
259 int alpha = 255;
260 SkXfermode::Mode mode;
261
262 if (p) {
263 alpha = p->getAlpha();
Romain Guya5aed0d2010-09-09 14:42:43 -0700264 if (!mExtensions.hasFramebufferFetch()) {
265 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
266 if (!isMode) {
267 // Assume SRC_OVER
268 mode = SkXfermode::kSrcOver_Mode;
269 }
270 } else {
271 mode = getXfermode(p->getXfermode());
Romain Guyd55a8612010-06-28 17:42:46 -0700272 }
273 } else {
274 mode = SkXfermode::kSrcOver_Mode;
275 }
276
Romain Guydbc26d22010-10-11 17:58:29 -0700277 if (!mSnapshot->previous->invisible) {
278 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
279 }
Romain Guyd55a8612010-06-28 17:42:46 -0700280
281 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700282}
283
284int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
285 int alpha, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700286 if (alpha == 0xff) {
287 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700288 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700289 SkPaint paint;
290 paint.setAlpha(alpha);
291 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700292 }
Romain Guyd55a8612010-06-28 17:42:46 -0700293}
Romain Guybd6b79b2010-06-26 00:13:53 -0700294
Romain Guy1c740bc2010-09-13 18:00:09 -0700295/**
296 * Layers are viewed by Skia are slightly different than layers in image editing
297 * programs (for instance.) When a layer is created, previously created layers
298 * and the frame buffer still receive every drawing command. For instance, if a
299 * layer is created and a shape intersecting the bounds of the layers and the
300 * framebuffer is draw, the shape will be drawn on both (unless the layer was
301 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
302 *
303 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
304 * texture. Unfortunately, this is inefficient as it requires every primitive to
305 * be drawn n + 1 times, where n is the number of active layers. In practice this
306 * means, for every primitive:
307 * - Switch active frame buffer
308 * - Change viewport, clip and projection matrix
309 * - Issue the drawing
310 *
311 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700312 * To avoid this, layers are implemented in a different way here, at least in the
313 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
314 * is set. When this flag is set we can redirect all drawing operations into a
315 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700316 *
317 * This implementation relies on the frame buffer being at least RGBA 8888. When
318 * a layer is created, only a texture is created, not an FBO. The content of the
319 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700320 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700321 * buffer and drawing continues as normal. This technique therefore treats the
322 * frame buffer as a scratch buffer for the layers.
323 *
324 * To compose the layers back onto the frame buffer, each layer texture
325 * (containing the original frame buffer data) is drawn as a simple quad over
326 * the frame buffer. The trick is that the quad is set as the composition
327 * destination in the blending equation, and the frame buffer becomes the source
328 * of the composition.
329 *
330 * Drawing layers with an alpha value requires an extra step before composition.
331 * An empty quad is drawn over the layer's region in the frame buffer. This quad
332 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
333 * quad is used to multiply the colors in the frame buffer. This is achieved by
334 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
335 * GL_ZERO, GL_SRC_ALPHA.
336 *
337 * Because glCopyTexImage2D() can be slow, an alternative implementation might
338 * be use to draw a single clipped layer. The implementation described above
339 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700340 *
341 * (1) The frame buffer is actually not cleared right away. To allow the GPU
342 * to potentially optimize series of calls to glCopyTexImage2D, the frame
343 * buffer is left untouched until the first drawing operation. Only when
344 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700345 */
Romain Guyd55a8612010-06-28 17:42:46 -0700346bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700347 float right, float bottom, int alpha, SkXfermode::Mode mode,
348 int flags, GLuint previousFbo) {
349 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700350 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700351
Romain Guyeb993562010-10-05 18:14:38 -0700352 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
353
Romain Guyf607bdc2010-09-10 19:20:06 -0700354 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700355 Rect bounds(left, top, right, bottom);
Romain Guyae88e5e2010-10-22 17:49:18 -0700356 if (fboLayer) {
357 // Clear the previous layer regions before we change the viewport
358 clearLayerRegions();
359 } else {
Romain Guyeb993562010-10-05 18:14:38 -0700360 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700361
Romain Guyeb993562010-10-05 18:14:38 -0700362 // Layers only make sense if they are in the framebuffer's bounds
Romain Guy58ae6db2010-10-22 11:16:37 -0700363 bounds.intersect(*snapshot->clipRect);
Romain Guyae517592010-10-22 10:40:27 -0700364
Romain Guyae88e5e2010-10-22 17:49:18 -0700365 // We cannot work with sub-pixels in this case
366 bounds.snapToPixelBoundaries();
367
Romain Guyae517592010-10-22 10:40:27 -0700368 // When the layer is not an FBO, we may use glCopyTexImage so we
369 // need to make sure the layer does not extend outside the bounds
370 // of the framebuffer
371 bounds.intersect(snapshot->previous->viewport);
Romain Guyeb993562010-10-05 18:14:38 -0700372 }
Romain Guybf434112010-09-16 14:40:17 -0700373
Romain Guyb025b9c2010-09-16 14:16:48 -0700374 if (bounds.isEmpty() || bounds.getWidth() > mMaxTextureSize ||
375 bounds.getHeight() > mMaxTextureSize) {
Romain Guydbc26d22010-10-11 17:58:29 -0700376 snapshot->invisible = true;
377 } else {
378 // TODO: Should take the mode into account
Romain Guyd2a1ff02010-10-14 14:46:44 -0700379 snapshot->invisible = snapshot->previous->invisible ||
380 (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700381 }
382
383 // Bail out if we won't draw in this snapshot
384 if (snapshot->invisible) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700385 return false;
386 }
Romain Guyf18fd992010-07-08 11:45:51 -0700387
Romain Guy0bb56672010-10-01 00:25:02 -0700388 glActiveTexture(GL_TEXTURE0);
389
Romain Guy8550c4c2010-10-08 15:49:53 -0700390 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700391 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700392 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700393 }
394
Romain Guydda570202010-07-06 11:39:32 -0700395 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700396 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700397 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700398 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
399 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guydda570202010-07-06 11:39:32 -0700400
Romain Guy8fb95422010-08-17 18:38:51 -0700401 // Save the layer in the snapshot
402 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700403 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700404
Romain Guyeb993562010-10-05 18:14:38 -0700405 if (fboLayer) {
406 layer->fbo = mCaches.fboCache.get();
Romain Guy38c85b92010-09-22 22:48:20 -0700407
Romain Guyeb993562010-10-05 18:14:38 -0700408 snapshot->flags |= Snapshot::kFlagIsFboLayer;
409 snapshot->fbo = layer->fbo;
410 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
411 snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
412 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
413 snapshot->height = bounds.getHeight();
414 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
415 snapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf607bdc2010-09-10 19:20:06 -0700416
Romain Guyeb993562010-10-05 18:14:38 -0700417 // Bind texture to FBO
418 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
419 glBindTexture(GL_TEXTURE_2D, layer->texture);
420
421 // Initialize the texture if needed
422 if (layer->empty) {
423 layer->empty = false;
Romain Guy8550c4c2010-10-08 15:49:53 -0700424 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
Romain Guyeb993562010-10-05 18:14:38 -0700425 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
426 }
427
428 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
429 layer->texture, 0);
430
Romain Guye9108052010-10-12 18:15:42 -0700431#if DEBUG_LAYERS
Romain Guyeb993562010-10-05 18:14:38 -0700432 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
433 if (status != GL_FRAMEBUFFER_COMPLETE) {
434 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
435
436 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
437 glDeleteTextures(1, &layer->texture);
438 mCaches.fboCache.put(layer->fbo);
439
440 delete layer;
441
442 return false;
443 }
Romain Guye9108052010-10-12 18:15:42 -0700444#endif
Romain Guyeb993562010-10-05 18:14:38 -0700445
446 // Clear the FBO
Romain Guy93d23612010-10-13 18:26:36 -0700447 glScissor(0.0f, 0.0f, bounds.getWidth() + 1.0f, bounds.getHeight() + 1.0f);
Romain Guyeb993562010-10-05 18:14:38 -0700448 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
449 glClear(GL_COLOR_BUFFER_BIT);
Romain Guyeb07af62010-10-12 18:40:36 -0700450
451 setScissorFromClip();
Romain Guyeb993562010-10-05 18:14:38 -0700452
453 // Change the ortho projection
454 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
455 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
456 } else {
457 // Copy the framebuffer into the layer
458 glBindTexture(GL_TEXTURE_2D, layer->texture);
459
Romain Guyae88e5e2010-10-22 17:49:18 -0700460 if (layer->empty) {
461 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
462 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
463 layer->empty = false;
464 } else {
465 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
466 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
467 }
Romain Guyeb993562010-10-05 18:14:38 -0700468
469 // Enqueue the buffer coordinates to clear the corresponding region later
470 mLayers.push(new Rect(bounds));
471 }
Romain Guyf86ef572010-07-01 11:05:42 -0700472
Romain Guyd55a8612010-06-28 17:42:46 -0700473 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700474}
475
Romain Guy1c740bc2010-09-13 18:00:09 -0700476/**
477 * Read the documentation of createLayer() before doing anything in this method.
478 */
Romain Guy1d83e192010-08-17 11:37:00 -0700479void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
480 if (!current->layer) {
481 LOGE("Attempting to compose a layer that does not exist");
482 return;
483 }
484
Romain Guyeb993562010-10-05 18:14:38 -0700485 const bool fboLayer = current->flags & SkCanvas::kClipToLayer_SaveFlag;
486
487 if (fboLayer) {
488 // Unbind current FBO and restore previous one
489 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
490 }
491
Romain Guy1d83e192010-08-17 11:37:00 -0700492 // Restore the clip from the previous snapshot
Romain Guyae88e5e2010-10-22 17:49:18 -0700493 Rect& clip(*previous->clipRect);
494 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700495 glScissor(clip.left, previous->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700496
497 Layer* layer = current->layer;
498 const Rect& rect = layer->layer;
499
Romain Guyeb993562010-10-05 18:14:38 -0700500 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700501 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700502 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guyf607bdc2010-09-10 19:20:06 -0700503 }
504
Romain Guy8550c4c2010-10-08 15:49:53 -0700505 const Rect& texCoords = layer->texCoords;
Romain Guy03750a02010-10-18 14:06:08 -0700506 mCaches.unbindMeshBuffer();
Romain Guy8550c4c2010-10-08 15:49:53 -0700507 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
Romain Guy8b55f372010-08-18 17:10:07 -0700508
Romain Guyeb993562010-10-05 18:14:38 -0700509 if (fboLayer) {
Romain Guy03750a02010-10-18 14:06:08 -0700510 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
511 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
512 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyeb993562010-10-05 18:14:38 -0700513 } else {
514 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
515 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
516 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, true, true);
517 }
Romain Guy1d83e192010-08-17 11:37:00 -0700518
Romain Guy8b55f372010-08-18 17:10:07 -0700519 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
520
Romain Guyeb993562010-10-05 18:14:38 -0700521 if (fboLayer) {
522 // Detach the texture from the FBO
523 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
524 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
525 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
526
527 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
528 mCaches.fboCache.put(current->fbo);
529 }
530
Romain Guyeb993562010-10-05 18:14:38 -0700531 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700532 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700533 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700534 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700535 delete layer;
536 }
537}
538
Romain Guy86942302010-09-12 13:02:16 -0700539void OpenGLRenderer::clearLayerRegions() {
Romain Guydbc26d22010-10-11 17:58:29 -0700540 if (mLayers.size() == 0 || mSnapshot->invisible) return;
Romain Guy86942302010-09-12 13:02:16 -0700541
542 for (uint32_t i = 0; i < mLayers.size(); i++) {
543 Rect* bounds = mLayers.itemAt(i);
544
545 // Clear the framebuffer where the layer will draw
Romain Guyd2a1ff02010-10-14 14:46:44 -0700546 glScissor(bounds->left, mSnapshot->height - bounds->bottom,
Romain Guy86942302010-09-12 13:02:16 -0700547 bounds->getWidth(), bounds->getHeight());
548 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
549 glClear(GL_COLOR_BUFFER_BIT);
550
551 delete bounds;
552 }
553 mLayers.clear();
554
555 // Restore the clip
556 setScissorFromClip();
557}
558
Romain Guybd6b79b2010-06-26 00:13:53 -0700559///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700560// Transforms
561///////////////////////////////////////////////////////////////////////////////
562
563void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700564 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700565}
566
567void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700568 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700569}
570
571void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700572 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700573}
574
575void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700576 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700577}
578
Romain Guy41030da2010-10-13 13:40:37 -0700579const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700580 if (mSnapshot->fbo != 0) {
581 return &mSnapshot->transform->data[0];
582 }
583 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700584}
585
Romain Guyf6a11b82010-06-23 17:47:49 -0700586void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700587 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700588}
589
590void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700591 SkMatrix transform;
592 mSnapshot->transform->copyTo(transform);
593 transform.preConcat(*matrix);
594 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700595}
596
597///////////////////////////////////////////////////////////////////////////////
598// Clipping
599///////////////////////////////////////////////////////////////////////////////
600
Romain Guybb9524b2010-06-22 18:56:38 -0700601void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700602 Rect clip(*mSnapshot->clipRect);
603 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700604 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700605}
606
607const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700608 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700609}
610
Romain Guyc7d53492010-06-25 13:41:57 -0700611bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guydbc26d22010-10-11 17:58:29 -0700612 if (mSnapshot->invisible) {
613 return true;
614 }
615
Romain Guy1d83e192010-08-17 11:37:00 -0700616 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700617 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700618 r.snapToPixelBoundaries();
619
620 Rect clipRect(*mSnapshot->clipRect);
621 clipRect.snapToPixelBoundaries();
622
623 return !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
Chet Haase5c13d892010-10-08 08:37:55 -0700638void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, 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
Chet Haase5c13d892010-10-08 08:37:55 -0700654void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -0700655 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,
Chet Haase5c13d892010-10-08 08:37:55 -0700674 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
Romain Guy03750a02010-10-18 14:06:08 -0700692 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -0700693 resetDrawTextureTexCoords(u1, v1, u2, v2);
694
Romain Guy03750a02010-10-18 14:06:08 -0700695 int alpha;
696 SkXfermode::Mode mode;
697 getAlphaAndMode(paint, &alpha, &mode);
698
699 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
700 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
701 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy8ba548f2010-06-30 19:21:21 -0700702
703 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
704}
705
Romain Guy4aa90572010-09-26 18:40:37 -0700706void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -0700707 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -0700708 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700709 if (quickReject(left, top, right, bottom)) {
710 return;
711 }
712
Romain Guy61c8c9c2010-08-09 20:48:09 -0700713 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700714 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700715 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700716 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700717
718 int alpha;
719 SkXfermode::Mode mode;
720 getAlphaAndMode(paint, &alpha, &mode);
721
Romain Guy2728f962010-10-08 18:36:15 -0700722 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -0700723 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -0700724
Romain Guy054dc182010-10-15 17:55:25 -0700725 if (mesh) {
726 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
727 // patch mesh already defines the final size
728 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
Romain Guy03750a02010-10-18 14:06:08 -0700729 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
730 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer);
Romain Guy054dc182010-10-15 17:55:25 -0700731 }
Romain Guyf7f93552010-07-08 19:17:03 -0700732}
733
Chet Haase5c13d892010-10-08 08:37:55 -0700734void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -0700735 if (mSnapshot->invisible) return;
736
Romain Guy759ea802010-09-16 20:49:46 -0700737 int alpha;
738 SkXfermode::Mode mode;
739 getAlphaAndMode(paint, &alpha, &mode);
740
741 uint32_t color = paint->getColor();
742 const GLfloat a = alpha / 255.0f;
743 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
744 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
745 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
746
Romain Guyc95c8d62010-09-17 15:31:32 -0700747 const bool isAA = paint->isAntiAlias();
748 if (isAA) {
749 GLuint textureUnit = 0;
Romain Guy29d89972010-09-22 16:10:57 -0700750 setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
Romain Guy03750a02010-10-18 14:06:08 -0700751 mode, false, true, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
752 mCaches.line.getMeshBuffer());
Romain Guyc95c8d62010-09-17 15:31:32 -0700753 } else {
754 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false);
755 }
756
757 const float strokeWidth = paint->getStrokeWidth();
Romain Guy29d89972010-09-22 16:10:57 -0700758 const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount;
Romain Guyc95c8d62010-09-17 15:31:32 -0700759 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
Romain Guy759ea802010-09-16 20:49:46 -0700760
761 for (int i = 0; i < count; i += 4) {
762 float tx = 0.0f;
763 float ty = 0.0f;
764
Romain Guyc95c8d62010-09-17 15:31:32 -0700765 if (isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700766 mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3],
Romain Guyc95c8d62010-09-17 15:31:32 -0700767 strokeWidth, tx, ty);
768 } else {
Romain Guyb5ab4172010-09-17 15:36:56 -0700769 ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f;
Romain Guyc95c8d62010-09-17 15:31:32 -0700770 }
Romain Guy759ea802010-09-16 20:49:46 -0700771
772 const float dx = points[i + 2] - points[i];
773 const float dy = points[i + 3] - points[i + 1];
774 const float mag = sqrtf(dx * dx + dy * dy);
775 const float angle = acos(dx / mag);
776
777 mModelView.loadTranslate(points[i], points[i + 1], 0.0f);
778 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) {
779 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f);
780 }
781 mModelView.translate(tx, ty, 0.0f);
Romain Guyc95c8d62010-09-17 15:31:32 -0700782 if (!isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700783 float length = mCaches.line.getLength(points[i], points[i + 1],
784 points[i + 2], points[i + 3]);
Romain Guyc95c8d62010-09-17 15:31:32 -0700785 mModelView.scale(length, strokeWidth, 1.0f);
786 }
Romain Guy759ea802010-09-16 20:49:46 -0700787 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
788
789 if (mShader) {
790 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot);
791 }
792
Romain Guyc95c8d62010-09-17 15:31:32 -0700793 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy759ea802010-09-16 20:49:46 -0700794 }
795
Romain Guy29d89972010-09-22 16:10:57 -0700796 if (isAA) {
797 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
798 }
Romain Guy759ea802010-09-16 20:49:46 -0700799}
800
Romain Guy85bf02f2010-06-22 13:11:24 -0700801void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guyae88e5e2010-10-22 17:49:18 -0700802 Rect& clip(*mSnapshot->clipRect);
803 clip.snapToPixelBoundaries();
Romain Guy3d58c032010-07-14 16:34:53 -0700804 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700805}
Romain Guy9d5316e2010-06-24 19:30:36 -0700806
Chet Haase5c13d892010-10-08 08:37:55 -0700807void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700808 if (quickReject(left, top, right, bottom)) {
809 return;
810 }
811
Romain Guy026c5e162010-06-28 17:12:22 -0700812 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700813 if (!mExtensions.hasFramebufferFetch()) {
814 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
815 if (!isMode) {
816 // Assume SRC_OVER
817 mode = SkXfermode::kSrcOver_Mode;
818 }
819 } else {
820 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700821 }
822
823 // Skia draws using the color's alpha channel if < 255
824 // Otherwise, it uses the paint's alpha
825 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700826 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700827 color |= p->getAlpha() << 24;
828 }
829
830 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700831}
Romain Guy9d5316e2010-06-24 19:30:36 -0700832
Romain Guye8e62a42010-07-23 18:55:21 -0700833void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
834 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700835 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700836 return;
837 }
Romain Guydbc26d22010-10-11 17:58:29 -0700838 if (mSnapshot->invisible) return;
Romain Guye2d345e2010-09-24 18:39:22 -0700839
Romain Guyf607bdc2010-09-10 19:20:06 -0700840 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700841
Romain Guya674ab72010-08-10 17:26:42 -0700842 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700843 switch (paint->getTextAlign()) {
844 case SkPaint::kCenter_Align:
845 length = paint->measureText(text, bytesCount);
846 x -= length / 2.0f;
847 break;
848 case SkPaint::kRight_Align:
849 length = paint->measureText(text, bytesCount);
850 x -= length;
851 break;
852 default:
853 break;
854 }
855
Romain Guy694b5192010-07-21 21:33:20 -0700856 int alpha;
857 SkXfermode::Mode mode;
858 getAlphaAndMode(paint, &alpha, &mode);
859
Romain Guy2542d192010-08-18 11:47:12 -0700860 uint32_t color = paint->getColor();
861 const GLfloat a = alpha / 255.0f;
862 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
863 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
864 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
865
Romain Guyb45c0c92010-08-26 20:35:23 -0700866 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
867 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700868 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -0700869
Romain Guy9d13fe252010-10-15 16:06:03 -0700870 Rect clipRect(*mSnapshot->clipRect);
Romain Guyae88e5e2010-10-22 17:49:18 -0700871 clipRect.snapToPixelBoundaries();
Romain Guy9d13fe252010-10-15 16:06:03 -0700872 glScissor(clipRect.left, mSnapshot->height - clipRect.bottom,
873 clipRect.getWidth(), clipRect.getHeight());
874
Romain Guy1e45aae2010-08-13 19:39:53 -0700875 if (mHasShadow) {
876 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700877 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700878 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700879 count, mShadowRadius);
880 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700881
Romain Guy2542d192010-08-18 11:47:12 -0700882 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700883
884 // Draw the mesh
885 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700886 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700887 }
888
Romain Guy06f96e22010-07-30 19:18:16 -0700889 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700890 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700891
Romain Guye8cb9c142010-10-04 14:14:11 -0700892 // Assume that the modelView matrix does not force scales, rotates, etc.
893 const bool linearFilter = mSnapshot->transform->changesBounds();
894 setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit,
Romain Guy03750a02010-10-18 14:06:08 -0700895 x, y, r, g, b, a, mode, false, true, NULL, NULL);
Romain Guy06f96e22010-07-30 19:18:16 -0700896
Romain Guy09147fb2010-07-22 13:08:20 -0700897 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy86942302010-09-12 13:02:16 -0700898 clearLayerRegions();
Romain Guy9d13fe252010-10-15 16:06:03 -0700899
Romain Guy03750a02010-10-18 14:06:08 -0700900 mCaches.unbindMeshBuffer();
Romain Guyb45c0c92010-08-26 20:35:23 -0700901 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700902
903 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700904 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700905
Romain Guy0a417492010-08-16 20:26:20 -0700906 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy9d13fe252010-10-15 16:06:03 -0700907
908 setScissorFromClip();
Romain Guy694b5192010-07-21 21:33:20 -0700909}
910
Romain Guy7fbcc042010-08-04 15:40:07 -0700911void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -0700912 if (mSnapshot->invisible) return;
913
Romain Guy7fbcc042010-08-04 15:40:07 -0700914 GLuint textureUnit = 0;
915 glActiveTexture(gTextureUnits[textureUnit]);
916
Romain Guyfb8b7632010-08-23 21:05:08 -0700917 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700918 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700919 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700920
Romain Guy8b55f372010-08-18 17:10:07 -0700921 const float x = texture->left - texture->offset;
922 const float y = texture->top - texture->offset;
923
924 if (quickReject(x, y, x + texture->width, y + texture->height)) {
925 return;
926 }
927
Romain Guy7fbcc042010-08-04 15:40:07 -0700928 int alpha;
929 SkXfermode::Mode mode;
930 getAlphaAndMode(paint, &alpha, &mode);
931
932 uint32_t color = paint->getColor();
933 const GLfloat a = alpha / 255.0f;
934 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
935 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
936 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
937
Romain Guy0a417492010-08-16 20:26:20 -0700938 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
939
Romain Guy86942302010-09-12 13:02:16 -0700940 clearLayerRegions();
941
Romain Guy0a417492010-08-16 20:26:20 -0700942 // Draw the mesh
943 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700944 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700945}
946
Romain Guy6926c722010-07-12 20:20:03 -0700947///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700948// Shaders
949///////////////////////////////////////////////////////////////////////////////
950
951void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700952 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700953}
954
Romain Guy06f96e22010-07-30 19:18:16 -0700955void OpenGLRenderer::setupShader(SkiaShader* shader) {
956 mShader = shader;
957 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700958 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700959 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700960}
961
Romain Guyd27977d2010-07-14 19:18:51 -0700962///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700963// Color filters
964///////////////////////////////////////////////////////////////////////////////
965
966void OpenGLRenderer::resetColorFilter() {
967 mColorFilter = NULL;
968}
969
970void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
971 mColorFilter = filter;
972}
973
974///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700975// Drop shadow
976///////////////////////////////////////////////////////////////////////////////
977
978void OpenGLRenderer::resetShadow() {
979 mHasShadow = false;
980}
981
982void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
983 mHasShadow = true;
984 mShadowRadius = radius;
985 mShadowDx = dx;
986 mShadowDy = dy;
987 mShadowColor = color;
988}
989
990///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700991// Drawing implementation
992///////////////////////////////////////////////////////////////////////////////
993
Romain Guy0a417492010-08-16 20:26:20 -0700994void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700995 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700996 const float sx = x - texture->left + mShadowDx;
997 const float sy = y - texture->top + mShadowDy;
998
Romain Guy2542d192010-08-18 11:47:12 -0700999 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1000 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -07001001 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
1002 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
1003 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
1004
1005 GLuint textureUnit = 0;
1006 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
1007}
1008
1009void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
1010 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
1011 bool transforms, bool applyFilters) {
1012 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -07001013 x, y, r, g, b, a, mode, transforms, applyFilters,
Romain Guy03750a02010-10-18 14:06:08 -07001014 (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy0a417492010-08-16 20:26:20 -07001015}
1016
1017void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1018 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1019 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy03750a02010-10-18 14:06:08 -07001020 setupTextureAlpha8(texture, width, height, textureUnit, x, y, r, g, b, a, mode,
1021 transforms, applyFilters, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy759ea802010-09-16 20:49:46 -07001022}
1023
1024void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1025 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1026 SkXfermode::Mode mode, bool transforms, bool applyFilters,
Romain Guy03750a02010-10-18 14:06:08 -07001027 GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guy0a417492010-08-16 20:26:20 -07001028 // Describe the required shaders
1029 ProgramDescription description;
1030 description.hasTexture = true;
1031 description.hasAlpha8Texture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001032 const bool setColor = description.setAlpha8Color(r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -07001033
1034 if (applyFilters) {
1035 if (mShader) {
1036 mShader->describe(description, mExtensions);
1037 }
1038 if (mColorFilter) {
1039 mColorFilter->describe(description, mExtensions);
1040 }
1041 }
1042
Romain Guya5aed0d2010-09-09 14:42:43 -07001043 // Setup the blending mode
1044 chooseBlending(true, mode, description);
1045
Romain Guy0a417492010-08-16 20:26:20 -07001046 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001047 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -07001048
Romain Guy0a417492010-08-16 20:26:20 -07001049 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -07001050 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001051
Romain Guyfb8b7632010-08-23 21:05:08 -07001052 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -07001053 glEnableVertexAttribArray(texCoordsSlot);
1054
Romain Guy03750a02010-10-18 14:06:08 -07001055 if (texCoords) {
1056 // Setup attributes
1057 if (!vertices) {
1058 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1059 } else {
1060 mCaches.unbindMeshBuffer();
1061 }
1062 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1063 gMeshStride, vertices);
1064 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1065 }
Romain Guy0a417492010-08-16 20:26:20 -07001066
1067 // Setup uniforms
1068 if (transforms) {
1069 mModelView.loadTranslate(x, y, 0.0f);
1070 mModelView.scale(width, height, 1.0f);
1071 } else {
1072 mModelView.loadIdentity();
1073 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001074 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy707b2f72010-10-11 16:34:59 -07001075 if (setColor) {
1076 mCaches.currentProgram->setColor(r, g, b, a);
1077 }
Romain Guy0a417492010-08-16 20:26:20 -07001078
1079 textureUnit++;
1080 if (applyFilters) {
1081 // Setup attributes and uniforms required by the shaders
1082 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001083 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001084 }
1085 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001086 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -07001087 }
1088 }
1089}
1090
Romain Guyf607bdc2010-09-10 19:20:06 -07001091// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001092#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1093#define kStdUnderline_Offset (1.0f / 9.0f)
1094#define kStdUnderline_Thickness (1.0f / 18.0f)
1095
1096void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1097 float x, float y, SkPaint* paint) {
1098 // Handle underline and strike-through
1099 uint32_t flags = paint->getFlags();
1100 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1101 float underlineWidth = length;
1102 // If length is > 0.0f, we already measured the text for the text alignment
1103 if (length <= 0.0f) {
1104 underlineWidth = paint->measureText(text, bytesCount);
1105 }
1106
1107 float offsetX = 0;
1108 switch (paint->getTextAlign()) {
1109 case SkPaint::kCenter_Align:
1110 offsetX = underlineWidth * 0.5f;
1111 break;
1112 case SkPaint::kRight_Align:
1113 offsetX = underlineWidth;
1114 break;
1115 default:
1116 break;
1117 }
1118
1119 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001120 const float textSize = paint->getTextSize();
1121 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -07001122
Romain Guye20ecbd2010-09-22 19:49:04 -07001123 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001124 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001125
1126 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
1127 float points[pointsCount];
1128 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001129
1130 if (flags & SkPaint::kUnderlineText_Flag) {
1131 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001132 points[currentPoint++] = left;
1133 points[currentPoint++] = top;
1134 points[currentPoint++] = left + underlineWidth;
1135 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001136 }
1137
1138 if (flags & SkPaint::kStrikeThruText_Flag) {
1139 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001140 points[currentPoint++] = left;
1141 points[currentPoint++] = top;
1142 points[currentPoint++] = left + underlineWidth;
1143 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001144 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001145
1146 SkPaint linesPaint(*paint);
1147 linesPaint.setStrokeWidth(strokeWidth);
1148
1149 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001150 }
1151 }
1152}
1153
Romain Guy026c5e162010-06-28 17:12:22 -07001154void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001155 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001156 clearLayerRegions();
1157
Romain Guyd27977d2010-07-14 19:18:51 -07001158 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001159 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001160 color |= 0x00ffffff;
1161 }
1162
1163 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -07001164 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -07001165 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -07001166 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1167 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1168 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1169
Romain Guyc95c8d62010-09-17 15:31:32 -07001170 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform);
1171
1172 // Draw the mesh
1173 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1174}
1175
1176void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom,
1177 float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy06f96e22010-07-30 19:18:16 -07001178 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -07001179
Romain Guy06f96e22010-07-30 19:18:16 -07001180 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -07001181 ProgramDescription description;
Romain Guy707b2f72010-10-11 16:34:59 -07001182 const bool setColor = description.setColor(r, g, b, a);
1183
Romain Guy06f96e22010-07-30 19:18:16 -07001184 if (mShader) {
1185 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -07001186 }
Romain Guydb1938e2010-08-02 18:50:22 -07001187 if (mColorFilter) {
1188 mColorFilter->describe(description, mExtensions);
1189 }
Romain Guyd27977d2010-07-14 19:18:51 -07001190
Romain Guy1c740bc2010-09-13 18:00:09 -07001191 // Setup the blending mode
Romain Guyc95c8d62010-09-17 15:31:32 -07001192 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -07001193
Romain Guy06f96e22010-07-30 19:18:16 -07001194 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001195 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -07001196
1197 // Setup attributes
Romain Guy03750a02010-10-18 14:06:08 -07001198 mCaches.bindMeshBuffer();
Romain Guyfb8b7632010-08-23 21:05:08 -07001199 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy03750a02010-10-18 14:06:08 -07001200 gMeshStride, 0);
Romain Guy06f96e22010-07-30 19:18:16 -07001201
1202 // Setup uniforms
1203 mModelView.loadTranslate(left, top, 0.0f);
1204 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -07001205 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001206 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -07001207 } else {
1208 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -07001209 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -07001210 }
Romain Guy707b2f72010-10-11 16:34:59 -07001211 mCaches.currentProgram->setColor(r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -07001212
Romain Guy06f96e22010-07-30 19:18:16 -07001213 // Setup attributes and uniforms required by the shaders
1214 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001215 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001216 }
Romain Guydb1938e2010-08-02 18:50:22 -07001217 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001218 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001219 }
Romain Guyd27977d2010-07-14 19:18:51 -07001220}
1221
Romain Guy82ba8142010-07-09 13:25:56 -07001222void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001223 const Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001224 int alpha;
1225 SkXfermode::Mode mode;
1226 getAlphaAndMode(paint, &alpha, &mode);
1227
Romain Guy6820ac82010-09-15 18:11:50 -07001228 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy03750a02010-10-18 14:06:08 -07001229 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy6820ac82010-09-15 18:11:50 -07001230 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -07001231}
1232
Romain Guybd6b79b2010-06-26 00:13:53 -07001233void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001234 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1235 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001236 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001237}
1238
1239void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001240 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001241 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy03750a02010-10-18 14:06:08 -07001242 bool swapSrcDst, bool ignoreTransform, GLuint vbo) {
Romain Guy86942302010-09-12 13:02:16 -07001243 clearLayerRegions();
1244
Romain Guy889f8d12010-07-29 14:37:42 -07001245 ProgramDescription description;
1246 description.hasTexture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001247 const bool setColor = description.setColor(alpha, alpha, alpha, alpha);
Romain Guydb1938e2010-08-02 18:50:22 -07001248 if (mColorFilter) {
1249 mColorFilter->describe(description, mExtensions);
1250 }
Romain Guy889f8d12010-07-29 14:37:42 -07001251
Romain Guybd6b79b2010-06-26 00:13:53 -07001252 mModelView.loadTranslate(left, top, 0.0f);
1253 mModelView.scale(right - left, bottom - top, 1.0f);
1254
Romain Guyf607bdc2010-09-10 19:20:06 -07001255 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001256
Romain Guyfb8b7632010-08-23 21:05:08 -07001257 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001258 if (!ignoreTransform) {
1259 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1260 } else {
1261 mat4 m;
1262 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
1263 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001264
Romain Guy889f8d12010-07-29 14:37:42 -07001265 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -07001266 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001267 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001268
Romain Guya9794742010-07-13 11:37:54 -07001269 // Always premultiplied
Romain Guy707b2f72010-10-11 16:34:59 -07001270 if (setColor) {
1271 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha);
1272 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001273
Romain Guy889f8d12010-07-29 14:37:42 -07001274 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001275 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001276 glEnableVertexAttribArray(texCoordsSlot);
Romain Guy03750a02010-10-18 14:06:08 -07001277
1278 if (!vertices) {
1279 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1280 } else {
1281 mCaches.unbindMeshBuffer();
1282 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001283 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001284 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001285 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001286
Romain Guydb1938e2010-08-02 18:50:22 -07001287 // Color filter
1288 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001289 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001290 }
1291
Romain Guy6820ac82010-09-15 18:11:50 -07001292 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001293 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001294}
1295
Romain Guya5aed0d2010-09-09 14:42:43 -07001296void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001297 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001298 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1299 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001300 if (mode < SkXfermode::kPlus_Mode) {
1301 if (!mCaches.blend) {
1302 glEnable(GL_BLEND);
1303 }
Romain Guy82ba8142010-07-09 13:25:56 -07001304
Romain Guyf607bdc2010-09-10 19:20:06 -07001305 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1306 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001307
Romain Guya5aed0d2010-09-09 14:42:43 -07001308 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1309 glBlendFunc(sourceMode, destMode);
1310 mCaches.lastSrcMode = sourceMode;
1311 mCaches.lastDstMode = destMode;
1312 }
1313 } else {
1314 // These blend modes are not supported by OpenGL directly and have
1315 // to be implemented using shaders. Since the shader will perform
1316 // the blending, turn blending off here
1317 if (mExtensions.hasFramebufferFetch()) {
1318 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001319 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001320 }
1321
1322 if (mCaches.blend) {
1323 glDisable(GL_BLEND);
1324 }
1325 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001326 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001327 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001328 glDisable(GL_BLEND);
1329 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001330 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001331}
1332
Romain Guy889f8d12010-07-29 14:37:42 -07001333bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001334 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001335 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001336 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001337 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001338 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001339 }
Romain Guy6926c722010-07-12 20:20:03 -07001340 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001341}
1342
Romain Guy026c5e162010-06-28 17:12:22 -07001343void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001344 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001345 TextureVertex::setUV(v++, u1, v1);
1346 TextureVertex::setUV(v++, u2, v1);
1347 TextureVertex::setUV(v++, u1, v2);
1348 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001349}
1350
Chet Haase5c13d892010-10-08 08:37:55 -07001351void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001352 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001353 if (!mExtensions.hasFramebufferFetch()) {
1354 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1355 if (!isMode) {
1356 // Assume SRC_OVER
1357 *mode = SkXfermode::kSrcOver_Mode;
1358 }
1359 } else {
1360 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001361 }
1362
1363 // Skia draws using the color's alpha channel if < 255
1364 // Otherwise, it uses the paint's alpha
1365 int color = paint->getColor();
1366 *alpha = (color >> 24) & 0xFF;
1367 if (*alpha == 255) {
1368 *alpha = paint->getAlpha();
1369 }
1370 } else {
1371 *mode = SkXfermode::kSrcOver_Mode;
1372 *alpha = 255;
1373 }
Romain Guy026c5e162010-06-28 17:12:22 -07001374}
1375
Romain Guya5aed0d2010-09-09 14:42:43 -07001376SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1377 if (mode == NULL) {
1378 return SkXfermode::kSrcOver_Mode;
1379 }
1380 return mode->fMode;
1381}
1382
Romain Guy889f8d12010-07-29 14:37:42 -07001383void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1384 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001385 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001386 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1387 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1388}
1389
Romain Guy9d5316e2010-06-24 19:30:36 -07001390}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001391}; // namespace android