blob: 7cf70f7e5eee2a74125bbb1aca19772d28672eba [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>
27
Romain Guy85bf02f2010-06-22 13:11:24 -070028#include "OpenGLRenderer.h"
Romain Guye4d01122010-06-16 18:44:05 -070029
30namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070031namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Defines
35///////////////////////////////////////////////////////////////////////////////
36
Romain Guy06f96e22010-07-30 19:18:16 -070037#define REQUIRED_TEXTURE_UNITS_COUNT 3
38
Romain Guydda57022010-07-06 11:39:32 -070039// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070040#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070041
42///////////////////////////////////////////////////////////////////////////////
43// Globals
44///////////////////////////////////////////////////////////////////////////////
45
Romain Guy026c5e162010-06-28 17:12:22 -070046// This array is never used directly but used as a memcpy source in the
47// OpenGLRenderer constructor
Romain Guyac670c02010-07-27 17:39:27 -070048static const TextureVertex gMeshVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070049 FV(0.0f, 0.0f, 0.0f, 0.0f),
50 FV(1.0f, 0.0f, 1.0f, 0.0f),
51 FV(0.0f, 1.0f, 0.0f, 1.0f),
52 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070053};
Romain Guyac670c02010-07-27 17:39:27 -070054static const GLsizei gMeshStride = sizeof(TextureVertex);
55static const GLsizei gMeshCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070056
Romain Guy889f8d12010-07-29 14:37:42 -070057/**
58 * Structure mapping Skia xfermodes to OpenGL blending factors.
59 */
60struct Blender {
61 SkXfermode::Mode mode;
62 GLenum src;
63 GLenum dst;
64}; // struct Blender
65
Romain Guy026c5e162010-06-28 17:12:22 -070066// In this array, the index of each Blender equals the value of the first
67// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
68static const Blender gBlends[] = {
69 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
70 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
71 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
72 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
73 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
74 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
75 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
76 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
80 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
81};
Romain Guye4d01122010-06-16 18:44:05 -070082
Romain Guyf607bdc2010-09-10 19:20:06 -070083static const Blender gBlendsSwap[] = {
84 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
85 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
86 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
87 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
88 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
89 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
90 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
92 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
93 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
94 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
96};
97
Romain Guy889f8d12010-07-29 14:37:42 -070098static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -070099 GL_TEXTURE0,
100 GL_TEXTURE1,
101 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700102};
103
Romain Guyf6a11b82010-06-23 17:47:49 -0700104///////////////////////////////////////////////////////////////////////////////
105// Constructors/destructor
106///////////////////////////////////////////////////////////////////////////////
107
Romain Guyfb8b7632010-08-23 21:05:08 -0700108OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy85bf02f2010-06-22 13:11:24 -0700109 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700110
Romain Guy06f96e22010-07-30 19:18:16 -0700111 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700112 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700113 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700114
Romain Guyac670c02010-07-27 17:39:27 -0700115 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
116
Romain Guyae5575b2010-07-29 18:48:04 -0700117 mFirstSnapshot = new Snapshot;
118
119 GLint maxTextureUnits;
120 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
121 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Romain Guy889f8d12010-07-29 14:37:42 -0700122 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
123 }
Romain Guye4d01122010-06-16 18:44:05 -0700124}
125
Romain Guy85bf02f2010-06-22 13:11:24 -0700126OpenGLRenderer::~OpenGLRenderer() {
127 LOGD("Destroy OpenGLRenderer");
Romain Guye4d01122010-06-16 18:44:05 -0700128}
129
Romain Guyf6a11b82010-06-23 17:47:49 -0700130///////////////////////////////////////////////////////////////////////////////
131// Setup
132///////////////////////////////////////////////////////////////////////////////
133
Romain Guy85bf02f2010-06-22 13:11:24 -0700134void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700135 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700136 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700137
138 mWidth = width;
139 mHeight = height;
Romain Guye4d01122010-06-16 18:44:05 -0700140}
141
Romain Guy85bf02f2010-06-22 13:11:24 -0700142void OpenGLRenderer::prepare() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700143 mSnapshot = new Snapshot(mFirstSnapshot,
144 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy8fb95422010-08-17 18:38:51 -0700145 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700146
Romain Guyfb8b7632010-08-23 21:05:08 -0700147 glViewport(0, 0, mWidth, mHeight);
148
Romain Guyd90f23e2010-09-09 11:47:54 -0700149 glDisable(GL_DITHER);
Romain Guy08ae3172010-06-21 19:35:50 -0700150 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700151
Romain Guy08ae3172010-06-21 19:35:50 -0700152 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
153 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700154
Romain Guy08ae3172010-06-21 19:35:50 -0700155 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700156 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700157
Romain Guyb82da652010-07-30 11:36:12 -0700158 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700159}
160
Romain Guyda8532c2010-08-31 11:50:35 -0700161void OpenGLRenderer::acquireContext() {
162 if (mCaches.currentProgram) {
163 if (mCaches.currentProgram->isInUse()) {
164 mCaches.currentProgram->remove();
165 mCaches.currentProgram = NULL;
166 }
167 }
168}
169
170void OpenGLRenderer::releaseContext() {
Romain Guyf607bdc2010-09-10 19:20:06 -0700171 glViewport(0, 0, mWidth, mHeight);
Romain Guyda8532c2010-08-31 11:50:35 -0700172
173 glEnable(GL_SCISSOR_TEST);
174 setScissorFromClip();
175
Romain Guyf607bdc2010-09-10 19:20:06 -0700176 glDisable(GL_DITHER);
177
178 glBindFramebuffer(GL_FRAMEBUFFER, 0);
179
Romain Guyda8532c2010-08-31 11:50:35 -0700180 if (mCaches.blend) {
181 glEnable(GL_BLEND);
182 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
Romain Guyf607bdc2010-09-10 19:20:06 -0700183 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700184 } else {
185 glDisable(GL_BLEND);
186 }
187}
188
Romain Guyf6a11b82010-06-23 17:47:49 -0700189///////////////////////////////////////////////////////////////////////////////
190// State management
191///////////////////////////////////////////////////////////////////////////////
192
Romain Guybb9524b2010-06-22 18:56:38 -0700193int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700194 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700195}
196
197int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700198 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700199}
200
201void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700202 if (mSaveCount > 1) {
203 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700204 }
Romain Guybb9524b2010-06-22 18:56:38 -0700205}
206
207void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700208 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700209
Romain Guy8fb95422010-08-17 18:38:51 -0700210 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700211 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700212 }
Romain Guybb9524b2010-06-22 18:56:38 -0700213}
214
Romain Guy8aef54f2010-09-01 15:13:49 -0700215int OpenGLRenderer::saveSnapshot(int flags) {
216 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700217 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700218}
219
220bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700221 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700222 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guybb9524b2010-06-22 18:56:38 -0700223
Romain Guybd6b79b2010-06-26 00:13:53 -0700224 sp<Snapshot> current = mSnapshot;
225 sp<Snapshot> previous = mSnapshot->previous;
226
Romain Guy8b55f372010-08-18 17:10:07 -0700227 mSaveCount--;
228 mSnapshot = previous;
229
Romain Guybd6b79b2010-06-26 00:13:53 -0700230 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700231 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700232 }
233
Romain Guy2542d192010-08-18 11:47:12 -0700234 if (restoreClip) {
235 setScissorFromClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700236 }
Romain Guy2542d192010-08-18 11:47:12 -0700237
238 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700239}
240
Romain Guyf6a11b82010-06-23 17:47:49 -0700241///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700242// Layers
243///////////////////////////////////////////////////////////////////////////////
244
245int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
246 const SkPaint* p, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700247 int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700248
249 int alpha = 255;
250 SkXfermode::Mode mode;
251
252 if (p) {
253 alpha = p->getAlpha();
Romain Guya5aed0d2010-09-09 14:42:43 -0700254 if (!mExtensions.hasFramebufferFetch()) {
255 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
256 if (!isMode) {
257 // Assume SRC_OVER
258 mode = SkXfermode::kSrcOver_Mode;
259 }
260 } else {
261 mode = getXfermode(p->getXfermode());
Romain Guyd55a8612010-06-28 17:42:46 -0700262 }
263 } else {
264 mode = SkXfermode::kSrcOver_Mode;
265 }
266
Romain Guyf607bdc2010-09-10 19:20:06 -0700267 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700268
269 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700270}
271
272int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
273 int alpha, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700274 if (alpha == 0xff) {
275 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700276 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700277 SkPaint paint;
278 paint.setAlpha(alpha);
279 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700280 }
Romain Guyd55a8612010-06-28 17:42:46 -0700281}
Romain Guybd6b79b2010-06-26 00:13:53 -0700282
Romain Guyd55a8612010-06-28 17:42:46 -0700283bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
284 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700285 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700286 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700287
Romain Guyf607bdc2010-09-10 19:20:06 -0700288 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700289 Rect bounds(left, top, right, bottom);
Romain Guyf607bdc2010-09-10 19:20:06 -0700290 mSnapshot->transform->mapRect(bounds);
Romain Guy8aef54f2010-09-01 15:13:49 -0700291
Romain Guy8aef54f2010-09-01 15:13:49 -0700292 LayerSize size(bounds.getWidth(), bounds.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700293
Romain Guyf607bdc2010-09-10 19:20:06 -0700294 Layer* layer = mCaches.layerCache.get(size);
Romain Guydda57022010-07-06 11:39:32 -0700295 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700296 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700297 }
298
Romain Guydda57022010-07-06 11:39:32 -0700299 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700300 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700301 layer->layer.set(bounds);
Romain Guydda57022010-07-06 11:39:32 -0700302
Romain Guy8fb95422010-08-17 18:38:51 -0700303 // Save the layer in the snapshot
304 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700305 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700306
Romain Guyf607bdc2010-09-10 19:20:06 -0700307 // Copy the framebuffer into the layer
308 glBindTexture(GL_TEXTURE_2D, layer->texture);
309 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
310 bounds.getWidth(), bounds.getHeight(), 0);
311
312 // Clear the framebuffer where the layer will draw
313 glScissor(bounds.left, mHeight - bounds.bottom, bounds.getWidth(), bounds.getHeight());
314 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
315 glClear(GL_COLOR_BUFFER_BIT);
316
317 if (flags & SkCanvas::kClipToLayer_SaveFlag) {
318 mSnapshot->clipTransformed(bounds);
319 }
320
321 // Restore the initial clip
Romain Guyf86ef572010-07-01 11:05:42 -0700322 setScissorFromClip();
323
Romain Guyd55a8612010-06-28 17:42:46 -0700324 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700325}
326
Romain Guy1d83e192010-08-17 11:37:00 -0700327void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
328 if (!current->layer) {
329 LOGE("Attempting to compose a layer that does not exist");
330 return;
331 }
332
Romain Guy1d83e192010-08-17 11:37:00 -0700333 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700334 const Rect& clip = *previous->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700335 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700336
337 Layer* layer = current->layer;
338 const Rect& rect = layer->layer;
339
Romain Guyf607bdc2010-09-10 19:20:06 -0700340 if (layer->alpha < 255) {
341 glEnable(GL_BLEND);
342 glBlendFuncSeparate(GL_ZERO, GL_SRC_ALPHA, GL_DST_ALPHA, GL_ZERO);
343
344 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
345 layer->alpha << 24, SkXfermode::kSrcOver_Mode, true, true);
346
347 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
348 if (!mCaches.blend) {
349 glDisable(GL_BLEND);
350 }
351 }
352
353 // Layers are already drawn with a top-left origin, don't flip the texture
Romain Guy8b55f372010-08-18 17:10:07 -0700354 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
355
Romain Guyf607bdc2010-09-10 19:20:06 -0700356 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
357 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
358 &mMeshVertices[0].texture[0], NULL, 0, true, true);
Romain Guy1d83e192010-08-17 11:37:00 -0700359
Romain Guy8b55f372010-08-18 17:10:07 -0700360 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
361
Romain Guy1d83e192010-08-17 11:37:00 -0700362 LayerSize size(rect.getWidth(), rect.getHeight());
363 // Failing to add the layer to the cache should happen only if the
364 // layer is too large
Romain Guyfb8b7632010-08-23 21:05:08 -0700365 if (!mCaches.layerCache.put(size, layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700366 LAYER_LOGD("Deleting layer");
367
Romain Guy1d83e192010-08-17 11:37:00 -0700368 glDeleteTextures(1, &layer->texture);
369
370 delete layer;
371 }
372}
373
Romain Guybd6b79b2010-06-26 00:13:53 -0700374///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700375// Transforms
376///////////////////////////////////////////////////////////////////////////////
377
378void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700379 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700380}
381
382void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700383 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700384}
385
386void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700387 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700388}
389
390void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700391 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700392}
393
394void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700395 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700396}
397
398void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700399 mat4 m(*matrix);
Romain Guy8aef54f2010-09-01 15:13:49 -0700400 mSnapshot->transform->multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700401}
402
403///////////////////////////////////////////////////////////////////////////////
404// Clipping
405///////////////////////////////////////////////////////////////////////////////
406
Romain Guybb9524b2010-06-22 18:56:38 -0700407void OpenGLRenderer::setScissorFromClip() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700408 const Rect& clip = *mSnapshot->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700409 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700410}
411
412const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700413 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700414}
415
Romain Guyc7d53492010-06-25 13:41:57 -0700416bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy1d83e192010-08-17 11:37:00 -0700417 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700418 mSnapshot->transform->mapRect(r);
419 return !mSnapshot->clipRect->intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700420}
421
Romain Guy079ba2c2010-07-16 14:12:24 -0700422bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
423 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700424 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700425 setScissorFromClip();
426 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700427 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700428}
429
Romain Guyf6a11b82010-06-23 17:47:49 -0700430///////////////////////////////////////////////////////////////////////////////
431// Drawing
432///////////////////////////////////////////////////////////////////////////////
433
Romain Guyc1396e92010-06-30 17:56:19 -0700434void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700435 const float right = left + bitmap->width();
436 const float bottom = top + bitmap->height();
437
438 if (quickReject(left, top, right, bottom)) {
439 return;
440 }
441
Romain Guy61c8c9c2010-08-09 20:48:09 -0700442 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700443 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700444 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700445 const AutoTexture autoCleanup(texture);
446
Romain Guy6926c722010-07-12 20:20:03 -0700447 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700448}
449
Romain Guyf86ef572010-07-01 11:05:42 -0700450void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
451 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
452 const mat4 transform(*matrix);
453 transform.mapRect(r);
454
Romain Guy6926c722010-07-12 20:20:03 -0700455 if (quickReject(r.left, r.top, r.right, r.bottom)) {
456 return;
457 }
458
Romain Guy61c8c9c2010-08-09 20:48:09 -0700459 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700460 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700461 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700462 const AutoTexture autoCleanup(texture);
463
Romain Guy82ba8142010-07-09 13:25:56 -0700464 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700465}
466
Romain Guy8ba548f2010-06-30 19:21:21 -0700467void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
468 float srcLeft, float srcTop, float srcRight, float srcBottom,
469 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700470 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700471 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
472 return;
473 }
474
Romain Guy61c8c9c2010-08-09 20:48:09 -0700475 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700476 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700477 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700478 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700479
Romain Guy8ba548f2010-06-30 19:21:21 -0700480 const float width = texture->width;
481 const float height = texture->height;
482
483 const float u1 = srcLeft / width;
484 const float v1 = srcTop / height;
485 const float u2 = srcRight / width;
486 const float v2 = srcBottom / height;
487
488 resetDrawTextureTexCoords(u1, v1, u2, v2);
489
Romain Guy82ba8142010-07-09 13:25:56 -0700490 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700491
492 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
493}
494
Romain Guydeba7852010-07-07 17:54:48 -0700495void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
496 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700497 if (quickReject(left, top, right, bottom)) {
498 return;
499 }
500
Romain Guy61c8c9c2010-08-09 20:48:09 -0700501 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700502 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700503 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700504 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700505
506 int alpha;
507 SkXfermode::Mode mode;
508 getAlphaAndMode(paint, &alpha, &mode);
509
Romain Guyfb8b7632010-08-23 21:05:08 -0700510 Patch* mesh = mCaches.patchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700511 mesh->updateVertices(bitmap, left, top, right, bottom,
512 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700513
514 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
515 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700516 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
517 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700518 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700519}
520
Romain Guy85bf02f2010-06-22 13:11:24 -0700521void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700522 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700523 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700524}
Romain Guy9d5316e2010-06-24 19:30:36 -0700525
Romain Guybd6b79b2010-06-26 00:13:53 -0700526void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700527 if (quickReject(left, top, right, bottom)) {
528 return;
529 }
530
Romain Guy026c5e162010-06-28 17:12:22 -0700531 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700532 if (!mExtensions.hasFramebufferFetch()) {
533 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
534 if (!isMode) {
535 // Assume SRC_OVER
536 mode = SkXfermode::kSrcOver_Mode;
537 }
538 } else {
539 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700540 }
541
542 // Skia draws using the color's alpha channel if < 255
543 // Otherwise, it uses the paint's alpha
544 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700545 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700546 color |= p->getAlpha() << 24;
547 }
548
549 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700550}
Romain Guy9d5316e2010-06-24 19:30:36 -0700551
Romain Guye8e62a42010-07-23 18:55:21 -0700552void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
553 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700554 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700555 return;
556 }
Romain Guyf607bdc2010-09-10 19:20:06 -0700557 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700558
Romain Guya80d32f2010-08-20 18:42:37 -0700559 float scaleX = paint->getTextScaleX();
560 bool applyScaleX = scaleX < 0.9999f || scaleX > 1.0001f;
561 if (applyScaleX) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700562 save(SkCanvas::kMatrix_SaveFlag);
Romain Guya80d32f2010-08-20 18:42:37 -0700563 translate(x - (x * scaleX), 0.0f);
564 scale(scaleX, 1.0f);
565 }
566
Romain Guya674ab72010-08-10 17:26:42 -0700567 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700568 switch (paint->getTextAlign()) {
569 case SkPaint::kCenter_Align:
570 length = paint->measureText(text, bytesCount);
571 x -= length / 2.0f;
572 break;
573 case SkPaint::kRight_Align:
574 length = paint->measureText(text, bytesCount);
575 x -= length;
576 break;
577 default:
578 break;
579 }
580
Romain Guy694b5192010-07-21 21:33:20 -0700581 int alpha;
582 SkXfermode::Mode mode;
583 getAlphaAndMode(paint, &alpha, &mode);
584
Romain Guy2542d192010-08-18 11:47:12 -0700585 uint32_t color = paint->getColor();
586 const GLfloat a = alpha / 255.0f;
587 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
588 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
589 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
590
Romain Guyb45c0c92010-08-26 20:35:23 -0700591 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
592 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700593 paint->getTextSize());
Romain Guy1e45aae2010-08-13 19:39:53 -0700594 if (mHasShadow) {
595 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700596 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700597 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700598 count, mShadowRadius);
599 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700600
Romain Guy2542d192010-08-18 11:47:12 -0700601 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700602
603 // Draw the mesh
604 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700605 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700606 }
607
Romain Guy06f96e22010-07-30 19:18:16 -0700608 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700609 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700610
Romain Guyb45c0c92010-08-26 20:35:23 -0700611 setupTextureAlpha8(fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
Romain Guy0a417492010-08-16 20:26:20 -0700612 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700613
Romain Guy09147fb2010-07-22 13:08:20 -0700614 const Rect& clip = mSnapshot->getLocalClip();
Romain Guyb45c0c92010-08-26 20:35:23 -0700615 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700616
617 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700618 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700619
Romain Guy0a417492010-08-16 20:26:20 -0700620 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guya80d32f2010-08-20 18:42:37 -0700621
622 if (applyScaleX) {
623 restore();
624 }
Romain Guy694b5192010-07-21 21:33:20 -0700625}
626
Romain Guy7fbcc042010-08-04 15:40:07 -0700627void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
628 GLuint textureUnit = 0;
629 glActiveTexture(gTextureUnits[textureUnit]);
630
Romain Guyfb8b7632010-08-23 21:05:08 -0700631 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700632 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700633 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700634
Romain Guy8b55f372010-08-18 17:10:07 -0700635 const float x = texture->left - texture->offset;
636 const float y = texture->top - texture->offset;
637
638 if (quickReject(x, y, x + texture->width, y + texture->height)) {
639 return;
640 }
641
Romain Guy7fbcc042010-08-04 15:40:07 -0700642 int alpha;
643 SkXfermode::Mode mode;
644 getAlphaAndMode(paint, &alpha, &mode);
645
646 uint32_t color = paint->getColor();
647 const GLfloat a = alpha / 255.0f;
648 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
649 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
650 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
651
Romain Guy0a417492010-08-16 20:26:20 -0700652 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
653
654 // Draw the mesh
655 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700656 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700657}
658
Romain Guy6926c722010-07-12 20:20:03 -0700659///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700660// Shaders
661///////////////////////////////////////////////////////////////////////////////
662
663void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700664 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700665}
666
Romain Guy06f96e22010-07-30 19:18:16 -0700667void OpenGLRenderer::setupShader(SkiaShader* shader) {
668 mShader = shader;
669 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700670 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700671 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700672}
673
Romain Guyd27977d2010-07-14 19:18:51 -0700674///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700675// Color filters
676///////////////////////////////////////////////////////////////////////////////
677
678void OpenGLRenderer::resetColorFilter() {
679 mColorFilter = NULL;
680}
681
682void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
683 mColorFilter = filter;
684}
685
686///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700687// Drop shadow
688///////////////////////////////////////////////////////////////////////////////
689
690void OpenGLRenderer::resetShadow() {
691 mHasShadow = false;
692}
693
694void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
695 mHasShadow = true;
696 mShadowRadius = radius;
697 mShadowDx = dx;
698 mShadowDy = dy;
699 mShadowColor = color;
700}
701
702///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700703// Drawing implementation
704///////////////////////////////////////////////////////////////////////////////
705
Romain Guy0a417492010-08-16 20:26:20 -0700706void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700707 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700708 const float sx = x - texture->left + mShadowDx;
709 const float sy = y - texture->top + mShadowDy;
710
Romain Guy2542d192010-08-18 11:47:12 -0700711 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
712 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700713 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
714 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
715 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
716
717 GLuint textureUnit = 0;
718 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
719}
720
721void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
722 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
723 bool transforms, bool applyFilters) {
724 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
725 x, y, r, g, b, a, mode, transforms, applyFilters);
726}
727
728void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
729 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
730 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
731 // Describe the required shaders
732 ProgramDescription description;
733 description.hasTexture = true;
734 description.hasAlpha8Texture = true;
735
736 if (applyFilters) {
737 if (mShader) {
738 mShader->describe(description, mExtensions);
739 }
740 if (mColorFilter) {
741 mColorFilter->describe(description, mExtensions);
742 }
743 }
744
Romain Guya5aed0d2010-09-09 14:42:43 -0700745 // Setup the blending mode
746 chooseBlending(true, mode, description);
747
Romain Guy0a417492010-08-16 20:26:20 -0700748 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700749 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700750
Romain Guy0a417492010-08-16 20:26:20 -0700751 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -0700752 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700753
Romain Guyfb8b7632010-08-23 21:05:08 -0700754 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -0700755 glEnableVertexAttribArray(texCoordsSlot);
756
757 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700758 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy0a417492010-08-16 20:26:20 -0700759 gMeshStride, &mMeshVertices[0].position[0]);
760 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
761 gMeshStride, &mMeshVertices[0].texture[0]);
762
763 // Setup uniforms
764 if (transforms) {
765 mModelView.loadTranslate(x, y, 0.0f);
766 mModelView.scale(width, height, 1.0f);
767 } else {
768 mModelView.loadIdentity();
769 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700770 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyfb8b7632010-08-23 21:05:08 -0700771 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -0700772
773 textureUnit++;
774 if (applyFilters) {
775 // Setup attributes and uniforms required by the shaders
776 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700777 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700778 }
779 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700780 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -0700781 }
782 }
783}
784
Romain Guyf607bdc2010-09-10 19:20:06 -0700785// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -0700786#define kStdStrikeThru_Offset (-6.0f / 21.0f)
787#define kStdUnderline_Offset (1.0f / 9.0f)
788#define kStdUnderline_Thickness (1.0f / 18.0f)
789
790void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
791 float x, float y, SkPaint* paint) {
792 // Handle underline and strike-through
793 uint32_t flags = paint->getFlags();
794 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
795 float underlineWidth = length;
796 // If length is > 0.0f, we already measured the text for the text alignment
797 if (length <= 0.0f) {
798 underlineWidth = paint->measureText(text, bytesCount);
799 }
800
801 float offsetX = 0;
802 switch (paint->getTextAlign()) {
803 case SkPaint::kCenter_Align:
804 offsetX = underlineWidth * 0.5f;
805 break;
806 case SkPaint::kRight_Align:
807 offsetX = underlineWidth;
808 break;
809 default:
810 break;
811 }
812
813 if (underlineWidth > 0.0f) {
814 float textSize = paint->getTextSize();
815 float height = textSize * kStdUnderline_Thickness;
816
817 float left = x - offsetX;
818 float top = 0.0f;
819 float right = left + underlineWidth;
820 float bottom = 0.0f;
821
822 if (flags & SkPaint::kUnderlineText_Flag) {
823 top = y + textSize * kStdUnderline_Offset;
824 bottom = top + height;
825 drawRect(left, top, right, bottom, paint);
826 }
827
828 if (flags & SkPaint::kStrikeThruText_Flag) {
829 top = y + textSize * kStdStrikeThru_Offset;
830 bottom = top + height;
831 drawRect(left, top, right, bottom, paint);
832 }
833 }
834 }
835}
836
Romain Guy026c5e162010-06-28 17:12:22 -0700837void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guyf607bdc2010-09-10 19:20:06 -0700838 int color, SkXfermode::Mode mode, bool ignoreTransform, bool ignoreBlending) {
Romain Guyd27977d2010-07-14 19:18:51 -0700839 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700840 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700841 color |= 0x00ffffff;
842 }
843
844 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700845 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700846 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700847 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
848 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
849 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
850
Romain Guy06f96e22010-07-30 19:18:16 -0700851 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700852
Romain Guy06f96e22010-07-30 19:18:16 -0700853 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700854 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700855 if (mShader) {
856 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700857 }
Romain Guydb1938e2010-08-02 18:50:22 -0700858 if (mColorFilter) {
859 mColorFilter->describe(description, mExtensions);
860 }
Romain Guyd27977d2010-07-14 19:18:51 -0700861
Romain Guyf607bdc2010-09-10 19:20:06 -0700862 if (!ignoreBlending) {
863 // Setup the blending mode
864 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode, description);
865 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700866
Romain Guy06f96e22010-07-30 19:18:16 -0700867 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700868 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -0700869
870 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700871 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -0700872 gMeshStride, &mMeshVertices[0].position[0]);
873
874 // Setup uniforms
875 mModelView.loadTranslate(left, top, 0.0f);
876 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700877 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700878 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700879 } else {
880 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -0700881 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700882 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700883 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700884
Romain Guy06f96e22010-07-30 19:18:16 -0700885 // Setup attributes and uniforms required by the shaders
886 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700887 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700888 }
Romain Guydb1938e2010-08-02 18:50:22 -0700889 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700890 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700891 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700892
Romain Guy06f96e22010-07-30 19:18:16 -0700893 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700894 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700895}
896
Romain Guy82ba8142010-07-09 13:25:56 -0700897void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700898 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700899 int alpha;
900 SkXfermode::Mode mode;
901 getAlphaAndMode(paint, &alpha, &mode);
902
Romain Guya9794742010-07-13 11:37:54 -0700903 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700904 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700905}
906
Romain Guybd6b79b2010-06-26 00:13:53 -0700907void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700908 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
909 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700910 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700911}
912
913void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700914 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf607bdc2010-09-10 19:20:06 -0700915 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount,
916 bool swapSrcDst, bool ignoreTransform) {
Romain Guy889f8d12010-07-29 14:37:42 -0700917 ProgramDescription description;
918 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700919 if (mColorFilter) {
920 mColorFilter->describe(description, mExtensions);
921 }
Romain Guy889f8d12010-07-29 14:37:42 -0700922
Romain Guybd6b79b2010-06-26 00:13:53 -0700923 mModelView.loadTranslate(left, top, 0.0f);
924 mModelView.scale(right - left, bottom - top, 1.0f);
925
Romain Guyf607bdc2010-09-10 19:20:06 -0700926 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -0700927
Romain Guyfb8b7632010-08-23 21:05:08 -0700928 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -0700929 if (!ignoreTransform) {
930 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
931 } else {
932 mat4 m;
933 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
934 }
Romain Guybd6b79b2010-06-26 00:13:53 -0700935
Romain Guy889f8d12010-07-29 14:37:42 -0700936 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700937 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700938 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700939
Romain Guya9794742010-07-13 11:37:54 -0700940 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -0700941 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700942
Romain Guy889f8d12010-07-29 14:37:42 -0700943 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -0700944 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -0700945 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -0700946 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700947 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700948 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700949
Romain Guydb1938e2010-08-02 18:50:22 -0700950 // Color filter
951 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700952 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700953 }
954
Romain Guyf7f93552010-07-08 19:17:03 -0700955 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700956 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700957 } else {
958 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
959 }
Romain Guy889f8d12010-07-29 14:37:42 -0700960 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700961}
962
Romain Guya5aed0d2010-09-09 14:42:43 -0700963void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -0700964 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -0700965 blend = blend || mode != SkXfermode::kSrcOver_Mode;
966 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700967 if (mode < SkXfermode::kPlus_Mode) {
968 if (!mCaches.blend) {
969 glEnable(GL_BLEND);
970 }
Romain Guy82ba8142010-07-09 13:25:56 -0700971
Romain Guyf607bdc2010-09-10 19:20:06 -0700972 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
973 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -0700974
Romain Guya5aed0d2010-09-09 14:42:43 -0700975 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
976 glBlendFunc(sourceMode, destMode);
977 mCaches.lastSrcMode = sourceMode;
978 mCaches.lastDstMode = destMode;
979 }
980 } else {
981 // These blend modes are not supported by OpenGL directly and have
982 // to be implemented using shaders. Since the shader will perform
983 // the blending, turn blending off here
984 if (mExtensions.hasFramebufferFetch()) {
985 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700986 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -0700987 }
988
989 if (mCaches.blend) {
990 glDisable(GL_BLEND);
991 }
992 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -0700993 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700994 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -0700995 glDisable(GL_BLEND);
996 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700997 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700998}
999
Romain Guy889f8d12010-07-29 14:37:42 -07001000bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001001 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001002 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001003 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001004 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001005 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001006 }
Romain Guy6926c722010-07-12 20:20:03 -07001007 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001008}
1009
Romain Guy026c5e162010-06-28 17:12:22 -07001010void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001011 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001012 TextureVertex::setUV(v++, u1, v1);
1013 TextureVertex::setUV(v++, u2, v1);
1014 TextureVertex::setUV(v++, u1, v2);
1015 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001016}
1017
1018void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1019 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001020 if (!mExtensions.hasFramebufferFetch()) {
1021 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1022 if (!isMode) {
1023 // Assume SRC_OVER
1024 *mode = SkXfermode::kSrcOver_Mode;
1025 }
1026 } else {
1027 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001028 }
1029
1030 // Skia draws using the color's alpha channel if < 255
1031 // Otherwise, it uses the paint's alpha
1032 int color = paint->getColor();
1033 *alpha = (color >> 24) & 0xFF;
1034 if (*alpha == 255) {
1035 *alpha = paint->getAlpha();
1036 }
1037 } else {
1038 *mode = SkXfermode::kSrcOver_Mode;
1039 *alpha = 255;
1040 }
Romain Guy026c5e162010-06-28 17:12:22 -07001041}
1042
Romain Guya5aed0d2010-09-09 14:42:43 -07001043SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1044 if (mode == NULL) {
1045 return SkXfermode::kSrcOver_Mode;
1046 }
1047 return mode->fMode;
1048}
1049
Romain Guy889f8d12010-07-29 14:37:42 -07001050void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1051 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001052 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001053 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1054 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1055}
1056
Romain Guy9d5316e2010-06-24 19:30:36 -07001057}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001058}; // namespace android