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 | |
| 29 | Image::Image(const RenderEngine& engine) : mEGLDisplay(engine.getEGLDisplay()) {} |
| 30 | |
| 31 | Image::~Image() { |
| 32 | setNativeWindowBuffer(nullptr, false, 0, 0); |
| 33 | } |
| 34 | |
| 35 | static std::vector<EGLint> buildAttributeList(bool isProtected, int32_t cropWidth, |
| 36 | int32_t cropHeight) { |
| 37 | std::vector<EGLint> attrs; |
| 38 | attrs.reserve(16); |
| 39 | |
| 40 | attrs.push_back(EGL_IMAGE_PRESERVED_KHR); |
| 41 | attrs.push_back(EGL_TRUE); |
| 42 | |
| 43 | if (isProtected && GLExtensions::getInstance().hasProtectedContent()) { |
| 44 | attrs.push_back(EGL_PROTECTED_CONTENT_EXT); |
| 45 | attrs.push_back(EGL_TRUE); |
| 46 | } |
| 47 | |
| 48 | if (cropWidth > 0 && cropHeight > 0) { |
| 49 | attrs.push_back(EGL_IMAGE_CROP_LEFT_ANDROID); |
| 50 | attrs.push_back(0); |
| 51 | attrs.push_back(EGL_IMAGE_CROP_TOP_ANDROID); |
| 52 | attrs.push_back(0); |
| 53 | attrs.push_back(EGL_IMAGE_CROP_RIGHT_ANDROID); |
| 54 | attrs.push_back(cropWidth); |
| 55 | attrs.push_back(EGL_IMAGE_CROP_BOTTOM_ANDROID); |
| 56 | attrs.push_back(cropHeight); |
| 57 | } |
| 58 | |
| 59 | attrs.push_back(EGL_NONE); |
| 60 | |
| 61 | return attrs; |
| 62 | } |
| 63 | |
| 64 | bool Image::setNativeWindowBuffer(ANativeWindowBuffer* buffer, bool isProtected, int32_t cropWidth, |
| 65 | int32_t cropHeight) { |
| 66 | if (mEGLImage != EGL_NO_IMAGE_KHR) { |
| 67 | if (!eglDestroyImageKHR(mEGLDisplay, mEGLImage)) { |
| 68 | ALOGE("failed to destroy image: %#x", eglGetError()); |
| 69 | } |
| 70 | mEGLImage = EGL_NO_IMAGE_KHR; |
| 71 | } |
| 72 | |
| 73 | if (buffer) { |
| 74 | std::vector<EGLint> attrs = buildAttributeList(isProtected, cropWidth, cropHeight); |
| 75 | mEGLImage = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, |
| 76 | static_cast<EGLClientBuffer>(buffer), attrs.data()); |
| 77 | if (mEGLImage == EGL_NO_IMAGE_KHR) { |
| 78 | ALOGE("failed to create EGLImage: %#x", eglGetError()); |
| 79 | return false; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | } // namespace RE |
| 87 | } // namespace android |