RecorderTest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "tests/Test.h"
  8. #include "include/core/SkPictureRecorder.h"
  9. #include "include/core/SkShader.h"
  10. #include "include/core/SkSurface.h"
  11. #include "src/core/SkRecord.h"
  12. #include "src/core/SkRecorder.h"
  13. #include "src/core/SkRecords.h"
  14. #define COUNT(T) + 1
  15. static const int kRecordTypes = SK_RECORD_TYPES(COUNT);
  16. #undef COUNT
  17. // Tallies the types of commands it sees into a histogram.
  18. class Tally {
  19. public:
  20. Tally() { sk_bzero(&fHistogram, sizeof(fHistogram)); }
  21. template <typename T>
  22. void operator()(const T&) { ++fHistogram[T::kType]; }
  23. template <typename T>
  24. int count() const { return fHistogram[T::kType]; }
  25. void apply(const SkRecord& record) {
  26. for (int i = 0; i < record.count(); i++) {
  27. record.visit(i, *this);
  28. }
  29. }
  30. private:
  31. int fHistogram[kRecordTypes];
  32. };
  33. DEF_TEST(Recorder, r) {
  34. SkRecord record;
  35. SkRecorder recorder(&record, 1920, 1080);
  36. recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint());
  37. Tally tally;
  38. tally.apply(record);
  39. REPORTER_ASSERT(r, 1 == tally.count<SkRecords::DrawRect>());
  40. }
  41. // Regression test for leaking refs held by optional arguments.
  42. DEF_TEST(Recorder_RefLeaking, r) {
  43. // We use SaveLayer to test:
  44. // - its SkRect argument is optional and SkRect is POD. Just testing that that works.
  45. // - its SkPaint argument is optional and SkPaint is not POD. The bug was here.
  46. SkRect bounds = SkRect::MakeWH(320, 240);
  47. SkPaint paint;
  48. paint.setShader(SkShaders::Empty());
  49. REPORTER_ASSERT(r, paint.getShader()->unique());
  50. {
  51. SkRecord record;
  52. SkRecorder recorder(&record, 1920, 1080);
  53. recorder.saveLayer(&bounds, &paint);
  54. REPORTER_ASSERT(r, !paint.getShader()->unique());
  55. }
  56. REPORTER_ASSERT(r, paint.getShader()->unique());
  57. }
  58. DEF_TEST(Recorder_drawImage_takeReference, reporter) {
  59. sk_sp<SkImage> image;
  60. {
  61. auto surface(SkSurface::MakeRasterN32Premul(100, 100));
  62. surface->getCanvas()->clear(SK_ColorGREEN);
  63. image = surface->makeImageSnapshot();
  64. }
  65. {
  66. SkRecord record;
  67. SkRecorder recorder(&record, 100, 100);
  68. // DrawImage is supposed to take a reference
  69. recorder.drawImage(image, 0, 0);
  70. REPORTER_ASSERT(reporter, !image->unique());
  71. Tally tally;
  72. tally.apply(record);
  73. REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImage>());
  74. }
  75. REPORTER_ASSERT(reporter, image->unique());
  76. {
  77. SkRecord record;
  78. SkRecorder recorder(&record, 100, 100);
  79. // DrawImageRect is supposed to take a reference
  80. recorder.drawImageRect(image, SkRect::MakeWH(100, 100), nullptr);
  81. REPORTER_ASSERT(reporter, !image->unique());
  82. Tally tally;
  83. tally.apply(record);
  84. REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImageRect>());
  85. }
  86. REPORTER_ASSERT(reporter, image->unique());
  87. }