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
218aa195d7081b889f3a7b1f426cbd8556377aae5eRomain Guy#include "Caches.h"
221212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy#include "Image.h"
231212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
241212c9dafe932f70956651338568c5e1fdf21bcfRomain Guynamespace android {
251212c9dafe932f70956651338568c5e1fdf21bcfRomain Guynamespace uirenderer {
261212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
271212c9dafe932f70956651338568c5e1fdf21bcfRomain GuyImage::Image(sp<GraphicBuffer> buffer) {
281212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    // Create the EGLImage object that maps the GraphicBuffer
291212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
301212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    EGLClientBuffer clientBuffer = (EGLClientBuffer) buffer->getNativeBuffer();
311212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    EGLint attrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE };
321212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
331212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    mImage = eglCreateImageKHR(display, EGL_NO_CONTEXT,
341212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy            EGL_NATIVE_BUFFER_ANDROID, clientBuffer, attrs);
351212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
361212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    if (mImage == EGL_NO_IMAGE_KHR) {
371212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        ALOGW("Error creating image (%#x)", eglGetError());
381212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        mTexture = 0;
391212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    } else {
401212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        // Create a 2D texture to sample from the EGLImage
411212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        glGenTextures(1, &mTexture);
4244eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik        Caches::getInstance().textureState().bindTexture(mTexture);
431212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, mImage);
441212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
451212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        GLenum status = GL_NO_ERROR;
461212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        while ((status = glGetError()) != GL_NO_ERROR) {
471212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy            ALOGW("Error creating image (%#x)", status);
481212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        }
491212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    }
501212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy}
511212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
521212c9dafe932f70956651338568c5e1fdf21bcfRomain GuyImage::~Image() {
531212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    if (mImage != EGL_NO_IMAGE_KHR) {
541212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        eglDestroyImageKHR(eglGetDisplay(EGL_DEFAULT_DISPLAY), mImage);
551212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        mImage = EGL_NO_IMAGE_KHR;
561212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
5744eb2c00861098dd3e2950d923646814b4cc57c2Chris Craik        Caches::getInstance().textureState().deleteTexture(mTexture);
581212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy        mTexture = 0;
591212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy    }
601212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy}
611212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy
621212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy}; // namespace uirenderer
631212c9dafe932f70956651338568c5e1fdf21bcfRomain Guy}; // namespace android
64