blob: 2be92b6f41c092d57516f82108cc90628555afab [file] [log] [blame]
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +00001/*
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"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000017#include "Test.h"
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000018
commit-bot@chromium.org07adb632014-01-02 22:20:49 +000019struct SkFlattenableTraits {
commit-bot@chromium.org186c0cc2014-02-18 16:15:05 +000020 static void Flatten(SkWriteBuffer& buffer, const SkFlattenable& flattenable) {
commit-bot@chromium.org07adb632014-01-02 22:20:49 +000021 buffer.writeFlattenable(&flattenable);
22 }
23};
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000024
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 */
commit-bot@chromium.org07adb632014-01-02 22:20:49 +000041template <typename Traits, typename T>
42static void testCreate(skiatest::Reporter* reporter, const T& obj) {
scroggo@google.com15543602012-08-02 18:49:49 +000043 Controller controller;
commit-bot@chromium.org07adb632014-01-02 22:20:49 +000044 // 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);
mtklein@google.com9e3074e2013-08-20 16:48:47 +000047 REPORTER_ASSERT(reporter, *data1 == *data2);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000048}
49
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000050DEF_TEST(FlatData, reporter) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000051 // 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;
commit-bot@chromium.org07adb632014-01-02 22:20:49 +000058
59 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points, colors, NULL, 2,
60 SkShader::kRepeat_TileMode));
61 testCreate<SkFlattenableTraits>(reporter, *shader);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000062
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000063 // Test SkColorFilter
commit-bot@chromium.org07adb632014-01-02 22:20:49 +000064 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateLightingFilter(SK_ColorBLUE, SK_ColorRED));
65 testCreate<SkFlattenableTraits>(reporter, *cf);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000066
67 // Test SkXfermode
commit-bot@chromium.org07adb632014-01-02 22:20:49 +000068 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kDstOver_Mode));
69 testCreate<SkFlattenableTraits>(reporter, *xfer);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000070}