blob: 540f11580900a96791b3f2d360f617a371b65d4f [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 Guy0fe478e2010-11-08 12:08:41 -080032#include "DisplayListRenderer.h"
Romain Guye4d01122010-06-16 18:44:05 -070033
34namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070035namespace uirenderer {
36
37///////////////////////////////////////////////////////////////////////////////
38// Defines
39///////////////////////////////////////////////////////////////////////////////
40
Romain Guy759ea802010-09-16 20:49:46 -070041#define RAD_TO_DEG (180.0f / 3.14159265f)
42#define MIN_ANGLE 0.001f
43
Romain Guydbc26d22010-10-11 17:58:29 -070044// TODO: This should be set in properties
45#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
46
Romain Guy9d5316e2010-06-24 19:30:36 -070047///////////////////////////////////////////////////////////////////////////////
48// Globals
49///////////////////////////////////////////////////////////////////////////////
50
Romain Guy889f8d12010-07-29 14:37:42 -070051/**
52 * Structure mapping Skia xfermodes to OpenGL blending factors.
53 */
54struct Blender {
55 SkXfermode::Mode mode;
56 GLenum src;
57 GLenum dst;
58}; // struct Blender
59
Romain Guy026c5e162010-06-28 17:12:22 -070060// In this array, the index of each Blender equals the value of the first
61// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
62static const Blender gBlends[] = {
63 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
64 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
65 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
66 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
67 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
68 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
69 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
70 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
71 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
73 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
74 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
75};
Romain Guye4d01122010-06-16 18:44:05 -070076
Romain Guy87a76572010-09-13 18:11:21 -070077// This array contains the swapped version of each SkXfermode. For instance
78// this array's SrcOver blending mode is actually DstOver. You can refer to
79// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070080static const Blender gBlendsSwap[] = {
81 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
82 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
83 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
84 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
85 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
86 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
87 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
88 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
89 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
90 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
91 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
92 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
93};
94
Romain Guy889f8d12010-07-29 14:37:42 -070095static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -070096 GL_TEXTURE0,
97 GL_TEXTURE1,
98 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -070099};
100
Romain Guyf6a11b82010-06-23 17:47:49 -0700101///////////////////////////////////////////////////////////////////////////////
102// Constructors/destructor
103///////////////////////////////////////////////////////////////////////////////
104
Romain Guyfb8b7632010-08-23 21:05:08 -0700105OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700106 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700107 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700108 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700109
Romain Guyac670c02010-07-27 17:39:27 -0700110 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
111
Romain Guyae5575b2010-07-29 18:48:04 -0700112 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700113}
114
Romain Guy85bf02f2010-06-22 13:11:24 -0700115OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700116 // The context has already been destroyed at this point, do not call
117 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700118}
119
Romain Guyf6a11b82010-06-23 17:47:49 -0700120///////////////////////////////////////////////////////////////////////////////
121// Setup
122///////////////////////////////////////////////////////////////////////////////
123
Romain Guy85bf02f2010-06-22 13:11:24 -0700124void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700125 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700126 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700127
128 mWidth = width;
129 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700130
131 mFirstSnapshot->height = height;
132 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700133
134 mDirtyClip = false;
Romain Guye4d01122010-06-16 18:44:05 -0700135}
136
Romain Guy6b7bd242010-10-06 19:49:23 -0700137void OpenGLRenderer::prepare(bool opaque) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700138 mSnapshot = new Snapshot(mFirstSnapshot,
139 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy8fb95422010-08-17 18:38:51 -0700140 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700141
Romain Guyfb8b7632010-08-23 21:05:08 -0700142 glViewport(0, 0, mWidth, mHeight);
143
Romain Guyd90f23e2010-09-09 11:47:54 -0700144 glDisable(GL_DITHER);
Romain Guybb9524b2010-06-22 18:56:38 -0700145
Romain Guy6b7bd242010-10-06 19:49:23 -0700146 if (!opaque) {
Romain Guy746b7402010-10-26 16:27:31 -0700147 glDisable(GL_SCISSOR_TEST);
Romain Guy6b7bd242010-10-06 19:49:23 -0700148 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
149 glClear(GL_COLOR_BUFFER_BIT);
150 }
Romain Guybb9524b2010-06-22 18:56:38 -0700151
Chet Haase0d200832010-11-05 15:36:16 -0700152 glEnable(GL_SCISSOR_TEST);
153 glScissor(0, 0, mWidth, mHeight);
Romain Guyb82da652010-07-30 11:36:12 -0700154 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700155}
156
Romain Guyb025b9c2010-09-16 14:16:48 -0700157void OpenGLRenderer::finish() {
158#if DEBUG_OPENGL
159 GLenum status = GL_NO_ERROR;
160 while ((status = glGetError()) != GL_NO_ERROR) {
161 LOGD("GL error from OpenGLRenderer: 0x%x", status);
162 }
163#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800164#if DEBUG_MEMORY_USAGE
165 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800166#else
167 if (mCaches.getDebugLevel() & kDebugMemory) {
168 mCaches.dumpMemoryUsage();
169 }
Romain Guyc15008e2010-11-10 11:59:15 -0800170#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700171}
172
Romain Guyda8532c2010-08-31 11:50:35 -0700173void OpenGLRenderer::acquireContext() {
174 if (mCaches.currentProgram) {
175 if (mCaches.currentProgram->isInUse()) {
176 mCaches.currentProgram->remove();
177 mCaches.currentProgram = NULL;
178 }
179 }
Romain Guy50c0f092010-10-19 11:42:22 -0700180 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700181}
182
183void OpenGLRenderer::releaseContext() {
Romain Guyeb993562010-10-05 18:14:38 -0700184 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700185
186 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700187 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700188
Romain Guyf607bdc2010-09-10 19:20:06 -0700189 glDisable(GL_DITHER);
190
191 glBindFramebuffer(GL_FRAMEBUFFER, 0);
Romain Guy03750a02010-10-18 14:06:08 -0700192 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700193
Romain Guy50c0f092010-10-19 11:42:22 -0700194 mCaches.blend = true;
195 glEnable(GL_BLEND);
196 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
197 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700198}
199
Romain Guyf6a11b82010-06-23 17:47:49 -0700200///////////////////////////////////////////////////////////////////////////////
201// State management
202///////////////////////////////////////////////////////////////////////////////
203
Romain Guybb9524b2010-06-22 18:56:38 -0700204int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700205 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700206}
207
208int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700209 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700210}
211
212void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700213 if (mSaveCount > 1) {
214 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700215 }
Romain Guybb9524b2010-06-22 18:56:38 -0700216}
217
218void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700219 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700220
Romain Guy8fb95422010-08-17 18:38:51 -0700221 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700222 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700223 }
Romain Guybb9524b2010-06-22 18:56:38 -0700224}
225
Romain Guy8aef54f2010-09-01 15:13:49 -0700226int OpenGLRenderer::saveSnapshot(int flags) {
227 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700228 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700229}
230
231bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700232 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700233 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700234 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700235
Romain Guybd6b79b2010-06-26 00:13:53 -0700236 sp<Snapshot> current = mSnapshot;
237 sp<Snapshot> previous = mSnapshot->previous;
238
Romain Guyeb993562010-10-05 18:14:38 -0700239 if (restoreOrtho) {
240 Rect& r = previous->viewport;
241 glViewport(r.left, r.top, r.right, r.bottom);
242 mOrthoMatrix.load(current->orthoMatrix);
243 }
244
Romain Guy8b55f372010-08-18 17:10:07 -0700245 mSaveCount--;
246 mSnapshot = previous;
247
Romain Guy2542d192010-08-18 11:47:12 -0700248 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700249 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700250 }
Romain Guy2542d192010-08-18 11:47:12 -0700251
Romain Guy5ec99242010-11-03 16:19:08 -0700252 if (restoreLayer) {
253 composeLayer(current, previous);
254 }
255
Romain Guy2542d192010-08-18 11:47:12 -0700256 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700257}
258
Romain Guyf6a11b82010-06-23 17:47:49 -0700259///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700260// Layers
261///////////////////////////////////////////////////////////////////////////////
262
263int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700264 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700265 const GLuint previousFbo = mSnapshot->fbo;
266 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700267
Romain Guye45362c2010-11-03 19:58:32 -0700268 if (!mSnapshot->invisible) {
269 int alpha = 255;
270 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700271
Romain Guye45362c2010-11-03 19:58:32 -0700272 if (p) {
273 alpha = p->getAlpha();
274 if (!mCaches.extensions.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 Guya5aed0d2010-09-09 14:42:43 -0700282 }
283 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700284 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700285 }
Romain Guyd55a8612010-06-28 17:42:46 -0700286
Romain Guydbc26d22010-10-11 17:58:29 -0700287 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
288 }
Romain Guyd55a8612010-06-28 17:42:46 -0700289
290 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700291}
292
293int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
294 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700295 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700296 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700297 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700298 SkPaint paint;
299 paint.setAlpha(alpha);
300 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700301 }
Romain Guyd55a8612010-06-28 17:42:46 -0700302}
Romain Guybd6b79b2010-06-26 00:13:53 -0700303
Romain Guy1c740bc2010-09-13 18:00:09 -0700304/**
305 * Layers are viewed by Skia are slightly different than layers in image editing
306 * programs (for instance.) When a layer is created, previously created layers
307 * and the frame buffer still receive every drawing command. For instance, if a
308 * layer is created and a shape intersecting the bounds of the layers and the
309 * framebuffer is draw, the shape will be drawn on both (unless the layer was
310 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
311 *
312 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
313 * texture. Unfortunately, this is inefficient as it requires every primitive to
314 * be drawn n + 1 times, where n is the number of active layers. In practice this
315 * means, for every primitive:
316 * - Switch active frame buffer
317 * - Change viewport, clip and projection matrix
318 * - Issue the drawing
319 *
320 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700321 * To avoid this, layers are implemented in a different way here, at least in the
322 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
323 * is set. When this flag is set we can redirect all drawing operations into a
324 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700325 *
326 * This implementation relies on the frame buffer being at least RGBA 8888. When
327 * a layer is created, only a texture is created, not an FBO. The content of the
328 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700329 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700330 * buffer and drawing continues as normal. This technique therefore treats the
331 * frame buffer as a scratch buffer for the layers.
332 *
333 * To compose the layers back onto the frame buffer, each layer texture
334 * (containing the original frame buffer data) is drawn as a simple quad over
335 * the frame buffer. The trick is that the quad is set as the composition
336 * destination in the blending equation, and the frame buffer becomes the source
337 * of the composition.
338 *
339 * Drawing layers with an alpha value requires an extra step before composition.
340 * An empty quad is drawn over the layer's region in the frame buffer. This quad
341 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
342 * quad is used to multiply the colors in the frame buffer. This is achieved by
343 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
344 * GL_ZERO, GL_SRC_ALPHA.
345 *
346 * Because glCopyTexImage2D() can be slow, an alternative implementation might
347 * be use to draw a single clipped layer. The implementation described above
348 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700349 *
350 * (1) The frame buffer is actually not cleared right away. To allow the GPU
351 * to potentially optimize series of calls to glCopyTexImage2D, the frame
352 * buffer is left untouched until the first drawing operation. Only when
353 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700354 */
Romain Guyd55a8612010-06-28 17:42:46 -0700355bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700356 float right, float bottom, int alpha, SkXfermode::Mode mode,
357 int flags, GLuint previousFbo) {
358 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700359 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700360
Romain Guyeb993562010-10-05 18:14:38 -0700361 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
362
Romain Guyf607bdc2010-09-10 19:20:06 -0700363 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700364 Rect bounds(left, top, right, bottom);
Romain Guyae88e5e2010-10-22 17:49:18 -0700365 if (fboLayer) {
366 // Clear the previous layer regions before we change the viewport
367 clearLayerRegions();
368 } else {
Romain Guyeb993562010-10-05 18:14:38 -0700369 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700370
Romain Guyeb993562010-10-05 18:14:38 -0700371 // Layers only make sense if they are in the framebuffer's bounds
Romain Guy58ae6db2010-10-22 11:16:37 -0700372 bounds.intersect(*snapshot->clipRect);
Romain Guyae517592010-10-22 10:40:27 -0700373
Romain Guyae88e5e2010-10-22 17:49:18 -0700374 // We cannot work with sub-pixels in this case
375 bounds.snapToPixelBoundaries();
376
Romain Guyae517592010-10-22 10:40:27 -0700377 // When the layer is not an FBO, we may use glCopyTexImage so we
378 // need to make sure the layer does not extend outside the bounds
379 // of the framebuffer
380 bounds.intersect(snapshot->previous->viewport);
Romain Guyeb993562010-10-05 18:14:38 -0700381 }
Romain Guybf434112010-09-16 14:40:17 -0700382
Romain Guy746b7402010-10-26 16:27:31 -0700383 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
384 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guydbc26d22010-10-11 17:58:29 -0700385 snapshot->invisible = true;
386 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700387 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700388 }
389
390 // Bail out if we won't draw in this snapshot
391 if (snapshot->invisible) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700392 return false;
393 }
Romain Guyf18fd992010-07-08 11:45:51 -0700394
Romain Guy5b3b3522010-10-27 18:57:51 -0700395 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700396 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700397 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700398 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700399 }
400
Romain Guydda57022010-07-06 11:39:32 -0700401 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700402 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700403 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700404 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
405 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guydda57022010-07-06 11:39:32 -0700406
Romain Guy8fb95422010-08-17 18:38:51 -0700407 // Save the layer in the snapshot
408 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700409 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700410
Romain Guyeb993562010-10-05 18:14:38 -0700411 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700412 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700413 } else {
414 // Copy the framebuffer into the layer
415 glBindTexture(GL_TEXTURE_2D, layer->texture);
416
Romain Guyae88e5e2010-10-22 17:49:18 -0700417 if (layer->empty) {
418 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
419 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
420 layer->empty = false;
421 } else {
422 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
423 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
424 }
Romain Guyeb993562010-10-05 18:14:38 -0700425
426 // Enqueue the buffer coordinates to clear the corresponding region later
427 mLayers.push(new Rect(bounds));
428 }
Romain Guyf86ef572010-07-01 11:05:42 -0700429
Romain Guyd55a8612010-06-28 17:42:46 -0700430 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700431}
432
Romain Guy5b3b3522010-10-27 18:57:51 -0700433bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
434 GLuint previousFbo) {
435 layer->fbo = mCaches.fboCache.get();
436
437#if RENDER_LAYERS_AS_REGIONS
438 snapshot->region = &snapshot->layer->region;
439 snapshot->flags |= Snapshot::kFlagFboTarget;
440#endif
441
442 Rect clip(bounds);
443 snapshot->transform->mapRect(clip);
444 clip.intersect(*snapshot->clipRect);
445 clip.snapToPixelBoundaries();
446 clip.intersect(snapshot->previous->viewport);
447
448 mat4 inverse;
449 inverse.loadInverse(*mSnapshot->transform);
450
451 inverse.mapRect(clip);
452 clip.snapToPixelBoundaries();
453 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700454 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700455
456 snapshot->flags |= Snapshot::kFlagIsFboLayer;
457 snapshot->fbo = layer->fbo;
458 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
459 //snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
460 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
461 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
462 snapshot->height = bounds.getHeight();
463 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
464 snapshot->orthoMatrix.load(mOrthoMatrix);
465
466 // Bind texture to FBO
467 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
468 glBindTexture(GL_TEXTURE_2D, layer->texture);
469
470 // Initialize the texture if needed
471 if (layer->empty) {
472 layer->empty = false;
473 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
474 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
475 }
476
477 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
478 layer->texture, 0);
479
480#if DEBUG_LAYERS_AS_REGIONS
481 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
482 if (status != GL_FRAMEBUFFER_COMPLETE) {
483 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
484
485 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
486 glDeleteTextures(1, &layer->texture);
487 mCaches.fboCache.put(layer->fbo);
488
489 delete layer;
490
491 return false;
492 }
493#endif
494
495 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
496 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
497 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
498 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
499 glClear(GL_COLOR_BUFFER_BIT);
500
501 dirtyClip();
502
503 // Change the ortho projection
504 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
505 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
506
507 return true;
508}
509
Romain Guy1c740bc2010-09-13 18:00:09 -0700510/**
511 * Read the documentation of createLayer() before doing anything in this method.
512 */
Romain Guy1d83e192010-08-17 11:37:00 -0700513void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
514 if (!current->layer) {
515 LOGE("Attempting to compose a layer that does not exist");
516 return;
517 }
518
Romain Guy5b3b3522010-10-27 18:57:51 -0700519 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700520
521 if (fboLayer) {
522 // Unbind current FBO and restore previous one
523 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
524 }
525
Romain Guy1d83e192010-08-17 11:37:00 -0700526 Layer* layer = current->layer;
527 const Rect& rect = layer->layer;
528
Romain Guyeb993562010-10-05 18:14:38 -0700529 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700530 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700531 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700532 // Required below, composeLayerRect() will divide by 255
533 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700534 }
535
Romain Guy03750a02010-10-18 14:06:08 -0700536 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700537
Romain Guy746b7402010-10-26 16:27:31 -0700538 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700539
Romain Guy5b3b3522010-10-27 18:57:51 -0700540 // When the layer is stored in an FBO, we can save a bit of fillrate by
541 // drawing only the dirty region
542 if (fboLayer) {
543 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
544 composeLayerRegion(layer, rect);
545 } else {
546 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
547 composeLayerRect(layer, rect, true);
548 }
Romain Guy8b55f372010-08-18 17:10:07 -0700549
Romain Guyeb993562010-10-05 18:14:38 -0700550 if (fboLayer) {
551 // Detach the texture from the FBO
552 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
553 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
554 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
555
556 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
557 mCaches.fboCache.put(current->fbo);
558 }
559
Romain Guy746b7402010-10-26 16:27:31 -0700560 dirtyClip();
561
Romain Guyeb993562010-10-05 18:14:38 -0700562 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700563 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700564 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700565 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700566 delete layer;
567 }
568}
569
Romain Guy5b3b3522010-10-27 18:57:51 -0700570void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
571 const Rect& texCoords = layer->texCoords;
572 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
573
574 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
575 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
576 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
577
578 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
579}
580
581void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
582#if RENDER_LAYERS_AS_REGIONS
583 if (layer->region.isRect()) {
584 composeLayerRect(layer, rect);
585 layer->region.clear();
586 return;
587 }
588
589 if (!layer->region.isEmpty()) {
590 size_t count;
591 const android::Rect* rects = layer->region.getArray(&count);
592
593 setupDraw();
594
595 ProgramDescription description;
596 description.hasTexture = true;
597
598 const float alpha = layer->alpha / 255.0f;
599 const bool setColor = description.setColor(alpha, alpha, alpha, alpha);
600 chooseBlending(layer->blend || layer->alpha < 255, layer->mode, description, false);
601
602 useProgram(mCaches.programCache.get(description));
603
604 // Texture
605 bindTexture(layer->texture);
606 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
607
608 // Always premultiplied
609 if (setColor) {
610 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha);
611 }
612
613 // Mesh
614 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
615 glEnableVertexAttribArray(texCoordsSlot);
616
617 mModelView.loadIdentity();
618 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
619
620 const float texX = 1.0f / float(layer->width);
621 const float texY = 1.0f / float(layer->height);
622
623 TextureVertex* mesh = mCaches.getRegionMesh();
624 GLsizei numQuads = 0;
625
626 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
627 gMeshStride, &mesh[0].position[0]);
628 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
629 gMeshStride, &mesh[0].texture[0]);
630
631 for (size_t i = 0; i < count; i++) {
632 const android::Rect* r = &rects[i];
633
634 const float u1 = r->left * texX;
635 const float v1 = (rect.getHeight() - r->top) * texY;
636 const float u2 = r->right * texX;
637 const float v2 = (rect.getHeight() - r->bottom) * texY;
638
639 // TODO: Reject quads outside of the clip
640 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
641 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
642 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
643 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
644
645 numQuads++;
646
647 if (numQuads >= REGION_MESH_QUAD_COUNT) {
648 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
649 numQuads = 0;
650 mesh = mCaches.getRegionMesh();
651 }
652 }
653
654 if (numQuads > 0) {
655 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
656 }
657
658 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
659 glDisableVertexAttribArray(texCoordsSlot);
660
661#if DEBUG_LAYERS_AS_REGIONS
662 uint32_t colors[] = {
663 0x7fff0000, 0x7f00ff00,
664 0x7f0000ff, 0x7fff00ff,
665 };
666
667 int offset = 0;
668 int32_t top = rects[0].top;
669 int i = 0;
670
671 for (size_t i = 0; i < count; i++) {
672 if (top != rects[i].top) {
673 offset ^= 0x2;
674 top = rects[i].top;
675 }
676
677 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
678 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
679 SkXfermode::kSrcOver_Mode);
680 }
681#endif
682
683 layer->region.clear();
684 }
685#else
686 composeLayerRect(layer, rect);
687#endif
688}
689
690void OpenGLRenderer::dirtyLayer(const float left, const float top,
691 const float right, const float bottom, const mat4 transform) {
692#if RENDER_LAYERS_AS_REGIONS
693 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region) {
694 Rect bounds(left, top, right, bottom);
695 transform.mapRect(bounds);
696 bounds.intersect(*mSnapshot->clipRect);
697 bounds.snapToPixelBoundaries();
698
699 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
700 if (!dirty.isEmpty()) {
701 mSnapshot->region->orSelf(dirty);
702 }
703 }
704#endif
705}
706
707void OpenGLRenderer::dirtyLayer(const float left, const float top,
708 const float right, const float bottom) {
709#if RENDER_LAYERS_AS_REGIONS
710 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region) {
711 Rect bounds(left, top, right, bottom);
712 bounds.intersect(*mSnapshot->clipRect);
713 bounds.snapToPixelBoundaries();
714
715 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
716 if (!dirty.isEmpty()) {
717 mSnapshot->region->orSelf(dirty);
718 }
719 }
720#endif
721}
722
Romain Guy746b7402010-10-26 16:27:31 -0700723void OpenGLRenderer::setupDraw() {
724 clearLayerRegions();
725 if (mDirtyClip) {
726 setScissorFromClip();
727 }
728}
729
Romain Guy86942302010-09-12 13:02:16 -0700730void OpenGLRenderer::clearLayerRegions() {
Romain Guydbc26d22010-10-11 17:58:29 -0700731 if (mLayers.size() == 0 || mSnapshot->invisible) return;
Romain Guy86942302010-09-12 13:02:16 -0700732
Romain Guy5b3b3522010-10-27 18:57:51 -0700733 Rect clipRect(*mSnapshot->clipRect);
734 clipRect.snapToPixelBoundaries();
735
Romain Guy86942302010-09-12 13:02:16 -0700736 for (uint32_t i = 0; i < mLayers.size(); i++) {
737 Rect* bounds = mLayers.itemAt(i);
Romain Guy5b3b3522010-10-27 18:57:51 -0700738 if (clipRect.intersects(*bounds)) {
739 // Clear the framebuffer where the layer will draw
740 glScissor(bounds->left, mSnapshot->height - bounds->bottom,
741 bounds->getWidth(), bounds->getHeight());
742 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
743 glClear(GL_COLOR_BUFFER_BIT);
Romain Guy86942302010-09-12 13:02:16 -0700744
Romain Guy5b3b3522010-10-27 18:57:51 -0700745 // Restore the clip
746 dirtyClip();
747 }
Romain Guy86942302010-09-12 13:02:16 -0700748
749 delete bounds;
750 }
Romain Guy86942302010-09-12 13:02:16 -0700751
Romain Guy5b3b3522010-10-27 18:57:51 -0700752 mLayers.clear();
Romain Guy86942302010-09-12 13:02:16 -0700753}
754
Romain Guybd6b79b2010-06-26 00:13:53 -0700755///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700756// Transforms
757///////////////////////////////////////////////////////////////////////////////
758
759void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700760 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700761}
762
763void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700764 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700765}
766
767void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700768 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700769}
770
771void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700772 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700773}
774
Romain Guy41030da2010-10-13 13:40:37 -0700775const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700776 if (mSnapshot->fbo != 0) {
777 return &mSnapshot->transform->data[0];
778 }
779 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700780}
781
Romain Guyf6a11b82010-06-23 17:47:49 -0700782void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700783 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700784}
785
786void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700787 SkMatrix transform;
788 mSnapshot->transform->copyTo(transform);
789 transform.preConcat(*matrix);
790 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700791}
792
793///////////////////////////////////////////////////////////////////////////////
794// Clipping
795///////////////////////////////////////////////////////////////////////////////
796
Romain Guybb9524b2010-06-22 18:56:38 -0700797void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700798 Rect clip(*mSnapshot->clipRect);
799 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700800 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700801 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700802}
803
804const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700805 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700806}
807
Romain Guyc7d53492010-06-25 13:41:57 -0700808bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guydbc26d22010-10-11 17:58:29 -0700809 if (mSnapshot->invisible) {
810 return true;
811 }
812
Romain Guy1d83e192010-08-17 11:37:00 -0700813 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700814 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700815 r.snapToPixelBoundaries();
816
817 Rect clipRect(*mSnapshot->clipRect);
818 clipRect.snapToPixelBoundaries();
819
820 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700821}
822
Romain Guy079ba2c2010-07-16 14:12:24 -0700823bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
824 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700825 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700826 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700827 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700828 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700829}
830
Romain Guyf6a11b82010-06-23 17:47:49 -0700831///////////////////////////////////////////////////////////////////////////////
832// Drawing
833///////////////////////////////////////////////////////////////////////////////
834
Romain Guy0fe478e2010-11-08 12:08:41 -0800835void OpenGLRenderer::drawDisplayList(DisplayList* displayList) {
836 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
837 // will be performed by the display list itself
838 if (displayList) {
839 displayList->replay(*this);
840 }
841}
842
Chet Haase5c13d892010-10-08 08:37:55 -0700843void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700844 const float right = left + bitmap->width();
845 const float bottom = top + bitmap->height();
846
847 if (quickReject(left, top, right, bottom)) {
848 return;
849 }
850
Romain Guy61c8c9c2010-08-09 20:48:09 -0700851 glActiveTexture(GL_TEXTURE0);
Romain Guy8164c2d2010-10-25 18:03:28 -0700852 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700853 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700854 const AutoTexture autoCleanup(texture);
855
Romain Guy6926c722010-07-12 20:20:03 -0700856 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700857}
858
Chet Haase5c13d892010-10-08 08:37:55 -0700859void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -0700860 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
861 const mat4 transform(*matrix);
862 transform.mapRect(r);
863
Romain Guy6926c722010-07-12 20:20:03 -0700864 if (quickReject(r.left, r.top, r.right, r.bottom)) {
865 return;
866 }
867
Romain Guy61c8c9c2010-08-09 20:48:09 -0700868 glActiveTexture(GL_TEXTURE0);
Romain Guy8164c2d2010-10-25 18:03:28 -0700869 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700870 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700871 const AutoTexture autoCleanup(texture);
872
Romain Guy5b3b3522010-10-27 18:57:51 -0700873 // This could be done in a cheaper way, all we need is pass the matrix
874 // to the vertex shader. The save/restore is a bit overkill.
875 save(SkCanvas::kMatrix_SaveFlag);
876 concatMatrix(matrix);
877 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
878 restore();
Romain Guyf86ef572010-07-01 11:05:42 -0700879}
880
Romain Guy8ba548f2010-06-30 19:21:21 -0700881void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
882 float srcLeft, float srcTop, float srcRight, float srcBottom,
883 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700884 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700885 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
886 return;
887 }
888
Romain Guy746b7402010-10-26 16:27:31 -0700889 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -0700890 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700891 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700892 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -0700893 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -0700894
Romain Guy8ba548f2010-06-30 19:21:21 -0700895 const float width = texture->width;
896 const float height = texture->height;
897
898 const float u1 = srcLeft / width;
899 const float v1 = srcTop / height;
900 const float u2 = srcRight / width;
901 const float v2 = srcBottom / height;
902
Romain Guy03750a02010-10-18 14:06:08 -0700903 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -0700904 resetDrawTextureTexCoords(u1, v1, u2, v2);
905
Romain Guy03750a02010-10-18 14:06:08 -0700906 int alpha;
907 SkXfermode::Mode mode;
908 getAlphaAndMode(paint, &alpha, &mode);
909
910 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
911 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
912 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy8ba548f2010-06-30 19:21:21 -0700913
914 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
915}
916
Romain Guy4aa90572010-09-26 18:40:37 -0700917void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -0700918 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -0700919 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700920 if (quickReject(left, top, right, bottom)) {
921 return;
922 }
923
Romain Guy746b7402010-10-26 16:27:31 -0700924 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -0700925 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700926 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700927 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -0700928 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -0700929
930 int alpha;
931 SkXfermode::Mode mode;
932 getAlphaAndMode(paint, &alpha, &mode);
933
Romain Guy2728f962010-10-08 18:36:15 -0700934 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -0700935 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -0700936
Romain Guy054dc182010-10-15 17:55:25 -0700937 if (mesh) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700938 // Mark the current layer dirty where we are going to draw the patch
939 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) &&
940 mSnapshot->region && mesh->hasEmptyQuads) {
941 const size_t count = mesh->quads.size();
942 for (size_t i = 0; i < count; i++) {
943 Rect bounds = mesh->quads.itemAt(i);
944 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom,
945 *mSnapshot->transform);
946 }
947 }
948
949 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
Romain Guy03750a02010-10-18 14:06:08 -0700950 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
Romain Guy5b3b3522010-10-27 18:57:51 -0700951 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
952 true, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -0700953 }
Romain Guyf7f93552010-07-08 19:17:03 -0700954}
955
Chet Haase5c13d892010-10-08 08:37:55 -0700956void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guy7f78b0c2010-11-04 14:36:26 -0700957 // TODO: Should do quickReject for each line
Romain Guydbc26d22010-10-11 17:58:29 -0700958 if (mSnapshot->invisible) return;
959
Romain Guy7f78b0c2010-11-04 14:36:26 -0700960 setupDraw();
961
Romain Guy759ea802010-09-16 20:49:46 -0700962 int alpha;
963 SkXfermode::Mode mode;
964 getAlphaAndMode(paint, &alpha, &mode);
965
966 uint32_t color = paint->getColor();
967 const GLfloat a = alpha / 255.0f;
968 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
969 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
970 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
971
Romain Guyc95c8d62010-09-17 15:31:32 -0700972 const bool isAA = paint->isAntiAlias();
973 if (isAA) {
974 GLuint textureUnit = 0;
Romain Guy746b7402010-10-26 16:27:31 -0700975 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy29d89972010-09-22 16:10:57 -0700976 setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
Romain Guy03750a02010-10-18 14:06:08 -0700977 mode, false, true, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
978 mCaches.line.getMeshBuffer());
Romain Guyc95c8d62010-09-17 15:31:32 -0700979 } else {
Romain Guy7f78b0c2010-11-04 14:36:26 -0700980 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false, true);
Romain Guyc95c8d62010-09-17 15:31:32 -0700981 }
982
983 const float strokeWidth = paint->getStrokeWidth();
Romain Guy29d89972010-09-22 16:10:57 -0700984 const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount;
Romain Guyc95c8d62010-09-17 15:31:32 -0700985 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
Romain Guy759ea802010-09-16 20:49:46 -0700986
987 for (int i = 0; i < count; i += 4) {
988 float tx = 0.0f;
989 float ty = 0.0f;
990
Romain Guyc95c8d62010-09-17 15:31:32 -0700991 if (isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700992 mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3],
Romain Guyc95c8d62010-09-17 15:31:32 -0700993 strokeWidth, tx, ty);
994 } else {
Romain Guyb5ab4172010-09-17 15:36:56 -0700995 ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f;
Romain Guyc95c8d62010-09-17 15:31:32 -0700996 }
Romain Guy759ea802010-09-16 20:49:46 -0700997
998 const float dx = points[i + 2] - points[i];
999 const float dy = points[i + 3] - points[i + 1];
1000 const float mag = sqrtf(dx * dx + dy * dy);
1001 const float angle = acos(dx / mag);
1002
1003 mModelView.loadTranslate(points[i], points[i + 1], 0.0f);
1004 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) {
1005 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f);
1006 }
1007 mModelView.translate(tx, ty, 0.0f);
Romain Guyc95c8d62010-09-17 15:31:32 -07001008 if (!isAA) {
Romain Guy29d89972010-09-22 16:10:57 -07001009 float length = mCaches.line.getLength(points[i], points[i + 1],
1010 points[i + 2], points[i + 3]);
Romain Guyc95c8d62010-09-17 15:31:32 -07001011 mModelView.scale(length, strokeWidth, 1.0f);
1012 }
Romain Guy759ea802010-09-16 20:49:46 -07001013 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001014 // TODO: Add bounds to the layer's region
Romain Guy759ea802010-09-16 20:49:46 -07001015
1016 if (mShader) {
1017 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot);
1018 }
1019
Romain Guyc95c8d62010-09-17 15:31:32 -07001020 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy759ea802010-09-16 20:49:46 -07001021 }
1022
Romain Guy29d89972010-09-22 16:10:57 -07001023 if (isAA) {
1024 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
1025 }
Romain Guy759ea802010-09-16 20:49:46 -07001026}
1027
Romain Guy85bf02f2010-06-22 13:11:24 -07001028void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001029 // No need to check against the clip, we fill the clip region
1030 if (mSnapshot->invisible) return;
1031
Romain Guyae88e5e2010-10-22 17:49:18 -07001032 Rect& clip(*mSnapshot->clipRect);
1033 clip.snapToPixelBoundaries();
Romain Guy3d58c032010-07-14 16:34:53 -07001034 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001035}
Romain Guy9d5316e2010-06-24 19:30:36 -07001036
Chet Haase5c13d892010-10-08 08:37:55 -07001037void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -07001038 if (quickReject(left, top, right, bottom)) {
1039 return;
1040 }
1041
Romain Guy026c5e162010-06-28 17:12:22 -07001042 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001043 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001044 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1045 if (!isMode) {
1046 // Assume SRC_OVER
1047 mode = SkXfermode::kSrcOver_Mode;
1048 }
1049 } else {
1050 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001051 }
1052
1053 // Skia draws using the color's alpha channel if < 255
1054 // Otherwise, it uses the paint's alpha
1055 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07001056 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -07001057 color |= p->getAlpha() << 24;
1058 }
1059
1060 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001061}
Romain Guy9d5316e2010-06-24 19:30:36 -07001062
Romain Guye8e62a42010-07-23 18:55:21 -07001063void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1064 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001065 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07001066 return;
1067 }
Romain Guydbc26d22010-10-11 17:58:29 -07001068 if (mSnapshot->invisible) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001069
Romain Guyf607bdc2010-09-10 19:20:06 -07001070 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001071
Romain Guya674ab72010-08-10 17:26:42 -07001072 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001073 switch (paint->getTextAlign()) {
1074 case SkPaint::kCenter_Align:
1075 length = paint->measureText(text, bytesCount);
1076 x -= length / 2.0f;
1077 break;
1078 case SkPaint::kRight_Align:
1079 length = paint->measureText(text, bytesCount);
1080 x -= length;
1081 break;
1082 default:
1083 break;
1084 }
1085
Romain Guy694b5192010-07-21 21:33:20 -07001086 int alpha;
1087 SkXfermode::Mode mode;
1088 getAlphaAndMode(paint, &alpha, &mode);
1089
Romain Guy2542d192010-08-18 11:47:12 -07001090 uint32_t color = paint->getColor();
1091 const GLfloat a = alpha / 255.0f;
1092 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1093 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1094 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1095
Romain Guyb45c0c92010-08-26 20:35:23 -07001096 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1097 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001098 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001099
Romain Guy746b7402010-10-26 16:27:31 -07001100 setupDraw();
Romain Guy9d13fe252010-10-15 16:06:03 -07001101
Romain Guy1e45aae2010-08-13 19:39:53 -07001102 if (mHasShadow) {
1103 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -07001104 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001105 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001106 count, mShadowRadius);
1107 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001108
Romain Guy2542d192010-08-18 11:47:12 -07001109 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -07001110
1111 // Draw the mesh
1112 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -07001113 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -07001114 }
1115
Romain Guy06f96e22010-07-30 19:18:16 -07001116 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -07001117 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -07001118
Romain Guye8cb9c142010-10-04 14:14:11 -07001119 // Assume that the modelView matrix does not force scales, rotates, etc.
1120 const bool linearFilter = mSnapshot->transform->changesBounds();
Romain Guy5b3b3522010-10-27 18:57:51 -07001121
1122 // Dimensions are set to (0,0), the layer (if any) won't be dirtied
Romain Guye8cb9c142010-10-04 14:14:11 -07001123 setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit,
Romain Guy03750a02010-10-18 14:06:08 -07001124 x, y, r, g, b, a, mode, false, true, NULL, NULL);
Romain Guy06f96e22010-07-30 19:18:16 -07001125
Romain Guy09147fb2010-07-22 13:08:20 -07001126 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001127 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1128
1129#if RENDER_LAYERS_AS_REGIONS
1130 bool hasLayer = (mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region;
1131#else
1132 bool hasLayer = false;
1133#endif
Romain Guy9d13fe252010-10-15 16:06:03 -07001134
Romain Guy03750a02010-10-18 14:06:08 -07001135 mCaches.unbindMeshBuffer();
Romain Guy5b3b3522010-10-27 18:57:51 -07001136 if (fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y,
1137 hasLayer ? &bounds : NULL)) {
1138#if RENDER_LAYERS_AS_REGIONS
1139 if (hasLayer) {
1140 mSnapshot->transform->mapRect(bounds);
1141 bounds.intersect(*mSnapshot->clipRect);
1142 bounds.snapToPixelBoundaries();
1143
1144 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1145 mSnapshot->region->orSelf(dirty);
1146 }
1147#endif
1148 }
Romain Guy694b5192010-07-21 21:33:20 -07001149
1150 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001151 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001152
Romain Guy0a417492010-08-16 20:26:20 -07001153 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001154}
1155
Romain Guy7fbcc042010-08-04 15:40:07 -07001156void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -07001157 if (mSnapshot->invisible) return;
1158
Romain Guy7fbcc042010-08-04 15:40:07 -07001159 GLuint textureUnit = 0;
1160 glActiveTexture(gTextureUnits[textureUnit]);
1161
Romain Guyfb8b7632010-08-23 21:05:08 -07001162 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001163 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001164 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001165
Romain Guy8b55f372010-08-18 17:10:07 -07001166 const float x = texture->left - texture->offset;
1167 const float y = texture->top - texture->offset;
1168
1169 if (quickReject(x, y, x + texture->width, y + texture->height)) {
1170 return;
1171 }
1172
Romain Guy7fbcc042010-08-04 15:40:07 -07001173 int alpha;
1174 SkXfermode::Mode mode;
1175 getAlphaAndMode(paint, &alpha, &mode);
1176
1177 uint32_t color = paint->getColor();
1178 const GLfloat a = alpha / 255.0f;
1179 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1180 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1181 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1182
Romain Guy0a417492010-08-16 20:26:20 -07001183 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
1184
Romain Guy746b7402010-10-26 16:27:31 -07001185 setupDraw();
Romain Guy86942302010-09-12 13:02:16 -07001186
Romain Guy0a417492010-08-16 20:26:20 -07001187 // Draw the mesh
1188 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -07001189 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -07001190}
1191
Romain Guy6926c722010-07-12 20:20:03 -07001192///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001193// Shaders
1194///////////////////////////////////////////////////////////////////////////////
1195
1196void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001197 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001198}
1199
Romain Guy06f96e22010-07-30 19:18:16 -07001200void OpenGLRenderer::setupShader(SkiaShader* shader) {
1201 mShader = shader;
1202 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001203 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001204 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001205}
1206
Romain Guyd27977d2010-07-14 19:18:51 -07001207///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001208// Color filters
1209///////////////////////////////////////////////////////////////////////////////
1210
1211void OpenGLRenderer::resetColorFilter() {
1212 mColorFilter = NULL;
1213}
1214
1215void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1216 mColorFilter = filter;
1217}
1218
1219///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07001220// Drop shadow
1221///////////////////////////////////////////////////////////////////////////////
1222
1223void OpenGLRenderer::resetShadow() {
1224 mHasShadow = false;
1225}
1226
1227void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
1228 mHasShadow = true;
1229 mShadowRadius = radius;
1230 mShadowDx = dx;
1231 mShadowDy = dy;
1232 mShadowColor = color;
1233}
1234
1235///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001236// Drawing implementation
1237///////////////////////////////////////////////////////////////////////////////
1238
Romain Guy0a417492010-08-16 20:26:20 -07001239void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -07001240 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -07001241 const float sx = x - texture->left + mShadowDx;
1242 const float sy = y - texture->top + mShadowDy;
1243
Romain Guy2542d192010-08-18 11:47:12 -07001244 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1245 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -07001246 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
1247 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
1248 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
1249
1250 GLuint textureUnit = 0;
1251 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
1252}
1253
1254void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
1255 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
1256 bool transforms, bool applyFilters) {
1257 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -07001258 x, y, r, g, b, a, mode, transforms, applyFilters,
Romain Guy03750a02010-10-18 14:06:08 -07001259 (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy0a417492010-08-16 20:26:20 -07001260}
1261
1262void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1263 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1264 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy03750a02010-10-18 14:06:08 -07001265 setupTextureAlpha8(texture, width, height, textureUnit, x, y, r, g, b, a, mode,
1266 transforms, applyFilters, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy759ea802010-09-16 20:49:46 -07001267}
1268
1269void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1270 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1271 SkXfermode::Mode mode, bool transforms, bool applyFilters,
Romain Guy03750a02010-10-18 14:06:08 -07001272 GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guy0a417492010-08-16 20:26:20 -07001273 // Describe the required shaders
1274 ProgramDescription description;
1275 description.hasTexture = true;
1276 description.hasAlpha8Texture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001277 const bool setColor = description.setAlpha8Color(r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -07001278
1279 if (applyFilters) {
1280 if (mShader) {
Romain Guy746b7402010-10-26 16:27:31 -07001281 mShader->describe(description, mCaches.extensions);
Romain Guy0a417492010-08-16 20:26:20 -07001282 }
1283 if (mColorFilter) {
Romain Guy746b7402010-10-26 16:27:31 -07001284 mColorFilter->describe(description, mCaches.extensions);
Romain Guy0a417492010-08-16 20:26:20 -07001285 }
1286 }
1287
Romain Guya5aed0d2010-09-09 14:42:43 -07001288 // Setup the blending mode
1289 chooseBlending(true, mode, description);
1290
Romain Guy0a417492010-08-16 20:26:20 -07001291 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001292 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -07001293
Romain Guy746b7402010-10-26 16:27:31 -07001294 bindTexture(texture);
Romain Guyfb8b7632010-08-23 21:05:08 -07001295 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001296
Romain Guyfb8b7632010-08-23 21:05:08 -07001297 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -07001298 glEnableVertexAttribArray(texCoordsSlot);
1299
Romain Guy03750a02010-10-18 14:06:08 -07001300 if (texCoords) {
1301 // Setup attributes
1302 if (!vertices) {
1303 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1304 } else {
1305 mCaches.unbindMeshBuffer();
1306 }
1307 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1308 gMeshStride, vertices);
1309 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1310 }
Romain Guy0a417492010-08-16 20:26:20 -07001311
1312 // Setup uniforms
1313 if (transforms) {
1314 mModelView.loadTranslate(x, y, 0.0f);
1315 mModelView.scale(width, height, 1.0f);
1316 } else {
1317 mModelView.loadIdentity();
1318 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001319
Romain Guy8aef54f2010-09-01 15:13:49 -07001320 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001321 if (width > 0 && height > 0) {
1322 dirtyLayer(x, y, x + width, y + height, *mSnapshot->transform);
1323 }
1324
Romain Guy707b2f72010-10-11 16:34:59 -07001325 if (setColor) {
1326 mCaches.currentProgram->setColor(r, g, b, a);
1327 }
Romain Guy0a417492010-08-16 20:26:20 -07001328
1329 textureUnit++;
1330 if (applyFilters) {
1331 // Setup attributes and uniforms required by the shaders
1332 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001333 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001334 }
1335 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001336 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -07001337 }
1338 }
1339}
1340
Romain Guyf607bdc2010-09-10 19:20:06 -07001341// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001342#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1343#define kStdUnderline_Offset (1.0f / 9.0f)
1344#define kStdUnderline_Thickness (1.0f / 18.0f)
1345
1346void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1347 float x, float y, SkPaint* paint) {
1348 // Handle underline and strike-through
1349 uint32_t flags = paint->getFlags();
1350 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1351 float underlineWidth = length;
1352 // If length is > 0.0f, we already measured the text for the text alignment
1353 if (length <= 0.0f) {
1354 underlineWidth = paint->measureText(text, bytesCount);
1355 }
1356
1357 float offsetX = 0;
1358 switch (paint->getTextAlign()) {
1359 case SkPaint::kCenter_Align:
1360 offsetX = underlineWidth * 0.5f;
1361 break;
1362 case SkPaint::kRight_Align:
1363 offsetX = underlineWidth;
1364 break;
1365 default:
1366 break;
1367 }
1368
1369 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001370 const float textSize = paint->getTextSize();
1371 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -07001372
Romain Guye20ecbd2010-09-22 19:49:04 -07001373 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001374 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001375
1376 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
1377 float points[pointsCount];
1378 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001379
1380 if (flags & SkPaint::kUnderlineText_Flag) {
1381 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001382 points[currentPoint++] = left;
1383 points[currentPoint++] = top;
1384 points[currentPoint++] = left + underlineWidth;
1385 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001386 }
1387
1388 if (flags & SkPaint::kStrikeThruText_Flag) {
1389 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001390 points[currentPoint++] = left;
1391 points[currentPoint++] = top;
1392 points[currentPoint++] = left + underlineWidth;
1393 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001394 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001395
1396 SkPaint linesPaint(*paint);
1397 linesPaint.setStrokeWidth(strokeWidth);
1398
1399 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001400 }
1401 }
1402}
1403
Romain Guy026c5e162010-06-28 17:12:22 -07001404void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001405 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy746b7402010-10-26 16:27:31 -07001406 setupDraw();
Romain Guy86942302010-09-12 13:02:16 -07001407
Romain Guyd27977d2010-07-14 19:18:51 -07001408 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001409 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001410 color |= 0x00ffffff;
1411 }
1412
1413 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -07001414 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -07001415 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -07001416 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1417 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1418 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1419
Romain Guyc95c8d62010-09-17 15:31:32 -07001420 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform);
1421
1422 // Draw the mesh
1423 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1424}
1425
1426void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom,
Romain Guy7f78b0c2010-11-04 14:36:26 -07001427 float r, float g, float b, float a, SkXfermode::Mode mode,
1428 bool ignoreTransform, bool ignoreMatrix) {
Romain Guy06f96e22010-07-30 19:18:16 -07001429 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -07001430
Romain Guy06f96e22010-07-30 19:18:16 -07001431 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -07001432 ProgramDescription description;
Romain Guy707b2f72010-10-11 16:34:59 -07001433 const bool setColor = description.setColor(r, g, b, a);
1434
Romain Guy06f96e22010-07-30 19:18:16 -07001435 if (mShader) {
Romain Guy746b7402010-10-26 16:27:31 -07001436 mShader->describe(description, mCaches.extensions);
Romain Guy6926c722010-07-12 20:20:03 -07001437 }
Romain Guydb1938e2010-08-02 18:50:22 -07001438 if (mColorFilter) {
Romain Guy746b7402010-10-26 16:27:31 -07001439 mColorFilter->describe(description, mCaches.extensions);
Romain Guydb1938e2010-08-02 18:50:22 -07001440 }
Romain Guyd27977d2010-07-14 19:18:51 -07001441
Romain Guy1c740bc2010-09-13 18:00:09 -07001442 // Setup the blending mode
Romain Guyc95c8d62010-09-17 15:31:32 -07001443 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -07001444
Romain Guy06f96e22010-07-30 19:18:16 -07001445 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001446 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -07001447
1448 // Setup attributes
Romain Guy03750a02010-10-18 14:06:08 -07001449 mCaches.bindMeshBuffer();
Romain Guyfb8b7632010-08-23 21:05:08 -07001450 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy03750a02010-10-18 14:06:08 -07001451 gMeshStride, 0);
Romain Guy06f96e22010-07-30 19:18:16 -07001452
Romain Guy7f78b0c2010-11-04 14:36:26 -07001453 if (!ignoreMatrix) {
1454 // Setup uniforms
1455 mModelView.loadTranslate(left, top, 0.0f);
1456 mModelView.scale(right - left, bottom - top, 1.0f);
1457 if (!ignoreTransform) {
1458 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1459 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1460 } else {
1461 mat4 identity;
1462 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
1463 dirtyLayer(left, top, right, bottom);
1464 }
Romain Guyd27977d2010-07-14 19:18:51 -07001465 }
Romain Guy707b2f72010-10-11 16:34:59 -07001466 mCaches.currentProgram->setColor(r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -07001467
Romain Guy06f96e22010-07-30 19:18:16 -07001468 // Setup attributes and uniforms required by the shaders
1469 if (mShader) {
Romain Guy7f78b0c2010-11-04 14:36:26 -07001470 if (ignoreMatrix) mModelView.loadIdentity();
Romain Guyfb8b7632010-08-23 21:05:08 -07001471 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001472 }
Romain Guydb1938e2010-08-02 18:50:22 -07001473 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001474 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001475 }
Romain Guyd27977d2010-07-14 19:18:51 -07001476}
1477
Romain Guy82ba8142010-07-09 13:25:56 -07001478void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001479 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001480 int alpha;
1481 SkXfermode::Mode mode;
1482 getAlphaAndMode(paint, &alpha, &mode);
1483
Romain Guy8164c2d2010-10-25 18:03:28 -07001484 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1485
Romain Guy6820ac82010-09-15 18:11:50 -07001486 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy03750a02010-10-18 14:06:08 -07001487 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy6820ac82010-09-15 18:11:50 -07001488 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -07001489}
1490
Romain Guybd6b79b2010-06-26 00:13:53 -07001491void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001492 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1493 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001494 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001495}
1496
1497void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001498 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001499 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07001500 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy746b7402010-10-26 16:27:31 -07001501 setupDraw();
Romain Guy86942302010-09-12 13:02:16 -07001502
Romain Guy889f8d12010-07-29 14:37:42 -07001503 ProgramDescription description;
1504 description.hasTexture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001505 const bool setColor = description.setColor(alpha, alpha, alpha, alpha);
Romain Guydb1938e2010-08-02 18:50:22 -07001506 if (mColorFilter) {
Romain Guy746b7402010-10-26 16:27:31 -07001507 mColorFilter->describe(description, mCaches.extensions);
Romain Guydb1938e2010-08-02 18:50:22 -07001508 }
Romain Guy889f8d12010-07-29 14:37:42 -07001509
Romain Guybd6b79b2010-06-26 00:13:53 -07001510 mModelView.loadTranslate(left, top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07001511 if (!ignoreScale) {
1512 mModelView.scale(right - left, bottom - top, 1.0f);
1513 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001514
Romain Guyf607bdc2010-09-10 19:20:06 -07001515 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001516
Romain Guyfb8b7632010-08-23 21:05:08 -07001517 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001518 if (!ignoreTransform) {
1519 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001520 if (dirty) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001521 } else {
Romain Guy5b3b3522010-10-27 18:57:51 -07001522 mat4 identity;
1523 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
1524 if (dirty) dirtyLayer(left, top, right, bottom);
Romain Guyf607bdc2010-09-10 19:20:06 -07001525 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001526
Romain Guy889f8d12010-07-29 14:37:42 -07001527 // Texture
Romain Guy8164c2d2010-10-25 18:03:28 -07001528 bindTexture(texture);
Romain Guyfb8b7632010-08-23 21:05:08 -07001529 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001530
Romain Guya9794742010-07-13 11:37:54 -07001531 // Always premultiplied
Romain Guy707b2f72010-10-11 16:34:59 -07001532 if (setColor) {
1533 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha);
1534 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001535
Romain Guy889f8d12010-07-29 14:37:42 -07001536 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001537 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001538 glEnableVertexAttribArray(texCoordsSlot);
Romain Guy03750a02010-10-18 14:06:08 -07001539
1540 if (!vertices) {
1541 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1542 } else {
1543 mCaches.unbindMeshBuffer();
1544 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001545 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001546 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001547 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001548
Romain Guydb1938e2010-08-02 18:50:22 -07001549 // Color filter
1550 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001551 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001552 }
1553
Romain Guy6820ac82010-09-15 18:11:50 -07001554 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001555 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001556}
1557
Romain Guya5aed0d2010-09-09 14:42:43 -07001558void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001559 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001560 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1561 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001562 if (mode < SkXfermode::kPlus_Mode) {
1563 if (!mCaches.blend) {
1564 glEnable(GL_BLEND);
1565 }
Romain Guy82ba8142010-07-09 13:25:56 -07001566
Romain Guyf607bdc2010-09-10 19:20:06 -07001567 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1568 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001569
Romain Guya5aed0d2010-09-09 14:42:43 -07001570 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1571 glBlendFunc(sourceMode, destMode);
1572 mCaches.lastSrcMode = sourceMode;
1573 mCaches.lastDstMode = destMode;
1574 }
1575 } else {
1576 // These blend modes are not supported by OpenGL directly and have
1577 // to be implemented using shaders. Since the shader will perform
1578 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001579 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001580 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001581 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001582 }
1583
1584 if (mCaches.blend) {
1585 glDisable(GL_BLEND);
1586 }
1587 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001588 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001589 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001590 glDisable(GL_BLEND);
1591 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001592 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001593}
1594
Romain Guy889f8d12010-07-29 14:37:42 -07001595bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001596 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001597 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001598 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001599 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001600 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001601 }
Romain Guy6926c722010-07-12 20:20:03 -07001602 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001603}
1604
Romain Guy026c5e162010-06-28 17:12:22 -07001605void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001606 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001607 TextureVertex::setUV(v++, u1, v1);
1608 TextureVertex::setUV(v++, u2, v1);
1609 TextureVertex::setUV(v++, u1, v2);
1610 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001611}
1612
Chet Haase5c13d892010-10-08 08:37:55 -07001613void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001614 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07001615 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001616 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1617 if (!isMode) {
1618 // Assume SRC_OVER
1619 *mode = SkXfermode::kSrcOver_Mode;
1620 }
1621 } else {
1622 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001623 }
1624
1625 // Skia draws using the color's alpha channel if < 255
1626 // Otherwise, it uses the paint's alpha
1627 int color = paint->getColor();
1628 *alpha = (color >> 24) & 0xFF;
1629 if (*alpha == 255) {
1630 *alpha = paint->getAlpha();
1631 }
1632 } else {
1633 *mode = SkXfermode::kSrcOver_Mode;
1634 *alpha = 255;
1635 }
Romain Guy026c5e162010-06-28 17:12:22 -07001636}
1637
Romain Guya5aed0d2010-09-09 14:42:43 -07001638SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1639 if (mode == NULL) {
1640 return SkXfermode::kSrcOver_Mode;
1641 }
1642 return mode->fMode;
1643}
1644
Romain Guy746b7402010-10-26 16:27:31 -07001645void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07001646 bool bound = false;
1647 if (wrapS != texture->wrapS) {
1648 glBindTexture(GL_TEXTURE_2D, texture->id);
1649 bound = true;
1650 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1651 texture->wrapS = wrapS;
1652 }
1653 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001654 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001655 glBindTexture(GL_TEXTURE_2D, texture->id);
1656 }
Romain Guy8164c2d2010-10-25 18:03:28 -07001657 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1658 texture->wrapT = wrapT;
1659 }
Romain Guya1db5742010-07-20 13:09:13 -07001660}
1661
Romain Guy9d5316e2010-06-24 19:30:36 -07001662}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001663}; // namespace android