2013-01-15 15:40:19 +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.
|
|
|
|
*/
|
|
|
|
|
2015-01-26 19:24:32 +00:00
|
|
|
#include "SkDisplacementMapEffect.h"
|
2015-09-15 18:26:13 +00:00
|
|
|
#include "SkImage.h"
|
|
|
|
#include "SkImageSource.h"
|
2015-01-26 19:24:32 +00:00
|
|
|
#include "gm.h"
|
2015-03-23 12:46:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2013-01-15 15:40:19 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class DisplacementMapGM : public GM {
|
|
|
|
public:
|
2015-09-03 20:32:33 +00:00
|
|
|
DisplacementMapGM() {
|
2013-01-15 15:40:19 +00:00
|
|
|
this->setBGColor(0xFF000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-09-03 20:32:33 +00:00
|
|
|
SkString onShortName() override {
|
2013-01-15 15:40:19 +00:00
|
|
|
return SkString("displacement");
|
|
|
|
}
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
fBitmap = sk_tool_utils::create_string_bitmap(80, 80, 0xFF884422, 15, 55, 96, "g");
|
2013-12-12 21:48:32 +00:00
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
SkColor c1 = sk_tool_utils::color_to_565(0xFF244484);
|
|
|
|
SkColor c2 = sk_tool_utils::color_to_565(0xFF804020);
|
2013-01-15 15:40:19 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
fCheckerboard = SkImage::MakeFromBitmap(
|
|
|
|
sk_tool_utils::create_checkerboard_bitmap(80, 80, c1, c2, 8));
|
|
|
|
fSmall = SkImage::MakeFromBitmap(
|
|
|
|
sk_tool_utils::create_checkerboard_bitmap(64, 64, c1, c2, 8));
|
|
|
|
fLarge = SkImage::MakeFromBitmap(
|
|
|
|
sk_tool_utils::create_checkerboard_bitmap(96, 96, c1, c2, 8));
|
|
|
|
fLargeW =
|
|
|
|
SkImage::MakeFromBitmap(sk_tool_utils::create_checkerboard_bitmap(96, 64, c1, c2, 8));
|
|
|
|
fLargeH = SkImage::MakeFromBitmap(
|
|
|
|
sk_tool_utils::create_checkerboard_bitmap(64, 96, c1, c2, 8));
|
2013-01-15 15:40:19 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
SkISize onISize() override {
|
2016-01-21 17:55:47 +00:00
|
|
|
return SkISize::Make(600, 500);
|
2013-01-15 15:40:19 +00:00
|
|
|
}
|
2013-01-16 07:05:56 +00:00
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) const {
|
2013-01-15 15:40:19 +00:00
|
|
|
canvas->save();
|
2013-10-15 19:30:58 +00:00
|
|
|
canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
|
2013-10-15 19:02:43 +00:00
|
|
|
canvas->clipRect(SkRect::MakeWH(SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
|
|
|
|
canvas->drawBitmap(fBitmap, 0, 0, &paint);
|
2013-01-15 15:40:19 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2015-04-09 18:13:24 +00:00
|
|
|
canvas->clear(SK_ColorBLACK);
|
2013-01-15 15:40:19 +00:00
|
|
|
SkPaint paint;
|
2016-04-01 16:28:51 +00:00
|
|
|
sk_sp<SkImageFilter> displ(SkImageSource::Make(fCheckerboard));
|
2016-04-15 14:17:36 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ, nullptr));
|
|
|
|
this->drawClippedBitmap(canvas, 0, 0, paint);
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ, nullptr));
|
|
|
|
this->drawClippedBitmap(canvas, 100, 0, paint);
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ, nullptr));
|
|
|
|
this->drawClippedBitmap(canvas, 200, 0, paint);
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType, 48.0f, displ, nullptr));
|
|
|
|
this->drawClippedBitmap(canvas, 300, 0, paint);
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType, 64.0f, displ, nullptr));
|
|
|
|
this->drawClippedBitmap(canvas, 400, 0, paint);
|
2013-01-15 15:40:19 +00:00
|
|
|
|
2016-04-15 14:17:36 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType, 40.0f, displ, nullptr));
|
|
|
|
this->drawClippedBitmap(canvas, 0, 100, paint);
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ, nullptr));
|
|
|
|
this->drawClippedBitmap(canvas, 100, 100, paint);
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType, 40.0f, displ, nullptr));
|
|
|
|
this->drawClippedBitmap(canvas, 200, 100, paint);
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ, nullptr));
|
|
|
|
this->drawClippedBitmap(canvas, 300, 100, paint);
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ, nullptr));
|
|
|
|
this->drawClippedBitmap(canvas, 400, 100, paint);
|
2013-10-15 19:02:43 +00:00
|
|
|
|
|
|
|
SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(30, 30, 40, 40));
|
2016-04-15 14:17:36 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
0.0f, displ, nullptr, &cropRect));
|
|
|
|
this->drawClippedBitmap(canvas, 0, 200, paint);
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
16.0f, displ, nullptr, &cropRect));
|
|
|
|
this->drawClippedBitmap(canvas, 100, 200, paint);
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
32.0f, displ, nullptr, &cropRect));
|
|
|
|
this->drawClippedBitmap(canvas, 200, 200, paint);
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
48.0f, displ, nullptr, &cropRect));
|
|
|
|
this->drawClippedBitmap(canvas, 300, 200, paint);
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
64.0f, displ, nullptr, &cropRect));
|
|
|
|
this->drawClippedBitmap(canvas, 400, 200, paint);
|
2013-10-15 19:02:43 +00:00
|
|
|
|
2016-04-15 14:17:36 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
40.0f, displ, nullptr, &cropRect));
|
|
|
|
this->drawClippedBitmap(canvas, 0, 300, paint);
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
40.0f, displ, nullptr, &cropRect));
|
|
|
|
this->drawClippedBitmap(canvas, 100, 300, paint);
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
40.0f, displ, nullptr, &cropRect));
|
|
|
|
this->drawClippedBitmap(canvas, 200, 300, paint);
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
40.0f, displ, nullptr, &cropRect));
|
|
|
|
this->drawClippedBitmap(canvas, 300, 300, paint);
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
40.0f, displ, nullptr, &cropRect));
|
|
|
|
this->drawClippedBitmap(canvas, 400, 300, paint);
|
2013-12-12 21:48:32 +00:00
|
|
|
|
2016-01-21 17:55:47 +00:00
|
|
|
// Test for negative scale.
|
2016-04-15 14:17:36 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2016-01-21 17:55:47 +00:00
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
-40.0f, displ, nullptr));
|
2016-01-21 17:55:47 +00:00
|
|
|
this->drawClippedBitmap(canvas, 500, 0, paint);
|
|
|
|
|
2013-12-19 17:00:46 +00:00
|
|
|
// Tests for images of different sizes
|
2016-04-01 16:28:51 +00:00
|
|
|
displ = SkImageSource::Make(fSmall);
|
2016-04-15 14:17:36 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
40.0f, std::move(displ), nullptr));
|
|
|
|
this->drawClippedBitmap(canvas, 0, 400, paint);
|
2016-04-01 16:28:51 +00:00
|
|
|
displ = SkImageSource::Make(fLarge);
|
2016-04-15 14:17:36 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
40.0f, std::move(displ), nullptr));
|
|
|
|
this->drawClippedBitmap(canvas, 100, 400, paint);
|
2016-04-01 16:28:51 +00:00
|
|
|
displ = SkImageSource::Make(fLargeW);
|
2016-04-15 14:17:36 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
40.0f, std::move(displ), nullptr));
|
|
|
|
this->drawClippedBitmap(canvas, 200, 400, paint);
|
2016-04-01 16:28:51 +00:00
|
|
|
displ = SkImageSource::Make(fLargeH);
|
2016-04-15 14:17:36 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
40.0f, std::move(displ), nullptr));
|
|
|
|
this->drawClippedBitmap(canvas, 300, 400, paint);
|
2013-12-19 17:00:46 +00:00
|
|
|
|
|
|
|
// Test for no given displacement input. In this case, both displacement
|
|
|
|
// and color should use the same bitmap, given to SkCanvas::drawBitmap()
|
|
|
|
// as an input argument.
|
2016-04-15 14:17:36 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Make(
|
2014-03-10 10:51:58 +00:00
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
2016-04-15 14:17:36 +00:00
|
|
|
40.0f, nullptr, nullptr));
|
|
|
|
this->drawClippedBitmap(canvas, 400, 400, paint);
|
2013-01-15 15:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-09-15 18:26:13 +00:00
|
|
|
SkBitmap fBitmap;
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fCheckerboard, fSmall, fLarge, fLargeW, fLargeH;
|
2015-09-03 20:32:33 +00:00
|
|
|
|
|
|
|
typedef GM INHERITED;
|
2013-01-15 15:40:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-09-03 20:32:33 +00:00
|
|
|
DEF_GM(return new DisplacementMapGM;)
|
2013-01-15 15:40:19 +00:00
|
|
|
}
|