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.
|
|
|
|
*/
|
2014-01-13 14:53:55 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
|
|
|
#include "include/core/SkGraphics.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkRegion.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "include/core/SkTime.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/effects/SkCornerPathEffect.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/private/SkTo.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
2009-03-23 16:05:19 +00:00
|
|
|
|
|
|
|
static SkRandom gRand;
|
|
|
|
|
|
|
|
static void generate_pts(SkPoint pts[], int count, int w, int h) {
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
pts[i].set(gRand.nextUScalar1() * 3 * w - SkIntToScalar(w),
|
|
|
|
gRand.nextUScalar1() * 3 * h - SkIntToScalar(h));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool check_zeros(const SkPMColor pixels[], int count, int skip) {
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if (*pixels) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pixels += skip;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool check_bitmap_margin(const SkBitmap& bm, int margin) {
|
|
|
|
size_t rb = bm.rowBytes();
|
|
|
|
for (int i = 0; i < margin; i++) {
|
|
|
|
if (!check_zeros(bm.getAddr32(0, i), bm.width(), 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int bottom = bm.height() - i - 1;
|
|
|
|
if (!check_zeros(bm.getAddr32(0, bottom), bm.width(), 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// left column
|
2014-01-27 13:42:58 +00:00
|
|
|
if (!check_zeros(bm.getAddr32(i, 0), bm.height(), SkToInt(rb >> 2))) {
|
2009-03-23 16:05:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int right = bm.width() - margin + i;
|
2014-01-27 13:42:58 +00:00
|
|
|
if (!check_zeros(bm.getAddr32(right, 0), bm.height(),
|
|
|
|
SkToInt(rb >> 2))) {
|
2009-03-23 16:05:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-08-28 15:34:46 +00:00
|
|
|
#define WIDTH 620
|
|
|
|
#define HEIGHT 460
|
|
|
|
#define MARGIN 10
|
|
|
|
|
|
|
|
static void line_proc(SkCanvas* canvas, const SkPaint& paint,
|
|
|
|
const SkBitmap& bm) {
|
|
|
|
const int N = 2;
|
|
|
|
SkPoint pts[N];
|
|
|
|
for (int i = 0; i < 400; i++) {
|
|
|
|
generate_pts(pts, N, WIDTH, HEIGHT);
|
|
|
|
|
2017-05-15 17:35:35 +00:00
|
|
|
canvas->drawLine(pts[0], pts[1], paint);
|
2009-08-28 15:34:46 +00:00
|
|
|
if (!check_bitmap_margin(bm, MARGIN)) {
|
|
|
|
SkDebugf("---- hairline failure (%g %g) (%g %g)\n",
|
|
|
|
pts[0].fX, pts[0].fY, pts[1].fX, pts[1].fY);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void poly_proc(SkCanvas* canvas, const SkPaint& paint,
|
|
|
|
const SkBitmap& bm) {
|
|
|
|
const int N = 8;
|
|
|
|
SkPoint pts[N];
|
|
|
|
for (int i = 0; i < 50; i++) {
|
|
|
|
generate_pts(pts, N, WIDTH, HEIGHT);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-08-28 15:34:46 +00:00
|
|
|
SkPath path;
|
|
|
|
path.moveTo(pts[0]);
|
|
|
|
for (int j = 1; j < N; j++) {
|
|
|
|
path.lineTo(pts[j]);
|
|
|
|
}
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkPoint ave(const SkPoint& a, const SkPoint& b) {
|
|
|
|
SkPoint c = a + b;
|
|
|
|
c.fX = SkScalarHalf(c.fX);
|
|
|
|
c.fY = SkScalarHalf(c.fY);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void quad_proc(SkCanvas* canvas, const SkPaint& paint,
|
|
|
|
const SkBitmap& bm) {
|
|
|
|
const int N = 30;
|
|
|
|
SkPoint pts[N];
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
generate_pts(pts, N, WIDTH, HEIGHT);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-08-28 15:34:46 +00:00
|
|
|
SkPath path;
|
|
|
|
path.moveTo(pts[0]);
|
|
|
|
for (int j = 1; j < N - 2; j++) {
|
|
|
|
path.quadTo(pts[j], ave(pts[j], pts[j+1]));
|
|
|
|
}
|
|
|
|
path.quadTo(pts[N - 2], pts[N - 1]);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-08-28 15:34:46 +00:00
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_cubic(SkPath* path, const SkPoint& mid, const SkPoint& end) {
|
|
|
|
SkPoint start;
|
|
|
|
path->getLastPt(&start);
|
|
|
|
path->cubicTo(ave(start, mid), ave(mid, end), end);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cube_proc(SkCanvas* canvas, const SkPaint& paint,
|
|
|
|
const SkBitmap& bm) {
|
|
|
|
const int N = 30;
|
|
|
|
SkPoint pts[N];
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
generate_pts(pts, N, WIDTH, HEIGHT);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-08-28 15:34:46 +00:00
|
|
|
SkPath path;
|
|
|
|
path.moveTo(pts[0]);
|
|
|
|
for (int j = 1; j < N - 2; j++) {
|
|
|
|
add_cubic(&path, pts[j], ave(pts[j], pts[j+1]));
|
|
|
|
}
|
|
|
|
add_cubic(&path, pts[N - 2], pts[N - 1]);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-08-28 15:34:46 +00:00
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void (*HairProc)(SkCanvas*, const SkPaint&, const SkBitmap&);
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char* fName;
|
|
|
|
HairProc fProc;
|
|
|
|
} gProcs[] = {
|
|
|
|
{ "line", line_proc },
|
|
|
|
{ "poly", poly_proc },
|
|
|
|
{ "quad", quad_proc },
|
|
|
|
{ "cube", cube_proc },
|
|
|
|
};
|
|
|
|
|
|
|
|
static int cycle_hairproc_index(int index) {
|
|
|
|
return (index + 1) % SK_ARRAY_COUNT(gProcs);
|
|
|
|
}
|
2009-03-23 16:05:19 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class HairlineView : public Sample {
|
2009-11-30 12:48:33 +00:00
|
|
|
SkMSec fNow;
|
2009-08-28 15:34:46 +00:00
|
|
|
int fProcIndex;
|
|
|
|
bool fDoAA;
|
2009-03-23 16:05:19 +00:00
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
HairlineView() {
|
2009-08-28 15:34:46 +00:00
|
|
|
fProcIndex = 0;
|
|
|
|
fDoAA = true;
|
2009-11-30 12:48:33 +00:00
|
|
|
fNow = 0;
|
2009-08-28 15:34:46 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-03-23 16:05:19 +00:00
|
|
|
protected:
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkStringPrintf("Hair-%s", gProcs[fProcIndex].fName); }
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-03-23 16:05:19 +00:00
|
|
|
void show_bitmaps(SkCanvas* canvas, const SkBitmap& b0, const SkBitmap& b1,
|
|
|
|
const SkIRect& inset) {
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->drawBitmap(b0, 0, 0, nullptr);
|
|
|
|
canvas->drawBitmap(b1, SkIntToScalar(b0.width()), 0, nullptr);
|
2009-03-23 16:05:19 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2009-11-30 12:48:33 +00:00
|
|
|
gRand.setSeed(fNow);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-03-23 16:05:19 +00:00
|
|
|
SkBitmap bm, bm2;
|
2014-02-17 02:55:57 +00:00
|
|
|
bm.allocN32Pixels(WIDTH + MARGIN*2, HEIGHT + MARGIN*2);
|
2009-03-23 16:05:19 +00:00
|
|
|
// this will erase our margin, which we want to always stay 0
|
2012-12-06 21:47:40 +00:00
|
|
|
bm.eraseColor(SK_ColorTRANSPARENT);
|
2009-03-23 16:05:19 +00:00
|
|
|
|
2014-02-17 02:55:57 +00:00
|
|
|
bm2.installPixels(SkImageInfo::MakeN32Premul(WIDTH, HEIGHT),
|
2014-05-29 15:57:20 +00:00
|
|
|
bm.getAddr32(MARGIN, MARGIN), bm.rowBytes());
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-03-23 16:05:19 +00:00
|
|
|
SkCanvas c2(bm2);
|
|
|
|
SkPaint paint;
|
2009-08-28 15:34:46 +00:00
|
|
|
paint.setAntiAlias(fDoAA);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
2012-12-06 21:47:40 +00:00
|
|
|
bm2.eraseColor(SK_ColorTRANSPARENT);
|
2009-08-28 15:34:46 +00:00
|
|
|
gProcs[fProcIndex].fProc(&c2, paint, bm);
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->drawBitmap(bm2, SkIntToScalar(10), SkIntToScalar(10), nullptr);
|
2015-02-02 03:01:04 +00:00
|
|
|
}
|
2009-11-30 12:48:33 +00:00
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool onAnimate(double /*nanos*/) override {
|
2015-02-02 03:01:04 +00:00
|
|
|
if (fDoAA) {
|
|
|
|
fProcIndex = cycle_hairproc_index(fProcIndex);
|
|
|
|
// todo: signal that we want to rebuild our TITLE
|
2009-08-28 15:34:46 +00:00
|
|
|
}
|
2015-02-02 03:01:04 +00:00
|
|
|
fDoAA = !fDoAA;
|
|
|
|
return true;
|
2009-03-23 16:05:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 20:07:57 +00:00
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey modi) override {
|
2009-08-28 15:34:46 +00:00
|
|
|
fDoAA = !fDoAA;
|
2013-01-08 16:17:50 +00:00
|
|
|
return this->INHERITED::onFindClickHandler(x, y, modi);
|
2009-08-28 15:34:46 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-08-28 15:34:46 +00:00
|
|
|
|
2009-03-23 16:05:19 +00:00
|
|
|
private:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2009-03-23 16:05:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new HairlineView(); )
|