blob: cabcace672a7811814f8557195e0bc43514c8f52 [file] [log] [blame]
Chia-I Wu401ef832017-12-01 10:52:22 -08001/*
2 * Copyright 2017 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
Peiyong Lincbc184f2018-08-22 13:24:10 -070017#include <renderengine/Image.h>
Chia-I Wu401ef832017-12-01 10:52:22 -080018
19#include <vector>
20
21#include <log/log.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070022#include <renderengine/RenderEngine.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070023#include "gl/GLExtensions.h"
Chia-I Wu401ef832017-12-01 10:52:22 -080024
25namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -070026namespace renderengine {
Chia-I Wu401ef832017-12-01 10:52:22 -080027
Lloyd Pique144e1162017-12-20 16:44:52 -080028Image::~Image() = default;
29
30namespace impl {
31
Chia-I Wu401ef832017-12-01 10:52:22 -080032Image::Image(const RenderEngine& engine) : mEGLDisplay(engine.getEGLDisplay()) {}
33
34Image::~Image() {
Krzysztof Kosiński7108c312018-06-27 19:12:54 -070035 setNativeWindowBuffer(nullptr, false);
Chia-I Wu401ef832017-12-01 10:52:22 -080036}
37
Krzysztof Kosiński7108c312018-06-27 19:12:54 -070038static std::vector<EGLint> buildAttributeList(bool isProtected) {
Chia-I Wu401ef832017-12-01 10:52:22 -080039 std::vector<EGLint> attrs;
40 attrs.reserve(16);
41
42 attrs.push_back(EGL_IMAGE_PRESERVED_KHR);
43 attrs.push_back(EGL_TRUE);
44
Peiyong Lin833074a2018-08-28 11:53:54 -070045 if (isProtected && gl::GLExtensions::getInstance().hasProtectedContent()) {
Chia-I Wu401ef832017-12-01 10:52:22 -080046 attrs.push_back(EGL_PROTECTED_CONTENT_EXT);
47 attrs.push_back(EGL_TRUE);
48 }
49
Chia-I Wu401ef832017-12-01 10:52:22 -080050 attrs.push_back(EGL_NONE);
51
52 return attrs;
53}
54
Krzysztof Kosiński7108c312018-06-27 19:12:54 -070055bool Image::setNativeWindowBuffer(ANativeWindowBuffer* buffer, bool isProtected) {
Chia-I Wu401ef832017-12-01 10:52:22 -080056 if (mEGLImage != EGL_NO_IMAGE_KHR) {
57 if (!eglDestroyImageKHR(mEGLDisplay, mEGLImage)) {
58 ALOGE("failed to destroy image: %#x", eglGetError());
59 }
60 mEGLImage = EGL_NO_IMAGE_KHR;
61 }
62
63 if (buffer) {
Krzysztof Kosiński7108c312018-06-27 19:12:54 -070064 std::vector<EGLint> attrs = buildAttributeList(isProtected);
Chia-I Wu401ef832017-12-01 10:52:22 -080065 mEGLImage = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
66 static_cast<EGLClientBuffer>(buffer), attrs.data());
67 if (mEGLImage == EGL_NO_IMAGE_KHR) {
68 ALOGE("failed to create EGLImage: %#x", eglGetError());
69 return false;
70 }
71 }
72
73 return true;
74}
75
Peiyong Lin833074a2018-08-28 11:53:54 -070076} // namespace impl
77} // namespace renderengine
78} // namespace android