blob: 013d58657bce3a69d84dfec530135f7449ade97b [file] [log] [blame]
vandebo@chromium.orgf66025d2010-10-01 23:26:55 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 The Android Open Source Project
vandebo@chromium.orgf66025d2010-10-01 23:26:55 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
vandebo@chromium.orgf66025d2010-10-01 23:26:55 +00006 */
7
edisonn@google.comd9dfa182013-04-24 13:01:01 +00008#include "SkBitmap.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00009#include "SkCanvas.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000010#include "SkData.h"
halcanary3d32d502015-03-01 06:55:20 -080011#include "SkDocument.h"
vandebo@chromium.org421d6442011-07-20 17:39:01 +000012#include "SkFlate.h"
edisonn@google.comd9dfa182013-04-24 13:01:01 +000013#include "SkImageEncoder.h"
14#include "SkMatrix.h"
halcanarya1f1ee92015-02-20 06:17:26 -080015#include "SkPDFCanon.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000016#include "SkPDFDevice.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000017#include "SkPDFStream.h"
18#include "SkPDFTypes.h"
senorblancob0e89dc2014-10-20 14:03:12 -070019#include "SkReadBuffer.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000020#include "SkScalar.h"
21#include "SkStream.h"
vandebo@chromium.org421d6442011-07-20 17:39:01 +000022#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000023#include "Test.h"
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000024
commit-bot@chromium.org608ea652013-10-03 19:29:21 +000025#define DUMMY_TEXT "DCT compessed stream."
26
halcanaryf7a169e2015-05-01 07:35:45 -070027namespace {
28struct Catalog {
29 SkPDFSubstituteMap substitutes;
30 SkPDFObjNumMap numbers;
31};
32} // namespace
reed@google.com8a85d0c2011-06-24 19:12:12 +000033
halcanaryf7a169e2015-05-01 07:35:45 -070034template <typename T>
35static SkString emit_to_string(T& obj, Catalog* catPtr = NULL) {
36 Catalog catalog;
halcanary6a144342015-01-23 11:45:10 -080037 SkDynamicMemoryWStream buffer;
halcanaryf7a169e2015-05-01 07:35:45 -070038 if (!catPtr) {
39 catPtr = &catalog;
halcanary130444f2015-04-25 06:45:07 -070040 }
halcanaryf7a169e2015-05-01 07:35:45 -070041 obj.emitObject(&buffer, catPtr->numbers, catPtr->substitutes);
42 SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream());
43 SkString tmp(asset->getLength());
44 asset->read(tmp.writable_str(), asset->getLength());
45 return tmp;
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000046}
47
halcanaryf7a169e2015-05-01 07:35:45 -070048static bool eq(const SkString& str, const char* strPtr, size_t len) {
49 return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len);
vandebo@chromium.org421d6442011-07-20 17:39:01 +000050}
51
halcanaryf7a169e2015-05-01 07:35:45 -070052#define ASSERT_EQL(REPORTER, SKSTRING, STRING, LEN) \
53 do { \
54 const char* strptr = STRING; \
55 const SkString& sks = SKSTRING; \
56 if (!eq(sks, strptr, LEN)) { \
57 REPORT_FAILURE( \
58 REPORTER, \
59 "", \
60 SkStringPrintf("'%s' != '%s'", strptr, sks.c_str())); \
61 } \
62 } while (false)
63
64#define ASSERT_EQ(REPORTER, SKSTRING, STRING) \
65 do { \
66 const char* str = STRING; \
67 ASSERT_EQL(REPORTER, SKSTRING, str, strlen(str)); \
68 } while (false)
69
70#define ASSERT_EMIT_EQ(REPORTER, OBJECT, STRING) \
71 do { \
72 SkString result = emit_to_string(OBJECT); \
73 ASSERT_EQ(REPORTER, result, STRING); \
74 } while (false)
75
76
77
vandebo@chromium.org421d6442011-07-20 17:39:01 +000078static void TestPDFStream(skiatest::Reporter* reporter) {
79 char streamBytes[] = "Test\nFoo\tBar";
scroggoa1193e42015-01-21 12:09:53 -080080 SkAutoTDelete<SkMemoryStream> streamData(new SkMemoryStream(
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +000081 streamBytes, strlen(streamBytes), true));
82 SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData.get()));
halcanaryf7a169e2015-05-01 07:35:45 -070083 ASSERT_EMIT_EQ(reporter,
84 *stream,
85 "<</Length 12>> stream\nTest\nFoo\tBar\nendstream");
86 stream->insertInt("Attribute", 42);
87 ASSERT_EMIT_EQ(reporter,
88 *stream,
89 "<</Length 12\n/Attribute 42>> stream\n"
90 "Test\nFoo\tBar\nendstream");
vandebo@chromium.org421d6442011-07-20 17:39:01 +000091
halcanary91d1d622015-02-17 14:43:06 -080092 {
vandebo@chromium.org421d6442011-07-20 17:39:01 +000093 char streamBytes2[] = "This is a longer string, so that compression "
94 "can do something with it. With shorter strings, "
95 "the short circuit logic cuts in and we end up "
96 "with an uncompressed string.";
97 SkAutoDataUnref streamData2(SkData::NewWithCopy(streamBytes2,
98 strlen(streamBytes2)));
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +000099 SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData2.get()));
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000100
101 SkDynamicMemoryWStream compressedByteStream;
102 SkFlate::Deflate(streamData2.get(), &compressedByteStream);
103 SkAutoDataUnref compressedData(compressedByteStream.copyToData());
104
mtkleincabc08c2015-02-19 08:29:24 -0800105 SkDynamicMemoryWStream expected;
halcanary37c46ca2015-03-31 12:30:20 -0700106 expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n");
mtkleincabc08c2015-02-19 08:29:24 -0800107 expected.write(compressedData->data(), compressedData->size());
108 expected.writeText("\nendstream");
109 SkAutoDataUnref expectedResultData2(expected.copyToData());
halcanaryf7a169e2015-05-01 07:35:45 -0700110 SkString result = emit_to_string(*stream);
111 ASSERT_EQL(reporter,
112 result,
113 (const char*)expectedResultData2->data(),
114 expectedResultData2->size());
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000115 }
116}
117
halcanaryf7a169e2015-05-01 07:35:45 -0700118static void TestObjectNumberMap(skiatest::Reporter* reporter) {
119 SkPDFObjNumMap objNumMap;
120 SkAutoTUnref<SkPDFArray> a1(new SkPDFArray);
121 SkAutoTUnref<SkPDFArray> a2(new SkPDFArray);
122 SkAutoTUnref<SkPDFArray> a3(new SkPDFArray);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000123
halcanaryf7a169e2015-05-01 07:35:45 -0700124 objNumMap.addObject(a1.get());
125 objNumMap.addObject(a2.get());
126 objNumMap.addObject(a3.get());
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000127
halcanaryf7a169e2015-05-01 07:35:45 -0700128 // The objects should be numbered in the order they are added,
129 // starting with 1.
130 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
131 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a2.get()) == 2);
132 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a3.get()) == 3);
133 // Assert that repeated calls to get the object number return
134 // consistent result.
135 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000136}
137
138static void TestObjectRef(skiatest::Reporter* reporter) {
halcanaryf7a169e2015-05-01 07:35:45 -0700139 SkAutoTUnref<SkPDFArray> a1(new SkPDFArray);
140 SkAutoTUnref<SkPDFArray> a2(new SkPDFArray);
141 a2->appendObjRef(SkRef(a1.get()));
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000142
halcanaryf7a169e2015-05-01 07:35:45 -0700143 Catalog catalog;
144 catalog.numbers.addObject(a1.get());
145 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(a1.get()) == 1);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000146
halcanaryf7a169e2015-05-01 07:35:45 -0700147 SkString result = emit_to_string(*a2, &catalog);
148 // If appendObjRef misbehaves, then the result would
149 // be [[]], not [1 0 R].
150 ASSERT_EQ(reporter, result, "[1 0 R]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000151}
152
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000153static void TestSubstitute(skiatest::Reporter* reporter) {
halcanaryf3d53a02015-03-24 15:29:39 -0700154 SkAutoTUnref<SkPDFDict> proxy(new SkPDFDict());
155 SkAutoTUnref<SkPDFDict> stub(new SkPDFDict());
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000156
halcanaryf7a169e2015-05-01 07:35:45 -0700157 proxy->insertInt("Value", 33);
158 stub->insertInt("Value", 44);
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000159
halcanary37c46ca2015-03-31 12:30:20 -0700160 SkPDFSubstituteMap substituteMap;
161 substituteMap.setSubstitute(proxy.get(), stub.get());
162 SkPDFObjNumMap catalog;
halcanaryf41061c2015-03-25 11:29:18 -0700163 catalog.addObject(proxy.get());
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000164
halcanary37c46ca2015-03-31 12:30:20 -0700165 REPORTER_ASSERT(reporter, stub.get() == substituteMap.getSubstitute(proxy));
166 REPORTER_ASSERT(reporter, proxy.get() != substituteMap.getSubstitute(stub));
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000167}
168
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000169// This test used to assert without the fix submitted for
170// http://code.google.com/p/skia/issues/detail?id=1083.
171// SKP files might have invalid glyph ids. This test ensures they are ignored,
172// and there is no assert on input data in Debug mode.
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000173static void test_issue1083() {
halcanary3d32d502015-03-01 06:55:20 -0800174 SkDynamicMemoryWStream outStream;
175 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&outStream));
176 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000177 SkPaint paint;
178 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
179
180 uint16_t glyphID = 65000;
halcanary3d32d502015-03-01 06:55:20 -0800181 canvas->drawText(&glyphID, 2, 0, 0, paint);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000182
halcanary3d32d502015-03-01 06:55:20 -0800183 doc->close();
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000184}
185
halcanaryf7a169e2015-05-01 07:35:45 -0700186static void TestPDFUnion(skiatest::Reporter* reporter) {
187 SkPDFUnion boolTrue = SkPDFUnion::Bool(true);
188 ASSERT_EMIT_EQ(reporter, boolTrue, "true");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000189
halcanaryf7a169e2015-05-01 07:35:45 -0700190 SkPDFUnion boolFalse = SkPDFUnion::Bool(false);
191 ASSERT_EMIT_EQ(reporter, boolFalse, "false");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000192
halcanaryf7a169e2015-05-01 07:35:45 -0700193 SkPDFUnion int42 = SkPDFUnion::Int(42);
194 ASSERT_EMIT_EQ(reporter, int42, "42");
195
196 SkPDFUnion realHalf = SkPDFUnion::Scalar(SK_ScalarHalf);
197 ASSERT_EMIT_EQ(reporter, realHalf, "0.5");
198
199 SkPDFUnion bigScalar = SkPDFUnion::Scalar(110999.75f);
vandebo@chromium.org6cc26da2011-05-18 17:08:05 +0000200#if !defined(SK_ALLOW_LARGE_PDF_SCALARS)
halcanaryf7a169e2015-05-01 07:35:45 -0700201 ASSERT_EMIT_EQ(reporter, bigScalar, "111000");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000202#else
halcanaryf7a169e2015-05-01 07:35:45 -0700203 ASSERT_EMIT_EQ(reporter, bigScalar, "110999.75");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000204
halcanaryf7a169e2015-05-01 07:35:45 -0700205 SkPDFUnion biggerScalar = SkPDFUnion::Scalar(50000000.1);
206 ASSERT_EMIT_EQ(reporter, biggerScalar, "50000000");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000207
halcanaryf7a169e2015-05-01 07:35:45 -0700208 SkPDFUnion smallestScalar = SkPDFUnion::Scalar(1.0 / 65536);
209 ASSERT_EMIT_EQ(reporter, smallestScalar, "0.00001526");
vandebo@chromium.org094316b2011-03-04 03:15:13 +0000210#endif
211
halcanaryf7a169e2015-05-01 07:35:45 -0700212 SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo");
213 ASSERT_EMIT_EQ(reporter, stringSimple, "(test \\) string \\( foo)");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000214
halcanaryf7a169e2015-05-01 07:35:45 -0700215 SkString stringComplexInput("\ttest ) string ( foo");
216 SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput);
217 ASSERT_EMIT_EQ(reporter,
218 stringComplex,
219 "<0974657374202920737472696E67202820666F6F>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000220
halcanaryf7a169e2015-05-01 07:35:45 -0700221 SkString nameInput("Test name\twith#tab");
222 SkPDFUnion name = SkPDFUnion::Name(nameInput);
223 ASSERT_EMIT_EQ(reporter, name, "/Test#20name#09with#23tab");
224
225 SkString nameInput2("A#/%()<>[]{}B");
226 SkPDFUnion name2 = SkPDFUnion::Name(nameInput2);
227 ASSERT_EMIT_EQ(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB");
228
229 SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII");
230 ASSERT_EMIT_EQ(reporter, name3, "/SimpleNameWithOnlyPrintableASCII");
vandebo@chromium.org251a7662012-09-21 17:50:50 +0000231
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000232 // Test that we correctly handle characters with the high-bit set.
halcanaryf7a169e2015-05-01 07:35:45 -0700233 SkString highBitString("\xDE\xAD" "be\xEF");
234 SkPDFUnion highBitName = SkPDFUnion::Name(highBitString);
235 ASSERT_EMIT_EQ(reporter, highBitName, "/#DE#ADbe#EF");
236}
vandebo@chromium.orgc0376fe2012-03-05 18:44:33 +0000237
halcanaryf7a169e2015-05-01 07:35:45 -0700238static void TestPDFArray(skiatest::Reporter* reporter) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000239 SkAutoTUnref<SkPDFArray> array(new SkPDFArray);
halcanaryf7a169e2015-05-01 07:35:45 -0700240 ASSERT_EMIT_EQ(reporter, *array, "[]");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000241
halcanaryf7a169e2015-05-01 07:35:45 -0700242 array->appendInt(42);
243 ASSERT_EMIT_EQ(reporter, *array, "[42]");
244
245 array->appendScalar(SK_ScalarHalf);
246 ASSERT_EMIT_EQ(reporter, *array, "[42 0.5]");
247
248 array->appendInt(0);
249 ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0]");
250
251 array->appendBool(true);
252 ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0 true]");
253
254 array->appendName("ThisName");
255 ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0 true /ThisName]");
256
257 array->appendName(SkString("AnotherName"));
258 ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0 true /ThisName /AnotherName]");
259
260 array->appendString("This String");
261 ASSERT_EMIT_EQ(reporter, *array,
262 "[42 0.5 0 true /ThisName /AnotherName (This String)]");
263
264 array->appendString(SkString("Another String"));
265 ASSERT_EMIT_EQ(reporter, *array,
266 "[42 0.5 0 true /ThisName /AnotherName (This String) "
267 "(Another String)]");
268
269 SkAutoTUnref<SkPDFArray> innerArray(new SkPDFArray);
270 innerArray->appendInt(-1);
271 array->appendObject(innerArray.detach());
272 ASSERT_EMIT_EQ(reporter, *array,
273 "[42 0.5 0 true /ThisName /AnotherName (This String) "
274 "(Another String) [-1]]");
275
276 SkAutoTUnref<SkPDFArray> referencedArray(new SkPDFArray);
277 Catalog catalog;
278 catalog.numbers.addObject(referencedArray.get());
279 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(
280 referencedArray.get()) == 1);
281 array->appendObjRef(referencedArray.detach());
282
283 SkString result = emit_to_string(*array, &catalog);
284 ASSERT_EQ(reporter, result,
285 "[42 0.5 0 true /ThisName /AnotherName (This String) "
286 "(Another String) [-1] 1 0 R]");
287}
288
289static void TestPDFDict(skiatest::Reporter* reporter) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +0000290 SkAutoTUnref<SkPDFDict> dict(new SkPDFDict);
halcanaryf7a169e2015-05-01 07:35:45 -0700291 ASSERT_EMIT_EQ(reporter, *dict, "<<>>");
292
293 dict->insertInt("n1", SkToSizeT(42));
294 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42>>");
295
296 dict.reset(new SkPDFDict);
297 ASSERT_EMIT_EQ(reporter, *dict, "<<>>");
298
299 dict->insertInt("n1", 42);
300 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42>>");
301
302 dict->insertScalar("n2", SK_ScalarHalf);
303
halcanary130444f2015-04-25 06:45:07 -0700304 SkString n3("n3");
halcanaryf7a169e2015-05-01 07:35:45 -0700305 SkAutoTUnref<SkPDFArray> innerArray(new SkPDFArray);
306 innerArray->appendInt(-100);
307 dict->insertObject(n3, innerArray.detach());
308 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42\n/n2 0.5\n/n3 [-100]>>");
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000309
halcanaryf7a169e2015-05-01 07:35:45 -0700310 dict.reset(new SkPDFDict);
311 ASSERT_EMIT_EQ(reporter, *dict, "<<>>");
312
313 dict->insertInt("n1", 24);
314 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24>>");
315
316 dict->insertInt("n2", SkToSizeT(99));
317 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99>>");
318
319 dict->insertScalar("n3", SK_ScalarHalf);
320 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5>>");
321
322 dict->insertName("n4", "AName");
323 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName>>");
324
325 dict->insertName("n5", SkString("AnotherName"));
326 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName\n"
327 "/n5 /AnotherName>>");
328
329 dict->insertString("n6", "A String");
330 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName\n"
331 "/n5 /AnotherName\n/n6 (A String)>>");
332
333 dict->insertString("n7", SkString("Another String"));
334 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName\n"
335 "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>");
336
337 dict.reset(new SkPDFDict("DType"));
338 ASSERT_EMIT_EQ(reporter, *dict, "<</Type /DType>>");
339
340 SkAutoTUnref<SkPDFArray> referencedArray(new SkPDFArray);
341 Catalog catalog;
342 catalog.numbers.addObject(referencedArray.get());
343 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(
344 referencedArray.get()) == 1);
345 dict->insertObjRef("n1", referencedArray.detach());
346 SkString result = emit_to_string(*dict, &catalog);
347 ASSERT_EQ(reporter, result, "<</Type /DType\n/n1 1 0 R>>");
348}
349
350DEF_TEST(PDFPrimitives, reporter) {
351 TestPDFUnion(reporter);
352 TestPDFArray(reporter);
353 TestPDFDict(reporter);
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000354 TestPDFStream(reporter);
halcanaryf7a169e2015-05-01 07:35:45 -0700355 TestObjectNumberMap(reporter);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000356 TestObjectRef(reporter);
vandebo@chromium.org2ef12d42011-07-06 23:31:24 +0000357 TestSubstitute(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000358 test_issue1083();
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +0000359}
senorblancob0e89dc2014-10-20 14:03:12 -0700360
361namespace {
362
363class DummyImageFilter : public SkImageFilter {
364public:
365 DummyImageFilter(bool visited = false) : SkImageFilter(0, NULL), fVisited(visited) {}
mtklein36352bf2015-03-25 18:17:31 -0700366 ~DummyImageFilter() override {}
senorblancob0e89dc2014-10-20 14:03:12 -0700367 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
mtklein36352bf2015-03-25 18:17:31 -0700368 SkBitmap* result, SkIPoint* offset) const override {
senorblancob0e89dc2014-10-20 14:03:12 -0700369 fVisited = true;
370 offset->fX = offset->fY = 0;
371 *result = src;
372 return true;
373 }
robertphillipsf3f5bad2014-12-19 13:49:15 -0800374 SK_TO_STRING_OVERRIDE()
senorblancob0e89dc2014-10-20 14:03:12 -0700375 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DummyImageFilter)
senorblancob0e89dc2014-10-20 14:03:12 -0700376 bool visited() const { return fVisited; }
377
378private:
379 mutable bool fVisited;
380};
381
382SkFlattenable* DummyImageFilter::CreateProc(SkReadBuffer& buffer) {
383 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
384 bool visited = buffer.readBool();
385 return SkNEW_ARGS(DummyImageFilter, (visited));
386}
387
robertphillipsf3f5bad2014-12-19 13:49:15 -0800388#ifndef SK_IGNORE_TO_STRING
389void DummyImageFilter::toString(SkString* str) const {
390 str->appendf("DummyImageFilter: (");
391 str->append(")");
392}
393#endif
394
senorblancob0e89dc2014-10-20 14:03:12 -0700395};
396
397// Check that PDF rendering of image filters successfully falls back to
398// CPU rasterization.
399DEF_TEST(PDFImageFilter, reporter) {
halcanary3d32d502015-03-01 06:55:20 -0800400 SkDynamicMemoryWStream stream;
401 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
402 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
403
senorblancob0e89dc2014-10-20 14:03:12 -0700404 SkAutoTUnref<DummyImageFilter> filter(new DummyImageFilter());
405
406 // Filter just created; should be unvisited.
407 REPORTER_ASSERT(reporter, !filter->visited());
408 SkPaint paint;
409 paint.setImageFilter(filter.get());
halcanary3d32d502015-03-01 06:55:20 -0800410 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
411 doc->close();
senorblancob0e89dc2014-10-20 14:03:12 -0700412
413 // Filter was used in rendering; should be visited.
414 REPORTER_ASSERT(reporter, filter->visited());
415}