Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions
ApplyGammaTest.cpp File Reference
#include "include/core/SkBitmap.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorFilter.h"
#include "include/core/SkColorPriv.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPaint.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkSamplingOptions.h"
#include "include/core/SkScalar.h"
#include "include/core/SkSize.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTypes.h"
#include "include/gpu/GpuTypes.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/GrTypes.h"
#include "include/gpu/ganesh/SkSurfaceGanesh.h"
#include "include/private/base/SkTemplates.h"
#include "src/core/SkMemset.h"
#include "src/gpu/ganesh/GrCaps.h"
#include "src/gpu/ganesh/GrDirectContextPriv.h"
#include "src/gpu/ganesh/GrShaderCaps.h"
#include "tests/CtsEnforcement.h"
#include "tests/Test.h"
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <initializer_list>

Go to the source code of this file.

Functions

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)
 
 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS (ApplyGamma, reporter, ctxInfo, CtsEnforcement::kNever)
 

Function Documentation

◆ check_gamma()

bool check_gamma ( uint32_t  src,
uint32_t  dst,
bool  toSRGB,
float  error,
uint32_t *  expected 
)

Definition at line 62 of file ApplyGammaTest.cpp.

63 {
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}
static float linear_to_srgb(float linear)
static float srgb_to_linear(float srgb)
#define SkASSERT(cond)
Definition SkAssert.h:116
#define SkGetPackedA32(packed)
Definition SkColorPriv.h:92
#define SkScalarRoundToInt(x)
Definition SkScalar.h:37
#define SkScalarCeilToInt(x)
Definition SkScalar.h:36
#define SkScalarFloorToInt(x)
Definition SkScalar.h:35
const uint8_t uint32_t uint32_t GError ** error
GAsyncResult * result
dst
Definition cp.py:12

◆ DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS()

DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS ( ApplyGamma  ,
reporter  ,
ctxInfo  ,
CtsEnforcement::kNever   
)

Definition at line 107 of file ApplyGammaTest.cpp.

107 {
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;
146 : SkColorFilters::SRGBToLinearGamma());
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}
bool check_gamma(uint32_t src, uint32_t dst, bool toSRGB, float error, uint32_t *expected)
reporter
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 ERRORF(r,...)
Definition Test.h:293
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 > LinearToSRGBGamma()
void setBlendMode(SkBlendMode mode)
Definition SkPaint.cpp:151
void setColorFilter(sk_sp< SkColorFilter > colorFilter)
struct MyStruct s
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)

◆ linear_to_srgb()

static float linear_to_srgb ( float  linear)
static

convert 0..1 linear value to 0..1 srgb

Definition at line 45 of file ApplyGammaTest.cpp.

45 {
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}
static sk_sp< SkShader > linear(sk_sp< SkShader > shader)

◆ srgb_to_linear()

static float srgb_to_linear ( float  srgb)
static

convert 0..1 srgb value to 0..1 linear

Definition at line 54 of file ApplyGammaTest.cpp.

54 {
55 if (srgb <= 0.04045f) {
56 return srgb / 12.92f;
57 } else {
58 return powf((srgb + 0.055f) / 1.055f, 2.4f);
59 }
60}