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