Image.cpp revision 1212c9dafe932f70956651338568c5e1fdf21bcf
11212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy/*
21212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy * Copyright (C) 2013 The Android Open Source Project
31212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy *
41212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy * Licensed under the Apache License, Version 2.0 (the "License");
51212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy * you may not use this file except in compliance with the License.
61212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy * You may obtain a copy of the License at
71212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy *
81212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy *      http://www.apache.org/licenses/LICENSE-2.0
91212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy *
101212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy * Unless required by applicable law or agreed to in writing, software
111212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy * distributed under the License is distributed on an "AS IS" BASIS,
121212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy * See the License for the specific language governing permissions and
141212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy * limitations under the License.
151212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy */
161212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
171212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy#define LOG_TAG "OpenGLRenderer"
181212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
191212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy#include <utils/Log.h>
201212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
211212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy#include "Image.h"
221212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
231212c9dafe932f70956651338568c5e1fdf21bcfRomain Guynamespace android {
241212c9dafe932f70956651338568c5e1fdf21bcfRomain Guynamespace uirenderer {
251212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
261212c9dafe932f70956651338568c5e1fdf21bcfRomain GuyImage::Image(sp<GraphicBuffer> buffer) {
271212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    // Create the EGLImage object that maps the GraphicBuffer
281212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
291212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    EGLClientBuffer clientBuffer = (EGLClientBuffer) buffer->getNativeBuffer();
301212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    EGLint attrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE };
311212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
321212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    mImage = eglCreateImageKHR(display, EGL_NO_CONTEXT,
331212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy            EGL_NATIVE_BUFFER_ANDROID, clientBuffer, attrs);
341212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
351212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    if (mImage == EGL_NO_IMAGE_KHR) {
361212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        ALOGW("Error creating image (%#x)", eglGetError());
371212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        mTexture = 0;
381212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    } else {
391212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        // Create a 2D texture to sample from the EGLImage
401212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        glGenTextures(1, &mTexture);
411212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        glBindTexture(GL_TEXTURE_2D, mTexture);
421212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, mImage);
431212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
441212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        GLenum status = GL_NO_ERROR;
451212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        while ((status = glGetError()) != GL_NO_ERROR) {
461212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy            ALOGW("Error creating image (%#x)", status);
471212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        }
481212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    }
491212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy}
501212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
511212c9dafe932f70956651338568c5e1fdf21bcfRomain GuyImage::~Image() {
521212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    if (mImage != EGL_NO_IMAGE_KHR) {
531212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        eglDestroyImageKHR(eglGetDisplay(EGL_DEFAULT_DISPLAY), mImage);
541212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        mImage = EGL_NO_IMAGE_KHR;
551212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
561212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        glDeleteTextures(1, &mTexture);
571212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        mTexture = 0;
581212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    }
591212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy}
601212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
611212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy}; // namespace uirenderer
621212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy}; // namespace android
63