| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2013 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 #include "SkBlendImageFilter.h" | 9 #include "SkArithmeticImageFilter.h" |
| 10 #include "SkBitmapSource.h" | 10 #include "SkBitmapSource.h" |
| 11 | 11 |
| 12 #define WIDTH 500 |
| 13 #define HEIGHT 300 |
| 14 #define MARGIN 12 |
| 15 |
| 12 namespace skiagm { | 16 namespace skiagm { |
| 13 | 17 |
| 14 class ImageBlendGM : public GM { | 18 class ArithmeticImageFilterGM : public GM { |
| 15 public: | 19 public: |
| 16 ImageBlendGM() : fInitialized(false) { | 20 ArithmeticImageFilterGM() : fInitialized(false) { |
| 17 this->setBGColor(0xFF000000); | 21 this->setBGColor(0xFF000000); |
| 18 } | 22 } |
| 19 | 23 |
| 20 protected: | 24 protected: |
| 21 virtual SkString onShortName() { | 25 virtual SkString onShortName() { |
| 22 return SkString("blend"); | 26 return SkString("arithimagefilter"); |
| 23 } | 27 } |
| 24 | 28 |
| 25 void make_bitmap() { | 29 void make_bitmap() { |
| 26 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80); | 30 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80); |
| 27 fBitmap.allocPixels(); | 31 fBitmap.allocPixels(); |
| 28 SkDevice device(fBitmap); | 32 SkDevice device(fBitmap); |
| 29 SkCanvas canvas(&device); | 33 SkCanvas canvas(&device); |
| 30 canvas.clear(0x00000000); | 34 canvas.clear(0x00000000); |
| 31 SkPaint paint; | 35 SkPaint paint; |
| 32 paint.setAntiAlias(true); | 36 paint.setAntiAlias(true); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 53 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); | 57 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); |
| 54 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); | 58 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); |
| 55 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); | 59 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); |
| 56 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); | 60 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); |
| 57 canvas.restore(); | 61 canvas.restore(); |
| 58 } | 62 } |
| 59 } | 63 } |
| 60 } | 64 } |
| 61 | 65 |
| 62 virtual SkISize onISize() { | 66 virtual SkISize onISize() { |
| 63 return make_isize(500, 100); | 67 return make_isize(WIDTH, HEIGHT); |
| 64 } | 68 } |
| 65 | 69 |
| 66 void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x) { | 70 void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPai
nt& paint, SkScalar x, SkScalar y) { |
| 67 canvas->save(); | 71 canvas->save(); |
| 68 canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), 0, | 72 canvas->clipRect(SkRect::MakeXYWH(x, y, |
| 69 SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height()))); | 73 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()))); |
| 70 canvas->drawBitmap(fBitmap, SkIntToScalar(x), 0, &paint); | 74 canvas->drawBitmap(bitmap, x, y, &paint); |
| 71 canvas->restore(); | 75 canvas->restore(); |
| 72 } | 76 } |
| 73 | 77 |
| 74 virtual void onDraw(SkCanvas* canvas) { | 78 virtual void onDraw(SkCanvas* canvas) { |
| 75 if (!fInitialized) { | 79 if (!fInitialized) { |
| 76 make_bitmap(); | 80 make_bitmap(); |
| 77 make_checkerboard(); | 81 make_checkerboard(); |
| 78 fInitialized = true; | 82 fInitialized = true; |
| 79 } | 83 } |
| 80 canvas->clear(0x00000000); | 84 canvas->clear(0x00000000); |
| 81 SkPaint paint; | 85 SkPaint paint; |
| 82 SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fChec
kerboard))); | 86 |
| 83 paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, (SkBlendImageFilter:
:kNormal_Mode, background)))->unref(); | 87 const SkScalar one = SK_Scalar1; |
| 84 drawClippedBitmap(canvas, paint, 0); | 88 static const SkScalar kValues[] = { |
| 85 paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, (SkBlendImageFilter:
:kMultiply_Mode, background)))->unref(); | 89 0, 0, 0, 0, |
| 86 drawClippedBitmap(canvas, paint, 100); | 90 0, 0, 0, one, |
| 87 paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, (SkBlendImageFilter:
:kScreen_Mode, background)))->unref(); | 91 0, one, 0, 0, |
| 88 drawClippedBitmap(canvas, paint, 200); | 92 0, 0, one, 0, |
| 89 paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, (SkBlendImageFilter:
:kDarken_Mode, background)))->unref(); | 93 0, one, one, 0, |
| 90 drawClippedBitmap(canvas, paint, 300); | 94 0, one, -one, 0, |
| 91 paint.setImageFilter(SkNEW_ARGS(SkBlendImageFilter, (SkBlendImageFilter:
:kLighten_Mode, background)))->unref(); | 95 0, one/2, one/2, 0, |
| 92 drawClippedBitmap(canvas, paint, 400); | 96 0, one/2, one/2, one/4, |
| 97 0, one/2, one/2, -one/4, |
| 98 one/4, one/2, one/2, 0, |
| 99 -one/4, one/2, one/2, 0, |
| 100 }; |
| 101 |
| 102 int x = 0, y = 0; |
| 103 const SkScalar* kEnd = kValues + SK_ARRAY_COUNT(kValues); |
| 104 for (const SkScalar* k = kValues; k < kEnd; k += 4) { |
| 105 SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (f
Checkerboard))); |
| 106 SkAutoTUnref<SkImageFilter> arith(SkNEW_ARGS(SkArithmeticImageFilter
, (k[0], k[1], k[2], k[3], background))); |
| 107 paint.setImageFilter(arith); |
| 108 drawClippedBitmap(canvas, fBitmap, paint, SkIntToScalar(x), SkIntToS
calar(y)); |
| 109 x += fBitmap.width() + MARGIN; |
| 110 if (x + fBitmap.width() > WIDTH) { |
| 111 x = 0; |
| 112 y += fBitmap.height() + MARGIN; |
| 113 } |
| 114 } |
| 93 } | 115 } |
| 94 | |
| 95 private: | 116 private: |
| 96 typedef GM INHERITED; | 117 typedef GM INHERITED; |
| 97 SkBitmap fBitmap, fCheckerboard; | 118 SkBitmap fBitmap, fCheckerboard; |
| 98 bool fInitialized; | 119 bool fInitialized; |
| 99 }; | 120 }; |
| 100 | 121 |
| 101 ////////////////////////////////////////////////////////////////////////////// | 122 ////////////////////////////////////////////////////////////////////////////// |
| 102 | 123 |
| 103 static GM* MyFactory(void*) { return new ImageBlendGM; } | 124 static GM* MyFactory(void*) { return new ArithmeticImageFilterGM; } |
| 104 static GMRegistry reg(MyFactory); | 125 static GMRegistry reg(MyFactory); |
| 105 | 126 |
| 106 } | 127 } |
| OLD | NEW |