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