Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | #undef LOG_TAG |
| 19 | #define LOG_TAG "RenderEngine" |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 21 | |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 22 | #include "GLES20RenderEngine.h" |
| 23 | |
| 24 | #include <math.h> |
| 25 | #include <fstream> |
| 26 | #include <sstream> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 27 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 28 | #include <GLES2/gl2.h> |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 29 | #include <GLES2/gl2ext.h> |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 30 | #include <cutils/compiler.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 31 | #include <renderengine/Mesh.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 32 | #include <renderengine/Texture.h> |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 33 | #include <renderengine/private/Description.h> |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 34 | #include <ui/ColorSpace.h> |
| 35 | #include <ui/DebugUtils.h> |
Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 36 | #include <ui/Rect.h> |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 37 | #include <ui/Region.h> |
Chia-I Wu | 56d7b0a | 2018-10-01 15:13:11 -0700 | [diff] [blame] | 38 | #include <utils/KeyedVector.h> |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 39 | #include <utils/String8.h> |
| 40 | #include <utils/Trace.h> |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 41 | #include "GLExtensions.h" |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 42 | #include "GLFramebuffer.h" |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 43 | #include "GLImage.h" |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 44 | #include "Program.h" |
| 45 | #include "ProgramCache.h" |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 46 | |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 47 | extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name); |
| 48 | |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 49 | bool checkGlError(const char* op, int lineNumber) { |
| 50 | bool errorFound = false; |
| 51 | GLint error = glGetError(); |
| 52 | while (error != GL_NO_ERROR) { |
| 53 | errorFound = true; |
| 54 | error = glGetError(); |
| 55 | ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error); |
| 56 | } |
| 57 | return errorFound; |
| 58 | } |
| 59 | |
Courtney Goeltzenleuchter | 4f20f9c | 2017-04-06 08:18:34 -0600 | [diff] [blame] | 60 | static constexpr bool outputDebugPPMs = false; |
| 61 | |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 62 | void writePPM(const char* basename, GLuint width, GLuint height) { |
| 63 | ALOGV("writePPM #%s: %d x %d", basename, width, height); |
| 64 | |
| 65 | std::vector<GLubyte> pixels(width * height * 4); |
| 66 | std::vector<GLubyte> outBuffer(width * height * 3); |
| 67 | |
| 68 | // TODO(courtneygo): We can now have float formats, need |
| 69 | // to remove this code or update to support. |
| 70 | // Make returned pixels fit in uint32_t, one byte per component |
| 71 | glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data()); |
| 72 | if (checkGlError(__FUNCTION__, __LINE__)) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | std::string filename(basename); |
| 77 | filename.append(".ppm"); |
| 78 | std::ofstream file(filename.c_str(), std::ios::binary); |
| 79 | if (!file.is_open()) { |
| 80 | ALOGE("Unable to open file: %s", filename.c_str()); |
| 81 | ALOGE("You may need to do: \"adb shell setenforce 0\" to enable " |
| 82 | "surfaceflinger to write debug images"); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | file << "P6\n"; |
| 87 | file << width << "\n"; |
| 88 | file << height << "\n"; |
| 89 | file << 255 << "\n"; |
| 90 | |
| 91 | auto ptr = reinterpret_cast<char*>(pixels.data()); |
| 92 | auto outPtr = reinterpret_cast<char*>(outBuffer.data()); |
| 93 | for (int y = height - 1; y >= 0; y--) { |
| 94 | char* data = ptr + y * width * sizeof(uint32_t); |
| 95 | |
| 96 | for (GLuint x = 0; x < width; x++) { |
| 97 | // Only copy R, G and B components |
| 98 | outPtr[0] = data[0]; |
| 99 | outPtr[1] = data[1]; |
| 100 | outPtr[2] = data[2]; |
| 101 | data += sizeof(uint32_t); |
| 102 | outPtr += 3; |
| 103 | } |
| 104 | } |
| 105 | file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size()); |
| 106 | } |
| 107 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 108 | namespace android { |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 109 | namespace renderengine { |
| 110 | namespace gl { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 111 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 112 | using ui::Dataspace; |
| 113 | |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 114 | static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute, |
| 115 | EGLint wanted, EGLConfig* outConfig) { |
| 116 | EGLint numConfigs = -1, n = 0; |
| 117 | eglGetConfigs(dpy, nullptr, 0, &numConfigs); |
Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 118 | std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR); |
| 119 | eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n); |
| 120 | configs.resize(n); |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 121 | |
Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 122 | if (!configs.empty()) { |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 123 | if (attribute != EGL_NONE) { |
Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 124 | for (EGLConfig config : configs) { |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 125 | EGLint value = 0; |
Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 126 | eglGetConfigAttrib(dpy, config, attribute, &value); |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 127 | if (wanted == value) { |
Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 128 | *outConfig = config; |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 129 | return NO_ERROR; |
| 130 | } |
| 131 | } |
| 132 | } else { |
| 133 | // just pick the first one |
| 134 | *outConfig = configs[0]; |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 135 | return NO_ERROR; |
| 136 | } |
| 137 | } |
Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 138 | |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 139 | return NAME_NOT_FOUND; |
| 140 | } |
| 141 | |
| 142 | class EGLAttributeVector { |
| 143 | struct Attribute; |
| 144 | class Adder; |
| 145 | friend class Adder; |
| 146 | KeyedVector<Attribute, EGLint> mList; |
| 147 | struct Attribute { |
| 148 | Attribute() : v(0){}; |
| 149 | explicit Attribute(EGLint v) : v(v) {} |
| 150 | EGLint v; |
| 151 | bool operator<(const Attribute& other) const { |
| 152 | // this places EGL_NONE at the end |
| 153 | EGLint lhs(v); |
| 154 | EGLint rhs(other.v); |
| 155 | if (lhs == EGL_NONE) lhs = 0x7FFFFFFF; |
| 156 | if (rhs == EGL_NONE) rhs = 0x7FFFFFFF; |
| 157 | return lhs < rhs; |
| 158 | } |
| 159 | }; |
| 160 | class Adder { |
| 161 | friend class EGLAttributeVector; |
| 162 | EGLAttributeVector& v; |
| 163 | EGLint attribute; |
| 164 | Adder(EGLAttributeVector& v, EGLint attribute) : v(v), attribute(attribute) {} |
| 165 | |
| 166 | public: |
| 167 | void operator=(EGLint value) { |
| 168 | if (attribute != EGL_NONE) { |
| 169 | v.mList.add(Attribute(attribute), value); |
| 170 | } |
| 171 | } |
| 172 | operator EGLint() const { return v.mList[attribute]; } |
| 173 | }; |
| 174 | |
| 175 | public: |
| 176 | EGLAttributeVector() { mList.add(Attribute(EGL_NONE), EGL_NONE); } |
| 177 | void remove(EGLint attribute) { |
| 178 | if (attribute != EGL_NONE) { |
| 179 | mList.removeItem(Attribute(attribute)); |
| 180 | } |
| 181 | } |
| 182 | Adder operator[](EGLint attribute) { return Adder(*this, attribute); } |
| 183 | EGLint operator[](EGLint attribute) const { return mList[attribute]; } |
| 184 | // cast-operator to (EGLint const*) |
| 185 | operator EGLint const*() const { return &mList.keyAt(0).v; } |
| 186 | }; |
| 187 | |
| 188 | static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType, |
| 189 | EGLConfig* config) { |
| 190 | // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if |
| 191 | // it is to be used with WIFI displays |
| 192 | status_t err; |
| 193 | EGLint wantedAttribute; |
| 194 | EGLint wantedAttributeValue; |
| 195 | |
| 196 | EGLAttributeVector attribs; |
| 197 | if (renderableType) { |
| 198 | attribs[EGL_RENDERABLE_TYPE] = renderableType; |
| 199 | attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE; |
| 200 | attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT; |
| 201 | attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE; |
| 202 | attribs[EGL_RED_SIZE] = 8; |
| 203 | attribs[EGL_GREEN_SIZE] = 8; |
| 204 | attribs[EGL_BLUE_SIZE] = 8; |
| 205 | attribs[EGL_ALPHA_SIZE] = 8; |
| 206 | wantedAttribute = EGL_NONE; |
| 207 | wantedAttributeValue = EGL_NONE; |
| 208 | } else { |
| 209 | // if no renderable type specified, fallback to a simplified query |
| 210 | wantedAttribute = EGL_NATIVE_VISUAL_ID; |
| 211 | wantedAttributeValue = format; |
| 212 | } |
| 213 | |
| 214 | err = selectConfigForAttribute(display, attribs, wantedAttribute, wantedAttributeValue, config); |
| 215 | if (err == NO_ERROR) { |
| 216 | EGLint caveat; |
| 217 | if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat)) |
| 218 | ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!"); |
| 219 | } |
| 220 | |
| 221 | return err; |
| 222 | } |
| 223 | |
| 224 | std::unique_ptr<GLES20RenderEngine> GLES20RenderEngine::create(int hwcFormat, |
| 225 | uint32_t featureFlags) { |
| 226 | // initialize EGL for the default display |
| 227 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 228 | if (!eglInitialize(display, nullptr, nullptr)) { |
| 229 | LOG_ALWAYS_FATAL("failed to initialize EGL"); |
| 230 | } |
| 231 | |
| 232 | GLExtensions& extensions = GLExtensions::getInstance(); |
| 233 | extensions.initWithEGLStrings(eglQueryStringImplementationANDROID(display, EGL_VERSION), |
| 234 | eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS)); |
| 235 | |
| 236 | // The code assumes that ES2 or later is available if this extension is |
| 237 | // supported. |
| 238 | EGLConfig config = EGL_NO_CONFIG; |
| 239 | if (!extensions.hasNoConfigContext()) { |
| 240 | config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true); |
| 241 | } |
| 242 | |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 243 | bool useContextPriority = extensions.hasContextPriority() && |
Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 244 | (featureFlags & RenderEngine::USE_HIGH_PRIORITY_CONTEXT); |
Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 245 | EGLContext ctxt = createEglContext(display, config, EGL_NO_CONTEXT, useContextPriority); |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 246 | |
| 247 | // if can't create a GL context, we can only abort. |
| 248 | LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed"); |
| 249 | |
Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 250 | EGLSurface dummy = EGL_NO_SURFACE; |
| 251 | if (!extensions.hasSurfacelessContext()) { |
| 252 | dummy = createDummyEglPbufferSurface(display, config, hwcFormat); |
| 253 | LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer"); |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 254 | } |
Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 255 | |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 256 | EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt); |
| 257 | LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current"); |
| 258 | |
| 259 | extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER), |
| 260 | glGetString(GL_VERSION), glGetString(GL_EXTENSIONS)); |
| 261 | |
Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 262 | // now figure out what version of GL did we actually get |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 263 | GlesVersion version = parseGlesVersion(extensions.getVersion()); |
| 264 | |
| 265 | // initialize the renderer while GL is current |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 266 | std::unique_ptr<GLES20RenderEngine> engine; |
| 267 | switch (version) { |
| 268 | case GLES_VERSION_1_0: |
| 269 | case GLES_VERSION_1_1: |
| 270 | LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run."); |
| 271 | break; |
| 272 | case GLES_VERSION_2_0: |
| 273 | case GLES_VERSION_3_0: |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 274 | engine = std::make_unique<GLES20RenderEngine>(featureFlags, display, config, ctxt, |
| 275 | dummy); |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 276 | break; |
| 277 | } |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 278 | |
| 279 | ALOGI("OpenGL ES informations:"); |
| 280 | ALOGI("vendor : %s", extensions.getVendor()); |
| 281 | ALOGI("renderer : %s", extensions.getRenderer()); |
| 282 | ALOGI("version : %s", extensions.getVersion()); |
| 283 | ALOGI("extensions: %s", extensions.getExtensions()); |
| 284 | ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize()); |
| 285 | ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims()); |
| 286 | |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 287 | return engine; |
| 288 | } |
| 289 | |
| 290 | EGLConfig GLES20RenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) { |
| 291 | status_t err; |
| 292 | EGLConfig config; |
| 293 | |
| 294 | // First try to get an ES2 config |
| 295 | err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config); |
| 296 | if (err != NO_ERROR) { |
| 297 | // If ES2 fails, try ES1 |
| 298 | err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config); |
| 299 | if (err != NO_ERROR) { |
| 300 | // still didn't work, probably because we're on the emulator... |
| 301 | // try a simplified query |
| 302 | ALOGW("no suitable EGLConfig found, trying a simpler query"); |
| 303 | err = selectEGLConfig(display, format, 0, &config); |
| 304 | if (err != NO_ERROR) { |
| 305 | // this EGL is too lame for android |
| 306 | LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up"); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | if (logConfig) { |
| 312 | // print some debugging info |
| 313 | EGLint r, g, b, a; |
| 314 | eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r); |
| 315 | eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g); |
| 316 | eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b); |
| 317 | eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a); |
| 318 | ALOGI("EGL information:"); |
| 319 | ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR)); |
| 320 | ALOGI("version : %s", eglQueryString(display, EGL_VERSION)); |
| 321 | ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS)); |
| 322 | ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported"); |
| 323 | ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config); |
| 324 | } |
| 325 | |
| 326 | return config; |
| 327 | } |
| 328 | |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 329 | GLES20RenderEngine::GLES20RenderEngine(uint32_t featureFlags, EGLDisplay display, EGLConfig config, |
| 330 | EGLContext ctxt, EGLSurface dummy) |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 331 | : renderengine::impl::RenderEngine(featureFlags), |
Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 332 | mEGLDisplay(display), |
| 333 | mEGLConfig(config), |
| 334 | mEGLContext(ctxt), |
| 335 | mDummySurface(dummy), |
Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 336 | mVpWidth(0), |
| 337 | mVpHeight(0), |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 338 | mUseColorManagement(featureFlags & USE_COLOR_MANAGEMENT) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 339 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
| 340 | glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims); |
| 341 | |
| 342 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 343 | glPixelStorei(GL_PACK_ALIGNMENT, 4); |
| 344 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 345 | const uint16_t protTexData[] = {0}; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 346 | glGenTextures(1, &mProtectedTexName); |
| 347 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); |
| 348 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 349 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 350 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 351 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 352 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 353 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 354 | // mColorBlindnessCorrection = M; |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 355 | |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 356 | if (mUseColorManagement) { |
Valerie Hau | eb8e076 | 2018-11-06 10:10:42 -0800 | [diff] [blame] | 357 | const ColorSpace srgb(ColorSpace::sRGB()); |
| 358 | const ColorSpace displayP3(ColorSpace::DisplayP3()); |
| 359 | const ColorSpace bt2020(ColorSpace::BT2020()); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 360 | |
| 361 | // no chromatic adaptation needed since all color spaces use D65 for their white points. |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 362 | mSrgbToXyz = mat4(srgb.getRGBtoXYZ()); |
| 363 | mDisplayP3ToXyz = mat4(displayP3.getRGBtoXYZ()); |
| 364 | mBt2020ToXyz = mat4(bt2020.getRGBtoXYZ()); |
Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 365 | mXyzToSrgb = mat4(srgb.getXYZtoRGB()); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 366 | mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB()); |
| 367 | mXyzToBt2020 = mat4(bt2020.getXYZtoRGB()); |
Valerie Hau | eb8e076 | 2018-11-06 10:10:42 -0800 | [diff] [blame] | 368 | |
| 369 | // Compute sRGB to Display P3 and BT2020 transform matrix. |
| 370 | // NOTE: For now, we are limiting output wide color space support to |
| 371 | // Display-P3 and BT2020 only. |
| 372 | mSrgbToDisplayP3 = mXyzToDisplayP3 * mSrgbToXyz; |
| 373 | mSrgbToBt2020 = mXyzToBt2020 * mSrgbToXyz; |
| 374 | |
| 375 | // Compute Display P3 to sRGB and BT2020 transform matrix. |
| 376 | mDisplayP3ToSrgb = mXyzToSrgb * mDisplayP3ToXyz; |
| 377 | mDisplayP3ToBt2020 = mXyzToBt2020 * mDisplayP3ToXyz; |
| 378 | |
| 379 | // Compute BT2020 to sRGB and Display P3 transform matrix |
| 380 | mBt2020ToSrgb = mXyzToSrgb * mBt2020ToXyz; |
| 381 | mBt2020ToDisplayP3 = mXyzToDisplayP3 * mBt2020ToXyz; |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 382 | } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 385 | GLES20RenderEngine::~GLES20RenderEngine() { |
| 386 | eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 387 | eglTerminate(mEGLDisplay); |
| 388 | } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 389 | |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 390 | std::unique_ptr<Framebuffer> GLES20RenderEngine::createFramebuffer() { |
| 391 | return std::make_unique<GLFramebuffer>(*this); |
| 392 | } |
| 393 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 394 | std::unique_ptr<Image> GLES20RenderEngine::createImage() { |
| 395 | return std::make_unique<GLImage>(*this); |
| 396 | } |
| 397 | |
| 398 | void GLES20RenderEngine::primeCache() const { |
| 399 | ProgramCache::getInstance().primeCache(mFeatureFlags & USE_COLOR_MANAGEMENT); |
| 400 | } |
| 401 | |
| 402 | bool GLES20RenderEngine::isCurrent() const { |
| 403 | return mEGLDisplay == eglGetCurrentDisplay() && mEGLContext == eglGetCurrentContext(); |
| 404 | } |
| 405 | |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 406 | base::unique_fd GLES20RenderEngine::flush() { |
| 407 | if (!GLExtensions::getInstance().hasNativeFenceSync()) { |
| 408 | return base::unique_fd(); |
| 409 | } |
| 410 | |
| 411 | EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr); |
| 412 | if (sync == EGL_NO_SYNC_KHR) { |
| 413 | ALOGW("failed to create EGL native fence sync: %#x", eglGetError()); |
| 414 | return base::unique_fd(); |
| 415 | } |
| 416 | |
| 417 | // native fence fd will not be populated until flush() is done. |
| 418 | glFlush(); |
| 419 | |
| 420 | // get the fence fd |
| 421 | base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync)); |
| 422 | eglDestroySyncKHR(mEGLDisplay, sync); |
| 423 | if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) { |
| 424 | ALOGW("failed to dup EGL native fence sync: %#x", eglGetError()); |
| 425 | } |
| 426 | |
| 427 | return fenceFd; |
| 428 | } |
| 429 | |
| 430 | bool GLES20RenderEngine::finish() { |
| 431 | if (!GLExtensions::getInstance().hasFenceSync()) { |
| 432 | ALOGW("no synchronization support"); |
| 433 | return false; |
| 434 | } |
| 435 | |
| 436 | EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr); |
| 437 | if (sync == EGL_NO_SYNC_KHR) { |
| 438 | ALOGW("failed to create EGL fence sync: %#x", eglGetError()); |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, |
| 443 | 2000000000 /*2 sec*/); |
| 444 | EGLint error = eglGetError(); |
| 445 | eglDestroySyncKHR(mEGLDisplay, sync); |
| 446 | if (result != EGL_CONDITION_SATISFIED_KHR) { |
| 447 | if (result == EGL_TIMEOUT_EXPIRED_KHR) { |
| 448 | ALOGW("fence wait timed out"); |
| 449 | } else { |
| 450 | ALOGW("error waiting on EGL fence: %#x", error); |
| 451 | } |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | return true; |
| 456 | } |
| 457 | |
| 458 | bool GLES20RenderEngine::waitFence(base::unique_fd fenceFd) { |
| 459 | if (!GLExtensions::getInstance().hasNativeFenceSync() || |
| 460 | !GLExtensions::getInstance().hasWaitSync()) { |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE}; |
| 465 | EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs); |
| 466 | if (sync == EGL_NO_SYNC_KHR) { |
| 467 | ALOGE("failed to create EGL native fence sync: %#x", eglGetError()); |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | // fenceFd is now owned by EGLSync |
| 472 | (void)fenceFd.release(); |
| 473 | |
| 474 | // XXX: The spec draft is inconsistent as to whether this should return an |
| 475 | // EGLint or void. Ignore the return value for now, as it's not strictly |
| 476 | // needed. |
| 477 | eglWaitSyncKHR(mEGLDisplay, sync, 0); |
| 478 | EGLint error = eglGetError(); |
| 479 | eglDestroySyncKHR(mEGLDisplay, sync); |
| 480 | if (error != EGL_SUCCESS) { |
| 481 | ALOGE("failed to wait for EGL native fence sync: %#x", error); |
| 482 | return false; |
| 483 | } |
| 484 | |
| 485 | return true; |
| 486 | } |
| 487 | |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 488 | void GLES20RenderEngine::clearWithColor(float red, float green, float blue, float alpha) { |
| 489 | glClearColor(red, green, blue, alpha); |
| 490 | glClear(GL_COLOR_BUFFER_BIT); |
| 491 | } |
| 492 | |
Chia-I Wu | 28e3a25 | 2018-09-07 12:05:02 -0700 | [diff] [blame] | 493 | void GLES20RenderEngine::fillRegionWithColor(const Region& region, float red, float green, |
| 494 | float blue, float alpha) { |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 495 | size_t c; |
| 496 | Rect const* r = region.getArray(&c); |
| 497 | Mesh mesh(Mesh::TRIANGLES, c * 6, 2); |
| 498 | Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>()); |
| 499 | for (size_t i = 0; i < c; i++, r++) { |
| 500 | position[i * 6 + 0].x = r->left; |
Chia-I Wu | 28e3a25 | 2018-09-07 12:05:02 -0700 | [diff] [blame] | 501 | position[i * 6 + 0].y = r->top; |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 502 | position[i * 6 + 1].x = r->left; |
Chia-I Wu | 28e3a25 | 2018-09-07 12:05:02 -0700 | [diff] [blame] | 503 | position[i * 6 + 1].y = r->bottom; |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 504 | position[i * 6 + 2].x = r->right; |
Chia-I Wu | 28e3a25 | 2018-09-07 12:05:02 -0700 | [diff] [blame] | 505 | position[i * 6 + 2].y = r->bottom; |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 506 | position[i * 6 + 3].x = r->left; |
Chia-I Wu | 28e3a25 | 2018-09-07 12:05:02 -0700 | [diff] [blame] | 507 | position[i * 6 + 3].y = r->top; |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 508 | position[i * 6 + 4].x = r->right; |
Chia-I Wu | 28e3a25 | 2018-09-07 12:05:02 -0700 | [diff] [blame] | 509 | position[i * 6 + 4].y = r->bottom; |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 510 | position[i * 6 + 5].x = r->right; |
Chia-I Wu | 28e3a25 | 2018-09-07 12:05:02 -0700 | [diff] [blame] | 511 | position[i * 6 + 5].y = r->top; |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 512 | } |
| 513 | setupFillWithColor(red, green, blue, alpha); |
| 514 | drawMesh(mesh); |
| 515 | } |
| 516 | |
Alec Mouri | 05483a0 | 2018-09-10 21:03:42 +0000 | [diff] [blame] | 517 | void GLES20RenderEngine::setScissor(const Rect& region) { |
| 518 | // Invert y-coordinate to map to GL-space. |
Alec Mouri | 7e59391 | 2018-11-17 04:57:33 +0000 | [diff] [blame] | 519 | int32_t canvasHeight = mFboHeight; |
Alec Mouri | 05483a0 | 2018-09-10 21:03:42 +0000 | [diff] [blame] | 520 | int32_t glBottom = canvasHeight - region.bottom; |
| 521 | |
| 522 | glScissor(region.left, glBottom, region.getWidth(), region.getHeight()); |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 523 | glEnable(GL_SCISSOR_TEST); |
| 524 | } |
| 525 | |
| 526 | void GLES20RenderEngine::disableScissor() { |
| 527 | glDisable(GL_SCISSOR_TEST); |
| 528 | } |
| 529 | |
| 530 | void GLES20RenderEngine::genTextures(size_t count, uint32_t* names) { |
| 531 | glGenTextures(count, names); |
| 532 | } |
| 533 | |
| 534 | void GLES20RenderEngine::deleteTextures(size_t count, uint32_t const* names) { |
| 535 | glDeleteTextures(count, names); |
| 536 | } |
| 537 | |
Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 538 | void GLES20RenderEngine::bindExternalTextureImage(uint32_t texName, const Image& image) { |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 539 | const GLImage& glImage = static_cast<const GLImage&>(image); |
| 540 | const GLenum target = GL_TEXTURE_EXTERNAL_OES; |
| 541 | |
| 542 | glBindTexture(target, texName); |
| 543 | if (glImage.getEGLImage() != EGL_NO_IMAGE_KHR) { |
Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 544 | glEGLImageTargetTexture2DOES(target, static_cast<GLeglImageOES>(glImage.getEGLImage())); |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 545 | } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 548 | status_t GLES20RenderEngine::bindFrameBuffer(Framebuffer* framebuffer) { |
| 549 | GLFramebuffer* glFramebuffer = static_cast<GLFramebuffer*>(framebuffer); |
| 550 | EGLImageKHR eglImage = glFramebuffer->getEGLImage(); |
| 551 | uint32_t textureName = glFramebuffer->getTextureName(); |
| 552 | uint32_t framebufferName = glFramebuffer->getFramebufferName(); |
| 553 | |
| 554 | // Bind the texture and turn our EGLImage into a texture |
| 555 | glBindTexture(GL_TEXTURE_2D, textureName); |
| 556 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)eglImage); |
| 557 | |
| 558 | // Bind the Framebuffer to render into |
| 559 | glBindFramebuffer(GL_FRAMEBUFFER, framebufferName); |
Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 560 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureName, 0); |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 561 | |
Alec Mouri | 05483a0 | 2018-09-10 21:03:42 +0000 | [diff] [blame] | 562 | mFboHeight = glFramebuffer->getBufferHeight(); |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 563 | |
| 564 | uint32_t glStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 565 | |
Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 566 | ALOGE_IF(glStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d", |
| 567 | glStatus); |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 568 | |
| 569 | return glStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE; |
| 570 | } |
| 571 | |
| 572 | void GLES20RenderEngine::unbindFrameBuffer(Framebuffer* /* framebuffer */) { |
Alec Mouri | 05483a0 | 2018-09-10 21:03:42 +0000 | [diff] [blame] | 573 | mFboHeight = 0; |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 574 | |
| 575 | // back to main framebuffer |
| 576 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 579 | void GLES20RenderEngine::checkErrors() const { |
| 580 | do { |
| 581 | // there could be more than one error flag |
| 582 | GLenum error = glGetError(); |
| 583 | if (error == GL_NO_ERROR) break; |
| 584 | ALOGE("GL error 0x%04x", int(error)); |
| 585 | } while (true); |
| 586 | } |
| 587 | |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 588 | status_t GLES20RenderEngine::drawLayers(const DisplaySettings& /*settings*/, |
| 589 | const std::vector<LayerSettings>& /*layers*/, |
| 590 | ANativeWindowBuffer* const /*buffer*/, |
| 591 | base::unique_fd* /*displayFence*/) const { |
| 592 | return NO_ERROR; |
| 593 | } |
| 594 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 595 | void GLES20RenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop, |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 596 | ui::Transform::orientation_flags rotation) { |
Ivan Lozano | 1f58ac5 | 2017-12-14 13:27:10 -0800 | [diff] [blame] | 597 | int32_t l = sourceCrop.left; |
| 598 | int32_t r = sourceCrop.right; |
Chia-I Wu | 1be50b5 | 2018-08-29 10:44:48 -0700 | [diff] [blame] | 599 | int32_t b = sourceCrop.bottom; |
| 600 | int32_t t = sourceCrop.top; |
Alec Mouri | 7e59391 | 2018-11-17 04:57:33 +0000 | [diff] [blame] | 601 | std::swap(t, b); |
Chia-I Wu | 1be50b5 | 2018-08-29 10:44:48 -0700 | [diff] [blame] | 602 | mat4 m = mat4::ortho(l, r, b, t, 0, 1); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 603 | |
Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 604 | // Apply custom rotation to the projection. |
| 605 | float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f; |
| 606 | switch (rotation) { |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 607 | case ui::Transform::ROT_0: |
Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 608 | break; |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 609 | case ui::Transform::ROT_90: |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 610 | m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m; |
Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 611 | break; |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 612 | case ui::Transform::ROT_180: |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 613 | m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m; |
Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 614 | break; |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 615 | case ui::Transform::ROT_270: |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 616 | m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m; |
Riley Andrews | c3ebe66 | 2014-09-04 16:20:31 -0700 | [diff] [blame] | 617 | break; |
| 618 | default: |
| 619 | break; |
| 620 | } |
| 621 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 622 | glViewport(0, 0, vpw, vph); |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 623 | mState.projectionMatrix = m; |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 624 | mVpWidth = vpw; |
| 625 | mVpHeight = vph; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 628 | void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque, |
Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame^] | 629 | bool disableTexture, const half4& color, |
| 630 | float cornerRadius) { |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 631 | mState.isPremultipliedAlpha = premultipliedAlpha; |
| 632 | mState.isOpaque = opaque; |
| 633 | mState.color = color; |
Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame^] | 634 | mState.cornerRadius = cornerRadius; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 635 | |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 636 | if (disableTexture) { |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 637 | mState.textureEnabled = false; |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 638 | } |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 639 | |
Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame^] | 640 | if (color.a < 1.0f || !opaque || cornerRadius > 0.0f) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 641 | glEnable(GL_BLEND); |
| 642 | glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 643 | } else { |
| 644 | glDisable(GL_BLEND); |
| 645 | } |
| 646 | } |
| 647 | |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 648 | void GLES20RenderEngine::setSourceY410BT2020(bool enable) { |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 649 | mState.isY410BT2020 = enable; |
Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 650 | } |
| 651 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 652 | void GLES20RenderEngine::setSourceDataSpace(Dataspace source) { |
Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 653 | mDataSpace = source; |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 654 | } |
| 655 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 656 | void GLES20RenderEngine::setOutputDataSpace(Dataspace dataspace) { |
Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 657 | mOutputDataSpace = dataspace; |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 658 | } |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 659 | |
Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 660 | void GLES20RenderEngine::setDisplayMaxLuminance(const float maxLuminance) { |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 661 | mState.displayMaxLuminance = maxLuminance; |
Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 664 | void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) { |
| 665 | GLuint target = texture.getTextureTarget(); |
| 666 | glBindTexture(target, texture.getTextureName()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 667 | GLenum filter = GL_NEAREST; |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 668 | if (texture.getFiltering()) { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 669 | filter = GL_LINEAR; |
| 670 | } |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 671 | glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 672 | glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 673 | glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter); |
| 674 | glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 675 | |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 676 | mState.texture = texture; |
| 677 | mState.textureEnabled = true; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | void GLES20RenderEngine::setupLayerBlackedOut() { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 681 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 682 | Texture texture(Texture::TEXTURE_2D, mProtectedTexName); |
| 683 | texture.setDimensions(1, 1); // FIXME: we should get that from somewhere |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 684 | mState.texture = texture; |
| 685 | mState.textureEnabled = true; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Peiyong Lin | d378863 | 2018-09-18 16:01:31 -0700 | [diff] [blame] | 688 | void GLES20RenderEngine::setColorTransform(const mat4& colorTransform) { |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 689 | mState.colorMatrix = colorTransform; |
Dan Stoza | f008799 | 2014-10-20 15:46:09 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 692 | void GLES20RenderEngine::disableTexturing() { |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 693 | mState.textureEnabled = false; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | void GLES20RenderEngine::disableBlending() { |
| 697 | glDisable(GL_BLEND); |
| 698 | } |
| 699 | |
Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 700 | void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) { |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 701 | mState.isPremultipliedAlpha = true; |
| 702 | mState.isOpaque = false; |
| 703 | mState.color = half4(r, g, b, a); |
| 704 | mState.textureEnabled = false; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 705 | glDisable(GL_BLEND); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame^] | 708 | void GLES20RenderEngine::setupCornerRadiusCropSize(float width, float height) { |
| 709 | mState.cropSize = half2(width, height); |
| 710 | } |
| 711 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 712 | void GLES20RenderEngine::drawMesh(const Mesh& mesh) { |
Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 713 | ATRACE_CALL(); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 714 | if (mesh.getTexCoordsSize()) { |
| 715 | glEnableVertexAttribArray(Program::texCoords); |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 716 | glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE, |
| 717 | mesh.getByteStride(), mesh.getTexCoords()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 720 | glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE, |
| 721 | mesh.getByteStride(), mesh.getPositions()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 722 | |
Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame^] | 723 | if (mState.cornerRadius > 0.0f) { |
| 724 | glEnableVertexAttribArray(Program::cropCoords); |
| 725 | glVertexAttribPointer(Program::cropCoords, mesh.getVertexSize(), GL_FLOAT, GL_FALSE, |
| 726 | mesh.getByteStride(), mesh.getCropCoords()); |
| 727 | } |
| 728 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 729 | // By default, DISPLAY_P3 is the only supported wide color output. However, |
| 730 | // when HDR content is present, hardware composer may be able to handle |
| 731 | // BT2020 data space, in that case, the output data space is set to be |
| 732 | // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need |
| 733 | // to respect this and convert non-HDR content to HDR format. |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 734 | if (mUseColorManagement) { |
| 735 | Description managedState = mState; |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 736 | Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK); |
| 737 | Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK); |
Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 738 | Dataspace outputStandard = |
| 739 | static_cast<Dataspace>(mOutputDataSpace & Dataspace::STANDARD_MASK); |
| 740 | Dataspace outputTransfer = |
| 741 | static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 742 | bool needsXYZConversion = needsXYZTransformMatrix(); |
| 743 | |
Valerie Hau | eb8e076 | 2018-11-06 10:10:42 -0800 | [diff] [blame] | 744 | // NOTE: if the input standard of the input dataspace is not STANDARD_DCI_P3 or |
| 745 | // STANDARD_BT2020, it will be treated as STANDARD_BT709 |
| 746 | if (inputStandard != Dataspace::STANDARD_DCI_P3 && |
| 747 | inputStandard != Dataspace::STANDARD_BT2020) { |
| 748 | inputStandard = Dataspace::STANDARD_BT709; |
| 749 | } |
| 750 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 751 | if (needsXYZConversion) { |
| 752 | // The supported input color spaces are standard RGB, Display P3 and BT2020. |
| 753 | switch (inputStandard) { |
| 754 | case Dataspace::STANDARD_DCI_P3: |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 755 | managedState.inputTransformMatrix = mDisplayP3ToXyz; |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 756 | break; |
| 757 | case Dataspace::STANDARD_BT2020: |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 758 | managedState.inputTransformMatrix = mBt2020ToXyz; |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 759 | break; |
| 760 | default: |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 761 | managedState.inputTransformMatrix = mSrgbToXyz; |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 762 | break; |
| 763 | } |
| 764 | |
Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 765 | // The supported output color spaces are BT2020, Display P3 and standard RGB. |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 766 | switch (outputStandard) { |
| 767 | case Dataspace::STANDARD_BT2020: |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 768 | managedState.outputTransformMatrix = mXyzToBt2020; |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 769 | break; |
Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 770 | case Dataspace::STANDARD_DCI_P3: |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 771 | managedState.outputTransformMatrix = mXyzToDisplayP3; |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 772 | break; |
Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 773 | default: |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 774 | managedState.outputTransformMatrix = mXyzToSrgb; |
Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 775 | break; |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 776 | } |
| 777 | } else if (inputStandard != outputStandard) { |
| 778 | // At this point, the input data space and output data space could be both |
| 779 | // HDR data spaces, but they match each other, we do nothing in this case. |
| 780 | // In addition to the case above, the input data space could be |
| 781 | // - scRGB linear |
| 782 | // - scRGB non-linear |
| 783 | // - sRGB |
| 784 | // - Display P3 |
Valerie Hau | eb8e076 | 2018-11-06 10:10:42 -0800 | [diff] [blame] | 785 | // - BT2020 |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 786 | // The output data spaces could be |
| 787 | // - sRGB |
| 788 | // - Display P3 |
Valerie Hau | eb8e076 | 2018-11-06 10:10:42 -0800 | [diff] [blame] | 789 | // - BT2020 |
| 790 | switch (outputStandard) { |
| 791 | case Dataspace::STANDARD_BT2020: |
| 792 | if (inputStandard == Dataspace::STANDARD_BT709) { |
| 793 | managedState.outputTransformMatrix = mSrgbToBt2020; |
| 794 | } else if (inputStandard == Dataspace::STANDARD_DCI_P3) { |
| 795 | managedState.outputTransformMatrix = mDisplayP3ToBt2020; |
| 796 | } |
| 797 | break; |
| 798 | case Dataspace::STANDARD_DCI_P3: |
| 799 | if (inputStandard == Dataspace::STANDARD_BT709) { |
| 800 | managedState.outputTransformMatrix = mSrgbToDisplayP3; |
| 801 | } else if (inputStandard == Dataspace::STANDARD_BT2020) { |
| 802 | managedState.outputTransformMatrix = mBt2020ToDisplayP3; |
| 803 | } |
| 804 | break; |
| 805 | default: |
| 806 | if (inputStandard == Dataspace::STANDARD_DCI_P3) { |
| 807 | managedState.outputTransformMatrix = mDisplayP3ToSrgb; |
| 808 | } else if (inputStandard == Dataspace::STANDARD_BT2020) { |
| 809 | managedState.outputTransformMatrix = mBt2020ToSrgb; |
| 810 | } |
| 811 | break; |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 812 | } |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 813 | } |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 814 | |
| 815 | // we need to convert the RGB value to linear space and convert it back when: |
| 816 | // - there is a color matrix that is not an identity matrix, or |
| 817 | // - there is an output transform matrix that is not an identity matrix, or |
| 818 | // - the input transfer function doesn't match the output transfer function. |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 819 | if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() || |
Chia-I Wu | d49d669 | 2018-06-27 07:17:41 +0800 | [diff] [blame] | 820 | inputTransfer != outputTransfer) { |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 821 | managedState.inputTransferFunction = |
Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 822 | Description::dataSpaceToTransferFunction(inputTransfer); |
Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 823 | managedState.outputTransferFunction = |
Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 824 | Description::dataSpaceToTransferFunction(outputTransfer); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 827 | ProgramCache::getInstance().useProgram(managedState); |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 828 | |
| 829 | glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); |
| 830 | |
| 831 | if (outputDebugPPMs) { |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 832 | static uint64_t managedColorFrameCount = 0; |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 833 | std::ostringstream out; |
Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 834 | out << "/data/texture_out" << managedColorFrameCount++; |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 835 | writePPM(out.str().c_str(), mVpWidth, mVpHeight); |
| 836 | } |
| 837 | } else { |
| 838 | ProgramCache::getInstance().useProgram(mState); |
| 839 | |
| 840 | glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); |
| 841 | } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 842 | |
| 843 | if (mesh.getTexCoordsSize()) { |
| 844 | glDisableVertexAttribArray(Program::texCoords); |
| 845 | } |
Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame^] | 846 | |
| 847 | if (mState.cornerRadius > 0.0f) { |
| 848 | glDisableVertexAttribArray(Program::cropCoords); |
| 849 | } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 852 | size_t GLES20RenderEngine::getMaxTextureSize() const { |
| 853 | return mMaxTextureSize; |
| 854 | } |
| 855 | |
| 856 | size_t GLES20RenderEngine::getMaxViewportDims() const { |
| 857 | return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1]; |
| 858 | } |
| 859 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 860 | void GLES20RenderEngine::dump(String8& result) { |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 861 | const GLExtensions& extensions = GLExtensions::getInstance(); |
| 862 | |
| 863 | result.appendFormat("EGL implementation : %s\n", extensions.getEGLVersion()); |
| 864 | result.appendFormat("%s\n", extensions.getEGLExtensions()); |
| 865 | |
| 866 | result.appendFormat("GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(), |
| 867 | extensions.getVersion()); |
| 868 | result.appendFormat("%s\n", extensions.getExtensions()); |
Chia-I Wu | 56d7b0a | 2018-10-01 15:13:11 -0700 | [diff] [blame] | 869 | |
| 870 | result.appendFormat("RenderEngine program cache size: %zu\n", |
| 871 | ProgramCache::getInstance().getSize()); |
| 872 | |
Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 873 | result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n", |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 874 | dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(), |
| 875 | dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 876 | } |
| 877 | |
Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 878 | GLES20RenderEngine::GlesVersion GLES20RenderEngine::parseGlesVersion(const char* str) { |
| 879 | int major, minor; |
| 880 | if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) { |
| 881 | if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) { |
| 882 | ALOGW("Unable to parse GL_VERSION string: \"%s\"", str); |
| 883 | return GLES_VERSION_1_0; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | if (major == 1 && minor == 0) return GLES_VERSION_1_0; |
| 888 | if (major == 1 && minor >= 1) return GLES_VERSION_1_1; |
| 889 | if (major == 2 && minor >= 0) return GLES_VERSION_2_0; |
| 890 | if (major == 3 && minor >= 0) return GLES_VERSION_3_0; |
| 891 | |
| 892 | ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor); |
| 893 | return GLES_VERSION_1_0; |
| 894 | } |
| 895 | |
Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 896 | EGLContext GLES20RenderEngine::createEglContext(EGLDisplay display, EGLConfig config, |
| 897 | EGLContext shareContext, bool useContextPriority) { |
| 898 | EGLint renderableType = 0; |
| 899 | if (config == EGL_NO_CONFIG) { |
| 900 | renderableType = EGL_OPENGL_ES2_BIT; |
| 901 | } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) { |
| 902 | LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE"); |
| 903 | } |
| 904 | EGLint contextClientVersion = 0; |
| 905 | if (renderableType & EGL_OPENGL_ES2_BIT) { |
| 906 | contextClientVersion = 2; |
| 907 | } else if (renderableType & EGL_OPENGL_ES_BIT) { |
| 908 | contextClientVersion = 1; |
| 909 | } else { |
| 910 | LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs"); |
| 911 | } |
| 912 | |
| 913 | std::vector<EGLint> contextAttributes; |
| 914 | contextAttributes.reserve(5); |
| 915 | contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION); |
| 916 | contextAttributes.push_back(contextClientVersion); |
| 917 | if (useContextPriority) { |
| 918 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG); |
| 919 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG); |
| 920 | } |
| 921 | contextAttributes.push_back(EGL_NONE); |
| 922 | |
| 923 | return eglCreateContext(display, config, shareContext, contextAttributes.data()); |
| 924 | } |
| 925 | |
| 926 | EGLSurface GLES20RenderEngine::createDummyEglPbufferSurface(EGLDisplay display, EGLConfig config, |
| 927 | int hwcFormat) { |
| 928 | EGLConfig dummyConfig = config; |
| 929 | if (dummyConfig == EGL_NO_CONFIG) { |
| 930 | dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true); |
| 931 | } |
| 932 | std::vector<EGLint> attributes; |
| 933 | attributes.reserve(5); |
| 934 | attributes.push_back(EGL_WIDTH); |
| 935 | attributes.push_back(1); |
| 936 | attributes.push_back(EGL_HEIGHT); |
| 937 | attributes.push_back(1); |
| 938 | attributes.push_back(EGL_NONE); |
| 939 | |
| 940 | return eglCreatePbufferSurface(display, dummyConfig, attributes.data()); |
| 941 | } |
| 942 | |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 943 | bool GLES20RenderEngine::isHdrDataSpace(const Dataspace dataSpace) const { |
| 944 | const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK); |
| 945 | const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK); |
| 946 | return standard == Dataspace::STANDARD_BT2020 && |
Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 947 | (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | // For convenience, we want to convert the input color space to XYZ color space first, |
| 951 | // and then convert from XYZ color space to output color space when |
| 952 | // - SDR and HDR contents are mixed, either SDR content will be converted to HDR or |
| 953 | // HDR content will be tone-mapped to SDR; Or, |
| 954 | // - there are HDR PQ and HLG contents presented at the same time, where we want to convert |
| 955 | // HLG content to PQ content. |
| 956 | // In either case above, we need to operate the Y value in XYZ color space. Thus, when either |
| 957 | // input data space or output data space is HDR data space, and the input transfer function |
| 958 | // doesn't match the output transfer function, we would enable an intermediate transfrom to |
| 959 | // XYZ color space. |
| 960 | bool GLES20RenderEngine::needsXYZTransformMatrix() const { |
| 961 | const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace); |
| 962 | const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace); |
| 963 | const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK); |
Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 964 | const Dataspace outputTransfer = |
| 965 | static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK); |
Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 966 | |
| 967 | return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer; |
| 968 | } |
| 969 | |
Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 970 | } // namespace gl |
| 971 | } // namespace renderengine |
| 972 | } // namespace android |