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.
|
|
|
|
*/
|
|
|
|
|
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"
|
|
|
|
#include "include/core/SkColorSpace.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkImageFilter.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkRegion.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-08-02 19:21:23 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2014-01-06 23:41:14 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2014-01-06 23:41:14 +00:00
|
|
|
#define WIDTH 500
|
|
|
|
#define HEIGHT 500
|
|
|
|
|
2016-12-19 21:51:53 +00:00
|
|
|
static void draw_rects(SkCanvas* canvas) {
|
2016-01-19 16:50:18 +00:00
|
|
|
SkPaint rectPaint;
|
2016-12-19 21:51:53 +00:00
|
|
|
rectPaint.setColor(SK_ColorBLUE);
|
2016-01-19 16:50:18 +00:00
|
|
|
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);
|
2016-12-19 21:51:53 +00:00
|
|
|
rectPaint.setColor(SK_ColorTRANSPARENT);
|
2016-01-19 16:50:18 +00:00
|
|
|
canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
|
|
|
|
}
|
|
|
|
|
2019-08-02 19:21:23 +00:00
|
|
|
static SkPaint create_filter_paint(SkIRect* 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;
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> offset(SkImageFilters::Offset(25, 25, nullptr));
|
|
|
|
paint.setImageFilter(
|
|
|
|
SkImageFilters::AlphaThreshold(region, 0.2f, 0.7f, std::move(offset), cropRect));
|
2016-01-19 16:50:18 +00:00
|
|
|
return paint;
|
|
|
|
}
|
|
|
|
|
2016-12-19 21:51:53 +00:00
|
|
|
class ImageAlphaThresholdGM : public skiagm::GM {
|
2014-01-06 23:41:14 +00:00
|
|
|
public:
|
2016-10-03 15:06:45 +00:00
|
|
|
ImageAlphaThresholdGM(bool useCropRect) : fUseCropRect(useCropRect) {
|
2016-12-19 21:51:53 +00:00
|
|
|
this->setBGColor(SK_ColorWHITE);
|
2014-01-06 23:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-08-02 19:21:23 +00:00
|
|
|
SkIRect cropRect = SkIRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100);
|
2016-04-12 18:02:25 +00:00
|
|
|
|
|
|
|
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-12-19 21:51:53 +00:00
|
|
|
// Create a 'width' x 'height' SkSurface that matches the colorType of 'canvas' as
|
|
|
|
// best we can
|
|
|
|
static sk_sp<SkSurface> make_color_matching_surface(SkCanvas* canvas, int width, int height,
|
2018-02-09 22:35:29 +00:00
|
|
|
SkAlphaType at) {
|
2016-12-19 21:51:53 +00:00
|
|
|
|
|
|
|
SkColorType ct = canvas->imageInfo().colorType();
|
2017-01-12 15:13:40 +00:00
|
|
|
sk_sp<SkColorSpace> cs(canvas->imageInfo().refColorSpace());
|
2016-12-19 21:51:53 +00:00
|
|
|
|
|
|
|
if (kUnknown_SkColorType == ct) {
|
|
|
|
// For backends that aren't yet color-space aware we just fallback to N32.
|
|
|
|
ct = kN32_SkColorType;
|
|
|
|
cs = nullptr;
|
2018-02-09 22:35:29 +00:00
|
|
|
} else if (SkColorTypeIsAlwaysOpaque(ct)) {
|
|
|
|
at = kOpaque_SkAlphaType;
|
2016-12-19 21:51:53 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 22:35:29 +00:00
|
|
|
SkImageInfo info = SkImageInfo::Make(width, height, ct, at, std::move(cs));
|
2016-12-19 21:51:53 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
return ToolUtils::makeSurface(canvas, info);
|
2016-12-19 21:51:53 +00:00
|
|
|
}
|
2016-04-12 18:02:25 +00:00
|
|
|
|
2016-12-19 21:51:53 +00:00
|
|
|
class ImageAlphaThresholdSurfaceGM : public skiagm::GM {
|
2016-01-19 16:50:18 +00:00
|
|
|
public:
|
|
|
|
ImageAlphaThresholdSurfaceGM() {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("imagealphathreshold_surface");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(WIDTH, HEIGHT);
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
|
2016-12-19 21:51:53 +00:00
|
|
|
SkMatrix matrix;
|
|
|
|
matrix.reset();
|
|
|
|
matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
|
|
|
|
matrix.postScale(.8f, .8f);
|
|
|
|
|
|
|
|
canvas->concat(matrix);
|
|
|
|
|
|
|
|
sk_sp<SkSurface> surface(make_color_matching_surface(canvas, WIDTH, HEIGHT,
|
|
|
|
kPremul_SkAlphaType));
|
2016-12-21 00:05:09 +00:00
|
|
|
if (!surface) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "make_color_matching_surface failed";
|
|
|
|
return DrawResult::kFail;
|
2016-12-21 00:05:09 +00:00
|
|
|
}
|
2016-12-19 21:51:53 +00:00
|
|
|
|
|
|
|
surface->getCanvas()->clear(SK_ColorTRANSPARENT);
|
2016-01-19 16:50:18 +00:00
|
|
|
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);
|
2019-02-07 23:20:09 +00:00
|
|
|
return DrawResult::kOk;
|
2016-01-19 16:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-12-19 21:51:53 +00:00
|
|
|
typedef skiagm::GM INHERITED;
|
2016-01-19 16:50:18 +00:00
|
|
|
};
|
|
|
|
|
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();)
|
2017-10-19 16:54:28 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static sk_sp<SkImage> make_img() {
|
|
|
|
SkBitmap bitmap;
|
|
|
|
bitmap.allocPixels(SkImageInfo::MakeS32(WIDTH, HEIGHT, kPremul_SkAlphaType));
|
|
|
|
SkCanvas canvas(bitmap);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
SkRect rect = SkRect::MakeWH(WIDTH, HEIGHT);
|
|
|
|
SkRandom rnd;
|
|
|
|
|
|
|
|
while (!rect.isEmpty()) {
|
|
|
|
paint.setColor(rnd.nextU() | (0xFF << 24));
|
|
|
|
canvas.drawRect(rect, paint);
|
|
|
|
rect.inset(25, 25);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SkImage::MakeFromBitmap(bitmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM_BG(imagealphathreshold_image, canvas, WIDTH * 2, HEIGHT, SK_ColorBLACK) {
|
|
|
|
sk_sp<SkImage> image(make_img());
|
|
|
|
|
|
|
|
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 filterPaint;
|
2019-08-02 19:21:23 +00:00
|
|
|
sk_sp<SkImageFilter> imageSource(SkImageFilters::Image(image));
|
|
|
|
filterPaint.setImageFilter(SkImageFilters::AlphaThreshold(region, 0.2f, 0.7f,
|
|
|
|
std::move(imageSource)));
|
2017-10-19 16:54:28 +00:00
|
|
|
|
|
|
|
canvas->saveLayer(nullptr, &filterPaint);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(WIDTH, 0);
|
|
|
|
canvas->drawImage(image, 0, 0);
|
|
|
|
}
|