1 /*
2  * Copyright (C) 2023 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 LOG_NDEBUG 0
18 #define LOG_TAG "EglUtil"
19 #include "EglUtil.h"
20 
21 #include <cstring>
22 
23 #include "EglDisplayContext.h"
24 #include "GLES/gl.h"
25 #include "log/log.h"
26 
27 namespace android {
28 namespace companion {
29 namespace virtualcamera {
30 
31 // Lower bound for maximum supported texture size is at least 2048x2048
32 constexpr int kDefaultMaxTextureSize = 2048;
33 
checkEglError(const char * operation)34 bool checkEglError(const char* operation) {
35   GLenum err = glGetError();
36   if (err == GL_NO_ERROR) {
37     return false;
38   }
39   ALOGE("%s failed: %d", operation, err);
40   return true;
41 }
42 
isGlExtensionSupported(const char * extension)43 bool isGlExtensionSupported(const char* extension) {
44   const char* extensions =
45       reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
46   if (extension == nullptr || extensions == nullptr) {
47     return false;
48   }
49   return strstr(extensions, extension) != nullptr;
50 }
51 
getMaximumTextureSize()52 int getMaximumTextureSize() {
53   static const int kMaxTextureSize = [] {
54     EglDisplayContext displayContext;
55     displayContext.makeCurrent();
56     int maxTextureSize = -1;
57     glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
58     return maxTextureSize;
59   }();
60   if (kMaxTextureSize <= 0) {
61     return kDefaultMaxTextureSize;
62   }
63   return kMaxTextureSize;
64 }
65 
66 }  // namespace virtualcamera
67 }  // namespace companion
68 }  // namespace android
69