blob: 755d8cd9441f3bcd6907dc0345a73577b877441f [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
25/**
26 * Verify that two SkFlatData objects that created from the same object are
27 * identical when using an SkNamedFactorySet.
28 * @param reporter Object to report failures.
29 * @param obj Flattenable object to be flattened.
30 * @param flattenProc Function that flattens objects with the same type as obj.
31 */
32static void testCreate(skiatest::Reporter* reporter, const void* obj,
33 void (*flattenProc)(SkOrderedWriteBuffer&, const void*)) {
34 SkChunkFlatController controller(1024);
35 SkNamedFactorySet factorySet;
36 // No need to delete data because that will be taken care of by the
37 // controller.
38 SkFlatData* data1 = SkFlatData::Create(&controller, obj, 0, flattenProc,
39 NULL, NULL, 0, &factorySet);
40 data1->setSentinelInCache();
41 SkFlatData* data2 = SkFlatData::Create(&controller, obj, 0, flattenProc,
42 NULL, NULL, 0, &factorySet);
43 data2->setSentinelAsCandidate();
44 REPORTER_ASSERT(reporter, SkFlatData::Compare(data1, data2) == 0);
45}
46
47static void Tests(skiatest::Reporter* reporter) {
48 // Test flattening SkShader
49 SkPoint points[2];
50 points[0].set(0, 0);
51 points[1].set(SkIntToScalar(20), SkIntToScalar(20));
52 SkColor colors[2];
53 colors[0] = SK_ColorRED;
54 colors[1] = SK_ColorBLUE;
55 SkShader* shader = SkGradientShader::CreateLinear(points, colors, NULL,
56 2, SkShader::kRepeat_TileMode);
57 SkAutoUnref aur(shader);
58 testCreate(reporter, shader, flattenFlattenableProc);
59
60 // Test SkBitmap
61 {
62 SkBitmap bm;
63 bm.setConfig(SkBitmap::kARGB_8888_Config, 50, 50);
64 bm.allocPixels();
65 SkCanvas canvas(bm);
66 SkPaint paint;
67 paint.setShader(shader);
68 canvas.drawPaint(paint);
69 testCreate(reporter, &bm, &SkFlattenObjectProc<SkBitmap>);
70 }
71
72 // Test SkColorFilter
73 SkColorFilter* cf = SkColorFilter::CreateLightingFilter(SK_ColorBLUE,
74 SK_ColorRED);
75 SkAutoUnref aurcf(cf);
76 testCreate(reporter, cf, &flattenFlattenableProc);
77
78 // Test SkXfermode
79 SkXfermode* xfer = SkXfermode::Create(SkXfermode::kDstOver_Mode);
80 SkAutoUnref aurxf(xfer);
81 testCreate(reporter, xfer, &flattenFlattenableProc);
82}
83
84#include "TestClassDef.h"
85DEFINE_TESTCLASS("FlatData", FlatDataClass, Tests)