2012-11-02 21:11:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 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 "SkBlurMaskFilter.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
|
|
|
|
#define STROKE_WIDTH SkIntToScalar(10)
|
|
|
|
|
|
|
|
typedef void (*Proc)(SkCanvas*, const SkRect&, const SkPaint&);
|
|
|
|
|
|
|
|
static void fill_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stroke_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
|
|
|
|
SkPaint paint(p);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(STROKE_WIDTH);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_donut(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
|
|
|
|
SkRect rect;
|
|
|
|
SkPath path;
|
2012-11-20 02:01:23 +00:00
|
|
|
|
2012-11-02 21:11:12 +00:00
|
|
|
rect = r;
|
|
|
|
rect.outset(STROKE_WIDTH/2, STROKE_WIDTH/2);
|
|
|
|
path.addRect(rect);
|
|
|
|
rect = r;
|
|
|
|
rect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2);
|
2012-11-20 02:01:23 +00:00
|
|
|
|
2012-11-02 21:11:12 +00:00
|
|
|
path.addRect(rect);
|
|
|
|
path.setFillType(SkPath::kEvenOdd_FillType);
|
2012-11-20 02:01:23 +00:00
|
|
|
|
2012-11-19 16:14:02 +00:00
|
|
|
canvas->drawPath(path, p);
|
|
|
|
}
|
2012-11-02 21:11:12 +00:00
|
|
|
|
2012-11-19 16:14:02 +00:00
|
|
|
static void draw_donut_skewed(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
|
|
|
|
SkRect rect;
|
|
|
|
SkPath path;
|
2012-11-20 02:01:23 +00:00
|
|
|
|
2012-11-19 16:14:02 +00:00
|
|
|
rect = r;
|
|
|
|
rect.outset(STROKE_WIDTH/2, STROKE_WIDTH/2);
|
|
|
|
path.addRect(rect);
|
|
|
|
rect = r;
|
|
|
|
rect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2);
|
2012-11-20 02:01:23 +00:00
|
|
|
|
2012-11-19 16:14:02 +00:00
|
|
|
rect.offset(7, -7);
|
2012-11-20 02:01:23 +00:00
|
|
|
|
2012-11-19 16:14:02 +00:00
|
|
|
path.addRect(rect);
|
|
|
|
path.setFillType(SkPath::kEvenOdd_FillType);
|
2012-11-20 02:01:23 +00:00
|
|
|
|
2012-11-02 21:11:12 +00:00
|
|
|
canvas->drawPath(path, p);
|
|
|
|
}
|
|
|
|
|
2012-11-26 14:39:50 +00:00
|
|
|
#include "SkGradientShader.h"
|
|
|
|
|
|
|
|
typedef void (*PaintProc)(SkPaint*, SkScalar width);
|
|
|
|
|
|
|
|
static void setgrad(SkPaint* paint, SkScalar width) {
|
|
|
|
SkPoint pts[] = { { 0, 0 }, { width, 0 } };
|
|
|
|
SkColor colors[] = { SK_ColorRED, SK_ColorGREEN };
|
|
|
|
SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2,
|
|
|
|
SkShader::kClamp_TileMode);
|
|
|
|
paint->setShader(s)->unref();
|
|
|
|
}
|
|
|
|
|
2012-11-02 21:11:12 +00:00
|
|
|
class BlurRectGM : public skiagm::GM {
|
|
|
|
SkAutoTUnref<SkMaskFilter> fMaskFilter;
|
2012-11-26 14:39:50 +00:00
|
|
|
SkString fName;
|
|
|
|
PaintProc fPProc;
|
|
|
|
SkAlpha fAlpha;
|
2012-11-02 21:11:12 +00:00
|
|
|
public:
|
2012-11-26 14:39:50 +00:00
|
|
|
BlurRectGM(const char name[], PaintProc pproc, U8CPU alpha) :
|
2012-11-02 21:11:12 +00:00
|
|
|
fMaskFilter(SkBlurMaskFilter::Create(STROKE_WIDTH/2,
|
|
|
|
SkBlurMaskFilter::kNormal_BlurStyle,
|
|
|
|
SkBlurMaskFilter::kHighQuality_BlurFlag))
|
2012-11-26 14:39:50 +00:00
|
|
|
, fName(name)
|
|
|
|
, fPProc(pproc)
|
|
|
|
, fAlpha(SkToU8(alpha))
|
2012-11-02 21:11:12 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual SkString onShortName() {
|
2012-11-26 14:39:50 +00:00
|
|
|
return fName;
|
2012-11-02 21:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkISize onISize() {
|
|
|
|
return SkISize::Make(640, 480);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2);
|
|
|
|
|
2012-11-26 14:39:50 +00:00
|
|
|
SkRect r = { 0, 0, 250, 120 };
|
2012-11-27 02:01:19 +00:00
|
|
|
|
2012-11-02 21:11:12 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setMaskFilter(fMaskFilter);
|
2012-11-26 14:39:50 +00:00
|
|
|
if (fPProc) {
|
|
|
|
fPProc(&paint, r.width());
|
|
|
|
}
|
|
|
|
paint.setAlpha(fAlpha);
|
2012-11-02 21:11:12 +00:00
|
|
|
|
|
|
|
static const Proc procs[] = {
|
2012-11-19 16:14:02 +00:00
|
|
|
fill_rect, draw_donut, draw_donut_skewed
|
2012-11-02 21:11:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this->drawProcs(canvas, r, paint, false, procs, SK_ARRAY_COUNT(procs));
|
|
|
|
canvas->translate(r.width() * 4/3, 0);
|
|
|
|
this->drawProcs(canvas, r, paint, true, procs, SK_ARRAY_COUNT(procs));
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual uint32_t onGetFlags() const { return kSkipPipe_Flag; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void drawProcs(SkCanvas* canvas, const SkRect& r, const SkPaint& paint,
|
|
|
|
bool doClip, const Proc procs[], size_t procsCount) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
for (size_t i = 0; i < procsCount; ++i) {
|
|
|
|
if (doClip) {
|
|
|
|
SkRect clipRect(r);
|
|
|
|
clipRect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2);
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(r);
|
|
|
|
}
|
|
|
|
procs[i](canvas, r, paint);
|
|
|
|
if (doClip) {
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
canvas->translate(0, r.height() * 4/3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-11-26 14:39:50 +00:00
|
|
|
DEF_GM(return new BlurRectGM("blurrect", NULL, 0xFF);)
|
|
|
|
DEF_GM(return new BlurRectGM("blurrect_grad_80", setgrad, 0x80);)
|
2012-11-02 21:11:12 +00:00
|
|
|
|