blob: bb6037cbe78cd2b79743b642a3459c2aced904ff [file] [log] [blame]
Robert Phillips3500b772017-01-27 10:11:42 -05001/*
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 Salomon72c7b982020-10-06 10:07:38 -04008#ifndef TestUtils_DEFINED
9#define TestUtils_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkBitmap.h"
Brian Salomon85aeccf2019-07-15 12:30:44 -040012#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "tests/Test.h"
Robert Phillips3500b772017-01-27 10:11:42 -050014
Robert Phillips3500b772017-01-27 10:11:42 -050015class GrSurfaceContext;
16class GrSurfaceProxy;
Timothy Liang760dbc42018-07-17 13:28:20 -040017typedef uint32_t GrColor;
Robert Phillips3500b772017-01-27 10:11:42 -050018
19// Ensure that reading back from 'srcContext' as RGBA 8888 matches 'expectedPixelValues
Adlai Hollerc95b5892020-08-11 12:02:22 -040020void TestReadPixels(skiatest::Reporter*, GrDirectContext*, GrSurfaceContext* srcContext,
Brian Salomon28a8f282019-10-24 20:07:39 -040021 uint32_t expectedPixelValues[], const char* testName);
Robert Phillips3500b772017-01-27 10:11:42 -050022
23// See if trying to write RGBA 8888 pixels to 'dstContext' matches matches the
24// expectation ('expectedToWork')
Adlai Hollerc95b5892020-08-11 12:02:22 -040025void TestWritePixels(skiatest::Reporter*, GrDirectContext*, GrSurfaceContext* srcContext,
26 bool expectedToWork, const char* testName);
Robert Phillips3500b772017-01-27 10:11:42 -050027
Brian Salomond6287472019-06-24 15:50:07 -040028// 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 Salomon982127b2021-01-21 10:43:35 -050030void 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 Liang760dbc42018-07-17 13:28:20 -040037
Michael Ludwige8e10752018-10-01 12:42:53 -040038// 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 Salomon28a8f282019-10-24 20:07:39 -040040bool BipmapToBase64DataURI(const SkBitmap& bitmap, SkString* dst);
Brian Salomon85aeccf2019-07-15 12:30:44 -040041
42/** Used by compare_pixels. */
43using 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 Salomond8db5882021-03-31 14:19:31 -040049 * If the pixmaps have different dimensions error is called with negative coordinate values and
Brian Salomon85aeccf2019-07-15 12:30:44 -040050 * 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 Salomond8db5882021-03-31 14:19:31 -040053 * 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 Salomon85aeccf2019-07-15 12:30:44 -040055 * 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 Salomond8db5882021-03-31 14:19:31 -040064bool ComparePixels(const GrCPixmap& a,
65 const GrCPixmap& b,
66 const float tolRGBA[4],
Brian Salomon28a8f282019-10-24 20:07:39 -040067 std::function<ComparePixmapsErrorReporter>& error);
Robert Phillipse3b6fe42019-09-11 11:26:46 -040068
69/**
70 * Convenience version that checks that 'pixmap' is a solid field of 'col'
71 */
Brian Salomon28a8f282019-10-24 20:07:39 -040072bool CheckSolidPixels(const SkColor4f& col,
73 const SkPixmap& pixmap,
74 const float tolRGBA[4],
75 std::function<ComparePixmapsErrorReporter>& error);
Brian Salomon557e8122019-10-24 10:37:08 -040076
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 Salomon28a8f282019-10-24 20:07:39 -040081void CheckSingleThreadedProxyRefs(skiatest::Reporter* reporter,
Greg Danield11ae2e2020-02-10 16:36:07 -050082 GrSurfaceProxy* proxy,
Brian Salomon28a8f282019-10-24 20:07:39 -040083 int32_t expectedProxyRefs,
84 int32_t expectedBackingRefs);
Brian Salomon72c7b982020-10-06 10:07:38 -040085
86#endif