blob: 8497721956739ad8c1baf890047c47ae21c1dca4 [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
Ady Abrahama3b08ef2019-07-15 18:43:10 -070023#include <gui/DebugEGLImageTracker.h>
Chia-I Wu401ef832017-12-01 10:52:22 -080024#include <log/log.h>
Alec Mourie7d1d4a2019-02-05 01:13:46 +000025#include <utils/Trace.h>
Peiyong Lin7e219eb2018-12-03 05:40:42 -080026#include "GLESRenderEngine.h"
Peiyong Lin46080ef2018-10-26 18:43:14 -070027#include "GLExtensions.h"
Chia-I Wu401ef832017-12-01 10:52:22 -080028
29namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -070030namespace renderengine {
Peiyong Linf1bada92018-08-29 09:39:31 -070031namespace gl {
Chia-I Wu401ef832017-12-01 10:52:22 -080032
Krzysztof Kosiński7108c312018-06-27 19:12:54 -070033static std::vector<EGLint> buildAttributeList(bool isProtected) {
Chia-I Wu401ef832017-12-01 10:52:22 -080034 std::vector<EGLint> attrs;
35 attrs.reserve(16);
36
37 attrs.push_back(EGL_IMAGE_PRESERVED_KHR);
38 attrs.push_back(EGL_TRUE);
39
Peiyong Linf1bada92018-08-29 09:39:31 -070040 if (isProtected && GLExtensions::getInstance().hasProtectedContent()) {
Chia-I Wu401ef832017-12-01 10:52:22 -080041 attrs.push_back(EGL_PROTECTED_CONTENT_EXT);
42 attrs.push_back(EGL_TRUE);
43 }
44
Chia-I Wu401ef832017-12-01 10:52:22 -080045 attrs.push_back(EGL_NONE);
46
47 return attrs;
48}
49
Peiyong Lin7e219eb2018-12-03 05:40:42 -080050GLImage::GLImage(const GLESRenderEngine& engine) : mEGLDisplay(engine.getEGLDisplay()) {}
Peiyong Linf1bada92018-08-29 09:39:31 -070051
52GLImage::~GLImage() {
53 setNativeWindowBuffer(nullptr, false);
54}
55
56bool GLImage::setNativeWindowBuffer(ANativeWindowBuffer* buffer, bool isProtected) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +000057 ATRACE_CALL();
Chia-I Wu401ef832017-12-01 10:52:22 -080058 if (mEGLImage != EGL_NO_IMAGE_KHR) {
59 if (!eglDestroyImageKHR(mEGLDisplay, mEGLImage)) {
60 ALOGE("failed to destroy image: %#x", eglGetError());
61 }
Ady Abrahama3b08ef2019-07-15 18:43:10 -070062 DEBUG_EGL_IMAGE_TRACKER_DESTROY();
Chia-I Wu401ef832017-12-01 10:52:22 -080063 mEGLImage = EGL_NO_IMAGE_KHR;
64 }
65
66 if (buffer) {
Krzysztof Kosiński7108c312018-06-27 19:12:54 -070067 std::vector<EGLint> attrs = buildAttributeList(isProtected);
Chia-I Wu401ef832017-12-01 10:52:22 -080068 mEGLImage = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
69 static_cast<EGLClientBuffer>(buffer), attrs.data());
70 if (mEGLImage == EGL_NO_IMAGE_KHR) {
71 ALOGE("failed to create EGLImage: %#x", eglGetError());
72 return false;
73 }
Ady Abrahama3b08ef2019-07-15 18:43:10 -070074 DEBUG_EGL_IMAGE_TRACKER_CREATE();
Peiyong Linfb530cf2018-12-15 05:07:38 +000075 mProtected = isProtected;
Chia-I Wu401ef832017-12-01 10:52:22 -080076 }
77
78 return true;
79}
80
Peiyong Lin46080ef2018-10-26 18:43:14 -070081} // namespace gl
82} // namespace renderengine
83} // namespace android