2015-09-14 20:31:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageFilter.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-08-02 19:21:23 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
|
|
|
|
|
|
|
#include <utility>
|
2015-09-14 20:31:18 +00:00
|
|
|
|
|
|
|
// This GM exercises the SkImageSource ImageFilter class.
|
|
|
|
|
2016-04-01 16:28:51 +00:00
|
|
|
static void fill_rect_filtered(SkCanvas* canvas,
|
|
|
|
const SkRect& clipRect,
|
|
|
|
sk_sp<SkImageFilter> filter) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(std::move(filter));
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(clipRect);
|
|
|
|
canvas->drawPaint(paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2015-09-14 20:31:18 +00:00
|
|
|
class ImageSourceGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
ImageSourceGM() { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("imagesource");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override { return SkISize::Make(500, 150); }
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
2020-12-23 15:11:33 +00:00
|
|
|
fImage = ToolUtils::create_string_image(100, 100, 0xFFFFFFFF, 20, 70, 96, "e");
|
2015-09-14 20:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
canvas->clear(SK_ColorBLACK);
|
|
|
|
|
2016-04-01 16:28:51 +00:00
|
|
|
const SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30);
|
|
|
|
const SkRect dstRect = SkRect::MakeXYWH(0, 10, 60, 60);
|
|
|
|
const SkRect clipRect = SkRect::MakeXYWH(0, 0, 100, 100);
|
|
|
|
const SkRect bounds = SkRect::MakeIWH(fImage->width(), fImage->height());
|
2021-02-14 02:04:58 +00:00
|
|
|
const SkSamplingOptions sampling({1/3.0f, 1/3.0f});
|
2016-04-01 16:28:51 +00:00
|
|
|
|
|
|
|
{
|
2015-09-14 20:31:18 +00:00
|
|
|
// Draw an unscaled bitmap.
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> imageSource(SkImageFilters::Image(fImage));
|
2016-04-01 16:28:51 +00:00
|
|
|
fill_rect_filtered(canvas, clipRect, std::move(imageSource));
|
2015-09-14 20:31:18 +00:00
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
2016-04-01 16:28:51 +00:00
|
|
|
}
|
|
|
|
{
|
2015-09-14 20:31:18 +00:00
|
|
|
// Draw an unscaled subset of the source bitmap (srcRect -> srcRect).
|
2016-04-01 16:28:51 +00:00
|
|
|
sk_sp<SkImageFilter> imageSourceSrcRect(
|
2021-02-14 02:04:58 +00:00
|
|
|
SkImageFilters::Image(fImage, srcRect, srcRect, sampling));
|
2016-04-01 16:28:51 +00:00
|
|
|
fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRect));
|
2015-09-14 20:31:18 +00:00
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
2016-04-01 16:28:51 +00:00
|
|
|
}
|
|
|
|
{
|
2015-09-14 20:31:18 +00:00
|
|
|
// Draw a subset of the bitmap scaled to a destination rect (srcRect -> dstRect).
|
2016-04-01 16:28:51 +00:00
|
|
|
sk_sp<SkImageFilter> imageSourceSrcRectDstRect(
|
2021-02-14 02:04:58 +00:00
|
|
|
SkImageFilters::Image(fImage, srcRect, dstRect, sampling));
|
2016-04-01 16:28:51 +00:00
|
|
|
fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRectDstRect));
|
2015-09-14 20:31:18 +00:00
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
2016-04-01 16:28:51 +00:00
|
|
|
}
|
|
|
|
{
|
2015-09-14 20:31:18 +00:00
|
|
|
// Draw the entire bitmap scaled to a destination rect (bounds -> dstRect).
|
2016-04-01 16:28:51 +00:00
|
|
|
sk_sp<SkImageFilter> imageSourceDstRectOnly(
|
2021-02-14 02:04:58 +00:00
|
|
|
SkImageFilters::Image(fImage, bounds, dstRect, sampling));
|
2016-04-01 16:28:51 +00:00
|
|
|
fill_rect_filtered(canvas, clipRect, std::move(imageSourceDstRectOnly));
|
2015-09-14 20:31:18 +00:00
|
|
|
canvas->translate(SkIntToScalar(100), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fImage;
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2015-09-14 20:31:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM( return new ImageSourceGM; )
|