blob: 5046d37150fe270f8875fa97d3e44db58e487428 [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
John Reck48247a22016-01-22 10:55:32 -080044 if (force || wrapS != mWrapS || wrapT != mWrapT) {
Romain Guy8aa195d2013-06-04 18:00:09 -070045 mWrapS = wrapS;
46 mWrapT = wrapT;
47
48 if (bindTexture) {
John Reck38e0c322015-11-10 12:19:17 -080049 mCaches.textureState().bindTexture(renderTarget, mId);
Romain Guy8aa195d2013-06-04 18:00:09 -070050 }
51
52 glTexParameteri(renderTarget, GL_TEXTURE_WRAP_S, wrapS);
53 glTexParameteri(renderTarget, GL_TEXTURE_WRAP_T, wrapT);
54 }
55}
56
57void Texture::setFilterMinMag(GLenum min, GLenum mag, bool bindTexture, bool force,
58 GLenum renderTarget) {
59
John Reck48247a22016-01-22 10:55:32 -080060 if (force || min != mMinFilter || mag != mMagFilter) {
Romain Guy8aa195d2013-06-04 18:00:09 -070061 mMinFilter = min;
62 mMagFilter = mag;
63
64 if (bindTexture) {
John Reck38e0c322015-11-10 12:19:17 -080065 mCaches.textureState().bindTexture(renderTarget, mId);
Romain Guy8aa195d2013-06-04 18:00:09 -070066 }
67
68 if (mipMap && min == GL_LINEAR) min = GL_LINEAR_MIPMAP_LINEAR;
69
70 glTexParameteri(renderTarget, GL_TEXTURE_MIN_FILTER, min);
71 glTexParameteri(renderTarget, GL_TEXTURE_MAG_FILTER, mag);
72 }
73}
74
John Reck38e0c322015-11-10 12:19:17 -080075void Texture::deleteTexture() {
76 mCaches.textureState().deleteTexture(mId);
77 mId = 0;
78}
79
80bool Texture::updateSize(uint32_t width, uint32_t height, GLint format) {
81 if (mWidth == width && mHeight == height && mFormat == format) {
82 return false;
83 }
84 mWidth = width;
85 mHeight = height;
86 mFormat = format;
87 notifySizeChanged(mWidth * mHeight * bytesPerPixel(mFormat));
88 return true;
89}
90
John Reck48247a22016-01-22 10:55:32 -080091void Texture::resetCachedParams() {
92 mWrapS = GL_REPEAT;
93 mWrapT = GL_REPEAT;
94 mMinFilter = GL_NEAREST_MIPMAP_LINEAR;
95 mMagFilter = GL_LINEAR;
96}
97
John Reck38e0c322015-11-10 12:19:17 -080098void Texture::upload(GLint internalformat, uint32_t width, uint32_t height,
99 GLenum format, GLenum type, const void* pixels) {
John Reck9372ac32016-01-19 11:46:52 -0800100 GL_CHECKPOINT();
John Reck66f65cb2016-01-21 09:08:42 -0800101 bool needsAlloc = updateSize(width, height, internalformat);
John Reck38e0c322015-11-10 12:19:17 -0800102 if (!mId) {
103 glGenTextures(1, &mId);
104 needsAlloc = true;
John Reck48247a22016-01-22 10:55:32 -0800105 resetCachedParams();
John Reck38e0c322015-11-10 12:19:17 -0800106 }
107 mCaches.textureState().bindTexture(GL_TEXTURE_2D, mId);
108 if (needsAlloc) {
109 glTexImage2D(GL_TEXTURE_2D, 0, mFormat, mWidth, mHeight, 0,
110 format, type, pixels);
John Reck66f65cb2016-01-21 09:08:42 -0800111 } else if (pixels) {
John Reck38e0c322015-11-10 12:19:17 -0800112 glTexSubImage2D(GL_TEXTURE_2D, 0, mFormat, mWidth, mHeight, 0,
113 format, type, pixels);
114 }
John Reck66f65cb2016-01-21 09:08:42 -0800115 GL_CHECKPOINT();
John Reck38e0c322015-11-10 12:19:17 -0800116}
117
118static void uploadToTexture(bool resize, GLenum format, GLenum type, GLsizei stride, GLsizei bpp,
119 GLsizei width, GLsizei height, const GLvoid * data) {
120
John Reck38e0c322015-11-10 12:19:17 -0800121 const bool useStride = stride != width
122 && Caches::getInstance().extensions().hasUnpackRowLength();
123 if ((stride == width) || useStride) {
124 if (useStride) {
125 glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);
126 }
127
128 if (resize) {
129 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data);
130 } else {
131 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
132 }
133
134 if (useStride) {
135 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
136 }
137 } else {
138 // With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer
139 // if the stride doesn't match the width
140
141 GLvoid * temp = (GLvoid *) malloc(width * height * bpp);
142 if (!temp) return;
143
144 uint8_t * pDst = (uint8_t *)temp;
145 uint8_t * pSrc = (uint8_t *)data;
146 for (GLsizei i = 0; i < height; i++) {
147 memcpy(pDst, pSrc, width * bpp);
148 pDst += width * bpp;
149 pSrc += stride * bpp;
150 }
151
152 if (resize) {
153 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, temp);
154 } else {
155 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, temp);
156 }
157
158 free(temp);
159 }
160}
161
162static void uploadSkBitmapToTexture(const SkBitmap& bitmap,
163 bool resize, GLenum format, GLenum type) {
164 uploadToTexture(resize, format, type, bitmap.rowBytesAsPixels(), bitmap.bytesPerPixel(),
165 bitmap.width(), bitmap.height(), bitmap.getPixels());
166}
167
168static void colorTypeToGlFormatAndType(SkColorType colorType,
169 GLint* outFormat, GLint* outType) {
170 switch (colorType) {
171 case kAlpha_8_SkColorType:
172 *outFormat = GL_ALPHA;
173 *outType = GL_UNSIGNED_BYTE;
174 break;
175 case kRGB_565_SkColorType:
176 *outFormat = GL_RGB;
177 *outType = GL_UNSIGNED_SHORT_5_6_5;
178 break;
179 // ARGB_4444 and Index_8 are both upconverted to RGBA_8888
180 case kARGB_4444_SkColorType:
181 case kIndex_8_SkColorType:
182 case kN32_SkColorType:
183 *outFormat = GL_RGBA;
184 *outType = GL_UNSIGNED_BYTE;
185 break;
Derek Sollenberger88d842f2016-01-20 10:37:30 -0500186 case kGray_8_SkColorType:
187 *outFormat = GL_LUMINANCE;
188 *outType = GL_UNSIGNED_BYTE;
189 break;
John Reck38e0c322015-11-10 12:19:17 -0800190 default:
191 LOG_ALWAYS_FATAL("Unsupported bitmap colorType: %d", colorType);
192 break;
193 }
194}
195
196void Texture::upload(const SkBitmap& bitmap) {
197 SkAutoLockPixels alp(bitmap);
198
199 if (!bitmap.readyToDraw()) {
200 ALOGE("Cannot generate texture from bitmap");
201 return;
202 }
203
204 ATRACE_FORMAT("Upload %ux%u Texture", bitmap.width(), bitmap.height());
205
206 // We could also enable mipmapping if both bitmap dimensions are powers
207 // of 2 but we'd have to deal with size changes. Let's keep this simple
208 const bool canMipMap = mCaches.extensions().hasNPot();
209
210 // If the texture had mipmap enabled but not anymore,
211 // force a glTexImage2D to discard the mipmap levels
212 bool needsAlloc = canMipMap && mipMap && !bitmap.hasHardwareMipMap();
John Reck48247a22016-01-22 10:55:32 -0800213 bool setDefaultParams = false;
John Reck38e0c322015-11-10 12:19:17 -0800214
215 if (!mId) {
216 glGenTextures(1, &mId);
217 needsAlloc = true;
John Reck48247a22016-01-22 10:55:32 -0800218 setDefaultParams = true;
John Reck38e0c322015-11-10 12:19:17 -0800219 }
220
221 GLint format, type;
222 colorTypeToGlFormatAndType(bitmap.colorType(), &format, &type);
223
224 if (updateSize(bitmap.width(), bitmap.height(), format)) {
225 needsAlloc = true;
226 }
227
228 blend = !bitmap.isOpaque();
229 mCaches.textureState().bindTexture(mId);
230
231 if (CC_UNLIKELY(bitmap.colorType() == kARGB_4444_SkColorType
232 || bitmap.colorType() == kIndex_8_SkColorType)) {
233 SkBitmap rgbaBitmap;
234 rgbaBitmap.allocPixels(SkImageInfo::MakeN32(mWidth, mHeight,
235 bitmap.alphaType()));
236 rgbaBitmap.eraseColor(0);
237
238 SkCanvas canvas(rgbaBitmap);
239 canvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr);
240
241 uploadSkBitmapToTexture(rgbaBitmap, needsAlloc, format, type);
242 } else {
243 uploadSkBitmapToTexture(bitmap, needsAlloc, format, type);
244 }
245
246 if (canMipMap) {
247 mipMap = bitmap.hasHardwareMipMap();
248 if (mipMap) {
249 glGenerateMipmap(GL_TEXTURE_2D);
250 }
251 }
252
John Reck48247a22016-01-22 10:55:32 -0800253 if (setDefaultParams) {
John Reck38e0c322015-11-10 12:19:17 -0800254 setFilter(GL_NEAREST);
John Reck38e0c322015-11-10 12:19:17 -0800255 setWrap(GL_CLAMP_TO_EDGE);
256 }
257}
258
259void Texture::wrap(GLuint id, uint32_t width, uint32_t height, GLint format) {
260 mId = id;
261 mWidth = width;
262 mHeight = height;
263 mFormat = format;
264 // We're wrapping an existing texture, so don't double count this memory
265 notifySizeChanged(0);
Romain Guybe1b1272013-06-06 14:02:54 -0700266}
267
Romain Guy8aa195d2013-06-04 18:00:09 -0700268}; // namespace uirenderer
269}; // namespace android