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