blob: ca4babefedfad48d3f684988219fed117af9e563 [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 Guydda570202010-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 Guy81ab0462010-09-13 17:32:34 -0700283
Romain Guyd55a8612010-06-28 17:42:46 -0700284bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
285 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700286 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700287 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700288
Romain Guyf607bdc2010-09-10 19:20:06 -0700289 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700290 Rect bounds(left, top, right, bottom);
Romain Guyf607bdc2010-09-10 19:20:06 -0700291 mSnapshot->transform->mapRect(bounds);
Romain Guy8aef54f2010-09-01 15:13:49 -0700292
Romain Guy8411f332010-09-13 17:27:57 -0700293 // Layers only make sense if they are in the framebuffer's bounds
294 bounds.intersect(*mSnapshot->clipRect);
Romain Guy81ab0462010-09-13 17:32:34 -0700295 if (bounds.isEmpty()) return false;
Romain Guyf18fd992010-07-08 11:45:51 -0700296
Romain Guy8411f332010-09-13 17:27:57 -0700297 LayerSize size(bounds.getWidth(), bounds.getHeight());
Romain Guyf607bdc2010-09-10 19:20:06 -0700298 Layer* layer = mCaches.layerCache.get(size);
Romain Guydda570202010-07-06 11:39:32 -0700299 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700300 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700301 }
302
Romain Guydda570202010-07-06 11:39:32 -0700303 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700304 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700305 layer->layer.set(bounds);
Romain Guydda570202010-07-06 11:39:32 -0700306
Romain Guy8fb95422010-08-17 18:38:51 -0700307 // Save the layer in the snapshot
308 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700309 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700310
Romain Guyf607bdc2010-09-10 19:20:06 -0700311 // Copy the framebuffer into the layer
312 glBindTexture(GL_TEXTURE_2D, layer->texture);
313 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
314 bounds.getWidth(), bounds.getHeight(), 0);
315
Romain Guyf607bdc2010-09-10 19:20:06 -0700316 if (flags & SkCanvas::kClipToLayer_SaveFlag) {
Romain Guy86942302010-09-12 13:02:16 -0700317 if (mSnapshot->clipTransformed(bounds)) setScissorFromClip();
Romain Guyf607bdc2010-09-10 19:20:06 -0700318 }
319
Romain Guy86942302010-09-12 13:02:16 -0700320 // Enqueue the buffer coordinates to clear the corresponding region later
321 mLayers.push(new Rect(bounds));
Romain Guyf86ef572010-07-01 11:05:42 -0700322
Romain Guyd55a8612010-06-28 17:42:46 -0700323 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700324}
325
Romain Guy1d83e192010-08-17 11:37:00 -0700326void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
327 if (!current->layer) {
328 LOGE("Attempting to compose a layer that does not exist");
329 return;
330 }
331
Romain Guy1d83e192010-08-17 11:37:00 -0700332 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700333 const Rect& clip = *previous->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700334 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700335
336 Layer* layer = current->layer;
337 const Rect& rect = layer->layer;
338
Romain Guyf607bdc2010-09-10 19:20:06 -0700339 if (layer->alpha < 255) {
340 glEnable(GL_BLEND);
341 glBlendFuncSeparate(GL_ZERO, GL_SRC_ALPHA, GL_DST_ALPHA, GL_ZERO);
342
343 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
344 layer->alpha << 24, SkXfermode::kSrcOver_Mode, true, true);
345
346 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
347 if (!mCaches.blend) {
348 glDisable(GL_BLEND);
349 }
350 }
351
352 // Layers are already drawn with a top-left origin, don't flip the texture
Romain Guy8b55f372010-08-18 17:10:07 -0700353 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
354
Romain Guyf607bdc2010-09-10 19:20:06 -0700355 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
356 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
357 &mMeshVertices[0].texture[0], NULL, 0, true, true);
Romain Guy1d83e192010-08-17 11:37:00 -0700358
Romain Guy8b55f372010-08-18 17:10:07 -0700359 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
360
Romain Guy1d83e192010-08-17 11:37:00 -0700361 LayerSize size(rect.getWidth(), rect.getHeight());
362 // Failing to add the layer to the cache should happen only if the
363 // layer is too large
Romain Guyfb8b7632010-08-23 21:05:08 -0700364 if (!mCaches.layerCache.put(size, layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700365 LAYER_LOGD("Deleting layer");
366
Romain Guy1d83e192010-08-17 11:37:00 -0700367 glDeleteTextures(1, &layer->texture);
368
369 delete layer;
370 }
371}
372
Romain Guy86942302010-09-12 13:02:16 -0700373void OpenGLRenderer::clearLayerRegions() {
374 if (mLayers.size() == 0) return;
375
376 for (uint32_t i = 0; i < mLayers.size(); i++) {
377 Rect* bounds = mLayers.itemAt(i);
378
379 // Clear the framebuffer where the layer will draw
380 glScissor(bounds->left, mHeight - bounds->bottom,
381 bounds->getWidth(), bounds->getHeight());
382 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
383 glClear(GL_COLOR_BUFFER_BIT);
384
385 delete bounds;
386 }
387 mLayers.clear();
388
389 // Restore the clip
390 setScissorFromClip();
391}
392
Romain Guybd6b79b2010-06-26 00:13:53 -0700393///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700394// Transforms
395///////////////////////////////////////////////////////////////////////////////
396
397void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700398 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700399}
400
401void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700402 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700403}
404
405void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700406 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700407}
408
409void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700410 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700411}
412
413void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700414 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700415}
416
417void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700418 mat4 m(*matrix);
Romain Guy8aef54f2010-09-01 15:13:49 -0700419 mSnapshot->transform->multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700420}
421
422///////////////////////////////////////////////////////////////////////////////
423// Clipping
424///////////////////////////////////////////////////////////////////////////////
425
Romain Guybb9524b2010-06-22 18:56:38 -0700426void OpenGLRenderer::setScissorFromClip() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700427 const Rect& clip = *mSnapshot->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700428 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700429}
430
431const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700432 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700433}
434
Romain Guyc7d53492010-06-25 13:41:57 -0700435bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy1d83e192010-08-17 11:37:00 -0700436 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700437 mSnapshot->transform->mapRect(r);
438 return !mSnapshot->clipRect->intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700439}
440
Romain Guy079ba2c2010-07-16 14:12:24 -0700441bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
442 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700443 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700444 setScissorFromClip();
445 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700446 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700447}
448
Romain Guyf6a11b82010-06-23 17:47:49 -0700449///////////////////////////////////////////////////////////////////////////////
450// Drawing
451///////////////////////////////////////////////////////////////////////////////
452
Romain Guyc1396e92010-06-30 17:56:19 -0700453void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700454 const float right = left + bitmap->width();
455 const float bottom = top + bitmap->height();
456
457 if (quickReject(left, top, right, bottom)) {
458 return;
459 }
460
Romain Guy61c8c9c2010-08-09 20:48:09 -0700461 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700462 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700463 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700464 const AutoTexture autoCleanup(texture);
465
Romain Guy6926c722010-07-12 20:20:03 -0700466 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700467}
468
Romain Guyf86ef572010-07-01 11:05:42 -0700469void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
470 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
471 const mat4 transform(*matrix);
472 transform.mapRect(r);
473
Romain Guy6926c722010-07-12 20:20:03 -0700474 if (quickReject(r.left, r.top, r.right, r.bottom)) {
475 return;
476 }
477
Romain Guy61c8c9c2010-08-09 20:48:09 -0700478 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700479 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700480 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700481 const AutoTexture autoCleanup(texture);
482
Romain Guy82ba8142010-07-09 13:25:56 -0700483 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700484}
485
Romain Guy8ba548f2010-06-30 19:21:21 -0700486void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
487 float srcLeft, float srcTop, float srcRight, float srcBottom,
488 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700489 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700490 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
491 return;
492 }
493
Romain Guy61c8c9c2010-08-09 20:48:09 -0700494 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700495 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700496 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700497 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700498
Romain Guy8ba548f2010-06-30 19:21:21 -0700499 const float width = texture->width;
500 const float height = texture->height;
501
502 const float u1 = srcLeft / width;
503 const float v1 = srcTop / height;
504 const float u2 = srcRight / width;
505 const float v2 = srcBottom / height;
506
507 resetDrawTextureTexCoords(u1, v1, u2, v2);
508
Romain Guy82ba8142010-07-09 13:25:56 -0700509 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700510
511 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
512}
513
Romain Guydeba7852010-07-07 17:54:48 -0700514void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
515 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700516 if (quickReject(left, top, right, bottom)) {
517 return;
518 }
519
Romain Guy61c8c9c2010-08-09 20:48:09 -0700520 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700521 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700522 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700523 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700524
525 int alpha;
526 SkXfermode::Mode mode;
527 getAlphaAndMode(paint, &alpha, &mode);
528
Romain Guyfb8b7632010-08-23 21:05:08 -0700529 Patch* mesh = mCaches.patchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700530 mesh->updateVertices(bitmap, left, top, right, bottom,
531 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guy8411f332010-09-13 17:27:57 -0700532 mesh->dump();
Romain Guyf7f93552010-07-08 19:17:03 -0700533
534 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
535 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700536 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
537 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700538 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700539}
540
Romain Guy85bf02f2010-06-22 13:11:24 -0700541void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700542 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700543 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700544}
Romain Guy9d5316e2010-06-24 19:30:36 -0700545
Romain Guybd6b79b2010-06-26 00:13:53 -0700546void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700547 if (quickReject(left, top, right, bottom)) {
548 return;
549 }
550
Romain Guy026c5e162010-06-28 17:12:22 -0700551 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700552 if (!mExtensions.hasFramebufferFetch()) {
553 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
554 if (!isMode) {
555 // Assume SRC_OVER
556 mode = SkXfermode::kSrcOver_Mode;
557 }
558 } else {
559 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700560 }
561
562 // Skia draws using the color's alpha channel if < 255
563 // Otherwise, it uses the paint's alpha
564 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700565 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700566 color |= p->getAlpha() << 24;
567 }
568
569 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700570}
Romain Guy9d5316e2010-06-24 19:30:36 -0700571
Romain Guye8e62a42010-07-23 18:55:21 -0700572void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
573 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700574 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700575 return;
576 }
Romain Guyf607bdc2010-09-10 19:20:06 -0700577 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700578
Romain Guya674ab72010-08-10 17:26:42 -0700579 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700580 switch (paint->getTextAlign()) {
581 case SkPaint::kCenter_Align:
582 length = paint->measureText(text, bytesCount);
583 x -= length / 2.0f;
584 break;
585 case SkPaint::kRight_Align:
586 length = paint->measureText(text, bytesCount);
587 x -= length;
588 break;
589 default:
590 break;
591 }
592
Romain Guy694b5192010-07-21 21:33:20 -0700593 int alpha;
594 SkXfermode::Mode mode;
595 getAlphaAndMode(paint, &alpha, &mode);
596
Romain Guy2542d192010-08-18 11:47:12 -0700597 uint32_t color = paint->getColor();
598 const GLfloat a = alpha / 255.0f;
599 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
600 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
601 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
602
Romain Guyb45c0c92010-08-26 20:35:23 -0700603 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
604 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700605 paint->getTextSize());
Romain Guy1e45aae2010-08-13 19:39:53 -0700606 if (mHasShadow) {
607 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700608 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700609 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700610 count, mShadowRadius);
611 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700612
Romain Guy2542d192010-08-18 11:47:12 -0700613 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700614
615 // Draw the mesh
616 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700617 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700618 }
619
Romain Guy06f96e22010-07-30 19:18:16 -0700620 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700621 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700622
Romain Guyb45c0c92010-08-26 20:35:23 -0700623 setupTextureAlpha8(fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
Romain Guy0a417492010-08-16 20:26:20 -0700624 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700625
Romain Guy09147fb2010-07-22 13:08:20 -0700626 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy86942302010-09-12 13:02:16 -0700627 clearLayerRegions();
Romain Guyb45c0c92010-08-26 20:35:23 -0700628 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700629
630 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700631 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700632
Romain Guy0a417492010-08-16 20:26:20 -0700633 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -0700634}
635
Romain Guy7fbcc042010-08-04 15:40:07 -0700636void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
637 GLuint textureUnit = 0;
638 glActiveTexture(gTextureUnits[textureUnit]);
639
Romain Guyfb8b7632010-08-23 21:05:08 -0700640 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700641 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700642 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700643
Romain Guy8b55f372010-08-18 17:10:07 -0700644 const float x = texture->left - texture->offset;
645 const float y = texture->top - texture->offset;
646
647 if (quickReject(x, y, x + texture->width, y + texture->height)) {
648 return;
649 }
650
Romain Guy7fbcc042010-08-04 15:40:07 -0700651 int alpha;
652 SkXfermode::Mode mode;
653 getAlphaAndMode(paint, &alpha, &mode);
654
655 uint32_t color = paint->getColor();
656 const GLfloat a = alpha / 255.0f;
657 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
658 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
659 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
660
Romain Guy0a417492010-08-16 20:26:20 -0700661 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
662
Romain Guy86942302010-09-12 13:02:16 -0700663 clearLayerRegions();
664
Romain Guy0a417492010-08-16 20:26:20 -0700665 // Draw the mesh
666 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700667 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700668}
669
Romain Guy6926c722010-07-12 20:20:03 -0700670///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700671// Shaders
672///////////////////////////////////////////////////////////////////////////////
673
674void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700675 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700676}
677
Romain Guy06f96e22010-07-30 19:18:16 -0700678void OpenGLRenderer::setupShader(SkiaShader* shader) {
679 mShader = shader;
680 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700681 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700682 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700683}
684
Romain Guyd27977d2010-07-14 19:18:51 -0700685///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700686// Color filters
687///////////////////////////////////////////////////////////////////////////////
688
689void OpenGLRenderer::resetColorFilter() {
690 mColorFilter = NULL;
691}
692
693void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
694 mColorFilter = filter;
695}
696
697///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700698// Drop shadow
699///////////////////////////////////////////////////////////////////////////////
700
701void OpenGLRenderer::resetShadow() {
702 mHasShadow = false;
703}
704
705void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
706 mHasShadow = true;
707 mShadowRadius = radius;
708 mShadowDx = dx;
709 mShadowDy = dy;
710 mShadowColor = color;
711}
712
713///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700714// Drawing implementation
715///////////////////////////////////////////////////////////////////////////////
716
Romain Guy0a417492010-08-16 20:26:20 -0700717void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700718 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700719 const float sx = x - texture->left + mShadowDx;
720 const float sy = y - texture->top + mShadowDy;
721
Romain Guy2542d192010-08-18 11:47:12 -0700722 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
723 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700724 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
725 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
726 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
727
728 GLuint textureUnit = 0;
729 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
730}
731
732void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
733 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
734 bool transforms, bool applyFilters) {
735 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
736 x, y, r, g, b, a, mode, transforms, applyFilters);
737}
738
739void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
740 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
741 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
742 // Describe the required shaders
743 ProgramDescription description;
744 description.hasTexture = true;
745 description.hasAlpha8Texture = true;
746
747 if (applyFilters) {
748 if (mShader) {
749 mShader->describe(description, mExtensions);
750 }
751 if (mColorFilter) {
752 mColorFilter->describe(description, mExtensions);
753 }
754 }
755
Romain Guya5aed0d2010-09-09 14:42:43 -0700756 // Setup the blending mode
757 chooseBlending(true, mode, description);
758
Romain Guy0a417492010-08-16 20:26:20 -0700759 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700760 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700761
Romain Guy0a417492010-08-16 20:26:20 -0700762 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -0700763 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700764
Romain Guyfb8b7632010-08-23 21:05:08 -0700765 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -0700766 glEnableVertexAttribArray(texCoordsSlot);
767
768 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700769 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy0a417492010-08-16 20:26:20 -0700770 gMeshStride, &mMeshVertices[0].position[0]);
771 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
772 gMeshStride, &mMeshVertices[0].texture[0]);
773
774 // Setup uniforms
775 if (transforms) {
776 mModelView.loadTranslate(x, y, 0.0f);
777 mModelView.scale(width, height, 1.0f);
778 } else {
779 mModelView.loadIdentity();
780 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700781 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyfb8b7632010-08-23 21:05:08 -0700782 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -0700783
784 textureUnit++;
785 if (applyFilters) {
786 // Setup attributes and uniforms required by the shaders
787 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700788 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700789 }
790 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700791 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -0700792 }
793 }
794}
795
Romain Guyf607bdc2010-09-10 19:20:06 -0700796// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -0700797#define kStdStrikeThru_Offset (-6.0f / 21.0f)
798#define kStdUnderline_Offset (1.0f / 9.0f)
799#define kStdUnderline_Thickness (1.0f / 18.0f)
800
801void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
802 float x, float y, SkPaint* paint) {
803 // Handle underline and strike-through
804 uint32_t flags = paint->getFlags();
805 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
806 float underlineWidth = length;
807 // If length is > 0.0f, we already measured the text for the text alignment
808 if (length <= 0.0f) {
809 underlineWidth = paint->measureText(text, bytesCount);
810 }
811
812 float offsetX = 0;
813 switch (paint->getTextAlign()) {
814 case SkPaint::kCenter_Align:
815 offsetX = underlineWidth * 0.5f;
816 break;
817 case SkPaint::kRight_Align:
818 offsetX = underlineWidth;
819 break;
820 default:
821 break;
822 }
823
824 if (underlineWidth > 0.0f) {
825 float textSize = paint->getTextSize();
826 float height = textSize * kStdUnderline_Thickness;
827
828 float left = x - offsetX;
829 float top = 0.0f;
830 float right = left + underlineWidth;
831 float bottom = 0.0f;
832
833 if (flags & SkPaint::kUnderlineText_Flag) {
834 top = y + textSize * kStdUnderline_Offset;
835 bottom = top + height;
836 drawRect(left, top, right, bottom, paint);
837 }
838
839 if (flags & SkPaint::kStrikeThruText_Flag) {
840 top = y + textSize * kStdStrikeThru_Offset;
841 bottom = top + height;
842 drawRect(left, top, right, bottom, paint);
843 }
844 }
845 }
846}
847
Romain Guy026c5e162010-06-28 17:12:22 -0700848void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guyf607bdc2010-09-10 19:20:06 -0700849 int color, SkXfermode::Mode mode, bool ignoreTransform, bool ignoreBlending) {
Romain Guy86942302010-09-12 13:02:16 -0700850 clearLayerRegions();
851
Romain Guyd27977d2010-07-14 19:18:51 -0700852 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700853 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700854 color |= 0x00ffffff;
855 }
856
857 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700858 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700859 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700860 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
861 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
862 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
863
Romain Guy06f96e22010-07-30 19:18:16 -0700864 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700865
Romain Guy06f96e22010-07-30 19:18:16 -0700866 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700867 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700868 if (mShader) {
869 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700870 }
Romain Guydb1938e2010-08-02 18:50:22 -0700871 if (mColorFilter) {
872 mColorFilter->describe(description, mExtensions);
873 }
Romain Guyd27977d2010-07-14 19:18:51 -0700874
Romain Guyf607bdc2010-09-10 19:20:06 -0700875 if (!ignoreBlending) {
876 // Setup the blending mode
877 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode, description);
878 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700879
Romain Guy06f96e22010-07-30 19:18:16 -0700880 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700881 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -0700882
883 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700884 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -0700885 gMeshStride, &mMeshVertices[0].position[0]);
886
887 // Setup uniforms
888 mModelView.loadTranslate(left, top, 0.0f);
889 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700890 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700891 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700892 } else {
893 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -0700894 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700895 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700896 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700897
Romain Guy06f96e22010-07-30 19:18:16 -0700898 // Setup attributes and uniforms required by the shaders
899 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700900 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700901 }
Romain Guydb1938e2010-08-02 18:50:22 -0700902 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700903 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700904 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700905
Romain Guy06f96e22010-07-30 19:18:16 -0700906 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700907 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700908}
909
Romain Guy82ba8142010-07-09 13:25:56 -0700910void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700911 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700912 int alpha;
913 SkXfermode::Mode mode;
914 getAlphaAndMode(paint, &alpha, &mode);
915
Romain Guya9794742010-07-13 11:37:54 -0700916 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700917 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700918}
919
Romain Guybd6b79b2010-06-26 00:13:53 -0700920void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700921 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
922 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700923 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700924}
925
926void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700927 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf607bdc2010-09-10 19:20:06 -0700928 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount,
929 bool swapSrcDst, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -0700930 clearLayerRegions();
931
Romain Guy889f8d12010-07-29 14:37:42 -0700932 ProgramDescription description;
933 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700934 if (mColorFilter) {
935 mColorFilter->describe(description, mExtensions);
936 }
Romain Guy889f8d12010-07-29 14:37:42 -0700937
Romain Guybd6b79b2010-06-26 00:13:53 -0700938 mModelView.loadTranslate(left, top, 0.0f);
939 mModelView.scale(right - left, bottom - top, 1.0f);
940
Romain Guyf607bdc2010-09-10 19:20:06 -0700941 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -0700942
Romain Guyfb8b7632010-08-23 21:05:08 -0700943 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -0700944 if (!ignoreTransform) {
945 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
946 } else {
947 mat4 m;
948 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
949 }
Romain Guybd6b79b2010-06-26 00:13:53 -0700950
Romain Guy889f8d12010-07-29 14:37:42 -0700951 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700952 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700953 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700954
Romain Guya9794742010-07-13 11:37:54 -0700955 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -0700956 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700957
Romain Guy889f8d12010-07-29 14:37:42 -0700958 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -0700959 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -0700960 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -0700961 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700962 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700963 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700964
Romain Guydb1938e2010-08-02 18:50:22 -0700965 // Color filter
966 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700967 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700968 }
969
Romain Guyf7f93552010-07-08 19:17:03 -0700970 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700971 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700972 } else {
973 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
974 }
Romain Guy889f8d12010-07-29 14:37:42 -0700975 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700976}
977
Romain Guya5aed0d2010-09-09 14:42:43 -0700978void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -0700979 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -0700980 blend = blend || mode != SkXfermode::kSrcOver_Mode;
981 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700982 if (mode < SkXfermode::kPlus_Mode) {
983 if (!mCaches.blend) {
984 glEnable(GL_BLEND);
985 }
Romain Guy82ba8142010-07-09 13:25:56 -0700986
Romain Guyf607bdc2010-09-10 19:20:06 -0700987 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
988 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -0700989
Romain Guya5aed0d2010-09-09 14:42:43 -0700990 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
991 glBlendFunc(sourceMode, destMode);
992 mCaches.lastSrcMode = sourceMode;
993 mCaches.lastDstMode = destMode;
994 }
995 } else {
996 // These blend modes are not supported by OpenGL directly and have
997 // to be implemented using shaders. Since the shader will perform
998 // the blending, turn blending off here
999 if (mExtensions.hasFramebufferFetch()) {
1000 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001001 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001002 }
1003
1004 if (mCaches.blend) {
1005 glDisable(GL_BLEND);
1006 }
1007 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001008 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001009 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001010 glDisable(GL_BLEND);
1011 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001012 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001013}
1014
Romain Guy889f8d12010-07-29 14:37:42 -07001015bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001016 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001017 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001018 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001019 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001020 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001021 }
Romain Guy6926c722010-07-12 20:20:03 -07001022 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001023}
1024
Romain Guy026c5e162010-06-28 17:12:22 -07001025void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001026 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001027 TextureVertex::setUV(v++, u1, v1);
1028 TextureVertex::setUV(v++, u2, v1);
1029 TextureVertex::setUV(v++, u1, v2);
1030 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001031}
1032
1033void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1034 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001035 if (!mExtensions.hasFramebufferFetch()) {
1036 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1037 if (!isMode) {
1038 // Assume SRC_OVER
1039 *mode = SkXfermode::kSrcOver_Mode;
1040 }
1041 } else {
1042 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001043 }
1044
1045 // Skia draws using the color's alpha channel if < 255
1046 // Otherwise, it uses the paint's alpha
1047 int color = paint->getColor();
1048 *alpha = (color >> 24) & 0xFF;
1049 if (*alpha == 255) {
1050 *alpha = paint->getAlpha();
1051 }
1052 } else {
1053 *mode = SkXfermode::kSrcOver_Mode;
1054 *alpha = 255;
1055 }
Romain Guy026c5e162010-06-28 17:12:22 -07001056}
1057
Romain Guya5aed0d2010-09-09 14:42:43 -07001058SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1059 if (mode == NULL) {
1060 return SkXfermode::kSrcOver_Mode;
1061 }
1062 return mode->fMode;
1063}
1064
Romain Guy889f8d12010-07-29 14:37:42 -07001065void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1066 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001067 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001068 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1069 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1070}
1071
Romain Guy9d5316e2010-06-24 19:30:36 -07001072}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001073}; // namespace android