blob: 26ebdee06c0830d6c5ddcab051d27f5871377789 [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
Chris Craik68f5b8a2015-09-09 13:23:09 -070018#include "Caches.h"
19#include "utils/TraceUtils.h"
20
21#include <GLES3/gl3.h>
22#include <memory>
23#include <SkCanvas.h>
24#include <SkBitmap.h>
25
Chris Craik44eb2c02015-01-29 09:45:09 -080026namespace android {
27namespace uirenderer {
28
29// Must define as many texture units as specified by kTextureUnitsCount
30const GLenum kTextureUnits[] = {
31 GL_TEXTURE0,
32 GL_TEXTURE1,
Chris Craike310f832015-07-13 13:34:07 -070033 GL_TEXTURE2,
34 GL_TEXTURE3
Chris Craik44eb2c02015-01-29 09:45:09 -080035};
36
37TextureState::TextureState()
38 : mTextureUnit(0) {
39 glActiveTexture(kTextureUnits[0]);
40 resetBoundTextures();
41
42 GLint maxTextureUnits;
43 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
44 LOG_ALWAYS_FATAL_IF(maxTextureUnits < kTextureUnitsCount,
Chris Craike310f832015-07-13 13:34:07 -070045 "At least %d texture units are required!", kTextureUnitsCount);
Chris Craik44eb2c02015-01-29 09:45:09 -080046}
47
48void TextureState::activateTexture(GLuint textureUnit) {
Chris Craike310f832015-07-13 13:34:07 -070049 LOG_ALWAYS_FATAL_IF(textureUnit >= kTextureUnitsCount,
50 "Tried to use texture unit index %d, only %d exist",
51 textureUnit, kTextureUnitsCount);
Chris Craik44eb2c02015-01-29 09:45:09 -080052 if (mTextureUnit != textureUnit) {
53 glActiveTexture(kTextureUnits[textureUnit]);
54 mTextureUnit = textureUnit;
55 }
56}
57
58void TextureState::resetActiveTexture() {
59 mTextureUnit = -1;
60}
61
62void TextureState::bindTexture(GLuint texture) {
63 if (mBoundTextures[mTextureUnit] != texture) {
64 glBindTexture(GL_TEXTURE_2D, texture);
65 mBoundTextures[mTextureUnit] = texture;
66 }
67}
68
69void TextureState::bindTexture(GLenum target, GLuint texture) {
70 if (target == GL_TEXTURE_2D) {
71 bindTexture(texture);
72 } else {
73 // GLConsumer directly calls glBindTexture() with
74 // target=GL_TEXTURE_EXTERNAL_OES, don't cache this target
75 // since the cached state could be stale
76 glBindTexture(target, texture);
77 }
78}
79
80void TextureState::deleteTexture(GLuint texture) {
81 // When glDeleteTextures() is called on a currently bound texture,
82 // OpenGL ES specifies that the texture is then considered unbound
83 // Consider the following series of calls:
84 //
85 // glGenTextures -> creates texture name 2
86 // glBindTexture(2)
87 // glDeleteTextures(2) -> 2 is now unbound
88 // glGenTextures -> can return 2 again
89 //
90 // If we don't call glBindTexture(2) after the second glGenTextures
91 // call, any texture operation will be performed on the default
92 // texture (name=0)
93
94 unbindTexture(texture);
95
96 glDeleteTextures(1, &texture);
97}
98
99void TextureState::resetBoundTextures() {
100 for (int i = 0; i < kTextureUnitsCount; i++) {
101 mBoundTextures[i] = 0;
102 }
103}
104
105void TextureState::unbindTexture(GLuint texture) {
106 for (int i = 0; i < kTextureUnitsCount; i++) {
107 if (mBoundTextures[i] == texture) {
108 mBoundTextures[i] = 0;
109 }
110 }
111}
112
113} /* namespace uirenderer */
114} /* namespace android */
115