2013-07-24 22:19:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 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 "SkCanvas.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
|
|
|
|
#include "SkBlurImageFilter.h"
|
|
|
|
#include "SkColorFilterImageFilter.h"
|
2013-09-19 17:26:47 +00:00
|
|
|
#include "SkMergeImageFilter.h"
|
|
|
|
#include "SkOffsetImageFilter.h"
|
2013-07-24 22:19:24 +00:00
|
|
|
#include "SkTestImageFilters.h"
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static void draw_paint(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(imf);
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(r);
|
|
|
|
canvas->drawPaint(paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_path(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorMAGENTA);
|
|
|
|
paint.setImageFilter(imf);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas->drawCircle(r.centerX(), r.centerY(), r.width()*2/5, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_text(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setImageFilter(imf);
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
paint.setAntiAlias(true);
|
2014-07-31 12:58:44 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
2013-07-24 22:19:24 +00:00
|
|
|
paint.setTextSize(r.height()/2);
|
|
|
|
paint.setTextAlign(SkPaint::kCenter_Align);
|
|
|
|
canvas->drawText("Text", 4, r.centerX(), r.centerY(), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_bitmap(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
|
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
SkIRect bounds;
|
|
|
|
r.roundOut(&bounds);
|
|
|
|
|
|
|
|
SkBitmap bm;
|
2014-01-25 16:46:20 +00:00
|
|
|
bm.allocN32Pixels(bounds.width(), bounds.height());
|
2013-07-24 22:19:24 +00:00
|
|
|
bm.eraseColor(SK_ColorTRANSPARENT);
|
|
|
|
SkCanvas c(bm);
|
|
|
|
draw_path(&c, r, NULL);
|
|
|
|
|
|
|
|
paint.setImageFilter(imf);
|
|
|
|
canvas->drawBitmap(bm, 0, 0, &paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_sprite(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
|
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
SkIRect bounds;
|
|
|
|
r.roundOut(&bounds);
|
|
|
|
|
|
|
|
SkBitmap bm;
|
2014-01-25 16:46:20 +00:00
|
|
|
bm.allocN32Pixels(bounds.width(), bounds.height());
|
2013-07-24 22:19:24 +00:00
|
|
|
bm.eraseColor(SK_ColorRED);
|
|
|
|
SkCanvas c(bm);
|
|
|
|
|
|
|
|
SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44);
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
c.drawRect(SkRect::Make(cropRect), paint);
|
|
|
|
|
|
|
|
paint.setImageFilter(imf);
|
|
|
|
SkPoint loc = { r.fLeft, r.fTop };
|
|
|
|
canvas->getTotalMatrix().mapPoints(&loc, 1);
|
|
|
|
canvas->drawSprite(bm,
|
|
|
|
SkScalarRoundToInt(loc.fX), SkScalarRoundToInt(loc.fY),
|
|
|
|
&paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class ImageFiltersCroppedGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
ImageFiltersCroppedGM () {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual SkString onShortName() {
|
|
|
|
return SkString("imagefilterscropped");
|
|
|
|
}
|
|
|
|
|
2013-07-25 18:08:13 +00:00
|
|
|
virtual SkISize onISize() { return SkISize::Make(400, 640); }
|
2013-07-24 22:19:24 +00:00
|
|
|
|
|
|
|
void draw_frame(SkCanvas* canvas, const SkRect& r) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual uint32_t onGetFlags() const {
|
|
|
|
// Because of the use of drawSprite, this test is excluded
|
|
|
|
// from scaled replay tests because drawSprite ignores the
|
|
|
|
// reciprocal scale that is applied at record time, which is
|
|
|
|
// the intended behavior of drawSprite.
|
2014-04-30 13:20:45 +00:00
|
|
|
return kSkipScaledReplay_Flag | kSkipTiled_Flag;
|
2013-07-24 22:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
void (*drawProc[])(SkCanvas*, const SkRect&, SkImageFilter*) = {
|
|
|
|
draw_sprite, draw_bitmap, draw_path, draw_paint, draw_text
|
|
|
|
};
|
|
|
|
|
2013-09-19 17:26:47 +00:00
|
|
|
SkAutoTUnref<SkColorFilter> cf(
|
|
|
|
SkColorFilter::CreateModeFilter(SK_ColorBLUE, SkXfermode::kSrcIn_Mode));
|
2013-10-10 13:51:19 +00:00
|
|
|
SkImageFilter::CropRect cropRect(SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)), SkImageFilter::CropRect::kHasAll_CropEdge);
|
|
|
|
SkImageFilter::CropRect bogusRect(SkRect::Make(SkIRect::MakeXYWH(-100, -100, 10, 10)), SkImageFilter::CropRect::kHasAll_CropEdge);
|
2013-07-24 22:19:24 +00:00
|
|
|
|
2014-03-10 10:51:58 +00:00
|
|
|
SkAutoTUnref<SkImageFilter> offset(SkOffsetImageFilter::Create(
|
2013-09-19 17:26:47 +00:00
|
|
|
SkIntToScalar(-10), SkIntToScalar(-10)));
|
|
|
|
|
|
|
|
SkAutoTUnref<SkImageFilter> cfOffset(SkColorFilterImageFilter::Create(cf.get(), offset.get()));
|
|
|
|
|
2013-07-24 22:19:24 +00:00
|
|
|
SkImageFilter* filters[] = {
|
|
|
|
NULL,
|
2013-09-19 17:26:47 +00:00
|
|
|
SkColorFilterImageFilter::Create(cf.get(), NULL, &cropRect),
|
2014-03-10 10:51:58 +00:00
|
|
|
SkBlurImageFilter::Create(1.0f, 1.0f, NULL, &cropRect),
|
|
|
|
SkBlurImageFilter::Create(8.0f, 0.0f, NULL, &cropRect),
|
|
|
|
SkBlurImageFilter::Create(0.0f, 8.0f, NULL, &cropRect),
|
|
|
|
SkBlurImageFilter::Create(8.0f, 8.0f, NULL, &cropRect),
|
|
|
|
SkMergeImageFilter::Create(NULL, cfOffset.get(), SkXfermode::kSrcOver_Mode, &cropRect),
|
|
|
|
SkBlurImageFilter::Create(8.0f, 8.0f, NULL, &bogusRect),
|
2013-09-19 17:26:47 +00:00
|
|
|
SkColorFilterImageFilter::Create(cf.get(), NULL, &bogusRect),
|
2013-07-24 22:19:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
|
|
|
|
SkScalar MARGIN = SkIntToScalar(16);
|
|
|
|
SkScalar DX = r.width() + MARGIN;
|
|
|
|
SkScalar DY = r.height() + MARGIN;
|
|
|
|
|
|
|
|
canvas->translate(MARGIN, MARGIN);
|
2013-07-25 18:08:13 +00:00
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) {
|
2013-07-24 22:19:24 +00:00
|
|
|
canvas->save();
|
2013-07-25 18:08:13 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
|
2013-07-24 22:19:24 +00:00
|
|
|
drawProc[j](canvas, r, filters[i]);
|
|
|
|
canvas->translate(0, DY);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(DX, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) {
|
|
|
|
SkSafeUnref(filters[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static skiagm::GM* MyFactory(void*) { return new ImageFiltersCroppedGM; }
|
|
|
|
static skiagm::GMRegistry reg(MyFactory);
|