blob: dbd499e2f87f1fab6cc29ab477db3e7ddcfd7385 [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 Guydda570202010-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 Guy85bf02f2010-06-22 13:11:24 -0700116 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700117
Romain Guy06f96e22010-07-30 19:18:16 -0700118 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700119 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700120 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700121
Romain Guyac670c02010-07-27 17:39:27 -0700122 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
123
Romain Guyae5575b2010-07-29 18:48:04 -0700124 mFirstSnapshot = new Snapshot;
125
126 GLint maxTextureUnits;
127 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
128 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Romain Guy889f8d12010-07-29 14:37:42 -0700129 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
130 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700131
132 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
Romain Guye4d01122010-06-16 18:44:05 -0700133}
134
Romain Guy85bf02f2010-06-22 13:11:24 -0700135OpenGLRenderer::~OpenGLRenderer() {
136 LOGD("Destroy OpenGLRenderer");
Romain Guy29d89972010-09-22 16:10:57 -0700137 // The context has already been destroyed at this point, do not call
138 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700139}
140
Romain Guyf6a11b82010-06-23 17:47:49 -0700141///////////////////////////////////////////////////////////////////////////////
142// Setup
143///////////////////////////////////////////////////////////////////////////////
144
Romain Guy85bf02f2010-06-22 13:11:24 -0700145void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700146 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700147 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700148
149 mWidth = width;
150 mHeight = 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 Guyf607bdc2010-09-10 19:20:06 -0700191 glViewport(0, 0, mWidth, mHeight);
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 Guybb9524b2010-06-22 18:56:38 -0700243
Romain Guybd6b79b2010-06-26 00:13:53 -0700244 sp<Snapshot> current = mSnapshot;
245 sp<Snapshot> previous = mSnapshot->previous;
246
Romain Guy8b55f372010-08-18 17:10:07 -0700247 mSaveCount--;
248 mSnapshot = previous;
249
Romain Guybd6b79b2010-06-26 00:13:53 -0700250 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700251 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700252 }
253
Romain Guy2542d192010-08-18 11:47:12 -0700254 if (restoreClip) {
255 setScissorFromClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700256 }
Romain Guy2542d192010-08-18 11:47:12 -0700257
258 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700259}
260
Romain Guyf6a11b82010-06-23 17:47:49 -0700261///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700262// Layers
263///////////////////////////////////////////////////////////////////////////////
264
265int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
266 const SkPaint* p, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700267 int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700268
269 int alpha = 255;
270 SkXfermode::Mode mode;
271
272 if (p) {
273 alpha = p->getAlpha();
Romain Guya5aed0d2010-09-09 14:42:43 -0700274 if (!mExtensions.hasFramebufferFetch()) {
275 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
276 if (!isMode) {
277 // Assume SRC_OVER
278 mode = SkXfermode::kSrcOver_Mode;
279 }
280 } else {
281 mode = getXfermode(p->getXfermode());
Romain Guyd55a8612010-06-28 17:42:46 -0700282 }
283 } else {
284 mode = SkXfermode::kSrcOver_Mode;
285 }
286
Romain Guyf607bdc2010-09-10 19:20:06 -0700287 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700288
289 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700290}
291
292int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
293 int alpha, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700294 if (alpha == 0xff) {
295 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700296 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700297 SkPaint paint;
298 paint.setAlpha(alpha);
299 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700300 }
Romain Guyd55a8612010-06-28 17:42:46 -0700301}
Romain Guybd6b79b2010-06-26 00:13:53 -0700302
Romain Guy1c740bc2010-09-13 18:00:09 -0700303/**
304 * Layers are viewed by Skia are slightly different than layers in image editing
305 * programs (for instance.) When a layer is created, previously created layers
306 * and the frame buffer still receive every drawing command. For instance, if a
307 * layer is created and a shape intersecting the bounds of the layers and the
308 * framebuffer is draw, the shape will be drawn on both (unless the layer was
309 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
310 *
311 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
312 * texture. Unfortunately, this is inefficient as it requires every primitive to
313 * be drawn n + 1 times, where n is the number of active layers. In practice this
314 * means, for every primitive:
315 * - Switch active frame buffer
316 * - Change viewport, clip and projection matrix
317 * - Issue the drawing
318 *
319 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
320 * To avoid this, layers are implemented in a different way here.
321 *
322 * This implementation relies on the frame buffer being at least RGBA 8888. When
323 * a layer is created, only a texture is created, not an FBO. The content of the
324 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700325 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700326 * buffer and drawing continues as normal. This technique therefore treats the
327 * frame buffer as a scratch buffer for the layers.
328 *
329 * To compose the layers back onto the frame buffer, each layer texture
330 * (containing the original frame buffer data) is drawn as a simple quad over
331 * the frame buffer. The trick is that the quad is set as the composition
332 * destination in the blending equation, and the frame buffer becomes the source
333 * of the composition.
334 *
335 * Drawing layers with an alpha value requires an extra step before composition.
336 * An empty quad is drawn over the layer's region in the frame buffer. This quad
337 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
338 * quad is used to multiply the colors in the frame buffer. This is achieved by
339 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
340 * GL_ZERO, GL_SRC_ALPHA.
341 *
342 * Because glCopyTexImage2D() can be slow, an alternative implementation might
343 * be use to draw a single clipped layer. The implementation described above
344 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700345 *
346 * (1) The frame buffer is actually not cleared right away. To allow the GPU
347 * to potentially optimize series of calls to glCopyTexImage2D, the frame
348 * buffer is left untouched until the first drawing operation. Only when
349 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700350 */
Romain Guyd55a8612010-06-28 17:42:46 -0700351bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
352 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700353 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700354 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700355
Romain Guyf607bdc2010-09-10 19:20:06 -0700356 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700357 Rect bounds(left, top, right, bottom);
Romain Guyf607bdc2010-09-10 19:20:06 -0700358 mSnapshot->transform->mapRect(bounds);
Romain Guy8aef54f2010-09-01 15:13:49 -0700359
Romain Guy8411f332010-09-13 17:27:57 -0700360 // Layers only make sense if they are in the framebuffer's bounds
361 bounds.intersect(*mSnapshot->clipRect);
Romain Guybf434112010-09-16 14:40:17 -0700362 bounds.snapToPixelBoundaries();
363
Romain Guyb025b9c2010-09-16 14:16:48 -0700364 if (bounds.isEmpty() || bounds.getWidth() > mMaxTextureSize ||
365 bounds.getHeight() > mMaxTextureSize) {
366 return false;
367 }
Romain Guyf18fd992010-07-08 11:45:51 -0700368
Romain Guy8411f332010-09-13 17:27:57 -0700369 LayerSize size(bounds.getWidth(), bounds.getHeight());
Romain Guyf607bdc2010-09-10 19:20:06 -0700370 Layer* layer = mCaches.layerCache.get(size);
Romain Guydda570202010-07-06 11:39:32 -0700371 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700372 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700373 }
374
Romain Guydda570202010-07-06 11:39:32 -0700375 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700376 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700377 layer->layer.set(bounds);
Romain Guydda570202010-07-06 11:39:32 -0700378
Romain Guy8fb95422010-08-17 18:38:51 -0700379 // Save the layer in the snapshot
380 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700381 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700382
Romain Guyf607bdc2010-09-10 19:20:06 -0700383 // Copy the framebuffer into the layer
384 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy38c85b92010-09-22 22:48:20 -0700385
386 if (layer->empty) {
387 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
388 bounds.getWidth(), bounds.getHeight(), 0);
389 layer->empty = false;
390 } else {
391 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, mHeight - bounds.bottom,
392 bounds.getWidth(), bounds.getHeight());
393 }
Romain Guyf607bdc2010-09-10 19:20:06 -0700394
Romain Guyf607bdc2010-09-10 19:20:06 -0700395 if (flags & SkCanvas::kClipToLayer_SaveFlag) {
Romain Guy86942302010-09-12 13:02:16 -0700396 if (mSnapshot->clipTransformed(bounds)) setScissorFromClip();
Romain Guyf607bdc2010-09-10 19:20:06 -0700397 }
398
Romain Guy86942302010-09-12 13:02:16 -0700399 // Enqueue the buffer coordinates to clear the corresponding region later
400 mLayers.push(new Rect(bounds));
Romain Guyf86ef572010-07-01 11:05:42 -0700401
Romain Guyd55a8612010-06-28 17:42:46 -0700402 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700403}
404
Romain Guy1c740bc2010-09-13 18:00:09 -0700405/**
406 * Read the documentation of createLayer() before doing anything in this method.
407 */
Romain Guy1d83e192010-08-17 11:37:00 -0700408void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
409 if (!current->layer) {
410 LOGE("Attempting to compose a layer that does not exist");
411 return;
412 }
413
Romain Guy1d83e192010-08-17 11:37:00 -0700414 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700415 const Rect& clip = *previous->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700416 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700417
418 Layer* layer = current->layer;
419 const Rect& rect = layer->layer;
420
Romain Guyf607bdc2010-09-10 19:20:06 -0700421 if (layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700422 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700423 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guyf607bdc2010-09-10 19:20:06 -0700424 }
425
426 // Layers are already drawn with a top-left origin, don't flip the texture
Romain Guy8b55f372010-08-18 17:10:07 -0700427 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
428
Romain Guyf607bdc2010-09-10 19:20:06 -0700429 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
430 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
Romain Guy6820ac82010-09-15 18:11:50 -0700431 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, true, true);
Romain Guy1d83e192010-08-17 11:37:00 -0700432
Romain Guy8b55f372010-08-18 17:10:07 -0700433 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
434
Romain Guy1d83e192010-08-17 11:37:00 -0700435 LayerSize size(rect.getWidth(), rect.getHeight());
436 // Failing to add the layer to the cache should happen only if the
437 // layer is too large
Romain Guyfb8b7632010-08-23 21:05:08 -0700438 if (!mCaches.layerCache.put(size, layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700439 LAYER_LOGD("Deleting layer");
440
Romain Guy1d83e192010-08-17 11:37:00 -0700441 glDeleteTextures(1, &layer->texture);
442
443 delete layer;
444 }
445}
446
Romain Guy86942302010-09-12 13:02:16 -0700447void OpenGLRenderer::clearLayerRegions() {
448 if (mLayers.size() == 0) return;
449
450 for (uint32_t i = 0; i < mLayers.size(); i++) {
451 Rect* bounds = mLayers.itemAt(i);
452
453 // Clear the framebuffer where the layer will draw
454 glScissor(bounds->left, mHeight - bounds->bottom,
455 bounds->getWidth(), bounds->getHeight());
456 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
457 glClear(GL_COLOR_BUFFER_BIT);
458
459 delete bounds;
460 }
461 mLayers.clear();
462
463 // Restore the clip
464 setScissorFromClip();
465}
466
Romain Guybd6b79b2010-06-26 00:13:53 -0700467///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700468// Transforms
469///////////////////////////////////////////////////////////////////////////////
470
471void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700472 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700473}
474
475void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700476 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700477}
478
479void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700480 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700481}
482
483void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700484 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700485}
486
487void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700488 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700489}
490
491void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700492 mat4 m(*matrix);
Romain Guy8aef54f2010-09-01 15:13:49 -0700493 mSnapshot->transform->multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700494}
495
496///////////////////////////////////////////////////////////////////////////////
497// Clipping
498///////////////////////////////////////////////////////////////////////////////
499
Romain Guybb9524b2010-06-22 18:56:38 -0700500void OpenGLRenderer::setScissorFromClip() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700501 const Rect& clip = *mSnapshot->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700502 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700503}
504
505const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700506 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700507}
508
Romain Guyc7d53492010-06-25 13:41:57 -0700509bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy1d83e192010-08-17 11:37:00 -0700510 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700511 mSnapshot->transform->mapRect(r);
512 return !mSnapshot->clipRect->intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700513}
514
Romain Guy079ba2c2010-07-16 14:12:24 -0700515bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
516 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700517 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700518 setScissorFromClip();
519 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700520 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700521}
522
Romain Guyf6a11b82010-06-23 17:47:49 -0700523///////////////////////////////////////////////////////////////////////////////
524// Drawing
525///////////////////////////////////////////////////////////////////////////////
526
Romain Guyc1396e92010-06-30 17:56:19 -0700527void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700528 const float right = left + bitmap->width();
529 const float bottom = top + bitmap->height();
530
531 if (quickReject(left, top, right, bottom)) {
532 return;
533 }
534
Romain Guy61c8c9c2010-08-09 20:48:09 -0700535 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700536 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700537 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700538 const AutoTexture autoCleanup(texture);
539
Romain Guy6926c722010-07-12 20:20:03 -0700540 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700541}
542
Romain Guyf86ef572010-07-01 11:05:42 -0700543void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
544 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
545 const mat4 transform(*matrix);
546 transform.mapRect(r);
547
Romain Guy6926c722010-07-12 20:20:03 -0700548 if (quickReject(r.left, r.top, r.right, r.bottom)) {
549 return;
550 }
551
Romain Guy61c8c9c2010-08-09 20:48:09 -0700552 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700553 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700554 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700555 const AutoTexture autoCleanup(texture);
556
Romain Guy82ba8142010-07-09 13:25:56 -0700557 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700558}
559
Romain Guy8ba548f2010-06-30 19:21:21 -0700560void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
561 float srcLeft, float srcTop, float srcRight, float srcBottom,
562 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700563 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700564 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
565 return;
566 }
567
Romain Guy61c8c9c2010-08-09 20:48:09 -0700568 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700569 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700570 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700571 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700572
Romain Guy8ba548f2010-06-30 19:21:21 -0700573 const float width = texture->width;
574 const float height = texture->height;
575
576 const float u1 = srcLeft / width;
577 const float v1 = srcTop / height;
578 const float u2 = srcRight / width;
579 const float v2 = srcBottom / height;
580
581 resetDrawTextureTexCoords(u1, v1, u2, v2);
582
Romain Guy82ba8142010-07-09 13:25:56 -0700583 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700584
585 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
586}
587
Romain Guydeba7852010-07-07 17:54:48 -0700588void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
589 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700590 if (quickReject(left, top, right, bottom)) {
591 return;
592 }
593
Romain Guy61c8c9c2010-08-09 20:48:09 -0700594 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700595 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700596 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700597 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700598
599 int alpha;
600 SkXfermode::Mode mode;
601 getAlphaAndMode(paint, &alpha, &mode);
602
Romain Guyfb8b7632010-08-23 21:05:08 -0700603 Patch* mesh = mCaches.patchCache.get(patch);
Romain Guy759ea802010-09-16 20:49:46 -0700604 mesh->updateVertices(bitmap->width(), bitmap->height(),left, top, right, bottom,
Romain Guyfb5e23c2010-07-09 13:52:56 -0700605 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700606
607 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
608 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700609 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
610 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy6820ac82010-09-15 18:11:50 -0700611 &mesh->vertices[0].texture[0], GL_TRIANGLES, mesh->verticesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700612}
613
Romain Guy759ea802010-09-16 20:49:46 -0700614void OpenGLRenderer::drawLines(float* points, int count, const SkPaint* paint) {
615 int alpha;
616 SkXfermode::Mode mode;
617 getAlphaAndMode(paint, &alpha, &mode);
618
619 uint32_t color = paint->getColor();
620 const GLfloat a = alpha / 255.0f;
621 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
622 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
623 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
624
Romain Guyc95c8d62010-09-17 15:31:32 -0700625 const bool isAA = paint->isAntiAlias();
626 if (isAA) {
627 GLuint textureUnit = 0;
Romain Guy29d89972010-09-22 16:10:57 -0700628 setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
629 mode, false, true, mCaches.line.getVertices(), mCaches.line.getTexCoords());
Romain Guyc95c8d62010-09-17 15:31:32 -0700630 } else {
631 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false);
632 }
633
634 const float strokeWidth = paint->getStrokeWidth();
Romain Guy29d89972010-09-22 16:10:57 -0700635 const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount;
Romain Guyc95c8d62010-09-17 15:31:32 -0700636 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
Romain Guy759ea802010-09-16 20:49:46 -0700637
638 for (int i = 0; i < count; i += 4) {
639 float tx = 0.0f;
640 float ty = 0.0f;
641
Romain Guyc95c8d62010-09-17 15:31:32 -0700642 if (isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700643 mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3],
Romain Guyc95c8d62010-09-17 15:31:32 -0700644 strokeWidth, tx, ty);
645 } else {
Romain Guyb5ab4172010-09-17 15:36:56 -0700646 ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f;
Romain Guyc95c8d62010-09-17 15:31:32 -0700647 }
Romain Guy759ea802010-09-16 20:49:46 -0700648
649 const float dx = points[i + 2] - points[i];
650 const float dy = points[i + 3] - points[i + 1];
651 const float mag = sqrtf(dx * dx + dy * dy);
652 const float angle = acos(dx / mag);
653
654 mModelView.loadTranslate(points[i], points[i + 1], 0.0f);
655 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) {
656 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f);
657 }
658 mModelView.translate(tx, ty, 0.0f);
Romain Guyc95c8d62010-09-17 15:31:32 -0700659 if (!isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700660 float length = mCaches.line.getLength(points[i], points[i + 1],
661 points[i + 2], points[i + 3]);
Romain Guyc95c8d62010-09-17 15:31:32 -0700662 mModelView.scale(length, strokeWidth, 1.0f);
663 }
Romain Guy759ea802010-09-16 20:49:46 -0700664 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
665
666 if (mShader) {
667 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot);
668 }
669
Romain Guyc95c8d62010-09-17 15:31:32 -0700670 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy759ea802010-09-16 20:49:46 -0700671 }
672
Romain Guy29d89972010-09-22 16:10:57 -0700673 if (isAA) {
674 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
675 }
Romain Guy759ea802010-09-16 20:49:46 -0700676}
677
Romain Guy85bf02f2010-06-22 13:11:24 -0700678void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700679 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700680 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700681}
Romain Guy9d5316e2010-06-24 19:30:36 -0700682
Romain Guybd6b79b2010-06-26 00:13:53 -0700683void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700684 if (quickReject(left, top, right, bottom)) {
685 return;
686 }
687
Romain Guy026c5e162010-06-28 17:12:22 -0700688 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700689 if (!mExtensions.hasFramebufferFetch()) {
690 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
691 if (!isMode) {
692 // Assume SRC_OVER
693 mode = SkXfermode::kSrcOver_Mode;
694 }
695 } else {
696 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700697 }
698
699 // Skia draws using the color's alpha channel if < 255
700 // Otherwise, it uses the paint's alpha
701 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700702 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700703 color |= p->getAlpha() << 24;
704 }
705
706 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700707}
Romain Guy9d5316e2010-06-24 19:30:36 -0700708
Romain Guye8e62a42010-07-23 18:55:21 -0700709void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
710 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700711 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700712 return;
713 }
Romain Guye2d345e2010-09-24 18:39:22 -0700714
Romain Guyf607bdc2010-09-10 19:20:06 -0700715 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700716
Romain Guya674ab72010-08-10 17:26:42 -0700717 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700718 switch (paint->getTextAlign()) {
719 case SkPaint::kCenter_Align:
720 length = paint->measureText(text, bytesCount);
721 x -= length / 2.0f;
722 break;
723 case SkPaint::kRight_Align:
724 length = paint->measureText(text, bytesCount);
725 x -= length;
726 break;
727 default:
728 break;
729 }
730
Romain Guy694b5192010-07-21 21:33:20 -0700731 int alpha;
732 SkXfermode::Mode mode;
733 getAlphaAndMode(paint, &alpha, &mode);
734
Romain Guy2542d192010-08-18 11:47:12 -0700735 uint32_t color = paint->getColor();
736 const GLfloat a = alpha / 255.0f;
737 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
738 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
739 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
740
Romain Guyb45c0c92010-08-26 20:35:23 -0700741 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
742 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700743 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -0700744
Romain Guy1e45aae2010-08-13 19:39:53 -0700745 if (mHasShadow) {
746 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700747 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700748 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700749 count, mShadowRadius);
750 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700751
Romain Guy2542d192010-08-18 11:47:12 -0700752 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700753
754 // Draw the mesh
755 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700756 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700757 }
758
Romain Guy06f96e22010-07-30 19:18:16 -0700759 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700760 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700761
Romain Guyb45c0c92010-08-26 20:35:23 -0700762 setupTextureAlpha8(fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
Romain Guy0a417492010-08-16 20:26:20 -0700763 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700764
Romain Guy09147fb2010-07-22 13:08:20 -0700765 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy86942302010-09-12 13:02:16 -0700766 clearLayerRegions();
Romain Guyb45c0c92010-08-26 20:35:23 -0700767 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700768
769 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700770 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700771
Romain Guy0a417492010-08-16 20:26:20 -0700772 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -0700773}
774
Romain Guy7fbcc042010-08-04 15:40:07 -0700775void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
776 GLuint textureUnit = 0;
777 glActiveTexture(gTextureUnits[textureUnit]);
778
Romain Guyfb8b7632010-08-23 21:05:08 -0700779 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700780 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700781 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700782
Romain Guy8b55f372010-08-18 17:10:07 -0700783 const float x = texture->left - texture->offset;
784 const float y = texture->top - texture->offset;
785
786 if (quickReject(x, y, x + texture->width, y + texture->height)) {
787 return;
788 }
789
Romain Guy7fbcc042010-08-04 15:40:07 -0700790 int alpha;
791 SkXfermode::Mode mode;
792 getAlphaAndMode(paint, &alpha, &mode);
793
794 uint32_t color = paint->getColor();
795 const GLfloat a = alpha / 255.0f;
796 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
797 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
798 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
799
Romain Guy0a417492010-08-16 20:26:20 -0700800 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
801
Romain Guy86942302010-09-12 13:02:16 -0700802 clearLayerRegions();
803
Romain Guy0a417492010-08-16 20:26:20 -0700804 // Draw the mesh
805 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700806 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700807}
808
Romain Guy6926c722010-07-12 20:20:03 -0700809///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700810// Shaders
811///////////////////////////////////////////////////////////////////////////////
812
813void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700814 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700815}
816
Romain Guy06f96e22010-07-30 19:18:16 -0700817void OpenGLRenderer::setupShader(SkiaShader* shader) {
818 mShader = shader;
819 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700820 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700821 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700822}
823
Romain Guyd27977d2010-07-14 19:18:51 -0700824///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700825// Color filters
826///////////////////////////////////////////////////////////////////////////////
827
828void OpenGLRenderer::resetColorFilter() {
829 mColorFilter = NULL;
830}
831
832void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
833 mColorFilter = filter;
834}
835
836///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700837// Drop shadow
838///////////////////////////////////////////////////////////////////////////////
839
840void OpenGLRenderer::resetShadow() {
841 mHasShadow = false;
842}
843
844void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
845 mHasShadow = true;
846 mShadowRadius = radius;
847 mShadowDx = dx;
848 mShadowDy = dy;
849 mShadowColor = color;
850}
851
852///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700853// Drawing implementation
854///////////////////////////////////////////////////////////////////////////////
855
Romain Guy0a417492010-08-16 20:26:20 -0700856void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700857 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700858 const float sx = x - texture->left + mShadowDx;
859 const float sy = y - texture->top + mShadowDy;
860
Romain Guy2542d192010-08-18 11:47:12 -0700861 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
862 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700863 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
864 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
865 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
866
867 GLuint textureUnit = 0;
868 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
869}
870
871void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
872 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
873 bool transforms, bool applyFilters) {
874 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -0700875 x, y, r, g, b, a, mode, transforms, applyFilters,
876 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
Romain Guy0a417492010-08-16 20:26:20 -0700877}
878
879void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
880 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
881 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy759ea802010-09-16 20:49:46 -0700882 setupTextureAlpha8(texture, width, height, textureUnit,
883 x, y, r, g, b, a, mode, transforms, applyFilters,
884 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
885}
886
887void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
888 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
889 SkXfermode::Mode mode, bool transforms, bool applyFilters,
890 GLvoid* vertices, GLvoid* texCoords) {
Romain Guy0a417492010-08-16 20:26:20 -0700891 // Describe the required shaders
892 ProgramDescription description;
893 description.hasTexture = true;
894 description.hasAlpha8Texture = true;
895
896 if (applyFilters) {
897 if (mShader) {
898 mShader->describe(description, mExtensions);
899 }
900 if (mColorFilter) {
901 mColorFilter->describe(description, mExtensions);
902 }
903 }
904
Romain Guya5aed0d2010-09-09 14:42:43 -0700905 // Setup the blending mode
906 chooseBlending(true, mode, description);
907
Romain Guy0a417492010-08-16 20:26:20 -0700908 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700909 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700910
Romain Guy0a417492010-08-16 20:26:20 -0700911 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -0700912 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700913
Romain Guyfb8b7632010-08-23 21:05:08 -0700914 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -0700915 glEnableVertexAttribArray(texCoordsSlot);
916
917 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700918 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -0700919 gMeshStride, vertices);
Romain Guy0a417492010-08-16 20:26:20 -0700920 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
Romain Guy759ea802010-09-16 20:49:46 -0700921 gMeshStride, texCoords);
Romain Guy0a417492010-08-16 20:26:20 -0700922
923 // Setup uniforms
924 if (transforms) {
925 mModelView.loadTranslate(x, y, 0.0f);
926 mModelView.scale(width, height, 1.0f);
927 } else {
928 mModelView.loadIdentity();
929 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700930 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyfb8b7632010-08-23 21:05:08 -0700931 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -0700932
933 textureUnit++;
934 if (applyFilters) {
935 // Setup attributes and uniforms required by the shaders
936 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700937 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700938 }
939 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700940 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -0700941 }
942 }
943}
944
Romain Guyf607bdc2010-09-10 19:20:06 -0700945// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -0700946#define kStdStrikeThru_Offset (-6.0f / 21.0f)
947#define kStdUnderline_Offset (1.0f / 9.0f)
948#define kStdUnderline_Thickness (1.0f / 18.0f)
949
950void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
951 float x, float y, SkPaint* paint) {
952 // Handle underline and strike-through
953 uint32_t flags = paint->getFlags();
954 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
955 float underlineWidth = length;
956 // If length is > 0.0f, we already measured the text for the text alignment
957 if (length <= 0.0f) {
958 underlineWidth = paint->measureText(text, bytesCount);
959 }
960
961 float offsetX = 0;
962 switch (paint->getTextAlign()) {
963 case SkPaint::kCenter_Align:
964 offsetX = underlineWidth * 0.5f;
965 break;
966 case SkPaint::kRight_Align:
967 offsetX = underlineWidth;
968 break;
969 default:
970 break;
971 }
972
973 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -0700974 const float textSize = paint->getTextSize();
975 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -0700976
Romain Guye20ecbd2010-09-22 19:49:04 -0700977 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -0700978 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -0700979
980 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
981 float points[pointsCount];
982 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -0700983
984 if (flags & SkPaint::kUnderlineText_Flag) {
985 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -0700986 points[currentPoint++] = left;
987 points[currentPoint++] = top;
988 points[currentPoint++] = left + underlineWidth;
989 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -0700990 }
991
992 if (flags & SkPaint::kStrikeThruText_Flag) {
993 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -0700994 points[currentPoint++] = left;
995 points[currentPoint++] = top;
996 points[currentPoint++] = left + underlineWidth;
997 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -0700998 }
Romain Guye20ecbd2010-09-22 19:49:04 -0700999
1000 SkPaint linesPaint(*paint);
1001 linesPaint.setStrokeWidth(strokeWidth);
1002
1003 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001004 }
1005 }
1006}
1007
Romain Guy026c5e162010-06-28 17:12:22 -07001008void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001009 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001010 clearLayerRegions();
1011
Romain Guyd27977d2010-07-14 19:18:51 -07001012 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001013 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001014 color |= 0x00ffffff;
1015 }
1016
1017 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -07001018 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -07001019 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -07001020 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1021 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1022 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1023
Romain Guyc95c8d62010-09-17 15:31:32 -07001024 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform);
1025
1026 // Draw the mesh
1027 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1028}
1029
1030void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom,
1031 float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy06f96e22010-07-30 19:18:16 -07001032 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -07001033
Romain Guy06f96e22010-07-30 19:18:16 -07001034 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -07001035 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -07001036 if (mShader) {
1037 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -07001038 }
Romain Guydb1938e2010-08-02 18:50:22 -07001039 if (mColorFilter) {
1040 mColorFilter->describe(description, mExtensions);
1041 }
Romain Guyd27977d2010-07-14 19:18:51 -07001042
Romain Guy1c740bc2010-09-13 18:00:09 -07001043 // Setup the blending mode
Romain Guyc95c8d62010-09-17 15:31:32 -07001044 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -07001045
Romain Guy06f96e22010-07-30 19:18:16 -07001046 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001047 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -07001048
1049 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -07001050 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -07001051 gMeshStride, &mMeshVertices[0].position[0]);
1052
1053 // Setup uniforms
1054 mModelView.loadTranslate(left, top, 0.0f);
1055 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -07001056 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001057 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -07001058 } else {
1059 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -07001060 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -07001061 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001062 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -07001063
Romain Guy06f96e22010-07-30 19:18:16 -07001064 // Setup attributes and uniforms required by the shaders
1065 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001066 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001067 }
Romain Guydb1938e2010-08-02 18:50:22 -07001068 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001069 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001070 }
Romain Guyd27977d2010-07-14 19:18:51 -07001071}
1072
Romain Guy82ba8142010-07-09 13:25:56 -07001073void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001074 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001075 int alpha;
1076 SkXfermode::Mode mode;
1077 getAlphaAndMode(paint, &alpha, &mode);
1078
Romain Guy6820ac82010-09-15 18:11:50 -07001079 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1080 texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1081 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -07001082}
1083
Romain Guybd6b79b2010-06-26 00:13:53 -07001084void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001085 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1086 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001087 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1088 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001089}
1090
1091void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001092 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001093 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guyf607bdc2010-09-10 19:20:06 -07001094 bool swapSrcDst, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -07001095 clearLayerRegions();
1096
Romain Guy889f8d12010-07-29 14:37:42 -07001097 ProgramDescription description;
1098 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -07001099 if (mColorFilter) {
1100 mColorFilter->describe(description, mExtensions);
1101 }
Romain Guy889f8d12010-07-29 14:37:42 -07001102
Romain Guybd6b79b2010-06-26 00:13:53 -07001103 mModelView.loadTranslate(left, top, 0.0f);
1104 mModelView.scale(right - left, bottom - top, 1.0f);
1105
Romain Guyf607bdc2010-09-10 19:20:06 -07001106 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001107
Romain Guyfb8b7632010-08-23 21:05:08 -07001108 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001109 if (!ignoreTransform) {
1110 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1111 } else {
1112 mat4 m;
1113 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
1114 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001115
Romain Guy889f8d12010-07-29 14:37:42 -07001116 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -07001117 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001118 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001119
Romain Guya9794742010-07-13 11:37:54 -07001120 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -07001121 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -07001122
Romain Guy889f8d12010-07-29 14:37:42 -07001123 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001124 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001125 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -07001126 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001127 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001128 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001129
Romain Guydb1938e2010-08-02 18:50:22 -07001130 // Color filter
1131 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001132 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001133 }
1134
Romain Guy6820ac82010-09-15 18:11:50 -07001135 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001136 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001137}
1138
Romain Guya5aed0d2010-09-09 14:42:43 -07001139void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001140 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001141 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1142 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001143 if (mode < SkXfermode::kPlus_Mode) {
1144 if (!mCaches.blend) {
1145 glEnable(GL_BLEND);
1146 }
Romain Guy82ba8142010-07-09 13:25:56 -07001147
Romain Guyf607bdc2010-09-10 19:20:06 -07001148 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1149 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001150
Romain Guya5aed0d2010-09-09 14:42:43 -07001151 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1152 glBlendFunc(sourceMode, destMode);
1153 mCaches.lastSrcMode = sourceMode;
1154 mCaches.lastDstMode = destMode;
1155 }
1156 } else {
1157 // These blend modes are not supported by OpenGL directly and have
1158 // to be implemented using shaders. Since the shader will perform
1159 // the blending, turn blending off here
1160 if (mExtensions.hasFramebufferFetch()) {
1161 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001162 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001163 }
1164
1165 if (mCaches.blend) {
1166 glDisable(GL_BLEND);
1167 }
1168 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001169 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001170 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001171 glDisable(GL_BLEND);
1172 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001173 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001174}
1175
Romain Guy889f8d12010-07-29 14:37:42 -07001176bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001177 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001178 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001179 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001180 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001181 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001182 }
Romain Guy6926c722010-07-12 20:20:03 -07001183 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001184}
1185
Romain Guy026c5e162010-06-28 17:12:22 -07001186void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001187 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001188 TextureVertex::setUV(v++, u1, v1);
1189 TextureVertex::setUV(v++, u2, v1);
1190 TextureVertex::setUV(v++, u1, v2);
1191 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001192}
1193
1194void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1195 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001196 if (!mExtensions.hasFramebufferFetch()) {
1197 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1198 if (!isMode) {
1199 // Assume SRC_OVER
1200 *mode = SkXfermode::kSrcOver_Mode;
1201 }
1202 } else {
1203 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001204 }
1205
1206 // Skia draws using the color's alpha channel if < 255
1207 // Otherwise, it uses the paint's alpha
1208 int color = paint->getColor();
1209 *alpha = (color >> 24) & 0xFF;
1210 if (*alpha == 255) {
1211 *alpha = paint->getAlpha();
1212 }
1213 } else {
1214 *mode = SkXfermode::kSrcOver_Mode;
1215 *alpha = 255;
1216 }
Romain Guy026c5e162010-06-28 17:12:22 -07001217}
1218
Romain Guya5aed0d2010-09-09 14:42:43 -07001219SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1220 if (mode == NULL) {
1221 return SkXfermode::kSrcOver_Mode;
1222 }
1223 return mode->fMode;
1224}
1225
Romain Guy889f8d12010-07-29 14:37:42 -07001226void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1227 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001228 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1231}
1232
Romain Guy9d5316e2010-06-24 19:30:36 -07001233}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001234}; // namespace android