blob: a9529a757c6d2f774b0bc69d87f4c00ea4abc897 [file] [log] [blame]
Chia-I Wu401ef832017-12-01 10:52:22 -08001/*
Peiyong Linf1bada92018-08-29 09:39:31 -07002 * Copyright 2018 The Android Open Source Project
Chia-I Wu401ef832017-12-01 10:52:22 -08003 *
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
Peiyong Linf1bada92018-08-29 09:39:31 -070017#include "GLImage.h"
Chia-I Wu401ef832017-12-01 10:52:22 -080018
19#include <vector>
20
21#include <log/log.h>
Peiyong Lin7e219eb2018-12-03 05:40:42 -080022#include "GLESRenderEngine.h"
Peiyong Lin46080ef2018-10-26 18:43:14 -070023#include "GLExtensions.h"
Chia-I Wu401ef832017-12-01 10:52:22 -080024
25namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -070026namespace renderengine {
Peiyong Linf1bada92018-08-29 09:39:31 -070027namespace gl {
Chia-I Wu401ef832017-12-01 10:52:22 -080028
Krzysztof Kosiński7108c312018-06-27 19:12:54 -070029static std::vector<EGLint> buildAttributeList(bool isProtected) {
Chia-I Wu401ef832017-12-01 10:52:22 -080030 std::vector<EGLint> attrs;
31 attrs.reserve(16);
32
33 attrs.push_back(EGL_IMAGE_PRESERVED_KHR);
34 attrs.push_back(EGL_TRUE);
35
Peiyong Linf1bada92018-08-29 09:39:31 -070036 if (isProtected && GLExtensions::getInstance().hasProtectedContent()) {
Chia-I Wu401ef832017-12-01 10:52:22 -080037 attrs.push_back(EGL_PROTECTED_CONTENT_EXT);
38 attrs.push_back(EGL_TRUE);
39 }
40
Chia-I Wu401ef832017-12-01 10:52:22 -080041 attrs.push_back(EGL_NONE);
42
43 return attrs;
44}
45
Peiyong Lin7e219eb2018-12-03 05:40:42 -080046GLImage::GLImage(const GLESRenderEngine& engine) : mEGLDisplay(engine.getEGLDisplay()) {}
Peiyong Linf1bada92018-08-29 09:39:31 -070047
48GLImage::~GLImage() {
49 setNativeWindowBuffer(nullptr, false);
50}
51
52bool GLImage::setNativeWindowBuffer(ANativeWindowBuffer* buffer, bool isProtected) {
Chia-I Wu401ef832017-12-01 10:52:22 -080053 if (mEGLImage != EGL_NO_IMAGE_KHR) {
54 if (!eglDestroyImageKHR(mEGLDisplay, mEGLImage)) {
55 ALOGE("failed to destroy image: %#x", eglGetError());
56 }
57 mEGLImage = EGL_NO_IMAGE_KHR;
58 }
59
60 if (buffer) {
Krzysztof Kosiński7108c312018-06-27 19:12:54 -070061 std::vector<EGLint> attrs = buildAttributeList(isProtected);
Chia-I Wu401ef832017-12-01 10:52:22 -080062 mEGLImage = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
63 static_cast<EGLClientBuffer>(buffer), attrs.data());
64 if (mEGLImage == EGL_NO_IMAGE_KHR) {
65 ALOGE("failed to create EGLImage: %#x", eglGetError());
66 return false;
67 }
68 }
69
70 return true;
71}
72
Peiyong Lin46080ef2018-10-26 18:43:14 -070073} // namespace gl
74} // namespace renderengine
75} // namespace android