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"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "Sk1DPathEffect.h"
|
|
|
|
#include "SkCornerPathEffect.h"
|
|
|
|
#include "SkPathMeasure.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkDither.h"
|
|
|
|
|
|
|
|
static void draw_sweep(SkCanvas* c, int width, int height, SkScalar angle) {
|
|
|
|
SkRect r;
|
|
|
|
SkPaint p;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
p.setAntiAlias(true);
|
|
|
|
// p.setDither(true);
|
|
|
|
p.setStrokeWidth(SkIntToScalar(width/10));
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// SkColor colors[] = { SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN, SK_ColorCYAN };
|
|
|
|
SkColor colors[] = { 0x4c737373, 0x4c737373, 0xffffd300 };
|
2016-03-09 02:50:00 +00:00
|
|
|
p.setShader(SkGradientShader::MakeSweep(r.centerX(), r.centerY(),
|
|
|
|
colors, nullptr, SK_ARRAY_COUNT(colors)));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkAutoCanvasRestore acr(c, true);
|
2016-07-12 22:01:19 +00:00
|
|
|
c->rotate(angle, r.centerX(), r.centerY());
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
SkRect bounds = r;
|
|
|
|
r.inset(p.getStrokeWidth(), p.getStrokeWidth());
|
|
|
|
SkRect innerBounds = r;
|
|
|
|
|
|
|
|
if (true) {
|
|
|
|
c->drawOval(r, p);
|
|
|
|
} else {
|
|
|
|
SkScalar x = r.centerX();
|
|
|
|
SkScalar y = r.centerY();
|
|
|
|
SkScalar radius = r.width() / 2;
|
|
|
|
SkScalar thickness = p.getStrokeWidth();
|
2013-11-25 19:44:07 +00:00
|
|
|
SkScalar sweep = 360.0f;
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPath path;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
path.moveTo(x + radius, y);
|
|
|
|
// outer top
|
|
|
|
path.lineTo(x + radius + thickness, y);
|
|
|
|
// outer arc
|
|
|
|
path.arcTo(bounds, 0, sweep, false);
|
|
|
|
// inner arc
|
|
|
|
path.arcTo(innerBounds, sweep, -sweep, false);
|
|
|
|
path.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
static void make_bm(SkBitmap* bm) {
|
2014-02-17 02:55:57 +00:00
|
|
|
bm->allocN32Pixels(100, 100);
|
2012-12-06 21:47:40 +00:00
|
|
|
bm->eraseColor(SK_ColorTRANSPARENT);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
|
|
|
SkCanvas c(*bm);
|
2008-12-17 15:59:43 +00:00
|
|
|
draw_sweep(&c, bm->width(), bm->height(), 0);
|
|
|
|
}
|
|
|
|
|
2011-04-14 17:53:24 +00:00
|
|
|
static void pre_dither(const SkBitmap& bm) {
|
|
|
|
for (int y = 0; y < bm.height(); y++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
DITHER_4444_SCAN(y);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPMColor* p = bm.getAddr32(0, y);
|
2011-04-14 17:53:24 +00:00
|
|
|
for (int x = 0; x < bm.width(); x++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPMColor c = *p;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
unsigned a = SkGetPackedA32(c);
|
|
|
|
unsigned r = SkGetPackedR32(c);
|
|
|
|
unsigned g = SkGetPackedG32(c);
|
|
|
|
unsigned b = SkGetPackedB32(c);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
unsigned d = DITHER_VALUE(x);
|
|
|
|
|
|
|
|
a = SkDITHER_A32To4444(a, d);
|
|
|
|
r = SkDITHER_R32To4444(r, d);
|
|
|
|
g = SkDITHER_G32To4444(g, d);
|
|
|
|
b = SkDITHER_B32To4444(b, d);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
a = SkA4444ToA32(a);
|
|
|
|
r = SkR4444ToR32(r);
|
|
|
|
g = SkG4444ToG32(g);
|
|
|
|
b = SkB4444ToB32(b);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
*p++ = SkPackARGB32(a, r, g, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
class DitherView : public SampleView {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
|
|
|
SkBitmap fBM, fBMPreDither, fBM16;
|
|
|
|
SkScalar fAngle;
|
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
DitherView() {
|
2008-12-17 15:59:43 +00:00
|
|
|
make_bm(&fBM);
|
|
|
|
make_bm(&fBMPreDither);
|
|
|
|
pre_dither(fBMPreDither);
|
2014-02-23 03:59:35 +00:00
|
|
|
fBM.copyTo(&fBM16, kARGB_4444_SkColorType);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
fAngle = 0;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
this->setBGColor(0xFF181818);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "Dither");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-05 14:03:48 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
SkScalar x = SkIntToScalar(10);
|
|
|
|
SkScalar y = SkIntToScalar(10);
|
|
|
|
const SkScalar DX = SkIntToScalar(fBM.width() + 10);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setAntiAlias(true);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
if (true) {
|
|
|
|
canvas->drawBitmap(fBM, x, y, &paint);
|
|
|
|
x += DX;
|
|
|
|
paint.setDither(true);
|
|
|
|
canvas->drawBitmap(fBM, x, y, &paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
x += DX;
|
|
|
|
paint.setDither(false);
|
|
|
|
canvas->drawBitmap(fBMPreDither, x, y, &paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
x += DX;
|
|
|
|
canvas->drawBitmap(fBM16, x, y, &paint);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->translate(DX, DX*2);
|
|
|
|
draw_sweep(canvas, fBM.width(), fBM.height(), fAngle);
|
|
|
|
canvas->translate(DX, 0);
|
|
|
|
draw_sweep(canvas, fBM.width()>>1, fBM.height()>>1, fAngle);
|
|
|
|
canvas->translate(DX, 0);
|
|
|
|
draw_sweep(canvas, fBM.width()>>2, fBM.height()>>2, fAngle);
|
|
|
|
|
|
|
|
fAngle += SK_Scalar1/2;
|
2015-08-27 14:41:13 +00:00
|
|
|
this->inval(nullptr);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-05-05 14:03:48 +00:00
|
|
|
typedef SampleView INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new DitherView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|