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

Commit 9776d3e3 authored by Alec Mouri's avatar Alec Mouri
Browse files

Tonemap in RecordingCanvas

Intecepts bitmap calls to tonemap whenever the source is HDR (PQ/HLG)
and the destination is SDR.

Also, fix the following bugs discovered as part of testing:
1. Don't implicitly cast to booleans when extracting transfer functions
   from a dataspace in hwui's tonemapper.
2. Fix some typos in defining the HLG/PQ transfer functions.

Bug: 261088450
Test: New ColorBitmapActivity in HwAccelerationTest
Change-Id: I9d9d68fc4f57b999b3c6d4156bef281b4409f37e
parent 397065c0
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -212,9 +212,9 @@ public abstract class ColorSpace {
            new Rgb.TransferParameters(1 / 1.055, 0.055 / 1.055, 1 / 12.92, 0.04045, 2.4);
    private static final Rgb.TransferParameters BT2020_HLG_TRANSFER_PARAMETERS =
            new Rgb.TransferParameters(2.0f, 2.0f, 1 / 0.17883277f,
                0.28466892f, 0.5599107f, 0.0f, -3.0f, true);
                0.28466892f, 0.5599107f, -11 / 12.0f, -3.0f, true);
    private static final Rgb.TransferParameters BT2020_PQ_TRANSFER_PARAMETERS =
            new Rgb.TransferParameters(107 / 128.0f, 1.0f, 32 / 2523.0f,
            new Rgb.TransferParameters(-107 / 128.0f, 1.0f, 32 / 2523.0f,
                2413 / 128.0f, -2392 / 128.0f, 8192 / 1305.0f, -2.0f, true);

    // See static initialization block next to #get(Named)
+1 −1
Original line number Diff line number Diff line
@@ -536,6 +536,7 @@ cc_defaults {
        "RootRenderNode.cpp",
        "SkiaCanvas.cpp",
        "SkiaInterpolator.cpp",
        "Tonemapper.cpp",
        "VectorDrawable.cpp",
    ],

@@ -594,7 +595,6 @@ cc_defaults {
                "ProfileData.cpp",
                "ProfileDataContainer.cpp",
                "Readback.cpp",
                "Tonemapper.cpp",
                "TreeInfo.cpp",
                "WebViewFunctorManager.cpp",
                "protos/graphicsstats.proto",
+1 −1
Original line number Diff line number Diff line
@@ -45,4 +45,4 @@ bool transformPaint(ColorTransform transform, SkPaint* paint, BitmapPalette pale
SkColor transformColor(ColorTransform transform, SkColor color);
SkColor transformColorInverse(ColorTransform transform, SkColor color);

}  // namespace android::uirenderer;
 No newline at end of file
}  // namespace android::uirenderer
+11 −4
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@
#include "SkRegion.h"
#include "SkTextBlob.h"
#include "SkVertices.h"
#include "Tonemapper.h"
#include "VectorDrawable.h"
#include "pipeline/skia/AnimatedDrawables.h"
#include "pipeline/skia/FunctorDrawable.h"
@@ -334,7 +335,9 @@ struct DrawImage final : Op {
    SkPaint paint;
    BitmapPalette palette;
    void draw(SkCanvas* c, const SkMatrix&) const {
        c->drawImage(image.get(), x, y, sampling, &paint);
        SkPaint newPaint = paint;
        tonemapPaint(image->imageInfo(), c->imageInfo(), -1, newPaint);
        c->drawImage(image.get(), x, y, sampling, &newPaint);
    }
};
struct DrawImageRect final : Op {
@@ -356,7 +359,9 @@ struct DrawImageRect final : Op {
    SkCanvas::SrcRectConstraint constraint;
    BitmapPalette palette;
    void draw(SkCanvas* c, const SkMatrix&) const {
        c->drawImageRect(image.get(), src, dst, sampling, &paint, constraint);
        SkPaint newPaint = paint;
        tonemapPaint(image->imageInfo(), c->imageInfo(), -1, newPaint);
        c->drawImageRect(image.get(), src, dst, sampling, &newPaint, constraint);
    }
};
struct DrawImageLattice final : Op {
@@ -389,8 +394,10 @@ struct DrawImageLattice final : Op {
        auto flags =
                (0 == fs) ? nullptr : pod<SkCanvas::Lattice::RectType>(
                                              this, (xs + ys) * sizeof(int) + fs * sizeof(SkColor));
        c->drawImageLattice(image.get(), {xdivs, ydivs, flags, xs, ys, &src, colors}, dst,
                            filter, &paint);
        SkPaint newPaint = paint;
        tonemapPaint(image->imageInfo(), c->imageInfo(), -1, newPaint);
        c->drawImageLattice(image.get(), {xdivs, ydivs, flags, xs, ys, &src, colors}, dst, filter,
                            &newPaint);
    }
};

+14 −3
Original line number Diff line number Diff line
@@ -18,7 +18,10 @@

#include <SkRuntimeEffect.h>
#include <log/log.h>
// libshaders only exists on Android devices
#ifdef __ANDROID__
#include <shaders/shaders.h>
#endif

#include "utils/Color.h"

@@ -26,6 +29,8 @@ namespace android::uirenderer {

namespace {

// custom tonemapping only exists on Android devices
#ifdef __ANDROID__
class ColorFilterRuntimeEffectBuilder : public SkRuntimeEffectBuilder {
public:
    explicit ColorFilterRuntimeEffectBuilder(sk_sp<SkRuntimeEffect> effect)
@@ -59,20 +64,21 @@ static sk_sp<SkColorFilter> createLinearEffectColorFilter(const shaders::LinearE
    return effectBuilder.makeColorFilter();
}

static bool extractTransfer(ui::Dataspace dataspace) {
    return dataspace & HAL_DATASPACE_TRANSFER_MASK;
static ui::Dataspace extractTransfer(ui::Dataspace dataspace) {
    return static_cast<ui::Dataspace>(dataspace & HAL_DATASPACE_TRANSFER_MASK);
}

static bool isHdrDataspace(ui::Dataspace dataspace) {
    const auto transfer = extractTransfer(dataspace);

    return transfer == HAL_DATASPACE_TRANSFER_ST2084 || transfer == HAL_DATASPACE_TRANSFER_HLG;
    return transfer == ui::Dataspace::TRANSFER_ST2084 || transfer == ui::Dataspace::TRANSFER_HLG;
}

static ui::Dataspace getDataspace(const SkImageInfo& image) {
    return static_cast<ui::Dataspace>(
            ColorSpaceToADataSpace(image.colorSpace(), image.colorType()));
}
#endif

}  // namespace

@@ -80,6 +86,8 @@ static ui::Dataspace getDataspace(const SkImageInfo& image) {
// shader and tag it on the supplied paint.
void tonemapPaint(const SkImageInfo& source, const SkImageInfo& destination, float maxLuminanceNits,
                  SkPaint& paint) {
// custom tonemapping only exists on Android devices
#ifdef __ANDROID__
    const auto sourceDataspace = getDataspace(source);
    const auto destinationDataspace = getDataspace(destination);

@@ -102,6 +110,9 @@ void tonemapPaint(const SkImageInfo& source, const SkImageInfo& destination, flo
            paint.setColorFilter(colorFilter);
        }
    }
#else
    return;
#endif
}

}  // namespace android::uirenderer
Loading