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
|
|
|
#include "SampleCode.h"
|
2013-09-06 14:16:12 +00:00
|
|
|
#include "SkBlurMask.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "Sk1DPathEffect.h"
|
|
|
|
#include "Sk2DPathEffect.h"
|
|
|
|
#include "SkAvoidXfermode.h"
|
|
|
|
#include "SkBlurMaskFilter.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkCornerPathEffect.h"
|
|
|
|
#include "SkDashPathEffect.h"
|
|
|
|
#include "SkDiscretePathEffect.h"
|
|
|
|
#include "SkEmbossMaskFilter.h"
|
2014-01-30 18:58:24 +00:00
|
|
|
#include "SkReadBuffer.h"
|
|
|
|
#include "SkWriteBuffer.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#include "SkLayerRasterizer.h"
|
|
|
|
#include "SkMath.h"
|
|
|
|
#include "SkPath.h"
|
2014-04-18 18:04:41 +00:00
|
|
|
#include "SkPictureRecorder.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
2009-05-27 01:20:10 +00:00
|
|
|
#include "SkComposeShader.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkCornerPathEffect.h"
|
|
|
|
#include "SkPathMeasure.h"
|
|
|
|
#include "SkPicture.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkTransparentShader.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
static inline SkPMColor rgb2gray(SkPMColor c) {
|
2008-12-17 15:59:43 +00:00
|
|
|
unsigned r = SkGetPackedR32(c);
|
|
|
|
unsigned g = SkGetPackedG32(c);
|
|
|
|
unsigned b = SkGetPackedB32(c);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
unsigned x = (r * 5 + g * 7 + b * 4) >> 4;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
return SkPackARGB32(0, x, x, x) | (c & (SK_A32_MASK << SK_A32_SHIFT));
|
|
|
|
}
|
|
|
|
|
|
|
|
class SkGrayScaleColorFilter : public SkColorFilter {
|
|
|
|
public:
|
2012-12-24 14:38:46 +00:00
|
|
|
virtual void filterSpan(const SkPMColor src[], int count,
|
|
|
|
SkPMColor result[]) const SK_OVERRIDE {
|
2008-12-17 15:59:43 +00:00
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
result[i] = rgb2gray(src[i]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SkChannelMaskColorFilter : public SkColorFilter {
|
|
|
|
public:
|
2011-05-05 01:59:48 +00:00
|
|
|
SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask);
|
|
|
|
}
|
|
|
|
|
2012-12-24 14:38:46 +00:00
|
|
|
virtual void filterSpan(const SkPMColor src[], int count,
|
|
|
|
SkPMColor result[]) const SK_OVERRIDE {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPMColor mask = fMask;
|
2011-05-05 01:59:48 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
result[i] = src[i] & mask;
|
2011-05-05 01:59:48 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
SkPMColor fMask;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r0(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
2014-04-28 16:25:35 +00:00
|
|
|
p.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
|
2013-09-06 14:16:12 +00:00
|
|
|
SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3)),
|
2013-08-27 16:14:03 +00:00
|
|
|
SkBlurMaskFilter::kNone_BlurFlag))->unref();
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
p.setMaskFilter(NULL);
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
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);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r1(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
|
|
|
rastBuilder->addLayer(p);
|
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);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r2(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setStyle(SkPaint::kStrokeAndFill_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1*4);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
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);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r3(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1*3);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
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);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r4(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAlpha(0x60);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
p.setAlpha(0xFF);
|
2009-06-24 12:41:42 +00:00
|
|
|
p.setXfermodeMode(SkXfermode::kClear_Mode);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
p.setXfermode(NULL);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r5(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-02-20 20:40:19 +00:00
|
|
|
p.setPathEffect(SkDiscretePathEffect::Create(SK_Scalar1*4, SK_Scalar1*3))->unref();
|
2009-06-24 12:41:42 +00:00
|
|
|
p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r6(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
|
|
|
rastBuilder->addLayer(p);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAntiAlias(false);
|
2014-02-26 13:27:37 +00:00
|
|
|
SkLayerRasterizer::Builder rastBuilder2;
|
|
|
|
r5(&rastBuilder2, p);
|
|
|
|
p.setRasterizer(rastBuilder2.detachRasterizer())->unref();
|
2009-06-24 12:41:42 +00:00
|
|
|
p.setXfermodeMode(SkXfermode::kClear_Mode);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Dot2DPathEffect : public Sk2DPathEffect {
|
|
|
|
public:
|
|
|
|
Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix)
|
|
|
|
: Sk2DPathEffect(matrix), fRadius(radius) {}
|
|
|
|
|
2012-03-26 17:57:35 +00:00
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect)
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
protected:
|
2012-12-24 14:38:46 +00:00
|
|
|
virtual void next(const SkPoint& loc, int u, int v, SkPath* dst) const SK_OVERRIDE {
|
2008-12-17 15:59:43 +00:00
|
|
|
dst->addCircle(loc.fX, loc.fY, fRadius);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2014-08-21 14:59:51 +00:00
|
|
|
#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
|
2014-01-30 18:58:24 +00:00
|
|
|
Dot2DPathEffect(SkReadBuffer& buffer) : INHERITED(buffer) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fRadius = buffer.readScalar();
|
|
|
|
}
|
2014-08-21 14:59:51 +00:00
|
|
|
#endif
|
2014-01-30 18:58:24 +00:00
|
|
|
virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE {
|
2012-03-29 15:18:04 +00:00
|
|
|
this->INHERITED::flatten(buffer);
|
|
|
|
buffer.writeScalar(fRadius);
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
SkScalar fRadius;
|
|
|
|
|
|
|
|
typedef Sk2DPathEffect INHERITED;
|
|
|
|
};
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r7(SkLayerRasterizer::Builder* rastBuilder, 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);
|
|
|
|
p.setPathEffect(new Dot2DPathEffect(SK_Scalar1*4, lattice))->unref();
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r8(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
|
|
|
rastBuilder->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);
|
|
|
|
p.setPathEffect(new Dot2DPathEffect(SK_Scalar1*2, lattice))->unref();
|
2009-06-24 12:41:42 +00:00
|
|
|
p.setXfermodeMode(SkXfermode::kClear_Mode);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
p.setPathEffect(NULL);
|
|
|
|
p.setXfermode(NULL);
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
static void r9(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
|
|
|
|
rastBuilder->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);
|
2014-02-20 20:40:19 +00:00
|
|
|
p.setPathEffect(SkLine2DPathEffect::Create(SK_Scalar1*2, lattice))->unref();
|
2009-06-24 12:41:42 +00:00
|
|
|
p.setXfermodeMode(SkXfermode::kClear_Mode);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
p.setPathEffect(NULL);
|
|
|
|
p.setXfermode(NULL);
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setStrokeWidth(SK_Scalar1);
|
2014-02-26 13:27:37 +00:00
|
|
|
rastBuilder->addLayer(p);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 13:27:37 +00:00
|
|
|
typedef void (*raster_proc)(SkLayerRasterizer::Builder*, SkPaint&);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
static void apply_shader(SkPaint* paint, int index) {
|
2008-12-17 15:59:43 +00:00
|
|
|
raster_proc proc = gRastProcs[index];
|
2011-05-05 01:59:48 +00:00
|
|
|
if (proc) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint p;
|
2014-02-26 13:27:37 +00:00
|
|
|
SkLayerRasterizer::Builder rastBuilder;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
p.setAntiAlias(true);
|
2014-02-26 13:27:37 +00:00
|
|
|
proc(&rastBuilder, p);
|
|
|
|
paint->setRasterizer(rastBuilder.detachRasterizer())->unref();
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
|
2013-09-06 14:16:12 +00:00
|
|
|
paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(
|
|
|
|
SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3)), dir,
|
|
|
|
SK_Scalar1/4, SkIntToScalar(4)))->unref();
|
2008-12-17 15:59:43 +00:00
|
|
|
paint->setColor(SK_ColorBLUE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
class DemoView : public SampleView {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2011-05-02 17:33:45 +00:00
|
|
|
DemoView() {}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
2011-05-05 01:59:48 +00:00
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SampleCode::TitleR(evt, "Demo");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
virtual bool onClick(Click* click) {
|
2008-12-17 15:59:43 +00:00
|
|
|
return this->INHERITED::onClick(click);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
void makePath(SkPath& path) {
|
2008-12-17 15:59:43 +00:00
|
|
|
path.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(20),
|
|
|
|
SkPath::kCCW_Direction);
|
|
|
|
for (int index = 0; index < 10; index++) {
|
2013-11-25 19:44:07 +00:00
|
|
|
SkScalar x = (float) cos(index / 10.0f * 2 * 3.1415925358f);
|
|
|
|
SkScalar y = (float) sin(index / 10.0f * 2 * 3.1415925358f);
|
2008-12-17 15:59:43 +00:00
|
|
|
x *= index & 1 ? 7 : 14;
|
|
|
|
y *= index & 1 ? 7 : 14;
|
|
|
|
x += SkIntToScalar(20);
|
|
|
|
y += SkIntToScalar(20);
|
|
|
|
if (index == 0)
|
|
|
|
path.moveTo(x, y);
|
|
|
|
else
|
|
|
|
path.lineTo(x, y);
|
|
|
|
}
|
|
|
|
path.close();
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save();
|
2014-04-13 19:09:42 +00:00
|
|
|
this->drawPicture(canvas, 0);
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
{
|
2014-04-13 19:09:42 +00:00
|
|
|
SkPictureRecorder recorder;
|
|
|
|
{
|
2014-04-17 23:35:06 +00:00
|
|
|
SkCanvas* record = recorder.beginRecording(320, 480, NULL, 0);
|
2014-04-13 19:09:42 +00:00
|
|
|
this->drawPicture(record, 120);
|
|
|
|
}
|
|
|
|
SkAutoTUnref<SkPicture> picture(recorder.endRecording());
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(120));
|
|
|
|
|
|
|
|
SkRect clip;
|
|
|
|
clip.set(0, 0, SkIntToScalar(160), SkIntToScalar(160));
|
|
|
|
do {
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(clip);
|
2014-09-04 15:42:50 +00:00
|
|
|
picture->playback(canvas);
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->restore();
|
|
|
|
if (clip.fRight < SkIntToScalar(320))
|
|
|
|
clip.offset(SkIntToScalar(160), 0);
|
|
|
|
else if (clip.fBottom < SkIntToScalar(480))
|
|
|
|
clip.offset(-SkIntToScalar(320), SkIntToScalar(160));
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
} while (true);
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
void drawPicture(SkCanvas* canvas, int spriteOffset) {
|
2012-08-23 18:19:56 +00:00
|
|
|
SkMatrix matrix; matrix.reset();
|
|
|
|
SkPaint paint;
|
|
|
|
SkPath path;
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPoint start = {0, 0};
|
|
|
|
SkPoint stop = { SkIntToScalar(40), SkIntToScalar(40) };
|
2012-08-23 18:19:56 +00:00
|
|
|
SkRect rect = {0, 0, SkIntToScalar(40), SkIntToScalar(40) };
|
|
|
|
SkRect rect2 = {0, 0, SkIntToScalar(65), SkIntToScalar(20) };
|
|
|
|
SkScalar left = 0, top = 0, x = 0, y = 0;
|
2014-01-27 13:42:58 +00:00
|
|
|
int index;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
|
|
|
char ascii[] = "ascii...";
|
2014-01-27 13:42:58 +00:00
|
|
|
int asciiLength = sizeof(ascii) - 1;
|
2012-08-23 18:19:56 +00:00
|
|
|
char utf8[] = "utf8" "\xe2\x80\xa6";
|
|
|
|
short utf16[] = {'u', 't', 'f', '1', '6', 0x2026 };
|
|
|
|
short utf16simple[] = {'u', 't', 'f', '1', '6', '!' };
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
makePath(path);
|
|
|
|
SkTDArray<SkPoint>(pos);
|
2012-08-23 18:19:56 +00:00
|
|
|
pos.setCount(asciiLength);
|
|
|
|
for (index = 0; index < asciiLength; index++)
|
|
|
|
pos[index].set(SkIntToScalar((unsigned int)index * 10),
|
2012-05-17 15:38:00 +00:00
|
|
|
SkIntToScalar((unsigned int)index * 2));
|
2008-12-17 15:59:43 +00:00
|
|
|
SkTDArray<SkPoint>(pos2);
|
2012-08-23 18:19:56 +00:00
|
|
|
pos2.setCount(asciiLength);
|
|
|
|
for (index = 0; index < asciiLength; index++)
|
|
|
|
pos2[index].set(SkIntToScalar((unsigned int)index * 10),
|
2012-05-17 15:38:00 +00:00
|
|
|
SkIntToScalar(20));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// shaders
|
2011-05-19 19:58:58 +00:00
|
|
|
SkPoint linearPoints[] = { { 0, 0, }, { SkIntToScalar(40), SkIntToScalar(40) } };
|
2008-12-17 15:59:43 +00:00
|
|
|
SkColor linearColors[] = { SK_ColorRED, SK_ColorBLUE };
|
|
|
|
SkScalar* linearPos = NULL;
|
|
|
|
int linearCount = 2;
|
|
|
|
SkShader::TileMode linearMode = SkShader::kMirror_TileMode;
|
|
|
|
SkShader* linear = SkGradientShader::CreateLinear(linearPoints,
|
2014-05-22 12:27:41 +00:00
|
|
|
linearColors, linearPos, linearCount, linearMode);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
SkPoint radialCenter = { SkIntToScalar(25), SkIntToScalar(25) };
|
|
|
|
SkScalar radialRadius = SkIntToScalar(25);
|
|
|
|
SkColor radialColors[] = { SK_ColorGREEN, SK_ColorGRAY, SK_ColorRED };
|
|
|
|
SkScalar radialPos[] = { 0, SkIntToScalar(3) / 5, SkIntToScalar(1)};
|
|
|
|
int radialCount = 3;
|
|
|
|
SkShader::TileMode radialMode = SkShader::kRepeat_TileMode;
|
2012-08-23 18:19:56 +00:00
|
|
|
SkShader* radial = SkGradientShader::CreateRadial(radialCenter,
|
2008-12-17 15:59:43 +00:00
|
|
|
radialRadius, radialColors, radialPos, radialCount,
|
2014-05-22 12:27:41 +00:00
|
|
|
radialMode);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkTransparentShader* transparentShader = new SkTransparentShader();
|
|
|
|
SkEmbossMaskFilter::Light light;
|
|
|
|
light.fDirection[0] = SK_Scalar1/2;
|
|
|
|
light.fDirection[1] = SK_Scalar1/2;
|
|
|
|
light.fDirection[2] = SK_Scalar1/3;
|
2012-08-23 18:19:56 +00:00
|
|
|
light.fAmbient = 0x48;
|
|
|
|
light.fSpecular = 0x80;
|
2013-09-06 14:16:12 +00:00
|
|
|
SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(12)/5);
|
2014-02-21 10:13:32 +00:00
|
|
|
SkEmbossMaskFilter* embossFilter = SkEmbossMaskFilter::Create(sigma, light);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-06-23 12:19:41 +00:00
|
|
|
SkXfermode* xfermode = SkXfermode::Create(SkXfermode::kXor_Mode);
|
2008-12-17 15:59:43 +00:00
|
|
|
SkColorFilter* lightingFilter = SkColorFilter::CreateLightingFilter(
|
|
|
|
0xff89bc45, 0xff112233);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save();
|
2012-08-23 18:19:56 +00:00
|
|
|
canvas->translate(SkIntToScalar(0), SkIntToScalar(5));
|
2013-07-22 17:18:18 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setFilterLevel(SkPaint::kLow_FilterLevel);
|
2012-08-23 18:19:56 +00:00
|
|
|
// !!! draw through a clip
|
|
|
|
paint.setColor(SK_ColorLTGRAY);
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
2008-12-17 15:59:43 +00:00
|
|
|
SkRect clip = {0, 0, SkIntToScalar(320), SkIntToScalar(120)};
|
|
|
|
canvas->clipRect(clip);
|
2012-08-23 18:19:56 +00:00
|
|
|
paint.setShader(SkShader::CreateBitmapShader(fTx,
|
2008-12-17 15:59:43 +00:00
|
|
|
SkShader::kMirror_TileMode, SkShader::kRepeat_TileMode))->unref();
|
2012-08-23 18:19:56 +00:00
|
|
|
canvas->drawPaint(paint);
|
|
|
|
canvas->save();
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// line (exercises xfermode, colorShader, colorFilter, filterShader)
|
2012-08-23 18:19:56 +00:00
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
paint.setStrokeWidth(SkIntToScalar(10));
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setXfermode(xfermode)->unref();
|
|
|
|
paint.setColorFilter(lightingFilter)->unref();
|
2012-08-23 18:19:56 +00:00
|
|
|
canvas->drawLine(start.fX, start.fY, stop.fX, stop.fY, paint); // should not be green
|
|
|
|
paint.setXfermode(NULL);
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setColorFilter(NULL);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// rectangle
|
2012-08-23 18:19:56 +00:00
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
canvas->translate(SkIntToScalar(50), 0);
|
|
|
|
paint.setColor(SK_ColorYELLOW);
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setShader(linear)->unref();
|
|
|
|
paint.setPathEffect(pathEffectTest())->unref();
|
2012-08-23 18:19:56 +00:00
|
|
|
canvas->drawRect(rect, paint);
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setPathEffect(NULL);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// circle w/ emboss & transparent (exercises 3dshader)
|
2012-08-23 18:19:56 +00:00
|
|
|
canvas->translate(SkIntToScalar(50), 0);
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setMaskFilter(embossFilter)->unref();
|
|
|
|
canvas->drawOval(rect, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setShader(transparentShader)->unref();
|
|
|
|
canvas->drawOval(rect, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(-10));
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// path
|
2012-08-23 18:19:56 +00:00
|
|
|
canvas->translate(SkIntToScalar(50), 0);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(SkIntToScalar(5));
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setShader(radial)->unref();
|
|
|
|
paint.setMaskFilter(NULL);
|
2012-08-23 18:19:56 +00:00
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setShader(NULL);
|
|
|
|
// bitmap, sprite
|
2012-08-23 18:19:56 +00:00
|
|
|
canvas->translate(SkIntToScalar(50), 0);
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
canvas->drawBitmap(fBug, left, top, &paint);
|
|
|
|
canvas->translate(SkIntToScalar(30), 0);
|
|
|
|
canvas->drawSprite(fTb,
|
2013-12-17 19:22:07 +00:00
|
|
|
SkScalarRoundToInt(canvas->getTotalMatrix().getTranslateX()),
|
2008-12-17 15:59:43 +00:00
|
|
|
spriteOffset + 10, &paint);
|
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
canvas->translate(-SkIntToScalar(30), SkIntToScalar(30));
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setShader(shaderTest())->unref(); // test compose shader
|
2012-08-23 18:19:56 +00:00
|
|
|
canvas->drawRect(rect2, paint);
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setShader(NULL);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->restore();
|
|
|
|
// text
|
2012-08-23 18:19:56 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(60));
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save();
|
2012-08-23 18:19:56 +00:00
|
|
|
paint.setColor(SK_ColorGRAY);
|
|
|
|
canvas->drawPosText(ascii, asciiLength, pos.begin(), paint);
|
|
|
|
canvas->drawPosText(ascii, asciiLength, pos2.begin(), paint);
|
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(50), 0);
|
|
|
|
paint.setColor(SK_ColorCYAN);
|
|
|
|
canvas->drawText(utf8, sizeof(utf8) - 1, x, y, paint);
|
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(30), 0);
|
|
|
|
paint.setColor(SK_ColorMAGENTA);
|
|
|
|
paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
|
2008-12-17 15:59:43 +00:00
|
|
|
matrix.setTranslate(SkIntToScalar(10), SkIntToScalar(10));
|
2012-08-23 18:19:56 +00:00
|
|
|
canvas->drawTextOnPath((void*) utf16, sizeof(utf16), path, &matrix, paint);
|
|
|
|
canvas->translate(0, SkIntToScalar(20));
|
|
|
|
canvas->drawTextOnPath((void*) utf16simple, sizeof(utf16simple), path, &matrix, paint);
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->restore();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(60));
|
2012-08-23 18:19:56 +00:00
|
|
|
paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2013-01-08 16:17:50 +00:00
|
|
|
virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fClickPt.set(x, y);
|
|
|
|
this->inval(NULL);
|
2013-01-08 16:17:50 +00:00
|
|
|
return this->INHERITED::onFindClickHandler(x, y, modi);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
SkPathEffect* pathEffectTest() {
|
2008-12-17 15:59:43 +00:00
|
|
|
static const int gXY[] = { 1, 0, 0, -1, 2, -1, 3, 0, 2, 1, 0, 1 };
|
|
|
|
SkScalar gPhase = 0;
|
|
|
|
SkPath path;
|
|
|
|
path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
|
|
|
|
for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
|
|
|
|
path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
|
|
|
|
path.close();
|
|
|
|
path.offset(SkIntToScalar(-6), 0);
|
2014-02-20 20:40:19 +00:00
|
|
|
SkPathEffect* outer = SkPath1DPathEffect::Create(path, SkIntToScalar(12),
|
2008-12-17 15:59:43 +00:00
|
|
|
gPhase, SkPath1DPathEffect::kRotate_Style);
|
2014-02-20 20:40:19 +00:00
|
|
|
SkPathEffect* inner = SkDiscretePathEffect::Create(SkIntToScalar(2),
|
2008-12-17 15:59:43 +00:00
|
|
|
SkIntToScalar(1)/10); // SkCornerPathEffect(SkIntToScalar(2));
|
2014-02-20 20:40:19 +00:00
|
|
|
SkPathEffect* result = SkComposePathEffect::Create(outer, inner);
|
2008-12-17 15:59:43 +00:00
|
|
|
outer->unref();
|
|
|
|
inner->unref();
|
|
|
|
return result;
|
|
|
|
}
|
2012-05-10 13:04:59 +00:00
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
SkShader* shaderTest() {
|
2011-05-19 19:58:58 +00:00
|
|
|
SkPoint pts[] = { { 0, 0, }, { SkIntToScalar(100), 0 } };
|
2008-12-17 15:59:43 +00:00
|
|
|
SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
|
2012-08-23 18:19:56 +00:00
|
|
|
SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL,
|
2008-12-17 15:59:43 +00:00
|
|
|
2, SkShader::kClamp_TileMode);
|
|
|
|
pts[1].set(0, SkIntToScalar(100));
|
|
|
|
SkColor colors2[] = {SK_ColorBLACK, SkColorSetARGB(0x80, 0, 0, 0)};
|
2012-08-23 18:19:56 +00:00
|
|
|
SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors2, NULL,
|
2008-12-17 15:59:43 +00:00
|
|
|
2, SkShader::kClamp_TileMode);
|
2009-06-23 12:19:41 +00:00
|
|
|
SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode);
|
2008-12-17 15:59:43 +00:00
|
|
|
SkShader* result = new SkComposeShader(shaderA, shaderB, mode);
|
|
|
|
shaderA->unref();
|
|
|
|
shaderB->unref();
|
|
|
|
mode->unref();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void startTest() {
|
2012-08-23 18:19:56 +00:00
|
|
|
SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/bugcirc.gif", &fBug);
|
|
|
|
SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/tbcirc.gif", &fTb);
|
|
|
|
SkImageDecoder::DecodeFile("/Users/caryclark/Desktop/05psp04.gif", &fTx);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
void drawRaster(SkCanvas* canvas) {
|
2011-05-19 19:58:58 +00:00
|
|
|
for (size_t index = 0; index < SK_ARRAY_COUNT(gRastProcs); index++)
|
2008-12-17 15:59:43 +00:00
|
|
|
drawOneRaster(canvas);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
void drawOneRaster(SkCanvas* canvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save();
|
|
|
|
|
|
|
|
SkScalar x = SkIntToScalar(20);
|
|
|
|
SkScalar y = SkIntToScalar(40);
|
|
|
|
SkPaint paint;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setTextSize(SkIntToScalar(48));
|
2009-05-27 01:20:10 +00:00
|
|
|
paint.setTypeface(SkTypeface::CreateFromName("sans-serif",
|
|
|
|
SkTypeface::kBold));
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
SkString str("GOOGLE");
|
|
|
|
|
2011-05-19 19:58:58 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
|
2014-01-27 13:42:58 +00:00
|
|
|
apply_shader(&paint, (int)i);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// paint.setMaskFilter(NULL);
|
|
|
|
// paint.setColor(SK_ColorBLACK);
|
|
|
|
|
|
|
|
#if 01
|
|
|
|
int index = i % SK_ARRAY_COUNT(gLightingColors);
|
|
|
|
paint.setColorFilter(SkColorFilter::CreateLightingFilter(
|
|
|
|
gLightingColors[index].fMul,
|
|
|
|
gLightingColors[index].fAdd))->unref();
|
|
|
|
#endif
|
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);
|
|
|
|
SkRect oval = { x, y - SkIntToScalar(40), x + SkIntToScalar(40), y };
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawOval(oval, paint);
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
|
|
|
|
y += paint.getFontSpacing();
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->restore();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-06-03 02:35:01 +00:00
|
|
|
if (1) {
|
2014-02-20 20:40:19 +00:00
|
|
|
SkAutoTUnref<SkAvoidXfermode> mode(SkAvoidXfermode::Create(SK_ColorWHITE, 0xFF,
|
|
|
|
SkAvoidXfermode::kTargetColor_Mode));
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
x += SkIntToScalar(20);
|
|
|
|
SkRect r = { x, 0, x + SkIntToScalar(360), SkIntToScalar(700) };
|
2014-02-20 20:40:19 +00:00
|
|
|
paint.setXfermode(mode);
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas->drawOval(r, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkPoint fClickPt;
|
|
|
|
SkBitmap fBug, fTb, fTx;
|
2011-05-05 01:59:48 +00:00
|
|
|
typedef SampleView INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new DemoView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|