1 /*
2 * Copyright 2012 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 "SkBitmap.h"
9 #include "SkCanvas.h"
10 #include "SkColor.h"
11 #include "SkColorFilter.h"
12 #include "SkGradientShader.h"
13 #include "SkPaint.h"
14 #include "SkPictureFlat.h"
15 #include "SkShader.h"
16 #include "SkXfermode.h"
17 #include "Test.h"
18
19 struct SkFlattenableTraits {
FlattenSkFlattenableTraits20 static void Flatten(SkWriteBuffer& buffer, const SkFlattenable& flattenable) {
21 buffer.writeFlattenable(&flattenable);
22 }
23 };
24
25 class Controller : public SkChunkFlatController {
26 public:
Controller()27 Controller() : INHERITED(1024) {
28 this->INHERITED::setNamedFactorySet(SkNEW(SkNamedFactorySet))->unref();
29 }
30 private:
31 typedef SkChunkFlatController INHERITED;
32 };
33
34 /**
35 * Verify that two SkFlatData objects that created from the same object are
36 * identical when using an SkNamedFactorySet.
37 * @param reporter Object to report failures.
38 * @param obj Flattenable object to be flattened.
39 * @param flattenProc Function that flattens objects with the same type as obj.
40 */
41 template <typename Traits, typename T>
testCreate(skiatest::Reporter * reporter,const T & obj)42 static void testCreate(skiatest::Reporter* reporter, const T& obj) {
43 Controller controller;
44 // No need to delete data because that will be taken care of by the controller.
45 SkFlatData* data1 = SkFlatData::Create<Traits>(&controller, obj, 0);
46 SkFlatData* data2 = SkFlatData::Create<Traits>(&controller, obj, 1);
47 REPORTER_ASSERT(reporter, *data1 == *data2);
48 }
49
DEF_TEST(FlatData,reporter)50 DEF_TEST(FlatData, reporter) {
51 // Test flattening SkShader
52 SkPoint points[2];
53 points[0].set(0, 0);
54 points[1].set(SkIntToScalar(20), SkIntToScalar(20));
55 SkColor colors[2];
56 colors[0] = SK_ColorRED;
57 colors[1] = SK_ColorBLUE;
58
59 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points, colors, NULL, 2,
60 SkShader::kRepeat_TileMode));
61 testCreate<SkFlattenableTraits>(reporter, *shader);
62
63 // Test SkColorFilter
64 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateLightingFilter(SK_ColorBLUE, SK_ColorRED));
65 testCreate<SkFlattenableTraits>(reporter, *cf);
66
67 // Test SkXfermode
68 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kDstOver_Mode));
69 testCreate<SkFlattenableTraits>(reporter, *xfer);
70 }
71