Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 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 | // Defines |
| 25 | /////////////////////////////////////////////////////////////////////////////// |
| 26 | |
| 27 | // Must be a power of two |
| 28 | #define DITHER_KERNEL_SIZE 4 |
| 29 | |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | // Lifecycle |
| 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | |
| 34 | void 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 | |
| 63 | void Dither::clear() { |
| 64 | if (mInitialized) { |
| 65 | glDeleteTextures(1, &mDitherTexture); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /////////////////////////////////////////////////////////////////////////////// |
| 70 | // Program management |
| 71 | /////////////////////////////////////////////////////////////////////////////// |
| 72 | |
| 73 | void Dither::setupProgram(Program* program, GLuint* textureUnit) { |
| 74 | GLuint textureSlot = (*textureUnit)++; |
| 75 | Caches::getInstance().activeTexture(textureSlot); |
| 76 | |
| 77 | bindDitherTexture(); |
| 78 | |
Chet Haase | a1d12dd | 2012-09-21 14:50:14 -0700 | [diff] [blame] | 79 | float ditherSize = 1.0f / DITHER_KERNEL_SIZE; |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 80 | glUniform1i(program->getUniform("ditherSampler"), textureSlot); |
Chet Haase | a1d12dd | 2012-09-21 14:50:14 -0700 | [diff] [blame] | 81 | glUniform1f(program->getUniform("ditherSize"), ditherSize); |
| 82 | glUniform1f(program->getUniform("ditherSizeSquared"), ditherSize * ditherSize); |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | }; // namespace uirenderer |
| 86 | }; // namespace android |