blob: 0f02f4c0aba7677d1a2f1dfa541950f6ead98135 [file] [log] [blame]
Romain Guy8aa195d2013-06-04 18:00:09 -07001/*
2 * Copyright (C) 2013 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 */
16
Romain Guy8aa195d2013-06-04 18:00:09 -070017#include "Caches.h"
18#include "Texture.h"
John Reck9372ac32016-01-19 11:46:52 -080019#include "utils/GLUtils.h"
John Reck38e0c322015-11-10 12:19:17 -080020#include "utils/TraceUtils.h"
21
22#include <utils/Log.h>
23
24#include <SkCanvas.h>
Romain Guy8aa195d2013-06-04 18:00:09 -070025
26namespace android {
27namespace uirenderer {
28
John Reck38e0c322015-11-10 12:19:17 -080029static int bytesPerPixel(GLint glFormat) {
30 switch (glFormat) {
31 case GL_ALPHA:
32 return 1;
33 case GL_RGB:
34 return 3;
35 case GL_RGBA:
36 default:
37 return 4;
38 }
39}
40
Romain Guy8aa195d2013-06-04 18:00:09 -070041void Texture::setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture, bool force,
42 GLenum renderTarget) {
43
44 if (mFirstWrap || force || wrapS != mWrapS || wrapT != mWrapT) {
45 mFirstWrap = false;
46
47 mWrapS = wrapS;
48 mWrapT = wrapT;
49
50 if (bindTexture) {
John Reck38e0c322015-11-10 12:19:17 -080051 mCaches.textureState().bindTexture(renderTarget, mId);
Romain Guy8aa195d2013-06-04 18:00:09 -070052 }
53
54 glTexParameteri(renderTarget, GL_TEXTURE_WRAP_S, wrapS);
55 glTexParameteri(renderTarget, GL_TEXTURE_WRAP_T, wrapT);
56 }
57}
58
59void Texture::setFilterMinMag(GLenum min, GLenum mag, bool bindTexture, bool force,
60 GLenum renderTarget) {
61
62 if (mFirstFilter || force || min != mMinFilter || mag != mMagFilter) {
63 mFirstFilter = false;
64
65 mMinFilter = min;
66 mMagFilter = mag;
67
68 if (bindTexture) {
John Reck38e0c322015-11-10 12:19:17 -080069 mCaches.textureState().bindTexture(renderTarget, mId);
Romain Guy8aa195d2013-06-04 18:00:09 -070070 }
71
72 if (mipMap && min == GL_LINEAR) min = GL_LINEAR_MIPMAP_LINEAR;
73
74 glTexParameteri(renderTarget, GL_TEXTURE_MIN_FILTER, min);
75 glTexParameteri(renderTarget, GL_TEXTURE_MAG_FILTER, mag);
76 }
77}
78
John Reck38e0c322015-11-10 12:19:17 -080079void Texture::deleteTexture() {
80 mCaches.textureState().deleteTexture(mId);
81 mId = 0;
82}
83
84bool Texture::updateSize(uint32_t width, uint32_t height, GLint format) {
85 if (mWidth == width && mHeight == height && mFormat == format) {
86 return false;
87 }
88 mWidth = width;
89 mHeight = height;
90 mFormat = format;
91 notifySizeChanged(mWidth * mHeight * bytesPerPixel(mFormat));
92 return true;
93}
94
95void Texture::upload(GLint internalformat, uint32_t width, uint32_t height,
96 GLenum format, GLenum type, const void* pixels) {
John Reck9372ac32016-01-19 11:46:52 -080097 GL_CHECKPOINT();
John Reck66f65cb2016-01-21 09:08:42 -080098 bool needsAlloc = updateSize(width, height, internalformat);
John Reck38e0c322015-11-10 12:19:17 -080099 if (!mId) {
100 glGenTextures(1, &mId);
101 needsAlloc = true;
102 }
103 mCaches.textureState().bindTexture(GL_TEXTURE_2D, mId);
104 if (needsAlloc) {
105 glTexImage2D(GL_TEXTURE_2D, 0, mFormat, mWidth, mHeight, 0,
106 format, type, pixels);
John Reck66f65cb2016-01-21 09:08:42 -0800107 } else if (pixels) {
John Reck38e0c322015-11-10 12:19:17 -0800108 glTexSubImage2D(GL_TEXTURE_2D, 0, mFormat, mWidth, mHeight, 0,
109 format, type, pixels);
110 }
John Reck66f65cb2016-01-21 09:08:42 -0800111 GL_CHECKPOINT();
John Reck38e0c322015-11-10 12:19:17 -0800112}
113
114static void uploadToTexture(bool resize, GLenum format, GLenum type, GLsizei stride, GLsizei bpp,
115 GLsizei width, GLsizei height, const GLvoid * data) {
116
John Reck38e0c322015-11-10 12:19:17 -0800117 const bool useStride = stride != width
118 && Caches::getInstance().extensions().hasUnpackRowLength();
119 if ((stride == width) || useStride) {
120 if (useStride) {
121 glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);
122 }
123
124 if (resize) {
125 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data);
126 } else {
127 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
128 }
129
130 if (useStride) {
131 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
132 }
133 } else {
134 // With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer
135 // if the stride doesn't match the width
136
137 GLvoid * temp = (GLvoid *) malloc(width * height * bpp);
138 if (!temp) return;
139
140 uint8_t * pDst = (uint8_t *)temp;
141 uint8_t * pSrc = (uint8_t *)data;
142 for (GLsizei i = 0; i < height; i++) {
143 memcpy(pDst, pSrc, width * bpp);
144 pDst += width * bpp;
145 pSrc += stride * bpp;
146 }
147
148 if (resize) {
149 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, temp);
150 } else {
151 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, temp);
152 }
153
154 free(temp);
155 }
156}
157
158static void uploadSkBitmapToTexture(const SkBitmap& bitmap,
159 bool resize, GLenum format, GLenum type) {
160 uploadToTexture(resize, format, type, bitmap.rowBytesAsPixels(), bitmap.bytesPerPixel(),
161 bitmap.width(), bitmap.height(), bitmap.getPixels());
162}
163
164static void colorTypeToGlFormatAndType(SkColorType colorType,
165 GLint* outFormat, GLint* outType) {
166 switch (colorType) {
167 case kAlpha_8_SkColorType:
168 *outFormat = GL_ALPHA;
169 *outType = GL_UNSIGNED_BYTE;
170 break;
171 case kRGB_565_SkColorType:
172 *outFormat = GL_RGB;
173 *outType = GL_UNSIGNED_SHORT_5_6_5;
174 break;
175 // ARGB_4444 and Index_8 are both upconverted to RGBA_8888
176 case kARGB_4444_SkColorType:
177 case kIndex_8_SkColorType:
178 case kN32_SkColorType:
179 *outFormat = GL_RGBA;
180 *outType = GL_UNSIGNED_BYTE;
181 break;
Derek Sollenberger88d842f2016-01-20 10:37:30 -0500182 case kGray_8_SkColorType:
183 *outFormat = GL_LUMINANCE;
184 *outType = GL_UNSIGNED_BYTE;
185 break;
John Reck38e0c322015-11-10 12:19:17 -0800186 default:
187 LOG_ALWAYS_FATAL("Unsupported bitmap colorType: %d", colorType);
188 break;
189 }
190}
191
192void Texture::upload(const SkBitmap& bitmap) {
193 SkAutoLockPixels alp(bitmap);
194
195 if (!bitmap.readyToDraw()) {
196 ALOGE("Cannot generate texture from bitmap");
197 return;
198 }
199
200 ATRACE_FORMAT("Upload %ux%u Texture", bitmap.width(), bitmap.height());
201
202 // We could also enable mipmapping if both bitmap dimensions are powers
203 // of 2 but we'd have to deal with size changes. Let's keep this simple
204 const bool canMipMap = mCaches.extensions().hasNPot();
205
206 // If the texture had mipmap enabled but not anymore,
207 // force a glTexImage2D to discard the mipmap levels
208 bool needsAlloc = canMipMap && mipMap && !bitmap.hasHardwareMipMap();
209
210 if (!mId) {
211 glGenTextures(1, &mId);
212 needsAlloc = true;
213 }
214
215 GLint format, type;
216 colorTypeToGlFormatAndType(bitmap.colorType(), &format, &type);
217
218 if (updateSize(bitmap.width(), bitmap.height(), format)) {
219 needsAlloc = true;
220 }
221
222 blend = !bitmap.isOpaque();
223 mCaches.textureState().bindTexture(mId);
224
225 if (CC_UNLIKELY(bitmap.colorType() == kARGB_4444_SkColorType
226 || bitmap.colorType() == kIndex_8_SkColorType)) {
227 SkBitmap rgbaBitmap;
228 rgbaBitmap.allocPixels(SkImageInfo::MakeN32(mWidth, mHeight,
229 bitmap.alphaType()));
230 rgbaBitmap.eraseColor(0);
231
232 SkCanvas canvas(rgbaBitmap);
233 canvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr);
234
235 uploadSkBitmapToTexture(rgbaBitmap, needsAlloc, format, type);
236 } else {
237 uploadSkBitmapToTexture(bitmap, needsAlloc, format, type);
238 }
239
240 if (canMipMap) {
241 mipMap = bitmap.hasHardwareMipMap();
242 if (mipMap) {
243 glGenerateMipmap(GL_TEXTURE_2D);
244 }
245 }
246
247 if (mFirstFilter) {
248 setFilter(GL_NEAREST);
249 }
250
251 if (mFirstWrap) {
252 setWrap(GL_CLAMP_TO_EDGE);
253 }
254}
255
256void Texture::wrap(GLuint id, uint32_t width, uint32_t height, GLint format) {
257 mId = id;
258 mWidth = width;
259 mHeight = height;
260 mFormat = format;
261 // We're wrapping an existing texture, so don't double count this memory
262 notifySizeChanged(0);
Romain Guybe1b1272013-06-06 14:02:54 -0700263}
264
Romain Guy8aa195d2013-06-04 18:00:09 -0700265}; // namespace uirenderer
266}; // namespace android