2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 namespace uirenderer {
23 ///////////////////////////////////////////////////////////////////////////////
25 ///////////////////////////////////////////////////////////////////////////////
27 Dither::Dither(Caches& caches)
33 void Dither::bindDitherTexture() {
35 glGenTextures(1, &mDitherTexture);
36 mCaches.textureState().bindTexture(mDitherTexture);
38 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
39 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
41 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
42 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
44 if (mCaches.extensions().hasFloatTextures()) {
45 // 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);
49 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
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);
61 const uint8_t pattern[] = {
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);
75 mCaches.textureState().bindTexture(mDitherTexture);
79 void Dither::clear() {
81 mCaches.textureState().deleteTexture(mDitherTexture);
86 ///////////////////////////////////////////////////////////////////////////////
88 ///////////////////////////////////////////////////////////////////////////////
90 void Dither::setupProgram(Program& program, GLuint* textureUnit) {
91 GLuint textureSlot = (*textureUnit)++;
92 mCaches.textureState().activateTexture(textureSlot);
96 glUniform1i(program.getUniform("ditherSampler"), textureSlot);
99 }; // namespace uirenderer
100 }; // namespace android