scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 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" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 17 | #include "Test.h" |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 18 | |
commit-bot@chromium.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 19 | struct SkFlattenableTraits { |
commit-bot@chromium.org | 186c0cc | 2014-02-18 16:15:05 +0000 | [diff] [blame] | 20 | static void Flatten(SkWriteBuffer& buffer, const SkFlattenable& flattenable) { |
commit-bot@chromium.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 21 | buffer.writeFlattenable(&flattenable); |
| 22 | } |
| 23 | }; |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 24 | |
scroggo@google.com | 1554360 | 2012-08-02 18:49:49 +0000 | [diff] [blame] | 25 | class Controller : public SkChunkFlatController { |
| 26 | public: |
| 27 | Controller() : INHERITED(1024) { |
| 28 | this->INHERITED::setNamedFactorySet(SkNEW(SkNamedFactorySet))->unref(); |
| 29 | } |
| 30 | private: |
| 31 | typedef SkChunkFlatController INHERITED; |
| 32 | }; |
| 33 | |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 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 | */ |
commit-bot@chromium.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 41 | template <typename Traits, typename T> |
| 42 | static void testCreate(skiatest::Reporter* reporter, const T& obj) { |
scroggo@google.com | 1554360 | 2012-08-02 18:49:49 +0000 | [diff] [blame] | 43 | Controller controller; |
commit-bot@chromium.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 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); |
mtklein@google.com | 9e3074e | 2013-08-20 16:48:47 +0000 | [diff] [blame] | 47 | REPORTER_ASSERT(reporter, *data1 == *data2); |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 48 | } |
| 49 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 50 | DEF_TEST(FlatData, reporter) { |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 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; |
commit-bot@chromium.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 58 | |
| 59 | SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points, colors, NULL, 2, |
| 60 | SkShader::kRepeat_TileMode)); |
| 61 | testCreate<SkFlattenableTraits>(reporter, *shader); |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 62 | |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 63 | // Test SkColorFilter |
commit-bot@chromium.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 64 | SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateLightingFilter(SK_ColorBLUE, SK_ColorRED)); |
| 65 | testCreate<SkFlattenableTraits>(reporter, *cf); |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 66 | |
| 67 | // Test SkXfermode |
commit-bot@chromium.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 68 | SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kDstOver_Mode)); |
| 69 | testCreate<SkFlattenableTraits>(reporter, *xfer); |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 70 | } |