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