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