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