blob: 2de270d50e5cabe529e67a3de37b6af1ff059f29 [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};
24struct SkBitmapTraits {
commit-bot@chromium.org186c0cc2014-02-18 16:15:05 +000025 static void Flatten(SkWriteBuffer& buffer, const SkBitmap& bitmap) {
commit-bot@chromium.org07adb632014-01-02 22:20:49 +000026 bitmap.flatten(buffer);
27 }
28};
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000029
scroggo@google.com15543602012-08-02 18:49:49 +000030class Controller : public SkChunkFlatController {
31public:
32 Controller() : INHERITED(1024) {
33 this->INHERITED::setNamedFactorySet(SkNEW(SkNamedFactorySet))->unref();
34 }
35private:
36 typedef SkChunkFlatController INHERITED;
37};
38
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000039/**
40 * Verify that two SkFlatData objects that created from the same object are
41 * identical when using an SkNamedFactorySet.
42 * @param reporter Object to report failures.
43 * @param obj Flattenable object to be flattened.
44 * @param flattenProc Function that flattens objects with the same type as obj.
45 */
commit-bot@chromium.org07adb632014-01-02 22:20:49 +000046template <typename Traits, typename T>
47static void testCreate(skiatest::Reporter* reporter, const T& obj) {
scroggo@google.com15543602012-08-02 18:49:49 +000048 Controller controller;
commit-bot@chromium.org07adb632014-01-02 22:20:49 +000049 // No need to delete data because that will be taken care of by the controller.
50 SkFlatData* data1 = SkFlatData::Create<Traits>(&controller, obj, 0);
51 SkFlatData* data2 = SkFlatData::Create<Traits>(&controller, obj, 1);
mtklein@google.com9e3074e2013-08-20 16:48:47 +000052 REPORTER_ASSERT(reporter, *data1 == *data2);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000053}
54
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000055DEF_TEST(FlatData, reporter) {
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000056 // Test flattening SkShader
57 SkPoint points[2];
58 points[0].set(0, 0);
59 points[1].set(SkIntToScalar(20), SkIntToScalar(20));
60 SkColor colors[2];
61 colors[0] = SK_ColorRED;
62 colors[1] = SK_ColorBLUE;
commit-bot@chromium.org07adb632014-01-02 22:20:49 +000063
64 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points, colors, NULL, 2,
65 SkShader::kRepeat_TileMode));
66 testCreate<SkFlattenableTraits>(reporter, *shader);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000067
68 // Test SkBitmap
69 {
70 SkBitmap bm;
mike@reedtribe.orgdeee4962014-02-13 14:41:43 +000071 bm.allocN32Pixels(50, 50);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000072 SkCanvas canvas(bm);
73 SkPaint paint;
74 paint.setShader(shader);
75 canvas.drawPaint(paint);
commit-bot@chromium.org07adb632014-01-02 22:20:49 +000076 testCreate<SkBitmapTraits>(reporter, bm);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000077 }
78
79 // Test SkColorFilter
commit-bot@chromium.org07adb632014-01-02 22:20:49 +000080 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateLightingFilter(SK_ColorBLUE, SK_ColorRED));
81 testCreate<SkFlattenableTraits>(reporter, *cf);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000082
83 // Test SkXfermode
commit-bot@chromium.org07adb632014-01-02 22:20:49 +000084 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kDstOver_Mode));
85 testCreate<SkFlattenableTraits>(reporter, *xfer);
scroggo@google.com0c3e5fe2012-08-01 19:34:20 +000086}