1 /*
2 * Copyright 2018 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 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
19 #include "GLImage.h"
20
21 #include <vector>
22
23 #include <log/log.h>
24 #include <utils/Trace.h>
25 #include "GLESRenderEngine.h"
26 #include "GLExtensions.h"
27
28 namespace android {
29 namespace renderengine {
30 namespace gl {
31
buildAttributeList(bool isProtected)32 static std::vector<EGLint> buildAttributeList(bool isProtected) {
33 std::vector<EGLint> attrs;
34 attrs.reserve(16);
35
36 attrs.push_back(EGL_IMAGE_PRESERVED_KHR);
37 attrs.push_back(EGL_TRUE);
38
39 if (isProtected && GLExtensions::getInstance().hasProtectedContent()) {
40 attrs.push_back(EGL_PROTECTED_CONTENT_EXT);
41 attrs.push_back(EGL_TRUE);
42 }
43
44 attrs.push_back(EGL_NONE);
45
46 return attrs;
47 }
48
GLImage(const GLESRenderEngine & engine)49 GLImage::GLImage(const GLESRenderEngine& engine) : mEGLDisplay(engine.getEGLDisplay()) {}
50
~GLImage()51 GLImage::~GLImage() {
52 setNativeWindowBuffer(nullptr, false);
53 }
54
setNativeWindowBuffer(ANativeWindowBuffer * buffer,bool isProtected)55 bool GLImage::setNativeWindowBuffer(ANativeWindowBuffer* buffer, bool isProtected) {
56 ATRACE_CALL();
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) {
65 std::vector<EGLint> attrs = buildAttributeList(isProtected);
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 mProtected = isProtected;
73 }
74
75 return true;
76 }
77
78 } // namespace gl
79 } // namespace renderengine
80 } // namespace android
81