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 "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"
|
2017-03-15 19:37:43 +00:00
|
|
|
#include "SkSurface.h"
|
2019-03-20 16:12:10 +00:00
|
|
|
#include "ToolUtils.h"
|
|
|
|
#include "gm.h"
|
2015-01-28 19:08:00 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders overlapping shapes with colorburn against a checkerboard.
|
|
|
|
*/
|
|
|
|
class DstReadShuffle : public GM {
|
|
|
|
public:
|
2017-03-17 14:41:39 +00:00
|
|
|
DstReadShuffle() { this->setBGColor(kBackground); }
|
2015-01-28 19:08:00 +00:00
|
|
|
|
|
|
|
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 {
|
2017-03-15 19:37:43 +00:00
|
|
|
return SkISize::Make(530, 680);
|
2015-01-28 19:08:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-15 19:37:43 +00:00
|
|
|
void drawShape(SkCanvas* canvas, SkPaint* paint, ShapeType type) {
|
|
|
|
const SkRect kRect = SkRect::MakeXYWH(0, 0, 75.f, 85.f);
|
2015-01-28 19:08:00 +00:00
|
|
|
switch (type) {
|
|
|
|
case kCircle_ShapeType:
|
2017-03-15 19:37:43 +00:00
|
|
|
canvas->drawCircle(kRect.centerX(), kRect.centerY(), kRect.width() / 2.f, *paint);
|
2015-01-28 19:08:00 +00:00
|
|
|
break;
|
|
|
|
case kRoundRect_ShapeType:
|
2017-03-15 19:37:43 +00:00
|
|
|
canvas->drawRoundRect(kRect, 15.f, 15.f, *paint);
|
2015-01-28 19:08:00 +00:00
|
|
|
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()) {
|
2017-03-15 19:37:43 +00:00
|
|
|
SkPoint points[5] = {{50.f, 0.f}};
|
2015-01-28 19:08:00 +00:00
|
|
|
SkMatrix rot;
|
2017-03-15 19:37:43 +00:00
|
|
|
rot.setRotate(360.f / 5, 50.f, 70.f);
|
2015-01-28 19:08:00 +00:00
|
|
|
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: {
|
2017-03-15 19:37:43 +00:00
|
|
|
const char* text = "N";
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), 100);
|
2019-01-07 21:01:22 +00:00
|
|
|
font.setEmbolden(true);
|
|
|
|
canvas->drawString(text, 0.f, 100.f, font, *paint);
|
2015-01-28 19:08:00 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-15 19:37:43 +00:00
|
|
|
static SkColor GetColor(SkRandom* random) {
|
2019-03-20 16:12:10 +00:00
|
|
|
SkColor color = ToolUtils::color_to_565(random->nextU() | 0xFF000000);
|
2017-03-15 19:37:43 +00:00
|
|
|
return SkColorSetA(color, 0x80);
|
2015-01-28 19:08:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-15 19:37:43 +00:00
|
|
|
static void DrawHairlines(SkCanvas* canvas) {
|
2017-03-17 14:41:39 +00:00
|
|
|
if (canvas->imageInfo().alphaType() == kOpaque_SkAlphaType) {
|
|
|
|
canvas->clear(kBackground);
|
|
|
|
} else {
|
|
|
|
canvas->clear(SK_ColorTRANSPARENT);
|
|
|
|
}
|
2017-03-15 19:37:43 +00:00
|
|
|
SkPaint hairPaint;
|
|
|
|
hairPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
hairPaint.setStrokeWidth(0);
|
|
|
|
hairPaint.setAntiAlias(true);
|
|
|
|
static constexpr int kNumHairlines = 12;
|
|
|
|
SkPoint pts[] = {{3.f, 7.f}, {29.f, 7.f}};
|
|
|
|
SkRandom colorRandom;
|
|
|
|
SkMatrix rot;
|
|
|
|
rot.setRotate(360.f / kNumHairlines, 15.5f, 12.f);
|
|
|
|
rot.postTranslate(3.f, 0);
|
|
|
|
for (int i = 0; i < 12; ++i) {
|
|
|
|
hairPaint.setColor(GetColor(&colorRandom));
|
2017-05-15 17:35:35 +00:00
|
|
|
canvas->drawLine(pts[0], pts[1], hairPaint);
|
2017-03-15 19:37:43 +00:00
|
|
|
rot.mapPoints(pts, 2);
|
2015-01-28 19:08:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2017-03-15 19:37:43 +00:00
|
|
|
SkScalar y = 5;
|
2015-01-28 19:08:00 +00:00
|
|
|
for (int i = 0; i < kNumShapeTypes; i++) {
|
2017-03-15 19:37:43 +00:00
|
|
|
SkRandom colorRandom;
|
2015-01-28 19:08:00 +00:00
|
|
|
ShapeType shapeType = static_cast<ShapeType>(i);
|
2017-03-15 19:37:43 +00:00
|
|
|
SkScalar x = 5;
|
|
|
|
for (int r = 0; r <= 15; r++) {
|
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
p.setColor(GetColor(&colorRandom));
|
|
|
|
// In order to get some op combining on the GPU backend we do 2 src over
|
|
|
|
// for each xfer mode which requires a dst read
|
|
|
|
p.setBlendMode(r % 3 == 0 ? SkBlendMode::kColorBurn : SkBlendMode::kSrcOver);
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(x, y);
|
|
|
|
this->drawShape(canvas, &p, shapeType);
|
|
|
|
canvas->restore();
|
|
|
|
x += 15;
|
2015-01-28 19:08:00 +00:00
|
|
|
}
|
2017-03-15 19:37:43 +00:00
|
|
|
y += 110;
|
|
|
|
}
|
|
|
|
// Draw hairlines to a surface and then draw that to the main canvas with a zoom so that
|
|
|
|
// it is easier to see how they blend.
|
|
|
|
SkImageInfo info;
|
2017-03-17 14:41:39 +00:00
|
|
|
// Recording canvases don't have a color type.
|
|
|
|
if (SkColorType::kUnknown_SkColorType == canvas->imageInfo().colorType()) {
|
|
|
|
info = SkImageInfo::MakeN32Premul(35, 35);
|
|
|
|
} else {
|
2017-03-15 19:37:43 +00:00
|
|
|
info = SkImageInfo::Make(35, 35,
|
|
|
|
canvas->imageInfo().colorType(),
|
|
|
|
canvas->imageInfo().alphaType(),
|
|
|
|
canvas->imageInfo().refColorSpace());
|
2015-01-28 19:08:00 +00:00
|
|
|
}
|
2017-03-15 19:37:43 +00:00
|
|
|
auto surf = canvas->makeSurface(info);
|
2018-09-05 18:41:40 +00:00
|
|
|
if (!surf) {
|
|
|
|
// Fall back to raster. Raster supports only one of the 8 bit per-channel RGBA or BGRA
|
|
|
|
// formats. This fall back happens when running with --preAbandonGpuContext.
|
|
|
|
if ((info.colorType() == kRGBA_8888_SkColorType ||
|
|
|
|
info.colorType() == kBGRA_8888_SkColorType) &&
|
|
|
|
info.colorType() != kN32_SkColorType) {
|
|
|
|
info = SkImageInfo::Make(35, 35,
|
|
|
|
kN32_SkColorType,
|
|
|
|
canvas->imageInfo().alphaType(),
|
|
|
|
canvas->imageInfo().refColorSpace());
|
|
|
|
}
|
|
|
|
surf = SkSurface::MakeRaster(info);
|
|
|
|
SkASSERT(surf);
|
|
|
|
}
|
2017-03-15 19:37:43 +00:00
|
|
|
canvas->scale(5.f, 5.f);
|
|
|
|
canvas->translate(67.f, 10.f);
|
|
|
|
DrawHairlines(surf->getCanvas());
|
|
|
|
canvas->drawImage(surf->makeImageSnapshot(), 0.f, 0.f);
|
2015-01-28 19:08:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-03-17 14:41:39 +00:00
|
|
|
static constexpr SkColor kBackground = SK_ColorLTGRAY;
|
2017-03-15 19:37:43 +00:00
|
|
|
SkPath fConcavePath;
|
|
|
|
SkPath fConvexPath;
|
2015-01-28 19:08:00 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-01-23 15:22:01 +00:00
|
|
|
DEF_GM( return new DstReadShuffle; )
|
2015-01-28 19:08:00 +00:00
|
|
|
|
|
|
|
}
|