SpecialSurfaceTest.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2016 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 "include/core/SkCanvas.h"
  9. #include "src/core/SkSpecialImage.h"
  10. #include "src/core/SkSpecialSurface.h"
  11. #include "tests/Test.h"
  12. #include "include/gpu/GrContext.h"
  13. #include "src/gpu/GrCaps.h"
  14. #include "src/gpu/GrContextPriv.h"
  15. #include "src/gpu/SkGr.h"
  16. class TestingSpecialSurfaceAccess {
  17. public:
  18. static const SkIRect& Subset(const SkSpecialSurface* surf) {
  19. return surf->subset();
  20. }
  21. };
  22. // Both 'kSmallerSize' and 'kFullSize' need to be a non-power-of-2 to exercise
  23. // the gpu's loose fit behavior
  24. static const int kSmallerSize = 10;
  25. static const int kPad = 5;
  26. static const int kFullSize = kSmallerSize + 2 * kPad;
  27. // Exercise the public API of SkSpecialSurface (e.g., getCanvas, newImageSnapshot)
  28. static void test_surface(const sk_sp<SkSpecialSurface>& surf,
  29. skiatest::Reporter* reporter,
  30. int offset) {
  31. const SkIRect surfSubset = TestingSpecialSurfaceAccess::Subset(surf.get());
  32. REPORTER_ASSERT(reporter, offset == surfSubset.fLeft);
  33. REPORTER_ASSERT(reporter, offset == surfSubset.fTop);
  34. REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.width());
  35. REPORTER_ASSERT(reporter, kSmallerSize == surfSubset.height());
  36. SkCanvas* canvas = surf->getCanvas();
  37. SkASSERT_RELEASE(canvas);
  38. canvas->clear(SK_ColorRED);
  39. sk_sp<SkSpecialImage> img(surf->makeImageSnapshot());
  40. REPORTER_ASSERT(reporter, img);
  41. const SkIRect imgSubset = img->subset();
  42. REPORTER_ASSERT(reporter, surfSubset == imgSubset);
  43. // the canvas was invalidated by the newImageSnapshot call
  44. REPORTER_ASSERT(reporter, !surf->getCanvas());
  45. }
  46. DEF_TEST(SpecialSurface_Raster, reporter) {
  47. SkImageInfo info = SkImageInfo::MakeN32(kSmallerSize, kSmallerSize, kOpaque_SkAlphaType);
  48. sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRaster(info));
  49. test_surface(surf, reporter, 0);
  50. }
  51. DEF_TEST(SpecialSurface_Raster2, reporter) {
  52. SkBitmap bm;
  53. bm.allocN32Pixels(kFullSize, kFullSize, true);
  54. const SkIRect subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
  55. sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeFromBitmap(subset, bm));
  56. test_surface(surf, reporter, kPad);
  57. // TODO: check that the clear didn't escape the active region
  58. }
  59. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialSurface_Gpu1, reporter, ctxInfo) {
  60. for (auto colorType : {GrColorType::kRGBA_8888, GrColorType::kRGBA_1010102}) {
  61. if (!ctxInfo.grContext()->colorTypeSupportedAsSurface(
  62. GrColorTypeToSkColorType(colorType))) {
  63. continue;
  64. }
  65. sk_sp<SkSpecialSurface> surf(SkSpecialSurface::MakeRenderTarget(
  66. ctxInfo.grContext(), kSmallerSize, kSmallerSize, colorType, nullptr));
  67. test_surface(surf, reporter, 0);
  68. }
  69. }