blob: 5ab5f065529238b2b4185965c2fe17b8ad96dbec [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 }
171}
172
173void OpenGLRenderer::releaseContext() {
Romain Guyeb993562010-10-05 18:14:38 -0700174 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700175
176 glEnable(GL_SCISSOR_TEST);
177 setScissorFromClip();
178
Romain Guyf607bdc2010-09-10 19:20:06 -0700179 glDisable(GL_DITHER);
180
181 glBindFramebuffer(GL_FRAMEBUFFER, 0);
Romain Guy03750a02010-10-18 14:06:08 -0700182 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
183 mCaches.bindMeshBuffer();
Romain Guyf607bdc2010-09-10 19:20:06 -0700184
Romain Guyda8532c2010-08-31 11:50:35 -0700185 if (mCaches.blend) {
186 glEnable(GL_BLEND);
187 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
Romain Guyf607bdc2010-09-10 19:20:06 -0700188 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700189 } else {
190 glDisable(GL_BLEND);
191 }
192}
193
Romain Guyf6a11b82010-06-23 17:47:49 -0700194///////////////////////////////////////////////////////////////////////////////
195// State management
196///////////////////////////////////////////////////////////////////////////////
197
Romain Guybb9524b2010-06-22 18:56:38 -0700198int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700199 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700200}
201
202int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700203 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700204}
205
206void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700207 if (mSaveCount > 1) {
208 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700209 }
Romain Guybb9524b2010-06-22 18:56:38 -0700210}
211
212void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700213 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700214
Romain Guy8fb95422010-08-17 18:38:51 -0700215 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700216 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700217 }
Romain Guybb9524b2010-06-22 18:56:38 -0700218}
219
Romain Guy8aef54f2010-09-01 15:13:49 -0700220int OpenGLRenderer::saveSnapshot(int flags) {
221 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700222 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700223}
224
225bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700226 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700227 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700228 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700229
Romain Guybd6b79b2010-06-26 00:13:53 -0700230 sp<Snapshot> current = mSnapshot;
231 sp<Snapshot> previous = mSnapshot->previous;
232
Romain Guyeb993562010-10-05 18:14:38 -0700233 if (restoreOrtho) {
234 Rect& r = previous->viewport;
235 glViewport(r.left, r.top, r.right, r.bottom);
236 mOrthoMatrix.load(current->orthoMatrix);
237 }
238
Romain Guy8b55f372010-08-18 17:10:07 -0700239 mSaveCount--;
240 mSnapshot = previous;
241
Romain Guybd6b79b2010-06-26 00:13:53 -0700242 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700243 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700244 }
245
Romain Guy2542d192010-08-18 11:47:12 -0700246 if (restoreClip) {
247 setScissorFromClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700248 }
Romain Guy2542d192010-08-18 11:47:12 -0700249
250 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700251}
252
Romain Guyf6a11b82010-06-23 17:47:49 -0700253///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700254// Layers
255///////////////////////////////////////////////////////////////////////////////
256
257int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
258 const SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700259 const GLuint previousFbo = mSnapshot->fbo;
260 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700261
262 int alpha = 255;
263 SkXfermode::Mode mode;
264
265 if (p) {
266 alpha = p->getAlpha();
Romain Guya5aed0d2010-09-09 14:42:43 -0700267 if (!mExtensions.hasFramebufferFetch()) {
268 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
269 if (!isMode) {
270 // Assume SRC_OVER
271 mode = SkXfermode::kSrcOver_Mode;
272 }
273 } else {
274 mode = getXfermode(p->getXfermode());
Romain Guyd55a8612010-06-28 17:42:46 -0700275 }
276 } else {
277 mode = SkXfermode::kSrcOver_Mode;
278 }
279
Romain Guydbc26d22010-10-11 17:58:29 -0700280 if (!mSnapshot->previous->invisible) {
281 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
282 }
Romain Guyd55a8612010-06-28 17:42:46 -0700283
284 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700285}
286
287int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
288 int alpha, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700289 if (alpha == 0xff) {
290 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700291 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700292 SkPaint paint;
293 paint.setAlpha(alpha);
294 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700295 }
Romain Guyd55a8612010-06-28 17:42:46 -0700296}
Romain Guybd6b79b2010-06-26 00:13:53 -0700297
Romain Guy1c740bc2010-09-13 18:00:09 -0700298/**
299 * Layers are viewed by Skia are slightly different than layers in image editing
300 * programs (for instance.) When a layer is created, previously created layers
301 * and the frame buffer still receive every drawing command. For instance, if a
302 * layer is created and a shape intersecting the bounds of the layers and the
303 * framebuffer is draw, the shape will be drawn on both (unless the layer was
304 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
305 *
306 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
307 * texture. Unfortunately, this is inefficient as it requires every primitive to
308 * be drawn n + 1 times, where n is the number of active layers. In practice this
309 * means, for every primitive:
310 * - Switch active frame buffer
311 * - Change viewport, clip and projection matrix
312 * - Issue the drawing
313 *
314 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700315 * To avoid this, layers are implemented in a different way here, at least in the
316 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
317 * is set. When this flag is set we can redirect all drawing operations into a
318 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700319 *
320 * This implementation relies on the frame buffer being at least RGBA 8888. When
321 * a layer is created, only a texture is created, not an FBO. The content of the
322 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700323 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700324 * buffer and drawing continues as normal. This technique therefore treats the
325 * frame buffer as a scratch buffer for the layers.
326 *
327 * To compose the layers back onto the frame buffer, each layer texture
328 * (containing the original frame buffer data) is drawn as a simple quad over
329 * the frame buffer. The trick is that the quad is set as the composition
330 * destination in the blending equation, and the frame buffer becomes the source
331 * of the composition.
332 *
333 * Drawing layers with an alpha value requires an extra step before composition.
334 * An empty quad is drawn over the layer's region in the frame buffer. This quad
335 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
336 * quad is used to multiply the colors in the frame buffer. This is achieved by
337 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
338 * GL_ZERO, GL_SRC_ALPHA.
339 *
340 * Because glCopyTexImage2D() can be slow, an alternative implementation might
341 * be use to draw a single clipped layer. The implementation described above
342 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700343 *
344 * (1) The frame buffer is actually not cleared right away. To allow the GPU
345 * to potentially optimize series of calls to glCopyTexImage2D, the frame
346 * buffer is left untouched until the first drawing operation. Only when
347 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700348 */
Romain Guyd55a8612010-06-28 17:42:46 -0700349bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700350 float right, float bottom, int alpha, SkXfermode::Mode mode,
351 int flags, GLuint previousFbo) {
352 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700353 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700354
Romain Guyeb993562010-10-05 18:14:38 -0700355 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
356
Romain Guyf607bdc2010-09-10 19:20:06 -0700357 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700358 Rect bounds(left, top, right, bottom);
Romain Guyeb993562010-10-05 18:14:38 -0700359 if (!fboLayer) {
360 mSnapshot->transform->mapRect(bounds);
361 // Layers only make sense if they are in the framebuffer's bounds
362 bounds.intersect(*mSnapshot->clipRect);
363 bounds.snapToPixelBoundaries();
364 }
Romain Guybf434112010-09-16 14:40:17 -0700365
Romain Guyb025b9c2010-09-16 14:16:48 -0700366 if (bounds.isEmpty() || bounds.getWidth() > mMaxTextureSize ||
367 bounds.getHeight() > mMaxTextureSize) {
Romain Guydbc26d22010-10-11 17:58:29 -0700368 snapshot->invisible = true;
369 } else {
370 // TODO: Should take the mode into account
Romain Guyd2a1ff02010-10-14 14:46:44 -0700371 snapshot->invisible = snapshot->previous->invisible ||
372 (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700373 }
374
375 // Bail out if we won't draw in this snapshot
376 if (snapshot->invisible) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700377 return false;
378 }
Romain Guyf18fd992010-07-08 11:45:51 -0700379
Romain Guy0bb56672010-10-01 00:25:02 -0700380 glActiveTexture(GL_TEXTURE0);
381
Romain Guy8550c4c2010-10-08 15:49:53 -0700382 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700383 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700384 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700385 }
386
Romain Guydda570202010-07-06 11:39:32 -0700387 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700388 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700389 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700390 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
391 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guydda570202010-07-06 11:39:32 -0700392
Romain Guy8fb95422010-08-17 18:38:51 -0700393 // Save the layer in the snapshot
394 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700395 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700396
Romain Guyeb993562010-10-05 18:14:38 -0700397 if (fboLayer) {
398 layer->fbo = mCaches.fboCache.get();
Romain Guy38c85b92010-09-22 22:48:20 -0700399
Romain Guyeb993562010-10-05 18:14:38 -0700400 snapshot->flags |= Snapshot::kFlagIsFboLayer;
401 snapshot->fbo = layer->fbo;
402 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
403 snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
404 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
405 snapshot->height = bounds.getHeight();
406 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
407 snapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf607bdc2010-09-10 19:20:06 -0700408
Romain Guyeb993562010-10-05 18:14:38 -0700409 // Bind texture to FBO
410 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
411 glBindTexture(GL_TEXTURE_2D, layer->texture);
412
413 // Initialize the texture if needed
414 if (layer->empty) {
415 layer->empty = false;
Romain Guy8550c4c2010-10-08 15:49:53 -0700416 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
Romain Guyeb993562010-10-05 18:14:38 -0700417 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
418 }
419
420 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
421 layer->texture, 0);
422
Romain Guye9108052010-10-12 18:15:42 -0700423#if DEBUG_LAYERS
Romain Guyeb993562010-10-05 18:14:38 -0700424 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
425 if (status != GL_FRAMEBUFFER_COMPLETE) {
426 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
427
428 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
429 glDeleteTextures(1, &layer->texture);
430 mCaches.fboCache.put(layer->fbo);
431
432 delete layer;
433
434 return false;
435 }
Romain Guye9108052010-10-12 18:15:42 -0700436#endif
Romain Guyeb993562010-10-05 18:14:38 -0700437
438 // Clear the FBO
Romain Guy93d23612010-10-13 18:26:36 -0700439 glScissor(0.0f, 0.0f, bounds.getWidth() + 1.0f, bounds.getHeight() + 1.0f);
Romain Guyeb993562010-10-05 18:14:38 -0700440 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
441 glClear(GL_COLOR_BUFFER_BIT);
Romain Guyeb07af62010-10-12 18:40:36 -0700442
443 setScissorFromClip();
Romain Guyeb993562010-10-05 18:14:38 -0700444
445 // Change the ortho projection
446 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
447 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
448 } else {
449 // Copy the framebuffer into the layer
450 glBindTexture(GL_TEXTURE_2D, layer->texture);
451
Romain Guyc00972b2010-10-12 11:31:07 -0700452 if (layer->empty) {
453 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
454 layer->width, layer->height, 0);
455 layer->empty = false;
456 } else {
457 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, mHeight - bounds.bottom,
458 bounds.getWidth(), bounds.getHeight());
459 }
Romain Guyeb993562010-10-05 18:14:38 -0700460
461 // Enqueue the buffer coordinates to clear the corresponding region later
462 mLayers.push(new Rect(bounds));
463 }
Romain Guyf86ef572010-07-01 11:05:42 -0700464
Romain Guyd55a8612010-06-28 17:42:46 -0700465 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700466}
467
Romain Guy1c740bc2010-09-13 18:00:09 -0700468/**
469 * Read the documentation of createLayer() before doing anything in this method.
470 */
Romain Guy1d83e192010-08-17 11:37:00 -0700471void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
472 if (!current->layer) {
473 LOGE("Attempting to compose a layer that does not exist");
474 return;
475 }
476
Romain Guyeb993562010-10-05 18:14:38 -0700477 const bool fboLayer = current->flags & SkCanvas::kClipToLayer_SaveFlag;
478
479 if (fboLayer) {
480 // Unbind current FBO and restore previous one
481 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
482 }
483
Romain Guy1d83e192010-08-17 11:37:00 -0700484 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700485 const Rect& clip = *previous->clipRect;
Romain Guyeb993562010-10-05 18:14:38 -0700486 glScissor(clip.left, previous->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700487
488 Layer* layer = current->layer;
489 const Rect& rect = layer->layer;
490
Romain Guyeb993562010-10-05 18:14:38 -0700491 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700492 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700493 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guyf607bdc2010-09-10 19:20:06 -0700494 }
495
Romain Guy8550c4c2010-10-08 15:49:53 -0700496 const Rect& texCoords = layer->texCoords;
Romain Guy03750a02010-10-18 14:06:08 -0700497 mCaches.unbindMeshBuffer();
Romain Guy8550c4c2010-10-08 15:49:53 -0700498 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
Romain Guy8b55f372010-08-18 17:10:07 -0700499
Romain Guyeb993562010-10-05 18:14:38 -0700500 if (fboLayer) {
Romain Guy03750a02010-10-18 14:06:08 -0700501 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
502 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
503 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyeb993562010-10-05 18:14:38 -0700504 } else {
505 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
506 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
507 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, true, true);
508 }
Romain Guy1d83e192010-08-17 11:37:00 -0700509
Romain Guy8b55f372010-08-18 17:10:07 -0700510 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
511
Romain Guyeb993562010-10-05 18:14:38 -0700512 if (fboLayer) {
513 // Detach the texture from the FBO
514 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
515 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
516 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
517
518 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
519 mCaches.fboCache.put(current->fbo);
520 }
521
Romain Guyeb993562010-10-05 18:14:38 -0700522 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700523 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700524 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700525 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700526 delete layer;
527 }
528}
529
Romain Guy86942302010-09-12 13:02:16 -0700530void OpenGLRenderer::clearLayerRegions() {
Romain Guydbc26d22010-10-11 17:58:29 -0700531 if (mLayers.size() == 0 || mSnapshot->invisible) return;
Romain Guy86942302010-09-12 13:02:16 -0700532
533 for (uint32_t i = 0; i < mLayers.size(); i++) {
534 Rect* bounds = mLayers.itemAt(i);
535
536 // Clear the framebuffer where the layer will draw
Romain Guyd2a1ff02010-10-14 14:46:44 -0700537 glScissor(bounds->left, mSnapshot->height - bounds->bottom,
Romain Guy86942302010-09-12 13:02:16 -0700538 bounds->getWidth(), bounds->getHeight());
539 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
540 glClear(GL_COLOR_BUFFER_BIT);
541
542 delete bounds;
543 }
544 mLayers.clear();
545
546 // Restore the clip
547 setScissorFromClip();
548}
549
Romain Guybd6b79b2010-06-26 00:13:53 -0700550///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700551// Transforms
552///////////////////////////////////////////////////////////////////////////////
553
554void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700555 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700556}
557
558void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700559 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700560}
561
562void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700563 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700564}
565
566void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700567 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700568}
569
Romain Guy41030da2010-10-13 13:40:37 -0700570const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700571 if (mSnapshot->fbo != 0) {
572 return &mSnapshot->transform->data[0];
573 }
574 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700575}
576
Romain Guyf6a11b82010-06-23 17:47:49 -0700577void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700578 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700579}
580
581void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700582 SkMatrix transform;
583 mSnapshot->transform->copyTo(transform);
584 transform.preConcat(*matrix);
585 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700586}
587
588///////////////////////////////////////////////////////////////////////////////
589// Clipping
590///////////////////////////////////////////////////////////////////////////////
591
Romain Guybb9524b2010-06-22 18:56:38 -0700592void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700593 Rect clip(*mSnapshot->clipRect);
594 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700595 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700596}
597
598const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700599 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700600}
601
Romain Guyc7d53492010-06-25 13:41:57 -0700602bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guydbc26d22010-10-11 17:58:29 -0700603 if (mSnapshot->invisible) {
604 return true;
605 }
606
Romain Guy1d83e192010-08-17 11:37:00 -0700607 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700608 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700609 r.snapToPixelBoundaries();
610
611 Rect clipRect(*mSnapshot->clipRect);
612 clipRect.snapToPixelBoundaries();
613
614 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700615}
616
Romain Guy079ba2c2010-07-16 14:12:24 -0700617bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
618 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700619 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700620 setScissorFromClip();
621 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700622 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700623}
624
Romain Guyf6a11b82010-06-23 17:47:49 -0700625///////////////////////////////////////////////////////////////////////////////
626// Drawing
627///////////////////////////////////////////////////////////////////////////////
628
Romain Guyc1396e92010-06-30 17:56:19 -0700629void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700630 const float right = left + bitmap->width();
631 const float bottom = top + bitmap->height();
632
633 if (quickReject(left, top, right, bottom)) {
634 return;
635 }
636
Romain Guy61c8c9c2010-08-09 20:48:09 -0700637 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700638 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700639 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700640 const AutoTexture autoCleanup(texture);
641
Romain Guy6926c722010-07-12 20:20:03 -0700642 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700643}
644
Romain Guyf86ef572010-07-01 11:05:42 -0700645void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
646 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
647 const mat4 transform(*matrix);
648 transform.mapRect(r);
649
Romain Guy6926c722010-07-12 20:20:03 -0700650 if (quickReject(r.left, r.top, r.right, r.bottom)) {
651 return;
652 }
653
Romain Guy61c8c9c2010-08-09 20:48:09 -0700654 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700655 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700656 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700657 const AutoTexture autoCleanup(texture);
658
Romain Guy82ba8142010-07-09 13:25:56 -0700659 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700660}
661
Romain Guy8ba548f2010-06-30 19:21:21 -0700662void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
663 float srcLeft, float srcTop, float srcRight, float srcBottom,
664 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700665 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700666 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
667 return;
668 }
669
Romain Guy61c8c9c2010-08-09 20:48:09 -0700670 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700671 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700672 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700673 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700674
Romain Guy8ba548f2010-06-30 19:21:21 -0700675 const float width = texture->width;
676 const float height = texture->height;
677
678 const float u1 = srcLeft / width;
679 const float v1 = srcTop / height;
680 const float u2 = srcRight / width;
681 const float v2 = srcBottom / height;
682
Romain Guy03750a02010-10-18 14:06:08 -0700683 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -0700684 resetDrawTextureTexCoords(u1, v1, u2, v2);
685
Romain Guy03750a02010-10-18 14:06:08 -0700686 int alpha;
687 SkXfermode::Mode mode;
688 getAlphaAndMode(paint, &alpha, &mode);
689
690 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
691 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
692 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy8ba548f2010-06-30 19:21:21 -0700693
694 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
695}
696
Romain Guy4aa90572010-09-26 18:40:37 -0700697void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -0700698 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
699 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700700 if (quickReject(left, top, right, bottom)) {
701 return;
702 }
703
Romain Guy61c8c9c2010-08-09 20:48:09 -0700704 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700705 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700706 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700707 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700708
709 int alpha;
710 SkXfermode::Mode mode;
711 getAlphaAndMode(paint, &alpha, &mode);
712
Romain Guy2728f962010-10-08 18:36:15 -0700713 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -0700714 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -0700715
Romain Guy054dc182010-10-15 17:55:25 -0700716 if (mesh) {
717 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
718 // patch mesh already defines the final size
719 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
Romain Guy03750a02010-10-18 14:06:08 -0700720 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
721 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer);
Romain Guy054dc182010-10-15 17:55:25 -0700722 }
Romain Guyf7f93552010-07-08 19:17:03 -0700723}
724
Romain Guy759ea802010-09-16 20:49:46 -0700725void OpenGLRenderer::drawLines(float* points, int count, const SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -0700726 if (mSnapshot->invisible) return;
727
Romain Guy759ea802010-09-16 20:49:46 -0700728 int alpha;
729 SkXfermode::Mode mode;
730 getAlphaAndMode(paint, &alpha, &mode);
731
732 uint32_t color = paint->getColor();
733 const GLfloat a = alpha / 255.0f;
734 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
735 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
736 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
737
Romain Guyc95c8d62010-09-17 15:31:32 -0700738 const bool isAA = paint->isAntiAlias();
739 if (isAA) {
740 GLuint textureUnit = 0;
Romain Guy29d89972010-09-22 16:10:57 -0700741 setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
Romain Guy03750a02010-10-18 14:06:08 -0700742 mode, false, true, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
743 mCaches.line.getMeshBuffer());
Romain Guyc95c8d62010-09-17 15:31:32 -0700744 } else {
745 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false);
746 }
747
748 const float strokeWidth = paint->getStrokeWidth();
Romain Guy29d89972010-09-22 16:10:57 -0700749 const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount;
Romain Guyc95c8d62010-09-17 15:31:32 -0700750 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
Romain Guy759ea802010-09-16 20:49:46 -0700751
752 for (int i = 0; i < count; i += 4) {
753 float tx = 0.0f;
754 float ty = 0.0f;
755
Romain Guyc95c8d62010-09-17 15:31:32 -0700756 if (isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700757 mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3],
Romain Guyc95c8d62010-09-17 15:31:32 -0700758 strokeWidth, tx, ty);
759 } else {
Romain Guyb5ab4172010-09-17 15:36:56 -0700760 ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f;
Romain Guyc95c8d62010-09-17 15:31:32 -0700761 }
Romain Guy759ea802010-09-16 20:49:46 -0700762
763 const float dx = points[i + 2] - points[i];
764 const float dy = points[i + 3] - points[i + 1];
765 const float mag = sqrtf(dx * dx + dy * dy);
766 const float angle = acos(dx / mag);
767
768 mModelView.loadTranslate(points[i], points[i + 1], 0.0f);
769 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) {
770 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f);
771 }
772 mModelView.translate(tx, ty, 0.0f);
Romain Guyc95c8d62010-09-17 15:31:32 -0700773 if (!isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700774 float length = mCaches.line.getLength(points[i], points[i + 1],
775 points[i + 2], points[i + 3]);
Romain Guyc95c8d62010-09-17 15:31:32 -0700776 mModelView.scale(length, strokeWidth, 1.0f);
777 }
Romain Guy759ea802010-09-16 20:49:46 -0700778 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
779
780 if (mShader) {
781 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot);
782 }
783
Romain Guyc95c8d62010-09-17 15:31:32 -0700784 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy759ea802010-09-16 20:49:46 -0700785 }
786
Romain Guy29d89972010-09-22 16:10:57 -0700787 if (isAA) {
788 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
789 }
Romain Guy759ea802010-09-16 20:49:46 -0700790}
791
Romain Guy85bf02f2010-06-22 13:11:24 -0700792void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700793 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700794 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700795}
Romain Guy9d5316e2010-06-24 19:30:36 -0700796
Romain Guybd6b79b2010-06-26 00:13:53 -0700797void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700798 if (quickReject(left, top, right, bottom)) {
799 return;
800 }
801
Romain Guy026c5e162010-06-28 17:12:22 -0700802 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700803 if (!mExtensions.hasFramebufferFetch()) {
804 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
805 if (!isMode) {
806 // Assume SRC_OVER
807 mode = SkXfermode::kSrcOver_Mode;
808 }
809 } else {
810 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700811 }
812
813 // Skia draws using the color's alpha channel if < 255
814 // Otherwise, it uses the paint's alpha
815 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700816 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700817 color |= p->getAlpha() << 24;
818 }
819
820 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700821}
Romain Guy9d5316e2010-06-24 19:30:36 -0700822
Romain Guye8e62a42010-07-23 18:55:21 -0700823void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
824 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700825 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700826 return;
827 }
Romain Guydbc26d22010-10-11 17:58:29 -0700828 if (mSnapshot->invisible) return;
Romain Guye2d345e2010-09-24 18:39:22 -0700829
Romain Guyf607bdc2010-09-10 19:20:06 -0700830 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700831
Romain Guya674ab72010-08-10 17:26:42 -0700832 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700833 switch (paint->getTextAlign()) {
834 case SkPaint::kCenter_Align:
835 length = paint->measureText(text, bytesCount);
836 x -= length / 2.0f;
837 break;
838 case SkPaint::kRight_Align:
839 length = paint->measureText(text, bytesCount);
840 x -= length;
841 break;
842 default:
843 break;
844 }
845
Romain Guy694b5192010-07-21 21:33:20 -0700846 int alpha;
847 SkXfermode::Mode mode;
848 getAlphaAndMode(paint, &alpha, &mode);
849
Romain Guy2542d192010-08-18 11:47:12 -0700850 uint32_t color = paint->getColor();
851 const GLfloat a = alpha / 255.0f;
852 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
853 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
854 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
855
Romain Guyb45c0c92010-08-26 20:35:23 -0700856 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
857 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700858 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -0700859
Romain Guy9d13fe252010-10-15 16:06:03 -0700860 Rect clipRect(*mSnapshot->clipRect);
861 glScissor(clipRect.left, mSnapshot->height - clipRect.bottom,
862 clipRect.getWidth(), clipRect.getHeight());
863
Romain Guy1e45aae2010-08-13 19:39:53 -0700864 if (mHasShadow) {
865 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700866 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700867 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700868 count, mShadowRadius);
869 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700870
Romain Guy2542d192010-08-18 11:47:12 -0700871 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700872
873 // Draw the mesh
874 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700875 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700876 }
877
Romain Guy06f96e22010-07-30 19:18:16 -0700878 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700879 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700880
Romain Guye8cb9c142010-10-04 14:14:11 -0700881 // Assume that the modelView matrix does not force scales, rotates, etc.
882 const bool linearFilter = mSnapshot->transform->changesBounds();
883 setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit,
Romain Guy03750a02010-10-18 14:06:08 -0700884 x, y, r, g, b, a, mode, false, true, NULL, NULL);
Romain Guy06f96e22010-07-30 19:18:16 -0700885
Romain Guy09147fb2010-07-22 13:08:20 -0700886 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy86942302010-09-12 13:02:16 -0700887 clearLayerRegions();
Romain Guy9d13fe252010-10-15 16:06:03 -0700888
Romain Guy03750a02010-10-18 14:06:08 -0700889 mCaches.unbindMeshBuffer();
Romain Guyb45c0c92010-08-26 20:35:23 -0700890 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700891
892 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700893 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700894
Romain Guy0a417492010-08-16 20:26:20 -0700895 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy9d13fe252010-10-15 16:06:03 -0700896
897 setScissorFromClip();
Romain Guy694b5192010-07-21 21:33:20 -0700898}
899
Romain Guy7fbcc042010-08-04 15:40:07 -0700900void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -0700901 if (mSnapshot->invisible) return;
902
Romain Guy7fbcc042010-08-04 15:40:07 -0700903 GLuint textureUnit = 0;
904 glActiveTexture(gTextureUnits[textureUnit]);
905
Romain Guyfb8b7632010-08-23 21:05:08 -0700906 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700907 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700908 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700909
Romain Guy8b55f372010-08-18 17:10:07 -0700910 const float x = texture->left - texture->offset;
911 const float y = texture->top - texture->offset;
912
913 if (quickReject(x, y, x + texture->width, y + texture->height)) {
914 return;
915 }
916
Romain Guy7fbcc042010-08-04 15:40:07 -0700917 int alpha;
918 SkXfermode::Mode mode;
919 getAlphaAndMode(paint, &alpha, &mode);
920
921 uint32_t color = paint->getColor();
922 const GLfloat a = alpha / 255.0f;
923 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
924 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
925 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
926
Romain Guy0a417492010-08-16 20:26:20 -0700927 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
928
Romain Guy86942302010-09-12 13:02:16 -0700929 clearLayerRegions();
930
Romain Guy0a417492010-08-16 20:26:20 -0700931 // Draw the mesh
932 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700933 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700934}
935
Romain Guy6926c722010-07-12 20:20:03 -0700936///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700937// Shaders
938///////////////////////////////////////////////////////////////////////////////
939
940void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700941 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700942}
943
Romain Guy06f96e22010-07-30 19:18:16 -0700944void OpenGLRenderer::setupShader(SkiaShader* shader) {
945 mShader = shader;
946 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700947 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700948 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700949}
950
Romain Guyd27977d2010-07-14 19:18:51 -0700951///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700952// Color filters
953///////////////////////////////////////////////////////////////////////////////
954
955void OpenGLRenderer::resetColorFilter() {
956 mColorFilter = NULL;
957}
958
959void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
960 mColorFilter = filter;
961}
962
963///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700964// Drop shadow
965///////////////////////////////////////////////////////////////////////////////
966
967void OpenGLRenderer::resetShadow() {
968 mHasShadow = false;
969}
970
971void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
972 mHasShadow = true;
973 mShadowRadius = radius;
974 mShadowDx = dx;
975 mShadowDy = dy;
976 mShadowColor = color;
977}
978
979///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700980// Drawing implementation
981///////////////////////////////////////////////////////////////////////////////
982
Romain Guy0a417492010-08-16 20:26:20 -0700983void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700984 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700985 const float sx = x - texture->left + mShadowDx;
986 const float sy = y - texture->top + mShadowDy;
987
Romain Guy2542d192010-08-18 11:47:12 -0700988 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
989 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700990 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
991 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
992 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
993
994 GLuint textureUnit = 0;
995 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
996}
997
998void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
999 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
1000 bool transforms, bool applyFilters) {
1001 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -07001002 x, y, r, g, b, a, mode, transforms, applyFilters,
Romain Guy03750a02010-10-18 14:06:08 -07001003 (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy0a417492010-08-16 20:26:20 -07001004}
1005
1006void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1007 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1008 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy03750a02010-10-18 14:06:08 -07001009 setupTextureAlpha8(texture, width, height, textureUnit, x, y, r, g, b, a, mode,
1010 transforms, applyFilters, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy759ea802010-09-16 20:49:46 -07001011}
1012
1013void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1014 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1015 SkXfermode::Mode mode, bool transforms, bool applyFilters,
Romain Guy03750a02010-10-18 14:06:08 -07001016 GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guy0a417492010-08-16 20:26:20 -07001017 // Describe the required shaders
1018 ProgramDescription description;
1019 description.hasTexture = true;
1020 description.hasAlpha8Texture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001021 const bool setColor = description.setAlpha8Color(r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -07001022
1023 if (applyFilters) {
1024 if (mShader) {
1025 mShader->describe(description, mExtensions);
1026 }
1027 if (mColorFilter) {
1028 mColorFilter->describe(description, mExtensions);
1029 }
1030 }
1031
Romain Guya5aed0d2010-09-09 14:42:43 -07001032 // Setup the blending mode
1033 chooseBlending(true, mode, description);
1034
Romain Guy0a417492010-08-16 20:26:20 -07001035 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001036 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -07001037
Romain Guy0a417492010-08-16 20:26:20 -07001038 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -07001039 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001040
Romain Guyfb8b7632010-08-23 21:05:08 -07001041 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -07001042 glEnableVertexAttribArray(texCoordsSlot);
1043
Romain Guy03750a02010-10-18 14:06:08 -07001044 if (texCoords) {
1045 // Setup attributes
1046 if (!vertices) {
1047 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1048 } else {
1049 mCaches.unbindMeshBuffer();
1050 }
1051 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1052 gMeshStride, vertices);
1053 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1054 }
Romain Guy0a417492010-08-16 20:26:20 -07001055
1056 // Setup uniforms
1057 if (transforms) {
1058 mModelView.loadTranslate(x, y, 0.0f);
1059 mModelView.scale(width, height, 1.0f);
1060 } else {
1061 mModelView.loadIdentity();
1062 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001063 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy707b2f72010-10-11 16:34:59 -07001064 if (setColor) {
1065 mCaches.currentProgram->setColor(r, g, b, a);
1066 }
Romain Guy0a417492010-08-16 20:26:20 -07001067
1068 textureUnit++;
1069 if (applyFilters) {
1070 // Setup attributes and uniforms required by the shaders
1071 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001072 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001073 }
1074 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001075 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -07001076 }
1077 }
1078}
1079
Romain Guyf607bdc2010-09-10 19:20:06 -07001080// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001081#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1082#define kStdUnderline_Offset (1.0f / 9.0f)
1083#define kStdUnderline_Thickness (1.0f / 18.0f)
1084
1085void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1086 float x, float y, SkPaint* paint) {
1087 // Handle underline and strike-through
1088 uint32_t flags = paint->getFlags();
1089 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1090 float underlineWidth = length;
1091 // If length is > 0.0f, we already measured the text for the text alignment
1092 if (length <= 0.0f) {
1093 underlineWidth = paint->measureText(text, bytesCount);
1094 }
1095
1096 float offsetX = 0;
1097 switch (paint->getTextAlign()) {
1098 case SkPaint::kCenter_Align:
1099 offsetX = underlineWidth * 0.5f;
1100 break;
1101 case SkPaint::kRight_Align:
1102 offsetX = underlineWidth;
1103 break;
1104 default:
1105 break;
1106 }
1107
1108 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001109 const float textSize = paint->getTextSize();
1110 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -07001111
Romain Guye20ecbd2010-09-22 19:49:04 -07001112 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001113 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001114
1115 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
1116 float points[pointsCount];
1117 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001118
1119 if (flags & SkPaint::kUnderlineText_Flag) {
1120 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001121 points[currentPoint++] = left;
1122 points[currentPoint++] = top;
1123 points[currentPoint++] = left + underlineWidth;
1124 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001125 }
1126
1127 if (flags & SkPaint::kStrikeThruText_Flag) {
1128 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001129 points[currentPoint++] = left;
1130 points[currentPoint++] = top;
1131 points[currentPoint++] = left + underlineWidth;
1132 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001133 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001134
1135 SkPaint linesPaint(*paint);
1136 linesPaint.setStrokeWidth(strokeWidth);
1137
1138 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001139 }
1140 }
1141}
1142
Romain Guy026c5e162010-06-28 17:12:22 -07001143void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001144 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001145 clearLayerRegions();
1146
Romain Guyd27977d2010-07-14 19:18:51 -07001147 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001148 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001149 color |= 0x00ffffff;
1150 }
1151
1152 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -07001153 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -07001154 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -07001155 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1156 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1157 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1158
Romain Guyc95c8d62010-09-17 15:31:32 -07001159 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform);
1160
1161 // Draw the mesh
1162 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1163}
1164
1165void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom,
1166 float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy06f96e22010-07-30 19:18:16 -07001167 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -07001168
Romain Guy06f96e22010-07-30 19:18:16 -07001169 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -07001170 ProgramDescription description;
Romain Guy707b2f72010-10-11 16:34:59 -07001171 const bool setColor = description.setColor(r, g, b, a);
1172
Romain Guy06f96e22010-07-30 19:18:16 -07001173 if (mShader) {
1174 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -07001175 }
Romain Guydb1938e2010-08-02 18:50:22 -07001176 if (mColorFilter) {
1177 mColorFilter->describe(description, mExtensions);
1178 }
Romain Guyd27977d2010-07-14 19:18:51 -07001179
Romain Guy1c740bc2010-09-13 18:00:09 -07001180 // Setup the blending mode
Romain Guyc95c8d62010-09-17 15:31:32 -07001181 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -07001182
Romain Guy06f96e22010-07-30 19:18:16 -07001183 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001184 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -07001185
1186 // Setup attributes
Romain Guy03750a02010-10-18 14:06:08 -07001187 mCaches.bindMeshBuffer();
Romain Guyfb8b7632010-08-23 21:05:08 -07001188 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy03750a02010-10-18 14:06:08 -07001189 gMeshStride, 0);
Romain Guy06f96e22010-07-30 19:18:16 -07001190
1191 // Setup uniforms
1192 mModelView.loadTranslate(left, top, 0.0f);
1193 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -07001194 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001195 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -07001196 } else {
1197 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -07001198 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -07001199 }
Romain Guy707b2f72010-10-11 16:34:59 -07001200 mCaches.currentProgram->setColor(r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -07001201
Romain Guy06f96e22010-07-30 19:18:16 -07001202 // Setup attributes and uniforms required by the shaders
1203 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001204 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001205 }
Romain Guydb1938e2010-08-02 18:50:22 -07001206 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001207 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001208 }
Romain Guyd27977d2010-07-14 19:18:51 -07001209}
1210
Romain Guy82ba8142010-07-09 13:25:56 -07001211void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001212 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001213 int alpha;
1214 SkXfermode::Mode mode;
1215 getAlphaAndMode(paint, &alpha, &mode);
1216
Romain Guy6820ac82010-09-15 18:11:50 -07001217 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy03750a02010-10-18 14:06:08 -07001218 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy6820ac82010-09-15 18:11:50 -07001219 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -07001220}
1221
Romain Guybd6b79b2010-06-26 00:13:53 -07001222void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001223 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1224 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001225 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001226}
1227
1228void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001229 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001230 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy03750a02010-10-18 14:06:08 -07001231 bool swapSrcDst, bool ignoreTransform, GLuint vbo) {
Romain Guy86942302010-09-12 13:02:16 -07001232 clearLayerRegions();
1233
Romain Guy889f8d12010-07-29 14:37:42 -07001234 ProgramDescription description;
1235 description.hasTexture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001236 const bool setColor = description.setColor(alpha, alpha, alpha, alpha);
Romain Guydb1938e2010-08-02 18:50:22 -07001237 if (mColorFilter) {
1238 mColorFilter->describe(description, mExtensions);
1239 }
Romain Guy889f8d12010-07-29 14:37:42 -07001240
Romain Guybd6b79b2010-06-26 00:13:53 -07001241 mModelView.loadTranslate(left, top, 0.0f);
1242 mModelView.scale(right - left, bottom - top, 1.0f);
1243
Romain Guyf607bdc2010-09-10 19:20:06 -07001244 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001245
Romain Guyfb8b7632010-08-23 21:05:08 -07001246 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001247 if (!ignoreTransform) {
1248 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1249 } else {
1250 mat4 m;
1251 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
1252 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001253
Romain Guy889f8d12010-07-29 14:37:42 -07001254 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -07001255 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001256 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001257
Romain Guya9794742010-07-13 11:37:54 -07001258 // Always premultiplied
Romain Guy707b2f72010-10-11 16:34:59 -07001259 if (setColor) {
1260 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha);
1261 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001262
Romain Guy889f8d12010-07-29 14:37:42 -07001263 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001264 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001265 glEnableVertexAttribArray(texCoordsSlot);
Romain Guy03750a02010-10-18 14:06:08 -07001266
1267 if (!vertices) {
1268 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1269 } else {
1270 mCaches.unbindMeshBuffer();
1271 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001272 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001273 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001274 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001275
Romain Guydb1938e2010-08-02 18:50:22 -07001276 // Color filter
1277 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001278 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001279 }
1280
Romain Guy6820ac82010-09-15 18:11:50 -07001281 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001282 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001283}
1284
Romain Guya5aed0d2010-09-09 14:42:43 -07001285void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001286 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001287 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1288 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001289 if (mode < SkXfermode::kPlus_Mode) {
1290 if (!mCaches.blend) {
1291 glEnable(GL_BLEND);
1292 }
Romain Guy82ba8142010-07-09 13:25:56 -07001293
Romain Guyf607bdc2010-09-10 19:20:06 -07001294 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1295 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001296
Romain Guya5aed0d2010-09-09 14:42:43 -07001297 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1298 glBlendFunc(sourceMode, destMode);
1299 mCaches.lastSrcMode = sourceMode;
1300 mCaches.lastDstMode = destMode;
1301 }
1302 } else {
1303 // These blend modes are not supported by OpenGL directly and have
1304 // to be implemented using shaders. Since the shader will perform
1305 // the blending, turn blending off here
1306 if (mExtensions.hasFramebufferFetch()) {
1307 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001308 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001309 }
1310
1311 if (mCaches.blend) {
1312 glDisable(GL_BLEND);
1313 }
1314 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001315 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001316 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001317 glDisable(GL_BLEND);
1318 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001319 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001320}
1321
Romain Guy889f8d12010-07-29 14:37:42 -07001322bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001323 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001324 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001325 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001326 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001327 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001328 }
Romain Guy6926c722010-07-12 20:20:03 -07001329 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001330}
1331
Romain Guy026c5e162010-06-28 17:12:22 -07001332void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001333 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001334 TextureVertex::setUV(v++, u1, v1);
1335 TextureVertex::setUV(v++, u2, v1);
1336 TextureVertex::setUV(v++, u1, v2);
1337 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001338}
1339
1340void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1341 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001342 if (!mExtensions.hasFramebufferFetch()) {
1343 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1344 if (!isMode) {
1345 // Assume SRC_OVER
1346 *mode = SkXfermode::kSrcOver_Mode;
1347 }
1348 } else {
1349 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001350 }
1351
1352 // Skia draws using the color's alpha channel if < 255
1353 // Otherwise, it uses the paint's alpha
1354 int color = paint->getColor();
1355 *alpha = (color >> 24) & 0xFF;
1356 if (*alpha == 255) {
1357 *alpha = paint->getAlpha();
1358 }
1359 } else {
1360 *mode = SkXfermode::kSrcOver_Mode;
1361 *alpha = 255;
1362 }
Romain Guy026c5e162010-06-28 17:12:22 -07001363}
1364
Romain Guya5aed0d2010-09-09 14:42:43 -07001365SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1366 if (mode == NULL) {
1367 return SkXfermode::kSrcOver_Mode;
1368 }
1369 return mode->fMode;
1370}
1371
Romain Guy889f8d12010-07-29 14:37:42 -07001372void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1373 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001374 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001375 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1376 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1377}
1378
Romain Guy9d5316e2010-06-24 19:30:36 -07001379}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001380}; // namespace android