blob: e99f73a9c64e71313f5f5ac40be231f238613bfd [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 Guy32963c32010-12-10 14:43:41 -0800388 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700389 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700390 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700391 }
392
393 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800394 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700395 return false;
396 }
Romain Guyf18fd992010-07-08 11:45:51 -0700397
Romain Guy5b3b3522010-10-27 18:57:51 -0700398 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700399 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700400 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700401 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700402 }
403
Romain Guydda570202010-07-06 11:39:32 -0700404 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700405 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700406 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700407 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
408 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guydda570202010-07-06 11:39:32 -0700409
Romain Guy8fb95422010-08-17 18:38:51 -0700410 // Save the layer in the snapshot
411 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700412 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700413
Romain Guyeb993562010-10-05 18:14:38 -0700414 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700415 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700416 } else {
417 // Copy the framebuffer into the layer
418 glBindTexture(GL_TEXTURE_2D, layer->texture);
419
Romain Guyae88e5e2010-10-22 17:49:18 -0700420 if (layer->empty) {
421 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
422 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
423 layer->empty = false;
424 } else {
425 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
426 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
427 }
Romain Guyeb993562010-10-05 18:14:38 -0700428
429 // Enqueue the buffer coordinates to clear the corresponding region later
430 mLayers.push(new Rect(bounds));
431 }
Romain Guyf86ef572010-07-01 11:05:42 -0700432
Romain Guyd55a8612010-06-28 17:42:46 -0700433 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700434}
435
Romain Guy5b3b3522010-10-27 18:57:51 -0700436bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
437 GLuint previousFbo) {
438 layer->fbo = mCaches.fboCache.get();
439
440#if RENDER_LAYERS_AS_REGIONS
441 snapshot->region = &snapshot->layer->region;
442 snapshot->flags |= Snapshot::kFlagFboTarget;
443#endif
444
445 Rect clip(bounds);
446 snapshot->transform->mapRect(clip);
447 clip.intersect(*snapshot->clipRect);
448 clip.snapToPixelBoundaries();
449 clip.intersect(snapshot->previous->viewport);
450
451 mat4 inverse;
452 inverse.loadInverse(*mSnapshot->transform);
453
454 inverse.mapRect(clip);
455 clip.snapToPixelBoundaries();
456 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700457 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700458
459 snapshot->flags |= Snapshot::kFlagIsFboLayer;
460 snapshot->fbo = layer->fbo;
461 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
462 //snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
463 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
464 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
465 snapshot->height = bounds.getHeight();
466 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
467 snapshot->orthoMatrix.load(mOrthoMatrix);
468
469 // Bind texture to FBO
470 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
471 glBindTexture(GL_TEXTURE_2D, layer->texture);
472
473 // Initialize the texture if needed
474 if (layer->empty) {
475 layer->empty = false;
476 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
477 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
478 }
479
480 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
481 layer->texture, 0);
482
483#if DEBUG_LAYERS_AS_REGIONS
484 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
485 if (status != GL_FRAMEBUFFER_COMPLETE) {
486 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
487
488 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
489 glDeleteTextures(1, &layer->texture);
490 mCaches.fboCache.put(layer->fbo);
491
492 delete layer;
493
494 return false;
495 }
496#endif
497
498 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
499 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
500 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
501 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
502 glClear(GL_COLOR_BUFFER_BIT);
503
504 dirtyClip();
505
506 // Change the ortho projection
507 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
508 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
509
510 return true;
511}
512
Romain Guy1c740bc2010-09-13 18:00:09 -0700513/**
514 * Read the documentation of createLayer() before doing anything in this method.
515 */
Romain Guy1d83e192010-08-17 11:37:00 -0700516void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
517 if (!current->layer) {
518 LOGE("Attempting to compose a layer that does not exist");
519 return;
520 }
521
Romain Guy5b3b3522010-10-27 18:57:51 -0700522 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700523
524 if (fboLayer) {
525 // Unbind current FBO and restore previous one
526 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
527 }
528
Romain Guy1d83e192010-08-17 11:37:00 -0700529 Layer* layer = current->layer;
530 const Rect& rect = layer->layer;
531
Romain Guyeb993562010-10-05 18:14:38 -0700532 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700533 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700534 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700535 // Required below, composeLayerRect() will divide by 255
536 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700537 }
538
Romain Guy03750a02010-10-18 14:06:08 -0700539 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700540
Romain Guy746b7402010-10-26 16:27:31 -0700541 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700542
Romain Guy5b3b3522010-10-27 18:57:51 -0700543 // When the layer is stored in an FBO, we can save a bit of fillrate by
544 // drawing only the dirty region
545 if (fboLayer) {
546 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
547 composeLayerRegion(layer, rect);
548 } else {
549 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
550 composeLayerRect(layer, rect, true);
551 }
Romain Guy8b55f372010-08-18 17:10:07 -0700552
Romain Guyeb993562010-10-05 18:14:38 -0700553 if (fboLayer) {
554 // Detach the texture from the FBO
555 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
556 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
557 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
558
559 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
560 mCaches.fboCache.put(current->fbo);
561 }
562
Romain Guy746b7402010-10-26 16:27:31 -0700563 dirtyClip();
564
Romain Guyeb993562010-10-05 18:14:38 -0700565 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700566 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700567 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700568 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700569 delete layer;
570 }
571}
572
Romain Guy5b3b3522010-10-27 18:57:51 -0700573void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
574 const Rect& texCoords = layer->texCoords;
575 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
576
577 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
578 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
579 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
580
581 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
582}
583
584void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
585#if RENDER_LAYERS_AS_REGIONS
586 if (layer->region.isRect()) {
587 composeLayerRect(layer, rect);
588 layer->region.clear();
589 return;
590 }
591
592 if (!layer->region.isEmpty()) {
593 size_t count;
594 const android::Rect* rects = layer->region.getArray(&count);
595
596 setupDraw();
597
598 ProgramDescription description;
599 description.hasTexture = true;
600
601 const float alpha = layer->alpha / 255.0f;
602 const bool setColor = description.setColor(alpha, alpha, alpha, alpha);
603 chooseBlending(layer->blend || layer->alpha < 255, layer->mode, description, false);
604
605 useProgram(mCaches.programCache.get(description));
606
607 // Texture
608 bindTexture(layer->texture);
609 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
610
611 // Always premultiplied
612 if (setColor) {
613 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha);
614 }
615
616 // Mesh
617 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
618 glEnableVertexAttribArray(texCoordsSlot);
619
620 mModelView.loadIdentity();
621 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
622
623 const float texX = 1.0f / float(layer->width);
624 const float texY = 1.0f / float(layer->height);
625
626 TextureVertex* mesh = mCaches.getRegionMesh();
627 GLsizei numQuads = 0;
628
629 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
630 gMeshStride, &mesh[0].position[0]);
631 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
632 gMeshStride, &mesh[0].texture[0]);
633
634 for (size_t i = 0; i < count; i++) {
635 const android::Rect* r = &rects[i];
636
637 const float u1 = r->left * texX;
638 const float v1 = (rect.getHeight() - r->top) * texY;
639 const float u2 = r->right * texX;
640 const float v2 = (rect.getHeight() - r->bottom) * texY;
641
642 // TODO: Reject quads outside of the clip
643 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
644 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
645 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
646 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
647
648 numQuads++;
649
650 if (numQuads >= REGION_MESH_QUAD_COUNT) {
651 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
652 numQuads = 0;
653 mesh = mCaches.getRegionMesh();
654 }
655 }
656
657 if (numQuads > 0) {
658 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
659 }
660
661 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
662 glDisableVertexAttribArray(texCoordsSlot);
663
664#if DEBUG_LAYERS_AS_REGIONS
665 uint32_t colors[] = {
666 0x7fff0000, 0x7f00ff00,
667 0x7f0000ff, 0x7fff00ff,
668 };
669
670 int offset = 0;
671 int32_t top = rects[0].top;
672 int i = 0;
673
674 for (size_t i = 0; i < count; i++) {
675 if (top != rects[i].top) {
676 offset ^= 0x2;
677 top = rects[i].top;
678 }
679
680 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
681 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
682 SkXfermode::kSrcOver_Mode);
683 }
684#endif
685
686 layer->region.clear();
687 }
688#else
689 composeLayerRect(layer, rect);
690#endif
691}
692
693void OpenGLRenderer::dirtyLayer(const float left, const float top,
694 const float right, const float bottom, const mat4 transform) {
695#if RENDER_LAYERS_AS_REGIONS
696 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region) {
697 Rect bounds(left, top, right, bottom);
698 transform.mapRect(bounds);
699 bounds.intersect(*mSnapshot->clipRect);
700 bounds.snapToPixelBoundaries();
701
702 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
703 if (!dirty.isEmpty()) {
704 mSnapshot->region->orSelf(dirty);
705 }
706 }
707#endif
708}
709
710void OpenGLRenderer::dirtyLayer(const float left, const float top,
711 const float right, const float bottom) {
712#if RENDER_LAYERS_AS_REGIONS
713 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region) {
714 Rect bounds(left, top, right, bottom);
715 bounds.intersect(*mSnapshot->clipRect);
716 bounds.snapToPixelBoundaries();
717
718 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
719 if (!dirty.isEmpty()) {
720 mSnapshot->region->orSelf(dirty);
721 }
722 }
723#endif
724}
725
Romain Guy86942302010-09-12 13:02:16 -0700726void OpenGLRenderer::clearLayerRegions() {
Romain Guyaf636eb2010-12-09 17:47:21 -0800727 if (mLayers.size() == 0 || mSnapshot->isIgnored()) return;
Romain Guy86942302010-09-12 13:02:16 -0700728
Romain Guy5b3b3522010-10-27 18:57:51 -0700729 Rect clipRect(*mSnapshot->clipRect);
730 clipRect.snapToPixelBoundaries();
731
Romain Guy86942302010-09-12 13:02:16 -0700732 for (uint32_t i = 0; i < mLayers.size(); i++) {
733 Rect* bounds = mLayers.itemAt(i);
Romain Guy5b3b3522010-10-27 18:57:51 -0700734 if (clipRect.intersects(*bounds)) {
735 // Clear the framebuffer where the layer will draw
736 glScissor(bounds->left, mSnapshot->height - bounds->bottom,
737 bounds->getWidth(), bounds->getHeight());
738 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
739 glClear(GL_COLOR_BUFFER_BIT);
Romain Guy86942302010-09-12 13:02:16 -0700740
Romain Guy5b3b3522010-10-27 18:57:51 -0700741 // Restore the clip
742 dirtyClip();
743 }
Romain Guy86942302010-09-12 13:02:16 -0700744
745 delete bounds;
746 }
Romain Guy86942302010-09-12 13:02:16 -0700747
Romain Guy5b3b3522010-10-27 18:57:51 -0700748 mLayers.clear();
Romain Guy86942302010-09-12 13:02:16 -0700749}
750
Romain Guybd6b79b2010-06-26 00:13:53 -0700751///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700752// Transforms
753///////////////////////////////////////////////////////////////////////////////
754
755void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700756 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700757}
758
759void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700760 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700761}
762
763void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700764 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700765}
766
767void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700768 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700769}
770
Romain Guy41030da2010-10-13 13:40:37 -0700771const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700772 if (mSnapshot->fbo != 0) {
773 return &mSnapshot->transform->data[0];
774 }
775 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700776}
777
Romain Guyf6a11b82010-06-23 17:47:49 -0700778void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700779 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700780}
781
782void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700783 SkMatrix transform;
784 mSnapshot->transform->copyTo(transform);
785 transform.preConcat(*matrix);
786 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700787}
788
789///////////////////////////////////////////////////////////////////////////////
790// Clipping
791///////////////////////////////////////////////////////////////////////////////
792
Romain Guybb9524b2010-06-22 18:56:38 -0700793void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700794 Rect clip(*mSnapshot->clipRect);
795 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700796 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700797 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700798}
799
800const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700801 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700802}
803
Romain Guyc7d53492010-06-25 13:41:57 -0700804bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800805 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700806 return true;
807 }
808
Romain Guy1d83e192010-08-17 11:37:00 -0700809 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700810 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700811 r.snapToPixelBoundaries();
812
813 Rect clipRect(*mSnapshot->clipRect);
814 clipRect.snapToPixelBoundaries();
815
816 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700817}
818
Romain Guy079ba2c2010-07-16 14:12:24 -0700819bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
820 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700821 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700822 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700823 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700824 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700825}
826
Romain Guyf6a11b82010-06-23 17:47:49 -0700827///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800828// Drawing commands
829///////////////////////////////////////////////////////////////////////////////
830
831void OpenGLRenderer::setupDraw() {
832 clearLayerRegions();
833 if (mDirtyClip) {
834 setScissorFromClip();
835 }
836 mDescription.reset();
837 mSetShaderColor = false;
838 mColorSet = false;
839 mColorA = mColorR = mColorG = mColorB = 0.0f;
840 mTextureUnit = 0;
841 mTrackDirtyRegions = true;
842}
843
844void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
845 mDescription.hasTexture = true;
846 mDescription.hasAlpha8Texture = isAlpha8;
847}
848
849void OpenGLRenderer::setupDrawColor(int color) {
850 mColorA = ((color >> 24) & 0xFF) / 255.0f;
851 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -0800852 mColorR = a * ((color >> 16) & 0xFF);
853 mColorG = a * ((color >> 8) & 0xFF);
854 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -0800855 mColorSet = true;
856 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
857}
858
Romain Guy86568192010-12-14 15:55:39 -0800859void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
860 mColorA = alpha / 255.0f;
861 const float a = mColorA / 255.0f;
862 mColorR = a * ((color >> 16) & 0xFF);
863 mColorG = a * ((color >> 8) & 0xFF);
864 mColorB = a * ((color ) & 0xFF);
865 mColorSet = true;
866 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
867}
868
Romain Guy70ca14e2010-12-13 18:24:33 -0800869void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
870 mColorA = a;
871 mColorR = r;
872 mColorG = g;
873 mColorB = b;
874 mColorSet = true;
875 mSetShaderColor = mDescription.setColor(r, g, b, a);
876}
877
Romain Guy86568192010-12-14 15:55:39 -0800878void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
879 mColorA = a;
880 mColorR = r;
881 mColorG = g;
882 mColorB = b;
883 mColorSet = true;
884 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
885}
886
Romain Guy70ca14e2010-12-13 18:24:33 -0800887void OpenGLRenderer::setupDrawShader() {
888 if (mShader) {
889 mShader->describe(mDescription, mCaches.extensions);
890 }
891}
892
893void OpenGLRenderer::setupDrawColorFilter() {
894 if (mColorFilter) {
895 mColorFilter->describe(mDescription, mCaches.extensions);
896 }
897}
898
899void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
900 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
901 mDescription, swapSrcDst);
902}
903
904void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
905 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
906 mDescription, swapSrcDst);
907}
908
909void OpenGLRenderer::setupDrawProgram() {
910 useProgram(mCaches.programCache.get(mDescription));
911}
912
913void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
914 mTrackDirtyRegions = false;
915}
916
917void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
918 bool ignoreTransform) {
919 mModelView.loadTranslate(left, top, 0.0f);
920 if (!ignoreTransform) {
921 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
922 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
923 } else {
924 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
925 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
926 }
927}
928
929void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
930 bool ignoreTransform, bool ignoreModelView) {
931 if (!ignoreModelView) {
932 mModelView.loadTranslate(left, top, 0.0f);
933 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -0800934 } else {
935 mModelView.loadIdentity();
936 }
Romain Guy86568192010-12-14 15:55:39 -0800937 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
938 if (!ignoreTransform) {
939 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
940 if (mTrackDirtyRegions && dirty) {
941 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
942 }
943 } else {
944 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
945 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
946 }
Romain Guy70ca14e2010-12-13 18:24:33 -0800947}
948
949void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800950 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -0800951 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
952 }
953}
954
Romain Guy86568192010-12-14 15:55:39 -0800955void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800956 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -0800957 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -0800958 }
959}
960
Romain Guy70ca14e2010-12-13 18:24:33 -0800961void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
962 if (mShader) {
963 if (ignoreTransform) {
964 mModelView.loadInverse(*mSnapshot->transform);
965 }
966 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
967 }
968}
969
970void OpenGLRenderer::setupDrawColorFilterUniforms() {
971 if (mColorFilter) {
972 mColorFilter->setupProgram(mCaches.currentProgram);
973 }
974}
975
976void OpenGLRenderer::setupDrawSimpleMesh() {
977 mCaches.bindMeshBuffer();
978 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
979 gMeshStride, 0);
980}
981
982void OpenGLRenderer::setupDrawTexture(GLuint texture) {
983 bindTexture(texture);
984 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
985
986 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
987 glEnableVertexAttribArray(mTexCoordsSlot);
988}
989
990void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
991 if (!vertices) {
992 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
993 } else {
994 mCaches.unbindMeshBuffer();
995 }
996 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
997 gMeshStride, vertices);
998 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
999}
1000
1001void OpenGLRenderer::finishDrawTexture() {
1002 glDisableVertexAttribArray(mTexCoordsSlot);
1003}
1004
1005///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001006// Drawing
1007///////////////////////////////////////////////////////////////////////////////
1008
Romain Guy0fe478e2010-11-08 12:08:41 -08001009void OpenGLRenderer::drawDisplayList(DisplayList* displayList) {
1010 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1011 // will be performed by the display list itself
1012 if (displayList) {
1013 displayList->replay(*this);
1014 }
1015}
1016
Chet Haase5c13d892010-10-08 08:37:55 -07001017void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001018 const float right = left + bitmap->width();
1019 const float bottom = top + bitmap->height();
1020
1021 if (quickReject(left, top, right, bottom)) {
1022 return;
1023 }
1024
Romain Guy86568192010-12-14 15:55:39 -08001025 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001026 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001027 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001028 const AutoTexture autoCleanup(texture);
1029
Romain Guy6926c722010-07-12 20:20:03 -07001030 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -07001031}
1032
Chet Haase5c13d892010-10-08 08:37:55 -07001033void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001034 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1035 const mat4 transform(*matrix);
1036 transform.mapRect(r);
1037
Romain Guy6926c722010-07-12 20:20:03 -07001038 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1039 return;
1040 }
1041
Romain Guy86568192010-12-14 15:55:39 -08001042 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001043 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001044 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001045 const AutoTexture autoCleanup(texture);
1046
Romain Guy5b3b3522010-10-27 18:57:51 -07001047 // This could be done in a cheaper way, all we need is pass the matrix
1048 // to the vertex shader. The save/restore is a bit overkill.
1049 save(SkCanvas::kMatrix_SaveFlag);
1050 concatMatrix(matrix);
1051 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1052 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001053}
1054
Romain Guy8ba548f2010-06-30 19:21:21 -07001055void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1056 float srcLeft, float srcTop, float srcRight, float srcBottom,
1057 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001058 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001059 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1060 return;
1061 }
1062
Romain Guy746b7402010-10-26 16:27:31 -07001063 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001064 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001065 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001066 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001067 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -07001068
Romain Guy8ba548f2010-06-30 19:21:21 -07001069 const float width = texture->width;
1070 const float height = texture->height;
1071
1072 const float u1 = srcLeft / width;
1073 const float v1 = srcTop / height;
1074 const float u2 = srcRight / width;
1075 const float v2 = srcBottom / height;
1076
Romain Guy03750a02010-10-18 14:06:08 -07001077 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001078 resetDrawTextureTexCoords(u1, v1, u2, v2);
1079
Romain Guy03750a02010-10-18 14:06:08 -07001080 int alpha;
1081 SkXfermode::Mode mode;
1082 getAlphaAndMode(paint, &alpha, &mode);
1083
Romain Guy6620c6d2010-12-06 18:07:02 -08001084 if (mSnapshot->transform->isPureTranslate()) {
1085 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1086 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1087
1088 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1089 texture->id, alpha / 255.0f, mode, texture->blend,
1090 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1091 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1092 } else {
1093 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1094 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1095 GL_TRIANGLE_STRIP, gMeshCount);
1096 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001097
1098 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1099}
1100
Romain Guy4aa90572010-09-26 18:40:37 -07001101void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001102 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001103 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001104 if (quickReject(left, top, right, bottom)) {
1105 return;
1106 }
1107
Romain Guy746b7402010-10-26 16:27:31 -07001108 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001109 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001110 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001111 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001112 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -07001113
1114 int alpha;
1115 SkXfermode::Mode mode;
1116 getAlphaAndMode(paint, &alpha, &mode);
1117
Romain Guy2728f962010-10-08 18:36:15 -07001118 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001119 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001120
Romain Guya5ef39a2010-12-03 16:48:20 -08001121 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001122 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001123#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001124 // Mark the current layer dirty where we are going to draw the patch
1125 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) &&
1126 mSnapshot->region && mesh->hasEmptyQuads) {
1127 const size_t count = mesh->quads.size();
1128 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001129 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001130 if (pureTranslate) {
1131 const float x = (int) floorf(bounds.left + 0.5f);
1132 const float y = (int) floorf(bounds.top + 0.5f);
Romain Guy8ab40792010-12-07 13:30:10 -08001133 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Romain Guy6620c6d2010-12-06 18:07:02 -08001134 *mSnapshot->transform);
1135 } else {
1136 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom,
1137 *mSnapshot->transform);
1138 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001139 }
1140 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001141#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001142
Romain Guy6620c6d2010-12-06 18:07:02 -08001143 if (pureTranslate) {
1144 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1145 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1146
1147 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1148 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1149 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1150 true, !mesh->hasEmptyQuads);
1151 } else {
1152 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1153 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1154 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1155 true, !mesh->hasEmptyQuads);
1156 }
Romain Guy054dc182010-10-15 17:55:25 -07001157 }
Romain Guyf7f93552010-07-08 19:17:03 -07001158}
1159
Chet Haase5c13d892010-10-08 08:37:55 -07001160void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guy7f78b0c2010-11-04 14:36:26 -07001161 // TODO: Should do quickReject for each line
Romain Guyaf636eb2010-12-09 17:47:21 -08001162 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001163
Romain Guya957eea2010-12-08 18:34:42 -08001164 const bool isAA = paint->isAntiAlias();
1165 const float strokeWidth = paint->getStrokeWidth() * 0.5f;
1166 // A stroke width of 0 has a special meaningin Skia:
1167 // it draws an unscaled 1px wide line
1168 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1169
Romain Guy7f78b0c2010-11-04 14:36:26 -07001170 setupDraw();
1171
Romain Guy759ea802010-09-16 20:49:46 -07001172 int alpha;
1173 SkXfermode::Mode mode;
1174 getAlphaAndMode(paint, &alpha, &mode);
1175
1176 uint32_t color = paint->getColor();
1177 const GLfloat a = alpha / 255.0f;
1178 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1179 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1180 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1181
Romain Guya957eea2010-12-08 18:34:42 -08001182 // Used only with AA lines
1183 GLuint textureUnit = 0;
1184
1185 // Describe the required shaders
1186 ProgramDescription description;
1187 const bool setColor = description.setColor(r, g, b, a);
1188
1189 if (mShader) {
1190 mShader->describe(description, mCaches.extensions);
1191 }
1192 if (mColorFilter) {
1193 mColorFilter->describe(description, mCaches.extensions);
1194 }
1195
1196 // Setup the blending mode
1197 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
1198
1199 // We're not drawing with VBOs here
1200 mCaches.unbindMeshBuffer();
1201
1202 int verticesCount = count >> 2;
1203 if (!isHairLine) {
1204 // TODO: AA needs more vertices
1205 verticesCount *= 6;
Romain Guyc95c8d62010-09-17 15:31:32 -07001206 } else {
Romain Guya957eea2010-12-08 18:34:42 -08001207 // TODO: AA will be different
1208 verticesCount *= 2;
Romain Guyc95c8d62010-09-17 15:31:32 -07001209 }
1210
Romain Guya957eea2010-12-08 18:34:42 -08001211 TextureVertex lines[verticesCount];
1212 TextureVertex* vertex = &lines[0];
Romain Guy759ea802010-09-16 20:49:46 -07001213
Romain Guya957eea2010-12-08 18:34:42 -08001214 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1215 gMeshStride, vertex);
Romain Guy759ea802010-09-16 20:49:46 -07001216
Romain Guya957eea2010-12-08 18:34:42 -08001217 // Build and use the appropriate shader
1218 useProgram(mCaches.programCache.get(description));
Romain Guy70ca14e2010-12-13 18:24:33 -08001219 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform);
Romain Guy759ea802010-09-16 20:49:46 -07001220
Romain Guya957eea2010-12-08 18:34:42 -08001221 if (!mShader || (mShader && setColor)) {
1222 mCaches.currentProgram->setColor(r, g, b, a);
Romain Guy759ea802010-09-16 20:49:46 -07001223 }
1224
Romain Guya957eea2010-12-08 18:34:42 -08001225 if (mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001226 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &textureUnit);
Romain Guya957eea2010-12-08 18:34:42 -08001227 }
1228 if (mColorFilter) {
1229 mColorFilter->setupProgram(mCaches.currentProgram);
1230 }
1231
1232 if (!isHairLine) {
1233 // TODO: Handle the AA case
1234 for (int i = 0; i < count; i += 4) {
1235 // a = start point, b = end point
1236 vec2 a(points[i], points[i + 1]);
1237 vec2 b(points[i + 2], points[i + 3]);
1238
1239 // Bias to snap to the same pixels as Skia
1240 a += 0.375;
1241 b += 0.375;
1242
1243 // Find the normal to the line
1244 vec2 n = (b - a).copyNormalized() * strokeWidth;
1245 float x = n.x;
1246 n.x = -n.y;
1247 n.y = x;
1248
1249 // Four corners of the rectangle defining a thick line
1250 vec2 p1 = a - n;
1251 vec2 p2 = a + n;
1252 vec2 p3 = b + n;
1253 vec2 p4 = b - n;
1254
1255 // Draw the line as 2 triangles, could be optimized
1256 // by using only 4 vertices and the correct indices
1257 // Also we should probably used non textured vertices
1258 // when line AA is disabled to save on bandwidth
1259 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1260 TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
1261 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1262 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1263 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1264 TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
1265
1266 // TODO: Mark the dirty regions when RENDER_LAYERS_AS_REGIONS is set
1267 }
1268
1269 // GL_LINE does not give the result we want to match Skia
1270 glDrawArrays(GL_TRIANGLES, 0, verticesCount);
1271 } else {
1272 // TODO: Handle the AA case
1273 for (int i = 0; i < count; i += 4) {
1274 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1275 TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
1276 }
1277 glLineWidth(1.0f);
1278 glDrawArrays(GL_LINES, 0, verticesCount);
Romain Guy29d89972010-09-22 16:10:57 -07001279 }
Romain Guy759ea802010-09-16 20:49:46 -07001280}
1281
Romain Guy85bf02f2010-06-22 13:11:24 -07001282void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001283 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001284 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001285
Romain Guyae88e5e2010-10-22 17:49:18 -07001286 Rect& clip(*mSnapshot->clipRect);
1287 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001288
Romain Guy3d58c032010-07-14 16:34:53 -07001289 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001290}
Romain Guy9d5316e2010-06-24 19:30:36 -07001291
Chet Haase5c13d892010-10-08 08:37:55 -07001292void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -07001293 if (quickReject(left, top, right, bottom)) {
1294 return;
1295 }
1296
Romain Guy026c5e162010-06-28 17:12:22 -07001297 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001298 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001299 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1300 if (!isMode) {
1301 // Assume SRC_OVER
1302 mode = SkXfermode::kSrcOver_Mode;
1303 }
1304 } else {
1305 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001306 }
1307
1308 // Skia draws using the color's alpha channel if < 255
1309 // Otherwise, it uses the paint's alpha
1310 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07001311 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -07001312 color |= p->getAlpha() << 24;
1313 }
1314
1315 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001316}
Romain Guy9d5316e2010-06-24 19:30:36 -07001317
Romain Guye8e62a42010-07-23 18:55:21 -07001318void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1319 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001320 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07001321 return;
1322 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001323 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001324
Romain Guyf607bdc2010-09-10 19:20:06 -07001325 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001326
Romain Guya674ab72010-08-10 17:26:42 -07001327 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001328 switch (paint->getTextAlign()) {
1329 case SkPaint::kCenter_Align:
1330 length = paint->measureText(text, bytesCount);
1331 x -= length / 2.0f;
1332 break;
1333 case SkPaint::kRight_Align:
1334 length = paint->measureText(text, bytesCount);
1335 x -= length;
1336 break;
1337 default:
1338 break;
1339 }
1340
Romain Guy6620c6d2010-12-06 18:07:02 -08001341 // TODO: Handle paint->getTextScaleX()
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001342 const float oldX = x;
1343 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08001344 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1345 if (pureTranslate) {
1346 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
1347 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
1348 }
1349
Romain Guyb45c0c92010-08-26 20:35:23 -07001350 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1351 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001352 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001353
Romain Guy86568192010-12-14 15:55:39 -08001354 int alpha;
1355 SkXfermode::Mode mode;
1356 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07001357
Romain Guy1e45aae2010-08-13 19:39:53 -07001358 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07001359 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001360 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001361 count, mShadowRadius);
1362 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001363
Romain Guy86568192010-12-14 15:55:39 -08001364 const float sx = x - shadow->left + mShadowDx;
1365 const float sy = y - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07001366
Romain Guy86568192010-12-14 15:55:39 -08001367 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1368
1369 glActiveTexture(gTextureUnits[0]);
1370 setupDraw();
1371 setupDrawWithTexture(true);
1372 setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
1373 setupDrawBlending(true, mode);
1374 setupDrawProgram();
1375 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate);
1376 setupDrawTexture(shadow->id);
1377 setupDrawPureColorUniforms();
1378 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1379
Romain Guy0a417492010-08-16 20:26:20 -07001380 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy86568192010-12-14 15:55:39 -08001381 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07001382 }
1383
Romain Guy6620c6d2010-12-06 18:07:02 -08001384 // Pick the appropriate texture filtering
1385 bool linearFilter = mSnapshot->transform->changesBounds();
1386 if (pureTranslate && !linearFilter) {
1387 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
1388 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001389
Romain Guy86568192010-12-14 15:55:39 -08001390 glActiveTexture(gTextureUnits[0]);
1391 setupDraw();
1392 setupDrawDirtyRegionsDisabled();
1393 setupDrawWithTexture(true);
1394 setupDrawAlpha8Color(paint->getColor(), alpha);
1395 setupDrawColorFilter();
1396 setupDrawShader();
1397 setupDrawBlending(true, mode);
1398 setupDrawProgram();
1399 setupDrawModelView(x, y, x, y, pureTranslate, true);
1400 setupDrawTexture(fontRenderer.getTexture(linearFilter));
1401 setupDrawPureColorUniforms();
1402 setupDrawColorFilterUniforms();
1403 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07001404
Romain Guy6620c6d2010-12-06 18:07:02 -08001405 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001406 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1407
1408#if RENDER_LAYERS_AS_REGIONS
1409 bool hasLayer = (mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region;
1410#else
1411 bool hasLayer = false;
1412#endif
Romain Guy9d13fe252010-10-15 16:06:03 -07001413
Romain Guy03750a02010-10-18 14:06:08 -07001414 mCaches.unbindMeshBuffer();
Romain Guy6620c6d2010-12-06 18:07:02 -08001415 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy5b3b3522010-10-27 18:57:51 -07001416 hasLayer ? &bounds : NULL)) {
1417#if RENDER_LAYERS_AS_REGIONS
1418 if (hasLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001419 if (!pureTranslate) {
1420 mSnapshot->transform->mapRect(bounds);
1421 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001422 bounds.intersect(*mSnapshot->clipRect);
1423 bounds.snapToPixelBoundaries();
1424
1425 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1426 mSnapshot->region->orSelf(dirty);
1427 }
1428#endif
1429 }
Romain Guy694b5192010-07-21 21:33:20 -07001430
1431 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001432 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001433
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001434 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001435}
1436
Romain Guy7fbcc042010-08-04 15:40:07 -07001437void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001438 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001439
Romain Guy7fbcc042010-08-04 15:40:07 -07001440 GLuint textureUnit = 0;
1441 glActiveTexture(gTextureUnits[textureUnit]);
1442
Romain Guyfb8b7632010-08-23 21:05:08 -07001443 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001444 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001445 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001446
Romain Guy8b55f372010-08-18 17:10:07 -07001447 const float x = texture->left - texture->offset;
1448 const float y = texture->top - texture->offset;
1449
1450 if (quickReject(x, y, x + texture->width, y + texture->height)) {
1451 return;
1452 }
1453
Romain Guy7fbcc042010-08-04 15:40:07 -07001454 int alpha;
1455 SkXfermode::Mode mode;
1456 getAlphaAndMode(paint, &alpha, &mode);
1457
Romain Guy746b7402010-10-26 16:27:31 -07001458 setupDraw();
Romain Guy86568192010-12-14 15:55:39 -08001459 setupDrawWithTexture(true);
1460 setupDrawAlpha8Color(paint->getColor(), alpha);
1461 setupDrawColorFilter();
1462 setupDrawShader();
1463 setupDrawBlending(true, mode);
1464 setupDrawProgram();
1465 setupDrawModelView(x, y, x + texture->width, y + texture->height);
1466 setupDrawTexture(texture->id);
1467 setupDrawPureColorUniforms();
1468 setupDrawColorFilterUniforms();
1469 setupDrawShaderUniforms();
1470 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
Romain Guy86942302010-09-12 13:02:16 -07001471
Romain Guy0a417492010-08-16 20:26:20 -07001472 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy86568192010-12-14 15:55:39 -08001473
1474 finishDrawTexture();
Romain Guy7fbcc042010-08-04 15:40:07 -07001475}
1476
Romain Guy6926c722010-07-12 20:20:03 -07001477///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001478// Shaders
1479///////////////////////////////////////////////////////////////////////////////
1480
1481void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001482 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001483}
1484
Romain Guy06f96e22010-07-30 19:18:16 -07001485void OpenGLRenderer::setupShader(SkiaShader* shader) {
1486 mShader = shader;
1487 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001488 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001489 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001490}
1491
Romain Guyd27977d2010-07-14 19:18:51 -07001492///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001493// Color filters
1494///////////////////////////////////////////////////////////////////////////////
1495
1496void OpenGLRenderer::resetColorFilter() {
1497 mColorFilter = NULL;
1498}
1499
1500void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1501 mColorFilter = filter;
1502}
1503
1504///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07001505// Drop shadow
1506///////////////////////////////////////////////////////////////////////////////
1507
1508void OpenGLRenderer::resetShadow() {
1509 mHasShadow = false;
1510}
1511
1512void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
1513 mHasShadow = true;
1514 mShadowRadius = radius;
1515 mShadowDx = dx;
1516 mShadowDy = dy;
1517 mShadowColor = color;
1518}
1519
1520///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001521// Drawing implementation
1522///////////////////////////////////////////////////////////////////////////////
1523
Romain Guyf607bdc2010-09-10 19:20:06 -07001524// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001525#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1526#define kStdUnderline_Offset (1.0f / 9.0f)
1527#define kStdUnderline_Thickness (1.0f / 18.0f)
1528
1529void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1530 float x, float y, SkPaint* paint) {
1531 // Handle underline and strike-through
1532 uint32_t flags = paint->getFlags();
1533 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1534 float underlineWidth = length;
1535 // If length is > 0.0f, we already measured the text for the text alignment
1536 if (length <= 0.0f) {
1537 underlineWidth = paint->measureText(text, bytesCount);
1538 }
1539
1540 float offsetX = 0;
1541 switch (paint->getTextAlign()) {
1542 case SkPaint::kCenter_Align:
1543 offsetX = underlineWidth * 0.5f;
1544 break;
1545 case SkPaint::kRight_Align:
1546 offsetX = underlineWidth;
1547 break;
1548 default:
1549 break;
1550 }
1551
1552 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001553 const float textSize = paint->getTextSize();
1554 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -07001555
Romain Guye20ecbd2010-09-22 19:49:04 -07001556 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001557 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001558
1559 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
1560 float points[pointsCount];
1561 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001562
1563 if (flags & SkPaint::kUnderlineText_Flag) {
1564 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001565 points[currentPoint++] = left;
1566 points[currentPoint++] = top;
1567 points[currentPoint++] = left + underlineWidth;
1568 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001569 }
1570
1571 if (flags & SkPaint::kStrikeThruText_Flag) {
1572 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001573 points[currentPoint++] = left;
1574 points[currentPoint++] = top;
1575 points[currentPoint++] = left + underlineWidth;
1576 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001577 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001578
1579 SkPaint linesPaint(*paint);
1580 linesPaint.setStrokeWidth(strokeWidth);
1581
1582 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001583 }
1584 }
1585}
1586
Romain Guy026c5e162010-06-28 17:12:22 -07001587void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001588 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07001589 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001590 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001591 color |= 0x00ffffff;
1592 }
1593
Romain Guy70ca14e2010-12-13 18:24:33 -08001594 setupDraw();
1595 setupDrawColor(color);
1596 setupDrawShader();
1597 setupDrawColorFilter();
1598 setupDrawBlending(mode);
1599 setupDrawProgram();
1600 setupDrawModelView(left, top, right, bottom, ignoreTransform);
1601 setupDrawColorUniforms();
1602 setupDrawShaderUniforms(ignoreTransform);
1603 setupDrawColorFilterUniforms();
1604 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07001605
Romain Guyc95c8d62010-09-17 15:31:32 -07001606 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1607}
1608
Romain Guy82ba8142010-07-09 13:25:56 -07001609void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001610 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001611 int alpha;
1612 SkXfermode::Mode mode;
1613 getAlphaAndMode(paint, &alpha, &mode);
1614
Romain Guy8164c2d2010-10-25 18:03:28 -07001615 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1616
Romain Guy6620c6d2010-12-06 18:07:02 -08001617 if (mSnapshot->transform->isPureTranslate()) {
1618 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1619 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1620
1621 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1622 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
1623 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
1624 } else {
1625 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1626 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
1627 GL_TRIANGLE_STRIP, gMeshCount);
1628 }
Romain Guy85bf02f2010-06-22 13:11:24 -07001629}
1630
Romain Guybd6b79b2010-06-26 00:13:53 -07001631void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001632 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1633 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001634 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001635}
1636
1637void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001638 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001639 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07001640 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001641
Romain Guy746b7402010-10-26 16:27:31 -07001642 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08001643 setupDrawWithTexture();
1644 setupDrawColor(alpha, alpha, alpha, alpha);
1645 setupDrawColorFilter();
1646 setupDrawBlending(blend, mode, swapSrcDst);
1647 setupDrawProgram();
1648 if (!dirty) {
1649 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07001650 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001651 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001652 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001653 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08001654 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001655 }
Romain Guy86568192010-12-14 15:55:39 -08001656 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08001657 setupDrawColorFilterUniforms();
1658 setupDrawTexture(texture);
1659 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07001660
Romain Guy6820ac82010-09-15 18:11:50 -07001661 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08001662
1663 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07001664}
1665
Romain Guya5aed0d2010-09-09 14:42:43 -07001666void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001667 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001668 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1669 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001670 if (mode < SkXfermode::kPlus_Mode) {
1671 if (!mCaches.blend) {
1672 glEnable(GL_BLEND);
1673 }
Romain Guy82ba8142010-07-09 13:25:56 -07001674
Romain Guyf607bdc2010-09-10 19:20:06 -07001675 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1676 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001677
Romain Guya5aed0d2010-09-09 14:42:43 -07001678 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1679 glBlendFunc(sourceMode, destMode);
1680 mCaches.lastSrcMode = sourceMode;
1681 mCaches.lastDstMode = destMode;
1682 }
1683 } else {
1684 // These blend modes are not supported by OpenGL directly and have
1685 // to be implemented using shaders. Since the shader will perform
1686 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001687 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001688 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001689 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001690 }
1691
1692 if (mCaches.blend) {
1693 glDisable(GL_BLEND);
1694 }
1695 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001696 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001697 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001698 glDisable(GL_BLEND);
1699 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001700 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001701}
1702
Romain Guy889f8d12010-07-29 14:37:42 -07001703bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001704 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001705 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001706 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001707 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001708 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001709 }
Romain Guy6926c722010-07-12 20:20:03 -07001710 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001711}
1712
Romain Guy026c5e162010-06-28 17:12:22 -07001713void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001714 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001715 TextureVertex::setUV(v++, u1, v1);
1716 TextureVertex::setUV(v++, u2, v1);
1717 TextureVertex::setUV(v++, u1, v2);
1718 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001719}
1720
Chet Haase5c13d892010-10-08 08:37:55 -07001721void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001722 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07001723 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001724 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1725 if (!isMode) {
1726 // Assume SRC_OVER
1727 *mode = SkXfermode::kSrcOver_Mode;
1728 }
1729 } else {
1730 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001731 }
1732
1733 // Skia draws using the color's alpha channel if < 255
1734 // Otherwise, it uses the paint's alpha
1735 int color = paint->getColor();
1736 *alpha = (color >> 24) & 0xFF;
1737 if (*alpha == 255) {
1738 *alpha = paint->getAlpha();
1739 }
1740 } else {
1741 *mode = SkXfermode::kSrcOver_Mode;
1742 *alpha = 255;
1743 }
Romain Guy026c5e162010-06-28 17:12:22 -07001744}
1745
Romain Guya5aed0d2010-09-09 14:42:43 -07001746SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1747 if (mode == NULL) {
1748 return SkXfermode::kSrcOver_Mode;
1749 }
1750 return mode->fMode;
1751}
1752
Romain Guy746b7402010-10-26 16:27:31 -07001753void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07001754 bool bound = false;
1755 if (wrapS != texture->wrapS) {
1756 glBindTexture(GL_TEXTURE_2D, texture->id);
1757 bound = true;
1758 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1759 texture->wrapS = wrapS;
1760 }
1761 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001762 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001763 glBindTexture(GL_TEXTURE_2D, texture->id);
1764 }
Romain Guy8164c2d2010-10-25 18:03:28 -07001765 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1766 texture->wrapT = wrapT;
1767 }
Romain Guya1db5742010-07-20 13:09:13 -07001768}
1769
Romain Guy9d5316e2010-06-24 19:30:36 -07001770}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001771}; // namespace android