blob: 3f2978f7b2f8f5eba550d6cd6f4c77a2a7b5c127 [file] [log] [blame]
Romain Guy877cfe02013-05-02 17:36:28 -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
17#ifndef ANDROID_HWUI_IMAGE_H
18#define ANDROID_HWUI_IMAGE_H
19
20#define LOG_TAG "OpenGLRenderer"
21
22#include <EGL/egl.h>
23#include <EGL/eglext.h>
24
25#include <GLES2/gl2.h>
26#include <GLES2/gl2ext.h>
27
28#include <ui/GraphicBuffer.h>
29
30#include <utils/Log.h>
31
32namespace android {
33namespace uirenderer {
34
35/**
36 * A simple wrapper that creates an EGLImage and a texture for a GraphicBuffer.
37 */
38class Image {
39public:
40 /**
41 * Creates a new image from the specified graphic buffer. If the image
42 * cannot be created, getTexture() will return 0 and getImage() will
43 * return EGL_NO_IMAGE_KHR.
44 */
45 Image(sp<GraphicBuffer> buffer) {
46 // Create the EGLImage object that maps the GraphicBuffer
47 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
48 EGLClientBuffer clientBuffer = (EGLClientBuffer) buffer->getNativeBuffer();
49 EGLint attrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE };
50
51 mImage = eglCreateImageKHR(display, EGL_NO_CONTEXT,
52 EGL_NATIVE_BUFFER_ANDROID, clientBuffer, attrs);
53
54 if (mImage == EGL_NO_IMAGE_KHR) {
55 ALOGW("Error creating image (%#x)", eglGetError());
56 mTexture = 0;
57 } else {
58 // Create a 2D texture to sample from the EGLImage
59 glGenTextures(1, &mTexture);
60 glBindTexture(GL_TEXTURE_2D, mTexture);
61 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, mImage);
62
63 GLenum status = GL_NO_ERROR;
64 while ((status = glGetError()) != GL_NO_ERROR) {
65 ALOGW("Error creating image (%#x)", status);
66 }
67 }
68 }
69
70 ~Image() {
71 if (mImage != EGL_NO_IMAGE_KHR) {
72 eglDestroyImageKHR(eglGetDisplay(EGL_DEFAULT_DISPLAY), mImage);
73 mImage = EGL_NO_IMAGE_KHR;
74
75 glDeleteTextures(1, &mTexture);
76 mTexture = 0;
77 }
78 }
79
80 /**
81 * Returns the name of the GL texture that can be used to sample
82 * from this image.
83 */
84 GLuint getTexture() const {
85 return mTexture;
86 }
87
88 /**
89 * Returns the name of the EGL image represented by this object.
90 */
91 EGLImageKHR getImage() const {
92 return mImage;
93 }
94
95private:
96 GLuint mTexture;
97 EGLImageKHR mImage;
98}; // class Image
99
100}; // namespace uirenderer
101}; // namespace android
102
103#endif // ANDROID_HWUI_IMAGE_H