blob: c5000e4cadbc25fb49b4a378e8945793b4464433 [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 Reck38e0c322015-11-10 12:19:17 -080098 mCaches.textureState().activateTexture(0);
John Reck66f65cb2016-01-21 09:08:42 -080099 bool needsAlloc = updateSize(width, height, internalformat);
John Reck38e0c322015-11-10 12:19:17 -0800100 if (!mId) {
101 glGenTextures(1, &mId);
102 needsAlloc = true;
103 }
104 mCaches.textureState().bindTexture(GL_TEXTURE_2D, mId);
105 if (needsAlloc) {
106 glTexImage2D(GL_TEXTURE_2D, 0, mFormat, mWidth, mHeight, 0,
107 format, type, pixels);
John Reck66f65cb2016-01-21 09:08:42 -0800108 } else if (pixels) {
John Reck38e0c322015-11-10 12:19:17 -0800109 glTexSubImage2D(GL_TEXTURE_2D, 0, mFormat, mWidth, mHeight, 0,
110 format, type, pixels);
111 }
John Reck66f65cb2016-01-21 09:08:42 -0800112 GL_CHECKPOINT();
John Reck38e0c322015-11-10 12:19:17 -0800113}
114
115static void uploadToTexture(bool resize, GLenum format, GLenum type, GLsizei stride, GLsizei bpp,
116 GLsizei width, GLsizei height, const GLvoid * data) {
117
John Reck38e0c322015-11-10 12:19:17 -0800118 const bool useStride = stride != width
119 && Caches::getInstance().extensions().hasUnpackRowLength();
120 if ((stride == width) || useStride) {
121 if (useStride) {
122 glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);
123 }
124
125 if (resize) {
126 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, data);
127 } else {
128 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
129 }
130
131 if (useStride) {
132 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
133 }
134 } else {
135 // With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer
136 // if the stride doesn't match the width
137
138 GLvoid * temp = (GLvoid *) malloc(width * height * bpp);
139 if (!temp) return;
140
141 uint8_t * pDst = (uint8_t *)temp;
142 uint8_t * pSrc = (uint8_t *)data;
143 for (GLsizei i = 0; i < height; i++) {
144 memcpy(pDst, pSrc, width * bpp);
145 pDst += width * bpp;
146 pSrc += stride * bpp;
147 }
148
149 if (resize) {
150 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, temp);
151 } else {
152 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, temp);
153 }
154
155 free(temp);
156 }
157}
158
159static void uploadSkBitmapToTexture(const SkBitmap& bitmap,
160 bool resize, GLenum format, GLenum type) {
161 uploadToTexture(resize, format, type, bitmap.rowBytesAsPixels(), bitmap.bytesPerPixel(),
162 bitmap.width(), bitmap.height(), bitmap.getPixels());
163}
164
165static void colorTypeToGlFormatAndType(SkColorType colorType,
166 GLint* outFormat, GLint* outType) {
167 switch (colorType) {
168 case kAlpha_8_SkColorType:
169 *outFormat = GL_ALPHA;
170 *outType = GL_UNSIGNED_BYTE;
171 break;
172 case kRGB_565_SkColorType:
173 *outFormat = GL_RGB;
174 *outType = GL_UNSIGNED_SHORT_5_6_5;
175 break;
176 // ARGB_4444 and Index_8 are both upconverted to RGBA_8888
177 case kARGB_4444_SkColorType:
178 case kIndex_8_SkColorType:
179 case kN32_SkColorType:
180 *outFormat = GL_RGBA;
181 *outType = GL_UNSIGNED_BYTE;
182 break;
183 default:
184 LOG_ALWAYS_FATAL("Unsupported bitmap colorType: %d", colorType);
185 break;
186 }
187}
188
189void Texture::upload(const SkBitmap& bitmap) {
190 SkAutoLockPixels alp(bitmap);
191
192 if (!bitmap.readyToDraw()) {
193 ALOGE("Cannot generate texture from bitmap");
194 return;
195 }
196
197 ATRACE_FORMAT("Upload %ux%u Texture", bitmap.width(), bitmap.height());
198
199 // We could also enable mipmapping if both bitmap dimensions are powers
200 // of 2 but we'd have to deal with size changes. Let's keep this simple
201 const bool canMipMap = mCaches.extensions().hasNPot();
202
203 // If the texture had mipmap enabled but not anymore,
204 // force a glTexImage2D to discard the mipmap levels
205 bool needsAlloc = canMipMap && mipMap && !bitmap.hasHardwareMipMap();
206
207 if (!mId) {
208 glGenTextures(1, &mId);
209 needsAlloc = true;
210 }
211
212 GLint format, type;
213 colorTypeToGlFormatAndType(bitmap.colorType(), &format, &type);
214
215 if (updateSize(bitmap.width(), bitmap.height(), format)) {
216 needsAlloc = true;
217 }
218
219 blend = !bitmap.isOpaque();
220 mCaches.textureState().bindTexture(mId);
221
222 if (CC_UNLIKELY(bitmap.colorType() == kARGB_4444_SkColorType
223 || bitmap.colorType() == kIndex_8_SkColorType)) {
224 SkBitmap rgbaBitmap;
225 rgbaBitmap.allocPixels(SkImageInfo::MakeN32(mWidth, mHeight,
226 bitmap.alphaType()));
227 rgbaBitmap.eraseColor(0);
228
229 SkCanvas canvas(rgbaBitmap);
230 canvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr);
231
232 uploadSkBitmapToTexture(rgbaBitmap, needsAlloc, format, type);
233 } else {
234 uploadSkBitmapToTexture(bitmap, needsAlloc, format, type);
235 }
236
237 if (canMipMap) {
238 mipMap = bitmap.hasHardwareMipMap();
239 if (mipMap) {
240 glGenerateMipmap(GL_TEXTURE_2D);
241 }
242 }
243
244 if (mFirstFilter) {
245 setFilter(GL_NEAREST);
246 }
247
248 if (mFirstWrap) {
249 setWrap(GL_CLAMP_TO_EDGE);
250 }
251}
252
253void Texture::wrap(GLuint id, uint32_t width, uint32_t height, GLint format) {
254 mId = id;
255 mWidth = width;
256 mHeight = height;
257 mFormat = format;
258 // We're wrapping an existing texture, so don't double count this memory
259 notifySizeChanged(0);
Romain Guybe1b1272013-06-06 14:02:54 -0700260}
261
Romain Guy8aa195d2013-06-04 18:00:09 -0700262}; // namespace uirenderer
263}; // namespace android