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 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 24 | // Lifecycle |
| 25 | /////////////////////////////////////////////////////////////////////////////// |
| 26 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 27 | Dither::Dither(Caches& caches) |
| 28 | : mCaches(caches) |
| 29 | , mInitialized(false) |
| 30 | , mDitherTexture(0) { |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 33 | void Dither::bindDitherTexture() { |
| 34 | if (!mInitialized) { |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 35 | glGenTextures(1, &mDitherTexture); |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 36 | mCaches.textureState().bindTexture(mDitherTexture); |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 37 | |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 38 | 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 Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 44 | if (mCaches.extensions().hasFloatTextures()) { |
Romain Guy | 032d47a | 2013-04-08 19:45:40 -0700 | [diff] [blame] | 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); |
| 48 | |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 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 |
| 55 | }; |
| 56 | |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 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 | |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 67 | glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, DITHER_KERNEL_SIZE, DITHER_KERNEL_SIZE, 0, |
| 68 | GL_ALPHA, GL_UNSIGNED_BYTE, &pattern); |
| 69 | } |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 70 | |
| 71 | mInitialized = true; |
| 72 | } else { |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 73 | mCaches.textureState().bindTexture(mDitherTexture); |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | void Dither::clear() { |
| 78 | if (mInitialized) { |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 79 | mCaches.textureState().deleteTexture(mDitherTexture); |
Romain Guy | 4abab93 | 2013-04-12 16:51:21 -0700 | [diff] [blame] | 80 | mInitialized = false; |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | /////////////////////////////////////////////////////////////////////////////// |
| 85 | // Program management |
| 86 | /////////////////////////////////////////////////////////////////////////////// |
| 87 | |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 88 | void Dither::setupProgram(Program& program, GLuint* textureUnit) { |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 89 | GLuint textureSlot = (*textureUnit)++; |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 90 | mCaches.textureState().activateTexture(textureSlot); |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 91 | |
| 92 | bindDitherTexture(); |
| 93 | |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 94 | glUniform1i(program.getUniform("ditherSampler"), textureSlot); |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | }; // namespace uirenderer |
| 98 | }; // namespace android |