Image.cpp revision 1212c9dafe932f70956651338568c5e1fdf21bcf
1/*
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#define LOG_TAG "OpenGLRenderer"
18
19#include <utils/Log.h>
20
21#include "Image.h"
22
23namespace android {
24namespace uirenderer {
25
26Image::Image(sp<GraphicBuffer> buffer) {
27    // Create the EGLImage object that maps the GraphicBuffer
28    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
29    EGLClientBuffer clientBuffer = (EGLClientBuffer) buffer->getNativeBuffer();
30    EGLint attrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE };
31
32    mImage = eglCreateImageKHR(display, EGL_NO_CONTEXT,
33            EGL_NATIVE_BUFFER_ANDROID, clientBuffer, attrs);
34
35    if (mImage == EGL_NO_IMAGE_KHR) {
36        ALOGW("Error creating image (%#x)", eglGetError());
37        mTexture = 0;
38    } else {
39        // Create a 2D texture to sample from the EGLImage
40        glGenTextures(1, &mTexture);
41        glBindTexture(GL_TEXTURE_2D, mTexture);
42        glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, mImage);
43
44        GLenum status = GL_NO_ERROR;
45        while ((status = glGetError()) != GL_NO_ERROR) {
46            ALOGW("Error creating image (%#x)", status);
47        }
48    }
49}
50
51Image::~Image() {
52    if (mImage != EGL_NO_IMAGE_KHR) {
53        eglDestroyImageKHR(eglGetDisplay(EGL_DEFAULT_DISPLAY), mImage);
54        mImage = EGL_NO_IMAGE_KHR;
55
56        glDeleteTextures(1, &mTexture);
57        mTexture = 0;
58    }
59}
60
61}; // namespace uirenderer
62}; // namespace android
63