Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b577745f authored by Alec Mouri's avatar Alec Mouri
Browse files

Tag color spaces in Skia-renderengine

Current gaps after this patch:
1. Youtube HDR doesn't tone-map properly
2. Y410 hack is not supproted
3. Color transforms not plumbed yet

Bug: 164223050
Test: check wide color test image
https: //android-developers.googleblog.com/2019/05/wide-color-photos-are-coming-to-android.html
Change-Id: I981d16a1f751c085a246eb0f031052be9eb9b7cd
parent 384914ae
Loading
Loading
Loading
Loading
+63 −13
Original line number Diff line number Diff line
@@ -19,26 +19,27 @@
#define LOG_TAG "RenderEngine"
#define ATRACE_TAG ATRACE_TAG_GRAPHICS

#include <cmath>
#include "SkiaGLRenderEngine.h"

#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>
#include <sync/sync.h>
#include <ui/GraphicBuffer.h>
#include <utils/Trace.h>
#include "../gl/GLExtensions.h"
#include "SkiaGLRenderEngine.h"
#include "filters/BlurFilter.h"

#include <GrContextOptions.h>
#include <gl/GrGLInterface.h>

#include <SkCanvas.h>
#include <SkColorSpace.h>
#include <SkImage.h>
#include <SkImageFilters.h>
#include <SkShadowUtils.h>
#include <SkSurface.h>
#include <gl/GrGLInterface.h>
#include <sync/sync.h>
#include <ui/GraphicBuffer.h>
#include <utils/Trace.h>

#include <cmath>

#include "../gl/GLExtensions.h"
#include "filters/BlurFilter.h"

extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);

@@ -130,6 +131,47 @@ static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint render
    return err;
}

// Converts an android dataspace to a supported SkColorSpace
// Supported dataspaces are
// 1. sRGB
// 2. Display P3
// 3. BT2020 PQ
// 4. BT2020 HLG
// Unknown primaries are mapped to BT709, and unknown transfer functions
// are mapped to sRGB.
static sk_sp<SkColorSpace> toColorSpace(ui::Dataspace dataspace) {
    skcms_Matrix3x3 gamut;
    switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
        case HAL_DATASPACE_STANDARD_BT709:
            gamut = SkNamedGamut::kSRGB;
            break;
        case HAL_DATASPACE_STANDARD_BT2020:
            gamut = SkNamedGamut::kRec2020;
            break;
        case HAL_DATASPACE_STANDARD_DCI_P3:
            gamut = SkNamedGamut::kDisplayP3;
            break;
        default:
            ALOGV("Unsupported Gamut: %d, defaulting to sRGB", dataspace);
            gamut = SkNamedGamut::kSRGB;
            break;
    }

    switch (dataspace & HAL_DATASPACE_TRANSFER_MASK) {
        case HAL_DATASPACE_TRANSFER_LINEAR:
            return SkColorSpace::MakeRGB(SkNamedTransferFn::kLinear, gamut);
        case HAL_DATASPACE_TRANSFER_SRGB:
            return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, gamut);
        case HAL_DATASPACE_TRANSFER_ST2084:
            return SkColorSpace::MakeRGB(SkNamedTransferFn::kPQ, gamut);
        case HAL_DATASPACE_TRANSFER_HLG:
            return SkColorSpace::MakeRGB(SkNamedTransferFn::kHLG, gamut);
        default:
            ALOGV("Unsupported Gamma: %d, defaulting to sRGB transfer", dataspace);
            return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, gamut);
    }
}

std::unique_ptr<SkiaGLRenderEngine> SkiaGLRenderEngine::create(
        const RenderEngineCreationArgs& args) {
    // initialize EGL for the default display
@@ -257,7 +299,8 @@ SkiaGLRenderEngine::SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGL
        mEGLContext(ctxt),
        mPlaceholderSurface(placeholder),
        mProtectedEGLContext(protectedContext),
        mProtectedPlaceholderSurface(protectedPlaceholder) {
        mProtectedPlaceholderSurface(protectedPlaceholder),
        mUseColorManagement(args.useColorManagement) {
    // Suppress unused field warnings for things we definitely will need/use
    // These EGL fields will all be needed for toggling between protected & unprotected contexts
    // Or we need different RE instances for that
@@ -385,7 +428,10 @@ status_t SkiaGLRenderEngine::drawLayers(const DisplaySettings& display,
    if (!surface) {
        surface = SkSurface::MakeFromAHardwareBuffer(mGrContext.get(), buffer->toAHardwareBuffer(),
                                                     GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
                                                     SkColorSpace::MakeSRGB(), nullptr);
                                                     mUseColorManagement
                                                             ? toColorSpace(display.outputDataspace)
                                                             : SkColorSpace::MakeSRGB(),
                                                     nullptr);
        if (useFramebufferCache && surface) {
            ALOGD("Adding to cache");
            mSurfaceCache.insert({buffer->getId(), surface});
@@ -430,7 +476,11 @@ status_t SkiaGLRenderEngine::drawLayers(const DisplaySettings& display,
                image = SkImage::MakeFromAHardwareBuffer(item.buffer->toAHardwareBuffer(),
                                                         item.usePremultipliedAlpha
                                                                 ? kPremul_SkAlphaType
                                                                 : kUnpremul_SkAlphaType);
                                                                 : kUnpremul_SkAlphaType,
                                                         mUseColorManagement
                                                                 ? toColorSpace(
                                                                           layer->sourceDataspace)
                                                                 : SkColorSpace::MakeSRGB());
                mImageCache.insert({item.buffer->getId(), image});
            }

+11 −6
Original line number Diff line number Diff line
@@ -17,16 +17,19 @@
#ifndef SF_SKIAGLRENDERENGINE_H_
#define SF_SKIAGLRENDERENGINE_H_

#include <sys/types.h>
#include <mutex>
#include <unordered_map>

#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>
#include <GrDirectContext.h>
#include <SkSurface.h>
#include <android-base/thread_annotations.h>
#include <renderengine/RenderEngine.h>
#include <sys/types.h>

#include <GrDirectContext.h>
#include <SkSurface.h>
#include <mutex>
#include <unordered_map>

#include "EGL/egl.h"
#include "SkiaRenderEngine.h"
#include "filters/BlurFilter.h"

@@ -81,6 +84,8 @@ private:
    EGLSurface mProtectedPlaceholderSurface;
    BlurFilter* mBlurFilter = nullptr;

    const bool mUseColorManagement;

    // Cache of GL images that we'll store per GraphicBuffer ID
    std::unordered_map<uint64_t, sk_sp<SkImage>> mImageCache GUARDED_BY(mRenderingMutex);
    // Mutex guarding rendering operations, so that: