blob: 1ba651174c8ae082e5f68a69fa3fb10177225a28 [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
Chris Craik44eb2c02015-01-29 09:45:09 -080027Dither::Dither(Caches& caches)
28 : mCaches(caches)
29 , mInitialized(false)
30 , mDitherTexture(0) {
Romain Guy8aa195d2013-06-04 18:00:09 -070031}
32
Romain Guy211efea2012-07-31 21:16:07 -070033void Dither::bindDitherTexture() {
34 if (!mInitialized) {
Romain Guy211efea2012-07-31 21:16:07 -070035 glGenTextures(1, &mDitherTexture);
Chris Craik44eb2c02015-01-29 09:45:09 -080036 mCaches.textureState().bindTexture(mDitherTexture);
Romain Guy211efea2012-07-31 21:16:07 -070037
Romain Guy211efea2012-07-31 21:16:07 -070038 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
39 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
40
41 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
42 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
43
Chris Craik117bdbc2015-02-05 10:12:38 -080044 if (mCaches.extensions().hasFloatTextures()) {
Romain Guy032d47a2013-04-08 19:45:40 -070045 // We use a R16F texture, let's remap the alpha channel to the
46 // red channel to avoid changing the shader sampling code on GL ES 3.0+
47 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_RED);
48
Romain Guyb4880042013-04-05 11:17:55 -070049 float dither = 1.0f / (255.0f * DITHER_KERNEL_SIZE * DITHER_KERNEL_SIZE);
50 const GLfloat pattern[] = {
51 0 * dither, 8 * dither, 2 * dither, 10 * dither,
52 12 * dither, 4 * dither, 14 * dither, 6 * dither,
53 3 * dither, 11 * dither, 1 * dither, 9 * dither,
54 15 * dither, 7 * dither, 13 * dither, 5 * dither
55 };
56
57 glPixelStorei(GL_UNPACK_ALIGNMENT, sizeof(GLfloat));
58 glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, DITHER_KERNEL_SIZE, DITHER_KERNEL_SIZE, 0,
59 GL_RED, GL_FLOAT, &pattern);
60 } else {
61 const uint8_t pattern[] = {
62 0, 8, 2, 10,
63 12, 4, 14, 6,
64 3, 11, 1, 9,
65 15, 7, 13, 5
66 };
67
68 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
69 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, DITHER_KERNEL_SIZE, DITHER_KERNEL_SIZE, 0,
70 GL_ALPHA, GL_UNSIGNED_BYTE, &pattern);
71 }
Romain Guy211efea2012-07-31 21:16:07 -070072
73 mInitialized = true;
74 } else {
Chris Craik44eb2c02015-01-29 09:45:09 -080075 mCaches.textureState().bindTexture(mDitherTexture);
Romain Guy211efea2012-07-31 21:16:07 -070076 }
77}
78
79void Dither::clear() {
80 if (mInitialized) {
Chris Craik44eb2c02015-01-29 09:45:09 -080081 mCaches.textureState().deleteTexture(mDitherTexture);
Romain Guy4abab932013-04-12 16:51:21 -070082 mInitialized = false;
Romain Guy211efea2012-07-31 21:16:07 -070083 }
84}
85
86///////////////////////////////////////////////////////////////////////////////
87// Program management
88///////////////////////////////////////////////////////////////////////////////
89
Chris Craik6c15ffa2015-02-02 13:50:55 -080090void Dither::setupProgram(Program& program, GLuint* textureUnit) {
Romain Guy211efea2012-07-31 21:16:07 -070091 GLuint textureSlot = (*textureUnit)++;
Chris Craik44eb2c02015-01-29 09:45:09 -080092 mCaches.textureState().activateTexture(textureSlot);
Romain Guy211efea2012-07-31 21:16:07 -070093
94 bindDitherTexture();
95
Chris Craik6c15ffa2015-02-02 13:50:55 -080096 glUniform1i(program.getUniform("ditherSampler"), textureSlot);
Romain Guy211efea2012-07-31 21:16:07 -070097}
98
99}; // namespace uirenderer
100}; // namespace android