blob: 78b8edae2eed1388533ff0f5336afaac3f7e4224 [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);
John Reck2de77712016-01-20 11:09:53 -080046 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Chris Craik44eb2c02015-01-29 09:45:09 -080047}
48
49void TextureState::activateTexture(GLuint textureUnit) {
Chris Craike310f832015-07-13 13:34:07 -070050 LOG_ALWAYS_FATAL_IF(textureUnit >= kTextureUnitsCount,
51 "Tried to use texture unit index %d, only %d exist",
52 textureUnit, kTextureUnitsCount);
Chris Craik44eb2c02015-01-29 09:45:09 -080053 if (mTextureUnit != textureUnit) {
54 glActiveTexture(kTextureUnits[textureUnit]);
55 mTextureUnit = textureUnit;
56 }
57}
58
59void TextureState::resetActiveTexture() {
60 mTextureUnit = -1;
61}
62
63void TextureState::bindTexture(GLuint texture) {
64 if (mBoundTextures[mTextureUnit] != texture) {
65 glBindTexture(GL_TEXTURE_2D, texture);
66 mBoundTextures[mTextureUnit] = texture;
67 }
68}
69
70void TextureState::bindTexture(GLenum target, GLuint texture) {
71 if (target == GL_TEXTURE_2D) {
72 bindTexture(texture);
73 } else {
74 // GLConsumer directly calls glBindTexture() with
75 // target=GL_TEXTURE_EXTERNAL_OES, don't cache this target
76 // since the cached state could be stale
77 glBindTexture(target, texture);
78 }
79}
80
81void TextureState::deleteTexture(GLuint texture) {
82 // When glDeleteTextures() is called on a currently bound texture,
83 // OpenGL ES specifies that the texture is then considered unbound
84 // Consider the following series of calls:
85 //
86 // glGenTextures -> creates texture name 2
87 // glBindTexture(2)
88 // glDeleteTextures(2) -> 2 is now unbound
89 // glGenTextures -> can return 2 again
90 //
91 // If we don't call glBindTexture(2) after the second glGenTextures
92 // call, any texture operation will be performed on the default
93 // texture (name=0)
94
95 unbindTexture(texture);
96
97 glDeleteTextures(1, &texture);
98}
99
100void TextureState::resetBoundTextures() {
101 for (int i = 0; i < kTextureUnitsCount; i++) {
102 mBoundTextures[i] = 0;
103 }
104}
105
106void TextureState::unbindTexture(GLuint texture) {
107 for (int i = 0; i < kTextureUnitsCount; i++) {
108 if (mBoundTextures[i] == texture) {
109 mBoundTextures[i] = 0;
110 }
111 }
112}
113
114} /* namespace uirenderer */
115} /* namespace android */
116