blob: 4a519bb42269fed512220487ee3c5c9c5c34bf0a [file] [log] [blame]
Peiyong Line5a9a7f2018-08-30 15:32:13 -07001/*
2 * Copyright 2018 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#include "GLFramebuffer.h"
18
19#include <GLES/gl.h>
20#include <GLES/glext.h>
21#include <GLES2/gl2.h>
22#include <GLES2/gl2ext.h>
Alec Mouri05483a02018-09-10 21:03:42 +000023#include <nativebase/nativebase.h>
Peiyong Lin7e219eb2018-12-03 05:40:42 -080024#include "GLESRenderEngine.h"
Peiyong Line5a9a7f2018-08-30 15:32:13 -070025
26namespace android {
27namespace renderengine {
28namespace gl {
29
Peiyong Lin7e219eb2018-12-03 05:40:42 -080030GLFramebuffer::GLFramebuffer(const GLESRenderEngine& engine)
Peiyong Lin46080ef2018-10-26 18:43:14 -070031 : mEGLDisplay(engine.getEGLDisplay()), mEGLImage(EGL_NO_IMAGE_KHR) {
Peiyong Line5a9a7f2018-08-30 15:32:13 -070032 glGenTextures(1, &mTextureName);
33 glGenFramebuffers(1, &mFramebufferName);
34}
35
36GLFramebuffer::~GLFramebuffer() {
37 glDeleteFramebuffers(1, &mFramebufferName);
38 glDeleteTextures(1, &mTextureName);
39 eglDestroyImageKHR(mEGLDisplay, mEGLImage);
40}
41
Peiyong Lindc979242018-11-30 22:53:07 -080042bool GLFramebuffer::setNativeWindowBuffer(ANativeWindowBuffer* nativeBuffer, bool isProtected) {
Peiyong Line5a9a7f2018-08-30 15:32:13 -070043 if (mEGLImage != EGL_NO_IMAGE_KHR) {
44 eglDestroyImageKHR(mEGLDisplay, mEGLImage);
45 mEGLImage = EGL_NO_IMAGE_KHR;
Alec Mouri05483a02018-09-10 21:03:42 +000046 mBufferWidth = 0;
47 mBufferHeight = 0;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070048 }
49
50 if (nativeBuffer) {
Peiyong Lindc979242018-11-30 22:53:07 -080051 EGLint attributes[] = {
52 isProtected ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
53 isProtected ? EGL_TRUE : EGL_NONE,
54 EGL_NONE,
55 };
Peiyong Lin46080ef2018-10-26 18:43:14 -070056 mEGLImage = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
Peiyong Lindc979242018-11-30 22:53:07 -080057 nativeBuffer, attributes);
Peiyong Line5a9a7f2018-08-30 15:32:13 -070058 if (mEGLImage == EGL_NO_IMAGE_KHR) {
59 return false;
60 }
Alec Mouri05483a02018-09-10 21:03:42 +000061 mBufferWidth = nativeBuffer->width;
62 mBufferHeight = nativeBuffer->height;
Peiyong Line5a9a7f2018-08-30 15:32:13 -070063 }
64 return true;
65}
66
Peiyong Lin46080ef2018-10-26 18:43:14 -070067} // namespace gl
68} // namespace renderengine
69} // namespace android