| /* |
| * Copyright 2013 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| //#define LOG_NDEBUG 0 |
| #undef LOG_TAG |
| #define LOG_TAG "RenderEngine" |
| #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| |
| #include "GLES20RenderEngine.h" |
| |
| #include <math.h> |
| #include <fstream> |
| #include <sstream> |
| |
| #include <GLES2/gl2.h> |
| #include <GLES2/gl2ext.h> |
| #include <cutils/compiler.h> |
| #include <renderengine/Mesh.h> |
| #include <renderengine/Texture.h> |
| #include <renderengine/private/Description.h> |
| #include <ui/ColorSpace.h> |
| #include <ui/DebugUtils.h> |
| #include <ui/Rect.h> |
| #include <ui/Region.h> |
| #include <utils/String8.h> |
| #include <utils/Trace.h> |
| #include "GLExtensions.h" |
| #include "GLFramebuffer.h" |
| #include "GLImage.h" |
| #include "GLSurface.h" |
| #include "Program.h" |
| #include "ProgramCache.h" |
| |
| extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name); |
| |
| bool checkGlError(const char* op, int lineNumber) { |
| bool errorFound = false; |
| GLint error = glGetError(); |
| while (error != GL_NO_ERROR) { |
| errorFound = true; |
| error = glGetError(); |
| ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error); |
| } |
| return errorFound; |
| } |
| |
| static constexpr bool outputDebugPPMs = false; |
| |
| void writePPM(const char* basename, GLuint width, GLuint height) { |
| ALOGV("writePPM #%s: %d x %d", basename, width, height); |
| |
| std::vector<GLubyte> pixels(width * height * 4); |
| std::vector<GLubyte> outBuffer(width * height * 3); |
| |
| // TODO(courtneygo): We can now have float formats, need |
| // to remove this code or update to support. |
| // Make returned pixels fit in uint32_t, one byte per component |
| glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data()); |
| if (checkGlError(__FUNCTION__, __LINE__)) { |
| return; |
| } |
| |
| std::string filename(basename); |
| filename.append(".ppm"); |
| std::ofstream file(filename.c_str(), std::ios::binary); |
| if (!file.is_open()) { |
| ALOGE("Unable to open file: %s", filename.c_str()); |
| ALOGE("You may need to do: \"adb shell setenforce 0\" to enable " |
| "surfaceflinger to write debug images"); |
| return; |
| } |
| |
| file << "P6\n"; |
| file << width << "\n"; |
| file << height << "\n"; |
| file << 255 << "\n"; |
| |
| auto ptr = reinterpret_cast<char*>(pixels.data()); |
| auto outPtr = reinterpret_cast<char*>(outBuffer.data()); |
| for (int y = height - 1; y >= 0; y--) { |
| char* data = ptr + y * width * sizeof(uint32_t); |
| |
| for (GLuint x = 0; x < width; x++) { |
| // Only copy R, G and B components |
| outPtr[0] = data[0]; |
| outPtr[1] = data[1]; |
| outPtr[2] = data[2]; |
| data += sizeof(uint32_t); |
| outPtr += 3; |
| } |
| } |
| file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size()); |
| } |
| |
| namespace android { |
| namespace renderengine { |
| namespace gl { |
| |
| using ui::Dataspace; |
| |
| static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute, |
| EGLint wanted, EGLConfig* outConfig) { |
| EGLint numConfigs = -1, n = 0; |
| eglGetConfigs(dpy, nullptr, 0, &numConfigs); |
| EGLConfig* const configs = new EGLConfig[numConfigs]; |
| eglChooseConfig(dpy, attrs, configs, numConfigs, &n); |
| |
| if (n) { |
| if (attribute != EGL_NONE) { |
| for (int i = 0; i < n; i++) { |
| EGLint value = 0; |
| eglGetConfigAttrib(dpy, configs[i], attribute, &value); |
| if (wanted == value) { |
| *outConfig = configs[i]; |
| delete[] configs; |
| return NO_ERROR; |
| } |
| } |
| } else { |
| // just pick the first one |
| *outConfig = configs[0]; |
| delete[] configs; |
| return NO_ERROR; |
| } |
| } |
| delete[] configs; |
| return NAME_NOT_FOUND; |
| } |
| |
| class EGLAttributeVector { |
| struct Attribute; |
| class Adder; |
| friend class Adder; |
| KeyedVector<Attribute, EGLint> mList; |
| struct Attribute { |
| Attribute() : v(0){}; |
| explicit Attribute(EGLint v) : v(v) {} |
| EGLint v; |
| bool operator<(const Attribute& other) const { |
| // this places EGL_NONE at the end |
| EGLint lhs(v); |
| EGLint rhs(other.v); |
| if (lhs == EGL_NONE) lhs = 0x7FFFFFFF; |
| if (rhs == EGL_NONE) rhs = 0x7FFFFFFF; |
| return lhs < rhs; |
| } |
| }; |
| class Adder { |
| friend class EGLAttributeVector; |
| EGLAttributeVector& v; |
| EGLint attribute; |
| Adder(EGLAttributeVector& v, EGLint attribute) : v(v), attribute(attribute) {} |
| |
| public: |
| void operator=(EGLint value) { |
| if (attribute != EGL_NONE) { |
| v.mList.add(Attribute(attribute), value); |
| } |
| } |
| operator EGLint() const { return v.mList[attribute]; } |
| }; |
| |
| public: |
| EGLAttributeVector() { mList.add(Attribute(EGL_NONE), EGL_NONE); } |
| void remove(EGLint attribute) { |
| if (attribute != EGL_NONE) { |
| mList.removeItem(Attribute(attribute)); |
| } |
| } |
| Adder operator[](EGLint attribute) { return Adder(*this, attribute); } |
| EGLint operator[](EGLint attribute) const { return mList[attribute]; } |
| // cast-operator to (EGLint const*) |
| operator EGLint const*() const { return &mList.keyAt(0).v; } |
| }; |
| |
| static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType, |
| EGLConfig* config) { |
| // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if |
| // it is to be used with WIFI displays |
| status_t err; |
| EGLint wantedAttribute; |
| EGLint wantedAttributeValue; |
| |
| EGLAttributeVector attribs; |
| if (renderableType) { |
| attribs[EGL_RENDERABLE_TYPE] = renderableType; |
| attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE; |
| attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT; |
| attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE; |
| attribs[EGL_RED_SIZE] = 8; |
| attribs[EGL_GREEN_SIZE] = 8; |
| attribs[EGL_BLUE_SIZE] = 8; |
| attribs[EGL_ALPHA_SIZE] = 8; |
| wantedAttribute = EGL_NONE; |
| wantedAttributeValue = EGL_NONE; |
| } else { |
| // if no renderable type specified, fallback to a simplified query |
| wantedAttribute = EGL_NATIVE_VISUAL_ID; |
| wantedAttributeValue = format; |
| } |
| |
| err = selectConfigForAttribute(display, attribs, wantedAttribute, wantedAttributeValue, config); |
| if (err == NO_ERROR) { |
| EGLint caveat; |
| if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat)) |
| ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!"); |
| } |
| |
| return err; |
| } |
| |
| std::unique_ptr<GLES20RenderEngine> GLES20RenderEngine::create(int hwcFormat, |
| uint32_t featureFlags) { |
| // initialize EGL for the default display |
| EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| if (!eglInitialize(display, nullptr, nullptr)) { |
| LOG_ALWAYS_FATAL("failed to initialize EGL"); |
| } |
| |
| GLExtensions& extensions = GLExtensions::getInstance(); |
| extensions.initWithEGLStrings(eglQueryStringImplementationANDROID(display, EGL_VERSION), |
| eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS)); |
| |
| // The code assumes that ES2 or later is available if this extension is |
| // supported. |
| EGLConfig config = EGL_NO_CONFIG; |
| if (!extensions.hasNoConfigContext()) { |
| config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true); |
| } |
| |
| EGLint renderableType = 0; |
| if (config == EGL_NO_CONFIG) { |
| renderableType = EGL_OPENGL_ES2_BIT; |
| } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) { |
| LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE"); |
| } |
| EGLint contextClientVersion = 0; |
| if (renderableType & EGL_OPENGL_ES2_BIT) { |
| contextClientVersion = 2; |
| } else if (renderableType & EGL_OPENGL_ES_BIT) { |
| contextClientVersion = 1; |
| } else { |
| LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs"); |
| } |
| |
| std::vector<EGLint> contextAttributes; |
| contextAttributes.reserve(6); |
| contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION); |
| contextAttributes.push_back(contextClientVersion); |
| bool useContextPriority = extensions.hasContextPriority() && |
| (featureFlags & RenderEngine::USE_HIGH_PRIORITY_CONTEXT); |
| if (useContextPriority) { |
| contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG); |
| contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG); |
| } |
| contextAttributes.push_back(EGL_NONE); |
| |
| EGLContext ctxt = eglCreateContext(display, config, nullptr, contextAttributes.data()); |
| |
| // if can't create a GL context, we can only abort. |
| LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed"); |
| |
| // now figure out what version of GL did we actually get |
| // NOTE: a dummy surface is not needed if KHR_create_context is supported |
| |
| EGLConfig dummyConfig = config; |
| if (dummyConfig == EGL_NO_CONFIG) { |
| dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true); |
| } |
| EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE}; |
| EGLSurface dummy = eglCreatePbufferSurface(display, dummyConfig, attribs); |
| LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer"); |
| EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt); |
| LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current"); |
| |
| extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER), |
| glGetString(GL_VERSION), glGetString(GL_EXTENSIONS)); |
| |
| GlesVersion version = parseGlesVersion(extensions.getVersion()); |
| |
| // initialize the renderer while GL is current |
| |
| std::unique_ptr<GLES20RenderEngine> engine; |
| switch (version) { |
| case GLES_VERSION_1_0: |
| case GLES_VERSION_1_1: |
| LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run."); |
| break; |
| case GLES_VERSION_2_0: |
| case GLES_VERSION_3_0: |
| engine = std::make_unique<GLES20RenderEngine>(featureFlags); |
| break; |
| } |
| engine->setEGLHandles(display, config, ctxt); |
| |
| ALOGI("OpenGL ES informations:"); |
| ALOGI("vendor : %s", extensions.getVendor()); |
| ALOGI("renderer : %s", extensions.getRenderer()); |
| ALOGI("version : %s", extensions.getVersion()); |
| ALOGI("extensions: %s", extensions.getExtensions()); |
| ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize()); |
| ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims()); |
| |
| eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| eglDestroySurface(display, dummy); |
| |
| return engine; |
| } |
| |
| EGLConfig GLES20RenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) { |
| status_t err; |
| EGLConfig config; |
| |
| // First try to get an ES2 config |
| err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config); |
| if (err != NO_ERROR) { |
| // If ES2 fails, try ES1 |
| err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config); |
| if (err != NO_ERROR) { |
| // still didn't work, probably because we're on the emulator... |
| // try a simplified query |
| ALOGW("no suitable EGLConfig found, trying a simpler query"); |
| err = selectEGLConfig(display, format, 0, &config); |
| if (err != NO_ERROR) { |
| // this EGL is too lame for android |
| LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up"); |
| } |
| } |
| } |
| |
| if (logConfig) { |
| // print some debugging info |
| EGLint r, g, b, a; |
| eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r); |
| eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g); |
| eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b); |
| eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a); |
| ALOGI("EGL information:"); |
| ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR)); |
| ALOGI("version : %s", eglQueryString(display, EGL_VERSION)); |
| ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS)); |
| ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported"); |
| ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config); |
| } |
| |
| return config; |
| } |
| |
| GLES20RenderEngine::GLES20RenderEngine(uint32_t featureFlags) |
| : renderengine::impl::RenderEngine(featureFlags), |
| mEGLDisplay(EGL_NO_DISPLAY), |
| mEGLConfig(nullptr), |
| mEGLContext(EGL_NO_CONTEXT), |
| mVpWidth(0), |
| mVpHeight(0), |
| mUseColorManagement(featureFlags & USE_COLOR_MANAGEMENT) { |
| glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
| glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims); |
| |
| glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| glPixelStorei(GL_PACK_ALIGNMENT, 4); |
| |
| const uint16_t protTexData[] = {0}; |
| glGenTextures(1, &mProtectedTexName); |
| glBindTexture(GL_TEXTURE_2D, mProtectedTexName); |
| glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData); |
| |
| // mColorBlindnessCorrection = M; |
| |
| if (mUseColorManagement) { |
| ColorSpace srgb(ColorSpace::sRGB()); |
| ColorSpace displayP3(ColorSpace::DisplayP3()); |
| ColorSpace bt2020(ColorSpace::BT2020()); |
| |
| // Compute sRGB to Display P3 transform matrix. |
| // NOTE: For now, we are limiting output wide color space support to |
| // Display-P3 only. |
| mSrgbToDisplayP3 = mat4(ColorSpaceConnector(srgb, displayP3).getTransform()); |
| |
| // Compute Display P3 to sRGB transform matrix. |
| mDisplayP3ToSrgb = mat4(ColorSpaceConnector(displayP3, srgb).getTransform()); |
| |
| // no chromatic adaptation needed since all color spaces use D65 for their white points. |
| mSrgbToXyz = srgb.getRGBtoXYZ(); |
| mDisplayP3ToXyz = displayP3.getRGBtoXYZ(); |
| mBt2020ToXyz = bt2020.getRGBtoXYZ(); |
| mXyzToSrgb = mat4(srgb.getXYZtoRGB()); |
| mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB()); |
| mXyzToBt2020 = mat4(bt2020.getXYZtoRGB()); |
| } |
| } |
| |
| GLES20RenderEngine::~GLES20RenderEngine() { |
| eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| eglTerminate(mEGLDisplay); |
| } |
| |
| std::unique_ptr<Framebuffer> GLES20RenderEngine::createFramebuffer() { |
| return std::make_unique<GLFramebuffer>(*this); |
| } |
| |
| std::unique_ptr<Surface> GLES20RenderEngine::createSurface() { |
| return std::make_unique<GLSurface>(*this); |
| } |
| |
| std::unique_ptr<Image> GLES20RenderEngine::createImage() { |
| return std::make_unique<GLImage>(*this); |
| } |
| |
| void GLES20RenderEngine::primeCache() const { |
| ProgramCache::getInstance().primeCache(mFeatureFlags & USE_COLOR_MANAGEMENT); |
| } |
| |
| bool GLES20RenderEngine::isCurrent() const { |
| return mEGLDisplay == eglGetCurrentDisplay() && mEGLContext == eglGetCurrentContext(); |
| } |
| |
| bool GLES20RenderEngine::setCurrentSurface(const Surface& surface) { |
| // Surface is an abstract interface. GLES20RenderEngine only ever |
| // creates GLSurface's, so it is safe to just cast to the actual |
| // type. |
| bool success = true; |
| const GLSurface& glSurface = static_cast<const GLSurface&>(surface); |
| EGLSurface eglSurface = glSurface.getEGLSurface(); |
| if (eglSurface != eglGetCurrentSurface(EGL_DRAW)) { |
| success = eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext) == EGL_TRUE; |
| if (success && glSurface.getAsync()) { |
| eglSwapInterval(mEGLDisplay, 0); |
| } |
| if (success) { |
| mSurfaceHeight = glSurface.getHeight(); |
| } |
| } |
| |
| return success; |
| } |
| |
| void GLES20RenderEngine::resetCurrentSurface() { |
| eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| mSurfaceHeight = 0; |
| } |
| |
| base::unique_fd GLES20RenderEngine::flush() { |
| if (!GLExtensions::getInstance().hasNativeFenceSync()) { |
| return base::unique_fd(); |
| } |
| |
| EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr); |
| if (sync == EGL_NO_SYNC_KHR) { |
| ALOGW("failed to create EGL native fence sync: %#x", eglGetError()); |
| return base::unique_fd(); |
| } |
| |
| // native fence fd will not be populated until flush() is done. |
| glFlush(); |
| |
| // get the fence fd |
| base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync)); |
| eglDestroySyncKHR(mEGLDisplay, sync); |
| if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) { |
| ALOGW("failed to dup EGL native fence sync: %#x", eglGetError()); |
| } |
| |
| return fenceFd; |
| } |
| |
| bool GLES20RenderEngine::finish() { |
| if (!GLExtensions::getInstance().hasFenceSync()) { |
| ALOGW("no synchronization support"); |
| return false; |
| } |
| |
| EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr); |
| if (sync == EGL_NO_SYNC_KHR) { |
| ALOGW("failed to create EGL fence sync: %#x", eglGetError()); |
| return false; |
| } |
| |
| EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, |
| 2000000000 /*2 sec*/); |
| EGLint error = eglGetError(); |
| eglDestroySyncKHR(mEGLDisplay, sync); |
| if (result != EGL_CONDITION_SATISFIED_KHR) { |
| if (result == EGL_TIMEOUT_EXPIRED_KHR) { |
| ALOGW("fence wait timed out"); |
| } else { |
| ALOGW("error waiting on EGL fence: %#x", error); |
| } |
| return false; |
| } |
| |
| return true; |
| } |
| |
| bool GLES20RenderEngine::waitFence(base::unique_fd fenceFd) { |
| if (!GLExtensions::getInstance().hasNativeFenceSync() || |
| !GLExtensions::getInstance().hasWaitSync()) { |
| return false; |
| } |
| |
| EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE}; |
| EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs); |
| if (sync == EGL_NO_SYNC_KHR) { |
| ALOGE("failed to create EGL native fence sync: %#x", eglGetError()); |
| return false; |
| } |
| |
| // fenceFd is now owned by EGLSync |
| (void)fenceFd.release(); |
| |
| // XXX: The spec draft is inconsistent as to whether this should return an |
| // EGLint or void. Ignore the return value for now, as it's not strictly |
| // needed. |
| eglWaitSyncKHR(mEGLDisplay, sync, 0); |
| EGLint error = eglGetError(); |
| eglDestroySyncKHR(mEGLDisplay, sync); |
| if (error != EGL_SUCCESS) { |
| ALOGE("failed to wait for EGL native fence sync: %#x", error); |
| return false; |
| } |
| |
| return true; |
| } |
| |
| void GLES20RenderEngine::clearWithColor(float red, float green, float blue, float alpha) { |
| glClearColor(red, green, blue, alpha); |
| glClear(GL_COLOR_BUFFER_BIT); |
| } |
| |
| void GLES20RenderEngine::fillRegionWithColor(const Region& region, float red, float green, |
| float blue, float alpha) { |
| size_t c; |
| Rect const* r = region.getArray(&c); |
| Mesh mesh(Mesh::TRIANGLES, c * 6, 2); |
| Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>()); |
| for (size_t i = 0; i < c; i++, r++) { |
| position[i * 6 + 0].x = r->left; |
| position[i * 6 + 0].y = r->top; |
| position[i * 6 + 1].x = r->left; |
| position[i * 6 + 1].y = r->bottom; |
| position[i * 6 + 2].x = r->right; |
| position[i * 6 + 2].y = r->bottom; |
| position[i * 6 + 3].x = r->left; |
| position[i * 6 + 3].y = r->top; |
| position[i * 6 + 4].x = r->right; |
| position[i * 6 + 4].y = r->bottom; |
| position[i * 6 + 5].x = r->right; |
| position[i * 6 + 5].y = r->top; |
| } |
| setupFillWithColor(red, green, blue, alpha); |
| drawMesh(mesh); |
| } |
| |
| void GLES20RenderEngine::setScissor(const Rect& region) { |
| // Invert y-coordinate to map to GL-space. |
| int32_t canvasHeight = mRenderToFbo ? mFboHeight : mSurfaceHeight; |
| int32_t glBottom = canvasHeight - region.bottom; |
| |
| glScissor(region.left, glBottom, region.getWidth(), region.getHeight()); |
| glEnable(GL_SCISSOR_TEST); |
| } |
| |
| void GLES20RenderEngine::disableScissor() { |
| glDisable(GL_SCISSOR_TEST); |
| } |
| |
| void GLES20RenderEngine::genTextures(size_t count, uint32_t* names) { |
| glGenTextures(count, names); |
| } |
| |
| void GLES20RenderEngine::deleteTextures(size_t count, uint32_t const* names) { |
| glDeleteTextures(count, names); |
| } |
| |
| void GLES20RenderEngine::bindExternalTextureImage(uint32_t texName, |
| const Image& image) { |
| const GLImage& glImage = static_cast<const GLImage&>(image); |
| const GLenum target = GL_TEXTURE_EXTERNAL_OES; |
| |
| glBindTexture(target, texName); |
| if (glImage.getEGLImage() != EGL_NO_IMAGE_KHR) { |
| glEGLImageTargetTexture2DOES(target, |
| static_cast<GLeglImageOES>(glImage.getEGLImage())); |
| } |
| } |
| |
| void GLES20RenderEngine::readPixels(size_t l, size_t b, size_t w, size_t h, uint32_t* pixels) { |
| glReadPixels(l, b, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
| } |
| |
| status_t GLES20RenderEngine::bindFrameBuffer(Framebuffer* framebuffer) { |
| GLFramebuffer* glFramebuffer = static_cast<GLFramebuffer*>(framebuffer); |
| EGLImageKHR eglImage = glFramebuffer->getEGLImage(); |
| uint32_t textureName = glFramebuffer->getTextureName(); |
| uint32_t framebufferName = glFramebuffer->getFramebufferName(); |
| |
| // Bind the texture and turn our EGLImage into a texture |
| glBindTexture(GL_TEXTURE_2D, textureName); |
| glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)eglImage); |
| |
| // Bind the Framebuffer to render into |
| glBindFramebuffer(GL_FRAMEBUFFER, framebufferName); |
| glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| GL_TEXTURE_2D, textureName, 0); |
| |
| mRenderToFbo = true; |
| mFboHeight = glFramebuffer->getBufferHeight(); |
| |
| uint32_t glStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| |
| ALOGE_IF(glStatus != GL_FRAMEBUFFER_COMPLETE_OES, |
| "glCheckFramebufferStatusOES error %d", glStatus); |
| |
| return glStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE; |
| } |
| |
| void GLES20RenderEngine::unbindFrameBuffer(Framebuffer* /* framebuffer */) { |
| mRenderToFbo = false; |
| mFboHeight = 0; |
| |
| // back to main framebuffer |
| glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| |
| // Workaround for b/77935566 to force the EGL driver to release the |
| // screenshot buffer |
| setScissor(Rect::EMPTY_RECT); |
| clearWithColor(0.0, 0.0, 0.0, 0.0); |
| disableScissor(); |
| } |
| |
| void GLES20RenderEngine::checkErrors() const { |
| do { |
| // there could be more than one error flag |
| GLenum error = glGetError(); |
| if (error == GL_NO_ERROR) break; |
| ALOGE("GL error 0x%04x", int(error)); |
| } while (true); |
| } |
| |
| void GLES20RenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop, |
| ui::Transform::orientation_flags rotation) { |
| int32_t l = sourceCrop.left; |
| int32_t r = sourceCrop.right; |
| int32_t b = sourceCrop.bottom; |
| int32_t t = sourceCrop.top; |
| if (mRenderToFbo) { |
| std::swap(t, b); |
| } |
| mat4 m = mat4::ortho(l, r, b, t, 0, 1); |
| |
| // Apply custom rotation to the projection. |
| float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f; |
| switch (rotation) { |
| case ui::Transform::ROT_0: |
| break; |
| case ui::Transform::ROT_90: |
| m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m; |
| break; |
| case ui::Transform::ROT_180: |
| m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m; |
| break; |
| case ui::Transform::ROT_270: |
| m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m; |
| break; |
| default: |
| break; |
| } |
| |
| glViewport(0, 0, vpw, vph); |
| mState.setProjectionMatrix(m); |
| mVpWidth = vpw; |
| mVpHeight = vph; |
| } |
| |
| void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque, |
| bool disableTexture, const half4& color) { |
| mState.setPremultipliedAlpha(premultipliedAlpha); |
| mState.setOpaque(opaque); |
| mState.setColor(color); |
| |
| if (disableTexture) { |
| mState.disableTexture(); |
| } |
| |
| if (color.a < 1.0f || !opaque) { |
| glEnable(GL_BLEND); |
| glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| } else { |
| glDisable(GL_BLEND); |
| } |
| } |
| |
| void GLES20RenderEngine::setSourceY410BT2020(bool enable) { |
| mState.setY410BT2020(enable); |
| } |
| |
| void GLES20RenderEngine::setSourceDataSpace(Dataspace source) { |
| mDataSpace = source; |
| } |
| |
| void GLES20RenderEngine::setOutputDataSpace(Dataspace dataspace) { |
| mOutputDataSpace = dataspace; |
| } |
| |
| void GLES20RenderEngine::setDisplayMaxLuminance(const float maxLuminance) { |
| mState.setDisplayMaxLuminance(maxLuminance); |
| } |
| |
| void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) { |
| GLuint target = texture.getTextureTarget(); |
| glBindTexture(target, texture.getTextureName()); |
| GLenum filter = GL_NEAREST; |
| if (texture.getFiltering()) { |
| filter = GL_LINEAR; |
| } |
| glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter); |
| glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter); |
| |
| mState.setTexture(texture); |
| } |
| |
| void GLES20RenderEngine::setupLayerBlackedOut() { |
| glBindTexture(GL_TEXTURE_2D, mProtectedTexName); |
| Texture texture(Texture::TEXTURE_2D, mProtectedTexName); |
| texture.setDimensions(1, 1); // FIXME: we should get that from somewhere |
| mState.setTexture(texture); |
| } |
| |
| void GLES20RenderEngine::setupColorTransform(const mat4& colorTransform) { |
| mState.setColorMatrix(colorTransform); |
| } |
| |
| void GLES20RenderEngine::disableTexturing() { |
| mState.disableTexture(); |
| } |
| |
| void GLES20RenderEngine::disableBlending() { |
| glDisable(GL_BLEND); |
| } |
| |
| void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) { |
| mState.setPremultipliedAlpha(true); |
| mState.setOpaque(false); |
| mState.setColor(half4(r, g, b, a)); |
| mState.disableTexture(); |
| glDisable(GL_BLEND); |
| } |
| |
| void GLES20RenderEngine::drawMesh(const Mesh& mesh) { |
| ATRACE_CALL(); |
| if (mesh.getTexCoordsSize()) { |
| glEnableVertexAttribArray(Program::texCoords); |
| glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE, |
| mesh.getByteStride(), mesh.getTexCoords()); |
| } |
| |
| glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE, |
| mesh.getByteStride(), mesh.getPositions()); |
| |
| // By default, DISPLAY_P3 is the only supported wide color output. However, |
| // when HDR content is present, hardware composer may be able to handle |
| // BT2020 data space, in that case, the output data space is set to be |
| // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need |
| // to respect this and convert non-HDR content to HDR format. |
| if (mUseColorManagement) { |
| Description managedState = mState; |
| Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK); |
| Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK); |
| Dataspace outputStandard = static_cast<Dataspace>(mOutputDataSpace & |
| Dataspace::STANDARD_MASK); |
| Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace & |
| Dataspace::TRANSFER_MASK); |
| bool needsXYZConversion = needsXYZTransformMatrix(); |
| |
| if (needsXYZConversion) { |
| // The supported input color spaces are standard RGB, Display P3 and BT2020. |
| switch (inputStandard) { |
| case Dataspace::STANDARD_DCI_P3: |
| managedState.setInputTransformMatrix(mDisplayP3ToXyz); |
| break; |
| case Dataspace::STANDARD_BT2020: |
| managedState.setInputTransformMatrix(mBt2020ToXyz); |
| break; |
| default: |
| managedState.setInputTransformMatrix(mSrgbToXyz); |
| break; |
| } |
| |
| // The supported output color spaces are BT2020, Display P3 and standard RGB. |
| switch (outputStandard) { |
| case Dataspace::STANDARD_BT2020: |
| managedState.setOutputTransformMatrix(mXyzToBt2020); |
| break; |
| case Dataspace::STANDARD_DCI_P3: |
| managedState.setOutputTransformMatrix(mXyzToDisplayP3); |
| break; |
| default: |
| managedState.setOutputTransformMatrix(mXyzToSrgb); |
| break; |
| } |
| } else if (inputStandard != outputStandard) { |
| // At this point, the input data space and output data space could be both |
| // HDR data spaces, but they match each other, we do nothing in this case. |
| // In addition to the case above, the input data space could be |
| // - scRGB linear |
| // - scRGB non-linear |
| // - sRGB |
| // - Display P3 |
| // The output data spaces could be |
| // - sRGB |
| // - Display P3 |
| if (outputStandard == Dataspace::STANDARD_BT709) { |
| managedState.setOutputTransformMatrix(mDisplayP3ToSrgb); |
| } else if (outputStandard == Dataspace::STANDARD_DCI_P3) { |
| managedState.setOutputTransformMatrix(mSrgbToDisplayP3); |
| } |
| } |
| |
| // we need to convert the RGB value to linear space and convert it back when: |
| // - there is a color matrix that is not an identity matrix, or |
| // - there is an output transform matrix that is not an identity matrix, or |
| // - the input transfer function doesn't match the output transfer function. |
| if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() || |
| inputTransfer != outputTransfer) { |
| switch (inputTransfer) { |
| case Dataspace::TRANSFER_ST2084: |
| managedState.setInputTransferFunction(Description::TransferFunction::ST2084); |
| break; |
| case Dataspace::TRANSFER_HLG: |
| managedState.setInputTransferFunction(Description::TransferFunction::HLG); |
| break; |
| case Dataspace::TRANSFER_LINEAR: |
| managedState.setInputTransferFunction(Description::TransferFunction::LINEAR); |
| break; |
| default: |
| managedState.setInputTransferFunction(Description::TransferFunction::SRGB); |
| break; |
| } |
| |
| switch (outputTransfer) { |
| case Dataspace::TRANSFER_ST2084: |
| managedState.setOutputTransferFunction(Description::TransferFunction::ST2084); |
| break; |
| case Dataspace::TRANSFER_HLG: |
| managedState.setOutputTransferFunction(Description::TransferFunction::HLG); |
| break; |
| default: |
| managedState.setOutputTransferFunction(Description::TransferFunction::SRGB); |
| break; |
| } |
| } |
| |
| ProgramCache::getInstance().useProgram(managedState); |
| |
| glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); |
| |
| if (outputDebugPPMs) { |
| static uint64_t managedColorFrameCount = 0; |
| std::ostringstream out; |
| out << "/data/texture_out" << managedColorFrameCount++; |
| writePPM(out.str().c_str(), mVpWidth, mVpHeight); |
| } |
| } else { |
| ProgramCache::getInstance().useProgram(mState); |
| |
| glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); |
| } |
| |
| if (mesh.getTexCoordsSize()) { |
| glDisableVertexAttribArray(Program::texCoords); |
| } |
| } |
| |
| size_t GLES20RenderEngine::getMaxTextureSize() const { |
| return mMaxTextureSize; |
| } |
| |
| size_t GLES20RenderEngine::getMaxViewportDims() const { |
| return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1]; |
| } |
| |
| void GLES20RenderEngine::dump(String8& result) { |
| const GLExtensions& extensions = GLExtensions::getInstance(); |
| |
| result.appendFormat("EGL implementation : %s\n", extensions.getEGLVersion()); |
| result.appendFormat("%s\n", extensions.getEGLExtensions()); |
| |
| result.appendFormat("GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(), |
| extensions.getVersion()); |
| result.appendFormat("%s\n", extensions.getExtensions()); |
| result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n", |
| dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(), |
| dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str()); |
| } |
| |
| GLES20RenderEngine::GlesVersion GLES20RenderEngine::parseGlesVersion(const char* str) { |
| int major, minor; |
| if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) { |
| if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) { |
| ALOGW("Unable to parse GL_VERSION string: \"%s\"", str); |
| return GLES_VERSION_1_0; |
| } |
| } |
| |
| if (major == 1 && minor == 0) return GLES_VERSION_1_0; |
| if (major == 1 && minor >= 1) return GLES_VERSION_1_1; |
| if (major == 2 && minor >= 0) return GLES_VERSION_2_0; |
| if (major == 3 && minor >= 0) return GLES_VERSION_3_0; |
| |
| ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor); |
| return GLES_VERSION_1_0; |
| } |
| |
| bool GLES20RenderEngine::isHdrDataSpace(const Dataspace dataSpace) const { |
| const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK); |
| const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK); |
| return standard == Dataspace::STANDARD_BT2020 && |
| (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG); |
| } |
| |
| // For convenience, we want to convert the input color space to XYZ color space first, |
| // and then convert from XYZ color space to output color space when |
| // - SDR and HDR contents are mixed, either SDR content will be converted to HDR or |
| // HDR content will be tone-mapped to SDR; Or, |
| // - there are HDR PQ and HLG contents presented at the same time, where we want to convert |
| // HLG content to PQ content. |
| // In either case above, we need to operate the Y value in XYZ color space. Thus, when either |
| // input data space or output data space is HDR data space, and the input transfer function |
| // doesn't match the output transfer function, we would enable an intermediate transfrom to |
| // XYZ color space. |
| bool GLES20RenderEngine::needsXYZTransformMatrix() const { |
| const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace); |
| const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace); |
| const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK); |
| const Dataspace outputTransfer = static_cast<Dataspace>(mOutputDataSpace & |
| Dataspace::TRANSFER_MASK); |
| |
| return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer; |
| } |
| |
| void GLES20RenderEngine::setEGLHandles(EGLDisplay display, EGLConfig config, EGLContext ctxt) { |
| mEGLDisplay = display; |
| mEGLConfig = config; |
| mEGLContext = ctxt; |
| } |
| |
| } // namespace gl |
| } // namespace renderengine |
| } // namespace android |