blob: 987d4cd55a5ec1c0b5b11445b7a2fe7acc54db32 [file] [log] [blame]
Chris Craik44eb2c02015-01-29 09:45:09 -08001/*
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 */
Chris Craikf27133d2015-02-19 09:51:53 -080016#include "renderstate/TextureState.h"
Chris Craik44eb2c02015-01-29 09:45:09 -080017
18namespace android {
19namespace uirenderer {
20
21// Must define as many texture units as specified by kTextureUnitsCount
22const GLenum kTextureUnits[] = {
23 GL_TEXTURE0,
24 GL_TEXTURE1,
Chris Craike310f832015-07-13 13:34:07 -070025 GL_TEXTURE2,
26 GL_TEXTURE3
Chris Craik44eb2c02015-01-29 09:45:09 -080027};
28
29TextureState::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,
Chris Craike310f832015-07-13 13:34:07 -070037 "At least %d texture units are required!", kTextureUnitsCount);
Chris Craik44eb2c02015-01-29 09:45:09 -080038}
39
40void TextureState::activateTexture(GLuint textureUnit) {
Chris Craike310f832015-07-13 13:34:07 -070041 LOG_ALWAYS_FATAL_IF(textureUnit >= kTextureUnitsCount,
42 "Tried to use texture unit index %d, only %d exist",
43 textureUnit, kTextureUnitsCount);
Chris Craik44eb2c02015-01-29 09:45:09 -080044 if (mTextureUnit != textureUnit) {
45 glActiveTexture(kTextureUnits[textureUnit]);
46 mTextureUnit = textureUnit;
47 }
48}
49
50void TextureState::resetActiveTexture() {
51 mTextureUnit = -1;
52}
53
54void TextureState::bindTexture(GLuint texture) {
55 if (mBoundTextures[mTextureUnit] != texture) {
56 glBindTexture(GL_TEXTURE_2D, texture);
57 mBoundTextures[mTextureUnit] = texture;
58 }
59}
60
61void 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
72void 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
91void TextureState::resetBoundTextures() {
92 for (int i = 0; i < kTextureUnitsCount; i++) {
93 mBoundTextures[i] = 0;
94 }
95}
96
97void 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