2014-01-06 23:41:14 +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 "SkAlphaThresholdFilter.h"
|
|
|
|
#include "SkRandom.h"
|
2016-01-19 16:50:18 +00:00
|
|
|
#include "SkSurface.h"
|
2014-01-06 23:41:14 +00:00
|
|
|
|
|
|
|
#define WIDTH 500
|
|
|
|
#define HEIGHT 500
|
|
|
|
|
2016-01-19 16:50:18 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void draw_rects(SkCanvas* canvas) {
|
|
|
|
SkPaint rectPaint;
|
|
|
|
rectPaint.setColor(0xFF0000FF);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(0, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
|
|
|
|
rectPaint.setColor(0xBFFF0000);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
|
|
|
|
rectPaint.setColor(0x3F00FF00);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(0, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
|
|
|
|
rectPaint.setColor(0x00000000);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
|
|
|
|
}
|
|
|
|
|
2016-04-12 18:02:25 +00:00
|
|
|
SkPaint create_filter_paint(SkImageFilter::CropRect* cropRect = nullptr) {
|
2016-01-19 16:50:18 +00:00
|
|
|
SkIRect rects[2];
|
|
|
|
rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300);
|
|
|
|
rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT);
|
|
|
|
SkRegion region;
|
|
|
|
region.setRects(rects, 2);
|
|
|
|
|
|
|
|
SkPaint paint;
|
2016-04-12 18:02:25 +00:00
|
|
|
paint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f, nullptr, cropRect));
|
2016-01-19 16:50:18 +00:00
|
|
|
return paint;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-01-06 23:41:14 +00:00
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class ImageAlphaThresholdGM : public GM {
|
|
|
|
public:
|
2016-04-12 18:02:25 +00:00
|
|
|
ImageAlphaThresholdGM(bool useCropRect) : fUseCropRect(true) {
|
2014-01-06 23:41:14 +00:00
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2016-04-12 18:02:25 +00:00
|
|
|
if (fUseCropRect) {
|
|
|
|
return SkString("imagealphathreshold_crop");
|
|
|
|
}
|
|
|
|
|
2014-01-06 23:41:14 +00:00
|
|
|
return SkString("imagealphathreshold");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(WIDTH, HEIGHT);
|
2014-01-06 23:41:14 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-01-06 23:41:14 +00:00
|
|
|
SkMatrix matrix;
|
|
|
|
matrix.reset();
|
|
|
|
matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
|
2014-01-07 16:04:01 +00:00
|
|
|
matrix.postScale(.8f, .8f);
|
2014-01-06 23:41:14 +00:00
|
|
|
|
|
|
|
canvas->concat(matrix);
|
|
|
|
|
2016-04-12 18:02:25 +00:00
|
|
|
SkRect r = SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100);
|
|
|
|
SkImageFilter::CropRect cropRect(r);
|
|
|
|
|
|
|
|
SkPaint paint = create_filter_paint(fUseCropRect ? &cropRect : nullptr);
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->saveLayer(nullptr, &paint);
|
2016-01-19 16:50:18 +00:00
|
|
|
draw_rects(canvas);
|
2014-01-06 23:41:14 +00:00
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-04-12 18:02:25 +00:00
|
|
|
bool fUseCropRect;
|
|
|
|
|
2014-01-06 23:41:14 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2016-04-12 18:02:25 +00:00
|
|
|
|
2016-01-19 16:50:18 +00:00
|
|
|
class ImageAlphaThresholdSurfaceGM : public GM {
|
|
|
|
public:
|
|
|
|
ImageAlphaThresholdSurfaceGM() {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("imagealphathreshold_surface");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(WIDTH, HEIGHT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32(WIDTH, HEIGHT, kOpaque_SkAlphaType);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(canvas->makeSurface(info));
|
2016-01-19 16:50:18 +00:00
|
|
|
if (nullptr == surface) {
|
2016-03-24 01:59:25 +00:00
|
|
|
surface = SkSurface::MakeRaster(info);
|
2016-01-19 16:50:18 +00:00
|
|
|
}
|
|
|
|
surface->getCanvas()->clear(SK_ColorWHITE);
|
|
|
|
draw_rects(surface->getCanvas());
|
|
|
|
|
|
|
|
SkPaint paint = create_filter_paint();
|
|
|
|
canvas->clipRect(SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100));
|
2016-03-17 17:51:11 +00:00
|
|
|
canvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, &paint);
|
2016-01-19 16:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2014-01-06 23:41:14 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-04-12 18:02:25 +00:00
|
|
|
DEF_GM(return new ImageAlphaThresholdGM(true);)
|
|
|
|
DEF_GM(return new ImageAlphaThresholdGM(false);)
|
2016-01-19 16:50:18 +00:00
|
|
|
DEF_GM(return new ImageAlphaThresholdSurfaceGM();)
|
2014-01-06 23:41:14 +00:00
|
|
|
|
|
|
|
}
|