commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 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 | |
| 8 | #include "SkDeque.h" |
| 9 | #include "SkLayerRasterizer.h" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkRasterizer.h" |
| 12 | #include "Test.h" |
| 13 | |
| 14 | class SkReadBuffer; |
| 15 | |
| 16 | // Dummy class to place on a paint just to ensure the paint's destructor |
| 17 | // is called. |
| 18 | // ONLY to be used by LayerRasterizer_destructor, since other tests may |
| 19 | // be run in a separate thread, and this class is not threadsafe. |
| 20 | class DummyRasterizer : public SkRasterizer { |
| 21 | public: |
| 22 | DummyRasterizer() |
| 23 | : INHERITED() |
| 24 | { |
| 25 | // Not threadsafe. Only used in one thread. |
| 26 | gCount++; |
| 27 | } |
| 28 | |
| 29 | ~DummyRasterizer() { |
| 30 | // Not threadsafe. Only used in one thread. |
| 31 | gCount--; |
| 32 | } |
| 33 | |
| 34 | static int GetCount() { return gCount; } |
| 35 | |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 36 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyRasterizer) |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 37 | |
| 38 | private: |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 39 | static int gCount; |
| 40 | |
| 41 | typedef SkRasterizer INHERITED; |
| 42 | }; |
| 43 | |
| 44 | int DummyRasterizer::gCount; |
| 45 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 46 | sk_sp<SkFlattenable> DummyRasterizer::CreateProc(SkReadBuffer&) { |
| 47 | return sk_make_sp<DummyRasterizer>(); |
| 48 | } |
mtklein | 7e44bb1 | 2015-01-07 09:06:08 -0800 | [diff] [blame] | 49 | |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 50 | // Check to make sure that the SkPaint in the layer has its destructor called. |
| 51 | DEF_TEST(LayerRasterizer_destructor, reporter) { |
| 52 | { |
| 53 | SkPaint paint; |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 54 | paint.setRasterizer(sk_make_sp<DummyRasterizer>()); |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 55 | REPORTER_ASSERT(reporter, DummyRasterizer::GetCount() == 1); |
| 56 | |
| 57 | SkLayerRasterizer::Builder builder; |
| 58 | builder.addLayer(paint); |
| 59 | } |
| 60 | REPORTER_ASSERT(reporter, DummyRasterizer::GetCount() == 0); |
| 61 | } |
| 62 | |
| 63 | class LayerRasterizerTester { |
| 64 | public: |
| 65 | static int CountLayers(const SkLayerRasterizer& layerRasterizer) { |
| 66 | return layerRasterizer.fLayers->count(); |
| 67 | } |
| 68 | |
| 69 | static const SkDeque& GetLayers(const SkLayerRasterizer& layerRasterizer) { |
| 70 | return *layerRasterizer.fLayers; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | // MUST stay in sync with definition of SkLayerRasterizer_Rec in SkLayerRasterizer.cpp. |
| 75 | struct SkLayerRasterizer_Rec { |
| 76 | SkPaint fPaint; |
| 77 | SkVector fOffset; |
| 78 | }; |
| 79 | |
| 80 | static bool equals(const SkLayerRasterizer_Rec& rec1, const SkLayerRasterizer_Rec& rec2) { |
| 81 | return rec1.fPaint == rec2.fPaint && rec1.fOffset == rec2.fOffset; |
| 82 | } |
| 83 | |
| 84 | DEF_TEST(LayerRasterizer_copy, reporter) { |
| 85 | SkLayerRasterizer::Builder builder; |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 86 | REPORTER_ASSERT(reporter, nullptr == builder.snapshot()); |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 87 | SkPaint paint; |
| 88 | // Create a bunch of paints with different flags. |
| 89 | for (uint32_t flags = 0x01; flags < SkPaint::kAllFlags; flags <<= 1) { |
| 90 | paint.setFlags(flags); |
commit-bot@chromium.org | 55c9b4e | 2014-04-10 21:09:33 +0000 | [diff] [blame] | 91 | builder.addLayer(paint, static_cast<SkScalar>(flags), static_cast<SkScalar>(flags)); |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // Create a layer rasterizer with all the existing layers. |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 95 | sk_sp<SkLayerRasterizer> firstCopy(builder.snapshot()); |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 96 | |
| 97 | // Add one more layer. |
| 98 | paint.setFlags(SkPaint::kAllFlags); |
| 99 | builder.addLayer(paint); |
| 100 | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 101 | sk_sp<SkLayerRasterizer> oneLarger(builder.snapshot()); |
| 102 | sk_sp<SkLayerRasterizer> detached(builder.detach()); |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 103 | |
| 104 | // Check the counts for consistency. |
| 105 | const int largerCount = LayerRasterizerTester::CountLayers(*oneLarger.get()); |
| 106 | const int smallerCount = LayerRasterizerTester::CountLayers(*firstCopy.get()); |
| 107 | REPORTER_ASSERT(reporter, largerCount == LayerRasterizerTester::CountLayers(*detached.get())); |
| 108 | REPORTER_ASSERT(reporter, smallerCount == largerCount - 1); |
| 109 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 110 | const SkLayerRasterizer_Rec* recFirstCopy = nullptr; |
| 111 | const SkLayerRasterizer_Rec* recOneLarger = nullptr; |
| 112 | const SkLayerRasterizer_Rec* recDetached = nullptr; |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 113 | |
| 114 | const SkDeque& layersFirstCopy = LayerRasterizerTester::GetLayers(*firstCopy.get()); |
| 115 | const SkDeque& layersOneLarger = LayerRasterizerTester::GetLayers(*oneLarger.get()); |
| 116 | const SkDeque& layersDetached = LayerRasterizerTester::GetLayers(*detached.get()); |
| 117 | |
| 118 | // Ensure that our version of SkLayerRasterizer_Rec is the same as the one in |
| 119 | // SkLayerRasterizer.cpp - or at least the same size. If the order were switched, we |
| 120 | // would fail the test elsewhere. |
| 121 | REPORTER_ASSERT(reporter, layersFirstCopy.elemSize() == sizeof(SkLayerRasterizer_Rec)); |
| 122 | REPORTER_ASSERT(reporter, layersOneLarger.elemSize() == sizeof(SkLayerRasterizer_Rec)); |
| 123 | REPORTER_ASSERT(reporter, layersDetached.elemSize() == sizeof(SkLayerRasterizer_Rec)); |
| 124 | |
| 125 | SkDeque::F2BIter iterFirstCopy(layersFirstCopy); |
| 126 | SkDeque::F2BIter iterOneLarger(layersOneLarger); |
| 127 | SkDeque::F2BIter iterDetached(layersDetached); |
| 128 | |
| 129 | for (int i = 0; i < largerCount; ++i) { |
| 130 | recFirstCopy = static_cast<const SkLayerRasterizer_Rec*>(iterFirstCopy.next()); |
| 131 | recOneLarger = static_cast<const SkLayerRasterizer_Rec*>(iterOneLarger.next()); |
| 132 | recDetached = static_cast<const SkLayerRasterizer_Rec*>(iterDetached.next()); |
| 133 | |
| 134 | REPORTER_ASSERT(reporter, equals(*recOneLarger, *recDetached)); |
| 135 | if (smallerCount == i) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 136 | REPORTER_ASSERT(reporter, recFirstCopy == nullptr); |
commit-bot@chromium.org | 6573ce7 | 2014-04-10 20:42:53 +0000 | [diff] [blame] | 137 | } else { |
| 138 | REPORTER_ASSERT(reporter, equals(*recFirstCopy, *recOneLarger)); |
| 139 | } |
| 140 | } |
| 141 | } |
scroggo | 65044bf | 2014-06-03 13:12:51 -0700 | [diff] [blame] | 142 | |
| 143 | DEF_TEST(LayerRasterizer_detachEmpty, reporter) { |
| 144 | SkLayerRasterizer::Builder builder; |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 145 | REPORTER_ASSERT(reporter, nullptr == builder.detach()); |
scroggo | 65044bf | 2014-06-03 13:12:51 -0700 | [diff] [blame] | 146 | } |