blob: 9c37983fbb0b951bdb63de10533f6c810d25900f [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 Guydda57022010-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 Guydda57022010-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 Guydda57022010-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 Guydda57022010-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 Guy8164c2d2010-10-25 18:03:28 -0700647 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-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 Guy8164c2d2010-10-25 18:03:28 -0700664 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-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 Guy8164c2d2010-10-25 18:03:28 -0700680 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700681 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700682 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -0700683 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -0700684
Romain Guy8ba548f2010-06-30 19:21:21 -0700685 const float width = texture->width;
686 const float height = texture->height;
687
688 const float u1 = srcLeft / width;
689 const float v1 = srcTop / height;
690 const float u2 = srcRight / width;
691 const float v2 = srcBottom / height;
692
Romain Guy03750a02010-10-18 14:06:08 -0700693 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -0700694 resetDrawTextureTexCoords(u1, v1, u2, v2);
695
Romain Guy03750a02010-10-18 14:06:08 -0700696 int alpha;
697 SkXfermode::Mode mode;
698 getAlphaAndMode(paint, &alpha, &mode);
699
700 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
701 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
702 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy8ba548f2010-06-30 19:21:21 -0700703
704 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
705}
706
Romain Guy4aa90572010-09-26 18:40:37 -0700707void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -0700708 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -0700709 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700710 if (quickReject(left, top, right, bottom)) {
711 return;
712 }
713
Romain Guy61c8c9c2010-08-09 20:48:09 -0700714 glActiveTexture(GL_TEXTURE0);
Romain Guy8164c2d2010-10-25 18:03:28 -0700715 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700716 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700717 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -0700718 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -0700719
720 int alpha;
721 SkXfermode::Mode mode;
722 getAlphaAndMode(paint, &alpha, &mode);
723
Romain Guy2728f962010-10-08 18:36:15 -0700724 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -0700725 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -0700726
Romain Guy054dc182010-10-15 17:55:25 -0700727 if (mesh) {
728 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
729 // patch mesh already defines the final size
730 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
Romain Guy03750a02010-10-18 14:06:08 -0700731 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
732 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer);
Romain Guy054dc182010-10-15 17:55:25 -0700733 }
Romain Guyf7f93552010-07-08 19:17:03 -0700734}
735
Chet Haase5c13d892010-10-08 08:37:55 -0700736void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -0700737 if (mSnapshot->invisible) return;
738
Romain Guy759ea802010-09-16 20:49:46 -0700739 int alpha;
740 SkXfermode::Mode mode;
741 getAlphaAndMode(paint, &alpha, &mode);
742
743 uint32_t color = paint->getColor();
744 const GLfloat a = alpha / 255.0f;
745 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
746 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
747 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
748
Romain Guyc95c8d62010-09-17 15:31:32 -0700749 const bool isAA = paint->isAntiAlias();
750 if (isAA) {
751 GLuint textureUnit = 0;
Romain Guy29d89972010-09-22 16:10:57 -0700752 setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
Romain Guy03750a02010-10-18 14:06:08 -0700753 mode, false, true, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
754 mCaches.line.getMeshBuffer());
Romain Guyc95c8d62010-09-17 15:31:32 -0700755 } else {
756 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false);
757 }
758
759 const float strokeWidth = paint->getStrokeWidth();
Romain Guy29d89972010-09-22 16:10:57 -0700760 const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount;
Romain Guyc95c8d62010-09-17 15:31:32 -0700761 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
Romain Guy759ea802010-09-16 20:49:46 -0700762
763 for (int i = 0; i < count; i += 4) {
764 float tx = 0.0f;
765 float ty = 0.0f;
766
Romain Guyc95c8d62010-09-17 15:31:32 -0700767 if (isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700768 mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3],
Romain Guyc95c8d62010-09-17 15:31:32 -0700769 strokeWidth, tx, ty);
770 } else {
Romain Guyb5ab4172010-09-17 15:36:56 -0700771 ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f;
Romain Guyc95c8d62010-09-17 15:31:32 -0700772 }
Romain Guy759ea802010-09-16 20:49:46 -0700773
774 const float dx = points[i + 2] - points[i];
775 const float dy = points[i + 3] - points[i + 1];
776 const float mag = sqrtf(dx * dx + dy * dy);
777 const float angle = acos(dx / mag);
778
779 mModelView.loadTranslate(points[i], points[i + 1], 0.0f);
780 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) {
781 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f);
782 }
783 mModelView.translate(tx, ty, 0.0f);
Romain Guyc95c8d62010-09-17 15:31:32 -0700784 if (!isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700785 float length = mCaches.line.getLength(points[i], points[i + 1],
786 points[i + 2], points[i + 3]);
Romain Guyc95c8d62010-09-17 15:31:32 -0700787 mModelView.scale(length, strokeWidth, 1.0f);
788 }
Romain Guy759ea802010-09-16 20:49:46 -0700789 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
790
791 if (mShader) {
792 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot);
793 }
794
Romain Guyc95c8d62010-09-17 15:31:32 -0700795 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy759ea802010-09-16 20:49:46 -0700796 }
797
Romain Guy29d89972010-09-22 16:10:57 -0700798 if (isAA) {
799 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
800 }
Romain Guy759ea802010-09-16 20:49:46 -0700801}
802
Romain Guy85bf02f2010-06-22 13:11:24 -0700803void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guyae88e5e2010-10-22 17:49:18 -0700804 Rect& clip(*mSnapshot->clipRect);
805 clip.snapToPixelBoundaries();
Romain Guy3d58c032010-07-14 16:34:53 -0700806 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700807}
Romain Guy9d5316e2010-06-24 19:30:36 -0700808
Chet Haase5c13d892010-10-08 08:37:55 -0700809void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700810 if (quickReject(left, top, right, bottom)) {
811 return;
812 }
813
Romain Guy026c5e162010-06-28 17:12:22 -0700814 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700815 if (!mExtensions.hasFramebufferFetch()) {
816 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
817 if (!isMode) {
818 // Assume SRC_OVER
819 mode = SkXfermode::kSrcOver_Mode;
820 }
821 } else {
822 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700823 }
824
825 // Skia draws using the color's alpha channel if < 255
826 // Otherwise, it uses the paint's alpha
827 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700828 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700829 color |= p->getAlpha() << 24;
830 }
831
832 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700833}
Romain Guy9d5316e2010-06-24 19:30:36 -0700834
Romain Guye8e62a42010-07-23 18:55:21 -0700835void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
836 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700837 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700838 return;
839 }
Romain Guydbc26d22010-10-11 17:58:29 -0700840 if (mSnapshot->invisible) return;
Romain Guye2d345e2010-09-24 18:39:22 -0700841
Romain Guyf607bdc2010-09-10 19:20:06 -0700842 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700843
Romain Guya674ab72010-08-10 17:26:42 -0700844 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700845 switch (paint->getTextAlign()) {
846 case SkPaint::kCenter_Align:
847 length = paint->measureText(text, bytesCount);
848 x -= length / 2.0f;
849 break;
850 case SkPaint::kRight_Align:
851 length = paint->measureText(text, bytesCount);
852 x -= length;
853 break;
854 default:
855 break;
856 }
857
Romain Guy694b5192010-07-21 21:33:20 -0700858 int alpha;
859 SkXfermode::Mode mode;
860 getAlphaAndMode(paint, &alpha, &mode);
861
Romain Guy2542d192010-08-18 11:47:12 -0700862 uint32_t color = paint->getColor();
863 const GLfloat a = alpha / 255.0f;
864 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
865 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
866 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
867
Romain Guyb45c0c92010-08-26 20:35:23 -0700868 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
869 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700870 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -0700871
Romain Guy9d13fe252010-10-15 16:06:03 -0700872 Rect clipRect(*mSnapshot->clipRect);
Romain Guyae88e5e2010-10-22 17:49:18 -0700873 clipRect.snapToPixelBoundaries();
Romain Guy9d13fe252010-10-15 16:06:03 -0700874 glScissor(clipRect.left, mSnapshot->height - clipRect.bottom,
875 clipRect.getWidth(), clipRect.getHeight());
876
Romain Guy1e45aae2010-08-13 19:39:53 -0700877 if (mHasShadow) {
878 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700879 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700880 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700881 count, mShadowRadius);
882 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700883
Romain Guy2542d192010-08-18 11:47:12 -0700884 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700885
886 // Draw the mesh
887 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700888 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700889 }
890
Romain Guy06f96e22010-07-30 19:18:16 -0700891 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700892 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700893
Romain Guye8cb9c142010-10-04 14:14:11 -0700894 // Assume that the modelView matrix does not force scales, rotates, etc.
895 const bool linearFilter = mSnapshot->transform->changesBounds();
896 setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit,
Romain Guy03750a02010-10-18 14:06:08 -0700897 x, y, r, g, b, a, mode, false, true, NULL, NULL);
Romain Guy06f96e22010-07-30 19:18:16 -0700898
Romain Guy09147fb2010-07-22 13:08:20 -0700899 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy86942302010-09-12 13:02:16 -0700900 clearLayerRegions();
Romain Guy9d13fe252010-10-15 16:06:03 -0700901
Romain Guy03750a02010-10-18 14:06:08 -0700902 mCaches.unbindMeshBuffer();
Romain Guyb45c0c92010-08-26 20:35:23 -0700903 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700904
905 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700906 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700907
Romain Guy0a417492010-08-16 20:26:20 -0700908 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy9d13fe252010-10-15 16:06:03 -0700909
910 setScissorFromClip();
Romain Guy694b5192010-07-21 21:33:20 -0700911}
912
Romain Guy7fbcc042010-08-04 15:40:07 -0700913void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -0700914 if (mSnapshot->invisible) return;
915
Romain Guy7fbcc042010-08-04 15:40:07 -0700916 GLuint textureUnit = 0;
917 glActiveTexture(gTextureUnits[textureUnit]);
918
Romain Guyfb8b7632010-08-23 21:05:08 -0700919 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700920 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700921 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700922
Romain Guy8b55f372010-08-18 17:10:07 -0700923 const float x = texture->left - texture->offset;
924 const float y = texture->top - texture->offset;
925
926 if (quickReject(x, y, x + texture->width, y + texture->height)) {
927 return;
928 }
929
Romain Guy7fbcc042010-08-04 15:40:07 -0700930 int alpha;
931 SkXfermode::Mode mode;
932 getAlphaAndMode(paint, &alpha, &mode);
933
934 uint32_t color = paint->getColor();
935 const GLfloat a = alpha / 255.0f;
936 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
937 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
938 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
939
Romain Guy0a417492010-08-16 20:26:20 -0700940 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
941
Romain Guy86942302010-09-12 13:02:16 -0700942 clearLayerRegions();
943
Romain Guy0a417492010-08-16 20:26:20 -0700944 // Draw the mesh
945 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700946 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700947}
948
Romain Guy6926c722010-07-12 20:20:03 -0700949///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700950// Shaders
951///////////////////////////////////////////////////////////////////////////////
952
953void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700954 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700955}
956
Romain Guy06f96e22010-07-30 19:18:16 -0700957void OpenGLRenderer::setupShader(SkiaShader* shader) {
958 mShader = shader;
959 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700960 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700961 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700962}
963
Romain Guyd27977d2010-07-14 19:18:51 -0700964///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700965// Color filters
966///////////////////////////////////////////////////////////////////////////////
967
968void OpenGLRenderer::resetColorFilter() {
969 mColorFilter = NULL;
970}
971
972void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
973 mColorFilter = filter;
974}
975
976///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700977// Drop shadow
978///////////////////////////////////////////////////////////////////////////////
979
980void OpenGLRenderer::resetShadow() {
981 mHasShadow = false;
982}
983
984void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
985 mHasShadow = true;
986 mShadowRadius = radius;
987 mShadowDx = dx;
988 mShadowDy = dy;
989 mShadowColor = color;
990}
991
992///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700993// Drawing implementation
994///////////////////////////////////////////////////////////////////////////////
995
Romain Guy0a417492010-08-16 20:26:20 -0700996void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700997 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700998 const float sx = x - texture->left + mShadowDx;
999 const float sy = y - texture->top + mShadowDy;
1000
Romain Guy2542d192010-08-18 11:47:12 -07001001 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1002 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -07001003 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
1004 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
1005 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
1006
1007 GLuint textureUnit = 0;
1008 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
1009}
1010
1011void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
1012 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
1013 bool transforms, bool applyFilters) {
1014 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -07001015 x, y, r, g, b, a, mode, transforms, applyFilters,
Romain Guy03750a02010-10-18 14:06:08 -07001016 (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy0a417492010-08-16 20:26:20 -07001017}
1018
1019void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1020 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1021 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy03750a02010-10-18 14:06:08 -07001022 setupTextureAlpha8(texture, width, height, textureUnit, x, y, r, g, b, a, mode,
1023 transforms, applyFilters, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy759ea802010-09-16 20:49:46 -07001024}
1025
1026void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1027 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1028 SkXfermode::Mode mode, bool transforms, bool applyFilters,
Romain Guy03750a02010-10-18 14:06:08 -07001029 GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guy0a417492010-08-16 20:26:20 -07001030 // Describe the required shaders
1031 ProgramDescription description;
1032 description.hasTexture = true;
1033 description.hasAlpha8Texture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001034 const bool setColor = description.setAlpha8Color(r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -07001035
1036 if (applyFilters) {
1037 if (mShader) {
1038 mShader->describe(description, mExtensions);
1039 }
1040 if (mColorFilter) {
1041 mColorFilter->describe(description, mExtensions);
1042 }
1043 }
1044
Romain Guya5aed0d2010-09-09 14:42:43 -07001045 // Setup the blending mode
1046 chooseBlending(true, mode, description);
1047
Romain Guy0a417492010-08-16 20:26:20 -07001048 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001049 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -07001050
Romain Guy8164c2d2010-10-25 18:03:28 -07001051 bindTexture(texture, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -07001052 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001053
Romain Guyfb8b7632010-08-23 21:05:08 -07001054 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -07001055 glEnableVertexAttribArray(texCoordsSlot);
1056
Romain Guy03750a02010-10-18 14:06:08 -07001057 if (texCoords) {
1058 // Setup attributes
1059 if (!vertices) {
1060 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1061 } else {
1062 mCaches.unbindMeshBuffer();
1063 }
1064 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1065 gMeshStride, vertices);
1066 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1067 }
Romain Guy0a417492010-08-16 20:26:20 -07001068
1069 // Setup uniforms
1070 if (transforms) {
1071 mModelView.loadTranslate(x, y, 0.0f);
1072 mModelView.scale(width, height, 1.0f);
1073 } else {
1074 mModelView.loadIdentity();
1075 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001076 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy707b2f72010-10-11 16:34:59 -07001077 if (setColor) {
1078 mCaches.currentProgram->setColor(r, g, b, a);
1079 }
Romain Guy0a417492010-08-16 20:26:20 -07001080
1081 textureUnit++;
1082 if (applyFilters) {
1083 // Setup attributes and uniforms required by the shaders
1084 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001085 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001086 }
1087 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001088 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -07001089 }
1090 }
1091}
1092
Romain Guyf607bdc2010-09-10 19:20:06 -07001093// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001094#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1095#define kStdUnderline_Offset (1.0f / 9.0f)
1096#define kStdUnderline_Thickness (1.0f / 18.0f)
1097
1098void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1099 float x, float y, SkPaint* paint) {
1100 // Handle underline and strike-through
1101 uint32_t flags = paint->getFlags();
1102 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1103 float underlineWidth = length;
1104 // If length is > 0.0f, we already measured the text for the text alignment
1105 if (length <= 0.0f) {
1106 underlineWidth = paint->measureText(text, bytesCount);
1107 }
1108
1109 float offsetX = 0;
1110 switch (paint->getTextAlign()) {
1111 case SkPaint::kCenter_Align:
1112 offsetX = underlineWidth * 0.5f;
1113 break;
1114 case SkPaint::kRight_Align:
1115 offsetX = underlineWidth;
1116 break;
1117 default:
1118 break;
1119 }
1120
1121 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001122 const float textSize = paint->getTextSize();
1123 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -07001124
Romain Guye20ecbd2010-09-22 19:49:04 -07001125 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001126 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001127
1128 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
1129 float points[pointsCount];
1130 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001131
1132 if (flags & SkPaint::kUnderlineText_Flag) {
1133 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001134 points[currentPoint++] = left;
1135 points[currentPoint++] = top;
1136 points[currentPoint++] = left + underlineWidth;
1137 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001138 }
1139
1140 if (flags & SkPaint::kStrikeThruText_Flag) {
1141 top = y + textSize * kStdStrikeThru_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 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001147
1148 SkPaint linesPaint(*paint);
1149 linesPaint.setStrokeWidth(strokeWidth);
1150
1151 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001152 }
1153 }
1154}
1155
Romain Guy026c5e162010-06-28 17:12:22 -07001156void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001157 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001158 clearLayerRegions();
1159
Romain Guyd27977d2010-07-14 19:18:51 -07001160 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001161 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001162 color |= 0x00ffffff;
1163 }
1164
1165 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -07001166 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -07001167 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -07001168 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1169 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1170 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1171
Romain Guyc95c8d62010-09-17 15:31:32 -07001172 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform);
1173
1174 // Draw the mesh
1175 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1176}
1177
1178void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom,
1179 float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy06f96e22010-07-30 19:18:16 -07001180 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -07001181
Romain Guy06f96e22010-07-30 19:18:16 -07001182 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -07001183 ProgramDescription description;
Romain Guy707b2f72010-10-11 16:34:59 -07001184 const bool setColor = description.setColor(r, g, b, a);
1185
Romain Guy06f96e22010-07-30 19:18:16 -07001186 if (mShader) {
1187 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -07001188 }
Romain Guydb1938e2010-08-02 18:50:22 -07001189 if (mColorFilter) {
1190 mColorFilter->describe(description, mExtensions);
1191 }
Romain Guyd27977d2010-07-14 19:18:51 -07001192
Romain Guy1c740bc2010-09-13 18:00:09 -07001193 // Setup the blending mode
Romain Guyc95c8d62010-09-17 15:31:32 -07001194 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -07001195
Romain Guy06f96e22010-07-30 19:18:16 -07001196 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001197 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -07001198
1199 // Setup attributes
Romain Guy03750a02010-10-18 14:06:08 -07001200 mCaches.bindMeshBuffer();
Romain Guyfb8b7632010-08-23 21:05:08 -07001201 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy03750a02010-10-18 14:06:08 -07001202 gMeshStride, 0);
Romain Guy06f96e22010-07-30 19:18:16 -07001203
1204 // Setup uniforms
1205 mModelView.loadTranslate(left, top, 0.0f);
1206 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -07001207 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001208 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -07001209 } else {
1210 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -07001211 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -07001212 }
Romain Guy707b2f72010-10-11 16:34:59 -07001213 mCaches.currentProgram->setColor(r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -07001214
Romain Guy06f96e22010-07-30 19:18:16 -07001215 // Setup attributes and uniforms required by the shaders
1216 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001217 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001218 }
Romain Guydb1938e2010-08-02 18:50:22 -07001219 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001220 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001221 }
Romain Guyd27977d2010-07-14 19:18:51 -07001222}
1223
Romain Guy82ba8142010-07-09 13:25:56 -07001224void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001225 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001226 int alpha;
1227 SkXfermode::Mode mode;
1228 getAlphaAndMode(paint, &alpha, &mode);
1229
Romain Guy8164c2d2010-10-25 18:03:28 -07001230 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1231
Romain Guy6820ac82010-09-15 18:11:50 -07001232 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy03750a02010-10-18 14:06:08 -07001233 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy6820ac82010-09-15 18:11:50 -07001234 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -07001235}
1236
Romain Guybd6b79b2010-06-26 00:13:53 -07001237void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001238 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1239 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001240 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001241}
1242
1243void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001244 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001245 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy03750a02010-10-18 14:06:08 -07001246 bool swapSrcDst, bool ignoreTransform, GLuint vbo) {
Romain Guy86942302010-09-12 13:02:16 -07001247 clearLayerRegions();
1248
Romain Guy889f8d12010-07-29 14:37:42 -07001249 ProgramDescription description;
1250 description.hasTexture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001251 const bool setColor = description.setColor(alpha, alpha, alpha, alpha);
Romain Guydb1938e2010-08-02 18:50:22 -07001252 if (mColorFilter) {
1253 mColorFilter->describe(description, mExtensions);
1254 }
Romain Guy889f8d12010-07-29 14:37:42 -07001255
Romain Guybd6b79b2010-06-26 00:13:53 -07001256 mModelView.loadTranslate(left, top, 0.0f);
1257 mModelView.scale(right - left, bottom - top, 1.0f);
1258
Romain Guyf607bdc2010-09-10 19:20:06 -07001259 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001260
Romain Guyfb8b7632010-08-23 21:05:08 -07001261 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001262 if (!ignoreTransform) {
1263 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1264 } else {
1265 mat4 m;
1266 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
1267 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001268
Romain Guy889f8d12010-07-29 14:37:42 -07001269 // Texture
Romain Guy8164c2d2010-10-25 18:03:28 -07001270 bindTexture(texture);
Romain Guyfb8b7632010-08-23 21:05:08 -07001271 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001272
Romain Guya9794742010-07-13 11:37:54 -07001273 // Always premultiplied
Romain Guy707b2f72010-10-11 16:34:59 -07001274 if (setColor) {
1275 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha);
1276 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001277
Romain Guy889f8d12010-07-29 14:37:42 -07001278 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001279 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001280 glEnableVertexAttribArray(texCoordsSlot);
Romain Guy03750a02010-10-18 14:06:08 -07001281
1282 if (!vertices) {
1283 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1284 } else {
1285 mCaches.unbindMeshBuffer();
1286 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001287 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001288 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001289 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001290
Romain Guydb1938e2010-08-02 18:50:22 -07001291 // Color filter
1292 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001293 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001294 }
1295
Romain Guy6820ac82010-09-15 18:11:50 -07001296 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001297 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001298}
1299
Romain Guya5aed0d2010-09-09 14:42:43 -07001300void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001301 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001302 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1303 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001304 if (mode < SkXfermode::kPlus_Mode) {
1305 if (!mCaches.blend) {
1306 glEnable(GL_BLEND);
1307 }
Romain Guy82ba8142010-07-09 13:25:56 -07001308
Romain Guyf607bdc2010-09-10 19:20:06 -07001309 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1310 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001311
Romain Guya5aed0d2010-09-09 14:42:43 -07001312 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1313 glBlendFunc(sourceMode, destMode);
1314 mCaches.lastSrcMode = sourceMode;
1315 mCaches.lastDstMode = destMode;
1316 }
1317 } else {
1318 // These blend modes are not supported by OpenGL directly and have
1319 // to be implemented using shaders. Since the shader will perform
1320 // the blending, turn blending off here
1321 if (mExtensions.hasFramebufferFetch()) {
1322 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001323 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001324 }
1325
1326 if (mCaches.blend) {
1327 glDisable(GL_BLEND);
1328 }
1329 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001330 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001331 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001332 glDisable(GL_BLEND);
1333 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001334 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001335}
1336
Romain Guy889f8d12010-07-29 14:37:42 -07001337bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001338 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001339 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001340 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001341 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001342 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001343 }
Romain Guy6926c722010-07-12 20:20:03 -07001344 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001345}
1346
Romain Guy026c5e162010-06-28 17:12:22 -07001347void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001348 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001349 TextureVertex::setUV(v++, u1, v1);
1350 TextureVertex::setUV(v++, u2, v1);
1351 TextureVertex::setUV(v++, u1, v2);
1352 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001353}
1354
Chet Haase5c13d892010-10-08 08:37:55 -07001355void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001356 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001357 if (!mExtensions.hasFramebufferFetch()) {
1358 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1359 if (!isMode) {
1360 // Assume SRC_OVER
1361 *mode = SkXfermode::kSrcOver_Mode;
1362 }
1363 } else {
1364 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001365 }
1366
1367 // Skia draws using the color's alpha channel if < 255
1368 // Otherwise, it uses the paint's alpha
1369 int color = paint->getColor();
1370 *alpha = (color >> 24) & 0xFF;
1371 if (*alpha == 255) {
1372 *alpha = paint->getAlpha();
1373 }
1374 } else {
1375 *mode = SkXfermode::kSrcOver_Mode;
1376 *alpha = 255;
1377 }
Romain Guy026c5e162010-06-28 17:12:22 -07001378}
1379
Romain Guya5aed0d2010-09-09 14:42:43 -07001380SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1381 if (mode == NULL) {
1382 return SkXfermode::kSrcOver_Mode;
1383 }
1384 return mode->fMode;
1385}
1386
Romain Guy8164c2d2010-10-25 18:03:28 -07001387void OpenGLRenderer::bindTexture(GLuint texture, GLuint textureUnit) {
Romain Guy889f8d12010-07-29 14:37:42 -07001388 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001389 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001390}
1391
1392void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT,
1393 GLuint textureUnit) {
1394 glActiveTexture(gTextureUnits[textureUnit]);
1395 bool bound = false;
1396 if (wrapS != texture->wrapS) {
1397 glBindTexture(GL_TEXTURE_2D, texture->id);
1398 bound = true;
1399 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1400 texture->wrapS = wrapS;
1401 }
1402 if (wrapT != texture->wrapT) {
1403 if (!bound) glBindTexture(GL_TEXTURE_2D, texture->id);
1404 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1405 texture->wrapT = wrapT;
1406 }
Romain Guya1db5742010-07-20 13:09:13 -07001407}
1408
Romain Guy9d5316e2010-06-24 19:30:36 -07001409}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001410}; // namespace android