| 1 |
/* |
| 2 |
* Copyright 2013 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 "gm/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkImage.h" |
| 13 |
#include "include/core/SkImageFilter.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkRect.h" |
| 16 |
#include "include/core/SkRefCnt.h" |
| 17 |
#include "include/core/SkScalar.h" |
| 18 |
#include "include/core/SkSize.h" |
| 19 |
#include "include/core/SkString.h" |
| 20 |
#include "include/effects/SkImageFilters.h" |
| 21 |
#include "tools/ToolUtils.h" |
| 22 |
#include "tools/fonts/FontToolUtils.h" |
| 23 |
|
| 24 |
#include <utility> |
| 25 |
|
| 26 |
// This GM exercises the SkImageSource ImageFilter class. |
| 27 |
|
| 28 |
static void fill_rect_filtered(SkCanvas* canvas, |
| 29 |
const SkRect& clipRect, |
| 30 |
sk_sp<SkImageFilter> filter) { |
| 31 |
SkPaint paint; |
| 32 |
paint.setImageFilter(std::move(filter)); |
| 33 |
canvas->save(); |
| 34 |
canvas->clipRect(clipRect); |
| 35 |
canvas->drawPaint(paint); |
| 36 |
canvas->restore(); |
| 37 |
} |
| 38 |
|
| 39 |
class ImageSourceGM : public skiagm::GM { |
| 40 |
public: |
| 41 |
ImageSourceGM() { } |
| 42 |
|
| 43 |
protected: |
| 44 |
SkString getName() const override { return SkString("imagesource"); } |
| 45 |
|
| 46 |
SkISize getISize() override { return SkISize::Make(500, 150); } |
| 47 |
|
| 48 |
void onOnceBeforeDraw() override { |
| 49 |
fImage = ToolUtils::CreateStringImage(100, 100, 0xFFFFFFFF, 20, 70, 96, "e"); |
| 50 |
} |
| 51 |
|
| 52 |
void onDraw(SkCanvas* canvas) override { |
| 53 |
canvas->clear(SK_ColorBLACK); |
| 54 |
|
| 55 |
const SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30); |
| 56 |
const SkRect dstRect = SkRect::MakeXYWH(0, 10, 60, 60); |
| 57 |
const SkRect clipRect = SkRect::MakeXYWH(0, 0, 100, 100); |
| 58 |
const SkRect bounds = SkRect::MakeIWH(fImage->width(), fImage->height()); |
| 59 |
const SkSamplingOptions sampling({1/3.0f, 1/3.0f}); |
| 60 |
|
| 61 |
{ |
| 62 |
// Draw an unscaled bitmap. |
| 63 |
sk_sp<SkImageFilter> imageSource(SkImageFilters::Image(fImage, SkFilterMode::kNearest)); |
| 64 |
fill_rect_filtered(canvas, clipRect, std::move(imageSource)); |
| 65 |
canvas->translate(SkIntToScalar(100), 0); |
| 66 |
} |
| 67 |
{ |
| 68 |
// Draw an unscaled subset of the source bitmap (srcRect -> srcRect). |
| 69 |
sk_sp<SkImageFilter> imageSourceSrcRect( |
| 70 |
SkImageFilters::Image(fImage, srcRect, srcRect, sampling)); |
| 71 |
fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRect)); |
| 72 |
canvas->translate(SkIntToScalar(100), 0); |
| 73 |
} |
| 74 |
{ |
| 75 |
// Draw a subset of the bitmap scaled to a destination rect (srcRect -> dstRect). |
| 76 |
sk_sp<SkImageFilter> imageSourceSrcRectDstRect( |
| 77 |
SkImageFilters::Image(fImage, srcRect, dstRect, sampling)); |
| 78 |
fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRectDstRect)); |
| 79 |
canvas->translate(SkIntToScalar(100), 0); |
| 80 |
} |
| 81 |
{ |
| 82 |
// Draw the entire bitmap scaled to a destination rect (bounds -> dstRect). |
| 83 |
sk_sp<SkImageFilter> imageSourceDstRectOnly( |
| 84 |
SkImageFilters::Image(fImage, bounds, dstRect, sampling)); |
| 85 |
fill_rect_filtered(canvas, clipRect, std::move(imageSourceDstRectOnly)); |
| 86 |
canvas->translate(SkIntToScalar(100), 0); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
private: |
| 91 |
sk_sp<SkImage> fImage; |
| 92 |
using INHERITED = GM; |
| 93 |
}; |
| 94 |
|
| 95 |
/////////////////////////////////////////////////////////////////////////////// |
| 96 |
|
| 97 |
DEF_GM( return new ImageSourceGM; ) |