Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #include "Image.h" |
| 18 | |
| 19 | #include <vector> |
| 20 | |
| 21 | #include <log/log.h> |
| 22 | |
| 23 | #include "GLExtensions.h" |
| 24 | #include "RenderEngine.h" |
| 25 | |
| 26 | namespace android { |
| 27 | namespace RE { |
| 28 | |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 29 | Image::~Image() = default; |
| 30 | |
| 31 | namespace impl { |
| 32 | |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 33 | Image::Image(const RenderEngine& engine) : mEGLDisplay(engine.getEGLDisplay()) {} |
| 34 | |
| 35 | Image::~Image() { |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame^] | 36 | setNativeWindowBuffer(nullptr, false); |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame^] | 39 | static std::vector<EGLint> buildAttributeList(bool isProtected) { |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 40 | std::vector<EGLint> attrs; |
| 41 | attrs.reserve(16); |
| 42 | |
| 43 | attrs.push_back(EGL_IMAGE_PRESERVED_KHR); |
| 44 | attrs.push_back(EGL_TRUE); |
| 45 | |
| 46 | if (isProtected && GLExtensions::getInstance().hasProtectedContent()) { |
| 47 | attrs.push_back(EGL_PROTECTED_CONTENT_EXT); |
| 48 | attrs.push_back(EGL_TRUE); |
| 49 | } |
| 50 | |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 51 | attrs.push_back(EGL_NONE); |
| 52 | |
| 53 | return attrs; |
| 54 | } |
| 55 | |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame^] | 56 | bool Image::setNativeWindowBuffer(ANativeWindowBuffer* buffer, bool isProtected) { |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 57 | 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ński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame^] | 65 | std::vector<EGLint> attrs = buildAttributeList(isProtected); |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 66 | 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 | } |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 77 | } // namespace impl |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 78 | } // namespace RE |
| 79 | } // namespace android |