RecordTest.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/SkBitmap.h"
  8. #include "include/core/SkImageInfo.h"
  9. #include "include/core/SkShader.h"
  10. #include "src/core/SkRecord.h"
  11. #include "src/core/SkRecords.h"
  12. #include "tests/RecordTestUtils.h"
  13. #include "tests/Test.h"
  14. #include <new>
  15. // Sums the area of any DrawRect command it sees.
  16. class AreaSummer {
  17. public:
  18. AreaSummer() : fArea(0) {}
  19. template <typename T> void operator()(const T&) { }
  20. void operator()(const SkRecords::DrawRect& draw) {
  21. fArea += (int)(draw.rect.width() * draw.rect.height());
  22. }
  23. int area() const { return fArea; }
  24. void apply(const SkRecord& record) {
  25. for (int i = 0; i < record.count(); i++) {
  26. record.visit(i, *this);
  27. }
  28. }
  29. private:
  30. int fArea;
  31. };
  32. // Scales out the bottom-right corner of any DrawRect command it sees by 2x.
  33. struct Stretch {
  34. template <typename T> void operator()(T*) {}
  35. void operator()(SkRecords::DrawRect* draw) {
  36. draw->rect.fRight *= 2;
  37. draw->rect.fBottom *= 2;
  38. }
  39. void apply(SkRecord* record) {
  40. for (int i = 0; i < record->count(); i++) {
  41. record->mutate(i, *this);
  42. }
  43. }
  44. };
  45. #define APPEND(record, type, ...) new (record.append<type>()) type{__VA_ARGS__}
  46. // Basic tests for the low-level SkRecord code.
  47. DEF_TEST(Record, r) {
  48. SkRecord record;
  49. // Add a simple DrawRect command.
  50. SkRect rect = SkRect::MakeWH(10, 10);
  51. SkPaint paint;
  52. APPEND(record, SkRecords::DrawRect, paint, rect);
  53. // Its area should be 100.
  54. AreaSummer summer;
  55. summer.apply(record);
  56. REPORTER_ASSERT(r, summer.area() == 100);
  57. // Scale 2x.
  58. Stretch stretch;
  59. stretch.apply(&record);
  60. // Now its area should be 100 + 400.
  61. summer.apply(record);
  62. REPORTER_ASSERT(r, summer.area() == 500);
  63. }
  64. DEF_TEST(Record_defrag, r) {
  65. SkRecord record;
  66. APPEND(record, SkRecords::Save);
  67. APPEND(record, SkRecords::ClipRect);
  68. APPEND(record, SkRecords::NoOp);
  69. APPEND(record, SkRecords::DrawRect);
  70. APPEND(record, SkRecords::NoOp);
  71. APPEND(record, SkRecords::NoOp);
  72. APPEND(record, SkRecords::Restore);
  73. REPORTER_ASSERT(r, record.count() == 7);
  74. record.defrag();
  75. REPORTER_ASSERT(r, record.count() == 4);
  76. assert_type<SkRecords::Save >(r, record, 0);
  77. assert_type<SkRecords::ClipRect>(r, record, 1);
  78. assert_type<SkRecords::DrawRect>(r, record, 2);
  79. assert_type<SkRecords::Restore >(r, record, 3);
  80. }
  81. #undef APPEND
  82. template <typename T>
  83. static bool is_aligned(const T* p) {
  84. return (((uintptr_t)p) & (sizeof(T) - 1)) == 0;
  85. }
  86. DEF_TEST(Record_Alignment, r) {
  87. SkRecord record;
  88. REPORTER_ASSERT(r, is_aligned(record.alloc<uint8_t>()));
  89. REPORTER_ASSERT(r, is_aligned(record.alloc<uint16_t>()));
  90. REPORTER_ASSERT(r, is_aligned(record.alloc<uint32_t>()));
  91. REPORTER_ASSERT(r, is_aligned(record.alloc<void*>()));
  92. // It's not clear if we care that 8-byte values are aligned on 32-bit machines.
  93. if (sizeof(void*) == 8) {
  94. REPORTER_ASSERT(r, is_aligned(record.alloc<double>()));
  95. REPORTER_ASSERT(r, is_aligned(record.alloc<uint64_t>()));
  96. }
  97. }