blob: 8bfc8d469d4cc4f314bf641cacb966c1c240d656 [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 Guya957eea2010-12-08 18:34:42 -080033#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070034
35namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070036namespace uirenderer {
37
38///////////////////////////////////////////////////////////////////////////////
39// Defines
40///////////////////////////////////////////////////////////////////////////////
41
Romain Guy759ea802010-09-16 20:49:46 -070042#define RAD_TO_DEG (180.0f / 3.14159265f)
43#define MIN_ANGLE 0.001f
44
Romain Guydbc26d22010-10-11 17:58:29 -070045// TODO: This should be set in properties
46#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
47
Romain Guy9d5316e2010-06-24 19:30:36 -070048///////////////////////////////////////////////////////////////////////////////
49// Globals
50///////////////////////////////////////////////////////////////////////////////
51
Romain Guy889f8d12010-07-29 14:37:42 -070052/**
53 * Structure mapping Skia xfermodes to OpenGL blending factors.
54 */
55struct Blender {
56 SkXfermode::Mode mode;
57 GLenum src;
58 GLenum dst;
59}; // struct Blender
60
Romain Guy026c5e162010-06-28 17:12:22 -070061// In this array, the index of each Blender equals the value of the first
62// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
63static const Blender gBlends[] = {
64 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
65 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
66 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
67 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
68 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
69 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
70 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
71 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
72 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
73 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
74 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
75 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
76};
Romain Guye4d01122010-06-16 18:44:05 -070077
Romain Guy87a76572010-09-13 18:11:21 -070078// This array contains the swapped version of each SkXfermode. For instance
79// this array's SrcOver blending mode is actually DstOver. You can refer to
80// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070081static const Blender gBlendsSwap[] = {
82 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
83 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
84 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
85 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
86 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
87 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
88 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
89 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
90 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
92 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
93 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
94};
95
Romain Guy889f8d12010-07-29 14:37:42 -070096static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -070097 GL_TEXTURE0,
98 GL_TEXTURE1,
99 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700100};
101
Romain Guyf6a11b82010-06-23 17:47:49 -0700102///////////////////////////////////////////////////////////////////////////////
103// Constructors/destructor
104///////////////////////////////////////////////////////////////////////////////
105
Romain Guyfb8b7632010-08-23 21:05:08 -0700106OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700107 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700108 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700109 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700110
Romain Guyac670c02010-07-27 17:39:27 -0700111 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
112
Romain Guyae5575b2010-07-29 18:48:04 -0700113 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700114}
115
Romain Guy85bf02f2010-06-22 13:11:24 -0700116OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700117 // The context has already been destroyed at this point, do not call
118 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700119}
120
Romain Guyf6a11b82010-06-23 17:47:49 -0700121///////////////////////////////////////////////////////////////////////////////
122// Setup
123///////////////////////////////////////////////////////////////////////////////
124
Romain Guy85bf02f2010-06-22 13:11:24 -0700125void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700126 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700127 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700128
129 mWidth = width;
130 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700131
132 mFirstSnapshot->height = height;
133 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700134
135 mDirtyClip = false;
Romain Guye4d01122010-06-16 18:44:05 -0700136}
137
Romain Guy6b7bd242010-10-06 19:49:23 -0700138void OpenGLRenderer::prepare(bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800139 mCaches.clearGarbage();
140
Romain Guy8aef54f2010-09-01 15:13:49 -0700141 mSnapshot = new Snapshot(mFirstSnapshot,
142 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy8fb95422010-08-17 18:38:51 -0700143 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700144
Romain Guyfb8b7632010-08-23 21:05:08 -0700145 glViewport(0, 0, mWidth, mHeight);
146
Romain Guyd90f23e2010-09-09 11:47:54 -0700147 glDisable(GL_DITHER);
Romain Guybb9524b2010-06-22 18:56:38 -0700148
Romain Guy6b7bd242010-10-06 19:49:23 -0700149 if (!opaque) {
Romain Guy746b7402010-10-26 16:27:31 -0700150 glDisable(GL_SCISSOR_TEST);
Romain Guy6b7bd242010-10-06 19:49:23 -0700151 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
152 glClear(GL_COLOR_BUFFER_BIT);
153 }
Romain Guybb9524b2010-06-22 18:56:38 -0700154
Chet Haase0d200832010-11-05 15:36:16 -0700155 glEnable(GL_SCISSOR_TEST);
156 glScissor(0, 0, mWidth, mHeight);
Romain Guyb82da652010-07-30 11:36:12 -0700157 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700158}
159
Romain Guyb025b9c2010-09-16 14:16:48 -0700160void OpenGLRenderer::finish() {
161#if DEBUG_OPENGL
162 GLenum status = GL_NO_ERROR;
163 while ((status = glGetError()) != GL_NO_ERROR) {
164 LOGD("GL error from OpenGLRenderer: 0x%x", status);
165 }
166#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800167#if DEBUG_MEMORY_USAGE
168 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800169#else
170 if (mCaches.getDebugLevel() & kDebugMemory) {
171 mCaches.dumpMemoryUsage();
172 }
Romain Guyc15008e2010-11-10 11:59:15 -0800173#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700174}
175
Romain Guyda8532c2010-08-31 11:50:35 -0700176void OpenGLRenderer::acquireContext() {
177 if (mCaches.currentProgram) {
178 if (mCaches.currentProgram->isInUse()) {
179 mCaches.currentProgram->remove();
180 mCaches.currentProgram = NULL;
181 }
182 }
Romain Guy50c0f092010-10-19 11:42:22 -0700183 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700184}
185
186void OpenGLRenderer::releaseContext() {
Romain Guyeb993562010-10-05 18:14:38 -0700187 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700188
189 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700190 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700191
Romain Guyf607bdc2010-09-10 19:20:06 -0700192 glDisable(GL_DITHER);
193
194 glBindFramebuffer(GL_FRAMEBUFFER, 0);
Romain Guy03750a02010-10-18 14:06:08 -0700195 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700196
Romain Guy50c0f092010-10-19 11:42:22 -0700197 mCaches.blend = true;
198 glEnable(GL_BLEND);
199 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
200 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700201}
202
Romain Guyf6a11b82010-06-23 17:47:49 -0700203///////////////////////////////////////////////////////////////////////////////
204// State management
205///////////////////////////////////////////////////////////////////////////////
206
Romain Guybb9524b2010-06-22 18:56:38 -0700207int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700208 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700209}
210
211int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700212 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700213}
214
215void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700216 if (mSaveCount > 1) {
217 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700218 }
Romain Guybb9524b2010-06-22 18:56:38 -0700219}
220
221void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700222 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700223
Romain Guy8fb95422010-08-17 18:38:51 -0700224 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700225 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700226 }
Romain Guybb9524b2010-06-22 18:56:38 -0700227}
228
Romain Guy8aef54f2010-09-01 15:13:49 -0700229int OpenGLRenderer::saveSnapshot(int flags) {
230 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700231 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700232}
233
234bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700235 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700236 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700237 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700238
Romain Guybd6b79b2010-06-26 00:13:53 -0700239 sp<Snapshot> current = mSnapshot;
240 sp<Snapshot> previous = mSnapshot->previous;
241
Romain Guyeb993562010-10-05 18:14:38 -0700242 if (restoreOrtho) {
243 Rect& r = previous->viewport;
244 glViewport(r.left, r.top, r.right, r.bottom);
245 mOrthoMatrix.load(current->orthoMatrix);
246 }
247
Romain Guy8b55f372010-08-18 17:10:07 -0700248 mSaveCount--;
249 mSnapshot = previous;
250
Romain Guy2542d192010-08-18 11:47:12 -0700251 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700252 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700253 }
Romain Guy2542d192010-08-18 11:47:12 -0700254
Romain Guy5ec99242010-11-03 16:19:08 -0700255 if (restoreLayer) {
256 composeLayer(current, previous);
257 }
258
Romain Guy2542d192010-08-18 11:47:12 -0700259 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700260}
261
Romain Guyf6a11b82010-06-23 17:47:49 -0700262///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700263// Layers
264///////////////////////////////////////////////////////////////////////////////
265
266int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700267 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700268 const GLuint previousFbo = mSnapshot->fbo;
269 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700270
Romain Guyaf636eb2010-12-09 17:47:21 -0800271 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700272 int alpha = 255;
273 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700274
Romain Guye45362c2010-11-03 19:58:32 -0700275 if (p) {
276 alpha = p->getAlpha();
277 if (!mCaches.extensions.hasFramebufferFetch()) {
278 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
279 if (!isMode) {
280 // Assume SRC_OVER
281 mode = SkXfermode::kSrcOver_Mode;
282 }
283 } else {
284 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700285 }
286 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700287 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700288 }
Romain Guyd55a8612010-06-28 17:42:46 -0700289
Romain Guydbc26d22010-10-11 17:58:29 -0700290 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
291 }
Romain Guyd55a8612010-06-28 17:42:46 -0700292
293 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700294}
295
296int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
297 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700298 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700299 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700300 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700301 SkPaint paint;
302 paint.setAlpha(alpha);
303 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700304 }
Romain Guyd55a8612010-06-28 17:42:46 -0700305}
Romain Guybd6b79b2010-06-26 00:13:53 -0700306
Romain Guy1c740bc2010-09-13 18:00:09 -0700307/**
308 * Layers are viewed by Skia are slightly different than layers in image editing
309 * programs (for instance.) When a layer is created, previously created layers
310 * and the frame buffer still receive every drawing command. For instance, if a
311 * layer is created and a shape intersecting the bounds of the layers and the
312 * framebuffer is draw, the shape will be drawn on both (unless the layer was
313 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
314 *
315 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
316 * texture. Unfortunately, this is inefficient as it requires every primitive to
317 * be drawn n + 1 times, where n is the number of active layers. In practice this
318 * means, for every primitive:
319 * - Switch active frame buffer
320 * - Change viewport, clip and projection matrix
321 * - Issue the drawing
322 *
323 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700324 * To avoid this, layers are implemented in a different way here, at least in the
325 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
326 * is set. When this flag is set we can redirect all drawing operations into a
327 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700328 *
329 * This implementation relies on the frame buffer being at least RGBA 8888. When
330 * a layer is created, only a texture is created, not an FBO. The content of the
331 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700332 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700333 * buffer and drawing continues as normal. This technique therefore treats the
334 * frame buffer as a scratch buffer for the layers.
335 *
336 * To compose the layers back onto the frame buffer, each layer texture
337 * (containing the original frame buffer data) is drawn as a simple quad over
338 * the frame buffer. The trick is that the quad is set as the composition
339 * destination in the blending equation, and the frame buffer becomes the source
340 * of the composition.
341 *
342 * Drawing layers with an alpha value requires an extra step before composition.
343 * An empty quad is drawn over the layer's region in the frame buffer. This quad
344 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
345 * quad is used to multiply the colors in the frame buffer. This is achieved by
346 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
347 * GL_ZERO, GL_SRC_ALPHA.
348 *
349 * Because glCopyTexImage2D() can be slow, an alternative implementation might
350 * be use to draw a single clipped layer. The implementation described above
351 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700352 *
353 * (1) The frame buffer is actually not cleared right away. To allow the GPU
354 * to potentially optimize series of calls to glCopyTexImage2D, the frame
355 * buffer is left untouched until the first drawing operation. Only when
356 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700357 */
Romain Guyd55a8612010-06-28 17:42:46 -0700358bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700359 float right, float bottom, int alpha, SkXfermode::Mode mode,
360 int flags, GLuint previousFbo) {
361 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700362 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700363
Romain Guyeb993562010-10-05 18:14:38 -0700364 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
365
Romain Guyf607bdc2010-09-10 19:20:06 -0700366 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700367 Rect bounds(left, top, right, bottom);
Romain Guyae88e5e2010-10-22 17:49:18 -0700368 if (fboLayer) {
369 // Clear the previous layer regions before we change the viewport
370 clearLayerRegions();
371 } else {
Romain Guyeb993562010-10-05 18:14:38 -0700372 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700373
Romain Guyeb993562010-10-05 18:14:38 -0700374 // Layers only make sense if they are in the framebuffer's bounds
Romain Guy58ae6db2010-10-22 11:16:37 -0700375 bounds.intersect(*snapshot->clipRect);
Romain Guyae517592010-10-22 10:40:27 -0700376
Romain Guyae88e5e2010-10-22 17:49:18 -0700377 // We cannot work with sub-pixels in this case
378 bounds.snapToPixelBoundaries();
379
Romain Guyae517592010-10-22 10:40:27 -0700380 // When the layer is not an FBO, we may use glCopyTexImage so we
381 // need to make sure the layer does not extend outside the bounds
382 // of the framebuffer
383 bounds.intersect(snapshot->previous->viewport);
Romain Guyeb993562010-10-05 18:14:38 -0700384 }
Romain Guybf434112010-09-16 14:40:17 -0700385
Romain Guy746b7402010-10-26 16:27:31 -0700386 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
387 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800388 if (fboLayer) {
389 snapshot->invisible = true;
390 } else {
391 snapshot->empty = true;
392 }
Romain Guydbc26d22010-10-11 17:58:29 -0700393 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700394 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700395 }
396
397 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800398 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700399 return false;
400 }
Romain Guyf18fd992010-07-08 11:45:51 -0700401
Romain Guy5b3b3522010-10-27 18:57:51 -0700402 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700403 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700404 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700405 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700406 }
407
Romain Guydda570202010-07-06 11:39:32 -0700408 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700409 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700410 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700411 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
412 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guydda570202010-07-06 11:39:32 -0700413
Romain Guy8fb95422010-08-17 18:38:51 -0700414 // Save the layer in the snapshot
415 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700416 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700417
Romain Guyeb993562010-10-05 18:14:38 -0700418 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700419 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700420 } else {
421 // Copy the framebuffer into the layer
422 glBindTexture(GL_TEXTURE_2D, layer->texture);
423
Romain Guyae88e5e2010-10-22 17:49:18 -0700424 if (layer->empty) {
425 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
426 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
427 layer->empty = false;
428 } else {
429 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
430 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
431 }
Romain Guyeb993562010-10-05 18:14:38 -0700432
433 // Enqueue the buffer coordinates to clear the corresponding region later
434 mLayers.push(new Rect(bounds));
435 }
Romain Guyf86ef572010-07-01 11:05:42 -0700436
Romain Guyd55a8612010-06-28 17:42:46 -0700437 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700438}
439
Romain Guy5b3b3522010-10-27 18:57:51 -0700440bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
441 GLuint previousFbo) {
442 layer->fbo = mCaches.fboCache.get();
443
444#if RENDER_LAYERS_AS_REGIONS
445 snapshot->region = &snapshot->layer->region;
446 snapshot->flags |= Snapshot::kFlagFboTarget;
447#endif
448
449 Rect clip(bounds);
450 snapshot->transform->mapRect(clip);
451 clip.intersect(*snapshot->clipRect);
452 clip.snapToPixelBoundaries();
453 clip.intersect(snapshot->previous->viewport);
454
455 mat4 inverse;
456 inverse.loadInverse(*mSnapshot->transform);
457
458 inverse.mapRect(clip);
459 clip.snapToPixelBoundaries();
460 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700461 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700462
463 snapshot->flags |= Snapshot::kFlagIsFboLayer;
464 snapshot->fbo = layer->fbo;
465 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
466 //snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
467 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
468 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
469 snapshot->height = bounds.getHeight();
470 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
471 snapshot->orthoMatrix.load(mOrthoMatrix);
472
473 // Bind texture to FBO
474 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
475 glBindTexture(GL_TEXTURE_2D, layer->texture);
476
477 // Initialize the texture if needed
478 if (layer->empty) {
479 layer->empty = false;
480 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
481 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
482 }
483
484 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
485 layer->texture, 0);
486
487#if DEBUG_LAYERS_AS_REGIONS
488 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
489 if (status != GL_FRAMEBUFFER_COMPLETE) {
490 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
491
492 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
493 glDeleteTextures(1, &layer->texture);
494 mCaches.fboCache.put(layer->fbo);
495
496 delete layer;
497
498 return false;
499 }
500#endif
501
502 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
503 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
504 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
505 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
506 glClear(GL_COLOR_BUFFER_BIT);
507
508 dirtyClip();
509
510 // Change the ortho projection
511 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
512 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
513
514 return true;
515}
516
Romain Guy1c740bc2010-09-13 18:00:09 -0700517/**
518 * Read the documentation of createLayer() before doing anything in this method.
519 */
Romain Guy1d83e192010-08-17 11:37:00 -0700520void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
521 if (!current->layer) {
522 LOGE("Attempting to compose a layer that does not exist");
523 return;
524 }
525
Romain Guy5b3b3522010-10-27 18:57:51 -0700526 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700527
528 if (fboLayer) {
529 // Unbind current FBO and restore previous one
530 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
531 }
532
Romain Guy1d83e192010-08-17 11:37:00 -0700533 Layer* layer = current->layer;
534 const Rect& rect = layer->layer;
535
Romain Guyeb993562010-10-05 18:14:38 -0700536 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700537 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700538 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700539 // Required below, composeLayerRect() will divide by 255
540 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700541 }
542
Romain Guy03750a02010-10-18 14:06:08 -0700543 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700544
Romain Guy746b7402010-10-26 16:27:31 -0700545 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700546
Romain Guy5b3b3522010-10-27 18:57:51 -0700547 // When the layer is stored in an FBO, we can save a bit of fillrate by
548 // drawing only the dirty region
549 if (fboLayer) {
550 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
551 composeLayerRegion(layer, rect);
552 } else {
553 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
554 composeLayerRect(layer, rect, true);
555 }
Romain Guy8b55f372010-08-18 17:10:07 -0700556
Romain Guyeb993562010-10-05 18:14:38 -0700557 if (fboLayer) {
558 // Detach the texture from the FBO
559 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
560 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
561 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
562
563 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
564 mCaches.fboCache.put(current->fbo);
565 }
566
Romain Guy746b7402010-10-26 16:27:31 -0700567 dirtyClip();
568
Romain Guyeb993562010-10-05 18:14:38 -0700569 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700570 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700571 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700572 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700573 delete layer;
574 }
575}
576
Romain Guy5b3b3522010-10-27 18:57:51 -0700577void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
578 const Rect& texCoords = layer->texCoords;
579 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
580
581 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
582 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
583 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
584
585 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
586}
587
588void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
589#if RENDER_LAYERS_AS_REGIONS
590 if (layer->region.isRect()) {
591 composeLayerRect(layer, rect);
592 layer->region.clear();
593 return;
594 }
595
596 if (!layer->region.isEmpty()) {
597 size_t count;
598 const android::Rect* rects = layer->region.getArray(&count);
599
600 setupDraw();
601
602 ProgramDescription description;
603 description.hasTexture = true;
604
605 const float alpha = layer->alpha / 255.0f;
606 const bool setColor = description.setColor(alpha, alpha, alpha, alpha);
607 chooseBlending(layer->blend || layer->alpha < 255, layer->mode, description, false);
608
609 useProgram(mCaches.programCache.get(description));
610
611 // Texture
612 bindTexture(layer->texture);
613 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
614
615 // Always premultiplied
616 if (setColor) {
617 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha);
618 }
619
620 // Mesh
621 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
622 glEnableVertexAttribArray(texCoordsSlot);
623
624 mModelView.loadIdentity();
625 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
626
627 const float texX = 1.0f / float(layer->width);
628 const float texY = 1.0f / float(layer->height);
629
630 TextureVertex* mesh = mCaches.getRegionMesh();
631 GLsizei numQuads = 0;
632
633 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
634 gMeshStride, &mesh[0].position[0]);
635 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
636 gMeshStride, &mesh[0].texture[0]);
637
638 for (size_t i = 0; i < count; i++) {
639 const android::Rect* r = &rects[i];
640
641 const float u1 = r->left * texX;
642 const float v1 = (rect.getHeight() - r->top) * texY;
643 const float u2 = r->right * texX;
644 const float v2 = (rect.getHeight() - r->bottom) * texY;
645
646 // TODO: Reject quads outside of the clip
647 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
648 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
649 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
650 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
651
652 numQuads++;
653
654 if (numQuads >= REGION_MESH_QUAD_COUNT) {
655 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
656 numQuads = 0;
657 mesh = mCaches.getRegionMesh();
658 }
659 }
660
661 if (numQuads > 0) {
662 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
663 }
664
665 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
666 glDisableVertexAttribArray(texCoordsSlot);
667
668#if DEBUG_LAYERS_AS_REGIONS
669 uint32_t colors[] = {
670 0x7fff0000, 0x7f00ff00,
671 0x7f0000ff, 0x7fff00ff,
672 };
673
674 int offset = 0;
675 int32_t top = rects[0].top;
676 int i = 0;
677
678 for (size_t i = 0; i < count; i++) {
679 if (top != rects[i].top) {
680 offset ^= 0x2;
681 top = rects[i].top;
682 }
683
684 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
685 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
686 SkXfermode::kSrcOver_Mode);
687 }
688#endif
689
690 layer->region.clear();
691 }
692#else
693 composeLayerRect(layer, rect);
694#endif
695}
696
697void OpenGLRenderer::dirtyLayer(const float left, const float top,
698 const float right, const float bottom, const mat4 transform) {
699#if RENDER_LAYERS_AS_REGIONS
700 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region) {
701 Rect bounds(left, top, right, bottom);
702 transform.mapRect(bounds);
703 bounds.intersect(*mSnapshot->clipRect);
704 bounds.snapToPixelBoundaries();
705
706 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
707 if (!dirty.isEmpty()) {
708 mSnapshot->region->orSelf(dirty);
709 }
710 }
711#endif
712}
713
714void OpenGLRenderer::dirtyLayer(const float left, const float top,
715 const float right, const float bottom) {
716#if RENDER_LAYERS_AS_REGIONS
717 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region) {
718 Rect bounds(left, top, right, bottom);
719 bounds.intersect(*mSnapshot->clipRect);
720 bounds.snapToPixelBoundaries();
721
722 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
723 if (!dirty.isEmpty()) {
724 mSnapshot->region->orSelf(dirty);
725 }
726 }
727#endif
728}
729
Romain Guy746b7402010-10-26 16:27:31 -0700730void OpenGLRenderer::setupDraw() {
731 clearLayerRegions();
732 if (mDirtyClip) {
733 setScissorFromClip();
734 }
735}
736
Romain Guy86942302010-09-12 13:02:16 -0700737void OpenGLRenderer::clearLayerRegions() {
Romain Guyaf636eb2010-12-09 17:47:21 -0800738 if (mLayers.size() == 0 || mSnapshot->isIgnored()) return;
Romain Guy86942302010-09-12 13:02:16 -0700739
Romain Guy5b3b3522010-10-27 18:57:51 -0700740 Rect clipRect(*mSnapshot->clipRect);
741 clipRect.snapToPixelBoundaries();
742
Romain Guy86942302010-09-12 13:02:16 -0700743 for (uint32_t i = 0; i < mLayers.size(); i++) {
744 Rect* bounds = mLayers.itemAt(i);
Romain Guy5b3b3522010-10-27 18:57:51 -0700745 if (clipRect.intersects(*bounds)) {
746 // Clear the framebuffer where the layer will draw
747 glScissor(bounds->left, mSnapshot->height - bounds->bottom,
748 bounds->getWidth(), bounds->getHeight());
749 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
750 glClear(GL_COLOR_BUFFER_BIT);
Romain Guy86942302010-09-12 13:02:16 -0700751
Romain Guy5b3b3522010-10-27 18:57:51 -0700752 // Restore the clip
753 dirtyClip();
754 }
Romain Guy86942302010-09-12 13:02:16 -0700755
756 delete bounds;
757 }
Romain Guy86942302010-09-12 13:02:16 -0700758
Romain Guy5b3b3522010-10-27 18:57:51 -0700759 mLayers.clear();
Romain Guy86942302010-09-12 13:02:16 -0700760}
761
Romain Guybd6b79b2010-06-26 00:13:53 -0700762///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700763// Transforms
764///////////////////////////////////////////////////////////////////////////////
765
766void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700767 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700768}
769
770void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700771 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700772}
773
774void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700775 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700776}
777
778void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700779 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700780}
781
Romain Guy41030da2010-10-13 13:40:37 -0700782const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700783 if (mSnapshot->fbo != 0) {
784 return &mSnapshot->transform->data[0];
785 }
786 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700787}
788
Romain Guyf6a11b82010-06-23 17:47:49 -0700789void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700790 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700791}
792
793void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700794 SkMatrix transform;
795 mSnapshot->transform->copyTo(transform);
796 transform.preConcat(*matrix);
797 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700798}
799
800///////////////////////////////////////////////////////////////////////////////
801// Clipping
802///////////////////////////////////////////////////////////////////////////////
803
Romain Guybb9524b2010-06-22 18:56:38 -0700804void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700805 Rect clip(*mSnapshot->clipRect);
806 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700807 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700808 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700809}
810
811const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700812 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700813}
814
Romain Guyc7d53492010-06-25 13:41:57 -0700815bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800816 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700817 return true;
818 }
819
Romain Guy1d83e192010-08-17 11:37:00 -0700820 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700821 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700822 r.snapToPixelBoundaries();
823
824 Rect clipRect(*mSnapshot->clipRect);
825 clipRect.snapToPixelBoundaries();
826
827 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700828}
829
Romain Guy079ba2c2010-07-16 14:12:24 -0700830bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
831 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700832 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700833 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700834 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700835 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700836}
837
Romain Guyf6a11b82010-06-23 17:47:49 -0700838///////////////////////////////////////////////////////////////////////////////
839// Drawing
840///////////////////////////////////////////////////////////////////////////////
841
Romain Guy0fe478e2010-11-08 12:08:41 -0800842void OpenGLRenderer::drawDisplayList(DisplayList* displayList) {
843 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
844 // will be performed by the display list itself
845 if (displayList) {
846 displayList->replay(*this);
847 }
848}
849
Chet Haase5c13d892010-10-08 08:37:55 -0700850void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700851 const float right = left + bitmap->width();
852 const float bottom = top + bitmap->height();
853
854 if (quickReject(left, top, right, bottom)) {
855 return;
856 }
857
Romain Guy61c8c9c2010-08-09 20:48:09 -0700858 glActiveTexture(GL_TEXTURE0);
Romain Guy8164c2d2010-10-25 18:03:28 -0700859 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700860 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700861 const AutoTexture autoCleanup(texture);
862
Romain Guy6926c722010-07-12 20:20:03 -0700863 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700864}
865
Chet Haase5c13d892010-10-08 08:37:55 -0700866void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -0700867 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
868 const mat4 transform(*matrix);
869 transform.mapRect(r);
870
Romain Guy6926c722010-07-12 20:20:03 -0700871 if (quickReject(r.left, r.top, r.right, r.bottom)) {
872 return;
873 }
874
Romain Guy61c8c9c2010-08-09 20:48:09 -0700875 glActiveTexture(GL_TEXTURE0);
Romain Guy8164c2d2010-10-25 18:03:28 -0700876 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700877 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700878 const AutoTexture autoCleanup(texture);
879
Romain Guy5b3b3522010-10-27 18:57:51 -0700880 // This could be done in a cheaper way, all we need is pass the matrix
881 // to the vertex shader. The save/restore is a bit overkill.
882 save(SkCanvas::kMatrix_SaveFlag);
883 concatMatrix(matrix);
884 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
885 restore();
Romain Guyf86ef572010-07-01 11:05:42 -0700886}
887
Romain Guy8ba548f2010-06-30 19:21:21 -0700888void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
889 float srcLeft, float srcTop, float srcRight, float srcBottom,
890 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700891 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700892 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
893 return;
894 }
895
Romain Guy746b7402010-10-26 16:27:31 -0700896 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -0700897 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700898 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700899 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -0700900 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -0700901
Romain Guy8ba548f2010-06-30 19:21:21 -0700902 const float width = texture->width;
903 const float height = texture->height;
904
905 const float u1 = srcLeft / width;
906 const float v1 = srcTop / height;
907 const float u2 = srcRight / width;
908 const float v2 = srcBottom / height;
909
Romain Guy03750a02010-10-18 14:06:08 -0700910 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -0700911 resetDrawTextureTexCoords(u1, v1, u2, v2);
912
Romain Guy03750a02010-10-18 14:06:08 -0700913 int alpha;
914 SkXfermode::Mode mode;
915 getAlphaAndMode(paint, &alpha, &mode);
916
Romain Guy6620c6d2010-12-06 18:07:02 -0800917 if (mSnapshot->transform->isPureTranslate()) {
918 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
919 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
920
921 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
922 texture->id, alpha / 255.0f, mode, texture->blend,
923 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
924 GL_TRIANGLE_STRIP, gMeshCount, false, true);
925 } else {
926 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
927 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
928 GL_TRIANGLE_STRIP, gMeshCount);
929 }
Romain Guy8ba548f2010-06-30 19:21:21 -0700930
931 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
932}
933
Romain Guy4aa90572010-09-26 18:40:37 -0700934void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -0700935 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -0700936 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700937 if (quickReject(left, top, right, bottom)) {
938 return;
939 }
940
Romain Guy746b7402010-10-26 16:27:31 -0700941 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -0700942 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700943 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700944 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -0700945 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -0700946
947 int alpha;
948 SkXfermode::Mode mode;
949 getAlphaAndMode(paint, &alpha, &mode);
950
Romain Guy2728f962010-10-08 18:36:15 -0700951 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -0700952 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -0700953
Romain Guya5ef39a2010-12-03 16:48:20 -0800954 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -0800955 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -0800956#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -0700957 // Mark the current layer dirty where we are going to draw the patch
958 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) &&
959 mSnapshot->region && mesh->hasEmptyQuads) {
960 const size_t count = mesh->quads.size();
961 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -0800962 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -0800963 if (pureTranslate) {
964 const float x = (int) floorf(bounds.left + 0.5f);
965 const float y = (int) floorf(bounds.top + 0.5f);
Romain Guy8ab40792010-12-07 13:30:10 -0800966 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Romain Guy6620c6d2010-12-06 18:07:02 -0800967 *mSnapshot->transform);
968 } else {
969 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom,
970 *mSnapshot->transform);
971 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700972 }
973 }
Romain Guya5ef39a2010-12-03 16:48:20 -0800974#endif
Romain Guy5b3b3522010-10-27 18:57:51 -0700975
Romain Guy6620c6d2010-12-06 18:07:02 -0800976 if (pureTranslate) {
977 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
978 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
979
980 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
981 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
982 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
983 true, !mesh->hasEmptyQuads);
984 } else {
985 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
986 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
987 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
988 true, !mesh->hasEmptyQuads);
989 }
Romain Guy054dc182010-10-15 17:55:25 -0700990 }
Romain Guyf7f93552010-07-08 19:17:03 -0700991}
992
Chet Haase5c13d892010-10-08 08:37:55 -0700993void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guy7f78b0c2010-11-04 14:36:26 -0700994 // TODO: Should do quickReject for each line
Romain Guyaf636eb2010-12-09 17:47:21 -0800995 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -0700996
Romain Guya957eea2010-12-08 18:34:42 -0800997 const bool isAA = paint->isAntiAlias();
998 const float strokeWidth = paint->getStrokeWidth() * 0.5f;
999 // A stroke width of 0 has a special meaningin Skia:
1000 // it draws an unscaled 1px wide line
1001 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1002
Romain Guy7f78b0c2010-11-04 14:36:26 -07001003 setupDraw();
1004
Romain Guy759ea802010-09-16 20:49:46 -07001005 int alpha;
1006 SkXfermode::Mode mode;
1007 getAlphaAndMode(paint, &alpha, &mode);
1008
1009 uint32_t color = paint->getColor();
1010 const GLfloat a = alpha / 255.0f;
1011 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1012 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1013 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1014
Romain Guya957eea2010-12-08 18:34:42 -08001015 // Used only with AA lines
1016 GLuint textureUnit = 0;
1017
1018 // Describe the required shaders
1019 ProgramDescription description;
1020 const bool setColor = description.setColor(r, g, b, a);
1021
1022 if (mShader) {
1023 mShader->describe(description, mCaches.extensions);
1024 }
1025 if (mColorFilter) {
1026 mColorFilter->describe(description, mCaches.extensions);
1027 }
1028
1029 // Setup the blending mode
1030 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
1031
1032 // We're not drawing with VBOs here
1033 mCaches.unbindMeshBuffer();
1034
1035 int verticesCount = count >> 2;
1036 if (!isHairLine) {
1037 // TODO: AA needs more vertices
1038 verticesCount *= 6;
Romain Guyc95c8d62010-09-17 15:31:32 -07001039 } else {
Romain Guya957eea2010-12-08 18:34:42 -08001040 // TODO: AA will be different
1041 verticesCount *= 2;
Romain Guyc95c8d62010-09-17 15:31:32 -07001042 }
1043
Romain Guya957eea2010-12-08 18:34:42 -08001044 TextureVertex lines[verticesCount];
1045 TextureVertex* vertex = &lines[0];
Romain Guy759ea802010-09-16 20:49:46 -07001046
Romain Guya957eea2010-12-08 18:34:42 -08001047 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1048 gMeshStride, vertex);
Romain Guy759ea802010-09-16 20:49:46 -07001049
Romain Guya957eea2010-12-08 18:34:42 -08001050 mModelView.loadIdentity();
Romain Guy759ea802010-09-16 20:49:46 -07001051
Romain Guya957eea2010-12-08 18:34:42 -08001052 // Build and use the appropriate shader
1053 useProgram(mCaches.programCache.get(description));
1054 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy759ea802010-09-16 20:49:46 -07001055
Romain Guya957eea2010-12-08 18:34:42 -08001056 if (!mShader || (mShader && setColor)) {
1057 mCaches.currentProgram->setColor(r, g, b, a);
Romain Guy759ea802010-09-16 20:49:46 -07001058 }
1059
Romain Guya957eea2010-12-08 18:34:42 -08001060 if (mShader) {
1061 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
1062 }
1063 if (mColorFilter) {
1064 mColorFilter->setupProgram(mCaches.currentProgram);
1065 }
1066
1067 if (!isHairLine) {
1068 // TODO: Handle the AA case
1069 for (int i = 0; i < count; i += 4) {
1070 // a = start point, b = end point
1071 vec2 a(points[i], points[i + 1]);
1072 vec2 b(points[i + 2], points[i + 3]);
1073
1074 // Bias to snap to the same pixels as Skia
1075 a += 0.375;
1076 b += 0.375;
1077
1078 // Find the normal to the line
1079 vec2 n = (b - a).copyNormalized() * strokeWidth;
1080 float x = n.x;
1081 n.x = -n.y;
1082 n.y = x;
1083
1084 // Four corners of the rectangle defining a thick line
1085 vec2 p1 = a - n;
1086 vec2 p2 = a + n;
1087 vec2 p3 = b + n;
1088 vec2 p4 = b - n;
1089
1090 // Draw the line as 2 triangles, could be optimized
1091 // by using only 4 vertices and the correct indices
1092 // Also we should probably used non textured vertices
1093 // when line AA is disabled to save on bandwidth
1094 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1095 TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
1096 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1097 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1098 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1099 TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
1100
1101 // TODO: Mark the dirty regions when RENDER_LAYERS_AS_REGIONS is set
1102 }
1103
1104 // GL_LINE does not give the result we want to match Skia
1105 glDrawArrays(GL_TRIANGLES, 0, verticesCount);
1106 } else {
1107 // TODO: Handle the AA case
1108 for (int i = 0; i < count; i += 4) {
1109 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1110 TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
1111 }
1112 glLineWidth(1.0f);
1113 glDrawArrays(GL_LINES, 0, verticesCount);
Romain Guy29d89972010-09-22 16:10:57 -07001114 }
Romain Guy759ea802010-09-16 20:49:46 -07001115}
1116
Romain Guy85bf02f2010-06-22 13:11:24 -07001117void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001118 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001119 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001120
Romain Guyae88e5e2010-10-22 17:49:18 -07001121 Rect& clip(*mSnapshot->clipRect);
1122 clip.snapToPixelBoundaries();
Romain Guy3d58c032010-07-14 16:34:53 -07001123 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001124}
Romain Guy9d5316e2010-06-24 19:30:36 -07001125
Chet Haase5c13d892010-10-08 08:37:55 -07001126void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -07001127 if (quickReject(left, top, right, bottom)) {
1128 return;
1129 }
1130
Romain Guy026c5e162010-06-28 17:12:22 -07001131 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001132 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001133 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1134 if (!isMode) {
1135 // Assume SRC_OVER
1136 mode = SkXfermode::kSrcOver_Mode;
1137 }
1138 } else {
1139 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001140 }
1141
1142 // Skia draws using the color's alpha channel if < 255
1143 // Otherwise, it uses the paint's alpha
1144 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07001145 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -07001146 color |= p->getAlpha() << 24;
1147 }
1148
1149 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001150}
Romain Guy9d5316e2010-06-24 19:30:36 -07001151
Romain Guye8e62a42010-07-23 18:55:21 -07001152void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1153 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001154 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07001155 return;
1156 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001157 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001158
Romain Guyf607bdc2010-09-10 19:20:06 -07001159 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001160
Romain Guya674ab72010-08-10 17:26:42 -07001161 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001162 switch (paint->getTextAlign()) {
1163 case SkPaint::kCenter_Align:
1164 length = paint->measureText(text, bytesCount);
1165 x -= length / 2.0f;
1166 break;
1167 case SkPaint::kRight_Align:
1168 length = paint->measureText(text, bytesCount);
1169 x -= length;
1170 break;
1171 default:
1172 break;
1173 }
1174
Romain Guy6620c6d2010-12-06 18:07:02 -08001175 // TODO: Handle paint->getTextScaleX()
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001176 const float oldX = x;
1177 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08001178 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1179 if (pureTranslate) {
1180 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
1181 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
1182 }
1183
Romain Guy694b5192010-07-21 21:33:20 -07001184 int alpha;
1185 SkXfermode::Mode mode;
1186 getAlphaAndMode(paint, &alpha, &mode);
1187
Romain Guy2542d192010-08-18 11:47:12 -07001188 uint32_t color = paint->getColor();
1189 const GLfloat a = alpha / 255.0f;
1190 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1191 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1192 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1193
Romain Guyb45c0c92010-08-26 20:35:23 -07001194 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1195 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001196 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001197
Romain Guy746b7402010-10-26 16:27:31 -07001198 setupDraw();
Romain Guy9d13fe252010-10-15 16:06:03 -07001199
Romain Guy1e45aae2010-08-13 19:39:53 -07001200 if (mHasShadow) {
1201 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -07001202 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001203 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001204 count, mShadowRadius);
1205 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001206
Romain Guy6620c6d2010-12-06 18:07:02 -08001207 setupShadow(shadow, x, y, mode, a, pureTranslate);
Romain Guy0a417492010-08-16 20:26:20 -07001208
1209 // Draw the mesh
1210 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -07001211 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -07001212 }
1213
Romain Guy06f96e22010-07-30 19:18:16 -07001214 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -07001215 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -07001216
Romain Guy6620c6d2010-12-06 18:07:02 -08001217 // Pick the appropriate texture filtering
1218 bool linearFilter = mSnapshot->transform->changesBounds();
1219 if (pureTranslate && !linearFilter) {
1220 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
1221 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001222
1223 // Dimensions are set to (0,0), the layer (if any) won't be dirtied
Romain Guye8cb9c142010-10-04 14:14:11 -07001224 setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit,
Romain Guy6620c6d2010-12-06 18:07:02 -08001225 x, y, r, g, b, a, mode, false, true, NULL, NULL, 0, pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07001226
Romain Guy6620c6d2010-12-06 18:07:02 -08001227 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001228 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1229
1230#if RENDER_LAYERS_AS_REGIONS
1231 bool hasLayer = (mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region;
1232#else
1233 bool hasLayer = false;
1234#endif
Romain Guy9d13fe252010-10-15 16:06:03 -07001235
Romain Guy03750a02010-10-18 14:06:08 -07001236 mCaches.unbindMeshBuffer();
Romain Guy6620c6d2010-12-06 18:07:02 -08001237 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy5b3b3522010-10-27 18:57:51 -07001238 hasLayer ? &bounds : NULL)) {
1239#if RENDER_LAYERS_AS_REGIONS
1240 if (hasLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001241 if (!pureTranslate) {
1242 mSnapshot->transform->mapRect(bounds);
1243 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001244 bounds.intersect(*mSnapshot->clipRect);
1245 bounds.snapToPixelBoundaries();
1246
1247 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1248 mSnapshot->region->orSelf(dirty);
1249 }
1250#endif
1251 }
Romain Guy694b5192010-07-21 21:33:20 -07001252
1253 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001254 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001255
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001256 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001257}
1258
Romain Guy7fbcc042010-08-04 15:40:07 -07001259void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001260 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001261
Romain Guy7fbcc042010-08-04 15:40:07 -07001262 GLuint textureUnit = 0;
1263 glActiveTexture(gTextureUnits[textureUnit]);
1264
Romain Guyfb8b7632010-08-23 21:05:08 -07001265 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001266 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001267 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001268
Romain Guy8b55f372010-08-18 17:10:07 -07001269 const float x = texture->left - texture->offset;
1270 const float y = texture->top - texture->offset;
1271
1272 if (quickReject(x, y, x + texture->width, y + texture->height)) {
1273 return;
1274 }
1275
Romain Guy7fbcc042010-08-04 15:40:07 -07001276 int alpha;
1277 SkXfermode::Mode mode;
1278 getAlphaAndMode(paint, &alpha, &mode);
1279
1280 uint32_t color = paint->getColor();
1281 const GLfloat a = alpha / 255.0f;
1282 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1283 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1284 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1285
Romain Guy0a417492010-08-16 20:26:20 -07001286 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
1287
Romain Guy746b7402010-10-26 16:27:31 -07001288 setupDraw();
Romain Guy86942302010-09-12 13:02:16 -07001289
Romain Guy0a417492010-08-16 20:26:20 -07001290 // Draw the mesh
1291 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -07001292 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -07001293}
1294
Romain Guy6926c722010-07-12 20:20:03 -07001295///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001296// Shaders
1297///////////////////////////////////////////////////////////////////////////////
1298
1299void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001300 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001301}
1302
Romain Guy06f96e22010-07-30 19:18:16 -07001303void OpenGLRenderer::setupShader(SkiaShader* shader) {
1304 mShader = shader;
1305 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001306 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001307 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001308}
1309
Romain Guyd27977d2010-07-14 19:18:51 -07001310///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001311// Color filters
1312///////////////////////////////////////////////////////////////////////////////
1313
1314void OpenGLRenderer::resetColorFilter() {
1315 mColorFilter = NULL;
1316}
1317
1318void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1319 mColorFilter = filter;
1320}
1321
1322///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07001323// Drop shadow
1324///////////////////////////////////////////////////////////////////////////////
1325
1326void OpenGLRenderer::resetShadow() {
1327 mHasShadow = false;
1328}
1329
1330void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
1331 mHasShadow = true;
1332 mShadowRadius = radius;
1333 mShadowDx = dx;
1334 mShadowDy = dy;
1335 mShadowColor = color;
1336}
1337
1338///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001339// Drawing implementation
1340///////////////////////////////////////////////////////////////////////////////
1341
Romain Guy0a417492010-08-16 20:26:20 -07001342void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy6620c6d2010-12-06 18:07:02 -08001343 SkXfermode::Mode mode, float alpha, bool ignoreTransforms) {
Romain Guy0a417492010-08-16 20:26:20 -07001344 const float sx = x - texture->left + mShadowDx;
1345 const float sy = y - texture->top + mShadowDy;
1346
Romain Guy2542d192010-08-18 11:47:12 -07001347 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1348 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -07001349 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
1350 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
1351 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
1352
1353 GLuint textureUnit = 0;
Romain Guy6620c6d2010-12-06 18:07:02 -08001354 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
1355 sx, sy, r, g, b, a, mode, true, false,
1356 (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 0, ignoreTransforms);
Romain Guy0a417492010-08-16 20:26:20 -07001357}
1358
1359void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
1360 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
1361 bool transforms, bool applyFilters) {
1362 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -07001363 x, y, r, g, b, a, mode, transforms, applyFilters,
Romain Guy03750a02010-10-18 14:06:08 -07001364 (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy0a417492010-08-16 20:26:20 -07001365}
1366
1367void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1368 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1369 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy03750a02010-10-18 14:06:08 -07001370 setupTextureAlpha8(texture, width, height, textureUnit, x, y, r, g, b, a, mode,
1371 transforms, applyFilters, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy759ea802010-09-16 20:49:46 -07001372}
1373
1374void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1375 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1376 SkXfermode::Mode mode, bool transforms, bool applyFilters,
Romain Guy6620c6d2010-12-06 18:07:02 -08001377 GLvoid* vertices, GLvoid* texCoords, GLuint vbo, bool ignoreTransform) {
Romain Guy0a417492010-08-16 20:26:20 -07001378 // Describe the required shaders
1379 ProgramDescription description;
1380 description.hasTexture = true;
1381 description.hasAlpha8Texture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001382 const bool setColor = description.setAlpha8Color(r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -07001383
1384 if (applyFilters) {
1385 if (mShader) {
Romain Guy746b7402010-10-26 16:27:31 -07001386 mShader->describe(description, mCaches.extensions);
Romain Guy0a417492010-08-16 20:26:20 -07001387 }
1388 if (mColorFilter) {
Romain Guy746b7402010-10-26 16:27:31 -07001389 mColorFilter->describe(description, mCaches.extensions);
Romain Guy0a417492010-08-16 20:26:20 -07001390 }
1391 }
1392
Romain Guya5aed0d2010-09-09 14:42:43 -07001393 // Setup the blending mode
1394 chooseBlending(true, mode, description);
1395
Romain Guy0a417492010-08-16 20:26:20 -07001396 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001397 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -07001398
Romain Guy746b7402010-10-26 16:27:31 -07001399 bindTexture(texture);
Romain Guyfb8b7632010-08-23 21:05:08 -07001400 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001401
Romain Guyfb8b7632010-08-23 21:05:08 -07001402 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -07001403 glEnableVertexAttribArray(texCoordsSlot);
1404
Romain Guy03750a02010-10-18 14:06:08 -07001405 if (texCoords) {
1406 // Setup attributes
1407 if (!vertices) {
1408 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1409 } else {
1410 mCaches.unbindMeshBuffer();
1411 }
1412 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1413 gMeshStride, vertices);
1414 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1415 }
Romain Guy0a417492010-08-16 20:26:20 -07001416
1417 // Setup uniforms
1418 if (transforms) {
1419 mModelView.loadTranslate(x, y, 0.0f);
1420 mModelView.scale(width, height, 1.0f);
1421 } else {
1422 mModelView.loadIdentity();
1423 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001424
Romain Guy6620c6d2010-12-06 18:07:02 -08001425 mat4 t;
1426 if (!ignoreTransform) {
1427 t.load(*mSnapshot->transform);
1428 }
1429
1430 mCaches.currentProgram->set(mOrthoMatrix, mModelView, t);
Romain Guy5b3b3522010-10-27 18:57:51 -07001431 if (width > 0 && height > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001432 dirtyLayer(x, y, x + width, y + height, t);
Romain Guy5b3b3522010-10-27 18:57:51 -07001433 }
1434
Romain Guy707b2f72010-10-11 16:34:59 -07001435 if (setColor) {
1436 mCaches.currentProgram->setColor(r, g, b, a);
1437 }
Romain Guy0a417492010-08-16 20:26:20 -07001438
1439 textureUnit++;
1440 if (applyFilters) {
1441 // Setup attributes and uniforms required by the shaders
1442 if (mShader) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001443 if (ignoreTransform) {
1444 mModelView.loadInverse(*mSnapshot->transform);
1445 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001446 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001447 }
1448 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001449 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -07001450 }
1451 }
1452}
1453
Romain Guyf607bdc2010-09-10 19:20:06 -07001454// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001455#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1456#define kStdUnderline_Offset (1.0f / 9.0f)
1457#define kStdUnderline_Thickness (1.0f / 18.0f)
1458
1459void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1460 float x, float y, SkPaint* paint) {
1461 // Handle underline and strike-through
1462 uint32_t flags = paint->getFlags();
1463 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1464 float underlineWidth = length;
1465 // If length is > 0.0f, we already measured the text for the text alignment
1466 if (length <= 0.0f) {
1467 underlineWidth = paint->measureText(text, bytesCount);
1468 }
1469
1470 float offsetX = 0;
1471 switch (paint->getTextAlign()) {
1472 case SkPaint::kCenter_Align:
1473 offsetX = underlineWidth * 0.5f;
1474 break;
1475 case SkPaint::kRight_Align:
1476 offsetX = underlineWidth;
1477 break;
1478 default:
1479 break;
1480 }
1481
1482 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001483 const float textSize = paint->getTextSize();
1484 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -07001485
Romain Guye20ecbd2010-09-22 19:49:04 -07001486 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001487 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001488
1489 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
1490 float points[pointsCount];
1491 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001492
1493 if (flags & SkPaint::kUnderlineText_Flag) {
1494 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001495 points[currentPoint++] = left;
1496 points[currentPoint++] = top;
1497 points[currentPoint++] = left + underlineWidth;
1498 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001499 }
1500
1501 if (flags & SkPaint::kStrikeThruText_Flag) {
1502 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001503 points[currentPoint++] = left;
1504 points[currentPoint++] = top;
1505 points[currentPoint++] = left + underlineWidth;
1506 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001507 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001508
1509 SkPaint linesPaint(*paint);
1510 linesPaint.setStrokeWidth(strokeWidth);
1511
1512 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001513 }
1514 }
1515}
1516
Romain Guy026c5e162010-06-28 17:12:22 -07001517void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001518 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy746b7402010-10-26 16:27:31 -07001519 setupDraw();
Romain Guy86942302010-09-12 13:02:16 -07001520
Romain Guyd27977d2010-07-14 19:18:51 -07001521 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001522 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001523 color |= 0x00ffffff;
1524 }
1525
1526 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -07001527 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -07001528 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -07001529 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1530 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1531 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1532
Romain Guyc95c8d62010-09-17 15:31:32 -07001533 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform);
1534
1535 // Draw the mesh
1536 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1537}
1538
1539void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom,
Romain Guy7f78b0c2010-11-04 14:36:26 -07001540 float r, float g, float b, float a, SkXfermode::Mode mode,
1541 bool ignoreTransform, bool ignoreMatrix) {
Romain Guy06f96e22010-07-30 19:18:16 -07001542 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -07001543
Romain Guy06f96e22010-07-30 19:18:16 -07001544 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -07001545 ProgramDescription description;
Romain Guy707b2f72010-10-11 16:34:59 -07001546 const bool setColor = description.setColor(r, g, b, a);
1547
Romain Guy06f96e22010-07-30 19:18:16 -07001548 if (mShader) {
Romain Guy746b7402010-10-26 16:27:31 -07001549 mShader->describe(description, mCaches.extensions);
Romain Guy6926c722010-07-12 20:20:03 -07001550 }
Romain Guydb1938e2010-08-02 18:50:22 -07001551 if (mColorFilter) {
Romain Guy746b7402010-10-26 16:27:31 -07001552 mColorFilter->describe(description, mCaches.extensions);
Romain Guydb1938e2010-08-02 18:50:22 -07001553 }
Romain Guyd27977d2010-07-14 19:18:51 -07001554
Romain Guy1c740bc2010-09-13 18:00:09 -07001555 // Setup the blending mode
Romain Guyc95c8d62010-09-17 15:31:32 -07001556 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -07001557
Romain Guy06f96e22010-07-30 19:18:16 -07001558 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001559 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -07001560
1561 // Setup attributes
Romain Guy03750a02010-10-18 14:06:08 -07001562 mCaches.bindMeshBuffer();
Romain Guyfb8b7632010-08-23 21:05:08 -07001563 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy03750a02010-10-18 14:06:08 -07001564 gMeshStride, 0);
Romain Guy06f96e22010-07-30 19:18:16 -07001565
Romain Guy7f78b0c2010-11-04 14:36:26 -07001566 if (!ignoreMatrix) {
1567 // Setup uniforms
1568 mModelView.loadTranslate(left, top, 0.0f);
1569 mModelView.scale(right - left, bottom - top, 1.0f);
1570 if (!ignoreTransform) {
1571 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1572 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1573 } else {
1574 mat4 identity;
1575 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
1576 dirtyLayer(left, top, right, bottom);
1577 }
Romain Guyd27977d2010-07-14 19:18:51 -07001578 }
Romain Guy01d06572010-11-11 12:06:27 -08001579 if (!mShader || (mShader && setColor)) {
1580 mCaches.currentProgram->setColor(r, g, b, a);
1581 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001582
Romain Guy06f96e22010-07-30 19:18:16 -07001583 // Setup attributes and uniforms required by the shaders
1584 if (mShader) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001585 if (ignoreMatrix) {
1586 mModelView.loadIdentity();
1587 } else if (ignoreTransform) {
1588 mModelView.loadInverse(*mSnapshot->transform);
1589 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001590 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001591 }
Romain Guydb1938e2010-08-02 18:50:22 -07001592 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001593 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001594 }
Romain Guyd27977d2010-07-14 19:18:51 -07001595}
1596
Romain Guy82ba8142010-07-09 13:25:56 -07001597void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001598 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001599 int alpha;
1600 SkXfermode::Mode mode;
1601 getAlphaAndMode(paint, &alpha, &mode);
1602
Romain Guy8164c2d2010-10-25 18:03:28 -07001603 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1604
Romain Guy6620c6d2010-12-06 18:07:02 -08001605 if (mSnapshot->transform->isPureTranslate()) {
1606 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1607 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1608
1609 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1610 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
1611 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
1612 } else {
1613 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1614 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
1615 GL_TRIANGLE_STRIP, gMeshCount);
1616 }
Romain Guy85bf02f2010-06-22 13:11:24 -07001617}
1618
Romain Guybd6b79b2010-06-26 00:13:53 -07001619void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001620 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1621 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001622 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001623}
1624
1625void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001626 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001627 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07001628 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy746b7402010-10-26 16:27:31 -07001629 setupDraw();
Romain Guy86942302010-09-12 13:02:16 -07001630
Romain Guy889f8d12010-07-29 14:37:42 -07001631 ProgramDescription description;
1632 description.hasTexture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001633 const bool setColor = description.setColor(alpha, alpha, alpha, alpha);
Romain Guydb1938e2010-08-02 18:50:22 -07001634 if (mColorFilter) {
Romain Guy746b7402010-10-26 16:27:31 -07001635 mColorFilter->describe(description, mCaches.extensions);
Romain Guydb1938e2010-08-02 18:50:22 -07001636 }
Romain Guy889f8d12010-07-29 14:37:42 -07001637
Romain Guybd6b79b2010-06-26 00:13:53 -07001638 mModelView.loadTranslate(left, top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07001639 if (!ignoreScale) {
1640 mModelView.scale(right - left, bottom - top, 1.0f);
1641 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001642
Romain Guyf607bdc2010-09-10 19:20:06 -07001643 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001644
Romain Guyfb8b7632010-08-23 21:05:08 -07001645 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001646 if (!ignoreTransform) {
1647 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001648 if (dirty) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001649 } else {
Romain Guy5b3b3522010-10-27 18:57:51 -07001650 mat4 identity;
1651 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
1652 if (dirty) dirtyLayer(left, top, right, bottom);
Romain Guyf607bdc2010-09-10 19:20:06 -07001653 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001654
Romain Guy889f8d12010-07-29 14:37:42 -07001655 // Texture
Romain Guy8164c2d2010-10-25 18:03:28 -07001656 bindTexture(texture);
Romain Guyfb8b7632010-08-23 21:05:08 -07001657 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001658
Romain Guya9794742010-07-13 11:37:54 -07001659 // Always premultiplied
Romain Guy707b2f72010-10-11 16:34:59 -07001660 if (setColor) {
1661 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha);
1662 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001663
Romain Guy889f8d12010-07-29 14:37:42 -07001664 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001665 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001666 glEnableVertexAttribArray(texCoordsSlot);
Romain Guy03750a02010-10-18 14:06:08 -07001667
1668 if (!vertices) {
1669 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1670 } else {
1671 mCaches.unbindMeshBuffer();
1672 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001673 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001674 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001675 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001676
Romain Guydb1938e2010-08-02 18:50:22 -07001677 // Color filter
1678 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001679 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001680 }
1681
Romain Guy6820ac82010-09-15 18:11:50 -07001682 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001683 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001684}
1685
Romain Guya5aed0d2010-09-09 14:42:43 -07001686void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001687 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001688 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1689 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001690 if (mode < SkXfermode::kPlus_Mode) {
1691 if (!mCaches.blend) {
1692 glEnable(GL_BLEND);
1693 }
Romain Guy82ba8142010-07-09 13:25:56 -07001694
Romain Guyf607bdc2010-09-10 19:20:06 -07001695 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1696 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001697
Romain Guya5aed0d2010-09-09 14:42:43 -07001698 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1699 glBlendFunc(sourceMode, destMode);
1700 mCaches.lastSrcMode = sourceMode;
1701 mCaches.lastDstMode = destMode;
1702 }
1703 } else {
1704 // These blend modes are not supported by OpenGL directly and have
1705 // to be implemented using shaders. Since the shader will perform
1706 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001707 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001708 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001709 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001710 }
1711
1712 if (mCaches.blend) {
1713 glDisable(GL_BLEND);
1714 }
1715 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001716 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001717 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001718 glDisable(GL_BLEND);
1719 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001720 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001721}
1722
Romain Guy889f8d12010-07-29 14:37:42 -07001723bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001724 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001725 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001726 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001727 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001728 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001729 }
Romain Guy6926c722010-07-12 20:20:03 -07001730 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001731}
1732
Romain Guy026c5e162010-06-28 17:12:22 -07001733void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001734 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001735 TextureVertex::setUV(v++, u1, v1);
1736 TextureVertex::setUV(v++, u2, v1);
1737 TextureVertex::setUV(v++, u1, v2);
1738 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001739}
1740
Chet Haase5c13d892010-10-08 08:37:55 -07001741void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001742 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07001743 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001744 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1745 if (!isMode) {
1746 // Assume SRC_OVER
1747 *mode = SkXfermode::kSrcOver_Mode;
1748 }
1749 } else {
1750 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001751 }
1752
1753 // Skia draws using the color's alpha channel if < 255
1754 // Otherwise, it uses the paint's alpha
1755 int color = paint->getColor();
1756 *alpha = (color >> 24) & 0xFF;
1757 if (*alpha == 255) {
1758 *alpha = paint->getAlpha();
1759 }
1760 } else {
1761 *mode = SkXfermode::kSrcOver_Mode;
1762 *alpha = 255;
1763 }
Romain Guy026c5e162010-06-28 17:12:22 -07001764}
1765
Romain Guya5aed0d2010-09-09 14:42:43 -07001766SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1767 if (mode == NULL) {
1768 return SkXfermode::kSrcOver_Mode;
1769 }
1770 return mode->fMode;
1771}
1772
Romain Guy746b7402010-10-26 16:27:31 -07001773void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07001774 bool bound = false;
1775 if (wrapS != texture->wrapS) {
1776 glBindTexture(GL_TEXTURE_2D, texture->id);
1777 bound = true;
1778 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1779 texture->wrapS = wrapS;
1780 }
1781 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001782 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001783 glBindTexture(GL_TEXTURE_2D, texture->id);
1784 }
Romain Guy8164c2d2010-10-25 18:03:28 -07001785 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1786 texture->wrapT = wrapT;
1787 }
Romain Guya1db5742010-07-20 13:09:13 -07001788}
1789
Romain Guy9d5316e2010-06-24 19:30:36 -07001790}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001791}; // namespace android