2015-01-28 19:08:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 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 "SkBitmap.h"
|
2015-08-05 20:57:49 +00:00
|
|
|
#include "SkPath.h"
|
2015-01-28 19:08:00 +00:00
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders overlapping shapes with colorburn against a checkerboard.
|
|
|
|
*/
|
|
|
|
class DstReadShuffle : public GM {
|
|
|
|
public:
|
|
|
|
DstReadShuffle() {
|
|
|
|
this->setBGColor(SkColorSetARGB(0xff, 0xff, 0, 0xff));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
enum ShapeType {
|
|
|
|
kCircle_ShapeType,
|
|
|
|
kRoundRect_ShapeType,
|
|
|
|
kRect_ShapeType,
|
|
|
|
kConvexPath_ShapeType,
|
|
|
|
kConcavePath_ShapeType,
|
|
|
|
kText_ShapeType,
|
|
|
|
kNumShapeTypes
|
|
|
|
};
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2015-01-28 19:08:00 +00:00
|
|
|
return SkString("dstreadshuffle");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2015-01-28 19:08:00 +00:00
|
|
|
return SkISize::Make(kWidth, kHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawShape(SkCanvas* canvas,
|
|
|
|
SkPaint* paint,
|
|
|
|
ShapeType type) {
|
2016-09-01 18:24:54 +00:00
|
|
|
const SkRect kRect = SkRect::MakeXYWH(SkIntToScalar(-50), SkIntToScalar(-50),
|
|
|
|
SkIntToScalar(75), SkIntToScalar(105));
|
2015-01-28 19:08:00 +00:00
|
|
|
switch (type) {
|
|
|
|
case kCircle_ShapeType:
|
|
|
|
canvas->drawCircle(0, 0, 50, *paint);
|
|
|
|
break;
|
|
|
|
case kRoundRect_ShapeType:
|
|
|
|
canvas->drawRoundRect(kRect, SkIntToScalar(10), SkIntToScalar(20), *paint);
|
|
|
|
break;
|
|
|
|
case kRect_ShapeType:
|
|
|
|
canvas->drawRect(kRect, *paint);
|
|
|
|
break;
|
|
|
|
case kConvexPath_ShapeType:
|
|
|
|
if (fConvexPath.isEmpty()) {
|
|
|
|
SkPoint points[4];
|
|
|
|
kRect.toQuad(points);
|
|
|
|
fConvexPath.moveTo(points[0]);
|
|
|
|
fConvexPath.quadTo(points[1], points[2]);
|
|
|
|
fConvexPath.quadTo(points[3], points[0]);
|
|
|
|
SkASSERT(fConvexPath.isConvex());
|
|
|
|
}
|
|
|
|
canvas->drawPath(fConvexPath, *paint);
|
|
|
|
break;
|
|
|
|
case kConcavePath_ShapeType:
|
|
|
|
if (fConcavePath.isEmpty()) {
|
|
|
|
SkPoint points[5] = {{0, SkIntToScalar(-50)} };
|
|
|
|
SkMatrix rot;
|
|
|
|
rot.setRotate(SkIntToScalar(360) / 5);
|
|
|
|
for (int i = 1; i < 5; ++i) {
|
|
|
|
rot.mapPoints(points + i, points + i - 1, 1);
|
|
|
|
}
|
|
|
|
fConcavePath.moveTo(points[0]);
|
|
|
|
for (int i = 0; i < 5; ++i) {
|
|
|
|
fConcavePath.lineTo(points[(2 * i) % 5]);
|
|
|
|
}
|
|
|
|
fConcavePath.setFillType(SkPath::kEvenOdd_FillType);
|
|
|
|
SkASSERT(!fConcavePath.isConvex());
|
|
|
|
}
|
|
|
|
canvas->drawPath(fConcavePath, *paint);
|
|
|
|
break;
|
|
|
|
case kText_ShapeType: {
|
|
|
|
const char* text = "Hello!";
|
|
|
|
paint->setTextSize(30);
|
2015-07-29 13:58:40 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(paint);
|
2015-01-28 19:08:00 +00:00
|
|
|
canvas->drawText(text, strlen(text), 0, 0, *paint);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 15:08:28 +00:00
|
|
|
static SkColor GetColor(SkRandom* random, int i, int nextColor) {
|
|
|
|
static SkColor colors[] = { SK_ColorRED,
|
2015-07-29 12:27:47 +00:00
|
|
|
sk_tool_utils::color_to_565(0xFFFF7F00), // Orange
|
2015-04-15 15:08:28 +00:00
|
|
|
SK_ColorYELLOW,
|
|
|
|
SK_ColorGREEN,
|
|
|
|
SK_ColorBLUE,
|
2015-07-29 12:27:47 +00:00
|
|
|
sk_tool_utils::color_to_565(0xFF4B0082), // indigo
|
|
|
|
sk_tool_utils::color_to_565(0xFF7F00FF) }; // violet
|
2015-01-28 19:08:00 +00:00
|
|
|
SkColor color;
|
2015-04-15 15:08:28 +00:00
|
|
|
int index = nextColor % SK_ARRAY_COUNT(colors);
|
2015-01-28 19:08:00 +00:00
|
|
|
switch (i) {
|
|
|
|
case 0:
|
|
|
|
color = SK_ColorTRANSPARENT;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
color = SkColorSetARGB(0xff,
|
2015-04-15 15:08:28 +00:00
|
|
|
SkColorGetR(colors[index]),
|
|
|
|
SkColorGetG(colors[index]),
|
|
|
|
SkColorGetB(colors[index]));
|
2015-01-28 19:08:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-04-15 15:08:28 +00:00
|
|
|
uint8_t alpha = 0x80;
|
2015-01-28 19:08:00 +00:00
|
|
|
color = SkColorSetARGB(alpha,
|
2015-04-15 15:08:28 +00:00
|
|
|
SkColorGetR(colors[index]),
|
|
|
|
SkColorGetG(colors[index]),
|
|
|
|
SkColorGetB(colors[index]));
|
2015-01-28 19:08:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetStyle(SkPaint* p, int style, int width) {
|
|
|
|
switch (style) {
|
|
|
|
case 0:
|
|
|
|
p->setStyle(SkPaint::kStroke_Style);
|
|
|
|
p->setStrokeWidth((SkScalar)width);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
p->setStyle(SkPaint::kStrokeAndFill_Style);
|
|
|
|
p->setStrokeWidth((SkScalar)width);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
p->setStyle(SkPaint::kFill_Style);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2015-01-28 19:08:00 +00:00
|
|
|
SkRandom random;
|
|
|
|
SkScalar y = 100;
|
|
|
|
for (int i = 0; i < kNumShapeTypes; i++) {
|
|
|
|
ShapeType shapeType = static_cast<ShapeType>(i);
|
|
|
|
SkScalar x = 25;
|
|
|
|
for (int style = 0; style < 3; style++) {
|
|
|
|
for (int width = 0; width <= 1; width++) {
|
|
|
|
for (int alpha = 0; alpha <= 2; alpha++) {
|
|
|
|
for (int r = 0; r <= 5; r++) {
|
2015-04-15 15:08:28 +00:00
|
|
|
SkColor color = GetColor(&random, alpha, style + width + alpha + r);
|
2015-01-28 19:08:00 +00:00
|
|
|
|
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
p.setColor(color);
|
2015-04-15 15:08:28 +00:00
|
|
|
// In order to get some batching on the GPU backend we do 2 src over for
|
|
|
|
// each xfer mode which requires a dst read
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(r % 3 == 0 ? SkBlendMode::kLighten :
|
|
|
|
SkBlendMode::kSrcOver);
|
2015-01-28 19:08:00 +00:00
|
|
|
SetStyle(&p, style, width);
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(x, y);
|
|
|
|
canvas->rotate((SkScalar)(r < 3 ? 10 : 0));
|
|
|
|
this->drawShape(canvas, &p, shapeType);
|
|
|
|
canvas->restore();
|
|
|
|
x += 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
y += 50;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum {
|
|
|
|
kNumShapes = 100,
|
|
|
|
};
|
|
|
|
SkAutoTUnref<SkShader> fBG;
|
|
|
|
SkPath fConcavePath;
|
|
|
|
SkPath fConvexPath;
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr int kWidth = 900;
|
|
|
|
static constexpr int kHeight = 400;
|
2015-01-28 19:08:00 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new DstReadShuffle; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|