blob: 9bc5c140e8d5836819dacc67a14ca040389a739c [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///////////////////////////////////////////////////////////////////////////////
Romain Guy211efea2012-07-31 21:16:07 -070024// Lifecycle
25///////////////////////////////////////////////////////////////////////////////
26
27void Dither::bindDitherTexture() {
28 if (!mInitialized) {
Romain Guyb4880042013-04-05 11:17:55 -070029 bool useFloatTexture = Extensions::getInstance().getMajorGlVersion() >= 3;
Romain Guy211efea2012-07-31 21:16:07 -070030
31 glGenTextures(1, &mDitherTexture);
32 glBindTexture(GL_TEXTURE_2D, mDitherTexture);
33
Romain Guy211efea2012-07-31 21:16:07 -070034 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
35 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
36
37 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
38 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
39
Romain Guyb4880042013-04-05 11:17:55 -070040 if (useFloatTexture) {
41 float dither = 1.0f / (255.0f * DITHER_KERNEL_SIZE * DITHER_KERNEL_SIZE);
42 const GLfloat pattern[] = {
43 0 * dither, 8 * dither, 2 * dither, 10 * dither,
44 12 * dither, 4 * dither, 14 * dither, 6 * dither,
45 3 * dither, 11 * dither, 1 * dither, 9 * dither,
46 15 * dither, 7 * dither, 13 * dither, 5 * dither
47 };
48
49 glPixelStorei(GL_UNPACK_ALIGNMENT, sizeof(GLfloat));
50 glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, DITHER_KERNEL_SIZE, DITHER_KERNEL_SIZE, 0,
51 GL_RED, GL_FLOAT, &pattern);
52 } else {
53 const uint8_t pattern[] = {
54 0, 8, 2, 10,
55 12, 4, 14, 6,
56 3, 11, 1, 9,
57 15, 7, 13, 5
58 };
59
60 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
61 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, DITHER_KERNEL_SIZE, DITHER_KERNEL_SIZE, 0,
62 GL_ALPHA, GL_UNSIGNED_BYTE, &pattern);
63 }
Romain Guy211efea2012-07-31 21:16:07 -070064
65 mInitialized = true;
66 } else {
67 glBindTexture(GL_TEXTURE_2D, mDitherTexture);
68 }
69}
70
71void Dither::clear() {
72 if (mInitialized) {
73 glDeleteTextures(1, &mDitherTexture);
74 }
75}
76
77///////////////////////////////////////////////////////////////////////////////
78// Program management
79///////////////////////////////////////////////////////////////////////////////
80
81void Dither::setupProgram(Program* program, GLuint* textureUnit) {
82 GLuint textureSlot = (*textureUnit)++;
83 Caches::getInstance().activeTexture(textureSlot);
84
85 bindDitherTexture();
86
87 glUniform1i(program->getUniform("ditherSampler"), textureSlot);
Romain Guy211efea2012-07-31 21:16:07 -070088}
89
90}; // namespace uirenderer
91}; // namespace android