1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2017 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#ifndef TestUtils_DEFINED
9cb93a386Sopenharmony_ci#define TestUtils_DEFINED
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "include/core/SkBitmap.h"
12cb93a386Sopenharmony_ci#include "src/gpu/GrDataUtils.h"
13cb93a386Sopenharmony_ci#include "tests/Test.h"
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_cinamespace skgpu { class SurfaceContext; }
16cb93a386Sopenharmony_ciclass GrSurfaceProxy;
17cb93a386Sopenharmony_citypedef uint32_t GrColor;
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_ci// Ensure that reading back from 'srcContext' as RGBA 8888 matches 'expectedPixelValues
20cb93a386Sopenharmony_civoid TestReadPixels(skiatest::Reporter*, GrDirectContext*, skgpu::SurfaceContext*,
21cb93a386Sopenharmony_ci                    uint32_t expectedPixelValues[], const char* testName);
22cb93a386Sopenharmony_ci
23cb93a386Sopenharmony_ci// See if trying to write RGBA 8888 pixels to 'dstContext' matches matches the
24cb93a386Sopenharmony_ci// expectation ('expectedToWork')
25cb93a386Sopenharmony_civoid TestWritePixels(skiatest::Reporter*, GrDirectContext*, skgpu::SurfaceContext*,
26cb93a386Sopenharmony_ci                     bool expectedToWork, const char* testName);
27cb93a386Sopenharmony_ci
28cb93a386Sopenharmony_ci// Ensure that the pixels can be copied from 'proxy' viewed as colorType, to an RGBA 8888
29cb93a386Sopenharmony_ci// destination (both texture-backed and rendertarget-backed).
30cb93a386Sopenharmony_civoid TestCopyFromSurface(skiatest::Reporter*,
31cb93a386Sopenharmony_ci                         GrDirectContext*,
32cb93a386Sopenharmony_ci                         sk_sp<GrSurfaceProxy> proxy,
33cb93a386Sopenharmony_ci                         GrSurfaceOrigin origin,
34cb93a386Sopenharmony_ci                         GrColorType colorType,
35cb93a386Sopenharmony_ci                         uint32_t expectedPixelValues[],
36cb93a386Sopenharmony_ci                         const char* testName);
37cb93a386Sopenharmony_ci
38cb93a386Sopenharmony_ci// Encodes the bitmap into a data:/image/png;base64,... url suitable to view in a browser after
39cb93a386Sopenharmony_ci// printing to a log. If false is returned, dst holds an error message instead of a URI.
40cb93a386Sopenharmony_cibool BipmapToBase64DataURI(const SkBitmap& bitmap, SkString* dst);
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_ci/** Used by compare_pixels. */
43cb93a386Sopenharmony_ciusing ComparePixmapsErrorReporter = void(int x, int y, const float diffs[4]);
44cb93a386Sopenharmony_ci
45cb93a386Sopenharmony_ci/**
46cb93a386Sopenharmony_ci * Compares pixels pointed to by 'a' with 'infoA' and rowBytesA to pixels pointed to by 'b' with
47cb93a386Sopenharmony_ci * 'infoB' and 'rowBytesB'.
48cb93a386Sopenharmony_ci *
49cb93a386Sopenharmony_ci * If the pixmaps have different dimensions error is called with negative coordinate values and
50cb93a386Sopenharmony_ci * zero diffs and no comparisons are made.
51cb93a386Sopenharmony_ci *
52cb93a386Sopenharmony_ci * Before comparison pixels are converted to a common color type, alpha type, and color space.
53cb93a386Sopenharmony_ci * The color type is always 32 bit float. The alpha type is premul if one of the pixmaps is
54cb93a386Sopenharmony_ci * premul and the other is unpremul. The color space is linear sRGB if the pixmaps have
55cb93a386Sopenharmony_ci * different colorspaces, otherwise their common color space is used.
56cb93a386Sopenharmony_ci *
57cb93a386Sopenharmony_ci * 'tolRGBA' expresses the allowed difference between pixels in the comparison space per channel. If
58cb93a386Sopenharmony_ci * pixel components differ more than by 'tolRGBA' in absolute value in any channel then 'error' is
59cb93a386Sopenharmony_ci * called with the coordinate and difference in the comparison space (B - A).
60cb93a386Sopenharmony_ci *
61cb93a386Sopenharmony_ci * The function quits after a single error is reported and returns false if 'error' was called and
62cb93a386Sopenharmony_ci * true otherwise.
63cb93a386Sopenharmony_ci */
64cb93a386Sopenharmony_cibool ComparePixels(const GrCPixmap& a,
65cb93a386Sopenharmony_ci                   const GrCPixmap& b,
66cb93a386Sopenharmony_ci                   const float tolRGBA[4],
67cb93a386Sopenharmony_ci                   std::function<ComparePixmapsErrorReporter>& error);
68cb93a386Sopenharmony_ci
69cb93a386Sopenharmony_ci/**
70cb93a386Sopenharmony_ci * Convenience version that checks that 'pixmap' is a solid field of 'col'
71cb93a386Sopenharmony_ci */
72cb93a386Sopenharmony_cibool CheckSolidPixels(const SkColor4f& col,
73cb93a386Sopenharmony_ci                      const SkPixmap& pixmap,
74cb93a386Sopenharmony_ci                      const float tolRGBA[4],
75cb93a386Sopenharmony_ci                      std::function<ComparePixmapsErrorReporter>& error);
76cb93a386Sopenharmony_ci
77cb93a386Sopenharmony_ci/**
78cb93a386Sopenharmony_ci * Checks the ref cnt on a proxy and its backing store. This is only valid if the proxy and the
79cb93a386Sopenharmony_ci * resource are both used on a single thread.
80cb93a386Sopenharmony_ci */
81cb93a386Sopenharmony_civoid CheckSingleThreadedProxyRefs(skiatest::Reporter* reporter,
82cb93a386Sopenharmony_ci                                  GrSurfaceProxy* proxy,
83cb93a386Sopenharmony_ci                                  int32_t expectedProxyRefs,
84cb93a386Sopenharmony_ci                                  int32_t expectedBackingRefs);
85cb93a386Sopenharmony_ci
86cb93a386Sopenharmony_ci// Makes either a SurfaceContext, SurfaceFillContext, or a SurfaceDrawContext, depending on
87cb93a386Sopenharmony_ci// GrRenderable and the GrImageInfo.
88cb93a386Sopenharmony_ci// The texture format is the default for the provided color type.
89cb93a386Sopenharmony_cistd::unique_ptr<skgpu::SurfaceContext> CreateSurfaceContext(
90cb93a386Sopenharmony_ci            GrRecordingContext*,
91cb93a386Sopenharmony_ci            const GrImageInfo&,
92cb93a386Sopenharmony_ci            SkBackingFit = SkBackingFit::kExact,
93cb93a386Sopenharmony_ci            GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin,
94cb93a386Sopenharmony_ci            GrRenderable = GrRenderable::kNo,
95cb93a386Sopenharmony_ci            int sampleCount = 1,
96cb93a386Sopenharmony_ci            GrMipmapped = GrMipmapped::kNo,
97cb93a386Sopenharmony_ci            GrProtected = GrProtected::kNo,
98cb93a386Sopenharmony_ci            SkBudgeted = SkBudgeted::kYes);
99cb93a386Sopenharmony_ci
100cb93a386Sopenharmony_ci#endif
101