blob: 10d297a8644555cdc1628c3421bb34d96c53d5df [file] [log] [blame]
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkColor.h"
12#include "SkColorFilter.h"
13#include "SkGradientShader.h"
14#include "SkPaint.h"
15#include "SkPictureFlat.h"
16#include "SkShader.h"
17#include "SkXfermode.h"
18#include "Test.h"
19
20static void flattenFlattenableProc(SkOrderedWriteBuffer& buffer,
21 const void* obj) {
22 buffer.writeFlattenable((SkFlattenable*)obj);
23}
24
scroggo@google.com15543602012-08-02 18:49:49 +000025class Controller : public SkChunkFlatController {
26public:
27 Controller() : INHERITED(1024) {
28 this->INHERITED::setNamedFactorySet(SkNEW(SkNamedFactorySet))->unref();
29 }
30private:
31 typedef SkChunkFlatController INHERITED;
32};
33
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000034/**
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 */
41static void testCreate(skiatest::Reporter* reporter, const void* obj,
42 void (*flattenProc)(SkOrderedWriteBuffer&, const void*)) {
scroggo@google.com15543602012-08-02 18:49:49 +000043 Controller controller;
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000044 // No need to delete data because that will be taken care of by the
45 // controller.
scroggo@google.com40fbb182012-08-14 19:30:20 +000046 SkFlatData* data1 = SkFlatData::Create(&controller, obj, 0, flattenProc);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000047 data1->setSentinelInCache();
scroggo@google.com40fbb182012-08-14 19:30:20 +000048 SkFlatData* data2 = SkFlatData::Create(&controller, obj, 1, flattenProc);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000049 data2->setSentinelAsCandidate();
50 REPORTER_ASSERT(reporter, SkFlatData::Compare(data1, data2) == 0);
51}
52
53static void Tests(skiatest::Reporter* reporter) {
54 // Test flattening SkShader
55 SkPoint points[2];
56 points[0].set(0, 0);
57 points[1].set(SkIntToScalar(20), SkIntToScalar(20));
58 SkColor colors[2];
59 colors[0] = SK_ColorRED;
60 colors[1] = SK_ColorBLUE;
61 SkShader* shader = SkGradientShader::CreateLinear(points, colors, NULL,
62 2, SkShader::kRepeat_TileMode);
63 SkAutoUnref aur(shader);
64 testCreate(reporter, shader, flattenFlattenableProc);
65
66 // Test SkBitmap
67 {
68 SkBitmap bm;
69 bm.setConfig(SkBitmap::kARGB_8888_Config, 50, 50);
70 bm.allocPixels();
71 SkCanvas canvas(bm);
72 SkPaint paint;
73 paint.setShader(shader);
74 canvas.drawPaint(paint);
75 testCreate(reporter, &bm, &SkFlattenObjectProc<SkBitmap>);
76 }
77
78 // Test SkColorFilter
79 SkColorFilter* cf = SkColorFilter::CreateLightingFilter(SK_ColorBLUE,
80 SK_ColorRED);
81 SkAutoUnref aurcf(cf);
82 testCreate(reporter, cf, &flattenFlattenableProc);
83
84 // Test SkXfermode
85 SkXfermode* xfer = SkXfermode::Create(SkXfermode::kDstOver_Mode);
86 SkAutoUnref aurxf(xfer);
87 testCreate(reporter, xfer, &flattenFlattenableProc);
88}
89
90#include "TestClassDef.h"
91DEFINE_TESTCLASS("FlatData", FlatDataClass, Tests)