blob: 5817977bad5718d78f853388485413156896a6dc [file] [log] [blame]
Romain Guy211efea2012-07-31 21:16:07 -07001/*
2 * Copyright (C) 2012 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
17#include "Caches.h"
18#include "Dither.h"
19
20namespace android {
21namespace uirenderer {
22
23///////////////////////////////////////////////////////////////////////////////
24// Defines
25///////////////////////////////////////////////////////////////////////////////
26
27// Must be a power of two
28#define DITHER_KERNEL_SIZE 4
29
30///////////////////////////////////////////////////////////////////////////////
31// Lifecycle
32///////////////////////////////////////////////////////////////////////////////
33
34void Dither::bindDitherTexture() {
35 if (!mInitialized) {
36 const uint8_t pattern[] = {
37 0, 8, 2, 10,
38 12, 4, 14, 6,
39 3, 11, 1, 9,
40 15, 7, 13, 5
41 };
42
43 glGenTextures(1, &mDitherTexture);
44 glBindTexture(GL_TEXTURE_2D, mDitherTexture);
45
46 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
47
48 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
49 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
50
51 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
52 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
53
54 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, DITHER_KERNEL_SIZE, DITHER_KERNEL_SIZE, 0,
55 GL_ALPHA, GL_UNSIGNED_BYTE, &pattern);
56
57 mInitialized = true;
58 } else {
59 glBindTexture(GL_TEXTURE_2D, mDitherTexture);
60 }
61}
62
63void Dither::clear() {
64 if (mInitialized) {
65 glDeleteTextures(1, &mDitherTexture);
66 }
67}
68
69///////////////////////////////////////////////////////////////////////////////
70// Program management
71///////////////////////////////////////////////////////////////////////////////
72
73void Dither::setupProgram(Program* program, GLuint* textureUnit) {
74 GLuint textureSlot = (*textureUnit)++;
75 Caches::getInstance().activeTexture(textureSlot);
76
77 bindDitherTexture();
78
79 glUniform1i(program->getUniform("ditherSampler"), textureSlot);
80 glUniform1f(program->getUniform("ditherSize"), 1.0f / DITHER_KERNEL_SIZE);
81}
82
83}; // namespace uirenderer
84}; // namespace android