blob: 1f50f712c267c2f5071bb2d0b5555aa5c345c1ea [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
Chris Craik68f5b8a2015-09-09 13:23:09 -070037static void uploadToTexture(bool resize, GLenum format, GLenum type, GLsizei stride, GLsizei bpp,
38 GLsizei width, GLsizei height, const GLvoid * data) {
39
40 glPixelStorei(GL_UNPACK_ALIGNMENT, bpp);
41 const bool useStride = stride != width
42 && Caches::getInstance().extensions().hasUnpackRowLength();
43 if ((stride == width) || useStride) {
44 if (useStride) {
45 glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);
46 }
47
48 if (resize) {
49 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data);
50 } else {
51 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
52 }
53
54 if (useStride) {
55 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
56 }
57 } else {
58 // With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer
59 // if the stride doesn't match the width
60
61 GLvoid * temp = (GLvoid *) malloc(width * height * bpp);
62 if (!temp) return;
63
64 uint8_t * pDst = (uint8_t *)temp;
65 uint8_t * pSrc = (uint8_t *)data;
66 for (GLsizei i = 0; i < height; i++) {
67 memcpy(pDst, pSrc, width * bpp);
68 pDst += width * bpp;
69 pSrc += stride * bpp;
70 }
71
72 if (resize) {
73 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, temp);
74 } else {
75 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, temp);
76 }
77
78 free(temp);
79 }
80}
81
82static void uploadSkBitmapToTexture(const SkBitmap& bitmap,
83 bool resize, GLenum format, GLenum type) {
84 uploadToTexture(resize, format, type, bitmap.rowBytesAsPixels(), bitmap.bytesPerPixel(),
85 bitmap.width(), bitmap.height(), bitmap.getPixels());
86}
87
88void TextureState::generateTexture(const SkBitmap* bitmap, Texture* texture, bool regenerate) {
89 SkAutoLockPixels alp(*bitmap);
90
91 if (!bitmap->readyToDraw()) {
92 ALOGE("Cannot generate texture from bitmap");
93 return;
94 }
95
96 ATRACE_FORMAT("Upload %ux%u Texture", bitmap->width(), bitmap->height());
97
98 // We could also enable mipmapping if both bitmap dimensions are powers
99 // of 2 but we'd have to deal with size changes. Let's keep this simple
100 const bool canMipMap = Caches::getInstance().extensions().hasNPot();
101
102 // If the texture had mipmap enabled but not anymore,
103 // force a glTexImage2D to discard the mipmap levels
104 const bool resize = !regenerate || bitmap->width() != int(texture->width) ||
105 bitmap->height() != int(texture->height) ||
106 (regenerate && canMipMap && texture->mipMap && !bitmap->hasHardwareMipMap());
107
108 if (!regenerate) {
109 glGenTextures(1, &texture->id);
110 }
111
112 texture->generation = bitmap->getGenerationID();
113 texture->width = bitmap->width();
114 texture->height = bitmap->height();
115
116 bindTexture(texture->id);
117
118 switch (bitmap->colorType()) {
119 case kAlpha_8_SkColorType:
120 uploadSkBitmapToTexture(*bitmap, resize, GL_ALPHA, GL_UNSIGNED_BYTE);
121 texture->blend = true;
122 break;
123 case kRGB_565_SkColorType:
124 uploadSkBitmapToTexture(*bitmap, resize, GL_RGB, GL_UNSIGNED_SHORT_5_6_5);
125 texture->blend = false;
126 break;
127 case kN32_SkColorType:
128 uploadSkBitmapToTexture(*bitmap, resize, GL_RGBA, GL_UNSIGNED_BYTE);
129 // Do this after calling getPixels() to make sure Skia's deferred
130 // decoding happened
131 texture->blend = !bitmap->isOpaque();
132 break;
133 case kARGB_4444_SkColorType:
134 case kIndex_8_SkColorType: {
135 SkBitmap rgbaBitmap;
136 rgbaBitmap.allocPixels(SkImageInfo::MakeN32(texture->width, texture->height,
137 bitmap->alphaType()));
138 rgbaBitmap.eraseColor(0);
139
140 SkCanvas canvas(rgbaBitmap);
141 canvas.drawBitmap(*bitmap, 0.0f, 0.0f, nullptr);
142
143 uploadSkBitmapToTexture(rgbaBitmap, resize, GL_RGBA, GL_UNSIGNED_BYTE);
144 texture->blend = !bitmap->isOpaque();
145 break;
146 }
147 default:
148 ALOGW("Unsupported bitmap colorType: %d", bitmap->colorType());
149 break;
150 }
151
152 if (canMipMap) {
153 texture->mipMap = bitmap->hasHardwareMipMap();
154 if (texture->mipMap) {
155 glGenerateMipmap(GL_TEXTURE_2D);
156 }
157 }
158
159 if (!regenerate) {
160 texture->setFilter(GL_NEAREST);
161 texture->setWrap(GL_CLAMP_TO_EDGE);
162 }
163}
164
Chris Craik44eb2c02015-01-29 09:45:09 -0800165TextureState::TextureState()
166 : mTextureUnit(0) {
167 glActiveTexture(kTextureUnits[0]);
168 resetBoundTextures();
169
170 GLint maxTextureUnits;
171 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
172 LOG_ALWAYS_FATAL_IF(maxTextureUnits < kTextureUnitsCount,
Chris Craike310f832015-07-13 13:34:07 -0700173 "At least %d texture units are required!", kTextureUnitsCount);
Chris Craik44eb2c02015-01-29 09:45:09 -0800174}
175
176void TextureState::activateTexture(GLuint textureUnit) {
Chris Craike310f832015-07-13 13:34:07 -0700177 LOG_ALWAYS_FATAL_IF(textureUnit >= kTextureUnitsCount,
178 "Tried to use texture unit index %d, only %d exist",
179 textureUnit, kTextureUnitsCount);
Chris Craik44eb2c02015-01-29 09:45:09 -0800180 if (mTextureUnit != textureUnit) {
181 glActiveTexture(kTextureUnits[textureUnit]);
182 mTextureUnit = textureUnit;
183 }
184}
185
186void TextureState::resetActiveTexture() {
187 mTextureUnit = -1;
188}
189
190void TextureState::bindTexture(GLuint texture) {
191 if (mBoundTextures[mTextureUnit] != texture) {
192 glBindTexture(GL_TEXTURE_2D, texture);
193 mBoundTextures[mTextureUnit] = texture;
194 }
195}
196
197void TextureState::bindTexture(GLenum target, GLuint texture) {
198 if (target == GL_TEXTURE_2D) {
199 bindTexture(texture);
200 } else {
201 // GLConsumer directly calls glBindTexture() with
202 // target=GL_TEXTURE_EXTERNAL_OES, don't cache this target
203 // since the cached state could be stale
204 glBindTexture(target, texture);
205 }
206}
207
208void TextureState::deleteTexture(GLuint texture) {
209 // When glDeleteTextures() is called on a currently bound texture,
210 // OpenGL ES specifies that the texture is then considered unbound
211 // Consider the following series of calls:
212 //
213 // glGenTextures -> creates texture name 2
214 // glBindTexture(2)
215 // glDeleteTextures(2) -> 2 is now unbound
216 // glGenTextures -> can return 2 again
217 //
218 // If we don't call glBindTexture(2) after the second glGenTextures
219 // call, any texture operation will be performed on the default
220 // texture (name=0)
221
222 unbindTexture(texture);
223
224 glDeleteTextures(1, &texture);
225}
226
227void TextureState::resetBoundTextures() {
228 for (int i = 0; i < kTextureUnitsCount; i++) {
229 mBoundTextures[i] = 0;
230 }
231}
232
233void TextureState::unbindTexture(GLuint texture) {
234 for (int i = 0; i < kTextureUnitsCount; i++) {
235 if (mBoundTextures[i] == texture) {
236 mBoundTextures[i] = 0;
237 }
238 }
239}
240
241} /* namespace uirenderer */
242} /* namespace android */
243