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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
#include "SkDisplacementMapEffect.h"
|
|
|
|
#include "SkBitmapSource.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class DisplacementMapGM : public GM {
|
|
|
|
public:
|
|
|
|
DisplacementMapGM() : fInitialized(false) {
|
|
|
|
this->setBGColor(0xFF000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual SkString onShortName() {
|
|
|
|
return SkString("displacement");
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:48:32 +00:00
|
|
|
void make_bitmaps() {
|
|
|
|
make_bitmap();
|
|
|
|
make_checkerboard(&fCheckerboard, 80, 80);
|
|
|
|
make_checkerboard(&fSmall, 64, 64);
|
|
|
|
make_checkerboard(&fLarge, 96, 96);
|
|
|
|
make_checkerboard(&fLargeW, 96, 64);
|
|
|
|
make_checkerboard(&fLargeH, 64, 96);
|
|
|
|
}
|
|
|
|
|
2013-01-15 15:40:19 +00:00
|
|
|
void make_bitmap() {
|
2014-01-25 16:46:20 +00:00
|
|
|
fBitmap.allocN32Pixels(80, 80);
|
2014-02-13 17:14:46 +00:00
|
|
|
SkCanvas canvas(fBitmap);
|
2013-01-15 15:40:19 +00:00
|
|
|
canvas.clear(0x00000000);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2014-06-23 18:25:00 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
2013-01-15 15:40:19 +00:00
|
|
|
paint.setColor(0xFF884422);
|
|
|
|
paint.setTextSize(SkIntToScalar(96));
|
|
|
|
const char* str = "g";
|
|
|
|
canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), paint);
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:48:32 +00:00
|
|
|
void make_checkerboard(SkBitmap* bitmap, int w, int h) {
|
2014-01-25 16:46:20 +00:00
|
|
|
bitmap->allocN32Pixels(w, h);
|
2014-02-13 17:14:46 +00:00
|
|
|
SkCanvas canvas(*bitmap);
|
2013-01-15 15:40:19 +00:00
|
|
|
canvas.clear(0x00000000);
|
|
|
|
SkPaint darkPaint;
|
|
|
|
darkPaint.setColor(0xFF804020);
|
|
|
|
SkPaint lightPaint;
|
|
|
|
lightPaint.setColor(0xFF244484);
|
2013-12-12 21:48:32 +00:00
|
|
|
for (int y = 0; y < h; y += 16) {
|
|
|
|
for (int x = 0; x < w; x += 16) {
|
2013-01-15 15:40:19 +00:00
|
|
|
canvas.save();
|
|
|
|
canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
|
|
|
|
canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
|
|
|
|
canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
|
|
|
|
canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
|
|
|
|
canvas.restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkISize onISize() {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(500, 500);
|
2013-01-15 15:40:19 +00:00
|
|
|
}
|
2013-01-16 07:05:56 +00:00
|
|
|
|
2013-01-15 15:40:19 +00:00
|
|
|
void drawClippedBitmap(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
if (!fInitialized) {
|
2013-12-12 21:48:32 +00:00
|
|
|
make_bitmaps();
|
2013-01-15 15:40:19 +00:00
|
|
|
fInitialized = true;
|
|
|
|
}
|
|
|
|
canvas->clear(0x00000000);
|
|
|
|
SkPaint paint;
|
2014-03-10 10:51:58 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> displ(SkBitmapSource::Create(fCheckerboard));
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType, 0.0f, displ))->unref();
|
2013-01-15 15:40:19 +00:00
|
|
|
drawClippedBitmap(canvas, 0, 0, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType, 16.0f, displ))->unref();
|
2013-01-15 15:40:19 +00:00
|
|
|
drawClippedBitmap(canvas, 100, 0, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType, 32.0f, displ))->unref();
|
2013-01-15 15:40:19 +00:00
|
|
|
drawClippedBitmap(canvas, 200, 0, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType, 48.0f, displ))->unref();
|
2013-01-15 15:40:19 +00:00
|
|
|
drawClippedBitmap(canvas, 300, 0, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType, 64.0f, displ))->unref();
|
2013-01-15 15:40:19 +00:00
|
|
|
drawClippedBitmap(canvas, 400, 0, paint);
|
|
|
|
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType, 40.0f, displ))->unref();
|
2013-01-15 15:40:19 +00:00
|
|
|
drawClippedBitmap(canvas, 0, 100, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ))->unref();
|
2013-01-15 15:40:19 +00:00
|
|
|
drawClippedBitmap(canvas, 100, 100, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType, 40.0f, displ))->unref();
|
2013-01-15 15:40:19 +00:00
|
|
|
drawClippedBitmap(canvas, 200, 100, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ))->unref();
|
2013-01-15 15:40:19 +00:00
|
|
|
drawClippedBitmap(canvas, 300, 100, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType, 40.0f, displ))->unref();
|
2013-01-15 15:40:19 +00:00
|
|
|
drawClippedBitmap(canvas, 400, 100, paint);
|
2013-10-15 19:02:43 +00:00
|
|
|
|
|
|
|
SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(30, 30, 40, 40));
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
|
|
|
0.0f, displ, NULL, &cropRect))->unref();
|
2013-10-15 19:02:43 +00:00
|
|
|
drawClippedBitmap(canvas, 0, 200, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
|
|
|
16.0f, displ, NULL, &cropRect))->unref();
|
2013-10-15 19:02:43 +00:00
|
|
|
drawClippedBitmap(canvas, 100, 200, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType,
|
|
|
|
32.0f, displ, NULL, &cropRect))->unref();
|
2013-10-15 19:02:43 +00:00
|
|
|
drawClippedBitmap(canvas, 200, 200, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
|
|
|
48.0f, displ, NULL, &cropRect))->unref();
|
2013-10-15 19:02:43 +00:00
|
|
|
drawClippedBitmap(canvas, 300, 200, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
|
|
|
64.0f, displ, NULL, &cropRect))->unref();
|
2013-10-15 19:02:43 +00:00
|
|
|
drawClippedBitmap(canvas, 400, 200, paint);
|
|
|
|
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
|
|
|
40.0f, displ, NULL, &cropRect))->unref();
|
2013-10-15 19:02:43 +00:00
|
|
|
drawClippedBitmap(canvas, 0, 300, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
|
|
|
40.0f, displ, NULL, &cropRect))->unref();
|
2013-10-15 19:02:43 +00:00
|
|
|
drawClippedBitmap(canvas, 100, 300, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType,
|
|
|
|
40.0f, displ, NULL, &cropRect))->unref();
|
2013-10-15 19:02:43 +00:00
|
|
|
drawClippedBitmap(canvas, 200, 300, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
|
|
|
40.0f, displ, NULL, &cropRect))->unref();
|
2013-10-15 19:02:43 +00:00
|
|
|
drawClippedBitmap(canvas, 300, 300, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
|
|
|
40.0f, displ, NULL, &cropRect))->unref();
|
2013-10-15 19:02:43 +00:00
|
|
|
drawClippedBitmap(canvas, 400, 300, paint);
|
2013-12-12 21:48:32 +00:00
|
|
|
|
2013-12-19 17:00:46 +00:00
|
|
|
// Tests for images of different sizes
|
2014-03-10 10:51:58 +00:00
|
|
|
displ.reset(SkBitmapSource::Create(fSmall));
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
|
|
|
40.0f, displ))->unref();
|
2013-12-12 21:48:32 +00:00
|
|
|
drawClippedBitmap(canvas, 0, 400, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
displ.reset(SkBitmapSource::Create(fLarge));
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
|
|
|
40.0f, displ))->unref();
|
2013-12-12 21:48:32 +00:00
|
|
|
drawClippedBitmap(canvas, 100, 400, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
displ.reset(SkBitmapSource::Create(fLargeW));
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kR_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kB_ChannelSelectorType,
|
|
|
|
40.0f, displ))->unref();
|
2013-12-12 21:48:32 +00:00
|
|
|
drawClippedBitmap(canvas, 200, 400, paint);
|
2014-03-10 10:51:58 +00:00
|
|
|
displ.reset(SkBitmapSource::Create(fLargeH));
|
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
|
|
|
40.0f, displ))->unref();
|
2013-12-12 21:48:32 +00:00
|
|
|
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.
|
2014-03-10 10:51:58 +00:00
|
|
|
paint.setImageFilter(SkDisplacementMapEffect::Create(
|
|
|
|
SkDisplacementMapEffect::kG_ChannelSelectorType,
|
|
|
|
SkDisplacementMapEffect::kA_ChannelSelectorType,
|
|
|
|
40.0f, NULL))->unref();
|
2013-12-19 17:00:46 +00:00
|
|
|
drawClippedBitmap(canvas, 400, 400, paint);
|
2013-01-15 15:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
2013-12-12 21:48:32 +00:00
|
|
|
SkBitmap fBitmap, fCheckerboard, fSmall, fLarge, fLargeW, fLargeH;
|
2013-01-15 15:40:19 +00:00
|
|
|
bool fInitialized;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new DisplacementMapGM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|