blob: 0545ea0ee7f21520629d76c30ba63009e2364cb2 [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 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 Guy8411f332010-09-13 17:27:57 -0700292 // Layers only make sense if they are in the framebuffer's bounds
293 bounds.intersect(*mSnapshot->clipRect);
294 if (bounds.isEmpty()) return;
Romain Guyf18fd992010-07-08 11:45:51 -0700295
Romain Guy8411f332010-09-13 17:27:57 -0700296 LayerSize size(bounds.getWidth(), bounds.getHeight());
Romain Guyf607bdc2010-09-10 19:20:06 -0700297 Layer* layer = mCaches.layerCache.get(size);
Romain Guydda570202010-07-06 11:39:32 -0700298 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700299 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700300 }
301
Romain Guydda570202010-07-06 11:39:32 -0700302 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700303 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700304 layer->layer.set(bounds);
Romain Guydda570202010-07-06 11:39:32 -0700305
Romain Guy8fb95422010-08-17 18:38:51 -0700306 // Save the layer in the snapshot
307 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700308 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700309
Romain Guyf607bdc2010-09-10 19:20:06 -0700310 // Copy the framebuffer into the layer
311 glBindTexture(GL_TEXTURE_2D, layer->texture);
312 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom,
313 bounds.getWidth(), bounds.getHeight(), 0);
314
Romain Guyf607bdc2010-09-10 19:20:06 -0700315 if (flags & SkCanvas::kClipToLayer_SaveFlag) {
Romain Guy86942302010-09-12 13:02:16 -0700316 if (mSnapshot->clipTransformed(bounds)) setScissorFromClip();
Romain Guyf607bdc2010-09-10 19:20:06 -0700317 }
318
Romain Guy86942302010-09-12 13:02:16 -0700319 // Enqueue the buffer coordinates to clear the corresponding region later
320 mLayers.push(new Rect(bounds));
Romain Guyf86ef572010-07-01 11:05:42 -0700321
Romain Guyd55a8612010-06-28 17:42:46 -0700322 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700323}
324
Romain Guy1d83e192010-08-17 11:37:00 -0700325void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
326 if (!current->layer) {
327 LOGE("Attempting to compose a layer that does not exist");
328 return;
329 }
330
Romain Guy1d83e192010-08-17 11:37:00 -0700331 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700332 const Rect& clip = *previous->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700333 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700334
335 Layer* layer = current->layer;
336 const Rect& rect = layer->layer;
337
Romain Guyf607bdc2010-09-10 19:20:06 -0700338 if (layer->alpha < 255) {
339 glEnable(GL_BLEND);
340 glBlendFuncSeparate(GL_ZERO, GL_SRC_ALPHA, GL_DST_ALPHA, GL_ZERO);
341
342 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
343 layer->alpha << 24, SkXfermode::kSrcOver_Mode, true, true);
344
345 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
346 if (!mCaches.blend) {
347 glDisable(GL_BLEND);
348 }
349 }
350
351 // Layers are already drawn with a top-left origin, don't flip the texture
Romain Guy8b55f372010-08-18 17:10:07 -0700352 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
353
Romain Guyf607bdc2010-09-10 19:20:06 -0700354 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
355 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
356 &mMeshVertices[0].texture[0], NULL, 0, true, true);
Romain Guy1d83e192010-08-17 11:37:00 -0700357
Romain Guy8b55f372010-08-18 17:10:07 -0700358 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
359
Romain Guy1d83e192010-08-17 11:37:00 -0700360 LayerSize size(rect.getWidth(), rect.getHeight());
361 // Failing to add the layer to the cache should happen only if the
362 // layer is too large
Romain Guyfb8b7632010-08-23 21:05:08 -0700363 if (!mCaches.layerCache.put(size, layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700364 LAYER_LOGD("Deleting layer");
365
Romain Guy1d83e192010-08-17 11:37:00 -0700366 glDeleteTextures(1, &layer->texture);
367
368 delete layer;
369 }
370}
371
Romain Guy86942302010-09-12 13:02:16 -0700372void OpenGLRenderer::clearLayerRegions() {
373 if (mLayers.size() == 0) return;
374
375 for (uint32_t i = 0; i < mLayers.size(); i++) {
376 Rect* bounds = mLayers.itemAt(i);
377
378 // Clear the framebuffer where the layer will draw
379 glScissor(bounds->left, mHeight - bounds->bottom,
380 bounds->getWidth(), bounds->getHeight());
381 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
382 glClear(GL_COLOR_BUFFER_BIT);
383
384 delete bounds;
385 }
386 mLayers.clear();
387
388 // Restore the clip
389 setScissorFromClip();
390}
391
Romain Guybd6b79b2010-06-26 00:13:53 -0700392///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700393// Transforms
394///////////////////////////////////////////////////////////////////////////////
395
396void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700397 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700398}
399
400void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700401 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700402}
403
404void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700405 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700406}
407
408void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700409 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700410}
411
412void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700413 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700414}
415
416void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700417 mat4 m(*matrix);
Romain Guy8aef54f2010-09-01 15:13:49 -0700418 mSnapshot->transform->multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700419}
420
421///////////////////////////////////////////////////////////////////////////////
422// Clipping
423///////////////////////////////////////////////////////////////////////////////
424
Romain Guybb9524b2010-06-22 18:56:38 -0700425void OpenGLRenderer::setScissorFromClip() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700426 const Rect& clip = *mSnapshot->clipRect;
Romain Guyf607bdc2010-09-10 19:20:06 -0700427 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700428}
429
430const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700431 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700432}
433
Romain Guyc7d53492010-06-25 13:41:57 -0700434bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy1d83e192010-08-17 11:37:00 -0700435 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700436 mSnapshot->transform->mapRect(r);
437 return !mSnapshot->clipRect->intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700438}
439
Romain Guy079ba2c2010-07-16 14:12:24 -0700440bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
441 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700442 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700443 setScissorFromClip();
444 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700445 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700446}
447
Romain Guyf6a11b82010-06-23 17:47:49 -0700448///////////////////////////////////////////////////////////////////////////////
449// Drawing
450///////////////////////////////////////////////////////////////////////////////
451
Romain Guyc1396e92010-06-30 17:56:19 -0700452void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700453 const float right = left + bitmap->width();
454 const float bottom = top + bitmap->height();
455
456 if (quickReject(left, top, right, bottom)) {
457 return;
458 }
459
Romain Guy61c8c9c2010-08-09 20:48:09 -0700460 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700461 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700462 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700463 const AutoTexture autoCleanup(texture);
464
Romain Guy6926c722010-07-12 20:20:03 -0700465 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700466}
467
Romain Guyf86ef572010-07-01 11:05:42 -0700468void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
469 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
470 const mat4 transform(*matrix);
471 transform.mapRect(r);
472
Romain Guy6926c722010-07-12 20:20:03 -0700473 if (quickReject(r.left, r.top, r.right, r.bottom)) {
474 return;
475 }
476
Romain Guy61c8c9c2010-08-09 20:48:09 -0700477 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700478 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700479 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700480 const AutoTexture autoCleanup(texture);
481
Romain Guy82ba8142010-07-09 13:25:56 -0700482 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700483}
484
Romain Guy8ba548f2010-06-30 19:21:21 -0700485void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
486 float srcLeft, float srcTop, float srcRight, float srcBottom,
487 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700488 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700489 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
490 return;
491 }
492
Romain Guy61c8c9c2010-08-09 20:48:09 -0700493 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700494 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700495 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700496 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700497
Romain Guy8ba548f2010-06-30 19:21:21 -0700498 const float width = texture->width;
499 const float height = texture->height;
500
501 const float u1 = srcLeft / width;
502 const float v1 = srcTop / height;
503 const float u2 = srcRight / width;
504 const float v2 = srcBottom / height;
505
506 resetDrawTextureTexCoords(u1, v1, u2, v2);
507
Romain Guy82ba8142010-07-09 13:25:56 -0700508 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700509
510 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
511}
512
Romain Guydeba7852010-07-07 17:54:48 -0700513void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
514 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700515 if (quickReject(left, top, right, bottom)) {
516 return;
517 }
518
Romain Guy61c8c9c2010-08-09 20:48:09 -0700519 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700520 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700521 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700522 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700523
524 int alpha;
525 SkXfermode::Mode mode;
526 getAlphaAndMode(paint, &alpha, &mode);
527
Romain Guyfb8b7632010-08-23 21:05:08 -0700528 Patch* mesh = mCaches.patchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700529 mesh->updateVertices(bitmap, left, top, right, bottom,
530 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guy8411f332010-09-13 17:27:57 -0700531 mesh->dump();
Romain Guyf7f93552010-07-08 19:17:03 -0700532
533 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
534 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700535 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
536 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700537 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700538}
539
Romain Guy85bf02f2010-06-22 13:11:24 -0700540void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700541 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700542 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700543}
Romain Guy9d5316e2010-06-24 19:30:36 -0700544
Romain Guybd6b79b2010-06-26 00:13:53 -0700545void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700546 if (quickReject(left, top, right, bottom)) {
547 return;
548 }
549
Romain Guy026c5e162010-06-28 17:12:22 -0700550 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700551 if (!mExtensions.hasFramebufferFetch()) {
552 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
553 if (!isMode) {
554 // Assume SRC_OVER
555 mode = SkXfermode::kSrcOver_Mode;
556 }
557 } else {
558 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700559 }
560
561 // Skia draws using the color's alpha channel if < 255
562 // Otherwise, it uses the paint's alpha
563 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700564 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700565 color |= p->getAlpha() << 24;
566 }
567
568 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700569}
Romain Guy9d5316e2010-06-24 19:30:36 -0700570
Romain Guye8e62a42010-07-23 18:55:21 -0700571void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
572 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700573 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700574 return;
575 }
Romain Guyf607bdc2010-09-10 19:20:06 -0700576 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -0700577
Romain Guya674ab72010-08-10 17:26:42 -0700578 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700579 switch (paint->getTextAlign()) {
580 case SkPaint::kCenter_Align:
581 length = paint->measureText(text, bytesCount);
582 x -= length / 2.0f;
583 break;
584 case SkPaint::kRight_Align:
585 length = paint->measureText(text, bytesCount);
586 x -= length;
587 break;
588 default:
589 break;
590 }
591
Romain Guy694b5192010-07-21 21:33:20 -0700592 int alpha;
593 SkXfermode::Mode mode;
594 getAlphaAndMode(paint, &alpha, &mode);
595
Romain Guy2542d192010-08-18 11:47:12 -0700596 uint32_t color = paint->getColor();
597 const GLfloat a = alpha / 255.0f;
598 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
599 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
600 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
601
Romain Guyb45c0c92010-08-26 20:35:23 -0700602 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
603 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700604 paint->getTextSize());
Romain Guy1e45aae2010-08-13 19:39:53 -0700605 if (mHasShadow) {
606 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700607 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700608 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700609 count, mShadowRadius);
610 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700611
Romain Guy2542d192010-08-18 11:47:12 -0700612 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700613
614 // Draw the mesh
615 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700616 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700617 }
618
Romain Guy06f96e22010-07-30 19:18:16 -0700619 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700620 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700621
Romain Guyb45c0c92010-08-26 20:35:23 -0700622 setupTextureAlpha8(fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
Romain Guy0a417492010-08-16 20:26:20 -0700623 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700624
Romain Guy09147fb2010-07-22 13:08:20 -0700625 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy86942302010-09-12 13:02:16 -0700626 clearLayerRegions();
Romain Guyb45c0c92010-08-26 20:35:23 -0700627 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700628
629 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700630 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700631
Romain Guy0a417492010-08-16 20:26:20 -0700632 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -0700633}
634
Romain Guy7fbcc042010-08-04 15:40:07 -0700635void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
636 GLuint textureUnit = 0;
637 glActiveTexture(gTextureUnits[textureUnit]);
638
Romain Guyfb8b7632010-08-23 21:05:08 -0700639 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700640 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700641 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700642
Romain Guy8b55f372010-08-18 17:10:07 -0700643 const float x = texture->left - texture->offset;
644 const float y = texture->top - texture->offset;
645
646 if (quickReject(x, y, x + texture->width, y + texture->height)) {
647 return;
648 }
649
Romain Guy7fbcc042010-08-04 15:40:07 -0700650 int alpha;
651 SkXfermode::Mode mode;
652 getAlphaAndMode(paint, &alpha, &mode);
653
654 uint32_t color = paint->getColor();
655 const GLfloat a = alpha / 255.0f;
656 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
657 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
658 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
659
Romain Guy0a417492010-08-16 20:26:20 -0700660 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
661
Romain Guy86942302010-09-12 13:02:16 -0700662 clearLayerRegions();
663
Romain Guy0a417492010-08-16 20:26:20 -0700664 // Draw the mesh
665 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700666 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700667}
668
Romain Guy6926c722010-07-12 20:20:03 -0700669///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700670// Shaders
671///////////////////////////////////////////////////////////////////////////////
672
673void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700674 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700675}
676
Romain Guy06f96e22010-07-30 19:18:16 -0700677void OpenGLRenderer::setupShader(SkiaShader* shader) {
678 mShader = shader;
679 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700680 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700681 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700682}
683
Romain Guyd27977d2010-07-14 19:18:51 -0700684///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700685// Color filters
686///////////////////////////////////////////////////////////////////////////////
687
688void OpenGLRenderer::resetColorFilter() {
689 mColorFilter = NULL;
690}
691
692void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
693 mColorFilter = filter;
694}
695
696///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700697// Drop shadow
698///////////////////////////////////////////////////////////////////////////////
699
700void OpenGLRenderer::resetShadow() {
701 mHasShadow = false;
702}
703
704void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
705 mHasShadow = true;
706 mShadowRadius = radius;
707 mShadowDx = dx;
708 mShadowDy = dy;
709 mShadowColor = color;
710}
711
712///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700713// Drawing implementation
714///////////////////////////////////////////////////////////////////////////////
715
Romain Guy0a417492010-08-16 20:26:20 -0700716void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700717 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700718 const float sx = x - texture->left + mShadowDx;
719 const float sy = y - texture->top + mShadowDy;
720
Romain Guy2542d192010-08-18 11:47:12 -0700721 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
722 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700723 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
724 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
725 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
726
727 GLuint textureUnit = 0;
728 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
729}
730
731void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
732 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
733 bool transforms, bool applyFilters) {
734 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
735 x, y, r, g, b, a, mode, transforms, applyFilters);
736}
737
738void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
739 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
740 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
741 // Describe the required shaders
742 ProgramDescription description;
743 description.hasTexture = true;
744 description.hasAlpha8Texture = true;
745
746 if (applyFilters) {
747 if (mShader) {
748 mShader->describe(description, mExtensions);
749 }
750 if (mColorFilter) {
751 mColorFilter->describe(description, mExtensions);
752 }
753 }
754
Romain Guya5aed0d2010-09-09 14:42:43 -0700755 // Setup the blending mode
756 chooseBlending(true, mode, description);
757
Romain Guy0a417492010-08-16 20:26:20 -0700758 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700759 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700760
Romain Guy0a417492010-08-16 20:26:20 -0700761 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -0700762 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700763
Romain Guyfb8b7632010-08-23 21:05:08 -0700764 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -0700765 glEnableVertexAttribArray(texCoordsSlot);
766
767 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700768 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy0a417492010-08-16 20:26:20 -0700769 gMeshStride, &mMeshVertices[0].position[0]);
770 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
771 gMeshStride, &mMeshVertices[0].texture[0]);
772
773 // Setup uniforms
774 if (transforms) {
775 mModelView.loadTranslate(x, y, 0.0f);
776 mModelView.scale(width, height, 1.0f);
777 } else {
778 mModelView.loadIdentity();
779 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700780 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyfb8b7632010-08-23 21:05:08 -0700781 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -0700782
783 textureUnit++;
784 if (applyFilters) {
785 // Setup attributes and uniforms required by the shaders
786 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700787 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700788 }
789 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700790 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -0700791 }
792 }
793}
794
Romain Guyf607bdc2010-09-10 19:20:06 -0700795// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -0700796#define kStdStrikeThru_Offset (-6.0f / 21.0f)
797#define kStdUnderline_Offset (1.0f / 9.0f)
798#define kStdUnderline_Thickness (1.0f / 18.0f)
799
800void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
801 float x, float y, SkPaint* paint) {
802 // Handle underline and strike-through
803 uint32_t flags = paint->getFlags();
804 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
805 float underlineWidth = length;
806 // If length is > 0.0f, we already measured the text for the text alignment
807 if (length <= 0.0f) {
808 underlineWidth = paint->measureText(text, bytesCount);
809 }
810
811 float offsetX = 0;
812 switch (paint->getTextAlign()) {
813 case SkPaint::kCenter_Align:
814 offsetX = underlineWidth * 0.5f;
815 break;
816 case SkPaint::kRight_Align:
817 offsetX = underlineWidth;
818 break;
819 default:
820 break;
821 }
822
823 if (underlineWidth > 0.0f) {
824 float textSize = paint->getTextSize();
825 float height = textSize * kStdUnderline_Thickness;
826
827 float left = x - offsetX;
828 float top = 0.0f;
829 float right = left + underlineWidth;
830 float bottom = 0.0f;
831
832 if (flags & SkPaint::kUnderlineText_Flag) {
833 top = y + textSize * kStdUnderline_Offset;
834 bottom = top + height;
835 drawRect(left, top, right, bottom, paint);
836 }
837
838 if (flags & SkPaint::kStrikeThruText_Flag) {
839 top = y + textSize * kStdStrikeThru_Offset;
840 bottom = top + height;
841 drawRect(left, top, right, bottom, paint);
842 }
843 }
844 }
845}
846
Romain Guy026c5e162010-06-28 17:12:22 -0700847void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guyf607bdc2010-09-10 19:20:06 -0700848 int color, SkXfermode::Mode mode, bool ignoreTransform, bool ignoreBlending) {
Romain Guy86942302010-09-12 13:02:16 -0700849 clearLayerRegions();
850
Romain Guyd27977d2010-07-14 19:18:51 -0700851 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700852 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700853 color |= 0x00ffffff;
854 }
855
856 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700857 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700858 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700859 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
860 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
861 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
862
Romain Guy06f96e22010-07-30 19:18:16 -0700863 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700864
Romain Guy06f96e22010-07-30 19:18:16 -0700865 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700866 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700867 if (mShader) {
868 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700869 }
Romain Guydb1938e2010-08-02 18:50:22 -0700870 if (mColorFilter) {
871 mColorFilter->describe(description, mExtensions);
872 }
Romain Guyd27977d2010-07-14 19:18:51 -0700873
Romain Guyf607bdc2010-09-10 19:20:06 -0700874 if (!ignoreBlending) {
875 // Setup the blending mode
876 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode, description);
877 }
Romain Guya5aed0d2010-09-09 14:42:43 -0700878
Romain Guy06f96e22010-07-30 19:18:16 -0700879 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700880 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -0700881
882 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700883 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -0700884 gMeshStride, &mMeshVertices[0].position[0]);
885
886 // Setup uniforms
887 mModelView.loadTranslate(left, top, 0.0f);
888 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700889 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700890 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700891 } else {
892 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -0700893 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700894 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700895 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700896
Romain Guy06f96e22010-07-30 19:18:16 -0700897 // Setup attributes and uniforms required by the shaders
898 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700899 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700900 }
Romain Guydb1938e2010-08-02 18:50:22 -0700901 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700902 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700903 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700904
Romain Guy06f96e22010-07-30 19:18:16 -0700905 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700906 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700907}
908
Romain Guy82ba8142010-07-09 13:25:56 -0700909void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700910 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700911 int alpha;
912 SkXfermode::Mode mode;
913 getAlphaAndMode(paint, &alpha, &mode);
914
Romain Guya9794742010-07-13 11:37:54 -0700915 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700916 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700917}
918
Romain Guybd6b79b2010-06-26 00:13:53 -0700919void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700920 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
921 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700922 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700923}
924
925void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700926 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf607bdc2010-09-10 19:20:06 -0700927 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount,
928 bool swapSrcDst, bool ignoreTransform) {
Romain Guy86942302010-09-12 13:02:16 -0700929 clearLayerRegions();
930
Romain Guy889f8d12010-07-29 14:37:42 -0700931 ProgramDescription description;
932 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700933 if (mColorFilter) {
934 mColorFilter->describe(description, mExtensions);
935 }
Romain Guy889f8d12010-07-29 14:37:42 -0700936
Romain Guybd6b79b2010-06-26 00:13:53 -0700937 mModelView.loadTranslate(left, top, 0.0f);
938 mModelView.scale(right - left, bottom - top, 1.0f);
939
Romain Guyf607bdc2010-09-10 19:20:06 -0700940 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -0700941
Romain Guyfb8b7632010-08-23 21:05:08 -0700942 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -0700943 if (!ignoreTransform) {
944 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
945 } else {
946 mat4 m;
947 mCaches.currentProgram->set(mOrthoMatrix, mModelView, m);
948 }
Romain Guybd6b79b2010-06-26 00:13:53 -0700949
Romain Guy889f8d12010-07-29 14:37:42 -0700950 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700951 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700952 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700953
Romain Guya9794742010-07-13 11:37:54 -0700954 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -0700955 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700956
Romain Guy889f8d12010-07-29 14:37:42 -0700957 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -0700958 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -0700959 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -0700960 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700961 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700962 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700963
Romain Guydb1938e2010-08-02 18:50:22 -0700964 // Color filter
965 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700966 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700967 }
968
Romain Guyf7f93552010-07-08 19:17:03 -0700969 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700970 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700971 } else {
972 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
973 }
Romain Guy889f8d12010-07-29 14:37:42 -0700974 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700975}
976
Romain Guya5aed0d2010-09-09 14:42:43 -0700977void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -0700978 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -0700979 blend = blend || mode != SkXfermode::kSrcOver_Mode;
980 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700981 if (mode < SkXfermode::kPlus_Mode) {
982 if (!mCaches.blend) {
983 glEnable(GL_BLEND);
984 }
Romain Guy82ba8142010-07-09 13:25:56 -0700985
Romain Guyf607bdc2010-09-10 19:20:06 -0700986 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
987 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -0700988
Romain Guya5aed0d2010-09-09 14:42:43 -0700989 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
990 glBlendFunc(sourceMode, destMode);
991 mCaches.lastSrcMode = sourceMode;
992 mCaches.lastDstMode = destMode;
993 }
994 } else {
995 // These blend modes are not supported by OpenGL directly and have
996 // to be implemented using shaders. Since the shader will perform
997 // the blending, turn blending off here
998 if (mExtensions.hasFramebufferFetch()) {
999 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001000 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001001 }
1002
1003 if (mCaches.blend) {
1004 glDisable(GL_BLEND);
1005 }
1006 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001007 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001008 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001009 glDisable(GL_BLEND);
1010 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001011 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001012}
1013
Romain Guy889f8d12010-07-29 14:37:42 -07001014bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001015 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001016 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001017 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001018 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001019 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001020 }
Romain Guy6926c722010-07-12 20:20:03 -07001021 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001022}
1023
Romain Guy026c5e162010-06-28 17:12:22 -07001024void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001025 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001026 TextureVertex::setUV(v++, u1, v1);
1027 TextureVertex::setUV(v++, u2, v1);
1028 TextureVertex::setUV(v++, u1, v2);
1029 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001030}
1031
1032void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1033 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001034 if (!mExtensions.hasFramebufferFetch()) {
1035 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1036 if (!isMode) {
1037 // Assume SRC_OVER
1038 *mode = SkXfermode::kSrcOver_Mode;
1039 }
1040 } else {
1041 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001042 }
1043
1044 // Skia draws using the color's alpha channel if < 255
1045 // Otherwise, it uses the paint's alpha
1046 int color = paint->getColor();
1047 *alpha = (color >> 24) & 0xFF;
1048 if (*alpha == 255) {
1049 *alpha = paint->getAlpha();
1050 }
1051 } else {
1052 *mode = SkXfermode::kSrcOver_Mode;
1053 *alpha = 255;
1054 }
Romain Guy026c5e162010-06-28 17:12:22 -07001055}
1056
Romain Guya5aed0d2010-09-09 14:42:43 -07001057SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1058 if (mode == NULL) {
1059 return SkXfermode::kSrcOver_Mode;
1060 }
1061 return mode->fMode;
1062}
1063
Romain Guy889f8d12010-07-29 14:37:42 -07001064void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1065 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001066 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001067 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1068 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1069}
1070
Romain Guy9d5316e2010-06-24 19:30:36 -07001071}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001072}; // namespace android