OLD | NEW |
(Empty) | |
| 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 |
| 36 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyRasterizer) |
| 37 |
| 38 private: |
| 39 DummyRasterizer(SkReadBuffer&) {} |
| 40 |
| 41 static int gCount; |
| 42 |
| 43 typedef SkRasterizer INHERITED; |
| 44 }; |
| 45 |
| 46 int DummyRasterizer::gCount; |
| 47 |
| 48 // Check to make sure that the SkPaint in the layer has its destructor called. |
| 49 DEF_TEST(LayerRasterizer_destructor, reporter) { |
| 50 { |
| 51 SkPaint paint; |
| 52 paint.setRasterizer(SkNEW(DummyRasterizer))->unref(); |
| 53 REPORTER_ASSERT(reporter, DummyRasterizer::GetCount() == 1); |
| 54 |
| 55 SkLayerRasterizer::Builder builder; |
| 56 builder.addLayer(paint); |
| 57 } |
| 58 REPORTER_ASSERT(reporter, DummyRasterizer::GetCount() == 0); |
| 59 } |
| 60 |
| 61 class LayerRasterizerTester { |
| 62 public: |
| 63 static int CountLayers(const SkLayerRasterizer& layerRasterizer) { |
| 64 return layerRasterizer.fLayers->count(); |
| 65 } |
| 66 |
| 67 static const SkDeque& GetLayers(const SkLayerRasterizer& layerRasterizer) { |
| 68 return *layerRasterizer.fLayers; |
| 69 } |
| 70 }; |
| 71 |
| 72 // MUST stay in sync with definition of SkLayerRasterizer_Rec in SkLayerRasteriz
er.cpp. |
| 73 struct SkLayerRasterizer_Rec { |
| 74 SkPaint fPaint; |
| 75 SkVector fOffset; |
| 76 }; |
| 77 |
| 78 static bool equals(const SkLayerRasterizer_Rec& rec1, const SkLayerRasterizer_Re
c& rec2) { |
| 79 return rec1.fPaint == rec2.fPaint && rec1.fOffset == rec2.fOffset; |
| 80 } |
| 81 |
| 82 DEF_TEST(LayerRasterizer_copy, reporter) { |
| 83 SkLayerRasterizer::Builder builder; |
| 84 SkPaint paint; |
| 85 // Create a bunch of paints with different flags. |
| 86 for (uint32_t flags = 0x01; flags < SkPaint::kAllFlags; flags <<= 1) { |
| 87 paint.setFlags(flags); |
| 88 builder.addLayer(paint, flags, flags); |
| 89 } |
| 90 |
| 91 // Create a layer rasterizer with all the existing layers. |
| 92 SkAutoTUnref<SkLayerRasterizer> firstCopy(builder.snapshotRasterizer()); |
| 93 |
| 94 // Add one more layer. |
| 95 paint.setFlags(SkPaint::kAllFlags); |
| 96 builder.addLayer(paint); |
| 97 |
| 98 SkAutoTUnref<SkLayerRasterizer> oneLarger(builder.snapshotRasterizer()); |
| 99 SkAutoTUnref<SkLayerRasterizer> detached(builder.detachRasterizer()); |
| 100 |
| 101 // Check the counts for consistency. |
| 102 const int largerCount = LayerRasterizerTester::CountLayers(*oneLarger.get())
; |
| 103 const int smallerCount = LayerRasterizerTester::CountLayers(*firstCopy.get()
); |
| 104 REPORTER_ASSERT(reporter, largerCount == LayerRasterizerTester::CountLayers(
*detached.get())); |
| 105 REPORTER_ASSERT(reporter, smallerCount == largerCount - 1); |
| 106 |
| 107 const SkLayerRasterizer_Rec* recFirstCopy = NULL; |
| 108 const SkLayerRasterizer_Rec* recOneLarger = NULL; |
| 109 const SkLayerRasterizer_Rec* recDetached = NULL; |
| 110 |
| 111 const SkDeque& layersFirstCopy = LayerRasterizerTester::GetLayers(*firstCopy
.get()); |
| 112 const SkDeque& layersOneLarger = LayerRasterizerTester::GetLayers(*oneLarger
.get()); |
| 113 const SkDeque& layersDetached = LayerRasterizerTester::GetLayers(*detached.g
et()); |
| 114 |
| 115 // Ensure that our version of SkLayerRasterizer_Rec is the same as the one i
n |
| 116 // SkLayerRasterizer.cpp - or at least the same size. If the order were swit
ched, we |
| 117 // would fail the test elsewhere. |
| 118 REPORTER_ASSERT(reporter, layersFirstCopy.elemSize() == sizeof(SkLayerRaster
izer_Rec)); |
| 119 REPORTER_ASSERT(reporter, layersOneLarger.elemSize() == sizeof(SkLayerRaster
izer_Rec)); |
| 120 REPORTER_ASSERT(reporter, layersDetached.elemSize() == sizeof(SkLayerRasteri
zer_Rec)); |
| 121 |
| 122 SkDeque::F2BIter iterFirstCopy(layersFirstCopy); |
| 123 SkDeque::F2BIter iterOneLarger(layersOneLarger); |
| 124 SkDeque::F2BIter iterDetached(layersDetached); |
| 125 |
| 126 for (int i = 0; i < largerCount; ++i) { |
| 127 recFirstCopy = static_cast<const SkLayerRasterizer_Rec*>(iterFirstCopy.n
ext()); |
| 128 recOneLarger = static_cast<const SkLayerRasterizer_Rec*>(iterOneLarger.n
ext()); |
| 129 recDetached = static_cast<const SkLayerRasterizer_Rec*>(iterDetached.ne
xt()); |
| 130 |
| 131 REPORTER_ASSERT(reporter, equals(*recOneLarger, *recDetached)); |
| 132 if (smallerCount == i) { |
| 133 REPORTER_ASSERT(reporter, recFirstCopy == NULL); |
| 134 } else { |
| 135 REPORTER_ASSERT(reporter, equals(*recFirstCopy, *recOneLarger)); |
| 136 } |
| 137 } |
| 138 } |
OLD | NEW |