blob: 4b236acd52346c8347d12d8dbdf263c3912241a3 [file] [log] [blame]
commit-bot@chromium.org6573ce72014-04-10 20:42:53 +00001/*
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
14class 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.
20class DummyRasterizer : public SkRasterizer {
21public:
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
reed9fa60da2014-08-21 07:59:51 -070036 SK_DECLARE_NOT_FLATTENABLE_PROCS(DummyRasterizer)
commit-bot@chromium.org6573ce72014-04-10 20:42:53 +000037
38private:
commit-bot@chromium.org6573ce72014-04-10 20:42:53 +000039 static int gCount;
40
41 typedef SkRasterizer INHERITED;
42};
43
44int DummyRasterizer::gCount;
45
46// Check to make sure that the SkPaint in the layer has its destructor called.
47DEF_TEST(LayerRasterizer_destructor, reporter) {
48 {
49 SkPaint paint;
50 paint.setRasterizer(SkNEW(DummyRasterizer))->unref();
51 REPORTER_ASSERT(reporter, DummyRasterizer::GetCount() == 1);
52
53 SkLayerRasterizer::Builder builder;
54 builder.addLayer(paint);
55 }
56 REPORTER_ASSERT(reporter, DummyRasterizer::GetCount() == 0);
57}
58
59class LayerRasterizerTester {
60public:
61 static int CountLayers(const SkLayerRasterizer& layerRasterizer) {
62 return layerRasterizer.fLayers->count();
63 }
64
65 static const SkDeque& GetLayers(const SkLayerRasterizer& layerRasterizer) {
66 return *layerRasterizer.fLayers;
67 }
68};
69
70// MUST stay in sync with definition of SkLayerRasterizer_Rec in SkLayerRasterizer.cpp.
71struct SkLayerRasterizer_Rec {
72 SkPaint fPaint;
73 SkVector fOffset;
74};
75
76static bool equals(const SkLayerRasterizer_Rec& rec1, const SkLayerRasterizer_Rec& rec2) {
77 return rec1.fPaint == rec2.fPaint && rec1.fOffset == rec2.fOffset;
78}
79
80DEF_TEST(LayerRasterizer_copy, reporter) {
81 SkLayerRasterizer::Builder builder;
scroggo65044bf2014-06-03 13:12:51 -070082 REPORTER_ASSERT(reporter, NULL == builder.snapshotRasterizer());
commit-bot@chromium.org6573ce72014-04-10 20:42:53 +000083 SkPaint paint;
84 // Create a bunch of paints with different flags.
85 for (uint32_t flags = 0x01; flags < SkPaint::kAllFlags; flags <<= 1) {
86 paint.setFlags(flags);
commit-bot@chromium.org55c9b4e2014-04-10 21:09:33 +000087 builder.addLayer(paint, static_cast<SkScalar>(flags), static_cast<SkScalar>(flags));
commit-bot@chromium.org6573ce72014-04-10 20:42:53 +000088 }
89
90 // Create a layer rasterizer with all the existing layers.
91 SkAutoTUnref<SkLayerRasterizer> firstCopy(builder.snapshotRasterizer());
92
93 // Add one more layer.
94 paint.setFlags(SkPaint::kAllFlags);
95 builder.addLayer(paint);
96
97 SkAutoTUnref<SkLayerRasterizer> oneLarger(builder.snapshotRasterizer());
98 SkAutoTUnref<SkLayerRasterizer> detached(builder.detachRasterizer());
99
100 // Check the counts for consistency.
101 const int largerCount = LayerRasterizerTester::CountLayers(*oneLarger.get());
102 const int smallerCount = LayerRasterizerTester::CountLayers(*firstCopy.get());
103 REPORTER_ASSERT(reporter, largerCount == LayerRasterizerTester::CountLayers(*detached.get()));
104 REPORTER_ASSERT(reporter, smallerCount == largerCount - 1);
105
106 const SkLayerRasterizer_Rec* recFirstCopy = NULL;
107 const SkLayerRasterizer_Rec* recOneLarger = NULL;
108 const SkLayerRasterizer_Rec* recDetached = NULL;
109
110 const SkDeque& layersFirstCopy = LayerRasterizerTester::GetLayers(*firstCopy.get());
111 const SkDeque& layersOneLarger = LayerRasterizerTester::GetLayers(*oneLarger.get());
112 const SkDeque& layersDetached = LayerRasterizerTester::GetLayers(*detached.get());
113
114 // Ensure that our version of SkLayerRasterizer_Rec is the same as the one in
115 // SkLayerRasterizer.cpp - or at least the same size. If the order were switched, we
116 // would fail the test elsewhere.
117 REPORTER_ASSERT(reporter, layersFirstCopy.elemSize() == sizeof(SkLayerRasterizer_Rec));
118 REPORTER_ASSERT(reporter, layersOneLarger.elemSize() == sizeof(SkLayerRasterizer_Rec));
119 REPORTER_ASSERT(reporter, layersDetached.elemSize() == sizeof(SkLayerRasterizer_Rec));
120
121 SkDeque::F2BIter iterFirstCopy(layersFirstCopy);
122 SkDeque::F2BIter iterOneLarger(layersOneLarger);
123 SkDeque::F2BIter iterDetached(layersDetached);
124
125 for (int i = 0; i < largerCount; ++i) {
126 recFirstCopy = static_cast<const SkLayerRasterizer_Rec*>(iterFirstCopy.next());
127 recOneLarger = static_cast<const SkLayerRasterizer_Rec*>(iterOneLarger.next());
128 recDetached = static_cast<const SkLayerRasterizer_Rec*>(iterDetached.next());
129
130 REPORTER_ASSERT(reporter, equals(*recOneLarger, *recDetached));
131 if (smallerCount == i) {
132 REPORTER_ASSERT(reporter, recFirstCopy == NULL);
133 } else {
134 REPORTER_ASSERT(reporter, equals(*recFirstCopy, *recOneLarger));
135 }
136 }
137}
scroggo65044bf2014-06-03 13:12:51 -0700138
139DEF_TEST(LayerRasterizer_detachEmpty, reporter) {
140 SkLayerRasterizer::Builder builder;
141 REPORTER_ASSERT(reporter, NULL == builder.detachRasterizer());
142}