Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #include <renderstate/TextureState.h> |
| 17 | |
| 18 | namespace android { |
| 19 | namespace uirenderer { |
| 20 | |
| 21 | // Must define as many texture units as specified by kTextureUnitsCount |
| 22 | const GLenum kTextureUnits[] = { |
| 23 | GL_TEXTURE0, |
| 24 | GL_TEXTURE1, |
| 25 | GL_TEXTURE2 |
| 26 | }; |
| 27 | |
| 28 | TextureState::TextureState() |
| 29 | : mTextureUnit(0) { |
| 30 | glActiveTexture(kTextureUnits[0]); |
| 31 | resetBoundTextures(); |
| 32 | |
| 33 | GLint maxTextureUnits; |
| 34 | glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); |
| 35 | LOG_ALWAYS_FATAL_IF(maxTextureUnits < kTextureUnitsCount, |
| 36 | "At least %d texture units are required!", kTextureUnitsCount); |
| 37 | } |
| 38 | |
| 39 | void TextureState::activateTexture(GLuint textureUnit) { |
| 40 | if (mTextureUnit != textureUnit) { |
| 41 | glActiveTexture(kTextureUnits[textureUnit]); |
| 42 | mTextureUnit = textureUnit; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void TextureState::resetActiveTexture() { |
| 47 | mTextureUnit = -1; |
| 48 | } |
| 49 | |
| 50 | void TextureState::bindTexture(GLuint texture) { |
| 51 | if (mBoundTextures[mTextureUnit] != texture) { |
| 52 | glBindTexture(GL_TEXTURE_2D, texture); |
| 53 | mBoundTextures[mTextureUnit] = texture; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void TextureState::bindTexture(GLenum target, GLuint texture) { |
| 58 | if (target == GL_TEXTURE_2D) { |
| 59 | bindTexture(texture); |
| 60 | } else { |
| 61 | // GLConsumer directly calls glBindTexture() with |
| 62 | // target=GL_TEXTURE_EXTERNAL_OES, don't cache this target |
| 63 | // since the cached state could be stale |
| 64 | glBindTexture(target, texture); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void TextureState::deleteTexture(GLuint texture) { |
| 69 | // When glDeleteTextures() is called on a currently bound texture, |
| 70 | // OpenGL ES specifies that the texture is then considered unbound |
| 71 | // Consider the following series of calls: |
| 72 | // |
| 73 | // glGenTextures -> creates texture name 2 |
| 74 | // glBindTexture(2) |
| 75 | // glDeleteTextures(2) -> 2 is now unbound |
| 76 | // glGenTextures -> can return 2 again |
| 77 | // |
| 78 | // If we don't call glBindTexture(2) after the second glGenTextures |
| 79 | // call, any texture operation will be performed on the default |
| 80 | // texture (name=0) |
| 81 | |
| 82 | unbindTexture(texture); |
| 83 | |
| 84 | glDeleteTextures(1, &texture); |
| 85 | } |
| 86 | |
| 87 | void TextureState::resetBoundTextures() { |
| 88 | for (int i = 0; i < kTextureUnitsCount; i++) { |
| 89 | mBoundTextures[i] = 0; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void TextureState::unbindTexture(GLuint texture) { |
| 94 | for (int i = 0; i < kTextureUnitsCount; i++) { |
| 95 | if (mBoundTextures[i] == texture) { |
| 96 | mBoundTextures[i] = 0; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | } /* namespace uirenderer */ |
| 102 | } /* namespace android */ |
| 103 | |