scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 1 | |
| 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 | |
| 20 | static void flattenFlattenableProc(SkOrderedWriteBuffer& buffer, |
| 21 | const void* obj) { |
| 22 | buffer.writeFlattenable((SkFlattenable*)obj); |
| 23 | } |
| 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 | */ |
| 41 | static void testCreate(skiatest::Reporter* reporter, const void* obj, |
| 42 | void (*flattenProc)(SkOrderedWriteBuffer&, const void*)) { |
scroggo@google.com | 1554360 | 2012-08-02 18:49:49 +0000 | [diff] [blame] | 43 | Controller controller; |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 44 | // No need to delete data because that will be taken care of by the |
| 45 | // controller. |
scroggo@google.com | 40fbb18 | 2012-08-14 19:30:20 +0000 | [diff] [blame] | 46 | SkFlatData* data1 = SkFlatData::Create(&controller, obj, 0, flattenProc); |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 47 | data1->setSentinelInCache(); |
scroggo@google.com | 40fbb18 | 2012-08-14 19:30:20 +0000 | [diff] [blame] | 48 | SkFlatData* data2 = SkFlatData::Create(&controller, obj, 1, flattenProc); |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 49 | data2->setSentinelAsCandidate(); |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 50 | REPORTER_ASSERT(reporter, SkFlatData::Compare(*data1, *data2) == 0); |
scroggo@google.com | 0c3e5fe | 2012-08-01 19:34:20 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static void Tests(skiatest::Reporter* reporter) { |
| 54 | // Test flattening SkShader |
| 55 | SkPoint points[2]; |
| 56 | points[0].set(0, 0); |
| 57 | points[1].set(SkIntToScalar(20), SkIntToScalar(20)); |
| 58 | SkColor colors[2]; |
| 59 | colors[0] = SK_ColorRED; |
| 60 | colors[1] = SK_ColorBLUE; |
| 61 | SkShader* shader = SkGradientShader::CreateLinear(points, colors, NULL, |
| 62 | 2, SkShader::kRepeat_TileMode); |
| 63 | SkAutoUnref aur(shader); |
| 64 | testCreate(reporter, shader, flattenFlattenableProc); |
| 65 | |
| 66 | // Test SkBitmap |
| 67 | { |
| 68 | SkBitmap bm; |
| 69 | bm.setConfig(SkBitmap::kARGB_8888_Config, 50, 50); |
| 70 | bm.allocPixels(); |
| 71 | SkCanvas canvas(bm); |
| 72 | SkPaint paint; |
| 73 | paint.setShader(shader); |
| 74 | canvas.drawPaint(paint); |
| 75 | testCreate(reporter, &bm, &SkFlattenObjectProc<SkBitmap>); |
| 76 | } |
| 77 | |
| 78 | // Test SkColorFilter |
| 79 | SkColorFilter* cf = SkColorFilter::CreateLightingFilter(SK_ColorBLUE, |
| 80 | SK_ColorRED); |
| 81 | SkAutoUnref aurcf(cf); |
| 82 | testCreate(reporter, cf, &flattenFlattenableProc); |
| 83 | |
| 84 | // Test SkXfermode |
| 85 | SkXfermode* xfer = SkXfermode::Create(SkXfermode::kDstOver_Mode); |
| 86 | SkAutoUnref aurxf(xfer); |
| 87 | testCreate(reporter, xfer, &flattenFlattenableProc); |
| 88 | } |
| 89 | |
| 90 | #include "TestClassDef.h" |
| 91 | DEFINE_TESTCLASS("FlatData", FlatDataClass, Tests) |