Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 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 | |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 8 | #ifndef TestUtils_DEFINED |
| 9 | #define TestUtils_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkBitmap.h" |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrDataUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "tests/Test.h" |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 14 | |
Robert Phillips | 53eaa64 | 2021-08-10 13:49:51 -0400 | [diff] [blame] | 15 | namespace skgpu { class SurfaceContext; } |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 16 | class GrSurfaceProxy; |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 17 | typedef uint32_t GrColor; |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 18 | |
| 19 | // Ensure that reading back from 'srcContext' as RGBA 8888 matches 'expectedPixelValues |
Robert Phillips | 643f481 | 2021-08-11 09:31:00 -0400 | [diff] [blame] | 20 | void TestReadPixels(skiatest::Reporter*, GrDirectContext*, skgpu::SurfaceContext*, |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 21 | uint32_t expectedPixelValues[], const char* testName); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 22 | |
| 23 | // See if trying to write RGBA 8888 pixels to 'dstContext' matches matches the |
| 24 | // expectation ('expectedToWork') |
Robert Phillips | 643f481 | 2021-08-11 09:31:00 -0400 | [diff] [blame] | 25 | void TestWritePixels(skiatest::Reporter*, GrDirectContext*, skgpu::SurfaceContext*, |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 26 | bool expectedToWork, const char* testName); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 27 | |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 28 | // Ensure that the pixels can be copied from 'proxy' viewed as colorType, to an RGBA 8888 |
| 29 | // destination (both texture-backed and rendertarget-backed). |
Brian Salomon | 982127b | 2021-01-21 10:43:35 -0500 | [diff] [blame] | 30 | void TestCopyFromSurface(skiatest::Reporter*, |
| 31 | GrDirectContext*, |
| 32 | sk_sp<GrSurfaceProxy> proxy, |
| 33 | GrSurfaceOrigin origin, |
| 34 | GrColorType colorType, |
| 35 | uint32_t expectedPixelValues[], |
| 36 | const char* testName); |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 37 | |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 38 | // Encodes the bitmap into a data:/image/png;base64,... url suitable to view in a browser after |
| 39 | // printing to a log. If false is returned, dst holds an error message instead of a URI. |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 40 | bool BipmapToBase64DataURI(const SkBitmap& bitmap, SkString* dst); |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 41 | |
| 42 | /** Used by compare_pixels. */ |
| 43 | using ComparePixmapsErrorReporter = void(int x, int y, const float diffs[4]); |
| 44 | |
| 45 | /** |
| 46 | * Compares pixels pointed to by 'a' with 'infoA' and rowBytesA to pixels pointed to by 'b' with |
| 47 | * 'infoB' and 'rowBytesB'. |
| 48 | * |
Brian Salomon | d8db588 | 2021-03-31 14:19:31 -0400 | [diff] [blame] | 49 | * If the pixmaps have different dimensions error is called with negative coordinate values and |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 50 | * zero diffs and no comparisons are made. |
| 51 | * |
| 52 | * Before comparison pixels are converted to a common color type, alpha type, and color space. |
Brian Salomon | d8db588 | 2021-03-31 14:19:31 -0400 | [diff] [blame] | 53 | * The color type is always 32 bit float. The alpha type is premul if one of the pixmaps is |
| 54 | * premul and the other is unpremul. The color space is linear sRGB if the pixmaps have |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 55 | * different colorspaces, otherwise their common color space is used. |
| 56 | * |
| 57 | * 'tolRGBA' expresses the allowed difference between pixels in the comparison space per channel. If |
| 58 | * pixel components differ more than by 'tolRGBA' in absolute value in any channel then 'error' is |
| 59 | * called with the coordinate and difference in the comparison space (B - A). |
| 60 | * |
| 61 | * The function quits after a single error is reported and returns false if 'error' was called and |
| 62 | * true otherwise. |
| 63 | */ |
Brian Salomon | d8db588 | 2021-03-31 14:19:31 -0400 | [diff] [blame] | 64 | bool ComparePixels(const GrCPixmap& a, |
| 65 | const GrCPixmap& b, |
| 66 | const float tolRGBA[4], |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 67 | std::function<ComparePixmapsErrorReporter>& error); |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Convenience version that checks that 'pixmap' is a solid field of 'col' |
| 71 | */ |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 72 | bool CheckSolidPixels(const SkColor4f& col, |
| 73 | const SkPixmap& pixmap, |
| 74 | const float tolRGBA[4], |
| 75 | std::function<ComparePixmapsErrorReporter>& error); |
Brian Salomon | 557e812 | 2019-10-24 10:37:08 -0400 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Checks the ref cnt on a proxy and its backing store. This is only valid if the proxy and the |
| 79 | * resource are both used on a single thread. |
| 80 | */ |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 81 | void CheckSingleThreadedProxyRefs(skiatest::Reporter* reporter, |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 82 | GrSurfaceProxy* proxy, |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 83 | int32_t expectedProxyRefs, |
| 84 | int32_t expectedBackingRefs); |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 85 | |
Robert Phillips | 168296b | 2021-08-16 11:41:27 -0400 | [diff] [blame] | 86 | // Makes either a SurfaceContext, SurfaceFillContext, or a SurfaceDrawContext, depending on |
| 87 | // GrRenderable and the GrImageInfo. |
| 88 | // The texture format is the default for the provided color type. |
| 89 | std::unique_ptr<skgpu::SurfaceContext> CreateSurfaceContext( |
| 90 | GrRecordingContext*, |
| 91 | const GrImageInfo&, |
| 92 | SkBackingFit = SkBackingFit::kExact, |
| 93 | GrSurfaceOrigin = kTopLeft_GrSurfaceOrigin, |
| 94 | GrRenderable = GrRenderable::kNo, |
| 95 | int sampleCount = 1, |
| 96 | GrMipmapped = GrMipmapped::kNo, |
| 97 | GrProtected = GrProtected::kNo, |
| 98 | SkBudgeted = SkBudgeted::kYes); |
| 99 | |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 100 | #endif |