blob: faad29746a82eb28ef7ead54fc1b25e98c3e099e [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 Guyeb993562010-10-05 18:14:38 -0700356 if (!fboLayer) {
357 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700358
Romain Guyeb993562010-10-05 18:14:38 -0700359 // Layers only make sense if they are in the framebuffer's bounds
Romain Guy58ae6db2010-10-22 11:16:37 -0700360 bounds.intersect(*snapshot->clipRect);
Romain Guyae517592010-10-22 10:40:27 -0700361
362 // When the layer is not an FBO, we may use glCopyTexImage so we
363 // need to make sure the layer does not extend outside the bounds
364 // of the framebuffer
365 bounds.intersect(snapshot->previous->viewport);
366
367 // We cannot work with sub-pixels in this case
Romain Guyeb993562010-10-05 18:14:38 -0700368 bounds.snapToPixelBoundaries();
369 }
Romain Guybf434112010-09-16 14:40:17 -0700370
Romain Guyb025b9c2010-09-16 14:16:48 -0700371 if (bounds.isEmpty() || bounds.getWidth() > mMaxTextureSize ||
372 bounds.getHeight() > mMaxTextureSize) {
Romain Guydbc26d22010-10-11 17:58:29 -0700373 snapshot->invisible = true;
374 } else {
375 // TODO: Should take the mode into account
Romain Guyd2a1ff02010-10-14 14:46:44 -0700376 snapshot->invisible = snapshot->previous->invisible ||
377 (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700378 }
379
380 // Bail out if we won't draw in this snapshot
381 if (snapshot->invisible) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700382 return false;
383 }
Romain Guyf18fd992010-07-08 11:45:51 -0700384
Romain Guy0bb56672010-10-01 00:25:02 -0700385 glActiveTexture(GL_TEXTURE0);
386
Romain Guy8550c4c2010-10-08 15:49:53 -0700387 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700388 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700389 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700390 }
391
Romain Guydda570202010-07-06 11:39:32 -0700392 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700393 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700394 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700395 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
396 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guydda570202010-07-06 11:39:32 -0700397
Romain Guy8fb95422010-08-17 18:38:51 -0700398 // Save the layer in the snapshot
399 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700400 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700401
Romain Guyeb993562010-10-05 18:14:38 -0700402 if (fboLayer) {
403 layer->fbo = mCaches.fboCache.get();
Romain Guy38c85b92010-09-22 22:48:20 -0700404
Romain Guyeb993562010-10-05 18:14:38 -0700405 snapshot->flags |= Snapshot::kFlagIsFboLayer;
406 snapshot->fbo = layer->fbo;
407 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
408 snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
409 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
410 snapshot->height = bounds.getHeight();
411 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
412 snapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf607bdc2010-09-10 19:20:06 -0700413
Romain Guyeb993562010-10-05 18:14:38 -0700414 // Bind texture to FBO
415 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
416 glBindTexture(GL_TEXTURE_2D, layer->texture);
417
418 // Initialize the texture if needed
419 if (layer->empty) {
420 layer->empty = false;
Romain Guy8550c4c2010-10-08 15:49:53 -0700421 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
Romain Guyeb993562010-10-05 18:14:38 -0700422 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
423 }
424
425 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
426 layer->texture, 0);
427
Romain Guye9108052010-10-12 18:15:42 -0700428#if DEBUG_LAYERS
Romain Guyeb993562010-10-05 18:14:38 -0700429 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
430 if (status != GL_FRAMEBUFFER_COMPLETE) {
431 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
432
433 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
434 glDeleteTextures(1, &layer->texture);
435 mCaches.fboCache.put(layer->fbo);
436
437 delete layer;
438
439 return false;
440 }
Romain Guye9108052010-10-12 18:15:42 -0700441#endif
Romain Guyeb993562010-10-05 18:14:38 -0700442
443 // Clear the FBO
Romain Guy93d23612010-10-13 18:26:36 -0700444 glScissor(0.0f, 0.0f, bounds.getWidth() + 1.0f, bounds.getHeight() + 1.0f);
Romain Guyeb993562010-10-05 18:14:38 -0700445 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
446 glClear(GL_COLOR_BUFFER_BIT);
Romain Guyeb07af62010-10-12 18:40:36 -0700447
448 setScissorFromClip();
Romain Guyeb993562010-10-05 18:14:38 -0700449
450 // Change the ortho projection
451 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
452 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
453 } else {
454 // Copy the framebuffer into the layer
455 glBindTexture(GL_TEXTURE_2D, layer->texture);
456
Romain Guyc00972b2010-10-12 11:31:07 -0700457 if (layer->empty) {
458 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
459 layer->width, layer->height, 0);
460 layer->empty = false;
461 } else {
462 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, mHeight - bounds.bottom,
463 bounds.getWidth(), bounds.getHeight());
464 }
Romain Guyeb993562010-10-05 18:14:38 -0700465
466 // Enqueue the buffer coordinates to clear the corresponding region later
467 mLayers.push(new Rect(bounds));
468 }
Romain Guyf86ef572010-07-01 11:05:42 -0700469
Romain Guyd55a8612010-06-28 17:42:46 -0700470 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700471}
472
Romain Guy1c740bc2010-09-13 18:00:09 -0700473/**
474 * Read the documentation of createLayer() before doing anything in this method.
475 */
Romain Guy1d83e192010-08-17 11:37:00 -0700476void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
477 if (!current->layer) {
478 LOGE("Attempting to compose a layer that does not exist");
479 return;
480 }
481
Romain Guyeb993562010-10-05 18:14:38 -0700482 const bool fboLayer = current->flags & SkCanvas::kClipToLayer_SaveFlag;
483
484 if (fboLayer) {
485 // Unbind current FBO and restore previous one
486 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
487 }
488
Romain Guy1d83e192010-08-17 11:37:00 -0700489 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700490 const Rect& clip = *previous->clipRect;
Romain Guyeb993562010-10-05 18:14:38 -0700491 glScissor(clip.left, previous->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700492
493 Layer* layer = current->layer;
494 const Rect& rect = layer->layer;
495
Romain Guyeb993562010-10-05 18:14:38 -0700496 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700497 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700498 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guyf607bdc2010-09-10 19:20:06 -0700499 }
500
Romain Guy8550c4c2010-10-08 15:49:53 -0700501 const Rect& texCoords = layer->texCoords;
Romain Guy03750a02010-10-18 14:06:08 -0700502 mCaches.unbindMeshBuffer();
Romain Guy8550c4c2010-10-08 15:49:53 -0700503 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
Romain Guy8b55f372010-08-18 17:10:07 -0700504
Romain Guyeb993562010-10-05 18:14:38 -0700505 if (fboLayer) {
Romain Guy03750a02010-10-18 14:06:08 -0700506 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
507 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
508 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyeb993562010-10-05 18:14:38 -0700509 } else {
510 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
511 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
512 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, true, true);
513 }
Romain Guy1d83e192010-08-17 11:37:00 -0700514
Romain Guy8b55f372010-08-18 17:10:07 -0700515 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
516
Romain Guyeb993562010-10-05 18:14:38 -0700517 if (fboLayer) {
518 // Detach the texture from the FBO
519 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
520 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
521 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
522
523 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
524 mCaches.fboCache.put(current->fbo);
525 }
526
Romain Guyeb993562010-10-05 18:14:38 -0700527 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700528 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700529 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700530 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700531 delete layer;
532 }
533}
534
Romain Guy86942302010-09-12 13:02:16 -0700535void OpenGLRenderer::clearLayerRegions() {
Romain Guydbc26d22010-10-11 17:58:29 -0700536 if (mLayers.size() == 0 || mSnapshot->invisible) return;
Romain Guy86942302010-09-12 13:02:16 -0700537
538 for (uint32_t i = 0; i < mLayers.size(); i++) {
539 Rect* bounds = mLayers.itemAt(i);
540
541 // Clear the framebuffer where the layer will draw
Romain Guyd2a1ff02010-10-14 14:46:44 -0700542 glScissor(bounds->left, mSnapshot->height - bounds->bottom,
Romain Guy86942302010-09-12 13:02:16 -0700543 bounds->getWidth(), bounds->getHeight());
544 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
545 glClear(GL_COLOR_BUFFER_BIT);
546
547 delete bounds;
548 }
549 mLayers.clear();
550
551 // Restore the clip
552 setScissorFromClip();
553}
554
Romain Guybd6b79b2010-06-26 00:13:53 -0700555///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700556// Transforms
557///////////////////////////////////////////////////////////////////////////////
558
559void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700560 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700561}
562
563void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700564 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700565}
566
567void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700568 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700569}
570
571void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700572 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700573}
574
Romain Guy41030da2010-10-13 13:40:37 -0700575const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700576 if (mSnapshot->fbo != 0) {
577 return &mSnapshot->transform->data[0];
578 }
579 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700580}
581
Romain Guyf6a11b82010-06-23 17:47:49 -0700582void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700583 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700584}
585
586void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700587 SkMatrix transform;
588 mSnapshot->transform->copyTo(transform);
589 transform.preConcat(*matrix);
590 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700591}
592
593///////////////////////////////////////////////////////////////////////////////
594// Clipping
595///////////////////////////////////////////////////////////////////////////////
596
Romain Guybb9524b2010-06-22 18:56:38 -0700597void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700598 Rect clip(*mSnapshot->clipRect);
599 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700600 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700601}
602
603const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700604 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700605}
606
Romain Guyc7d53492010-06-25 13:41:57 -0700607bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guydbc26d22010-10-11 17:58:29 -0700608 if (mSnapshot->invisible) {
609 return true;
610 }
611
Romain Guy1d83e192010-08-17 11:37:00 -0700612 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700613 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700614 r.snapToPixelBoundaries();
615
616 Rect clipRect(*mSnapshot->clipRect);
617 clipRect.snapToPixelBoundaries();
618
619 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700620}
621
Romain Guy079ba2c2010-07-16 14:12:24 -0700622bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
623 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700624 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700625 setScissorFromClip();
626 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700627 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700628}
629
Romain Guyf6a11b82010-06-23 17:47:49 -0700630///////////////////////////////////////////////////////////////////////////////
631// Drawing
632///////////////////////////////////////////////////////////////////////////////
633
Chet Haase5c13d892010-10-08 08:37:55 -0700634void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700635 const float right = left + bitmap->width();
636 const float bottom = top + bitmap->height();
637
638 if (quickReject(left, top, right, bottom)) {
639 return;
640 }
641
Romain Guy61c8c9c2010-08-09 20:48:09 -0700642 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700643 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700644 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700645 const AutoTexture autoCleanup(texture);
646
Romain Guy6926c722010-07-12 20:20:03 -0700647 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700648}
649
Chet Haase5c13d892010-10-08 08:37:55 -0700650void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -0700651 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
652 const mat4 transform(*matrix);
653 transform.mapRect(r);
654
Romain Guy6926c722010-07-12 20:20:03 -0700655 if (quickReject(r.left, r.top, r.right, r.bottom)) {
656 return;
657 }
658
Romain Guy61c8c9c2010-08-09 20:48:09 -0700659 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700660 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700661 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700662 const AutoTexture autoCleanup(texture);
663
Romain Guy82ba8142010-07-09 13:25:56 -0700664 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700665}
666
Romain Guy8ba548f2010-06-30 19:21:21 -0700667void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
668 float srcLeft, float srcTop, float srcRight, float srcBottom,
669 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700670 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700671 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
672 return;
673 }
674
Romain Guy61c8c9c2010-08-09 20:48:09 -0700675 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700676 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700677 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700678 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700679
Romain Guy8ba548f2010-06-30 19:21:21 -0700680 const float width = texture->width;
681 const float height = texture->height;
682
683 const float u1 = srcLeft / width;
684 const float v1 = srcTop / height;
685 const float u2 = srcRight / width;
686 const float v2 = srcBottom / height;
687
Romain Guy03750a02010-10-18 14:06:08 -0700688 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -0700689 resetDrawTextureTexCoords(u1, v1, u2, v2);
690
Romain Guy03750a02010-10-18 14:06:08 -0700691 int alpha;
692 SkXfermode::Mode mode;
693 getAlphaAndMode(paint, &alpha, &mode);
694
695 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
696 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
697 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy8ba548f2010-06-30 19:21:21 -0700698
699 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
700}
701
Romain Guy4aa90572010-09-26 18:40:37 -0700702void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -0700703 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -0700704 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700705 if (quickReject(left, top, right, bottom)) {
706 return;
707 }
708
Romain Guy61c8c9c2010-08-09 20:48:09 -0700709 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700710 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700711 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700712 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700713
714 int alpha;
715 SkXfermode::Mode mode;
716 getAlphaAndMode(paint, &alpha, &mode);
717
Romain Guy2728f962010-10-08 18:36:15 -0700718 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -0700719 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -0700720
Romain Guy054dc182010-10-15 17:55:25 -0700721 if (mesh) {
722 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
723 // patch mesh already defines the final size
724 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
Romain Guy03750a02010-10-18 14:06:08 -0700725 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
726 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer);
Romain Guy054dc182010-10-15 17:55:25 -0700727 }
Romain Guyf7f93552010-07-08 19:17:03 -0700728}
729
Chet Haase5c13d892010-10-08 08:37:55 -0700730void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -0700731 if (mSnapshot->invisible) return;
732
Romain Guy759ea802010-09-16 20:49:46 -0700733 int alpha;
734 SkXfermode::Mode mode;
735 getAlphaAndMode(paint, &alpha, &mode);
736
737 uint32_t color = paint->getColor();
738 const GLfloat a = alpha / 255.0f;
739 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
740 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
741 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
742
Romain Guyc95c8d62010-09-17 15:31:32 -0700743 const bool isAA = paint->isAntiAlias();
744 if (isAA) {
745 GLuint textureUnit = 0;
Romain Guy29d89972010-09-22 16:10:57 -0700746 setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
Romain Guy03750a02010-10-18 14:06:08 -0700747 mode, false, true, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
748 mCaches.line.getMeshBuffer());
Romain Guyc95c8d62010-09-17 15:31:32 -0700749 } else {
750 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false);
751 }
752
753 const float strokeWidth = paint->getStrokeWidth();
Romain Guy29d89972010-09-22 16:10:57 -0700754 const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount;
Romain Guyc95c8d62010-09-17 15:31:32 -0700755 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
Romain Guy759ea802010-09-16 20:49:46 -0700756
757 for (int i = 0; i < count; i += 4) {
758 float tx = 0.0f;
759 float ty = 0.0f;
760
Romain Guyc95c8d62010-09-17 15:31:32 -0700761 if (isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700762 mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3],
Romain Guyc95c8d62010-09-17 15:31:32 -0700763 strokeWidth, tx, ty);
764 } else {
Romain Guyb5ab4172010-09-17 15:36:56 -0700765 ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f;
Romain Guyc95c8d62010-09-17 15:31:32 -0700766 }
Romain Guy759ea802010-09-16 20:49:46 -0700767
768 const float dx = points[i + 2] - points[i];
769 const float dy = points[i + 3] - points[i + 1];
770 const float mag = sqrtf(dx * dx + dy * dy);
771 const float angle = acos(dx / mag);
772
773 mModelView.loadTranslate(points[i], points[i + 1], 0.0f);
774 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) {
775 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f);
776 }
777 mModelView.translate(tx, ty, 0.0f);
Romain Guyc95c8d62010-09-17 15:31:32 -0700778 if (!isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700779 float length = mCaches.line.getLength(points[i], points[i + 1],
780 points[i + 2], points[i + 3]);
Romain Guyc95c8d62010-09-17 15:31:32 -0700781 mModelView.scale(length, strokeWidth, 1.0f);
782 }
Romain Guy759ea802010-09-16 20:49:46 -0700783 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
784
785 if (mShader) {
786 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot);
787 }
788
Romain Guyc95c8d62010-09-17 15:31:32 -0700789 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy759ea802010-09-16 20:49:46 -0700790 }
791
Romain Guy29d89972010-09-22 16:10:57 -0700792 if (isAA) {
793 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
794 }
Romain Guy759ea802010-09-16 20:49:46 -0700795}
796
Romain Guy85bf02f2010-06-22 13:11:24 -0700797void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700798 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700799 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700800}
Romain Guy9d5316e2010-06-24 19:30:36 -0700801
Chet Haase5c13d892010-10-08 08:37:55 -0700802void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700803 if (quickReject(left, top, right, bottom)) {
804 return;
805 }
806
Romain Guy026c5e162010-06-28 17:12:22 -0700807 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700808 if (!mExtensions.hasFramebufferFetch()) {
809 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
810 if (!isMode) {
811 // Assume SRC_OVER
812 mode = SkXfermode::kSrcOver_Mode;
813 }
814 } else {
815 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700816 }
817
818 // Skia draws using the color's alpha channel if < 255
819 // Otherwise, it uses the paint's alpha
820 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700821 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700822 color |= p->getAlpha() << 24;
823 }
824
825 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700826}
Romain Guy9d5316e2010-06-24 19:30:36 -0700827
Romain Guye8e62a42010-07-23 18:55:21 -0700828void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
829 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700830 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700831 return;
832 }
Romain Guydbc26d22010-10-11 17:58:29 -0700833 if (mSnapshot->invisible) return;
Romain Guye2d345e2010-09-24 18:39:22 -0700834
Romain Guyf607bdc2010-09-10 19:20:06 -0700835 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700836
Romain Guya674ab72010-08-10 17:26:42 -0700837 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700838 switch (paint->getTextAlign()) {
839 case SkPaint::kCenter_Align:
840 length = paint->measureText(text, bytesCount);
841 x -= length / 2.0f;
842 break;
843 case SkPaint::kRight_Align:
844 length = paint->measureText(text, bytesCount);
845 x -= length;
846 break;
847 default:
848 break;
849 }
850
Romain Guy694b5192010-07-21 21:33:20 -0700851 int alpha;
852 SkXfermode::Mode mode;
853 getAlphaAndMode(paint, &alpha, &mode);
854
Romain Guy2542d192010-08-18 11:47:12 -0700855 uint32_t color = paint->getColor();
856 const GLfloat a = alpha / 255.0f;
857 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
858 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
859 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
860
Romain Guyb45c0c92010-08-26 20:35:23 -0700861 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
862 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700863 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -0700864
Romain Guy9d13fe252010-10-15 16:06:03 -0700865 Rect clipRect(*mSnapshot->clipRect);
866 glScissor(clipRect.left, mSnapshot->height - clipRect.bottom,
867 clipRect.getWidth(), clipRect.getHeight());
868
Romain Guy1e45aae2010-08-13 19:39:53 -0700869 if (mHasShadow) {
870 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700871 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700872 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700873 count, mShadowRadius);
874 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700875
Romain Guy2542d192010-08-18 11:47:12 -0700876 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700877
878 // Draw the mesh
879 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700880 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700881 }
882
Romain Guy06f96e22010-07-30 19:18:16 -0700883 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700884 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700885
Romain Guye8cb9c142010-10-04 14:14:11 -0700886 // Assume that the modelView matrix does not force scales, rotates, etc.
887 const bool linearFilter = mSnapshot->transform->changesBounds();
888 setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit,
Romain Guy03750a02010-10-18 14:06:08 -0700889 x, y, r, g, b, a, mode, false, true, NULL, NULL);
Romain Guy06f96e22010-07-30 19:18:16 -0700890
Romain Guy09147fb2010-07-22 13:08:20 -0700891 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy86942302010-09-12 13:02:16 -0700892 clearLayerRegions();
Romain Guy9d13fe252010-10-15 16:06:03 -0700893
Romain Guy03750a02010-10-18 14:06:08 -0700894 mCaches.unbindMeshBuffer();
Romain Guyb45c0c92010-08-26 20:35:23 -0700895 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700896
897 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700898 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700899
Romain Guy0a417492010-08-16 20:26:20 -0700900 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy9d13fe252010-10-15 16:06:03 -0700901
902 setScissorFromClip();
Romain Guy694b5192010-07-21 21:33:20 -0700903}
904
Romain Guy7fbcc042010-08-04 15:40:07 -0700905void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -0700906 if (mSnapshot->invisible) return;
907
Romain Guy7fbcc042010-08-04 15:40:07 -0700908 GLuint textureUnit = 0;
909 glActiveTexture(gTextureUnits[textureUnit]);
910
Romain Guyfb8b7632010-08-23 21:05:08 -0700911 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700912 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700913 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700914
Romain Guy8b55f372010-08-18 17:10:07 -0700915 const float x = texture->left - texture->offset;
916 const float y = texture->top - texture->offset;
917
918 if (quickReject(x, y, x + texture->width, y + texture->height)) {
919 return;
920 }
921
Romain Guy7fbcc042010-08-04 15:40:07 -0700922 int alpha;
923 SkXfermode::Mode mode;
924 getAlphaAndMode(paint, &alpha, &mode);
925
926 uint32_t color = paint->getColor();
927 const GLfloat a = alpha / 255.0f;
928 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
929 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
930 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
931
Romain Guy0a417492010-08-16 20:26:20 -0700932 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
933
Romain Guy86942302010-09-12 13:02:16 -0700934 clearLayerRegions();
935
Romain Guy0a417492010-08-16 20:26:20 -0700936 // Draw the mesh
937 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700938 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700939}
940
Romain Guy6926c722010-07-12 20:20:03 -0700941///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700942// Shaders
943///////////////////////////////////////////////////////////////////////////////
944
945void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700946 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700947}
948
Romain Guy06f96e22010-07-30 19:18:16 -0700949void OpenGLRenderer::setupShader(SkiaShader* shader) {
950 mShader = shader;
951 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700952 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700953 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700954}
955
Romain Guyd27977d2010-07-14 19:18:51 -0700956///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700957// Color filters
958///////////////////////////////////////////////////////////////////////////////
959
960void OpenGLRenderer::resetColorFilter() {
961 mColorFilter = NULL;
962}
963
964void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
965 mColorFilter = filter;
966}
967
968///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700969// Drop shadow
970///////////////////////////////////////////////////////////////////////////////
971
972void OpenGLRenderer::resetShadow() {
973 mHasShadow = false;
974}
975
976void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
977 mHasShadow = true;
978 mShadowRadius = radius;
979 mShadowDx = dx;
980 mShadowDy = dy;
981 mShadowColor = color;
982}
983
984///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700985// Drawing implementation
986///////////////////////////////////////////////////////////////////////////////
987
Romain Guy0a417492010-08-16 20:26:20 -0700988void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700989 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700990 const float sx = x - texture->left + mShadowDx;
991 const float sy = y - texture->top + mShadowDy;
992
Romain Guy2542d192010-08-18 11:47:12 -0700993 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
994 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700995 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
996 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
997 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
998
999 GLuint textureUnit = 0;
1000 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
1001}
1002
1003void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
1004 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
1005 bool transforms, bool applyFilters) {
1006 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -07001007 x, y, r, g, b, a, mode, transforms, applyFilters,
Romain Guy03750a02010-10-18 14:06:08 -07001008 (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy0a417492010-08-16 20:26:20 -07001009}
1010
1011void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1012 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1013 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy03750a02010-10-18 14:06:08 -07001014 setupTextureAlpha8(texture, width, height, textureUnit, x, y, r, g, b, a, mode,
1015 transforms, applyFilters, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy759ea802010-09-16 20:49:46 -07001016}
1017
1018void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1019 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1020 SkXfermode::Mode mode, bool transforms, bool applyFilters,
Romain Guy03750a02010-10-18 14:06:08 -07001021 GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guy0a417492010-08-16 20:26:20 -07001022 // Describe the required shaders
1023 ProgramDescription description;
1024 description.hasTexture = true;
1025 description.hasAlpha8Texture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001026 const bool setColor = description.setAlpha8Color(r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -07001027
1028 if (applyFilters) {
1029 if (mShader) {
1030 mShader->describe(description, mExtensions);
1031 }
1032 if (mColorFilter) {
1033 mColorFilter->describe(description, mExtensions);
1034 }
1035 }
1036
Romain Guya5aed0d2010-09-09 14:42:43 -07001037 // Setup the blending mode
1038 chooseBlending(true, mode, description);
1039
Romain Guy0a417492010-08-16 20:26:20 -07001040 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001041 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -07001042
Romain Guy0a417492010-08-16 20:26:20 -07001043 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -07001044 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001045
Romain Guyfb8b7632010-08-23 21:05:08 -07001046 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -07001047 glEnableVertexAttribArray(texCoordsSlot);
1048
Romain Guy03750a02010-10-18 14:06:08 -07001049 if (texCoords) {
1050 // Setup attributes
1051 if (!vertices) {
1052 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1053 } else {
1054 mCaches.unbindMeshBuffer();
1055 }
1056 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1057 gMeshStride, vertices);
1058 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1059 }
Romain Guy0a417492010-08-16 20:26:20 -07001060
1061 // Setup uniforms
1062 if (transforms) {
1063 mModelView.loadTranslate(x, y, 0.0f);
1064 mModelView.scale(width, height, 1.0f);
1065 } else {
1066 mModelView.loadIdentity();
1067 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001068 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy707b2f72010-10-11 16:34:59 -07001069 if (setColor) {
1070 mCaches.currentProgram->setColor(r, g, b, a);
1071 }
Romain Guy0a417492010-08-16 20:26:20 -07001072
1073 textureUnit++;
1074 if (applyFilters) {
1075 // Setup attributes and uniforms required by the shaders
1076 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001077 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001078 }
1079 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001080 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -07001081 }
1082 }
1083}
1084
Romain Guyf607bdc2010-09-10 19:20:06 -07001085// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001086#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1087#define kStdUnderline_Offset (1.0f / 9.0f)
1088#define kStdUnderline_Thickness (1.0f / 18.0f)
1089
1090void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1091 float x, float y, SkPaint* paint) {
1092 // Handle underline and strike-through
1093 uint32_t flags = paint->getFlags();
1094 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1095 float underlineWidth = length;
1096 // If length is > 0.0f, we already measured the text for the text alignment
1097 if (length <= 0.0f) {
1098 underlineWidth = paint->measureText(text, bytesCount);
1099 }
1100
1101 float offsetX = 0;
1102 switch (paint->getTextAlign()) {
1103 case SkPaint::kCenter_Align:
1104 offsetX = underlineWidth * 0.5f;
1105 break;
1106 case SkPaint::kRight_Align:
1107 offsetX = underlineWidth;
1108 break;
1109 default:
1110 break;
1111 }
1112
1113 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001114 const float textSize = paint->getTextSize();
1115 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -07001116
Romain Guye20ecbd2010-09-22 19:49:04 -07001117 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001118 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001119
1120 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
1121 float points[pointsCount];
1122 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001123
1124 if (flags & SkPaint::kUnderlineText_Flag) {
1125 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001126 points[currentPoint++] = left;
1127 points[currentPoint++] = top;
1128 points[currentPoint++] = left + underlineWidth;
1129 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001130 }
1131
1132 if (flags & SkPaint::kStrikeThruText_Flag) {
1133 top = y + textSize * kStdStrikeThru_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 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001139
1140 SkPaint linesPaint(*paint);
1141 linesPaint.setStrokeWidth(strokeWidth);
1142
1143 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001144 }
1145 }
1146}
1147
Romain Guy026c5e162010-06-28 17:12:22 -07001148void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001149 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001150 clearLayerRegions();
1151
Romain Guyd27977d2010-07-14 19:18:51 -07001152 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001153 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001154 color |= 0x00ffffff;
1155 }
1156
1157 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -07001158 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -07001159 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -07001160 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1161 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1162 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1163
Romain Guyc95c8d62010-09-17 15:31:32 -07001164 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform);
1165
1166 // Draw the mesh
1167 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1168}
1169
1170void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom,
1171 float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy06f96e22010-07-30 19:18:16 -07001172 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -07001173
Romain Guy06f96e22010-07-30 19:18:16 -07001174 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -07001175 ProgramDescription description;
Romain Guy707b2f72010-10-11 16:34:59 -07001176 const bool setColor = description.setColor(r, g, b, a);
1177
Romain Guy06f96e22010-07-30 19:18:16 -07001178 if (mShader) {
1179 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -07001180 }
Romain Guydb1938e2010-08-02 18:50:22 -07001181 if (mColorFilter) {
1182 mColorFilter->describe(description, mExtensions);
1183 }
Romain Guyd27977d2010-07-14 19:18:51 -07001184
Romain Guy1c740bc2010-09-13 18:00:09 -07001185 // Setup the blending mode
Romain Guyc95c8d62010-09-17 15:31:32 -07001186 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -07001187
Romain Guy06f96e22010-07-30 19:18:16 -07001188 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001189 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -07001190
1191 // Setup attributes
Romain Guy03750a02010-10-18 14:06:08 -07001192 mCaches.bindMeshBuffer();
Romain Guyfb8b7632010-08-23 21:05:08 -07001193 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy03750a02010-10-18 14:06:08 -07001194 gMeshStride, 0);
Romain Guy06f96e22010-07-30 19:18:16 -07001195
1196 // Setup uniforms
1197 mModelView.loadTranslate(left, top, 0.0f);
1198 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -07001199 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001200 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -07001201 } else {
1202 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -07001203 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -07001204 }
Romain Guy707b2f72010-10-11 16:34:59 -07001205 mCaches.currentProgram->setColor(r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -07001206
Romain Guy06f96e22010-07-30 19:18:16 -07001207 // Setup attributes and uniforms required by the shaders
1208 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001209 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001210 }
Romain Guydb1938e2010-08-02 18:50:22 -07001211 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001212 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001213 }
Romain Guyd27977d2010-07-14 19:18:51 -07001214}
1215
Romain Guy82ba8142010-07-09 13:25:56 -07001216void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001217 const Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001218 int alpha;
1219 SkXfermode::Mode mode;
1220 getAlphaAndMode(paint, &alpha, &mode);
1221
Romain Guy6820ac82010-09-15 18:11:50 -07001222 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy03750a02010-10-18 14:06:08 -07001223 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy6820ac82010-09-15 18:11:50 -07001224 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -07001225}
1226
Romain Guybd6b79b2010-06-26 00:13:53 -07001227void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001228 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1229 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001230 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001231}
1232
1233void OpenGLRenderer::drawTextureMesh(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,
Romain Guy6820ac82010-09-15 18:11:50 -07001235 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy03750a02010-10-18 14:06:08 -07001236 bool swapSrcDst, bool ignoreTransform, GLuint vbo) {
Romain Guy86942302010-09-12 13:02:16 -07001237 clearLayerRegions();
1238
Romain Guy889f8d12010-07-29 14:37:42 -07001239 ProgramDescription description;
1240 description.hasTexture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001241 const bool setColor = description.setColor(alpha, alpha, alpha, alpha);
Romain Guydb1938e2010-08-02 18:50:22 -07001242 if (mColorFilter) {
1243 mColorFilter->describe(description, mExtensions);
1244 }
Romain Guy889f8d12010-07-29 14:37:42 -07001245
Romain Guybd6b79b2010-06-26 00:13:53 -07001246 mModelView.loadTranslate(left, top, 0.0f);
1247 mModelView.scale(right - left, bottom - top, 1.0f);
1248
Romain Guyf607bdc2010-09-10 19:20:06 -07001249 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001250
Romain Guyfb8b7632010-08-23 21:05:08 -07001251 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001252 if (!ignoreTransform) {
1253 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1254 } else {
1255 mat4 m;
1256 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
1257 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001258
Romain Guy889f8d12010-07-29 14:37:42 -07001259 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -07001260 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001261 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001262
Romain Guya9794742010-07-13 11:37:54 -07001263 // Always premultiplied
Romain Guy707b2f72010-10-11 16:34:59 -07001264 if (setColor) {
1265 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha);
1266 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001267
Romain Guy889f8d12010-07-29 14:37:42 -07001268 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001269 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001270 glEnableVertexAttribArray(texCoordsSlot);
Romain Guy03750a02010-10-18 14:06:08 -07001271
1272 if (!vertices) {
1273 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1274 } else {
1275 mCaches.unbindMeshBuffer();
1276 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001277 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001278 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001279 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001280
Romain Guydb1938e2010-08-02 18:50:22 -07001281 // Color filter
1282 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001283 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001284 }
1285
Romain Guy6820ac82010-09-15 18:11:50 -07001286 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001287 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001288}
1289
Romain Guya5aed0d2010-09-09 14:42:43 -07001290void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001291 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001292 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1293 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001294 if (mode < SkXfermode::kPlus_Mode) {
1295 if (!mCaches.blend) {
1296 glEnable(GL_BLEND);
1297 }
Romain Guy82ba8142010-07-09 13:25:56 -07001298
Romain Guyf607bdc2010-09-10 19:20:06 -07001299 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1300 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001301
Romain Guya5aed0d2010-09-09 14:42:43 -07001302 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1303 glBlendFunc(sourceMode, destMode);
1304 mCaches.lastSrcMode = sourceMode;
1305 mCaches.lastDstMode = destMode;
1306 }
1307 } else {
1308 // These blend modes are not supported by OpenGL directly and have
1309 // to be implemented using shaders. Since the shader will perform
1310 // the blending, turn blending off here
1311 if (mExtensions.hasFramebufferFetch()) {
1312 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001313 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001314 }
1315
1316 if (mCaches.blend) {
1317 glDisable(GL_BLEND);
1318 }
1319 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001320 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001321 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001322 glDisable(GL_BLEND);
1323 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001324 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001325}
1326
Romain Guy889f8d12010-07-29 14:37:42 -07001327bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001328 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001329 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001330 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001331 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001332 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001333 }
Romain Guy6926c722010-07-12 20:20:03 -07001334 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001335}
1336
Romain Guy026c5e162010-06-28 17:12:22 -07001337void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001338 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001339 TextureVertex::setUV(v++, u1, v1);
1340 TextureVertex::setUV(v++, u2, v1);
1341 TextureVertex::setUV(v++, u1, v2);
1342 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001343}
1344
Chet Haase5c13d892010-10-08 08:37:55 -07001345void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001346 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001347 if (!mExtensions.hasFramebufferFetch()) {
1348 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1349 if (!isMode) {
1350 // Assume SRC_OVER
1351 *mode = SkXfermode::kSrcOver_Mode;
1352 }
1353 } else {
1354 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001355 }
1356
1357 // Skia draws using the color's alpha channel if < 255
1358 // Otherwise, it uses the paint's alpha
1359 int color = paint->getColor();
1360 *alpha = (color >> 24) & 0xFF;
1361 if (*alpha == 255) {
1362 *alpha = paint->getAlpha();
1363 }
1364 } else {
1365 *mode = SkXfermode::kSrcOver_Mode;
1366 *alpha = 255;
1367 }
Romain Guy026c5e162010-06-28 17:12:22 -07001368}
1369
Romain Guya5aed0d2010-09-09 14:42:43 -07001370SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1371 if (mode == NULL) {
1372 return SkXfermode::kSrcOver_Mode;
1373 }
1374 return mode->fMode;
1375}
1376
Romain Guy889f8d12010-07-29 14:37:42 -07001377void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1378 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001379 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001380 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1381 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1382}
1383
Romain Guy9d5316e2010-06-24 19:30:36 -07001384}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001385}; // namespace android