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 | }; |
| 24 | struct SkBitmapTraits { |
commit-bot@chromium.org | 186c0cc | 2014-02-18 16:15:05 +0000 | [diff] [blame] | 25 | static void Flatten(SkWriteBuffer& buffer, const SkBitmap& bitmap) { |
commit-bot@chromium.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 26 | bitmap.flatten(buffer); |
| 27 | } |
| 28 | }; |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 29 | |
scroggo@google.com | 1554360 | 2012-08-02 18:49:49 +0000 | [diff] [blame] | 30 | class Controller : public SkChunkFlatController { |
| 31 | public: |
| 32 | Controller() : INHERITED(1024) { |
| 33 | this->INHERITED::setNamedFactorySet(SkNEW(SkNamedFactorySet))->unref(); |
| 34 | } |
| 35 | private: |
| 36 | typedef SkChunkFlatController INHERITED; |
| 37 | }; |
| 38 | |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 39 | /** |
| 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.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 46 | template <typename Traits, typename T> |
| 47 | static void testCreate(skiatest::Reporter* reporter, const T& obj) { |
scroggo@google.com | 1554360 | 2012-08-02 18:49:49 +0000 | [diff] [blame] | 48 | Controller controller; |
commit-bot@chromium.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 49 | // 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.com | 9e3074e | 2013-08-20 16:48:47 +0000 | [diff] [blame] | 52 | REPORTER_ASSERT(reporter, *data1 == *data2); |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 53 | } |
| 54 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 55 | DEF_TEST(FlatData, reporter) { |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 56 | // 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.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 63 | |
| 64 | SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points, colors, NULL, 2, |
| 65 | SkShader::kRepeat_TileMode)); |
| 66 | testCreate<SkFlattenableTraits>(reporter, *shader); |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 67 | |
| 68 | // Test SkBitmap |
| 69 | { |
| 70 | SkBitmap bm; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 71 | bm.allocN32Pixels(50, 50); |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 72 | SkCanvas canvas(bm); |
| 73 | SkPaint paint; |
| 74 | paint.setShader(shader); |
| 75 | canvas.drawPaint(paint); |
commit-bot@chromium.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 76 | testCreate<SkBitmapTraits>(reporter, bm); |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // Test SkColorFilter |
commit-bot@chromium.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 80 | SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateLightingFilter(SK_ColorBLUE, SK_ColorRED)); |
| 81 | testCreate<SkFlattenableTraits>(reporter, *cf); |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 82 | |
| 83 | // Test SkXfermode |
commit-bot@chromium.org | 07adb63 | 2014-01-02 22:20:49 +0000 | [diff] [blame] | 84 | SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kDstOver_Mode)); |
| 85 | testCreate<SkFlattenableTraits>(reporter, *xfer); |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 86 | } |