blob: edf3930856ea5b85ad5c16729bf0af5f823d67f1 [file] [log] [blame]
Romain Guy1212c9d2013-05-02 17:50:23 -07001/*
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
Romain Guy8aa195d2013-06-04 18:00:09 -070021#include "Caches.h"
Romain Guy1212c9d2013-05-02 17:50:23 -070022#include "Image.h"
23
24namespace android {
25namespace uirenderer {
26
27Image::Image(sp<GraphicBuffer> buffer) {
28 // Create the EGLImage object that maps the GraphicBuffer
29 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
30 EGLClientBuffer clientBuffer = (EGLClientBuffer) buffer->getNativeBuffer();
31 EGLint attrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE };
32
33 mImage = eglCreateImageKHR(display, EGL_NO_CONTEXT,
34 EGL_NATIVE_BUFFER_ANDROID, clientBuffer, attrs);
35
36 if (mImage == EGL_NO_IMAGE_KHR) {
37 ALOGW("Error creating image (%#x)", eglGetError());
38 mTexture = 0;
39 } else {
40 // Create a 2D texture to sample from the EGLImage
41 glGenTextures(1, &mTexture);
Romain Guy8aa195d2013-06-04 18:00:09 -070042 Caches::getInstance().bindTexture(mTexture);
Romain Guy1212c9d2013-05-02 17:50:23 -070043 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, mImage);
44
45 GLenum status = GL_NO_ERROR;
46 while ((status = glGetError()) != GL_NO_ERROR) {
47 ALOGW("Error creating image (%#x)", status);
48 }
49 }
50}
51
52Image::~Image() {
53 if (mImage != EGL_NO_IMAGE_KHR) {
54 eglDestroyImageKHR(eglGetDisplay(EGL_DEFAULT_DISPLAY), mImage);
55 mImage = EGL_NO_IMAGE_KHR;
56
Romain Guybe1b1272013-06-06 14:02:54 -070057 Caches::getInstance().deleteTexture(mTexture);
Romain Guy1212c9d2013-05-02 17:50:23 -070058 mTexture = 0;
59 }
60}
61
62}; // namespace uirenderer
63}; // namespace android