| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 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 "sk_tool_utils.h" | 8 #include "sk_tool_utils.h" |
| 9 #include "SkBitmapSource.h" | 9 #include "SkImage.h" |
| 10 #include "SkImageSource.h" |
| 10 #include "SkOffsetImageFilter.h" | 11 #include "SkOffsetImageFilter.h" |
| 12 #include "SkSurface.h" |
| 11 #include "gm.h" | 13 #include "gm.h" |
| 12 | 14 |
| 13 #define WIDTH 600 | 15 #define WIDTH 600 |
| 14 #define HEIGHT 100 | 16 #define HEIGHT 100 |
| 15 #define MARGIN 12 | 17 #define MARGIN 12 |
| 16 | 18 |
| 17 class OffsetImageFilterGM : public skiagm::GM { | 19 class OffsetImageFilterGM : public skiagm::GM { |
| 18 public: | 20 public: |
| 19 OffsetImageFilterGM() { | 21 OffsetImageFilterGM() { |
| 20 this->setBGColor(0xFF000000); | 22 this->setBGColor(0xFF000000); |
| 21 } | 23 } |
| 22 | 24 |
| 23 protected: | 25 protected: |
| 24 SkString onShortName() override { | 26 SkString onShortName() override { |
| 25 return SkString("offsetimagefilter"); | 27 return SkString("offsetimagefilter"); |
| 26 } | 28 } |
| 27 | 29 |
| 28 SkISize onISize() override { | 30 SkISize onISize() override { |
| 29 return SkISize::Make(WIDTH, HEIGHT); | 31 return SkISize::Make(WIDTH, HEIGHT); |
| 30 } | 32 } |
| 31 | 33 |
| 32 void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPai
nt& paint, | |
| 33 SkScalar scale, const SkIRect& cropRect) { | |
| 34 SkRect clipRect = SkRect::MakeIWH(bitmap.width(), bitmap.height()); | |
| 35 | |
| 36 canvas->save(); | |
| 37 canvas->clipRect(clipRect); | |
| 38 canvas->scale(scale, scale); | |
| 39 canvas->drawBitmap(bitmap, 0, 0, &paint); | |
| 40 canvas->restore(); | |
| 41 | |
| 42 // Draw a boundary rect around the intersection of the clip rect and cro
p rect. | |
| 43 SkRect cropRectFloat; | |
| 44 SkMatrix::MakeScale(scale, scale).mapRect(&cropRectFloat, SkRect::Make(c
ropRect)); | |
| 45 if (clipRect.intersect(cropRectFloat)) { | |
| 46 SkPaint strokePaint; | |
| 47 strokePaint.setStyle(SkPaint::kStroke_Style); | |
| 48 strokePaint.setStrokeWidth(2); | |
| 49 strokePaint.setColor(SK_ColorRED); | |
| 50 canvas->drawRect(clipRect, strokePaint); | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 void onOnceBeforeDraw() override { | 34 void onOnceBeforeDraw() override { |
| 55 fBitmap = sk_tool_utils::create_string_bitmap(80, 80, 0xD000D000, 15, 65
, 96, "e"); | 35 fBitmap.reset(SkImage::NewFromBitmap( |
| 36 sk_tool_utils::create_string_bitmap(80, 80, 0xD000D000, 15, 65, 96,
"e"))); |
| 56 | 37 |
| 57 fCheckerboard = sk_tool_utils::create_checkerboard_bitmap( | 38 fCheckerboard.reset(SkImage::NewFromBitmap( |
| 58 80, 80, | 39 sk_tool_utils::create_checkerboard_bitmap(80, 80, |
| 59 sk_tool_utils::color_to_
565(0xFFA0A0A0), | 40 sk_tool_utils::color_to_56
5(0xFFA0A0A0), |
| 60 sk_tool_utils::color_to_
565(0xFF404040), | 41 sk_tool_utils::color_to_56
5(0xFF404040), |
| 61 8); | 42 8))); |
| 62 } | 43 } |
| 63 | 44 |
| 64 void onDraw(SkCanvas* canvas) override { | 45 void onDraw(SkCanvas* canvas) override { |
| 65 canvas->clear(SK_ColorBLACK); | 46 canvas->clear(SK_ColorBLACK); |
| 66 SkPaint paint; | 47 SkPaint paint; |
| 67 | 48 |
| 68 for (int i = 0; i < 4; i++) { | 49 for (int i = 0; i < 4; i++) { |
| 69 const SkBitmap* bitmap = (i & 0x01) ? &fCheckerboard : &fBitmap; | 50 const SkImage* image = (i & 0x01) ? fCheckerboard : fBitmap; |
| 70 SkIRect cropRect = SkIRect::MakeXYWH(i * 12, | 51 SkIRect cropRect = SkIRect::MakeXYWH(i * 12, |
| 71 i * 8, | 52 i * 8, |
| 72 bitmap->width() - i * 8, | 53 image->width() - i * 8, |
| 73 bitmap->height() - i * 12); | 54 image->height() - i * 12); |
| 74 SkImageFilter::CropRect rect(SkRect::Make(cropRect)); | 55 SkImageFilter::CropRect rect(SkRect::Make(cropRect)); |
| 75 SkAutoTUnref<SkImageFilter> tileInput(SkBitmapSource::Create(*bitmap
)); | 56 SkAutoTUnref<SkImageFilter> tileInput(SkImageSource::Create(image)); |
| 76 SkScalar dx = SkIntToScalar(i*5); | 57 SkScalar dx = SkIntToScalar(i*5); |
| 77 SkScalar dy = SkIntToScalar(i*10); | 58 SkScalar dy = SkIntToScalar(i*10); |
| 78 SkAutoTUnref<SkImageFilter> filter(SkOffsetImageFilter::Create(dx, d
y, tileInput, | 59 SkAutoTUnref<SkImageFilter> filter(SkOffsetImageFilter::Create(dx, d
y, tileInput, |
| 79 &rect
)); | 60 &rect
)); |
| 80 paint.setImageFilter(filter); | 61 paint.setImageFilter(filter); |
| 81 drawClippedBitmap(canvas, *bitmap, paint, 1, cropRect); | 62 DrawClippedImage(canvas, image, paint, 1, cropRect); |
| 82 canvas->translate(SkIntToScalar(bitmap->width() + MARGIN), 0); | 63 canvas->translate(SkIntToScalar(image->width() + MARGIN), 0); |
| 83 } | 64 } |
| 84 | 65 |
| 85 SkIRect cropRect = SkIRect::MakeXYWH(0, 0, 100, 100); | 66 SkIRect cropRect = SkIRect::MakeXYWH(0, 0, 100, 100); |
| 86 SkImageFilter::CropRect rect(SkRect::Make(cropRect)); | 67 SkImageFilter::CropRect rect(SkRect::Make(cropRect)); |
| 87 SkAutoTUnref<SkImageFilter> filter(SkOffsetImageFilter::Create(-5, -10,
nullptr, &rect)); | 68 SkAutoTUnref<SkImageFilter> filter(SkOffsetImageFilter::Create(-5, -10,
nullptr, &rect)); |
| 88 paint.setImageFilter(filter); | 69 paint.setImageFilter(filter); |
| 89 drawClippedBitmap(canvas, fBitmap, paint, 2, cropRect); | 70 DrawClippedImage(canvas, fBitmap, paint, 2, cropRect); |
| 90 } | 71 } |
| 91 private: | 72 private: |
| 92 SkBitmap fBitmap, fCheckerboard; | 73 static void DrawClippedImage(SkCanvas* canvas, const SkImage* image, const S
kPaint& paint, |
| 74 SkScalar scale, const SkIRect& cropRect) { |
| 75 SkRect clipRect = SkRect::MakeIWH(image->width(), image->height()); |
| 76 |
| 77 canvas->save(); |
| 78 canvas->clipRect(clipRect); |
| 79 canvas->scale(scale, scale); |
| 80 canvas->drawImage(image, 0, 0, &paint); |
| 81 canvas->restore(); |
| 82 |
| 83 // Draw a boundary rect around the intersection of the clip rect and cro
p rect. |
| 84 SkRect cropRectFloat; |
| 85 SkMatrix::MakeScale(scale, scale).mapRect(&cropRectFloat, SkRect::Make(c
ropRect)); |
| 86 if (clipRect.intersect(cropRectFloat)) { |
| 87 SkPaint strokePaint; |
| 88 strokePaint.setStyle(SkPaint::kStroke_Style); |
| 89 strokePaint.setStrokeWidth(2); |
| 90 strokePaint.setColor(SK_ColorRED); |
| 91 canvas->drawRect(clipRect, strokePaint); |
| 92 } |
| 93 } |
| 94 |
| 95 SkAutoTUnref<SkImage> fBitmap, fCheckerboard; |
| 93 | 96 |
| 94 typedef skiagm::GM INHERITED; | 97 typedef skiagm::GM INHERITED; |
| 95 }; | 98 }; |
| 96 DEF_GM( return new OffsetImageFilterGM; ) | 99 DEF_GM( return new OffsetImageFilterGM; ) |
| 97 | 100 |
| 98 ////////////////////////////////////////////////////////////////////////////// | 101 ////////////////////////////////////////////////////////////////////////////// |
| 99 | 102 |
| 100 class SimpleOffsetImageFilterGM : public skiagm::GM { | 103 class SimpleOffsetImageFilterGM : public skiagm::GM { |
| 101 public: | 104 public: |
| 102 SimpleOffsetImageFilterGM() {} | 105 SimpleOffsetImageFilterGM() {} |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 | 198 |
| 196 // crop==clip==dst | 199 // crop==clip==dst |
| 197 canvas->translate(100, 0); | 200 canvas->translate(100, 0); |
| 198 this->doDraw(canvas, r, SkOffsetImageFilter::Create(40, 0, nullptr, &cr2
), &r2); | 201 this->doDraw(canvas, r, SkOffsetImageFilter::Create(40, 0, nullptr, &cr2
), &r2); |
| 199 } | 202 } |
| 200 | 203 |
| 201 private: | 204 private: |
| 202 typedef skiagm::GM INHERITED; | 205 typedef skiagm::GM INHERITED; |
| 203 }; | 206 }; |
| 204 DEF_GM( return new SimpleOffsetImageFilterGM; ) | 207 DEF_GM( return new SimpleOffsetImageFilterGM; ) |
| OLD | NEW |