blob: 65260325b21fb574b05a73bf28160c49cf052999 [file] [log] [blame]
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -07001/*
2 * Copyright (C) 2010 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
17#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
21#include <utils/Errors.h>
22#include <utils/Log.h>
23
24#include <ui/GraphicBuffer.h>
25
26#include <GLES/gl.h>
27#include <GLES/glext.h>
28
29#include <hardware/hardware.h>
30
31#include "clz.h"
32#include "DisplayHardware/DisplayHardware.h"
Mathias Agopian781953d2010-06-25 18:02:21 -070033#include "GLExtensions.h"
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070034#include "TextureManager.h"
35
36namespace android {
37
38// ---------------------------------------------------------------------------
39
Mathias Agopian781953d2010-06-25 18:02:21 -070040TextureManager::TextureManager()
41 : mGLExtensions(GLExtensions::getInstance())
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070042{
43}
44
Mathias Agopianf8b4b442010-06-14 21:20:00 -070045GLenum TextureManager::getTextureTarget(const Image* image) {
46#if defined(GL_OES_texture_external)
47 switch (image->target) {
48 case Texture::TEXTURE_EXTERNAL:
49 return GL_TEXTURE_EXTERNAL_OES;
50 }
51#endif
52 return GL_TEXTURE_2D;
53}
54
55status_t TextureManager::initTexture(Texture* texture)
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070056{
Mathias Agopianf8b4b442010-06-14 21:20:00 -070057 if (texture->name != -1UL)
58 return INVALID_OPERATION;
59
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -070060 GLuint textureName = -1;
61 glGenTextures(1, &textureName);
Mathias Agopianf8b4b442010-06-14 21:20:00 -070062 texture->name = textureName;
63 texture->width = 0;
64 texture->height = 0;
65
66 const GLenum target = GL_TEXTURE_2D;
67 glBindTexture(target, textureName);
68 glTexParameterx(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
69 glTexParameterx(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
70 glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
71 glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
72
73 return NO_ERROR;
74}
75
76status_t TextureManager::initTexture(Image* pImage, int32_t format)
77{
78 if (pImage->name != -1UL)
79 return INVALID_OPERATION;
80
81 GLuint textureName = -1;
82 glGenTextures(1, &textureName);
83 pImage->name = textureName;
84 pImage->width = 0;
85 pImage->height = 0;
86
87 GLenum target = GL_TEXTURE_2D;
88#if defined(GL_OES_texture_external)
Mathias Agopian781953d2010-06-25 18:02:21 -070089 if (GLExtensions::getInstance().haveTextureExternal()) {
Mathias Agopian61c55c42010-07-01 21:17:56 -070090 if (format && isYuvFormat(format)) {
Mathias Agopian781953d2010-06-25 18:02:21 -070091 target = GL_TEXTURE_EXTERNAL_OES;
92 pImage->target = Texture::TEXTURE_EXTERNAL;
93 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -070094 }
95#endif
96
97 glBindTexture(target, textureName);
98 glTexParameterx(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
99 glTexParameterx(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
100 glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
101 glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
102
103 return NO_ERROR;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700104}
105
106bool TextureManager::isSupportedYuvFormat(int format)
107{
Mathias Agopian61c55c42010-07-01 21:17:56 -0700108 switch (format) {
109 case HAL_PIXEL_FORMAT_YV12:
110 case HAL_PIXEL_FORMAT_YV16:
111 return true;
112 }
113 return false;
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700114}
115
116bool TextureManager::isYuvFormat(int format)
117{
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700118 switch (format) {
Mathias Agopian61c55c42010-07-01 21:17:56 -0700119 // supported YUV formats
120 case HAL_PIXEL_FORMAT_YV12:
121 case HAL_PIXEL_FORMAT_YV16:
122 // Legacy/deprecated YUV formats
123 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
124 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
125 case HAL_PIXEL_FORMAT_YCbCr_422_I:
126 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700127 return true;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700128 }
Mathias Agopian61c55c42010-07-01 21:17:56 -0700129
130 // Any OEM format needs to be considered
131 if (format>=0x100 && format<=0x1FF)
132 return true;
133
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700134 return false;
135}
136
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700137status_t TextureManager::initEglImage(Image* pImage,
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700138 EGLDisplay dpy, const sp<GraphicBuffer>& buffer)
139{
140 status_t err = NO_ERROR;
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700141 if (!pImage->dirty) return err;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700142
143 // free the previous image
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700144 if (pImage->image != EGL_NO_IMAGE_KHR) {
145 eglDestroyImageKHR(dpy, pImage->image);
146 pImage->image = EGL_NO_IMAGE_KHR;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700147 }
148
149 // construct an EGL_NATIVE_BUFFER_ANDROID
150 android_native_buffer_t* clientBuf = buffer->getNativeBuffer();
151
152 // create the new EGLImageKHR
153 const EGLint attrs[] = {
154 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
155 EGL_NONE, EGL_NONE
156 };
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700157 pImage->image = eglCreateImageKHR(
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700158 dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
159 (EGLClientBuffer)clientBuf, attrs);
160
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700161 if (pImage->image != EGL_NO_IMAGE_KHR) {
162 if (pImage->name == -1UL) {
163 initTexture(pImage, buffer->format);
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700164 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700165 const GLenum target = getTextureTarget(pImage);
166 glBindTexture(target, pImage->name);
167 glEGLImageTargetTexture2DOES(target, (GLeglImageOES)pImage->image);
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700168 GLint error = glGetError();
169 if (error != GL_NO_ERROR) {
170 LOGE("glEGLImageTargetTexture2DOES(%p) failed err=0x%04x",
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700171 pImage->image, error);
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700172 err = INVALID_OPERATION;
173 } else {
174 // Everything went okay!
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700175 pImage->dirty = false;
176 pImage->width = clientBuf->width;
177 pImage->height = clientBuf->height;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700178 }
179 } else {
180 LOGE("eglCreateImageKHR() failed. err=0x%4x", eglGetError());
181 err = INVALID_OPERATION;
182 }
183 return err;
184}
185
186status_t TextureManager::loadTexture(Texture* texture,
187 const Region& dirty, const GGLSurface& t)
188{
189 if (texture->name == -1UL) {
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700190 status_t err = initTexture(texture);
191 LOGE_IF(err, "loadTexture failed in initTexture (%s)", strerror(err));
192 return err;
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700193 }
194
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700195 if (texture->target != GL_TEXTURE_2D)
196 return INVALID_OPERATION;
197
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700198 glBindTexture(GL_TEXTURE_2D, texture->name);
199
200 /*
201 * In OpenGL ES we can't specify a stride with glTexImage2D (however,
202 * GL_UNPACK_ALIGNMENT is a limited form of stride).
203 * So if the stride here isn't representable with GL_UNPACK_ALIGNMENT, we
204 * need to do something reasonable (here creating a bigger texture).
205 *
206 * extra pixels = (((stride - width) * pixelsize) / GL_UNPACK_ALIGNMENT);
207 *
208 * This situation doesn't happen often, but some h/w have a limitation
209 * for their framebuffer (eg: must be multiple of 8 pixels), and
210 * we need to take that into account when using these buffers as
211 * textures.
212 *
213 * This should never be a problem with POT textures
214 */
215
216 int unpack = __builtin_ctz(t.stride * bytesPerPixel(t.format));
217 unpack = 1 << ((unpack > 3) ? 3 : unpack);
218 glPixelStorei(GL_UNPACK_ALIGNMENT, unpack);
219
220 /*
221 * round to POT if needed
222 */
Mathias Agopian781953d2010-06-25 18:02:21 -0700223 if (!mGLExtensions.haveNpot()) {
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700224 texture->NPOTAdjust = true;
225 }
226
227 if (texture->NPOTAdjust) {
228 // find the smallest power-of-two that will accommodate our surface
229 texture->potWidth = 1 << (31 - clz(t.width));
230 texture->potHeight = 1 << (31 - clz(t.height));
231 if (texture->potWidth < t.width) texture->potWidth <<= 1;
232 if (texture->potHeight < t.height) texture->potHeight <<= 1;
233 texture->wScale = float(t.width) / texture->potWidth;
234 texture->hScale = float(t.height) / texture->potHeight;
235 } else {
236 texture->potWidth = t.width;
237 texture->potHeight = t.height;
238 }
239
240 Rect bounds(dirty.bounds());
241 GLvoid* data = 0;
242 if (texture->width != t.width || texture->height != t.height) {
243 texture->width = t.width;
244 texture->height = t.height;
245
246 // texture size changed, we need to create a new one
247 bounds.set(Rect(t.width, t.height));
248 if (t.width == texture->potWidth &&
249 t.height == texture->potHeight) {
250 // we can do it one pass
251 data = t.data;
252 }
253
254 if (t.format == HAL_PIXEL_FORMAT_RGB_565) {
255 glTexImage2D(GL_TEXTURE_2D, 0,
256 GL_RGB, texture->potWidth, texture->potHeight, 0,
257 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
258 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_4444) {
259 glTexImage2D(GL_TEXTURE_2D, 0,
260 GL_RGBA, texture->potWidth, texture->potHeight, 0,
261 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
262 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_8888 ||
263 t.format == HAL_PIXEL_FORMAT_RGBX_8888) {
264 glTexImage2D(GL_TEXTURE_2D, 0,
265 GL_RGBA, texture->potWidth, texture->potHeight, 0,
266 GL_RGBA, GL_UNSIGNED_BYTE, data);
Mathias Agopian61c55c42010-07-01 21:17:56 -0700267 } else if (isSupportedYuvFormat(t.format)) {
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700268 // just show the Y plane of YUV buffers
269 glTexImage2D(GL_TEXTURE_2D, 0,
270 GL_LUMINANCE, texture->potWidth, texture->potHeight, 0,
271 GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
272 } else {
273 // oops, we don't handle this format!
274 LOGE("texture=%d, using format %d, which is not "
275 "supported by the GL", texture->name, t.format);
276 }
277 }
278 if (!data) {
279 if (t.format == HAL_PIXEL_FORMAT_RGB_565) {
280 glTexSubImage2D(GL_TEXTURE_2D, 0,
281 0, bounds.top, t.width, bounds.height(),
282 GL_RGB, GL_UNSIGNED_SHORT_5_6_5,
283 t.data + bounds.top*t.stride*2);
284 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_4444) {
285 glTexSubImage2D(GL_TEXTURE_2D, 0,
286 0, bounds.top, t.width, bounds.height(),
287 GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,
288 t.data + bounds.top*t.stride*2);
289 } else if (t.format == HAL_PIXEL_FORMAT_RGBA_8888 ||
290 t.format == HAL_PIXEL_FORMAT_RGBX_8888) {
291 glTexSubImage2D(GL_TEXTURE_2D, 0,
292 0, bounds.top, t.width, bounds.height(),
293 GL_RGBA, GL_UNSIGNED_BYTE,
294 t.data + bounds.top*t.stride*4);
Mathias Agopian61c55c42010-07-01 21:17:56 -0700295 } else if (isSupportedYuvFormat(t.format)) {
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700296 // just show the Y plane of YUV buffers
297 glTexSubImage2D(GL_TEXTURE_2D, 0,
298 0, bounds.top, t.width, bounds.height(),
299 GL_LUMINANCE, GL_UNSIGNED_BYTE,
300 t.data + bounds.top*t.stride);
301 }
302 }
303 return NO_ERROR;
304}
305
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700306void TextureManager::activateTexture(const Texture& texture, bool filter)
307{
308 const GLenum target = getTextureTarget(&texture);
Mathias Agopian8fa4c8112010-06-28 19:54:17 -0700309 if (target == GL_TEXTURE_2D) {
Mathias Agopian781953d2010-06-25 18:02:21 -0700310 glBindTexture(GL_TEXTURE_2D, texture.name);
311 glEnable(GL_TEXTURE_2D);
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700312#if defined(GL_OES_texture_external)
Mathias Agopian781953d2010-06-25 18:02:21 -0700313 if (GLExtensions::getInstance().haveTextureExternal()) {
314 glDisable(GL_TEXTURE_EXTERNAL_OES);
315 }
Mathias Agopian8fa4c8112010-06-28 19:54:17 -0700316 } else {
Mathias Agopian781953d2010-06-25 18:02:21 -0700317 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture.name);
318 glEnable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700319 glDisable(GL_TEXTURE_2D);
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700320#endif
Mathias Agopian8fa4c8112010-06-28 19:54:17 -0700321 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700322
323 if (filter) {
324 glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
325 glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
326 } else {
327 glTexParameterx(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
328 glTexParameterx(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
329 }
330}
331
332void TextureManager::deactivateTextures()
333{
334 glDisable(GL_TEXTURE_2D);
335#if defined(GL_OES_texture_external)
Mathias Agopian781953d2010-06-25 18:02:21 -0700336 if (GLExtensions::getInstance().haveTextureExternal()) {
337 glDisable(GL_TEXTURE_EXTERNAL_OES);
338 }
Mathias Agopianf8b4b442010-06-14 21:20:00 -0700339#endif
340}
341
Mathias Agopian9f2c4fd2010-05-10 20:06:11 -0700342// ---------------------------------------------------------------------------
343
344}; // namespace android