blob: 49a103c01108c8a1f0938a81f35ce5045e5fd9ea [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:
John Reck38e0c322015-11-10 12:19:17 -080036 return 4;
John Reck1d4e6a02016-02-11 13:22:25 -080037 case GL_RGBA16F:
38 return 16;
39 default:
40 LOG_ALWAYS_FATAL("UNKNOWN FORMAT %d", glFormat);
John Reck38e0c322015-11-10 12:19:17 -080041 }
42}
43
Romain Guy8aa195d2013-06-04 18:00:09 -070044void Texture::setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture, bool force,
45 GLenum renderTarget) {
46
John Reck48247a22016-01-22 10:55:32 -080047 if (force || wrapS != mWrapS || wrapT != mWrapT) {
Romain Guy8aa195d2013-06-04 18:00:09 -070048 mWrapS = wrapS;
49 mWrapT = wrapT;
50
51 if (bindTexture) {
John Reck38e0c322015-11-10 12:19:17 -080052 mCaches.textureState().bindTexture(renderTarget, mId);
Romain Guy8aa195d2013-06-04 18:00:09 -070053 }
54
55 glTexParameteri(renderTarget, GL_TEXTURE_WRAP_S, wrapS);
56 glTexParameteri(renderTarget, GL_TEXTURE_WRAP_T, wrapT);
57 }
58}
59
60void Texture::setFilterMinMag(GLenum min, GLenum mag, bool bindTexture, bool force,
61 GLenum renderTarget) {
62
John Reck48247a22016-01-22 10:55:32 -080063 if (force || min != mMinFilter || mag != mMagFilter) {
Romain Guy8aa195d2013-06-04 18:00:09 -070064 mMinFilter = min;
65 mMagFilter = mag;
66
67 if (bindTexture) {
John Reck38e0c322015-11-10 12:19:17 -080068 mCaches.textureState().bindTexture(renderTarget, mId);
Romain Guy8aa195d2013-06-04 18:00:09 -070069 }
70
71 if (mipMap && min == GL_LINEAR) min = GL_LINEAR_MIPMAP_LINEAR;
72
73 glTexParameteri(renderTarget, GL_TEXTURE_MIN_FILTER, min);
74 glTexParameteri(renderTarget, GL_TEXTURE_MAG_FILTER, mag);
75 }
76}
77
John Reck38e0c322015-11-10 12:19:17 -080078void Texture::deleteTexture() {
79 mCaches.textureState().deleteTexture(mId);
80 mId = 0;
81}
82
83bool Texture::updateSize(uint32_t width, uint32_t height, GLint format) {
84 if (mWidth == width && mHeight == height && mFormat == format) {
85 return false;
86 }
87 mWidth = width;
88 mHeight = height;
89 mFormat = format;
90 notifySizeChanged(mWidth * mHeight * bytesPerPixel(mFormat));
91 return true;
92}
93
John Reck48247a22016-01-22 10:55:32 -080094void Texture::resetCachedParams() {
95 mWrapS = GL_REPEAT;
96 mWrapT = GL_REPEAT;
97 mMinFilter = GL_NEAREST_MIPMAP_LINEAR;
98 mMagFilter = GL_LINEAR;
99}
100
John Reck38e0c322015-11-10 12:19:17 -0800101void Texture::upload(GLint internalformat, uint32_t width, uint32_t height,
102 GLenum format, GLenum type, const void* pixels) {
John Reck975591a2016-01-22 16:28:07 -0800103 GL_CHECKPOINT(MODERATE);
John Reck66f65cb2016-01-21 09:08:42 -0800104 bool needsAlloc = updateSize(width, height, internalformat);
John Reck38e0c322015-11-10 12:19:17 -0800105 if (!mId) {
106 glGenTextures(1, &mId);
107 needsAlloc = true;
John Reck48247a22016-01-22 10:55:32 -0800108 resetCachedParams();
John Reck38e0c322015-11-10 12:19:17 -0800109 }
110 mCaches.textureState().bindTexture(GL_TEXTURE_2D, mId);
111 if (needsAlloc) {
112 glTexImage2D(GL_TEXTURE_2D, 0, mFormat, mWidth, mHeight, 0,
113 format, type, pixels);
John Reck66f65cb2016-01-21 09:08:42 -0800114 } else if (pixels) {
John Reck38e0c322015-11-10 12:19:17 -0800115 glTexSubImage2D(GL_TEXTURE_2D, 0, mFormat, mWidth, mHeight, 0,
116 format, type, pixels);
117 }
John Reck975591a2016-01-22 16:28:07 -0800118 GL_CHECKPOINT(MODERATE);
John Reck38e0c322015-11-10 12:19:17 -0800119}
120
121static void uploadToTexture(bool resize, GLenum format, GLenum type, GLsizei stride, GLsizei bpp,
122 GLsizei width, GLsizei height, const GLvoid * data) {
123
John Reck38e0c322015-11-10 12:19:17 -0800124 const bool useStride = stride != width
125 && Caches::getInstance().extensions().hasUnpackRowLength();
126 if ((stride == width) || useStride) {
127 if (useStride) {
128 glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);
129 }
130
131 if (resize) {
132 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data);
133 } else {
134 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
135 }
136
137 if (useStride) {
138 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
139 }
140 } else {
141 // With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer
142 // if the stride doesn't match the width
143
144 GLvoid * temp = (GLvoid *) malloc(width * height * bpp);
145 if (!temp) return;
146
147 uint8_t * pDst = (uint8_t *)temp;
148 uint8_t * pSrc = (uint8_t *)data;
149 for (GLsizei i = 0; i < height; i++) {
150 memcpy(pDst, pSrc, width * bpp);
151 pDst += width * bpp;
152 pSrc += stride * bpp;
153 }
154
155 if (resize) {
156 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, temp);
157 } else {
158 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, temp);
159 }
160
161 free(temp);
162 }
163}
164
165static void uploadSkBitmapToTexture(const SkBitmap& bitmap,
166 bool resize, GLenum format, GLenum type) {
167 uploadToTexture(resize, format, type, bitmap.rowBytesAsPixels(), bitmap.bytesPerPixel(),
168 bitmap.width(), bitmap.height(), bitmap.getPixels());
169}
170
171static void colorTypeToGlFormatAndType(SkColorType colorType,
172 GLint* outFormat, GLint* outType) {
173 switch (colorType) {
174 case kAlpha_8_SkColorType:
175 *outFormat = GL_ALPHA;
176 *outType = GL_UNSIGNED_BYTE;
177 break;
178 case kRGB_565_SkColorType:
179 *outFormat = GL_RGB;
180 *outType = GL_UNSIGNED_SHORT_5_6_5;
181 break;
182 // ARGB_4444 and Index_8 are both upconverted to RGBA_8888
183 case kARGB_4444_SkColorType:
184 case kIndex_8_SkColorType:
185 case kN32_SkColorType:
186 *outFormat = GL_RGBA;
187 *outType = GL_UNSIGNED_BYTE;
188 break;
Derek Sollenberger88d842f2016-01-20 10:37:30 -0500189 case kGray_8_SkColorType:
190 *outFormat = GL_LUMINANCE;
191 *outType = GL_UNSIGNED_BYTE;
192 break;
John Reck38e0c322015-11-10 12:19:17 -0800193 default:
194 LOG_ALWAYS_FATAL("Unsupported bitmap colorType: %d", colorType);
195 break;
196 }
197}
198
199void Texture::upload(const SkBitmap& bitmap) {
200 SkAutoLockPixels alp(bitmap);
201
202 if (!bitmap.readyToDraw()) {
203 ALOGE("Cannot generate texture from bitmap");
204 return;
205 }
206
207 ATRACE_FORMAT("Upload %ux%u Texture", bitmap.width(), bitmap.height());
208
209 // We could also enable mipmapping if both bitmap dimensions are powers
210 // of 2 but we'd have to deal with size changes. Let's keep this simple
211 const bool canMipMap = mCaches.extensions().hasNPot();
212
213 // If the texture had mipmap enabled but not anymore,
214 // force a glTexImage2D to discard the mipmap levels
215 bool needsAlloc = canMipMap && mipMap && !bitmap.hasHardwareMipMap();
John Reck48247a22016-01-22 10:55:32 -0800216 bool setDefaultParams = false;
John Reck38e0c322015-11-10 12:19:17 -0800217
218 if (!mId) {
219 glGenTextures(1, &mId);
220 needsAlloc = true;
John Reck48247a22016-01-22 10:55:32 -0800221 setDefaultParams = true;
John Reck38e0c322015-11-10 12:19:17 -0800222 }
223
224 GLint format, type;
225 colorTypeToGlFormatAndType(bitmap.colorType(), &format, &type);
226
227 if (updateSize(bitmap.width(), bitmap.height(), format)) {
228 needsAlloc = true;
229 }
230
231 blend = !bitmap.isOpaque();
232 mCaches.textureState().bindTexture(mId);
233
234 if (CC_UNLIKELY(bitmap.colorType() == kARGB_4444_SkColorType
235 || bitmap.colorType() == kIndex_8_SkColorType)) {
236 SkBitmap rgbaBitmap;
237 rgbaBitmap.allocPixels(SkImageInfo::MakeN32(mWidth, mHeight,
238 bitmap.alphaType()));
239 rgbaBitmap.eraseColor(0);
240
241 SkCanvas canvas(rgbaBitmap);
242 canvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr);
243
244 uploadSkBitmapToTexture(rgbaBitmap, needsAlloc, format, type);
245 } else {
246 uploadSkBitmapToTexture(bitmap, needsAlloc, format, type);
247 }
248
249 if (canMipMap) {
250 mipMap = bitmap.hasHardwareMipMap();
251 if (mipMap) {
252 glGenerateMipmap(GL_TEXTURE_2D);
253 }
254 }
255
John Reck48247a22016-01-22 10:55:32 -0800256 if (setDefaultParams) {
John Reck38e0c322015-11-10 12:19:17 -0800257 setFilter(GL_NEAREST);
John Reck38e0c322015-11-10 12:19:17 -0800258 setWrap(GL_CLAMP_TO_EDGE);
259 }
260}
261
262void Texture::wrap(GLuint id, uint32_t width, uint32_t height, GLint format) {
263 mId = id;
264 mWidth = width;
265 mHeight = height;
266 mFormat = format;
267 // We're wrapping an existing texture, so don't double count this memory
268 notifySizeChanged(0);
Romain Guybe1b1272013-06-06 14:02:54 -0700269}
270
Romain Guy8aa195d2013-06-04 18:00:09 -0700271}; // namespace uirenderer
272}; // namespace android