ImageNewShaderTest.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2014 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/SkCanvas.h"
  8. #include "include/core/SkImage.h"
  9. #include "include/core/SkShader.h"
  10. #include "include/core/SkSurface.h"
  11. #include "include/core/SkTypes.h"
  12. #include "tests/Test.h"
  13. #include "include/gpu/GrContext.h"
  14. static void test_bitmap_equality(skiatest::Reporter* reporter, SkBitmap& bm1, SkBitmap& bm2) {
  15. REPORTER_ASSERT(reporter, bm1.computeByteSize() == bm2.computeByteSize());
  16. REPORTER_ASSERT(reporter, 0 == memcmp(bm1.getPixels(), bm2.getPixels(), bm1.computeByteSize()));
  17. }
  18. static void paint_source(SkSurface* sourceSurface) {
  19. SkCanvas* sourceCanvas = sourceSurface->getCanvas();
  20. sourceCanvas->clear(0xFFDEDEDE);
  21. SkPaint paintColor;
  22. paintColor.setColor(0xFFFF0000);
  23. paintColor.setStyle(SkPaint::kFill_Style);
  24. SkRect rect = SkRect::MakeXYWH(
  25. SkIntToScalar(1),
  26. SkIntToScalar(0),
  27. SkIntToScalar(1),
  28. SkIntToScalar(sourceSurface->height()));
  29. sourceCanvas->drawRect(rect, paintColor);
  30. }
  31. static void run_shader_test(skiatest::Reporter* reporter, SkSurface* sourceSurface,
  32. SkSurface* destinationSurface, SkImageInfo& info) {
  33. paint_source(sourceSurface);
  34. sk_sp<SkImage> sourceImage(sourceSurface->makeImageSnapshot());
  35. sk_sp<SkShader> sourceShader = sourceImage->makeShader(
  36. SkTileMode::kRepeat,
  37. SkTileMode::kRepeat);
  38. SkPaint paint;
  39. paint.setShader(sourceShader);
  40. SkCanvas* destinationCanvas = destinationSurface->getCanvas();
  41. destinationCanvas->clear(SK_ColorTRANSPARENT);
  42. destinationCanvas->drawPaint(paint);
  43. SkBitmap bmOrig;
  44. bmOrig.allocN32Pixels(info.width(), info.height());
  45. sourceSurface->readPixels(bmOrig, 0, 0);
  46. SkBitmap bm;
  47. bm.allocN32Pixels(info.width(), info.height());
  48. destinationSurface->readPixels(bm, 0, 0);
  49. test_bitmap_equality(reporter, bmOrig, bm);
  50. // Test with a translated shader
  51. SkMatrix matrix;
  52. matrix.setTranslate(SkIntToScalar(-1), SkIntToScalar(0));
  53. sk_sp<SkShader> sourceShaderTranslated = sourceImage->makeShader(
  54. SkTileMode::kRepeat,
  55. SkTileMode::kRepeat,
  56. &matrix);
  57. destinationCanvas->clear(SK_ColorTRANSPARENT);
  58. SkPaint paintTranslated;
  59. paintTranslated.setShader(sourceShaderTranslated);
  60. destinationCanvas->drawPaint(paintTranslated);
  61. SkBitmap bmt;
  62. bmt.allocN32Pixels(info.width(), info.height());
  63. destinationSurface->readPixels(bmt, 0, 0);
  64. // Test correctness
  65. {
  66. for (int y = 0; y < info.height(); y++) {
  67. REPORTER_ASSERT(reporter, 0xFFFF0000 == bmt.getColor(0, y));
  68. for (int x = 1; x < info.width(); x++) {
  69. REPORTER_ASSERT(reporter, 0xFFDEDEDE == bmt.getColor(x, y));
  70. }
  71. }
  72. }
  73. }
  74. DEF_TEST(ImageNewShader, reporter) {
  75. SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
  76. auto sourceSurface(SkSurface::MakeRaster(info));
  77. auto destinationSurface(SkSurface::MakeRaster(info));
  78. run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
  79. }
  80. static void gpu_to_gpu(skiatest::Reporter* reporter, GrContext* context) {
  81. SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
  82. auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
  83. auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
  84. run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
  85. }
  86. static void gpu_to_raster(skiatest::Reporter* reporter, GrContext* context) {
  87. SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
  88. auto sourceSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
  89. auto destinationSurface(SkSurface::MakeRaster(info));
  90. run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
  91. }
  92. static void raster_to_gpu(skiatest::Reporter* reporter, GrContext* context) {
  93. SkImageInfo info = SkImageInfo::MakeN32Premul(5, 5);
  94. auto sourceSurface(SkSurface::MakeRaster(info));
  95. auto destinationSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
  96. run_shader_test(reporter, sourceSurface.get(), destinationSurface.get(), info);
  97. }
  98. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageNewShader_GPU, reporter, ctxInfo) {
  99. // GPU -> GPU
  100. gpu_to_gpu(reporter, ctxInfo.grContext());
  101. // GPU -> RASTER
  102. gpu_to_raster(reporter, ctxInfo.grContext());
  103. // RASTER -> GPU
  104. raster_to_gpu(reporter, ctxInfo.grContext());
  105. }