TestUtils.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #include "include/core/SkBitmap.h"
  8. #include "src/gpu/GrDataUtils.h"
  9. #include "tests/Test.h"
  10. class GrSurfaceContext;
  11. class GrSurfaceProxy;
  12. typedef uint32_t GrColor;
  13. // Ensure that reading back from 'srcContext' as RGBA 8888 matches 'expectedPixelValues
  14. void test_read_pixels(skiatest::Reporter*, GrSurfaceContext* srcContext,
  15. uint32_t expectedPixelValues[], const char* testName);
  16. // See if trying to write RGBA 8888 pixels to 'dstContext' matches matches the
  17. // expectation ('expectedToWork')
  18. void test_write_pixels(skiatest::Reporter*, GrSurfaceContext* srcContext, bool expectedToWork,
  19. const char* testName);
  20. // Ensure that the pixels can be copied from 'proxy' viewed as colorType, to an RGBA 8888
  21. // destination (both texture-backed and rendertarget-backed).
  22. void test_copy_from_surface(skiatest::Reporter*, GrContext*, GrSurfaceProxy* proxy,
  23. GrColorType colorType, uint32_t expectedPixelValues[],
  24. const char* testName);
  25. // Fills data with a red-green gradient
  26. void fill_pixel_data(int width, int height, GrColor* data);
  27. // Create a solid colored backend texture
  28. bool create_backend_texture(GrContext*, GrBackendTexture* backendTex,
  29. const SkImageInfo& ii, const SkColor4f& color,
  30. GrMipMapped, GrRenderable);
  31. void delete_backend_texture(GrContext*, const GrBackendTexture& backendTex);
  32. // Checks srcBuffer and dstBuffer contain the same colors
  33. bool does_full_buffer_contain_correct_color(const GrColor* srcBuffer, const GrColor* dstBuffer,
  34. int width, int height);
  35. // Encodes the bitmap into a data:/image/png;base64,... url suitable to view in a browser after
  36. // printing to a log. If false is returned, dst holds an error message instead of a URI.
  37. bool bitmap_to_base64_data_uri(const SkBitmap& bitmap, SkString* dst);
  38. /** Used by compare_pixels. */
  39. using ComparePixmapsErrorReporter = void(int x, int y, const float diffs[4]);
  40. /**
  41. * Compares pixels pointed to by 'a' with 'infoA' and rowBytesA to pixels pointed to by 'b' with
  42. * 'infoB' and 'rowBytesB'.
  43. *
  44. * If the infos have different dimensions error is called with negative coordinate values and
  45. * zero diffs and no comparisons are made.
  46. *
  47. * Before comparison pixels are converted to a common color type, alpha type, and color space.
  48. * The color type is always 32 bit float. The alpha type is premul if one of 'infoA' and 'infoB' is
  49. * premul and the other is unpremul. The color space is linear sRGB if 'infoA' and 'infoB' have
  50. * different colorspaces, otherwise their common color space is used.
  51. *
  52. * 'tolRGBA' expresses the allowed difference between pixels in the comparison space per channel. If
  53. * pixel components differ more than by 'tolRGBA' in absolute value in any channel then 'error' is
  54. * called with the coordinate and difference in the comparison space (B - A).
  55. *
  56. * The function quits after a single error is reported and returns false if 'error' was called and
  57. * true otherwise.
  58. */
  59. bool compare_pixels(const GrPixelInfo& infoA, const char* a, size_t rowBytesA,
  60. const GrPixelInfo& infoB, const char* b, size_t rowBytesB,
  61. const float tolRGBA[4], std::function<ComparePixmapsErrorReporter>& error);
  62. /** Convenience version of above that takes SkPixmap inputs. */
  63. bool compare_pixels(const SkPixmap& a, const SkPixmap& b, const float tolRGBA[4],
  64. std::function<ComparePixmapsErrorReporter>& error);