OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 | 9 |
| 10 #include "SkArithmeticMode.h" |
10 #include "SkBitmapSource.h" | 11 #include "SkBitmapSource.h" |
11 #include "SkBlurImageFilter.h" | 12 #include "SkBlurImageFilter.h" |
12 #include "SkColorFilter.h" | 13 #include "SkColorFilter.h" |
13 #include "SkColorMatrixFilter.h" | 14 #include "SkColorMatrixFilter.h" |
14 #include "SkColorFilterImageFilter.h" | 15 #include "SkColorFilterImageFilter.h" |
| 16 #include "SkFlattenableBuffers.h" |
15 #include "SkMergeImageFilter.h" | 17 #include "SkMergeImageFilter.h" |
16 #include "SkMorphologyImageFilter.h" | 18 #include "SkMorphologyImageFilter.h" |
| 19 #include "SkOnce.h" |
17 #include "SkXfermodeImageFilter.h" | 20 #include "SkXfermodeImageFilter.h" |
18 | 21 |
19 #include "SkTestImageFilters.h" | 22 #include "SkTestImageFilters.h" |
20 | 23 |
21 /////////////////////////////////////////////////////////////////////////////// | 24 /////////////////////////////////////////////////////////////////////////////// |
22 | 25 |
| 26 namespace { |
| 27 // More closely models how Blink's OffsetFilter works as of 10/23/13. SkOffsetIm
ageFilter doesn't |
| 28 // perform a draw and this one does. |
| 29 class SimpleOffsetFilter : public SkImageFilter { |
| 30 public: |
| 31 SimpleOffsetFilter(SkScalar dx, SkScalar dy, SkImageFilter* input) |
| 32 : SkImageFilter(input), fDX(dx), fDY(dy) {} |
| 33 |
| 34 virtual bool onFilterImage(Proxy* proxy, const SkBitmap& src, const SkMatrix
& ctm, |
| 35 SkBitmap* dst, SkIPoint* offset) SK_OVERRIDE { |
| 36 SkBitmap source = src; |
| 37 SkImageFilter* input = getInput(0); |
| 38 SkIPoint srcOffset = SkIPoint::Make(0, 0); |
| 39 if (NULL != input && !input->filterImage(proxy, src, ctm, &source, &srcO
ffset)) { |
| 40 return false; |
| 41 } |
| 42 |
| 43 SkIRect bounds; |
| 44 source.getBounds(&bounds); |
| 45 |
| 46 if (!this->applyCropRect(&bounds, ctm)) { |
| 47 return false; |
| 48 } |
| 49 |
| 50 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bo
unds.height())); |
| 51 SkCanvas canvas(device); |
| 52 SkPaint paint; |
| 53 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 54 canvas.drawBitmap(source, fDX - bounds.left(), fDY - bounds.top(), &pain
t); |
| 55 *dst = device->accessBitmap(false); |
| 56 offset->fX += bounds.left(); |
| 57 offset->fY += bounds.top(); |
| 58 return true; |
| 59 } |
| 60 |
| 61 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SimpleOffsetFilter); |
| 62 |
| 63 protected: |
| 64 explicit SimpleOffsetFilter(SkFlattenableReadBuffer& buffer) |
| 65 : SkImageFilter(buffer) { |
| 66 fDX = buffer.readScalar(); |
| 67 fDY = buffer.readScalar(); |
| 68 } |
| 69 |
| 70 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE { |
| 71 this->SkImageFilter::flatten(buffer); |
| 72 buffer.writeScalar(fDX); |
| 73 buffer.writeScalar(fDY); |
| 74 } |
| 75 |
| 76 private: |
| 77 SkScalar fDX, fDY; |
| 78 }; |
| 79 } |
| 80 |
| 81 static void init_flattenable(int*) { |
| 82 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SimpleOffsetFilter) |
| 83 } |
| 84 |
23 class ImageFiltersGraphGM : public skiagm::GM { | 85 class ImageFiltersGraphGM : public skiagm::GM { |
24 public: | 86 public: |
25 ImageFiltersGraphGM() : fInitialized(false) {} | 87 ImageFiltersGraphGM() : fInitialized(false) { |
| 88 int dummy; |
| 89 SK_DECLARE_STATIC_ONCE(once); |
| 90 SkOnce(&once, init_flattenable, &dummy); |
| 91 } |
26 | 92 |
27 protected: | 93 protected: |
28 virtual SkString onShortName() { | 94 virtual SkString onShortName() { |
29 return SkString("imagefiltersgraph"); | 95 return SkString("imagefiltersgraph"); |
30 } | 96 } |
31 | 97 |
32 void make_bitmap() { | 98 void make_bitmap() { |
33 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); | 99 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); |
34 fBitmap.allocPixels(); | 100 fBitmap.allocPixels(); |
35 SkBitmapDevice device(fBitmap); | 101 SkBitmapDevice device(fBitmap); |
36 SkCanvas canvas(&device); | 102 SkCanvas canvas(&device); |
37 canvas.clear(0x00000000); | 103 canvas.clear(0x00000000); |
38 SkPaint paint; | 104 SkPaint paint; |
39 paint.setAntiAlias(true); | 105 paint.setAntiAlias(true); |
40 paint.setColor(0xFFFFFFFF); | 106 paint.setColor(0xFFFFFFFF); |
41 paint.setTextSize(SkIntToScalar(96)); | 107 paint.setTextSize(SkIntToScalar(96)); |
42 const char* str = "e"; | 108 const char* str = "e"; |
43 canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70),
paint); | 109 canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70),
paint); |
44 } | 110 } |
45 | 111 |
46 virtual SkISize onISize() { return SkISize::Make(200, 100); } | 112 virtual SkISize onISize() { return SkISize::Make(500, 150); } |
47 | 113 |
48 virtual void onDraw(SkCanvas* canvas) { | 114 virtual void onDraw(SkCanvas* canvas) { |
49 if (!fInitialized) { | 115 if (!fInitialized) { |
50 this->make_bitmap(); | 116 this->make_bitmap(); |
51 fInitialized = true; | 117 fInitialized = true; |
52 } | 118 } |
53 canvas->clear(0x00000000); | 119 canvas->clear(0x00000000); |
54 { | 120 { |
55 SkAutoTUnref<SkImageFilter> bitmapSource(new SkBitmapSource(fBitmap)
); | 121 SkAutoTUnref<SkImageFilter> bitmapSource(new SkBitmapSource(fBitmap)
); |
56 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_Co
lorRED, | 122 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_Co
lorRED, |
(...skipping 17 matching lines...) Expand all Loading... |
74 | 140 |
75 SkAutoTUnref<SkColorFilter> matrixFilter(new SkColorMatrixFilter(mat
rix)); | 141 SkAutoTUnref<SkColorFilter> matrixFilter(new SkColorMatrixFilter(mat
rix)); |
76 SkAutoTUnref<SkImageFilter> colorMorph(SkColorFilterImageFilter::Cre
ate(matrixFilter, morph)); | 142 SkAutoTUnref<SkImageFilter> colorMorph(SkColorFilterImageFilter::Cre
ate(matrixFilter, morph)); |
77 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOve
r_Mode)); | 143 SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(SkXfermode::kSrcOve
r_Mode)); |
78 SkAutoTUnref<SkImageFilter> blendColor(new SkXfermodeImageFilter(mod
e, colorMorph)); | 144 SkAutoTUnref<SkImageFilter> blendColor(new SkXfermodeImageFilter(mod
e, colorMorph)); |
79 | 145 |
80 SkPaint paint; | 146 SkPaint paint; |
81 paint.setImageFilter(blendColor); | 147 paint.setImageFilter(blendColor); |
82 canvas->drawBitmap(fBitmap, 100, 0, &paint); | 148 canvas->drawBitmap(fBitmap, 100, 0, &paint); |
83 } | 149 } |
| 150 { |
| 151 SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0, |
| 152 0, SK_Scalar1, 0, 0, 0, |
| 153 0, 0, SK_Scalar1, 0, 0, |
| 154 0, 0, 0, SkFloatToScalar(0.5f), 0 }; |
| 155 SkColorMatrixFilter matrixCF(matrix); |
| 156 SkAutoTUnref<SkImageFilter> matrixFilter(SkColorFilterImageFilter::C
reate(&matrixCF)); |
| 157 SimpleOffsetFilter offsetFilter(SkIntToScalar(10), SkIntToScalar(10)
, matrixFilter); |
| 158 |
| 159 SkAutoTUnref<SkXfermode> arith(SkArithmeticMode::Create(0, SK_Scalar
1, SK_Scalar1, 0)); |
| 160 SkXfermodeImageFilter arithFilter(arith, matrixFilter, &offsetFilter
); |
| 161 |
| 162 SkPaint paint; |
| 163 paint.setImageFilter(&arithFilter); |
| 164 canvas->drawSprite(fBitmap, 200, 0, &paint); |
| 165 } |
84 } | 166 } |
85 | 167 |
86 private: | 168 private: |
87 typedef GM INHERITED; | 169 typedef GM INHERITED; |
88 SkBitmap fBitmap; | 170 SkBitmap fBitmap; |
89 bool fInitialized; | 171 bool fInitialized; |
90 }; | 172 }; |
91 | 173 |
92 /////////////////////////////////////////////////////////////////////////////// | 174 /////////////////////////////////////////////////////////////////////////////// |
93 | 175 |
94 static skiagm::GM* MyFactory(void*) { return new ImageFiltersGraphGM; } | 176 static skiagm::GM* MyFactory(void*) { return new ImageFiltersGraphGM; } |
95 static skiagm::GMRegistry reg(MyFactory); | 177 static skiagm::GMRegistry reg(MyFactory); |
OLD | NEW |