1 /* 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 20 namespace android { 21 namespace uirenderer { 22 23 /////////////////////////////////////////////////////////////////////////////// 24 // Lifecycle 25 /////////////////////////////////////////////////////////////////////////////// 26 27 Dither::Dither(): mCaches(NULL), mInitialized(false), mDitherTexture(0) { 28 } 29 30 void Dither::bindDitherTexture() { 31 if (!mInitialized) { 32 bool useFloatTexture = Extensions::getInstance().hasFloatTextures(); 33 34 glGenTextures(1, &mDitherTexture); 35 mCaches->bindTexture(mDitherTexture); 36 37 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 38 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 39 40 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 41 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 42 43 if (useFloatTexture) { 44 // We use a R16F texture, let's remap the alpha channel to the 45 // red channel to avoid changing the shader sampling code on GL ES 3.0+ 46 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_RED); 47 48 float dither = 1.0f / (255.0f * DITHER_KERNEL_SIZE * DITHER_KERNEL_SIZE); 49 const GLfloat pattern[] = { 50 0 * dither, 8 * dither, 2 * dither, 10 * dither, 51 12 * dither, 4 * dither, 14 * dither, 6 * dither, 52 3 * dither, 11 * dither, 1 * dither, 9 * dither, 53 15 * dither, 7 * dither, 13 * dither, 5 * dither 54 }; 55 56 glPixelStorei(GL_UNPACK_ALIGNMENT, sizeof(GLfloat)); 57 glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, DITHER_KERNEL_SIZE, DITHER_KERNEL_SIZE, 0, 58 GL_RED, GL_FLOAT, &pattern); 59 } else { 60 const uint8_t pattern[] = { 61 0, 8, 2, 10, 62 12, 4, 14, 6, 63 3, 11, 1, 9, 64 15, 7, 13, 5 65 }; 66 67 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 68 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, DITHER_KERNEL_SIZE, DITHER_KERNEL_SIZE, 0, 69 GL_ALPHA, GL_UNSIGNED_BYTE, &pattern); 70 } 71 72 mInitialized = true; 73 } else { 74 mCaches->bindTexture(mDitherTexture); 75 } 76 } 77 78 void Dither::clear() { 79 if (mInitialized) { 80 mCaches->deleteTexture(mDitherTexture); 81 mInitialized = false; 82 } 83 } 84 85 /////////////////////////////////////////////////////////////////////////////// 86 // Program management 87 /////////////////////////////////////////////////////////////////////////////// 88 89 void Dither::setupProgram(Program* program, GLuint* textureUnit) { 90 if (!mCaches) mCaches = &Caches::getInstance(); 91 92 GLuint textureSlot = (*textureUnit)++; 93 mCaches->activeTexture(textureSlot); 94 95 bindDitherTexture(); 96 97 glUniform1i(program->getUniform("ditherSampler"), textureSlot); 98 } 99 100 }; // namespace uirenderer 101 }; // namespace android 102