blob: f9a1f8c3889515e6d92a10c0e78fe71b8c4d7d5b [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
Chris Craik138c21f2016-04-28 16:59:42 -070029// Width of mShadowLutTexture, defines how accurate the shadow alpha lookup table is
30static const int SHADOW_LUT_SIZE = 128;
31
Chris Craik44eb2c02015-01-29 09:45:09 -080032// Must define as many texture units as specified by kTextureUnitsCount
33const GLenum kTextureUnits[] = {
34 GL_TEXTURE0,
35 GL_TEXTURE1,
Chris Craike310f832015-07-13 13:34:07 -070036 GL_TEXTURE2,
37 GL_TEXTURE3
Chris Craik44eb2c02015-01-29 09:45:09 -080038};
39
40TextureState::TextureState()
41 : mTextureUnit(0) {
42 glActiveTexture(kTextureUnits[0]);
43 resetBoundTextures();
44
45 GLint maxTextureUnits;
46 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
47 LOG_ALWAYS_FATAL_IF(maxTextureUnits < kTextureUnitsCount,
Chris Craike310f832015-07-13 13:34:07 -070048 "At least %d texture units are required!", kTextureUnitsCount);
John Reck2de77712016-01-20 11:09:53 -080049 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Chris Craik44eb2c02015-01-29 09:45:09 -080050}
51
Chris Craik138c21f2016-04-28 16:59:42 -070052TextureState::~TextureState() {
53 if (mShadowLutTexture != nullptr) {
54 mShadowLutTexture->deleteTexture();
55 }
56}
57
58/**
59 * Maps shadow geometry 'alpha' varying (1 for darkest, 0 for transparent) to
60 * darkness at that spot. Input values of 0->1 should be mapped within the same
61 * range, but can affect the curve for a different visual falloff.
62 *
63 * This is used to populate the shadow LUT texture for quick lookup in the
64 * shadow shader.
65 */
66static float computeShadowOpacity(float ratio) {
67 // exponential falloff function provided by UX
68 float val = 1 - ratio;
69 return exp(-val * val * 4.0) - 0.018;
70}
71
72void TextureState::constructTexture(Caches& caches) {
73 if (mShadowLutTexture == nullptr) {
74 mShadowLutTexture.reset(new Texture(caches));
75
76 unsigned char bytes[SHADOW_LUT_SIZE];
77 for (int i = 0; i < SHADOW_LUT_SIZE; i++) {
78 float inputRatio = i / (SHADOW_LUT_SIZE - 1.0f);
79 bytes[i] = computeShadowOpacity(inputRatio) * 255;
80 }
81 mShadowLutTexture->upload(GL_ALPHA, SHADOW_LUT_SIZE, 1, GL_ALPHA, GL_UNSIGNED_BYTE, &bytes);
82 mShadowLutTexture->setFilter(GL_LINEAR);
83 mShadowLutTexture->setWrap(GL_CLAMP_TO_EDGE);
84 }
85}
86
Chris Craik44eb2c02015-01-29 09:45:09 -080087void TextureState::activateTexture(GLuint textureUnit) {
Chris Craike310f832015-07-13 13:34:07 -070088 LOG_ALWAYS_FATAL_IF(textureUnit >= kTextureUnitsCount,
89 "Tried to use texture unit index %d, only %d exist",
90 textureUnit, kTextureUnitsCount);
Chris Craik44eb2c02015-01-29 09:45:09 -080091 if (mTextureUnit != textureUnit) {
92 glActiveTexture(kTextureUnits[textureUnit]);
93 mTextureUnit = textureUnit;
94 }
95}
96
97void TextureState::resetActiveTexture() {
98 mTextureUnit = -1;
99}
100
101void TextureState::bindTexture(GLuint texture) {
102 if (mBoundTextures[mTextureUnit] != texture) {
103 glBindTexture(GL_TEXTURE_2D, texture);
104 mBoundTextures[mTextureUnit] = texture;
105 }
106}
107
108void TextureState::bindTexture(GLenum target, GLuint texture) {
109 if (target == GL_TEXTURE_2D) {
110 bindTexture(texture);
111 } else {
112 // GLConsumer directly calls glBindTexture() with
113 // target=GL_TEXTURE_EXTERNAL_OES, don't cache this target
114 // since the cached state could be stale
115 glBindTexture(target, texture);
116 }
117}
118
119void TextureState::deleteTexture(GLuint texture) {
120 // When glDeleteTextures() is called on a currently bound texture,
121 // OpenGL ES specifies that the texture is then considered unbound
122 // Consider the following series of calls:
123 //
124 // glGenTextures -> creates texture name 2
125 // glBindTexture(2)
126 // glDeleteTextures(2) -> 2 is now unbound
127 // glGenTextures -> can return 2 again
128 //
129 // If we don't call glBindTexture(2) after the second glGenTextures
130 // call, any texture operation will be performed on the default
131 // texture (name=0)
132
133 unbindTexture(texture);
134
135 glDeleteTextures(1, &texture);
136}
137
138void TextureState::resetBoundTextures() {
139 for (int i = 0; i < kTextureUnitsCount; i++) {
140 mBoundTextures[i] = 0;
141 }
142}
143
144void TextureState::unbindTexture(GLuint texture) {
145 for (int i = 0; i < kTextureUnitsCount; i++) {
146 if (mBoundTextures[i] == texture) {
147 mBoundTextures[i] = 0;
148 }
149 }
150}
151
152} /* namespace uirenderer */
153} /* namespace android */
154