Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2015 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 "sk_tool_utils.h" | 8 #include "sk_tool_utils.h" |
| 9 #include "SkBitmapSource.h" | 9 #include "SkBitmapSource.h" |
| 10 #include "SkBlurImageFilter.h" | 10 #include "SkBlurImageFilter.h" |
| 11 #include "SkColor.h" | 11 #include "SkColor.h" |
| 12 #include "SkDisplacementMapEffect.h" | 12 #include "SkDisplacementMapEffect.h" |
| 13 #include "SkDropShadowImageFilter.h" | 13 #include "SkDropShadowImageFilter.h" |
| 14 #include "SkGradientShader.h" | 14 #include "SkGradientShader.h" |
| 15 #include "SkMorphologyImageFilter.h" | 15 #include "SkMorphologyImageFilter.h" |
| 16 #include "SkOffsetImageFilter.h" | |
| 17 #include "SkPerlinNoiseShader.h" | |
| 18 #include "SkRectShaderImageFilter.h" | |
| 19 #include "SkScalar.h" | 16 #include "SkScalar.h" |
| 20 #include "gm.h" | 17 #include "gm.h" |
| 21 | 18 |
| 22 #define RESIZE_FACTOR_X SkIntToScalar(2) | |
| 23 #define RESIZE_FACTOR_Y SkIntToScalar(5) | |
| 24 | |
| 25 namespace skiagm { | 19 namespace skiagm { |
| 26 | 20 |
|
robertphillips
2015/03/20 14:01:09
// This GM checks that the scale portion of the ap
Stephen White
2015/03/20 14:19:08
Done (with tweaks).
| |
| 27 class ImageFiltersClippedGM : public GM { | 21 class ImageFiltersTransformedGM : public GM { |
| 28 public: | 22 public: |
| 29 ImageFiltersClippedGM() : fInitialized(false) { | 23 ImageFiltersTransformedGM() : fInitialized(false) { |
| 30 this->setBGColor(0x00000000); | 24 this->setBGColor(SK_ColorBLACK); |
| 31 } | 25 } |
| 32 | 26 |
| 33 protected: | 27 protected: |
| 34 | 28 |
|
robertphillips
2015/03/20 14:01:09
one line ?
Stephen White
2015/03/20 14:19:08
Done.
| |
| 35 SkString onShortName() SK_OVERRIDE { | 29 SkString onShortName() SK_OVERRIDE { |
| 36 return SkString("imagefiltersclipped"); | 30 return SkString("imagefilterstransformed"); |
| 37 } | 31 } |
| 38 | 32 |
|
robertphillips
2015/03/20 14:01:09
one line ?
Stephen White
2015/03/20 14:19:08
Done.
| |
| 39 SkISize onISize() SK_OVERRIDE { | 33 SkISize onISize() SK_OVERRIDE { |
| 40 return SkISize::Make(860, 500); | 34 return SkISize::Make(560, 300); |
| 41 } | 35 } |
| 42 | 36 |
|
robertphillips
2015/03/20 14:01:09
makeGradientCircle ?
Stephen White
2015/03/20 14:19:08
Done.
| |
| 43 void make_gradient_circle(int width, int height) { | 37 void make_gradient_circle(int width, int height) { |
| 44 SkScalar x = SkIntToScalar(width / 2); | 38 SkScalar x = SkIntToScalar(width / 2); |
| 45 SkScalar y = SkIntToScalar(height / 2); | 39 SkScalar y = SkIntToScalar(height / 2); |
| 46 SkScalar radius = SkMinScalar(x, y) * 0.8f; | 40 SkScalar radius = SkMinScalar(x, y) * 0.8f; |
| 47 fGradientCircle.allocN32Pixels(width, height); | 41 fGradientCircle.allocN32Pixels(width, height); |
| 48 SkCanvas canvas(fGradientCircle); | 42 SkCanvas canvas(fGradientCircle); |
| 49 canvas.clear(0x00000000); | 43 canvas.clear(0x00000000); |
| 50 SkColor colors[2]; | 44 SkColor colors[2]; |
| 51 colors[0] = SK_ColorWHITE; | 45 colors[0] = SK_ColorWHITE; |
| 52 colors[1] = SK_ColorBLACK; | 46 colors[1] = SK_ColorBLACK; |
| 53 SkAutoTUnref<SkShader> shader( | 47 SkAutoTUnref<SkShader> shader( |
| 54 SkGradientShader::CreateRadial(SkPoint::Make(x, y), radius, colors, NULL, 2, | 48 SkGradientShader::CreateRadial(SkPoint::Make(x, y), radius, colors, NULL, 2, |
| 55 SkShader::kClamp_TileMode) | 49 SkShader::kClamp_TileMode) |
| 56 ); | 50 ); |
| 57 SkPaint paint; | 51 SkPaint paint; |
| 58 paint.setShader(shader); | 52 paint.setShader(shader); |
| 59 canvas.drawCircle(x, y, radius, paint); | 53 canvas.drawCircle(x, y, radius, paint); |
| 60 } | 54 } |
| 61 | 55 |
| 62 void onDraw(SkCanvas* canvas) SK_OVERRIDE { | 56 void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
|
robertphillips
2015/03/20 14:01:09
Can we move this stuff to a onOnceBeforeDraw metho
Stephen White
2015/03/20 14:19:08
Done.
| |
| 63 if (!fInitialized) { | 57 if (!fInitialized) { |
| 64 fCheckerboard.allocN32Pixels(64, 64); | 58 fCheckerboard.allocN32Pixels(64, 64); |
| 65 SkCanvas checkerboardCanvas(fCheckerboard); | 59 SkCanvas checkerboardCanvas(fCheckerboard); |
| 66 sk_tool_utils::draw_checkerboard(&checkerboardCanvas, 0xFFA0A0A0, 0x FF404040, 8); | 60 sk_tool_utils::draw_checkerboard(&checkerboardCanvas, 0xFFA0A0A0, 0x FF404040, 8); |
| 67 | 61 |
| 68 this->make_gradient_circle(64, 64); | 62 this->make_gradient_circle(64, 64); |
| 69 fInitialized = true; | 63 fInitialized = true; |
| 70 } | 64 } |
| 71 canvas->clear(0x00000000); | 65 |
| 66 canvas->clear(SK_ColorBLACK); | |
| 72 | 67 |
| 73 SkAutoTUnref<SkImageFilter> gradient(SkBitmapSource::Create(fGradientCir cle)); | 68 SkAutoTUnref<SkImageFilter> gradient(SkBitmapSource::Create(fGradientCir cle)); |
| 74 SkAutoTUnref<SkImageFilter> checkerboard(SkBitmapSource::Create(fChecker board)); | 69 SkAutoTUnref<SkImageFilter> checkerboard(SkBitmapSource::Create(fChecker board)); |
| 75 SkAutoTUnref<SkShader> noise(SkPerlinNoiseShader::CreateFractalNoise( | |
| 76 SkDoubleToScalar(0.1), SkDoubleToScalar(0.05), 1, 0)); | |
| 77 SkMatrix resizeMatrix; | |
| 78 resizeMatrix.setScale(RESIZE_FACTOR_X, RESIZE_FACTOR_Y); | |
| 79 | |
| 80 SkImageFilter* filters[] = { | 70 SkImageFilter* filters[] = { |
| 81 SkBlurImageFilter::Create(SkIntToScalar(12), SkIntToScalar(12)), | 71 SkBlurImageFilter::Create(12, 0), |
| 82 SkDropShadowImageFilter::Create(SkIntToScalar(10), SkIntToScalar(10) , | 72 SkDropShadowImageFilter::Create(0, 15, 8, 0, SK_ColorGREEN, |
| 83 SkIntToScalar(3), SkIntToScalar(3), SK_ColorGREEN, | |
| 84 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode), | 73 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode), |
| 85 SkDisplacementMapEffect::Create(SkDisplacementMapEffect::kR_ChannelS electorType, | 74 SkDisplacementMapEffect::Create(SkDisplacementMapEffect::kR_ChannelS electorType, |
| 86 SkDisplacementMapEffect::kR_ChannelS electorType, | 75 SkDisplacementMapEffect::kR_ChannelS electorType, |
| 87 SkIntToScalar(12), | 76 12, |
| 88 gradient.get(), | 77 gradient.get(), |
| 89 checkerboard.get()), | 78 checkerboard.get()), |
| 90 SkDilateImageFilter::Create(2, 2, checkerboard.get()), | 79 SkDilateImageFilter::Create(2, 2, checkerboard.get()), |
| 91 SkErodeImageFilter::Create(2, 2, checkerboard.get()), | 80 SkErodeImageFilter::Create(2, 2, checkerboard.get()), |
| 92 SkOffsetImageFilter::Create(SkIntToScalar(-16), SkIntToScalar(32)), | |
| 93 SkImageFilter::CreateMatrixFilter(resizeMatrix, kNone_SkFilterQualit y), | |
| 94 SkRectShaderImageFilter::Create(noise), | |
| 95 }; | 81 }; |
| 96 | 82 |
|
robertphillips
2015/03/20 14:01:09
Can we make these two guys const ?
Stephen White
2015/03/20 14:19:08
Done.
| |
| 97 SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64)); | 83 SkScalar margin = SkIntToScalar(40); |
| 98 SkScalar margin = SkIntToScalar(16); | 84 SkScalar size = SkIntToScalar(60); |
| 99 SkRect bounds = r; | |
| 100 bounds.outset(margin, margin); | |
| 101 | 85 |
| 102 for (int xOffset = 0; xOffset < 80; xOffset += 16) { | 86 for (size_t j = 0; j < 3; j++) { |
| 103 canvas->save(); | 87 canvas->save(); |
| 104 bounds.fLeft = SkIntToScalar(xOffset); | 88 canvas->translate(margin, 0); |
| 105 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { | 89 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 106 SkPaint paint; | 90 SkPaint paint; |
| 107 paint.setColor(SK_ColorWHITE); | 91 paint.setColor(SK_ColorWHITE); |
| 108 paint.setImageFilter(filters[i]); | 92 paint.setImageFilter(filters[i]); |
| 109 paint.setAntiAlias(true); | 93 paint.setAntiAlias(true); |
| 110 canvas->save(); | 94 canvas->save(); |
| 111 canvas->clipRect(bounds); | 95 canvas->translate(size * 0.5, size * 0.5); |
| 112 if (5 == i) { | 96 if (j == 1) { |
| 113 canvas->translate(SkIntToScalar(16), SkIntToScalar(-32)); | 97 canvas->rotate(SkIntToScalar(45)); |
| 114 } else if (6 == i) { | 98 } else if (j == 2) { |
| 115 canvas->scale(SkScalarInvert(RESIZE_FACTOR_X), | 99 canvas->skew(0.5, 0.2); |
| 116 SkScalarInvert(RESIZE_FACTOR_Y)); | |
| 117 } | 100 } |
| 118 canvas->drawCircle(r.centerX(), r.centerY(), | 101 canvas->translate(-size * 0.5, -size * 0.5); |
| 119 SkScalarDiv(r.width()*2, SkIntToScalar(5)), p aint); | 102 canvas->drawOval(SkRect::MakeXYWH(0, size * 0.1, size, size * 0. 6), paint); |
| 120 canvas->restore(); | 103 canvas->restore(); |
| 121 canvas->translate(r.width() + margin, 0); | 104 canvas->translate(size + margin, 0); |
| 122 } | 105 } |
| 123 canvas->restore(); | 106 canvas->restore(); |
| 124 canvas->translate(0, r.height() + margin); | 107 canvas->translate(0, size + margin); |
| 125 } | 108 } |
| 126 | 109 |
| 127 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { | 110 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 128 SkSafeUnref(filters[i]); | 111 SkSafeUnref(filters[i]); |
| 129 } | 112 } |
| 130 } | 113 } |
| 131 | 114 |
| 132 private: | 115 private: |
| 133 bool fInitialized; | 116 bool fInitialized; |
| 134 SkBitmap fCheckerboard; | 117 SkBitmap fCheckerboard; |
| 135 SkBitmap fGradientCircle; | 118 SkBitmap fGradientCircle; |
| 136 typedef GM INHERITED; | 119 typedef GM INHERITED; |
| 137 }; | 120 }; |
| 138 | 121 |
| 139 ////////////////////////////////////////////////////////////////////////////// | 122 ////////////////////////////////////////////////////////////////////////////// |
| 140 | 123 |
|
robertphillips
2015/03/20 14:01:09
Use DEF_GM macro ?
Stephen White
2015/03/20 14:19:08
Done.
| |
| 141 static GM* MyFactory(void*) { return new ImageFiltersClippedGM; } | 124 static GM* MyFactory(void*) { return new ImageFiltersTransformedGM; } |
| 142 static GMRegistry reg(MyFactory); | 125 static GMRegistry reg(MyFactory); |
| 143 | 126 |
| 144 } | 127 } |
| OLD | NEW |