Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
ApplyGammaTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
19#include "include/core/SkSize.h"
24#include "include/gpu/GrTypes.h"
27#include "src/core/SkMemset.h"
32#include "tests/Test.h"
33
34#include <algorithm>
35#include <cmath>
36#include <cstddef>
37#include <cstdint>
38#include <initializer_list>
39
40using namespace skia_private;
41
42struct GrContextOptions;
43
44/** convert 0..1 linear value to 0..1 srgb */
45static float linear_to_srgb(float linear) {
46 if (linear <= 0.0031308) {
47 return linear * 12.92f;
48 } else {
49 return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
50 }
51}
52
53/** convert 0..1 srgb value to 0..1 linear */
54static float srgb_to_linear(float srgb) {
55 if (srgb <= 0.04045f) {
56 return srgb / 12.92f;
57 } else {
58 return powf((srgb + 0.055f) / 1.055f, 2.4f);
59 }
60}
61
62bool check_gamma(uint32_t src, uint32_t dst, bool toSRGB, float error,
63 uint32_t* expected) {
64 bool result = true;
65 uint32_t expectedColor = src & 0xff000000;
66
67 // Alpha should always be exactly preserved.
68 if ((src & 0xff000000) != (dst & 0xff000000)) {
69 result = false;
70 }
71
72 // need to unpremul before we can perform srgb magic
73 float invScale = 0;
74 float alpha = SkGetPackedA32(src);
75 if (alpha) {
76 invScale = 255.0f / alpha;
77 }
78
79 for (int c = 0; c < 3; ++c) {
80 float srcComponent = ((src & (0xff << (c * 8))) >> (c * 8)) * invScale;
81 float lower = std::max(0.f, srcComponent - error);
82 float upper = std::min(255.f, srcComponent + error);
83 if (toSRGB) {
84 lower = linear_to_srgb(lower / 255.f);
85 upper = linear_to_srgb(upper / 255.f);
86 } else {
87 lower = srgb_to_linear(lower / 255.f);
88 upper = srgb_to_linear(upper / 255.f);
89 }
90 lower *= alpha;
91 upper *= alpha;
92 SkASSERT(lower >= 0.f && lower <= 255.f);
93 SkASSERT(upper >= 0.f && upper <= 255.f);
94 uint8_t dstComponent = (dst & (0xff << (c * 8))) >> (c * 8);
95 if (dstComponent < SkScalarFloorToInt(lower) ||
96 dstComponent > SkScalarCeilToInt(upper)) {
97 result = false;
98 }
99 uint8_t expectedComponent = SkScalarRoundToInt((lower + upper) * 0.5f);
100 expectedColor |= expectedComponent << (c * 8);
101 }
102
103 *expected = expectedColor;
104 return result;
105}
106
108 auto context = ctxInfo.directContext();
109 static constexpr SkISize kBaseSize{256, 256};
110 static const size_t kRowBytes = sizeof(uint32_t) * kBaseSize.fWidth;
111
112 const SkImageInfo ii = SkImageInfo::MakeN32Premul(kBaseSize);
113
114 AutoTMalloc<uint32_t> srcPixels(kBaseSize.area());
115 for (int y = 0; y < kBaseSize.fHeight; ++y) {
116 for (int x = 0; x < kBaseSize.fWidth; ++x) {
117 srcPixels.get()[y*kBaseSize.fWidth+x] = SkPreMultiplyARGB(x, y, x, 0xFF);
118 }
119 }
120
121 SkBitmap bm;
122 bm.installPixels(ii, srcPixels.get(), kRowBytes);
123 auto img = bm.asImage();
124
125 AutoTMalloc<uint32_t> read(kBaseSize.area());
126
127 // We allow more error on GPUs with lower precision shader variables.
128 float error = context->priv().caps()->shaderCaps()->fHalfIs32Bits ? 0.5f : 1.2f;
129
130 for (auto toSRGB : { false, true }) {
132
133 if (!dst) {
134 ERRORF(reporter, "Could not create surfaces for copy surface test.");
135 continue;
136 }
137
138 SkCanvas* dstCanvas = dst->getCanvas();
139
140 dstCanvas->clear(SK_ColorRED);
141 context->flushAndSubmit(dst.get(), GrSyncCpu::kNo);
142
143 SkPaint gammaPaint;
147
148 dstCanvas->drawImage(img, 0, 0, SkSamplingOptions(), &gammaPaint);
149 context->flushAndSubmit(dst.get(), GrSyncCpu::kNo);
150
151 SkOpts::memset32(read.get(), 0, kBaseSize.fWidth * kBaseSize.fHeight);
152 if (!dst->readPixels(ii, read.get(), kRowBytes, 0, 0)) {
153 ERRORF(reporter, "Error calling readPixels");
154 continue;
155 }
156
157 bool abort = false;
158 // Validate that pixels were copied/transformed correctly.
159 for (int y = 0; y < kBaseSize.fHeight && !abort; ++y) {
160 for (int x = 0; x < kBaseSize.fWidth && !abort; ++x) {
161 uint32_t r = read.get()[y * kBaseSize.fWidth + x];
162 uint32_t s = srcPixels.get()[y * kBaseSize.fWidth + x];
163 uint32_t expected;
164 if (!check_gamma(s, r, toSRGB, error, &expected)) {
165 ERRORF(reporter, "Expected dst %d,%d to contain 0x%08x "
166 "from src 0x%08x and mode %s. Got %08x", x, y, expected, s,
167 toSRGB ? "ToSRGB" : "ToLinear", r);
168 abort = true;
169 break;
170 }
171 }
172 }
173 }
174}
static float linear_to_srgb(float linear)
static float srgb_to_linear(float srgb)
bool check_gamma(uint32_t src, uint32_t dst, bool toSRGB, float error, uint32_t *expected)
reporter
#define SkASSERT(cond)
Definition SkAssert.h:116
#define SkGetPackedA32(packed)
Definition SkColorPriv.h:92
SK_API SkPMColor SkPreMultiplyARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition SkColor.cpp:17
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
static bool read(SkStream *stream, void *buffer, size_t amount)
#define SkScalarRoundToInt(x)
Definition SkScalar.h:37
#define SkScalarCeilToInt(x)
Definition SkScalar.h:36
#define SkScalarFloorToInt(x)
Definition SkScalar.h:35
#define ApplyGamma(table, alpha)
#define ERRORF(r,...)
Definition Test.h:293
#define DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(name, reporter, context_info, ctsEnforcement)
Definition Test.h:434
sk_sp< SkImage > asImage() const
Definition SkBitmap.cpp:645
bool installPixels(const SkImageInfo &info, void *pixels, size_t rowBytes, void(*releaseProc)(void *addr, void *context), void *context)
Definition SkBitmap.cpp:323
void clear(SkColor color)
Definition SkCanvas.h:1199
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
static sk_sp< SkColorFilter > SRGBToLinearGamma()
static sk_sp< SkColorFilter > LinearToSRGBGamma()
void setBlendMode(SkBlendMode mode)
Definition SkPaint.cpp:151
void setColorFilter(sk_sp< SkColorFilter > colorFilter)
struct MyStruct s
const uint8_t uint32_t uint32_t GError ** error
GAsyncResult * result
double y
double x
void(* memset32)(uint32_t[], uint32_t, int)
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
int32_t fWidth
Definition SkSize.h:17
static SkImageInfo MakeN32Premul(int width, int height)
static sk_sp< SkShader > linear(sk_sp< SkShader > shader)