blob: 77e648e70f3e269c2d4747e598f379beb07deae7 [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
Alec Mourie7d1d4a2019-02-05 01:13:46 +000017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Peiyong Linf1bada92018-08-29 09:39:31 -070019#include "GLImage.h"
Chia-I Wu401ef832017-12-01 10:52:22 -080020
21#include <vector>
22
23#include <log/log.h>
Alec Mourie7d1d4a2019-02-05 01:13:46 +000024#include <utils/Trace.h>
Peiyong Lin7e219eb2018-12-03 05:40:42 -080025#include "GLESRenderEngine.h"
Peiyong Lin46080ef2018-10-26 18:43:14 -070026#include "GLExtensions.h"
Chia-I Wu401ef832017-12-01 10:52:22 -080027
28namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -070029namespace renderengine {
Peiyong Linf1bada92018-08-29 09:39:31 -070030namespace gl {
Chia-I Wu401ef832017-12-01 10:52:22 -080031
Krzysztof Kosiński7108c312018-06-27 19:12:54 -070032static std::vector<EGLint> buildAttributeList(bool isProtected) {
Chia-I Wu401ef832017-12-01 10:52:22 -080033 std::vector<EGLint> attrs;
34 attrs.reserve(16);
35
36 attrs.push_back(EGL_IMAGE_PRESERVED_KHR);
37 attrs.push_back(EGL_TRUE);
38
Peiyong Linf1bada92018-08-29 09:39:31 -070039 if (isProtected && GLExtensions::getInstance().hasProtectedContent()) {
Chia-I Wu401ef832017-12-01 10:52:22 -080040 attrs.push_back(EGL_PROTECTED_CONTENT_EXT);
41 attrs.push_back(EGL_TRUE);
42 }
43
Chia-I Wu401ef832017-12-01 10:52:22 -080044 attrs.push_back(EGL_NONE);
45
46 return attrs;
47}
48
Peiyong Lin7e219eb2018-12-03 05:40:42 -080049GLImage::GLImage(const GLESRenderEngine& engine) : mEGLDisplay(engine.getEGLDisplay()) {}
Peiyong Linf1bada92018-08-29 09:39:31 -070050
51GLImage::~GLImage() {
52 setNativeWindowBuffer(nullptr, false);
53}
54
55bool GLImage::setNativeWindowBuffer(ANativeWindowBuffer* buffer, bool isProtected) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +000056 ATRACE_CALL();
Chia-I Wu401ef832017-12-01 10:52:22 -080057 if (mEGLImage != EGL_NO_IMAGE_KHR) {
58 if (!eglDestroyImageKHR(mEGLDisplay, mEGLImage)) {
59 ALOGE("failed to destroy image: %#x", eglGetError());
60 }
61 mEGLImage = EGL_NO_IMAGE_KHR;
62 }
63
64 if (buffer) {
Krzysztof Kosiński7108c312018-06-27 19:12:54 -070065 std::vector<EGLint> attrs = buildAttributeList(isProtected);
Chia-I Wu401ef832017-12-01 10:52:22 -080066 mEGLImage = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
67 static_cast<EGLClientBuffer>(buffer), attrs.data());
68 if (mEGLImage == EGL_NO_IMAGE_KHR) {
69 ALOGE("failed to create EGLImage: %#x", eglGetError());
70 return false;
71 }
Peiyong Linfb530cf2018-12-15 05:07:38 +000072 mProtected = isProtected;
Chia-I Wu401ef832017-12-01 10:52:22 -080073 }
74
75 return true;
76}
77
Peiyong Lin46080ef2018-10-26 18:43:14 -070078} // namespace gl
79} // namespace renderengine
80} // namespace android