Romain Guy | 1212c9d | 2013-05-02 17:50:23 -0700 | [diff] [blame] | 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 | |
Romain Guy | 1212c9d | 2013-05-02 17:50:23 -0700 | [diff] [blame] | 17 | #include <utils/Log.h> |
| 18 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 19 | #include "Caches.h" |
Romain Guy | 1212c9d | 2013-05-02 17:50:23 -0700 | [diff] [blame] | 20 | #include "Image.h" |
| 21 | |
| 22 | namespace android { |
| 23 | namespace uirenderer { |
| 24 | |
| 25 | Image::Image(sp<GraphicBuffer> buffer) { |
| 26 | // Create the EGLImage object that maps the GraphicBuffer |
| 27 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 28 | EGLClientBuffer clientBuffer = (EGLClientBuffer)buffer->getNativeBuffer(); |
| 29 | EGLint attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE}; |
Romain Guy | 1212c9d | 2013-05-02 17:50:23 -0700 | [diff] [blame] | 30 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 31 | mImage = eglCreateImageKHR(display, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, clientBuffer, |
| 32 | attrs); |
Romain Guy | 1212c9d | 2013-05-02 17:50:23 -0700 | [diff] [blame] | 33 | |
| 34 | if (mImage == EGL_NO_IMAGE_KHR) { |
| 35 | ALOGW("Error creating image (%#x)", eglGetError()); |
| 36 | mTexture = 0; |
| 37 | } else { |
| 38 | // Create a 2D texture to sample from the EGLImage |
| 39 | glGenTextures(1, &mTexture); |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 40 | Caches::getInstance().textureState().bindTexture(mTexture); |
Romain Guy | 1212c9d | 2013-05-02 17:50:23 -0700 | [diff] [blame] | 41 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, mImage); |
| 42 | |
| 43 | GLenum status = GL_NO_ERROR; |
| 44 | while ((status = glGetError()) != GL_NO_ERROR) { |
| 45 | ALOGW("Error creating image (%#x)", status); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | Image::~Image() { |
| 51 | if (mImage != EGL_NO_IMAGE_KHR) { |
| 52 | eglDestroyImageKHR(eglGetDisplay(EGL_DEFAULT_DISPLAY), mImage); |
| 53 | mImage = EGL_NO_IMAGE_KHR; |
| 54 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 55 | Caches::getInstance().textureState().deleteTexture(mTexture); |
Romain Guy | 1212c9d | 2013-05-02 17:50:23 -0700 | [diff] [blame] | 56 | mTexture = 0; |
| 57 | } |
| 58 | } |
| 59 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 60 | }; // namespace uirenderer |
| 61 | }; // namespace android |