blob: ee5fe229b33fce4253a491d6d05b408630020e36 [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 Guy06f96e22010-07-30 19:18:16 -070038#define REQUIRED_TEXTURE_UNITS_COUNT 3
39
Romain Guydda57022010-07-06 11:39:32 -070040// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070041#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070042
Romain Guy759ea802010-09-16 20:49:46 -070043#define RAD_TO_DEG (180.0f / 3.14159265f)
44#define MIN_ANGLE 0.001f
45
Romain Guy9d5316e2010-06-24 19:30:36 -070046///////////////////////////////////////////////////////////////////////////////
47// Globals
48///////////////////////////////////////////////////////////////////////////////
49
Romain Guy026c5e162010-06-28 17:12:22 -070050// This array is never used directly but used as a memcpy source in the
51// OpenGLRenderer constructor
Romain Guyac670c02010-07-27 17:39:27 -070052static const TextureVertex gMeshVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070053 FV(0.0f, 0.0f, 0.0f, 0.0f),
54 FV(1.0f, 0.0f, 1.0f, 0.0f),
55 FV(0.0f, 1.0f, 0.0f, 1.0f),
56 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070057};
Romain Guyac670c02010-07-27 17:39:27 -070058static const GLsizei gMeshStride = sizeof(TextureVertex);
59static const GLsizei gMeshCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070060
Romain Guy889f8d12010-07-29 14:37:42 -070061/**
62 * Structure mapping Skia xfermodes to OpenGL blending factors.
63 */
64struct Blender {
65 SkXfermode::Mode mode;
66 GLenum src;
67 GLenum dst;
68}; // struct Blender
69
Romain Guy026c5e162010-06-28 17:12:22 -070070// In this array, the index of each Blender equals the value of the first
71// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
72static const Blender gBlends[] = {
73 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
74 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
75 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
76 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
77 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
78 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
79 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
80 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
81 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
82 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
84 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
85};
Romain Guye4d01122010-06-16 18:44:05 -070086
Romain Guy87a76572010-09-13 18:11:21 -070087// This array contains the swapped version of each SkXfermode. For instance
88// this array's SrcOver blending mode is actually DstOver. You can refer to
89// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070090static const Blender gBlendsSwap[] = {
91 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
92 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
93 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
94 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
95 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
96 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
97 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
98 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
99 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
100 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
101 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
103};
104
Romain Guy889f8d12010-07-29 14:37:42 -0700105static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -0700106 GL_TEXTURE0,
107 GL_TEXTURE1,
108 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700109};
110
Romain Guyf6a11b82010-06-23 17:47:49 -0700111///////////////////////////////////////////////////////////////////////////////
112// Constructors/destructor
113///////////////////////////////////////////////////////////////////////////////
114
Romain Guyfb8b7632010-08-23 21:05:08 -0700115OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700116 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700117 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700118 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700119
Romain Guyac670c02010-07-27 17:39:27 -0700120 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
121
Romain Guyae5575b2010-07-29 18:48:04 -0700122 mFirstSnapshot = new Snapshot;
123
124 GLint maxTextureUnits;
125 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
126 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Romain Guy889f8d12010-07-29 14:37:42 -0700127 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
128 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700129
130 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Romain Guye4d01122010-06-16 18:44:05 -0700131}
132
Romain Guy85bf02f2010-06-22 13:11:24 -0700133OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700134 // The context has already been destroyed at this point, do not call
135 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700136}
137
Romain Guyf6a11b82010-06-23 17:47:49 -0700138///////////////////////////////////////////////////////////////////////////////
139// Setup
140///////////////////////////////////////////////////////////////////////////////
141
Romain Guy85bf02f2010-06-22 13:11:24 -0700142void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700143 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700144 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700145
146 mWidth = width;
147 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700148
149 mFirstSnapshot->height = height;
150 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700151}
152
Romain Guy85bf02f2010-06-22 13:11:24 -0700153void OpenGLRenderer::prepare() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700154 mSnapshot = new Snapshot(mFirstSnapshot,
155 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy8fb95422010-08-17 18:38:51 -0700156 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700157
Romain Guyfb8b7632010-08-23 21:05:08 -0700158 glViewport(0, 0, mWidth, mHeight);
159
Romain Guyd90f23e2010-09-09 11:47:54 -0700160 glDisable(GL_DITHER);
Romain Guy08ae3172010-06-21 19:35:50 -0700161 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700162
Romain Guy08ae3172010-06-21 19:35:50 -0700163 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
164 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700165
Romain Guy08ae3172010-06-21 19:35:50 -0700166 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700167 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700168
Romain Guyb82da652010-07-30 11:36:12 -0700169 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700170}
171
Romain Guyb025b9c2010-09-16 14:16:48 -0700172void OpenGLRenderer::finish() {
173#if DEBUG_OPENGL
174 GLenum status = GL_NO_ERROR;
175 while ((status = glGetError()) != GL_NO_ERROR) {
176 LOGD("GL error from OpenGLRenderer: 0x%x", status);
177 }
178#endif
179}
180
Romain Guyda8532c2010-08-31 11:50:35 -0700181void OpenGLRenderer::acquireContext() {
182 if (mCaches.currentProgram) {
183 if (mCaches.currentProgram->isInUse()) {
184 mCaches.currentProgram->remove();
185 mCaches.currentProgram = NULL;
186 }
187 }
188}
189
190void OpenGLRenderer::releaseContext() {
Romain Guyeb993562010-10-05 18:14:38 -0700191 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700192
193 glEnable(GL_SCISSOR_TEST);
194 setScissorFromClip();
195
Romain Guyf607bdc2010-09-10 19:20:06 -0700196 glDisable(GL_DITHER);
197
198 glBindFramebuffer(GL_FRAMEBUFFER, 0);
199
Romain Guyda8532c2010-08-31 11:50:35 -0700200 if (mCaches.blend) {
201 glEnable(GL_BLEND);
202 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
Romain Guyf607bdc2010-09-10 19:20:06 -0700203 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700204 } else {
205 glDisable(GL_BLEND);
206 }
207}
208
Romain Guyf6a11b82010-06-23 17:47:49 -0700209///////////////////////////////////////////////////////////////////////////////
210// State management
211///////////////////////////////////////////////////////////////////////////////
212
Romain Guybb9524b2010-06-22 18:56:38 -0700213int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700214 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700215}
216
217int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700218 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700219}
220
221void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700222 if (mSaveCount > 1) {
223 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700224 }
Romain Guybb9524b2010-06-22 18:56:38 -0700225}
226
227void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700228 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700229
Romain Guy8fb95422010-08-17 18:38:51 -0700230 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700231 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700232 }
Romain Guybb9524b2010-06-22 18:56:38 -0700233}
234
Romain Guy8aef54f2010-09-01 15:13:49 -0700235int OpenGLRenderer::saveSnapshot(int flags) {
236 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700237 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700238}
239
240bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700241 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700242 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700243 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700244
Romain Guybd6b79b2010-06-26 00:13:53 -0700245 sp<Snapshot> current = mSnapshot;
246 sp<Snapshot> previous = mSnapshot->previous;
247
Romain Guyeb993562010-10-05 18:14:38 -0700248 if (restoreOrtho) {
249 Rect& r = previous->viewport;
250 glViewport(r.left, r.top, r.right, r.bottom);
251 mOrthoMatrix.load(current->orthoMatrix);
252 }
253
Romain Guy8b55f372010-08-18 17:10:07 -0700254 mSaveCount--;
255 mSnapshot = previous;
256
Romain Guybd6b79b2010-06-26 00:13:53 -0700257 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700258 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700259 }
260
Romain Guy2542d192010-08-18 11:47:12 -0700261 if (restoreClip) {
262 setScissorFromClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700263 }
Romain Guy2542d192010-08-18 11:47:12 -0700264
265 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700266}
267
Romain Guyf6a11b82010-06-23 17:47:49 -0700268///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700269// Layers
270///////////////////////////////////////////////////////////////////////////////
271
272int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
273 const SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700274 const GLuint previousFbo = mSnapshot->fbo;
275 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700276
277 int alpha = 255;
278 SkXfermode::Mode mode;
279
280 if (p) {
281 alpha = p->getAlpha();
Romain Guya5aed0d2010-09-09 14:42:43 -0700282 if (!mExtensions.hasFramebufferFetch()) {
283 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
284 if (!isMode) {
285 // Assume SRC_OVER
286 mode = SkXfermode::kSrcOver_Mode;
287 }
288 } else {
289 mode = getXfermode(p->getXfermode());
Romain Guyd55a8612010-06-28 17:42:46 -0700290 }
291 } else {
292 mode = SkXfermode::kSrcOver_Mode;
293 }
294
Romain Guyeb993562010-10-05 18:14:38 -0700295 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guyd55a8612010-06-28 17:42:46 -0700296
297 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700298}
299
300int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
301 int alpha, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700302 if (alpha == 0xff) {
303 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700304 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700305 SkPaint paint;
306 paint.setAlpha(alpha);
307 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700308 }
Romain Guyd55a8612010-06-28 17:42:46 -0700309}
Romain Guybd6b79b2010-06-26 00:13:53 -0700310
Romain Guy1c740bc2010-09-13 18:00:09 -0700311/**
312 * Layers are viewed by Skia are slightly different than layers in image editing
313 * programs (for instance.) When a layer is created, previously created layers
314 * and the frame buffer still receive every drawing command. For instance, if a
315 * layer is created and a shape intersecting the bounds of the layers and the
316 * framebuffer is draw, the shape will be drawn on both (unless the layer was
317 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
318 *
319 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
320 * texture. Unfortunately, this is inefficient as it requires every primitive to
321 * be drawn n + 1 times, where n is the number of active layers. In practice this
322 * means, for every primitive:
323 * - Switch active frame buffer
324 * - Change viewport, clip and projection matrix
325 * - Issue the drawing
326 *
327 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
328 * To avoid this, layers are implemented in a different way here.
329 *
330 * This implementation relies on the frame buffer being at least RGBA 8888. When
331 * a layer is created, only a texture is created, not an FBO. The content of the
332 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700333 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700334 * buffer and drawing continues as normal. This technique therefore treats the
335 * frame buffer as a scratch buffer for the layers.
336 *
337 * To compose the layers back onto the frame buffer, each layer texture
338 * (containing the original frame buffer data) is drawn as a simple quad over
339 * the frame buffer. The trick is that the quad is set as the composition
340 * destination in the blending equation, and the frame buffer becomes the source
341 * of the composition.
342 *
343 * Drawing layers with an alpha value requires an extra step before composition.
344 * An empty quad is drawn over the layer's region in the frame buffer. This quad
345 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
346 * quad is used to multiply the colors in the frame buffer. This is achieved by
347 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
348 * GL_ZERO, GL_SRC_ALPHA.
349 *
350 * Because glCopyTexImage2D() can be slow, an alternative implementation might
351 * be use to draw a single clipped layer. The implementation described above
352 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700353 *
354 * (1) The frame buffer is actually not cleared right away. To allow the GPU
355 * to potentially optimize series of calls to glCopyTexImage2D, the frame
356 * buffer is left untouched until the first drawing operation. Only when
357 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700358 */
Romain Guyd55a8612010-06-28 17:42:46 -0700359bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700360 float right, float bottom, int alpha, SkXfermode::Mode mode,
361 int flags, GLuint previousFbo) {
362 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700363 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700364
Romain Guyeb993562010-10-05 18:14:38 -0700365 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
366
Romain Guyf607bdc2010-09-10 19:20:06 -0700367 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700368 Rect bounds(left, top, right, bottom);
Romain Guyeb993562010-10-05 18:14:38 -0700369 if (!fboLayer) {
370 mSnapshot->transform->mapRect(bounds);
371 // Layers only make sense if they are in the framebuffer's bounds
372 bounds.intersect(*mSnapshot->clipRect);
373 bounds.snapToPixelBoundaries();
374 }
Romain Guybf434112010-09-16 14:40:17 -0700375
Romain Guyb025b9c2010-09-16 14:16:48 -0700376 if (bounds.isEmpty() || bounds.getWidth() > mMaxTextureSize ||
377 bounds.getHeight() > mMaxTextureSize) {
378 return false;
379 }
Romain Guyf18fd992010-07-08 11:45:51 -0700380
Romain Guy0bb56672010-10-01 00:25:02 -0700381 glActiveTexture(GL_TEXTURE0);
382
Romain Guy8411f332010-09-13 17:27:57 -0700383 LayerSize size(bounds.getWidth(), bounds.getHeight());
Romain Guyf607bdc2010-09-10 19:20:06 -0700384 Layer* layer = mCaches.layerCache.get(size);
Romain Guydda57022010-07-06 11:39:32 -0700385 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700386 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700387 }
388
Romain Guydda57022010-07-06 11:39:32 -0700389 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700390 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700391 layer->layer.set(bounds);
Romain Guydda57022010-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 Guydda57022010-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 Guy0bb56672010-10-01 00:25:02 -0700409 setScissorFromClip();
Romain Guyf607bdc2010-09-10 19:20:06 -0700410
Romain Guyeb993562010-10-05 18:14:38 -0700411 // Bind texture to FBO
412 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
413 glBindTexture(GL_TEXTURE_2D, layer->texture);
414
415 // Initialize the texture if needed
416 if (layer->empty) {
417 layer->empty = false;
418 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.width, size.height, 0,
419 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
420 }
421
422 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
423 layer->texture, 0);
424
425 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
426 if (status != GL_FRAMEBUFFER_COMPLETE) {
427 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
428
429 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
430 glDeleteTextures(1, &layer->texture);
431 mCaches.fboCache.put(layer->fbo);
432
433 delete layer;
434
435 return false;
436 }
437
438 // Clear the FBO
439 glDisable(GL_SCISSOR_TEST);
440 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
441 glClear(GL_COLOR_BUFFER_BIT);
442 glEnable(GL_SCISSOR_TEST);
443
444 // Change the ortho projection
445 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
446 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
447 } else {
448 // Copy the framebuffer into the layer
449 glBindTexture(GL_TEXTURE_2D, layer->texture);
450
451 // TODO: Workaround for b/3054204
452 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
453 bounds.getWidth(), bounds.getHeight(), 0);
454
455 // TODO: Waiting for b/3054204 to be fixed
456 // if (layer->empty) {
457 // glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
458 // bounds.getWidth(), bounds.getHeight(), 0);
459 // layer->empty = false;
460 // } else {
461 // glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, mHeight - bounds.bottom,
462 // bounds.getWidth(), bounds.getHeight());
463 // }
464
465 // Enqueue the buffer coordinates to clear the corresponding region later
466 mLayers.push(new Rect(bounds));
467 }
Romain Guyf86ef572010-07-01 11:05:42 -0700468
Romain Guyd55a8612010-06-28 17:42:46 -0700469 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700470}
471
Romain Guy1c740bc2010-09-13 18:00:09 -0700472/**
473 * Read the documentation of createLayer() before doing anything in this method.
474 */
Romain Guy1d83e192010-08-17 11:37:00 -0700475void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
476 if (!current->layer) {
477 LOGE("Attempting to compose a layer that does not exist");
478 return;
479 }
480
Romain Guyeb993562010-10-05 18:14:38 -0700481 const bool fboLayer = current->flags & SkCanvas::kClipToLayer_SaveFlag;
482
483 if (fboLayer) {
484 // Unbind current FBO and restore previous one
485 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
486 }
487
Romain Guy1d83e192010-08-17 11:37:00 -0700488 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700489 const Rect& clip = *previous->clipRect;
Romain Guyeb993562010-10-05 18:14:38 -0700490 glScissor(clip.left, previous->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700491
492 Layer* layer = current->layer;
493 const Rect& rect = layer->layer;
494
Romain Guyeb993562010-10-05 18:14:38 -0700495 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700496 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700497 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guyf607bdc2010-09-10 19:20:06 -0700498 }
499
500 // Layers are already drawn with a top-left origin, don't flip the texture
Romain Guy8b55f372010-08-18 17:10:07 -0700501 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
502
Romain Guyeb993562010-10-05 18:14:38 -0700503 if (fboLayer) {
504 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
505 layer->texture, layer->alpha / 255.0f, layer->mode, layer->blend);
506 } else {
507 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
508 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
509 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, true, true);
510 }
Romain Guy1d83e192010-08-17 11:37:00 -0700511
Romain Guy8b55f372010-08-18 17:10:07 -0700512 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
513
Romain Guyeb993562010-10-05 18:14:38 -0700514 if (fboLayer) {
515 // Detach the texture from the FBO
516 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
517 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
518 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
519
520 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
521 mCaches.fboCache.put(current->fbo);
522 }
523
Romain Guy1d83e192010-08-17 11:37:00 -0700524 LayerSize size(rect.getWidth(), rect.getHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700525 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guyfb8b7632010-08-23 21:05:08 -0700526 if (!mCaches.layerCache.put(size, layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700527 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700528 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700529 delete layer;
530 }
531}
532
Romain Guy86942302010-09-12 13:02:16 -0700533void OpenGLRenderer::clearLayerRegions() {
534 if (mLayers.size() == 0) return;
535
536 for (uint32_t i = 0; i < mLayers.size(); i++) {
537 Rect* bounds = mLayers.itemAt(i);
538
539 // Clear the framebuffer where the layer will draw
540 glScissor(bounds->left, mHeight - bounds->bottom,
541 bounds->getWidth(), bounds->getHeight());
542 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
543 glClear(GL_COLOR_BUFFER_BIT);
544
545 delete bounds;
546 }
547 mLayers.clear();
548
549 // Restore the clip
550 setScissorFromClip();
551}
552
Romain Guybd6b79b2010-06-26 00:13:53 -0700553///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700554// Transforms
555///////////////////////////////////////////////////////////////////////////////
556
557void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700558 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700559}
560
561void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700562 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700563}
564
565void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700566 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700567}
568
569void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700570 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700571}
572
573void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700574 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700575}
576
577void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700578 mat4 m(*matrix);
Romain Guy8aef54f2010-09-01 15:13:49 -0700579 mSnapshot->transform->multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700580}
581
582///////////////////////////////////////////////////////////////////////////////
583// Clipping
584///////////////////////////////////////////////////////////////////////////////
585
Romain Guybb9524b2010-06-22 18:56:38 -0700586void OpenGLRenderer::setScissorFromClip() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700587 const Rect& clip = *mSnapshot->clipRect;
Romain Guyeb993562010-10-05 18:14:38 -0700588 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700589}
590
591const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700592 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700593}
594
Romain Guyc7d53492010-06-25 13:41:57 -0700595bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy1d83e192010-08-17 11:37:00 -0700596 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700597 mSnapshot->transform->mapRect(r);
598 return !mSnapshot->clipRect->intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700599}
600
Romain Guy079ba2c2010-07-16 14:12:24 -0700601bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
602 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700603 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700604 setScissorFromClip();
605 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700606 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700607}
608
Romain Guyf6a11b82010-06-23 17:47:49 -0700609///////////////////////////////////////////////////////////////////////////////
610// Drawing
611///////////////////////////////////////////////////////////////////////////////
612
Romain Guyc1396e92010-06-30 17:56:19 -0700613void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700614 const float right = left + bitmap->width();
615 const float bottom = top + bitmap->height();
616
617 if (quickReject(left, top, right, bottom)) {
618 return;
619 }
620
Romain Guy61c8c9c2010-08-09 20:48:09 -0700621 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700622 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700623 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700624 const AutoTexture autoCleanup(texture);
625
Romain Guy6926c722010-07-12 20:20:03 -0700626 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700627}
628
Romain Guyf86ef572010-07-01 11:05:42 -0700629void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
630 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
631 const mat4 transform(*matrix);
632 transform.mapRect(r);
633
Romain Guy6926c722010-07-12 20:20:03 -0700634 if (quickReject(r.left, r.top, r.right, r.bottom)) {
635 return;
636 }
637
Romain Guy61c8c9c2010-08-09 20:48:09 -0700638 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700639 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700640 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700641 const AutoTexture autoCleanup(texture);
642
Romain Guy82ba8142010-07-09 13:25:56 -0700643 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700644}
645
Romain Guy8ba548f2010-06-30 19:21:21 -0700646void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
647 float srcLeft, float srcTop, float srcRight, float srcBottom,
648 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700649 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700650 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
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 Guy9cccc2b2010-08-07 23:46:15 -0700656 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700657 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700658
Romain Guy8ba548f2010-06-30 19:21:21 -0700659 const float width = texture->width;
660 const float height = texture->height;
661
662 const float u1 = srcLeft / width;
663 const float v1 = srcTop / height;
664 const float u2 = srcRight / width;
665 const float v2 = srcBottom / height;
666
667 resetDrawTextureTexCoords(u1, v1, u2, v2);
668
Romain Guy82ba8142010-07-09 13:25:56 -0700669 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700670
671 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
672}
673
Romain Guy4aa90572010-09-26 18:40:37 -0700674void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
675 uint32_t width, uint32_t height, float left, float top, float right, float bottom,
676 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700677 if (quickReject(left, top, right, bottom)) {
678 return;
679 }
680
Romain Guy61c8c9c2010-08-09 20:48:09 -0700681 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700682 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700683 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700684 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700685
686 int alpha;
687 SkXfermode::Mode mode;
688 getAlphaAndMode(paint, &alpha, &mode);
689
Romain Guy4aa90572010-09-26 18:40:37 -0700690 Patch* mesh = mCaches.patchCache.get(width, height);
Romain Guy759ea802010-09-16 20:49:46 -0700691 mesh->updateVertices(bitmap->width(), bitmap->height(),left, top, right, bottom,
Romain Guy4aa90572010-09-26 18:40:37 -0700692 xDivs, yDivs, width, height);
Romain Guyf7f93552010-07-08 19:17:03 -0700693
694 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
695 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700696 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
697 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy6820ac82010-09-15 18:11:50 -0700698 &mesh->vertices[0].texture[0], GL_TRIANGLES, mesh->verticesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700699}
700
Romain Guy759ea802010-09-16 20:49:46 -0700701void OpenGLRenderer::drawLines(float* points, int count, const SkPaint* paint) {
702 int alpha;
703 SkXfermode::Mode mode;
704 getAlphaAndMode(paint, &alpha, &mode);
705
706 uint32_t color = paint->getColor();
707 const GLfloat a = alpha / 255.0f;
708 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
709 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
710 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
711
Romain Guyc95c8d62010-09-17 15:31:32 -0700712 const bool isAA = paint->isAntiAlias();
713 if (isAA) {
714 GLuint textureUnit = 0;
Romain Guy29d89972010-09-22 16:10:57 -0700715 setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
716 mode, false, true, mCaches.line.getVertices(), mCaches.line.getTexCoords());
Romain Guyc95c8d62010-09-17 15:31:32 -0700717 } else {
718 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false);
719 }
720
721 const float strokeWidth = paint->getStrokeWidth();
Romain Guy29d89972010-09-22 16:10:57 -0700722 const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount;
Romain Guyc95c8d62010-09-17 15:31:32 -0700723 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
Romain Guy759ea802010-09-16 20:49:46 -0700724
725 for (int i = 0; i < count; i += 4) {
726 float tx = 0.0f;
727 float ty = 0.0f;
728
Romain Guyc95c8d62010-09-17 15:31:32 -0700729 if (isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700730 mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3],
Romain Guyc95c8d62010-09-17 15:31:32 -0700731 strokeWidth, tx, ty);
732 } else {
Romain Guyb5ab4172010-09-17 15:36:56 -0700733 ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f;
Romain Guyc95c8d62010-09-17 15:31:32 -0700734 }
Romain Guy759ea802010-09-16 20:49:46 -0700735
736 const float dx = points[i + 2] - points[i];
737 const float dy = points[i + 3] - points[i + 1];
738 const float mag = sqrtf(dx * dx + dy * dy);
739 const float angle = acos(dx / mag);
740
741 mModelView.loadTranslate(points[i], points[i + 1], 0.0f);
742 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) {
743 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f);
744 }
745 mModelView.translate(tx, ty, 0.0f);
Romain Guyc95c8d62010-09-17 15:31:32 -0700746 if (!isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700747 float length = mCaches.line.getLength(points[i], points[i + 1],
748 points[i + 2], points[i + 3]);
Romain Guyc95c8d62010-09-17 15:31:32 -0700749 mModelView.scale(length, strokeWidth, 1.0f);
750 }
Romain Guy759ea802010-09-16 20:49:46 -0700751 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
752
753 if (mShader) {
754 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot);
755 }
756
Romain Guyc95c8d62010-09-17 15:31:32 -0700757 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy759ea802010-09-16 20:49:46 -0700758 }
759
Romain Guy29d89972010-09-22 16:10:57 -0700760 if (isAA) {
761 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
762 }
Romain Guy759ea802010-09-16 20:49:46 -0700763}
764
Romain Guy85bf02f2010-06-22 13:11:24 -0700765void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700766 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700767 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700768}
Romain Guy9d5316e2010-06-24 19:30:36 -0700769
Romain Guybd6b79b2010-06-26 00:13:53 -0700770void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700771 if (quickReject(left, top, right, bottom)) {
772 return;
773 }
774
Romain Guy026c5e162010-06-28 17:12:22 -0700775 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700776 if (!mExtensions.hasFramebufferFetch()) {
777 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
778 if (!isMode) {
779 // Assume SRC_OVER
780 mode = SkXfermode::kSrcOver_Mode;
781 }
782 } else {
783 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700784 }
785
786 // Skia draws using the color's alpha channel if < 255
787 // Otherwise, it uses the paint's alpha
788 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700789 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700790 color |= p->getAlpha() << 24;
791 }
792
793 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700794}
Romain Guy9d5316e2010-06-24 19:30:36 -0700795
Romain Guye8e62a42010-07-23 18:55:21 -0700796void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
797 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700798 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700799 return;
800 }
Romain Guye2d345e2010-09-24 18:39:22 -0700801
Romain Guyf607bdc2010-09-10 19:20:06 -0700802 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700803
Romain Guya674ab72010-08-10 17:26:42 -0700804 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700805 switch (paint->getTextAlign()) {
806 case SkPaint::kCenter_Align:
807 length = paint->measureText(text, bytesCount);
808 x -= length / 2.0f;
809 break;
810 case SkPaint::kRight_Align:
811 length = paint->measureText(text, bytesCount);
812 x -= length;
813 break;
814 default:
815 break;
816 }
817
Romain Guy694b5192010-07-21 21:33:20 -0700818 int alpha;
819 SkXfermode::Mode mode;
820 getAlphaAndMode(paint, &alpha, &mode);
821
Romain Guy2542d192010-08-18 11:47:12 -0700822 uint32_t color = paint->getColor();
823 const GLfloat a = alpha / 255.0f;
824 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
825 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
826 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
827
Romain Guyb45c0c92010-08-26 20:35:23 -0700828 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
829 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700830 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -0700831
Romain Guy1e45aae2010-08-13 19:39:53 -0700832 if (mHasShadow) {
833 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700834 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700835 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700836 count, mShadowRadius);
837 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700838
Romain Guy2542d192010-08-18 11:47:12 -0700839 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700840
841 // Draw the mesh
842 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700843 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700844 }
845
Romain Guy06f96e22010-07-30 19:18:16 -0700846 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700847 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700848
Romain Guye8cb9c142010-10-04 14:14:11 -0700849 // Assume that the modelView matrix does not force scales, rotates, etc.
850 const bool linearFilter = mSnapshot->transform->changesBounds();
851 setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit,
852 x, y, r, g, b, a, mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700853
Romain Guy09147fb2010-07-22 13:08:20 -0700854 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy86942302010-09-12 13:02:16 -0700855 clearLayerRegions();
Romain Guyb45c0c92010-08-26 20:35:23 -0700856 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700857
858 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700859 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700860
Romain Guy0a417492010-08-16 20:26:20 -0700861 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -0700862}
863
Romain Guy7fbcc042010-08-04 15:40:07 -0700864void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
865 GLuint textureUnit = 0;
866 glActiveTexture(gTextureUnits[textureUnit]);
867
Romain Guyfb8b7632010-08-23 21:05:08 -0700868 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700869 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700870 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700871
Romain Guy8b55f372010-08-18 17:10:07 -0700872 const float x = texture->left - texture->offset;
873 const float y = texture->top - texture->offset;
874
875 if (quickReject(x, y, x + texture->width, y + texture->height)) {
876 return;
877 }
878
Romain Guy7fbcc042010-08-04 15:40:07 -0700879 int alpha;
880 SkXfermode::Mode mode;
881 getAlphaAndMode(paint, &alpha, &mode);
882
883 uint32_t color = paint->getColor();
884 const GLfloat a = alpha / 255.0f;
885 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
886 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
887 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
888
Romain Guy0a417492010-08-16 20:26:20 -0700889 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
890
Romain Guy86942302010-09-12 13:02:16 -0700891 clearLayerRegions();
892
Romain Guy0a417492010-08-16 20:26:20 -0700893 // Draw the mesh
894 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700895 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700896}
897
Romain Guy6926c722010-07-12 20:20:03 -0700898///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700899// Shaders
900///////////////////////////////////////////////////////////////////////////////
901
902void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700903 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700904}
905
Romain Guy06f96e22010-07-30 19:18:16 -0700906void OpenGLRenderer::setupShader(SkiaShader* shader) {
907 mShader = shader;
908 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700909 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700910 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700911}
912
Romain Guyd27977d2010-07-14 19:18:51 -0700913///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700914// Color filters
915///////////////////////////////////////////////////////////////////////////////
916
917void OpenGLRenderer::resetColorFilter() {
918 mColorFilter = NULL;
919}
920
921void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
922 mColorFilter = filter;
923}
924
925///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700926// Drop shadow
927///////////////////////////////////////////////////////////////////////////////
928
929void OpenGLRenderer::resetShadow() {
930 mHasShadow = false;
931}
932
933void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
934 mHasShadow = true;
935 mShadowRadius = radius;
936 mShadowDx = dx;
937 mShadowDy = dy;
938 mShadowColor = color;
939}
940
941///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700942// Drawing implementation
943///////////////////////////////////////////////////////////////////////////////
944
Romain Guy0a417492010-08-16 20:26:20 -0700945void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700946 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700947 const float sx = x - texture->left + mShadowDx;
948 const float sy = y - texture->top + mShadowDy;
949
Romain Guy2542d192010-08-18 11:47:12 -0700950 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
951 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700952 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
953 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
954 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
955
956 GLuint textureUnit = 0;
957 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
958}
959
960void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
961 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
962 bool transforms, bool applyFilters) {
963 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -0700964 x, y, r, g, b, a, mode, transforms, applyFilters,
965 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
Romain Guy0a417492010-08-16 20:26:20 -0700966}
967
968void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
969 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
970 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy759ea802010-09-16 20:49:46 -0700971 setupTextureAlpha8(texture, width, height, textureUnit,
972 x, y, r, g, b, a, mode, transforms, applyFilters,
973 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
974}
975
976void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
977 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
978 SkXfermode::Mode mode, bool transforms, bool applyFilters,
979 GLvoid* vertices, GLvoid* texCoords) {
Romain Guy0a417492010-08-16 20:26:20 -0700980 // Describe the required shaders
981 ProgramDescription description;
982 description.hasTexture = true;
983 description.hasAlpha8Texture = true;
984
985 if (applyFilters) {
986 if (mShader) {
987 mShader->describe(description, mExtensions);
988 }
989 if (mColorFilter) {
990 mColorFilter->describe(description, mExtensions);
991 }
992 }
993
Romain Guya5aed0d2010-09-09 14:42:43 -0700994 // Setup the blending mode
995 chooseBlending(true, mode, description);
996
Romain Guy0a417492010-08-16 20:26:20 -0700997 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700998 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700999
Romain Guy0a417492010-08-16 20:26:20 -07001000 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -07001001 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001002
Romain Guyfb8b7632010-08-23 21:05:08 -07001003 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -07001004 glEnableVertexAttribArray(texCoordsSlot);
1005
1006 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -07001007 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -07001008 gMeshStride, vertices);
Romain Guy0a417492010-08-16 20:26:20 -07001009 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -07001010 gMeshStride, texCoords);
Romain Guy0a417492010-08-16 20:26:20 -07001011
1012 // Setup uniforms
1013 if (transforms) {
1014 mModelView.loadTranslate(x, y, 0.0f);
1015 mModelView.scale(width, height, 1.0f);
1016 } else {
1017 mModelView.loadIdentity();
1018 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001019 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyfb8b7632010-08-23 21:05:08 -07001020 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -07001021
1022 textureUnit++;
1023 if (applyFilters) {
1024 // Setup attributes and uniforms required by the shaders
1025 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001026 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001027 }
1028 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001029 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -07001030 }
1031 }
1032}
1033
Romain Guyf607bdc2010-09-10 19:20:06 -07001034// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001035#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1036#define kStdUnderline_Offset (1.0f / 9.0f)
1037#define kStdUnderline_Thickness (1.0f / 18.0f)
1038
1039void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1040 float x, float y, SkPaint* paint) {
1041 // Handle underline and strike-through
1042 uint32_t flags = paint->getFlags();
1043 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1044 float underlineWidth = length;
1045 // If length is > 0.0f, we already measured the text for the text alignment
1046 if (length <= 0.0f) {
1047 underlineWidth = paint->measureText(text, bytesCount);
1048 }
1049
1050 float offsetX = 0;
1051 switch (paint->getTextAlign()) {
1052 case SkPaint::kCenter_Align:
1053 offsetX = underlineWidth * 0.5f;
1054 break;
1055 case SkPaint::kRight_Align:
1056 offsetX = underlineWidth;
1057 break;
1058 default:
1059 break;
1060 }
1061
1062 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001063 const float textSize = paint->getTextSize();
1064 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -07001065
Romain Guye20ecbd2010-09-22 19:49:04 -07001066 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001067 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001068
1069 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
1070 float points[pointsCount];
1071 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001072
1073 if (flags & SkPaint::kUnderlineText_Flag) {
1074 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001075 points[currentPoint++] = left;
1076 points[currentPoint++] = top;
1077 points[currentPoint++] = left + underlineWidth;
1078 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001079 }
1080
1081 if (flags & SkPaint::kStrikeThruText_Flag) {
1082 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001083 points[currentPoint++] = left;
1084 points[currentPoint++] = top;
1085 points[currentPoint++] = left + underlineWidth;
1086 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001087 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001088
1089 SkPaint linesPaint(*paint);
1090 linesPaint.setStrokeWidth(strokeWidth);
1091
1092 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001093 }
1094 }
1095}
1096
Romain Guy026c5e162010-06-28 17:12:22 -07001097void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001098 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001099 clearLayerRegions();
1100
Romain Guyd27977d2010-07-14 19:18:51 -07001101 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001102 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001103 color |= 0x00ffffff;
1104 }
1105
1106 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -07001107 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -07001108 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -07001109 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1110 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1111 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1112
Romain Guyc95c8d62010-09-17 15:31:32 -07001113 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform);
1114
1115 // Draw the mesh
1116 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1117}
1118
1119void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom,
1120 float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy06f96e22010-07-30 19:18:16 -07001121 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -07001122
Romain Guy06f96e22010-07-30 19:18:16 -07001123 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -07001124 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -07001125 if (mShader) {
1126 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -07001127 }
Romain Guydb1938e2010-08-02 18:50:22 -07001128 if (mColorFilter) {
1129 mColorFilter->describe(description, mExtensions);
1130 }
Romain Guyd27977d2010-07-14 19:18:51 -07001131
Romain Guy1c740bc2010-09-13 18:00:09 -07001132 // Setup the blending mode
Romain Guyc95c8d62010-09-17 15:31:32 -07001133 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -07001134
Romain Guy06f96e22010-07-30 19:18:16 -07001135 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001136 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -07001137
1138 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -07001139 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -07001140 gMeshStride, &mMeshVertices[0].position[0]);
1141
1142 // Setup uniforms
1143 mModelView.loadTranslate(left, top, 0.0f);
1144 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -07001145 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001146 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -07001147 } else {
1148 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -07001149 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -07001150 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001151 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -07001152
Romain Guy06f96e22010-07-30 19:18:16 -07001153 // Setup attributes and uniforms required by the shaders
1154 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001155 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001156 }
Romain Guydb1938e2010-08-02 18:50:22 -07001157 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001158 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001159 }
Romain Guyd27977d2010-07-14 19:18:51 -07001160}
1161
Romain Guy82ba8142010-07-09 13:25:56 -07001162void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001163 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001164 int alpha;
1165 SkXfermode::Mode mode;
1166 getAlphaAndMode(paint, &alpha, &mode);
1167
Romain Guy6820ac82010-09-15 18:11:50 -07001168 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1169 texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1170 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -07001171}
1172
Romain Guybd6b79b2010-06-26 00:13:53 -07001173void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001174 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1175 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001176 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1177 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001178}
1179
1180void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001181 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001182 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guyf607bdc2010-09-10 19:20:06 -07001183 bool swapSrcDst, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001184 clearLayerRegions();
1185
Romain Guy889f8d12010-07-29 14:37:42 -07001186 ProgramDescription description;
1187 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -07001188 if (mColorFilter) {
1189 mColorFilter->describe(description, mExtensions);
1190 }
Romain Guy889f8d12010-07-29 14:37:42 -07001191
Romain Guybd6b79b2010-06-26 00:13:53 -07001192 mModelView.loadTranslate(left, top, 0.0f);
1193 mModelView.scale(right - left, bottom - top, 1.0f);
1194
Romain Guyf607bdc2010-09-10 19:20:06 -07001195 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001196
Romain Guyfb8b7632010-08-23 21:05:08 -07001197 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001198 if (!ignoreTransform) {
1199 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1200 } else {
1201 mat4 m;
1202 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
1203 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001204
Romain Guy889f8d12010-07-29 14:37:42 -07001205 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -07001206 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001207 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001208
Romain Guya9794742010-07-13 11:37:54 -07001209 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -07001210 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -07001211
Romain Guy889f8d12010-07-29 14:37:42 -07001212 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001213 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001214 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -07001215 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001216 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001217 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001218
Romain Guydb1938e2010-08-02 18:50:22 -07001219 // Color filter
1220 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001221 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001222 }
1223
Romain Guy6820ac82010-09-15 18:11:50 -07001224 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001225 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001226}
1227
Romain Guya5aed0d2010-09-09 14:42:43 -07001228void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001229 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001230 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1231 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001232 if (mode < SkXfermode::kPlus_Mode) {
1233 if (!mCaches.blend) {
1234 glEnable(GL_BLEND);
1235 }
Romain Guy82ba8142010-07-09 13:25:56 -07001236
Romain Guyf607bdc2010-09-10 19:20:06 -07001237 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1238 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001239
Romain Guya5aed0d2010-09-09 14:42:43 -07001240 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1241 glBlendFunc(sourceMode, destMode);
1242 mCaches.lastSrcMode = sourceMode;
1243 mCaches.lastDstMode = destMode;
1244 }
1245 } else {
1246 // These blend modes are not supported by OpenGL directly and have
1247 // to be implemented using shaders. Since the shader will perform
1248 // the blending, turn blending off here
1249 if (mExtensions.hasFramebufferFetch()) {
1250 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001251 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001252 }
1253
1254 if (mCaches.blend) {
1255 glDisable(GL_BLEND);
1256 }
1257 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001258 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001259 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001260 glDisable(GL_BLEND);
1261 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001262 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001263}
1264
Romain Guy889f8d12010-07-29 14:37:42 -07001265bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001266 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001267 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001268 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001269 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001270 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001271 }
Romain Guy6926c722010-07-12 20:20:03 -07001272 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001273}
1274
Romain Guy026c5e162010-06-28 17:12:22 -07001275void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001276 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001277 TextureVertex::setUV(v++, u1, v1);
1278 TextureVertex::setUV(v++, u2, v1);
1279 TextureVertex::setUV(v++, u1, v2);
1280 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001281}
1282
1283void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1284 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001285 if (!mExtensions.hasFramebufferFetch()) {
1286 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1287 if (!isMode) {
1288 // Assume SRC_OVER
1289 *mode = SkXfermode::kSrcOver_Mode;
1290 }
1291 } else {
1292 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001293 }
1294
1295 // Skia draws using the color's alpha channel if < 255
1296 // Otherwise, it uses the paint's alpha
1297 int color = paint->getColor();
1298 *alpha = (color >> 24) & 0xFF;
1299 if (*alpha == 255) {
1300 *alpha = paint->getAlpha();
1301 }
1302 } else {
1303 *mode = SkXfermode::kSrcOver_Mode;
1304 *alpha = 255;
1305 }
Romain Guy026c5e162010-06-28 17:12:22 -07001306}
1307
Romain Guya5aed0d2010-09-09 14:42:43 -07001308SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1309 if (mode == NULL) {
1310 return SkXfermode::kSrcOver_Mode;
1311 }
1312 return mode->fMode;
1313}
1314
Romain Guy889f8d12010-07-29 14:37:42 -07001315void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1316 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001317 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001318 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1319 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1320}
1321
Romain Guy9d5316e2010-06-24 19:30:36 -07001322}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001323}; // namespace android