blob: 09e01f61cbae988b0a4deb90e2a85a18f9d26de1 [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 Guy5b3b3522010-10-27 18:57:51 -070029#include <ui/Rect.h>
30
Romain Guy85bf02f2010-06-22 13:11:24 -070031#include "OpenGLRenderer.h"
Romain Guye4d01122010-06-16 18:44:05 -070032
33namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070034namespace uirenderer {
35
36///////////////////////////////////////////////////////////////////////////////
37// Defines
38///////////////////////////////////////////////////////////////////////////////
39
Romain Guy759ea802010-09-16 20:49:46 -070040#define RAD_TO_DEG (180.0f / 3.14159265f)
41#define MIN_ANGLE 0.001f
42
Romain Guydbc26d22010-10-11 17:58:29 -070043// TODO: This should be set in properties
44#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
45
Romain Guy9d5316e2010-06-24 19:30:36 -070046///////////////////////////////////////////////////////////////////////////////
47// Globals
48///////////////////////////////////////////////////////////////////////////////
49
Romain Guy889f8d12010-07-29 14:37:42 -070050/**
51 * Structure mapping Skia xfermodes to OpenGL blending factors.
52 */
53struct Blender {
54 SkXfermode::Mode mode;
55 GLenum src;
56 GLenum dst;
57}; // struct Blender
58
Romain Guy026c5e162010-06-28 17:12:22 -070059// In this array, the index of each Blender equals the value of the first
60// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
61static const Blender gBlends[] = {
62 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
63 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
64 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
65 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
66 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
67 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
68 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
69 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
70 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
71 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
73 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
74};
Romain Guye4d01122010-06-16 18:44:05 -070075
Romain Guy87a76572010-09-13 18:11:21 -070076// This array contains the swapped version of each SkXfermode. For instance
77// this array's SrcOver blending mode is actually DstOver. You can refer to
78// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070079static const Blender gBlendsSwap[] = {
80 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
81 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
82 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
83 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
84 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
85 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
86 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
87 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
88 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
89 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
90 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
91 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
92};
93
Romain Guy889f8d12010-07-29 14:37:42 -070094static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -070095 GL_TEXTURE0,
96 GL_TEXTURE1,
97 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -070098};
99
Romain Guyf6a11b82010-06-23 17:47:49 -0700100///////////////////////////////////////////////////////////////////////////////
101// Constructors/destructor
102///////////////////////////////////////////////////////////////////////////////
103
Romain Guyfb8b7632010-08-23 21:05:08 -0700104OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700105 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700106 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700107 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700108
Romain Guyac670c02010-07-27 17:39:27 -0700109 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
110
Romain Guyae5575b2010-07-29 18:48:04 -0700111 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700112}
113
Romain Guy85bf02f2010-06-22 13:11:24 -0700114OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700115 // The context has already been destroyed at this point, do not call
116 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700117}
118
Romain Guyf6a11b82010-06-23 17:47:49 -0700119///////////////////////////////////////////////////////////////////////////////
120// Setup
121///////////////////////////////////////////////////////////////////////////////
122
Romain Guy85bf02f2010-06-22 13:11:24 -0700123void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700124 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700125 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700126
127 mWidth = width;
128 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700129
130 mFirstSnapshot->height = height;
131 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700132
133 mDirtyClip = false;
Romain Guye4d01122010-06-16 18:44:05 -0700134}
135
Romain Guy6b7bd242010-10-06 19:49:23 -0700136void OpenGLRenderer::prepare(bool opaque) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700137 mSnapshot = new Snapshot(mFirstSnapshot,
138 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy8fb95422010-08-17 18:38:51 -0700139 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700140
Romain Guyfb8b7632010-08-23 21:05:08 -0700141 glViewport(0, 0, mWidth, mHeight);
142
Romain Guyd90f23e2010-09-09 11:47:54 -0700143 glDisable(GL_DITHER);
Romain Guybb9524b2010-06-22 18:56:38 -0700144
Romain Guy6b7bd242010-10-06 19:49:23 -0700145 if (!opaque) {
Romain Guy746b7402010-10-26 16:27:31 -0700146 glDisable(GL_SCISSOR_TEST);
Romain Guy6b7bd242010-10-06 19:49:23 -0700147 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
148 glClear(GL_COLOR_BUFFER_BIT);
Romain Guy746b7402010-10-26 16:27:31 -0700149 glEnable(GL_SCISSOR_TEST);
150 } else {
151 glEnable(GL_SCISSOR_TEST);
152 glScissor(0, 0, mWidth, mHeight);
153 dirtyClip();
Romain Guy6b7bd242010-10-06 19:49:23 -0700154 }
Romain Guybb9524b2010-06-22 18:56:38 -0700155
Romain Guyb82da652010-07-30 11:36:12 -0700156 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700157}
158
Romain Guyb025b9c2010-09-16 14:16:48 -0700159void OpenGLRenderer::finish() {
160#if DEBUG_OPENGL
161 GLenum status = GL_NO_ERROR;
162 while ((status = glGetError()) != GL_NO_ERROR) {
163 LOGD("GL error from OpenGLRenderer: 0x%x", status);
164 }
165#endif
166}
167
Romain Guyda8532c2010-08-31 11:50:35 -0700168void OpenGLRenderer::acquireContext() {
169 if (mCaches.currentProgram) {
170 if (mCaches.currentProgram->isInUse()) {
171 mCaches.currentProgram->remove();
172 mCaches.currentProgram = NULL;
173 }
174 }
Romain Guy50c0f092010-10-19 11:42:22 -0700175 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700176}
177
178void OpenGLRenderer::releaseContext() {
Romain Guyeb993562010-10-05 18:14:38 -0700179 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700180
181 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700182 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700183
Romain Guyf607bdc2010-09-10 19:20:06 -0700184 glDisable(GL_DITHER);
185
186 glBindFramebuffer(GL_FRAMEBUFFER, 0);
Romain Guy03750a02010-10-18 14:06:08 -0700187 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700188
Romain Guy50c0f092010-10-19 11:42:22 -0700189 mCaches.blend = true;
190 glEnable(GL_BLEND);
191 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
192 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700193}
194
Romain Guyf6a11b82010-06-23 17:47:49 -0700195///////////////////////////////////////////////////////////////////////////////
196// State management
197///////////////////////////////////////////////////////////////////////////////
198
Romain Guybb9524b2010-06-22 18:56:38 -0700199int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700200 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700201}
202
203int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700204 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700205}
206
207void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700208 if (mSaveCount > 1) {
209 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700210 }
Romain Guybb9524b2010-06-22 18:56:38 -0700211}
212
213void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700214 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700215
Romain Guy8fb95422010-08-17 18:38:51 -0700216 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700217 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700218 }
Romain Guybb9524b2010-06-22 18:56:38 -0700219}
220
Romain Guy8aef54f2010-09-01 15:13:49 -0700221int OpenGLRenderer::saveSnapshot(int flags) {
222 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700223 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700224}
225
226bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700227 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700228 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700229 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700230
Romain Guybd6b79b2010-06-26 00:13:53 -0700231 sp<Snapshot> current = mSnapshot;
232 sp<Snapshot> previous = mSnapshot->previous;
233
Romain Guyeb993562010-10-05 18:14:38 -0700234 if (restoreOrtho) {
235 Rect& r = previous->viewport;
236 glViewport(r.left, r.top, r.right, r.bottom);
237 mOrthoMatrix.load(current->orthoMatrix);
238 }
239
Romain Guy8b55f372010-08-18 17:10:07 -0700240 mSaveCount--;
241 mSnapshot = previous;
242
Romain Guy2542d192010-08-18 11:47:12 -0700243 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700244 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700245 }
Romain Guy2542d192010-08-18 11:47:12 -0700246
Romain Guy5ec99242010-11-03 16:19:08 -0700247 if (restoreLayer) {
248 composeLayer(current, previous);
249 }
250
Romain Guy2542d192010-08-18 11:47:12 -0700251 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700252}
253
Romain Guyf6a11b82010-06-23 17:47:49 -0700254///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700255// Layers
256///////////////////////////////////////////////////////////////////////////////
257
258int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700259 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700260 const GLuint previousFbo = mSnapshot->fbo;
261 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700262
Romain Guye45362c2010-11-03 19:58:32 -0700263 if (!mSnapshot->invisible) {
264 int alpha = 255;
265 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700266
Romain Guye45362c2010-11-03 19:58:32 -0700267 if (p) {
268 alpha = p->getAlpha();
269 if (!mCaches.extensions.hasFramebufferFetch()) {
270 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
271 if (!isMode) {
272 // Assume SRC_OVER
273 mode = SkXfermode::kSrcOver_Mode;
274 }
275 } else {
276 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700277 }
278 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700279 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700280 }
Romain Guyd55a8612010-06-28 17:42:46 -0700281
Romain Guydbc26d22010-10-11 17:58:29 -0700282 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
283 }
Romain Guyd55a8612010-06-28 17:42:46 -0700284
285 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700286}
287
288int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
289 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700290 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700291 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700292 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700293 SkPaint paint;
294 paint.setAlpha(alpha);
295 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700296 }
Romain Guyd55a8612010-06-28 17:42:46 -0700297}
Romain Guybd6b79b2010-06-26 00:13:53 -0700298
Romain Guy1c740bc2010-09-13 18:00:09 -0700299/**
300 * Layers are viewed by Skia are slightly different than layers in image editing
301 * programs (for instance.) When a layer is created, previously created layers
302 * and the frame buffer still receive every drawing command. For instance, if a
303 * layer is created and a shape intersecting the bounds of the layers and the
304 * framebuffer is draw, the shape will be drawn on both (unless the layer was
305 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
306 *
307 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
308 * texture. Unfortunately, this is inefficient as it requires every primitive to
309 * be drawn n + 1 times, where n is the number of active layers. In practice this
310 * means, for every primitive:
311 * - Switch active frame buffer
312 * - Change viewport, clip and projection matrix
313 * - Issue the drawing
314 *
315 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700316 * To avoid this, layers are implemented in a different way here, at least in the
317 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
318 * is set. When this flag is set we can redirect all drawing operations into a
319 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700320 *
321 * This implementation relies on the frame buffer being at least RGBA 8888. When
322 * a layer is created, only a texture is created, not an FBO. The content of the
323 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700324 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700325 * buffer and drawing continues as normal. This technique therefore treats the
326 * frame buffer as a scratch buffer for the layers.
327 *
328 * To compose the layers back onto the frame buffer, each layer texture
329 * (containing the original frame buffer data) is drawn as a simple quad over
330 * the frame buffer. The trick is that the quad is set as the composition
331 * destination in the blending equation, and the frame buffer becomes the source
332 * of the composition.
333 *
334 * Drawing layers with an alpha value requires an extra step before composition.
335 * An empty quad is drawn over the layer's region in the frame buffer. This quad
336 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
337 * quad is used to multiply the colors in the frame buffer. This is achieved by
338 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
339 * GL_ZERO, GL_SRC_ALPHA.
340 *
341 * Because glCopyTexImage2D() can be slow, an alternative implementation might
342 * be use to draw a single clipped layer. The implementation described above
343 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700344 *
345 * (1) The frame buffer is actually not cleared right away. To allow the GPU
346 * to potentially optimize series of calls to glCopyTexImage2D, the frame
347 * buffer is left untouched until the first drawing operation. Only when
348 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700349 */
Romain Guyd55a8612010-06-28 17:42:46 -0700350bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700351 float right, float bottom, int alpha, SkXfermode::Mode mode,
352 int flags, GLuint previousFbo) {
353 LAYER_LOGD("Requesting layer %.2fx%.2f", 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 Guyeb993562010-10-05 18:14:38 -0700356 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
357
Romain Guyf607bdc2010-09-10 19:20:06 -0700358 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700359 Rect bounds(left, top, right, bottom);
Romain Guyae88e5e2010-10-22 17:49:18 -0700360 if (fboLayer) {
361 // Clear the previous layer regions before we change the viewport
362 clearLayerRegions();
363 } else {
Romain Guyeb993562010-10-05 18:14:38 -0700364 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700365
Romain Guyeb993562010-10-05 18:14:38 -0700366 // Layers only make sense if they are in the framebuffer's bounds
Romain Guy58ae6db2010-10-22 11:16:37 -0700367 bounds.intersect(*snapshot->clipRect);
Romain Guyae517592010-10-22 10:40:27 -0700368
Romain Guyae88e5e2010-10-22 17:49:18 -0700369 // We cannot work with sub-pixels in this case
370 bounds.snapToPixelBoundaries();
371
Romain Guyae517592010-10-22 10:40:27 -0700372 // When the layer is not an FBO, we may use glCopyTexImage so we
373 // need to make sure the layer does not extend outside the bounds
374 // of the framebuffer
375 bounds.intersect(snapshot->previous->viewport);
Romain Guyeb993562010-10-05 18:14:38 -0700376 }
Romain Guybf434112010-09-16 14:40:17 -0700377
Romain Guy746b7402010-10-26 16:27:31 -0700378 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
379 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guydbc26d22010-10-11 17:58:29 -0700380 snapshot->invisible = true;
381 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700382 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700383 }
384
385 // Bail out if we won't draw in this snapshot
386 if (snapshot->invisible) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700387 return false;
388 }
Romain Guyf18fd992010-07-08 11:45:51 -0700389
Romain Guy5b3b3522010-10-27 18:57:51 -0700390 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700391 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700392 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700393 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700394 }
395
Romain Guydda57022010-07-06 11:39:32 -0700396 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700397 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700398 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700399 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
400 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guydda57022010-07-06 11:39:32 -0700401
Romain Guy8fb95422010-08-17 18:38:51 -0700402 // Save the layer in the snapshot
403 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700404 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700405
Romain Guyeb993562010-10-05 18:14:38 -0700406 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700407 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700408 } else {
409 // Copy the framebuffer into the layer
410 glBindTexture(GL_TEXTURE_2D, layer->texture);
411
Romain Guyae88e5e2010-10-22 17:49:18 -0700412 if (layer->empty) {
413 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
414 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
415 layer->empty = false;
416 } else {
417 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
418 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
419 }
Romain Guyeb993562010-10-05 18:14:38 -0700420
421 // Enqueue the buffer coordinates to clear the corresponding region later
422 mLayers.push(new Rect(bounds));
423 }
Romain Guyf86ef572010-07-01 11:05:42 -0700424
Romain Guyd55a8612010-06-28 17:42:46 -0700425 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700426}
427
Romain Guy5b3b3522010-10-27 18:57:51 -0700428bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
429 GLuint previousFbo) {
430 layer->fbo = mCaches.fboCache.get();
431
432#if RENDER_LAYERS_AS_REGIONS
433 snapshot->region = &snapshot->layer->region;
434 snapshot->flags |= Snapshot::kFlagFboTarget;
435#endif
436
437 Rect clip(bounds);
438 snapshot->transform->mapRect(clip);
439 clip.intersect(*snapshot->clipRect);
440 clip.snapToPixelBoundaries();
441 clip.intersect(snapshot->previous->viewport);
442
443 mat4 inverse;
444 inverse.loadInverse(*mSnapshot->transform);
445
446 inverse.mapRect(clip);
447 clip.snapToPixelBoundaries();
448 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700449 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700450
451 snapshot->flags |= Snapshot::kFlagIsFboLayer;
452 snapshot->fbo = layer->fbo;
453 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
454 //snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
455 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
456 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
457 snapshot->height = bounds.getHeight();
458 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
459 snapshot->orthoMatrix.load(mOrthoMatrix);
460
461 // Bind texture to FBO
462 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
463 glBindTexture(GL_TEXTURE_2D, layer->texture);
464
465 // Initialize the texture if needed
466 if (layer->empty) {
467 layer->empty = false;
468 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
469 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
470 }
471
472 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
473 layer->texture, 0);
474
475#if DEBUG_LAYERS_AS_REGIONS
476 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
477 if (status != GL_FRAMEBUFFER_COMPLETE) {
478 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
479
480 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
481 glDeleteTextures(1, &layer->texture);
482 mCaches.fboCache.put(layer->fbo);
483
484 delete layer;
485
486 return false;
487 }
488#endif
489
490 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
491 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
492 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
493 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
494 glClear(GL_COLOR_BUFFER_BIT);
495
496 dirtyClip();
497
498 // Change the ortho projection
499 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
500 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
501
502 return true;
503}
504
Romain Guy1c740bc2010-09-13 18:00:09 -0700505/**
506 * Read the documentation of createLayer() before doing anything in this method.
507 */
Romain Guy1d83e192010-08-17 11:37:00 -0700508void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
509 if (!current->layer) {
510 LOGE("Attempting to compose a layer that does not exist");
511 return;
512 }
513
Romain Guy5b3b3522010-10-27 18:57:51 -0700514 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700515
516 if (fboLayer) {
517 // Unbind current FBO and restore previous one
518 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
519 }
520
Romain Guy1d83e192010-08-17 11:37:00 -0700521 Layer* layer = current->layer;
522 const Rect& rect = layer->layer;
523
Romain Guyeb993562010-10-05 18:14:38 -0700524 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700525 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700526 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700527 // Required below, composeLayerRect() will divide by 255
528 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700529 }
530
Romain Guy03750a02010-10-18 14:06:08 -0700531 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700532
Romain Guy746b7402010-10-26 16:27:31 -0700533 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700534
Romain Guy5b3b3522010-10-27 18:57:51 -0700535 // When the layer is stored in an FBO, we can save a bit of fillrate by
536 // drawing only the dirty region
537 if (fboLayer) {
538 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
539 composeLayerRegion(layer, rect);
540 } else {
541 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
542 composeLayerRect(layer, rect, true);
543 }
Romain Guy8b55f372010-08-18 17:10:07 -0700544
Romain Guyeb993562010-10-05 18:14:38 -0700545 if (fboLayer) {
546 // Detach the texture from the FBO
547 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
548 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
549 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
550
551 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
552 mCaches.fboCache.put(current->fbo);
553 }
554
Romain Guy746b7402010-10-26 16:27:31 -0700555 dirtyClip();
556
Romain Guyeb993562010-10-05 18:14:38 -0700557 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700558 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700559 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700560 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700561 delete layer;
562 }
563}
564
Romain Guy5b3b3522010-10-27 18:57:51 -0700565void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
566 const Rect& texCoords = layer->texCoords;
567 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
568
569 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
570 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
571 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
572
573 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
574}
575
576void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
577#if RENDER_LAYERS_AS_REGIONS
578 if (layer->region.isRect()) {
579 composeLayerRect(layer, rect);
580 layer->region.clear();
581 return;
582 }
583
584 if (!layer->region.isEmpty()) {
585 size_t count;
586 const android::Rect* rects = layer->region.getArray(&count);
587
588 setupDraw();
589
590 ProgramDescription description;
591 description.hasTexture = true;
592
593 const float alpha = layer->alpha / 255.0f;
594 const bool setColor = description.setColor(alpha, alpha, alpha, alpha);
595 chooseBlending(layer->blend || layer->alpha < 255, layer->mode, description, false);
596
597 useProgram(mCaches.programCache.get(description));
598
599 // Texture
600 bindTexture(layer->texture);
601 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
602
603 // Always premultiplied
604 if (setColor) {
605 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha);
606 }
607
608 // Mesh
609 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
610 glEnableVertexAttribArray(texCoordsSlot);
611
612 mModelView.loadIdentity();
613 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
614
615 const float texX = 1.0f / float(layer->width);
616 const float texY = 1.0f / float(layer->height);
617
618 TextureVertex* mesh = mCaches.getRegionMesh();
619 GLsizei numQuads = 0;
620
621 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
622 gMeshStride, &mesh[0].position[0]);
623 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
624 gMeshStride, &mesh[0].texture[0]);
625
626 for (size_t i = 0; i < count; i++) {
627 const android::Rect* r = &rects[i];
628
629 const float u1 = r->left * texX;
630 const float v1 = (rect.getHeight() - r->top) * texY;
631 const float u2 = r->right * texX;
632 const float v2 = (rect.getHeight() - r->bottom) * texY;
633
634 // TODO: Reject quads outside of the clip
635 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
636 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
637 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
638 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
639
640 numQuads++;
641
642 if (numQuads >= REGION_MESH_QUAD_COUNT) {
643 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
644 numQuads = 0;
645 mesh = mCaches.getRegionMesh();
646 }
647 }
648
649 if (numQuads > 0) {
650 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
651 }
652
653 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
654 glDisableVertexAttribArray(texCoordsSlot);
655
656#if DEBUG_LAYERS_AS_REGIONS
657 uint32_t colors[] = {
658 0x7fff0000, 0x7f00ff00,
659 0x7f0000ff, 0x7fff00ff,
660 };
661
662 int offset = 0;
663 int32_t top = rects[0].top;
664 int i = 0;
665
666 for (size_t i = 0; i < count; i++) {
667 if (top != rects[i].top) {
668 offset ^= 0x2;
669 top = rects[i].top;
670 }
671
672 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
673 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
674 SkXfermode::kSrcOver_Mode);
675 }
676#endif
677
678 layer->region.clear();
679 }
680#else
681 composeLayerRect(layer, rect);
682#endif
683}
684
685void OpenGLRenderer::dirtyLayer(const float left, const float top,
686 const float right, const float bottom, const mat4 transform) {
687#if RENDER_LAYERS_AS_REGIONS
688 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region) {
689 Rect bounds(left, top, right, bottom);
690 transform.mapRect(bounds);
691 bounds.intersect(*mSnapshot->clipRect);
692 bounds.snapToPixelBoundaries();
693
694 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
695 if (!dirty.isEmpty()) {
696 mSnapshot->region->orSelf(dirty);
697 }
698 }
699#endif
700}
701
702void OpenGLRenderer::dirtyLayer(const float left, const float top,
703 const float right, const float bottom) {
704#if RENDER_LAYERS_AS_REGIONS
705 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region) {
706 Rect bounds(left, top, right, bottom);
707 bounds.intersect(*mSnapshot->clipRect);
708 bounds.snapToPixelBoundaries();
709
710 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
711 if (!dirty.isEmpty()) {
712 mSnapshot->region->orSelf(dirty);
713 }
714 }
715#endif
716}
717
Romain Guy746b7402010-10-26 16:27:31 -0700718void OpenGLRenderer::setupDraw() {
719 clearLayerRegions();
720 if (mDirtyClip) {
721 setScissorFromClip();
722 }
723}
724
Romain Guy86942302010-09-12 13:02:16 -0700725void OpenGLRenderer::clearLayerRegions() {
Romain Guydbc26d22010-10-11 17:58:29 -0700726 if (mLayers.size() == 0 || mSnapshot->invisible) return;
Romain Guy86942302010-09-12 13:02:16 -0700727
Romain Guy5b3b3522010-10-27 18:57:51 -0700728 Rect clipRect(*mSnapshot->clipRect);
729 clipRect.snapToPixelBoundaries();
730
Romain Guy86942302010-09-12 13:02:16 -0700731 for (uint32_t i = 0; i < mLayers.size(); i++) {
732 Rect* bounds = mLayers.itemAt(i);
Romain Guy5b3b3522010-10-27 18:57:51 -0700733 if (clipRect.intersects(*bounds)) {
734 // Clear the framebuffer where the layer will draw
735 glScissor(bounds->left, mSnapshot->height - bounds->bottom,
736 bounds->getWidth(), bounds->getHeight());
737 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
738 glClear(GL_COLOR_BUFFER_BIT);
Romain Guy86942302010-09-12 13:02:16 -0700739
Romain Guy5b3b3522010-10-27 18:57:51 -0700740 // Restore the clip
741 dirtyClip();
742 }
Romain Guy86942302010-09-12 13:02:16 -0700743
744 delete bounds;
745 }
Romain Guy86942302010-09-12 13:02:16 -0700746
Romain Guy5b3b3522010-10-27 18:57:51 -0700747 mLayers.clear();
Romain Guy86942302010-09-12 13:02:16 -0700748}
749
Romain Guybd6b79b2010-06-26 00:13:53 -0700750///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700751// Transforms
752///////////////////////////////////////////////////////////////////////////////
753
754void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700755 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700756}
757
758void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700759 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700760}
761
762void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700763 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700764}
765
766void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700767 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700768}
769
Romain Guy41030da2010-10-13 13:40:37 -0700770const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700771 if (mSnapshot->fbo != 0) {
772 return &mSnapshot->transform->data[0];
773 }
774 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700775}
776
Romain Guyf6a11b82010-06-23 17:47:49 -0700777void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700778 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700779}
780
781void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700782 SkMatrix transform;
783 mSnapshot->transform->copyTo(transform);
784 transform.preConcat(*matrix);
785 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700786}
787
788///////////////////////////////////////////////////////////////////////////////
789// Clipping
790///////////////////////////////////////////////////////////////////////////////
791
Romain Guybb9524b2010-06-22 18:56:38 -0700792void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700793 Rect clip(*mSnapshot->clipRect);
794 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700795 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700796 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700797}
798
799const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700800 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700801}
802
Romain Guyc7d53492010-06-25 13:41:57 -0700803bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guydbc26d22010-10-11 17:58:29 -0700804 if (mSnapshot->invisible) {
805 return true;
806 }
807
Romain Guy1d83e192010-08-17 11:37:00 -0700808 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700809 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700810 r.snapToPixelBoundaries();
811
812 Rect clipRect(*mSnapshot->clipRect);
813 clipRect.snapToPixelBoundaries();
814
815 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700816}
817
Romain Guy079ba2c2010-07-16 14:12:24 -0700818bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
819 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700820 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700821 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700822 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700823 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700824}
825
Romain Guyf6a11b82010-06-23 17:47:49 -0700826///////////////////////////////////////////////////////////////////////////////
827// Drawing
828///////////////////////////////////////////////////////////////////////////////
829
Chet Haase5c13d892010-10-08 08:37:55 -0700830void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700831 const float right = left + bitmap->width();
832 const float bottom = top + bitmap->height();
833
834 if (quickReject(left, top, right, bottom)) {
835 return;
836 }
837
Romain Guy61c8c9c2010-08-09 20:48:09 -0700838 glActiveTexture(GL_TEXTURE0);
Romain Guy8164c2d2010-10-25 18:03:28 -0700839 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700840 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700841 const AutoTexture autoCleanup(texture);
842
Romain Guy6926c722010-07-12 20:20:03 -0700843 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700844}
845
Chet Haase5c13d892010-10-08 08:37:55 -0700846void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -0700847 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
848 const mat4 transform(*matrix);
849 transform.mapRect(r);
850
Romain Guy6926c722010-07-12 20:20:03 -0700851 if (quickReject(r.left, r.top, r.right, r.bottom)) {
852 return;
853 }
854
Romain Guy61c8c9c2010-08-09 20:48:09 -0700855 glActiveTexture(GL_TEXTURE0);
Romain Guy8164c2d2010-10-25 18:03:28 -0700856 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700857 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700858 const AutoTexture autoCleanup(texture);
859
Romain Guy5b3b3522010-10-27 18:57:51 -0700860 // This could be done in a cheaper way, all we need is pass the matrix
861 // to the vertex shader. The save/restore is a bit overkill.
862 save(SkCanvas::kMatrix_SaveFlag);
863 concatMatrix(matrix);
864 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
865 restore();
Romain Guyf86ef572010-07-01 11:05:42 -0700866}
867
Romain Guy8ba548f2010-06-30 19:21:21 -0700868void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
869 float srcLeft, float srcTop, float srcRight, float srcBottom,
870 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700871 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700872 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
873 return;
874 }
875
Romain Guy746b7402010-10-26 16:27:31 -0700876 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -0700877 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700878 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700879 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -0700880 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -0700881
Romain Guy8ba548f2010-06-30 19:21:21 -0700882 const float width = texture->width;
883 const float height = texture->height;
884
885 const float u1 = srcLeft / width;
886 const float v1 = srcTop / height;
887 const float u2 = srcRight / width;
888 const float v2 = srcBottom / height;
889
Romain Guy03750a02010-10-18 14:06:08 -0700890 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -0700891 resetDrawTextureTexCoords(u1, v1, u2, v2);
892
Romain Guy03750a02010-10-18 14:06:08 -0700893 int alpha;
894 SkXfermode::Mode mode;
895 getAlphaAndMode(paint, &alpha, &mode);
896
897 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
898 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
899 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy8ba548f2010-06-30 19:21:21 -0700900
901 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
902}
903
Romain Guy4aa90572010-09-26 18:40:37 -0700904void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -0700905 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -0700906 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700907 if (quickReject(left, top, right, bottom)) {
908 return;
909 }
910
Romain Guy746b7402010-10-26 16:27:31 -0700911 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -0700912 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700913 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700914 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -0700915 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -0700916
917 int alpha;
918 SkXfermode::Mode mode;
919 getAlphaAndMode(paint, &alpha, &mode);
920
Romain Guy2728f962010-10-08 18:36:15 -0700921 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -0700922 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -0700923
Romain Guy054dc182010-10-15 17:55:25 -0700924 if (mesh) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700925 // Mark the current layer dirty where we are going to draw the patch
926 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) &&
927 mSnapshot->region && mesh->hasEmptyQuads) {
928 const size_t count = mesh->quads.size();
929 for (size_t i = 0; i < count; i++) {
930 Rect bounds = mesh->quads.itemAt(i);
931 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom,
932 *mSnapshot->transform);
933 }
934 }
935
936 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
Romain Guy03750a02010-10-18 14:06:08 -0700937 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
Romain Guy5b3b3522010-10-27 18:57:51 -0700938 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
939 true, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -0700940 }
Romain Guyf7f93552010-07-08 19:17:03 -0700941}
942
Chet Haase5c13d892010-10-08 08:37:55 -0700943void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -0700944 if (mSnapshot->invisible) return;
945
Romain Guy759ea802010-09-16 20:49:46 -0700946 int alpha;
947 SkXfermode::Mode mode;
948 getAlphaAndMode(paint, &alpha, &mode);
949
950 uint32_t color = paint->getColor();
951 const GLfloat a = alpha / 255.0f;
952 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
953 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
954 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
955
Romain Guyc95c8d62010-09-17 15:31:32 -0700956 const bool isAA = paint->isAntiAlias();
957 if (isAA) {
958 GLuint textureUnit = 0;
Romain Guy746b7402010-10-26 16:27:31 -0700959 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy29d89972010-09-22 16:10:57 -0700960 setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
Romain Guy03750a02010-10-18 14:06:08 -0700961 mode, false, true, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
962 mCaches.line.getMeshBuffer());
Romain Guyc95c8d62010-09-17 15:31:32 -0700963 } else {
964 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false);
965 }
966
967 const float strokeWidth = paint->getStrokeWidth();
Romain Guy29d89972010-09-22 16:10:57 -0700968 const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount;
Romain Guyc95c8d62010-09-17 15:31:32 -0700969 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
Romain Guy759ea802010-09-16 20:49:46 -0700970
971 for (int i = 0; i < count; i += 4) {
972 float tx = 0.0f;
973 float ty = 0.0f;
974
Romain Guyc95c8d62010-09-17 15:31:32 -0700975 if (isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700976 mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3],
Romain Guyc95c8d62010-09-17 15:31:32 -0700977 strokeWidth, tx, ty);
978 } else {
Romain Guyb5ab4172010-09-17 15:36:56 -0700979 ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f;
Romain Guyc95c8d62010-09-17 15:31:32 -0700980 }
Romain Guy759ea802010-09-16 20:49:46 -0700981
982 const float dx = points[i + 2] - points[i];
983 const float dy = points[i + 3] - points[i + 1];
984 const float mag = sqrtf(dx * dx + dy * dy);
985 const float angle = acos(dx / mag);
986
987 mModelView.loadTranslate(points[i], points[i + 1], 0.0f);
988 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) {
989 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f);
990 }
991 mModelView.translate(tx, ty, 0.0f);
Romain Guyc95c8d62010-09-17 15:31:32 -0700992 if (!isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700993 float length = mCaches.line.getLength(points[i], points[i + 1],
994 points[i + 2], points[i + 3]);
Romain Guyc95c8d62010-09-17 15:31:32 -0700995 mModelView.scale(length, strokeWidth, 1.0f);
996 }
Romain Guy759ea802010-09-16 20:49:46 -0700997 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700998 // TODO: Add bounds to the layer's region
Romain Guy759ea802010-09-16 20:49:46 -0700999
1000 if (mShader) {
1001 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot);
1002 }
1003
Romain Guyc95c8d62010-09-17 15:31:32 -07001004 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy759ea802010-09-16 20:49:46 -07001005 }
1006
Romain Guy29d89972010-09-22 16:10:57 -07001007 if (isAA) {
1008 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
1009 }
Romain Guy759ea802010-09-16 20:49:46 -07001010}
1011
Romain Guy85bf02f2010-06-22 13:11:24 -07001012void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001013 // No need to check against the clip, we fill the clip region
1014 if (mSnapshot->invisible) return;
1015
Romain Guyae88e5e2010-10-22 17:49:18 -07001016 Rect& clip(*mSnapshot->clipRect);
1017 clip.snapToPixelBoundaries();
Romain Guy3d58c032010-07-14 16:34:53 -07001018 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001019}
Romain Guy9d5316e2010-06-24 19:30:36 -07001020
Chet Haase5c13d892010-10-08 08:37:55 -07001021void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -07001022 if (quickReject(left, top, right, bottom)) {
1023 return;
1024 }
1025
Romain Guy026c5e162010-06-28 17:12:22 -07001026 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001027 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001028 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1029 if (!isMode) {
1030 // Assume SRC_OVER
1031 mode = SkXfermode::kSrcOver_Mode;
1032 }
1033 } else {
1034 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001035 }
1036
1037 // Skia draws using the color's alpha channel if < 255
1038 // Otherwise, it uses the paint's alpha
1039 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07001040 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -07001041 color |= p->getAlpha() << 24;
1042 }
1043
1044 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001045}
Romain Guy9d5316e2010-06-24 19:30:36 -07001046
Romain Guye8e62a42010-07-23 18:55:21 -07001047void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1048 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001049 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07001050 return;
1051 }
Romain Guydbc26d22010-10-11 17:58:29 -07001052 if (mSnapshot->invisible) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001053
Romain Guyf607bdc2010-09-10 19:20:06 -07001054 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001055
Romain Guya674ab72010-08-10 17:26:42 -07001056 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001057 switch (paint->getTextAlign()) {
1058 case SkPaint::kCenter_Align:
1059 length = paint->measureText(text, bytesCount);
1060 x -= length / 2.0f;
1061 break;
1062 case SkPaint::kRight_Align:
1063 length = paint->measureText(text, bytesCount);
1064 x -= length;
1065 break;
1066 default:
1067 break;
1068 }
1069
Romain Guy694b5192010-07-21 21:33:20 -07001070 int alpha;
1071 SkXfermode::Mode mode;
1072 getAlphaAndMode(paint, &alpha, &mode);
1073
Romain Guy2542d192010-08-18 11:47:12 -07001074 uint32_t color = paint->getColor();
1075 const GLfloat a = alpha / 255.0f;
1076 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1077 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1078 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1079
Romain Guyb45c0c92010-08-26 20:35:23 -07001080 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1081 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001082 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001083
Romain Guy746b7402010-10-26 16:27:31 -07001084 setupDraw();
Romain Guy9d13fe252010-10-15 16:06:03 -07001085
Romain Guy1e45aae2010-08-13 19:39:53 -07001086 if (mHasShadow) {
1087 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -07001088 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001089 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001090 count, mShadowRadius);
1091 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001092
Romain Guy2542d192010-08-18 11:47:12 -07001093 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -07001094
1095 // Draw the mesh
1096 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -07001097 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -07001098 }
1099
Romain Guy06f96e22010-07-30 19:18:16 -07001100 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -07001101 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -07001102
Romain Guye8cb9c142010-10-04 14:14:11 -07001103 // Assume that the modelView matrix does not force scales, rotates, etc.
1104 const bool linearFilter = mSnapshot->transform->changesBounds();
Romain Guy5b3b3522010-10-27 18:57:51 -07001105
1106 // Dimensions are set to (0,0), the layer (if any) won't be dirtied
Romain Guye8cb9c142010-10-04 14:14:11 -07001107 setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit,
Romain Guy03750a02010-10-18 14:06:08 -07001108 x, y, r, g, b, a, mode, false, true, NULL, NULL);
Romain Guy06f96e22010-07-30 19:18:16 -07001109
Romain Guy09147fb2010-07-22 13:08:20 -07001110 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001111 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1112
1113#if RENDER_LAYERS_AS_REGIONS
1114 bool hasLayer = (mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region;
1115#else
1116 bool hasLayer = false;
1117#endif
Romain Guy9d13fe252010-10-15 16:06:03 -07001118
Romain Guy03750a02010-10-18 14:06:08 -07001119 mCaches.unbindMeshBuffer();
Romain Guy5b3b3522010-10-27 18:57:51 -07001120 if (fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y,
1121 hasLayer ? &bounds : NULL)) {
1122#if RENDER_LAYERS_AS_REGIONS
1123 if (hasLayer) {
1124 mSnapshot->transform->mapRect(bounds);
1125 bounds.intersect(*mSnapshot->clipRect);
1126 bounds.snapToPixelBoundaries();
1127
1128 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1129 mSnapshot->region->orSelf(dirty);
1130 }
1131#endif
1132 }
Romain Guy694b5192010-07-21 21:33:20 -07001133
1134 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001135 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001136
Romain Guy0a417492010-08-16 20:26:20 -07001137 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001138}
1139
Romain Guy7fbcc042010-08-04 15:40:07 -07001140void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -07001141 if (mSnapshot->invisible) return;
1142
Romain Guy7fbcc042010-08-04 15:40:07 -07001143 GLuint textureUnit = 0;
1144 glActiveTexture(gTextureUnits[textureUnit]);
1145
Romain Guyfb8b7632010-08-23 21:05:08 -07001146 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001147 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001148 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001149
Romain Guy8b55f372010-08-18 17:10:07 -07001150 const float x = texture->left - texture->offset;
1151 const float y = texture->top - texture->offset;
1152
1153 if (quickReject(x, y, x + texture->width, y + texture->height)) {
1154 return;
1155 }
1156
Romain Guy7fbcc042010-08-04 15:40:07 -07001157 int alpha;
1158 SkXfermode::Mode mode;
1159 getAlphaAndMode(paint, &alpha, &mode);
1160
1161 uint32_t color = paint->getColor();
1162 const GLfloat a = alpha / 255.0f;
1163 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1164 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1165 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1166
Romain Guy0a417492010-08-16 20:26:20 -07001167 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
1168
Romain Guy746b7402010-10-26 16:27:31 -07001169 setupDraw();
Romain Guy86942302010-09-12 13:02:16 -07001170
Romain Guy0a417492010-08-16 20:26:20 -07001171 // Draw the mesh
1172 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -07001173 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -07001174}
1175
Romain Guy6926c722010-07-12 20:20:03 -07001176///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001177// Shaders
1178///////////////////////////////////////////////////////////////////////////////
1179
1180void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001181 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001182}
1183
Romain Guy06f96e22010-07-30 19:18:16 -07001184void OpenGLRenderer::setupShader(SkiaShader* shader) {
1185 mShader = shader;
1186 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001187 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001188 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001189}
1190
Romain Guyd27977d2010-07-14 19:18:51 -07001191///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001192// Color filters
1193///////////////////////////////////////////////////////////////////////////////
1194
1195void OpenGLRenderer::resetColorFilter() {
1196 mColorFilter = NULL;
1197}
1198
1199void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1200 mColorFilter = filter;
1201}
1202
1203///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07001204// Drop shadow
1205///////////////////////////////////////////////////////////////////////////////
1206
1207void OpenGLRenderer::resetShadow() {
1208 mHasShadow = false;
1209}
1210
1211void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
1212 mHasShadow = true;
1213 mShadowRadius = radius;
1214 mShadowDx = dx;
1215 mShadowDy = dy;
1216 mShadowColor = color;
1217}
1218
1219///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001220// Drawing implementation
1221///////////////////////////////////////////////////////////////////////////////
1222
Romain Guy0a417492010-08-16 20:26:20 -07001223void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -07001224 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -07001225 const float sx = x - texture->left + mShadowDx;
1226 const float sy = y - texture->top + mShadowDy;
1227
Romain Guy2542d192010-08-18 11:47:12 -07001228 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1229 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -07001230 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
1231 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
1232 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
1233
1234 GLuint textureUnit = 0;
1235 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
1236}
1237
1238void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
1239 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
1240 bool transforms, bool applyFilters) {
1241 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -07001242 x, y, r, g, b, a, mode, transforms, applyFilters,
Romain Guy03750a02010-10-18 14:06:08 -07001243 (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy0a417492010-08-16 20:26:20 -07001244}
1245
1246void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1247 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1248 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy03750a02010-10-18 14:06:08 -07001249 setupTextureAlpha8(texture, width, height, textureUnit, x, y, r, g, b, a, mode,
1250 transforms, applyFilters, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy759ea802010-09-16 20:49:46 -07001251}
1252
1253void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1254 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1255 SkXfermode::Mode mode, bool transforms, bool applyFilters,
Romain Guy03750a02010-10-18 14:06:08 -07001256 GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guy0a417492010-08-16 20:26:20 -07001257 // Describe the required shaders
1258 ProgramDescription description;
1259 description.hasTexture = true;
1260 description.hasAlpha8Texture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001261 const bool setColor = description.setAlpha8Color(r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -07001262
1263 if (applyFilters) {
1264 if (mShader) {
Romain Guy746b7402010-10-26 16:27:31 -07001265 mShader->describe(description, mCaches.extensions);
Romain Guy0a417492010-08-16 20:26:20 -07001266 }
1267 if (mColorFilter) {
Romain Guy746b7402010-10-26 16:27:31 -07001268 mColorFilter->describe(description, mCaches.extensions);
Romain Guy0a417492010-08-16 20:26:20 -07001269 }
1270 }
1271
Romain Guya5aed0d2010-09-09 14:42:43 -07001272 // Setup the blending mode
1273 chooseBlending(true, mode, description);
1274
Romain Guy0a417492010-08-16 20:26:20 -07001275 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001276 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -07001277
Romain Guy746b7402010-10-26 16:27:31 -07001278 bindTexture(texture);
Romain Guyfb8b7632010-08-23 21:05:08 -07001279 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001280
Romain Guyfb8b7632010-08-23 21:05:08 -07001281 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -07001282 glEnableVertexAttribArray(texCoordsSlot);
1283
Romain Guy03750a02010-10-18 14:06:08 -07001284 if (texCoords) {
1285 // Setup attributes
1286 if (!vertices) {
1287 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1288 } else {
1289 mCaches.unbindMeshBuffer();
1290 }
1291 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1292 gMeshStride, vertices);
1293 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1294 }
Romain Guy0a417492010-08-16 20:26:20 -07001295
1296 // Setup uniforms
1297 if (transforms) {
1298 mModelView.loadTranslate(x, y, 0.0f);
1299 mModelView.scale(width, height, 1.0f);
1300 } else {
1301 mModelView.loadIdentity();
1302 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001303
Romain Guy8aef54f2010-09-01 15:13:49 -07001304 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001305 if (width > 0 && height > 0) {
1306 dirtyLayer(x, y, x + width, y + height, *mSnapshot->transform);
1307 }
1308
Romain Guy707b2f72010-10-11 16:34:59 -07001309 if (setColor) {
1310 mCaches.currentProgram->setColor(r, g, b, a);
1311 }
Romain Guy0a417492010-08-16 20:26:20 -07001312
1313 textureUnit++;
1314 if (applyFilters) {
1315 // Setup attributes and uniforms required by the shaders
1316 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001317 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001318 }
1319 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001320 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -07001321 }
1322 }
1323}
1324
Romain Guyf607bdc2010-09-10 19:20:06 -07001325// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001326#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1327#define kStdUnderline_Offset (1.0f / 9.0f)
1328#define kStdUnderline_Thickness (1.0f / 18.0f)
1329
1330void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1331 float x, float y, SkPaint* paint) {
1332 // Handle underline and strike-through
1333 uint32_t flags = paint->getFlags();
1334 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1335 float underlineWidth = length;
1336 // If length is > 0.0f, we already measured the text for the text alignment
1337 if (length <= 0.0f) {
1338 underlineWidth = paint->measureText(text, bytesCount);
1339 }
1340
1341 float offsetX = 0;
1342 switch (paint->getTextAlign()) {
1343 case SkPaint::kCenter_Align:
1344 offsetX = underlineWidth * 0.5f;
1345 break;
1346 case SkPaint::kRight_Align:
1347 offsetX = underlineWidth;
1348 break;
1349 default:
1350 break;
1351 }
1352
1353 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001354 const float textSize = paint->getTextSize();
1355 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -07001356
Romain Guye20ecbd2010-09-22 19:49:04 -07001357 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001358 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001359
1360 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
1361 float points[pointsCount];
1362 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001363
1364 if (flags & SkPaint::kUnderlineText_Flag) {
1365 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001366 points[currentPoint++] = left;
1367 points[currentPoint++] = top;
1368 points[currentPoint++] = left + underlineWidth;
1369 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001370 }
1371
1372 if (flags & SkPaint::kStrikeThruText_Flag) {
1373 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001374 points[currentPoint++] = left;
1375 points[currentPoint++] = top;
1376 points[currentPoint++] = left + underlineWidth;
1377 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001378 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001379
1380 SkPaint linesPaint(*paint);
1381 linesPaint.setStrokeWidth(strokeWidth);
1382
1383 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001384 }
1385 }
1386}
1387
Romain Guy026c5e162010-06-28 17:12:22 -07001388void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001389 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy746b7402010-10-26 16:27:31 -07001390 setupDraw();
Romain Guy86942302010-09-12 13:02:16 -07001391
Romain Guyd27977d2010-07-14 19:18:51 -07001392 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001393 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001394 color |= 0x00ffffff;
1395 }
1396
1397 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -07001398 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -07001399 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -07001400 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1401 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1402 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1403
Romain Guyc95c8d62010-09-17 15:31:32 -07001404 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform);
1405
1406 // Draw the mesh
1407 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1408}
1409
1410void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom,
1411 float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy06f96e22010-07-30 19:18:16 -07001412 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -07001413
Romain Guy06f96e22010-07-30 19:18:16 -07001414 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -07001415 ProgramDescription description;
Romain Guy707b2f72010-10-11 16:34:59 -07001416 const bool setColor = description.setColor(r, g, b, a);
1417
Romain Guy06f96e22010-07-30 19:18:16 -07001418 if (mShader) {
Romain Guy746b7402010-10-26 16:27:31 -07001419 mShader->describe(description, mCaches.extensions);
Romain Guy6926c722010-07-12 20:20:03 -07001420 }
Romain Guydb1938e2010-08-02 18:50:22 -07001421 if (mColorFilter) {
Romain Guy746b7402010-10-26 16:27:31 -07001422 mColorFilter->describe(description, mCaches.extensions);
Romain Guydb1938e2010-08-02 18:50:22 -07001423 }
Romain Guyd27977d2010-07-14 19:18:51 -07001424
Romain Guy1c740bc2010-09-13 18:00:09 -07001425 // Setup the blending mode
Romain Guyc95c8d62010-09-17 15:31:32 -07001426 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -07001427
Romain Guy06f96e22010-07-30 19:18:16 -07001428 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001429 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -07001430
1431 // Setup attributes
Romain Guy03750a02010-10-18 14:06:08 -07001432 mCaches.bindMeshBuffer();
Romain Guyfb8b7632010-08-23 21:05:08 -07001433 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy03750a02010-10-18 14:06:08 -07001434 gMeshStride, 0);
Romain Guy06f96e22010-07-30 19:18:16 -07001435
1436 // Setup uniforms
1437 mModelView.loadTranslate(left, top, 0.0f);
1438 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -07001439 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001440 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001441 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -07001442 } else {
1443 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -07001444 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guy5b3b3522010-10-27 18:57:51 -07001445 dirtyLayer(left, top, right, bottom);
Romain Guyd27977d2010-07-14 19:18:51 -07001446 }
Romain Guy707b2f72010-10-11 16:34:59 -07001447 mCaches.currentProgram->setColor(r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -07001448
Romain Guy06f96e22010-07-30 19:18:16 -07001449 // Setup attributes and uniforms required by the shaders
1450 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001451 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001452 }
Romain Guydb1938e2010-08-02 18:50:22 -07001453 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001454 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001455 }
Romain Guyd27977d2010-07-14 19:18:51 -07001456}
1457
Romain Guy82ba8142010-07-09 13:25:56 -07001458void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001459 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001460 int alpha;
1461 SkXfermode::Mode mode;
1462 getAlphaAndMode(paint, &alpha, &mode);
1463
Romain Guy8164c2d2010-10-25 18:03:28 -07001464 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1465
Romain Guy6820ac82010-09-15 18:11:50 -07001466 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy03750a02010-10-18 14:06:08 -07001467 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy6820ac82010-09-15 18:11:50 -07001468 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -07001469}
1470
Romain Guybd6b79b2010-06-26 00:13:53 -07001471void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001472 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1473 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001474 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001475}
1476
1477void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001478 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001479 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07001480 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy746b7402010-10-26 16:27:31 -07001481 setupDraw();
Romain Guy86942302010-09-12 13:02:16 -07001482
Romain Guy889f8d12010-07-29 14:37:42 -07001483 ProgramDescription description;
1484 description.hasTexture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001485 const bool setColor = description.setColor(alpha, alpha, alpha, alpha);
Romain Guydb1938e2010-08-02 18:50:22 -07001486 if (mColorFilter) {
Romain Guy746b7402010-10-26 16:27:31 -07001487 mColorFilter->describe(description, mCaches.extensions);
Romain Guydb1938e2010-08-02 18:50:22 -07001488 }
Romain Guy889f8d12010-07-29 14:37:42 -07001489
Romain Guybd6b79b2010-06-26 00:13:53 -07001490 mModelView.loadTranslate(left, top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07001491 if (!ignoreScale) {
1492 mModelView.scale(right - left, bottom - top, 1.0f);
1493 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001494
Romain Guyf607bdc2010-09-10 19:20:06 -07001495 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001496
Romain Guyfb8b7632010-08-23 21:05:08 -07001497 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001498 if (!ignoreTransform) {
1499 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001500 if (dirty) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001501 } else {
Romain Guy5b3b3522010-10-27 18:57:51 -07001502 mat4 identity;
1503 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
1504 if (dirty) dirtyLayer(left, top, right, bottom);
Romain Guyf607bdc2010-09-10 19:20:06 -07001505 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001506
Romain Guy889f8d12010-07-29 14:37:42 -07001507 // Texture
Romain Guy8164c2d2010-10-25 18:03:28 -07001508 bindTexture(texture);
Romain Guyfb8b7632010-08-23 21:05:08 -07001509 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001510
Romain Guya9794742010-07-13 11:37:54 -07001511 // Always premultiplied
Romain Guy707b2f72010-10-11 16:34:59 -07001512 if (setColor) {
1513 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha);
1514 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001515
Romain Guy889f8d12010-07-29 14:37:42 -07001516 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001517 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001518 glEnableVertexAttribArray(texCoordsSlot);
Romain Guy03750a02010-10-18 14:06:08 -07001519
1520 if (!vertices) {
1521 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1522 } else {
1523 mCaches.unbindMeshBuffer();
1524 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001525 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001526 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001527 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001528
Romain Guydb1938e2010-08-02 18:50:22 -07001529 // Color filter
1530 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001531 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001532 }
1533
Romain Guy6820ac82010-09-15 18:11:50 -07001534 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001535 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001536}
1537
Romain Guya5aed0d2010-09-09 14:42:43 -07001538void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001539 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001540 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1541 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001542 if (mode < SkXfermode::kPlus_Mode) {
1543 if (!mCaches.blend) {
1544 glEnable(GL_BLEND);
1545 }
Romain Guy82ba8142010-07-09 13:25:56 -07001546
Romain Guyf607bdc2010-09-10 19:20:06 -07001547 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1548 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001549
Romain Guya5aed0d2010-09-09 14:42:43 -07001550 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1551 glBlendFunc(sourceMode, destMode);
1552 mCaches.lastSrcMode = sourceMode;
1553 mCaches.lastDstMode = destMode;
1554 }
1555 } else {
1556 // These blend modes are not supported by OpenGL directly and have
1557 // to be implemented using shaders. Since the shader will perform
1558 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001559 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001560 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001561 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001562 }
1563
1564 if (mCaches.blend) {
1565 glDisable(GL_BLEND);
1566 }
1567 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001568 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001569 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001570 glDisable(GL_BLEND);
1571 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001572 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001573}
1574
Romain Guy889f8d12010-07-29 14:37:42 -07001575bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001576 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001577 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001578 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001579 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001580 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001581 }
Romain Guy6926c722010-07-12 20:20:03 -07001582 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001583}
1584
Romain Guy026c5e162010-06-28 17:12:22 -07001585void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001586 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001587 TextureVertex::setUV(v++, u1, v1);
1588 TextureVertex::setUV(v++, u2, v1);
1589 TextureVertex::setUV(v++, u1, v2);
1590 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001591}
1592
Chet Haase5c13d892010-10-08 08:37:55 -07001593void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001594 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07001595 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001596 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1597 if (!isMode) {
1598 // Assume SRC_OVER
1599 *mode = SkXfermode::kSrcOver_Mode;
1600 }
1601 } else {
1602 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001603 }
1604
1605 // Skia draws using the color's alpha channel if < 255
1606 // Otherwise, it uses the paint's alpha
1607 int color = paint->getColor();
1608 *alpha = (color >> 24) & 0xFF;
1609 if (*alpha == 255) {
1610 *alpha = paint->getAlpha();
1611 }
1612 } else {
1613 *mode = SkXfermode::kSrcOver_Mode;
1614 *alpha = 255;
1615 }
Romain Guy026c5e162010-06-28 17:12:22 -07001616}
1617
Romain Guya5aed0d2010-09-09 14:42:43 -07001618SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1619 if (mode == NULL) {
1620 return SkXfermode::kSrcOver_Mode;
1621 }
1622 return mode->fMode;
1623}
1624
Romain Guy746b7402010-10-26 16:27:31 -07001625void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07001626 bool bound = false;
1627 if (wrapS != texture->wrapS) {
1628 glBindTexture(GL_TEXTURE_2D, texture->id);
1629 bound = true;
1630 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1631 texture->wrapS = wrapS;
1632 }
1633 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001634 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001635 glBindTexture(GL_TEXTURE_2D, texture->id);
1636 }
Romain Guy8164c2d2010-10-25 18:03:28 -07001637 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1638 texture->wrapT = wrapT;
1639 }
Romain Guya1db5742010-07-20 13:09:13 -07001640}
1641
Romain Guy9d5316e2010-06-24 19:30:36 -07001642}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001643}; // namespace android