| reed | fd3a91e | 2015-03-26 13:29:56 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 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 "SkCanvas.h" |
| 9 | #include "SkColorFilter.h" |
| 10 | #include "SkColorFilterImageFilter.h" |
| 11 | #include "SkDrawLooper.h" |
| 12 | #include "Test.h" |
| 13 | |
| 14 | /* Tests for SkDrawLooper -related APIs and implementations. */ |
| 15 | |
| 16 | namespace { |
| 17 | /* |
| 18 | * Subclass that caused an assert at the time of writing. |
| 19 | */ |
| 20 | class GetSaveCountAssertLooper : public SkDrawLooper { |
| 21 | public: |
| 22 | |
| 23 | SkDrawLooper::Context* createContext(SkCanvas*, void* storage) const override { |
| 24 | return SkNEW_PLACEMENT(storage, GetSaveCountAssertLooperContext); |
| 25 | } |
| 26 | |
| 27 | size_t contextSize() const override { return sizeof(GetSaveCountAssertLooperContext); } |
| 28 | |
| 29 | #ifndef SK_IGNORE_TO_STRING |
| 30 | void toString(SkString* str) const override { |
| 31 | str->append("GetSaveCountAssertLooper:"); |
| 32 | } |
| 33 | #endif |
| 34 | |
| 35 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(GetSaveCountAssertLooper); |
| 36 | |
| 37 | private: |
| 38 | class GetSaveCountAssertLooperContext : public SkDrawLooper::Context { |
| 39 | public: |
| 40 | GetSaveCountAssertLooperContext() : fOnce(0) {} |
| 41 | bool next(SkCanvas* canvas, SkPaint* p) override { |
| 42 | // Getting the save count would assert in SkCanvas at the time of writing. |
| 43 | canvas->getSaveCount(); |
| 44 | |
| 45 | SkASSERT(p->getColor() == SK_ColorRED); |
| 46 | // Set the color green so the test knows payload was run. We use this color in order to |
| 47 | // try to express the expectation that Skia can not away the color filter. Due to |
| 48 | // non-pm-to-pm-to-non-pm conversions, this probably is not exactly correct. |
| 49 | p->setColor(SkColorSetARGB(255, 0, 254, 0)); |
| 50 | return fOnce++ < 1; |
| 51 | } |
| 52 | private: |
| 53 | int fOnce; |
| 54 | }; |
| 55 | }; |
| 56 | |
| 57 | SkFlattenable* GetSaveCountAssertLooper::CreateProc(SkReadBuffer&) { |
| 58 | return SkNEW(GetSaveCountAssertLooper); |
| 59 | } |
| 60 | |
| 61 | } |
| 62 | |
| 63 | DEF_TEST(SkCanvas_GetSaveCountInDrawLooperAssert, reporter) { |
| 64 | SkBitmap dst; |
| 65 | dst.allocN32Pixels(10, 10); |
| 66 | dst.eraseColor(SK_ColorTRANSPARENT); |
| 67 | |
| 68 | SkCanvas canvas(dst); |
| 69 | SkPaint paint; |
| 70 | { |
| 71 | SkAutoTUnref<SkColorFilter> addGreenCF( |
| 72 | SkColorFilter::CreateModeFilter(SkColorSetARGB(64, 0, 4, 0), SkXfermode::kPlus_Mode)); |
| 73 | SkAutoTUnref<SkImageFilter> addGreenIF( |
| 74 | SkColorFilterImageFilter::Create(addGreenCF)); |
| 75 | |
| 76 | // This would trigger the assert upon a draw. It is a color filter that adds (roughly) 1 to |
| 77 | // the green component. |
| 78 | paint.setImageFilter(addGreenIF); |
| 79 | |
| 80 | SkAutoTUnref<SkDrawLooper> looper(SkNEW(GetSaveCountAssertLooper)); |
| 81 | paint.setLooper(looper); |
| 82 | } |
| 83 | paint.setColor(SK_ColorRED); |
| 84 | paint.setStrokeWidth(1); |
| 85 | paint.setStyle(SkPaint::kStroke_Style); |
| 86 | canvas.drawPoint(0, 0, paint); |
| 87 | |
| 88 | uint32_t pixel = 0; |
| 89 | SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, kUnpremul_SkAlphaType); |
| 90 | canvas.readPixels(info, &pixel, 4, 0, 0); |
| 91 | REPORTER_ASSERT(reporter, pixel == SK_ColorGREEN); |
| 92 | } |