xref: /aosp_15_r20/external/skia/tests/TestUtils.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2017 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 
8 #ifndef TestUtils_DEFINED
9 #define TestUtils_DEFINED
10 
11 #include "include/core/SkColor.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkTypes.h"
14 #include "include/gpu/GpuTypes.h"
15 #include "include/gpu/ganesh/GrTypes.h"
16 #include "src/gpu/SkBackingFit.h"
17 #include "src/gpu/ganesh/GrImageInfo.h"
18 #include "src/gpu/ganesh/GrPixmap.h"
19 
20 #include <cstdint>
21 #include <functional>
22 #include <memory>
23 
24 class GrDirectContext;
25 class GrRecordingContext;
26 class GrSurfaceProxy;
27 class SkPixmap;
28 enum class GrColorType;
29 namespace skiatest { class Reporter; }
30 namespace skgpu::ganesh {
31 class SurfaceContext;
32 }
33 typedef uint32_t GrColor;
34 
35 // Ensure that reading back from 'srcContext' as RGBA 8888 matches 'expectedPixelValues
36 void TestReadPixels(skiatest::Reporter*,
37                     GrDirectContext*,
38                     skgpu::ganesh::SurfaceContext*,
39                     uint32_t expectedPixelValues[],
40                     const char* testName);
41 
42 // See if trying to write RGBA 8888 pixels to 'dstContext' matches the
43 // expectation ('expectedToWork')
44 void TestWritePixels(skiatest::Reporter*,
45                      GrDirectContext*,
46                      skgpu::ganesh::SurfaceContext*,
47                      bool expectedToWork,
48                      const char* testName);
49 
50 // Ensure that the pixels can be copied from 'proxy' viewed as colorType, to an RGBA 8888
51 // destination (both texture-backed and rendertarget-backed).
52 void TestCopyFromSurface(skiatest::Reporter*,
53                          GrDirectContext*,
54                          sk_sp<GrSurfaceProxy> proxy,
55                          GrSurfaceOrigin origin,
56                          GrColorType colorType,
57                          uint32_t expectedPixelValues[],
58                          const char* testName);
59 
60 /** Used by compare_pixels. */
61 using ComparePixmapsErrorReporter = void(int x, int y, const float diffs[4]);
62 
63 /**
64  * Compares pixels pointed to by 'a' to pixels pointed to by 'b'.
65  *
66  * If the pixmaps have different dimensions error is called with negative coordinate values and
67  * zero diffs and no comparisons are made.
68  *
69  * Before comparison pixels are converted to a common color type, alpha type, and color space.
70  * The color type is always 32 bit float. The alpha type is premul if one of the pixmaps is
71  * premul and the other is unpremul. The color space is linear sRGB if the pixmaps have
72  * different colorspaces, otherwise their common color space is used.
73  *
74  * 'tolRGBA' expresses the allowed difference between pixels in the comparison space per channel. If
75  * pixel components differ more than by 'tolRGBA' in absolute value in any channel then 'error' is
76  * called with the coordinate and difference in the comparison space (B - A).
77  *
78  * The function quits after a single error is reported and returns false if 'error' was called and
79  * true otherwise.
80  */
81 bool ComparePixels(const GrCPixmap& a,
82                    const GrCPixmap& b,
83                    const float tolRGBA[4],
84                    std::function<ComparePixmapsErrorReporter>& error);
85 
86 /**
87  * Convenience version that checks that 'pixmap' is a solid field of 'col'
88  */
89 bool CheckSolidPixels(const SkColor4f& col,
90                       const SkPixmap& pixmap,
91                       const float tolRGBA[4],
92                       std::function<ComparePixmapsErrorReporter>& error);
93 
94 /**
95  * Checks the ref cnt on a proxy and its backing store. This is only valid if the proxy and the
96  * resource are both used on a single thread.
97  */
98 void CheckSingleThreadedProxyRefs(skiatest::Reporter* reporter,
99                                   GrSurfaceProxy* proxy,
100                                   int32_t expectedProxyRefs,
101                                   int32_t expectedBackingRefs);
102 
103 // Makes either a SurfaceContext, SurfaceFillContext, or a SurfaceDrawContext, depending on
104 // GrRenderable and the GrImageInfo.
105 // The texture format is the default for the provided color type.
106 std::unique_ptr<skgpu::ganesh::SurfaceContext> CreateSurfaceContext(
107         GrRecordingContext*,
108         const GrImageInfo&,
109         SkBackingFit = SkBackingFit::kExact,
110         GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
111         GrRenderable = GrRenderable::kNo,
112         int sampleCount = 1,
113         skgpu::Mipmapped = skgpu::Mipmapped::kNo,
114         GrProtected = GrProtected::kNo,
115         skgpu::Budgeted = skgpu::Budgeted::kYes);
116 
117 #endif
118