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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Resources.h"
#include "SkCanvas.h"
#include "SkPipe.h"
#include "SkPaint.h"
#include "SkStream.h"
#include "SkSurface.h"
#include "Test.h"
#include "SkNullCanvas.h"
#include "SkAutoPixmapStorage.h"
#include "SkPictureRecorder.h"
static void drain(SkPipeDeserializer* deserial, SkDynamicMemoryWStream* stream) {
std::unique_ptr<SkCanvas> canvas = SkMakeNullCanvas();
sk_sp<SkData> data = stream->detachAsData();
deserial->playback(data->data(), data->size(), canvas.get());
}
static bool deep_equal(SkImage* a, SkImage* b) {
if (a->width() != b->width() || a->height() != b->height()) {
return false;
}
const SkImageInfo info = SkImageInfo::MakeN32Premul(a->width(), a->height());
SkAutoPixmapStorage pmapA, pmapB;
pmapA.alloc(info);
pmapB.alloc(info);
if (!a->readPixels(pmapA, 0, 0) || !b->readPixels(pmapB, 0, 0)) {
return false;
}
for (int y = 0; y < info.height(); ++y) {
if (memcmp(pmapA.addr32(0, y), pmapB.addr32(0, y), info.width() * sizeof(SkPMColor))) {
return false;
}
}
return true;
}
DEF_TEST(Pipe_image_draw_first, reporter) {
sk_sp<SkImage> img = GetResourceAsImage("mandrill_128.png");
SkASSERT(img.get());
SkPipeSerializer serializer;
SkPipeDeserializer deserializer;
SkDynamicMemoryWStream stream;
SkCanvas* wc = serializer.beginWrite(SkRect::MakeWH(100, 100), &stream);
wc->drawImage(img, 0, 0, nullptr);
serializer.endWrite();
size_t offset0 = stream.bytesWritten();
REPORTER_ASSERT(reporter, offset0 > 100); // the raw image must be sorta big
drain(&deserializer, &stream);
// try drawing the same image again -- it should be much smaller
wc = serializer.beginWrite(SkRect::MakeWH(100, 100), &stream);
wc->drawImage(img, 0, 0, nullptr);
size_t offset1 = stream.bytesWritten();
serializer.endWrite();
REPORTER_ASSERT(reporter, offset1 <= 32);
drain(&deserializer, &stream);
// try serializing the same image directly, again it should be small
sk_sp<SkData> data = serializer.writeImage(img.get());
size_t offset2 = data->size();
REPORTER_ASSERT(reporter, offset2 <= 32);
auto img1 = deserializer.readImage(data.get());
REPORTER_ASSERT(reporter, deep_equal(img.get(), img1.get()));
// try serializing the same image directly (again), check that it is the same!
data = serializer.writeImage(img.get());
size_t offset3 = data->size();
REPORTER_ASSERT(reporter, offset3 <= 32);
auto img2 = deserializer.readImage(data.get());
REPORTER_ASSERT(reporter, img1.get() == img2.get());
}
DEF_TEST(Pipe_image_draw_second, reporter) {
sk_sp<SkImage> img = GetResourceAsImage("mandrill_128.png");
SkASSERT(img.get());
SkPipeSerializer serializer;
SkPipeDeserializer deserializer;
SkDynamicMemoryWStream stream;
sk_sp<SkData> data = serializer.writeImage(img.get());
size_t offset0 = data->size();
REPORTER_ASSERT(reporter, offset0 > 100); // the raw image must be sorta big
auto img1 = deserializer.readImage(data.get());
// The 2nd image should be nice and small
data = serializer.writeImage(img.get());
size_t offset1 = data->size();
REPORTER_ASSERT(reporter, offset1 <= 16);
auto img2 = deserializer.readImage(data.get());
REPORTER_ASSERT(reporter, img1.get() == img2.get());
// Now try drawing the image, it should also be small
SkCanvas* wc = serializer.beginWrite(SkRect::MakeWH(100, 100), &stream);
wc->drawImage(img, 0, 0, nullptr);
serializer.endWrite();
size_t offset2 = stream.bytesWritten();
REPORTER_ASSERT(reporter, offset2 <= 16);
}
DEF_TEST(Pipe_picture_draw_first, reporter) {
sk_sp<SkPicture> picture = []() {
SkPictureRecorder rec;
SkCanvas* c = rec.beginRecording(SkRect::MakeWH(100, 100));
for (int i = 0; i < 100; ++i) {
c->drawColor(i);
}
return rec.finishRecordingAsPicture();
}();
SkPipeSerializer serializer;
SkPipeDeserializer deserializer;
SkDynamicMemoryWStream stream;
SkCanvas* wc = serializer.beginWrite(SkRect::MakeWH(100, 100), &stream);
wc->drawPicture(picture);
serializer.endWrite();
size_t offset0 = stream.bytesWritten();
REPORTER_ASSERT(reporter, offset0 > 100); // the raw picture must be sorta big
drain(&deserializer, &stream);
// try drawing the same picture again -- it should be much smaller
wc = serializer.beginWrite(SkRect::MakeWH(100, 100), &stream);
wc->drawPicture(picture);
size_t offset1 = stream.bytesWritten();
serializer.endWrite();
REPORTER_ASSERT(reporter, offset1 <= 16);
drain(&deserializer, &stream);
// try writing the picture directly, it should also be small
sk_sp<SkData> data = serializer.writePicture(picture.get());
size_t offset2 = data->size();
REPORTER_ASSERT(reporter, offset2 <= 16);
auto pic1 = deserializer.readPicture(data.get());
// try writing the picture directly, it should also be small
data = serializer.writePicture(picture.get());
size_t offset3 = data->size();
REPORTER_ASSERT(reporter, offset3 == offset2);
auto pic2 = deserializer.readPicture(data.get());
REPORTER_ASSERT(reporter, pic1.get() == pic2.get());
}
DEF_TEST(Pipe_picture_draw_second, reporter) {
sk_sp<SkPicture> picture = []() {
SkPictureRecorder rec;
SkCanvas* c = rec.beginRecording(SkRect::MakeWH(100, 100));
for (int i = 0; i < 100; ++i) {
c->drawColor(i);
}
return rec.finishRecordingAsPicture();
}();
SkPipeSerializer serializer;
SkPipeDeserializer deserializer;
SkDynamicMemoryWStream stream;
sk_sp<SkData> data = serializer.writePicture(picture.get());
size_t offset0 = data->size();
REPORTER_ASSERT(reporter, offset0 > 100); // the raw picture must be sorta big
auto pic1 = deserializer.readPicture(data.get());
// The 2nd picture should be nice and small
data = serializer.writePicture(picture.get());
size_t offset1 = data->size();
REPORTER_ASSERT(reporter, offset1 <= 16);
auto pic2 = deserializer.readPicture(data.get());
SkASSERT(pic1.get() == pic2.get());
// Now try drawing the image, it should also be small
SkCanvas* wc = serializer.beginWrite(SkRect::MakeWH(100, 100), &stream);
wc->drawPicture(picture);
serializer.endWrite();
size_t offset2 = stream.bytesWritten();
REPORTER_ASSERT(reporter, offset2 <= 16);
}
|