Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 1 | /* |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 2 | * Copyright 2018 The Android Open Source Project |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 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 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 19 | #include "GLImage.h" |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 20 | |
| 21 | #include <vector> |
| 22 | |
Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame^] | 23 | #include <gui/DebugEGLImageTracker.h> |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 24 | #include <log/log.h> |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 25 | #include <utils/Trace.h> |
Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 26 | #include "GLESRenderEngine.h" |
Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 27 | #include "GLExtensions.h" |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 30 | namespace renderengine { |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 31 | namespace gl { |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 32 | |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame] | 33 | static std::vector<EGLint> buildAttributeList(bool isProtected) { |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 34 | 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 Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 40 | if (isProtected && GLExtensions::getInstance().hasProtectedContent()) { |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 41 | attrs.push_back(EGL_PROTECTED_CONTENT_EXT); |
| 42 | attrs.push_back(EGL_TRUE); |
| 43 | } |
| 44 | |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 45 | attrs.push_back(EGL_NONE); |
| 46 | |
| 47 | return attrs; |
| 48 | } |
| 49 | |
Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 50 | GLImage::GLImage(const GLESRenderEngine& engine) : mEGLDisplay(engine.getEGLDisplay()) {} |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 51 | |
| 52 | GLImage::~GLImage() { |
| 53 | setNativeWindowBuffer(nullptr, false); |
| 54 | } |
| 55 | |
| 56 | bool GLImage::setNativeWindowBuffer(ANativeWindowBuffer* buffer, bool isProtected) { |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 57 | ATRACE_CALL(); |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 58 | if (mEGLImage != EGL_NO_IMAGE_KHR) { |
| 59 | if (!eglDestroyImageKHR(mEGLDisplay, mEGLImage)) { |
| 60 | ALOGE("failed to destroy image: %#x", eglGetError()); |
| 61 | } |
Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame^] | 62 | DEBUG_EGL_IMAGE_TRACKER_DESTROY(); |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 63 | mEGLImage = EGL_NO_IMAGE_KHR; |
| 64 | } |
| 65 | |
| 66 | if (buffer) { |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame] | 67 | std::vector<EGLint> attrs = buildAttributeList(isProtected); |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 68 | 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 Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame^] | 74 | DEBUG_EGL_IMAGE_TRACKER_CREATE(); |
Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 75 | mProtected = isProtected; |
Chia-I Wu | 401ef83 | 2017-12-01 10:52:22 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 81 | } // namespace gl |
| 82 | } // namespace renderengine |
| 83 | } // namespace android |