2011-07-28 14:26:00 +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.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-08-13 00:52:07 +00:00
|
|
|
#include "gm.h"
|
|
|
|
#include "SkFlattenableBuffers.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkLayerRasterizer.h"
|
|
|
|
#include "SkBlurMaskFilter.h"
|
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
static void r0(SkLayerRasterizer* rast, SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setMaskFilter(SkBlurMaskFilter::Create(SkIntToScalar(3),
|
|
|
|
SkBlurMaskFilter::kNormal_BlurStyle))->unref();
|
|
|
|
rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setMaskFilter(NULL);
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1);
|
|
|
|
rast->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAlpha(0x11);
|
|
|
|
p.setStyle(SkPaint::kFill_Style);
|
2009-06-24 12:41:42 +00:00
|
|
|
p.setXfermodeMode(SkXfermode::kSrc_Mode);
|
2008-12-17 15:59:43 +00:00
|
|
|
rast->addLayer(p);
|
|
|
|
}
|
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
static void r1(SkLayerRasterizer* rast, SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
rast->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAlpha(0x40);
|
2009-06-24 12:41:42 +00:00
|
|
|
p.setXfermodeMode(SkXfermode::kSrc_Mode);
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1*2);
|
|
|
|
rast->addLayer(p);
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
static void r2(SkLayerRasterizer* rast, SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setStyle(SkPaint::kStrokeAndFill_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1*4);
|
|
|
|
rast->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1*3/2);
|
2009-06-24 12:41:42 +00:00
|
|
|
p.setXfermodeMode(SkXfermode::kClear_Mode);
|
2008-12-17 15:59:43 +00:00
|
|
|
rast->addLayer(p);
|
|
|
|
}
|
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
static void r3(SkLayerRasterizer* rast, SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1*3);
|
|
|
|
rast->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAlpha(0x20);
|
|
|
|
p.setStyle(SkPaint::kFill_Style);
|
2009-06-24 12:41:42 +00:00
|
|
|
p.setXfermodeMode(SkXfermode::kSrc_Mode);
|
2008-12-17 15:59:43 +00:00
|
|
|
rast->addLayer(p);
|
|
|
|
}
|
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
static void r4(SkLayerRasterizer* rast, SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAlpha(0x60);
|
|
|
|
rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAlpha(0xFF);
|
2009-06-24 12:41:42 +00:00
|
|
|
p.setXfermodeMode(SkXfermode::kClear_Mode);
|
2008-12-17 15:59:43 +00:00
|
|
|
rast->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setXfermode(NULL);
|
|
|
|
rast->addLayer(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "SkDiscretePathEffect.h"
|
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
static void r5(SkLayerRasterizer* rast, SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
rast->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setPathEffect(new SkDiscretePathEffect(SK_Scalar1*4, SK_Scalar1*3))->unref();
|
2009-06-24 12:41:42 +00:00
|
|
|
p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
|
2008-12-17 15:59:43 +00:00
|
|
|
rast->addLayer(p);
|
|
|
|
}
|
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
static void r6(SkLayerRasterizer* rast, SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
rast->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAntiAlias(false);
|
|
|
|
SkLayerRasterizer* rast2 = new SkLayerRasterizer;
|
|
|
|
r5(rast2, p);
|
|
|
|
p.setRasterizer(rast2)->unref();
|
2009-06-24 12:41:42 +00:00
|
|
|
p.setXfermodeMode(SkXfermode::kClear_Mode);
|
2008-12-17 15:59:43 +00:00
|
|
|
rast->addLayer(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "Sk2DPathEffect.h"
|
|
|
|
|
2011-08-09 18:47:40 +00:00
|
|
|
static SkPathEffect* MakeDotEffect(SkScalar radius, const SkMatrix& matrix) {
|
|
|
|
SkPath path;
|
|
|
|
path.addCircle(0, 0, radius);
|
|
|
|
return new SkPath2DPathEffect(matrix, path);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
static void r7(SkLayerRasterizer* rast, SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMatrix lattice;
|
|
|
|
lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
|
|
|
|
lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
|
2011-08-09 18:47:40 +00:00
|
|
|
p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice))->unref();
|
2008-12-17 15:59:43 +00:00
|
|
|
rast->addLayer(p);
|
|
|
|
}
|
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
static void r8(SkLayerRasterizer* rast, SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
rast->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMatrix lattice;
|
|
|
|
lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
|
|
|
|
lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
|
2011-08-09 18:47:40 +00:00
|
|
|
p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice))->unref();
|
2009-06-24 12:41:42 +00:00
|
|
|
p.setXfermodeMode(SkXfermode::kClear_Mode);
|
2008-12-17 15:59:43 +00:00
|
|
|
rast->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setPathEffect(NULL);
|
|
|
|
p.setXfermode(NULL);
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1);
|
|
|
|
rast->addLayer(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
class Line2DPathEffect : public Sk2DPathEffect {
|
|
|
|
public:
|
|
|
|
Line2DPathEffect(SkScalar width, const SkMatrix& matrix)
|
2012-08-13 00:52:07 +00:00
|
|
|
: Sk2DPathEffect(matrix), fWidth(width) {}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
|
|
|
virtual bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec) SK_OVERRIDE {
|
Change patheffect to take a (new) StrokeRec object, which encapsulates the fill
or stroke parameters for a path.
Today, the patheffect only sees if the caller was going to stroke or fill, and
if stroke, it just sees the width. With this change, the effect can see all of the
related parameters (e.g. cap/join/miter). No other change is intended at this
time.
After this change, I hope to use this additional data to allow SkDashPathEffect
to, at times, apply the stroke as part of its effect, which may be much more
efficient than first dashing, and then reading that and stroking it.
Most of these files changed just because of the new parameter to filterPath. The
key changes are in SkPathEffect.[h,cpp], SkPaint.cpp and SkScalerContext.cpp
Review URL: https://codereview.appspot.com/6250051
git-svn-id: http://skia.googlecode.com/svn/trunk@4048 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-05-25 01:04:12 +00:00
|
|
|
if (this->INHERITED::filterPath(dst, src, rec)) {
|
|
|
|
rec->setStrokeStyle(fWidth);
|
2008-12-17 15:59:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-03-26 17:57:35 +00:00
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Line2DPathEffect)
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
2012-08-23 18:19:56 +00:00
|
|
|
virtual void nextSpan(int u, int v, int ucount, SkPath* dst) {
|
2011-04-22 14:10:48 +00:00
|
|
|
if (ucount > 1) {
|
2012-08-23 18:19:56 +00:00
|
|
|
SkPoint src[2], dstP[2];
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
src[0].set(SkIntToScalar(u) + SK_ScalarHalf,
|
|
|
|
SkIntToScalar(v) + SK_ScalarHalf);
|
|
|
|
src[1].set(SkIntToScalar(u+ucount) + SK_ScalarHalf,
|
|
|
|
SkIntToScalar(v) + SK_ScalarHalf);
|
|
|
|
this->getMatrix().mapPoints(dstP, src, 2);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
dst->moveTo(dstP[0]);
|
|
|
|
dst->lineTo(dstP[1]);
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-03-29 15:18:04 +00:00
|
|
|
Line2DPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fWidth = buffer.readScalar();
|
|
|
|
}
|
2012-03-29 15:18:04 +00:00
|
|
|
virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE {
|
|
|
|
this->INHERITED::flatten(buffer);
|
|
|
|
buffer.writeScalar(fWidth);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
SkScalar fWidth;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
typedef Sk2DPathEffect INHERITED;
|
|
|
|
};
|
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
static void r9(SkLayerRasterizer* rast, SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
rast->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkMatrix lattice;
|
|
|
|
lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
|
|
|
|
lattice.postRotate(SkIntToScalar(30), 0, 0);
|
|
|
|
p.setPathEffect(new Line2DPathEffect(SK_Scalar1*2, lattice))->unref();
|
2009-06-24 12:41:42 +00:00
|
|
|
p.setXfermodeMode(SkXfermode::kClear_Mode);
|
2008-12-17 15:59:43 +00:00
|
|
|
rast->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setPathEffect(NULL);
|
|
|
|
p.setXfermode(NULL);
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1);
|
|
|
|
rast->addLayer(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
|
|
|
|
|
|
|
|
static const raster_proc gRastProcs[] = {
|
|
|
|
r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
SkColor fMul, fAdd;
|
|
|
|
} gLightingColors[] = {
|
|
|
|
{ 0x808080, 0x800000 }, // general case
|
|
|
|
{ 0x707070, 0x707070 }, // no-pin case
|
|
|
|
{ 0xFFFFFF, 0x800000 }, // just-add case
|
|
|
|
{ 0x808080, 0x000000 }, // just-mul case
|
|
|
|
{ 0xFFFFFF, 0x000000 } // identity case
|
|
|
|
};
|
|
|
|
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
static void apply_shader(SkPaint* paint, int index) {
|
2008-12-17 15:59:43 +00:00
|
|
|
raster_proc proc = gRastProcs[index];
|
|
|
|
if (proc)
|
|
|
|
{
|
|
|
|
SkPaint p;
|
|
|
|
SkLayerRasterizer* rast = new SkLayerRasterizer;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAntiAlias(true);
|
|
|
|
proc(rast, p);
|
|
|
|
paint->setRasterizer(rast)->unref();
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#if 0
|
|
|
|
SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
|
2011-02-07 15:30:46 +00:00
|
|
|
paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref();
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|
|
|
|
paint->setColor(SK_ColorBLUE);
|
|
|
|
}
|
|
|
|
|
2012-08-13 00:52:07 +00:00
|
|
|
class TextEffectsGM : public skiagm::GM {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
TextEffectsGM() {}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
2012-08-13 00:52:07 +00:00
|
|
|
virtual SkString onShortName() SK_OVERRIDE {
|
|
|
|
return SkString("texteffects");
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-08-13 00:52:07 +00:00
|
|
|
virtual SkISize onISize() SK_OVERRIDE {
|
2012-08-13 01:13:38 +00:00
|
|
|
return SkISize::Make(460, 680);
|
2012-08-13 00:52:07 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-08-13 00:52:07 +00:00
|
|
|
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2010-05-17 14:50:04 +00:00
|
|
|
paint.setTextSize(SkIntToScalar(56));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2010-05-17 14:50:04 +00:00
|
|
|
SkScalar x = SkIntToScalar(20);
|
|
|
|
SkScalar y = paint.getTextSize();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-08-13 00:52:07 +00:00
|
|
|
SkString str("Hamburgefons");
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-19 19:58:58 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
apply_shader(&paint, i);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-08-13 00:52:07 +00:00
|
|
|
// paint.setMaskFilter(NULL);
|
|
|
|
// paint.setColor(SK_ColorBLACK);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawText(str.c_str(), str.size(), x, y, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
y += paint.getFontSpacing();
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
2012-08-13 01:57:05 +00:00
|
|
|
|
|
|
|
virtual uint32_t onGetFlags() const SK_OVERRIDE {
|
|
|
|
// want to skip serialization due to custom effects only defined here
|
|
|
|
return kSkipPipe_Flag;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2012-08-13 00:52:07 +00:00
|
|
|
typedef skiagm::GM INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-08-13 00:52:07 +00:00
|
|
|
static skiagm::GM* MyFactory(void*) { return new TextEffectsGM; }
|
|
|
|
static skiagm::GMRegistry reg(MyFactory);
|
2012-08-23 18:19:56 +00:00
|
|
|
|