PinnedImageTest.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // This is a GPU-backend specific test.
  8. #include "tests/Test.h"
  9. using namespace sk_gpu_test;
  10. #include "tools/gpu/GrContextFactory.h"
  11. #include "include/core/SkCanvas.h"
  12. #include "include/core/SkSurface.h"
  13. #include "src/core/SkImagePriv.h"
  14. static bool surface_is_expected_color(SkSurface* surf, const SkImageInfo& ii, SkColor color) {
  15. SkBitmap bm;
  16. bm.allocPixels(ii);
  17. surf->readPixels(bm, 0, 0);
  18. for (int y = 0; y < bm.height(); ++y) {
  19. for (int x = 0; x < bm.width(); ++x) {
  20. if (bm.getColor(x, y) != color) {
  21. return false;
  22. }
  23. }
  24. }
  25. return true;
  26. }
  27. static void basic_test(skiatest::Reporter* reporter, GrContext* context) {
  28. const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType);
  29. SkBitmap bm;
  30. bm.allocPixels(ii);
  31. SkCanvas bmCanvas(bm);
  32. bmCanvas.clear(SK_ColorRED);
  33. // We start off with the raster image being all red.
  34. sk_sp<SkImage> img = SkMakeImageFromRasterBitmap(bm, kNever_SkCopyPixelsMode);
  35. sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii);
  36. SkCanvas* canvas = gpuSurface->getCanvas();
  37. // w/o pinning - the gpu draw always reflects the current state of the underlying bitmap
  38. {
  39. canvas->drawImage(img, 0, 0);
  40. REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorRED));
  41. bmCanvas.clear(SK_ColorGREEN);
  42. canvas->drawImage(img, 0, 0);
  43. REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
  44. }
  45. // w/ pinning - the gpu draw is stuck at the pinned state
  46. {
  47. SkImage_pinAsTexture(img.get(), context); // pin at blue
  48. canvas->drawImage(img, 0, 0);
  49. REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
  50. bmCanvas.clear(SK_ColorBLUE);
  51. canvas->drawImage(img, 0, 0);
  52. REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorGREEN));
  53. SkImage_unpinAsTexture(img.get(), context);
  54. }
  55. // once unpinned local changes will be picked up
  56. {
  57. canvas->drawImage(img, 0, 0);
  58. REPORTER_ASSERT(reporter, surface_is_expected_color(gpuSurface.get(), ii, SK_ColorBLUE));
  59. }
  60. }
  61. // Deleting the context while there are still pinned images shouldn't result in a crash.
  62. static void cleanup_test(skiatest::Reporter* reporter) {
  63. const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType);
  64. SkBitmap bm;
  65. bm.allocPixels(ii);
  66. SkCanvas bmCanvas(bm);
  67. bmCanvas.clear(SK_ColorRED);
  68. for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
  69. GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType) i;
  70. {
  71. sk_sp<SkImage> img;
  72. GrContext* context = nullptr;
  73. {
  74. GrContextFactory testFactory;
  75. ContextInfo info = testFactory.getContextInfo(ctxType);
  76. context = info.grContext();
  77. if (!context) {
  78. continue;
  79. }
  80. img = SkMakeImageFromRasterBitmap(bm, kNever_SkCopyPixelsMode);
  81. if (!SkImage_pinAsTexture(img.get(), context)) {
  82. continue;
  83. }
  84. }
  85. // The GrContext used to pin the image is gone at this point!
  86. // "context" isn't technically used in this call but it can't be null!
  87. // We don't really want to support this use case but it currently happens.
  88. SkImage_unpinAsTexture(img.get(), context);
  89. }
  90. }
  91. }
  92. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PinnedImageTest, reporter, ctxInfo) {
  93. basic_test(reporter, ctxInfo.grContext());
  94. cleanup_test(reporter);
  95. }