blob: aaef61d63c79d38cd4422817c24c517d5c424e0b [file] [log] [blame]
joshualitt5ce33c12015-01-28 11:08:00 -08001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04008#include "sk_tool_utils.h"
joshualitt5ce33c12015-01-28 11:08:00 -08009#include "SkBitmap.h"
bungemand3ebb482015-08-05 13:57:49 -070010#include "SkPath.h"
joshualitt5ce33c12015-01-28 11:08:00 -080011#include "SkRandom.h"
12#include "SkShader.h"
Brian Salomon8336e942017-03-15 15:37:43 -040013#include "SkSurface.h"
joshualitt5ce33c12015-01-28 11:08:00 -080014
15namespace skiagm {
16
17/**
18 * Renders overlapping shapes with colorburn against a checkerboard.
19 */
20class DstReadShuffle : public GM {
21public:
Brian Salomond02b6f32017-03-17 10:41:39 -040022 DstReadShuffle() { this->setBGColor(kBackground); }
joshualitt5ce33c12015-01-28 11:08:00 -080023
24protected:
25 enum ShapeType {
26 kCircle_ShapeType,
27 kRoundRect_ShapeType,
28 kRect_ShapeType,
29 kConvexPath_ShapeType,
30 kConcavePath_ShapeType,
31 kText_ShapeType,
32 kNumShapeTypes
33 };
34
mtklein36352bf2015-03-25 18:17:31 -070035 SkString onShortName() override {
joshualitt5ce33c12015-01-28 11:08:00 -080036 return SkString("dstreadshuffle");
37 }
38
mtklein36352bf2015-03-25 18:17:31 -070039 SkISize onISize() override {
Brian Salomon8336e942017-03-15 15:37:43 -040040 return SkISize::Make(530, 680);
joshualitt5ce33c12015-01-28 11:08:00 -080041 }
42
Brian Salomon8336e942017-03-15 15:37:43 -040043 void drawShape(SkCanvas* canvas, SkPaint* paint, ShapeType type) {
44 const SkRect kRect = SkRect::MakeXYWH(0, 0, 75.f, 85.f);
joshualitt5ce33c12015-01-28 11:08:00 -080045 switch (type) {
46 case kCircle_ShapeType:
Brian Salomon8336e942017-03-15 15:37:43 -040047 canvas->drawCircle(kRect.centerX(), kRect.centerY(), kRect.width() / 2.f, *paint);
joshualitt5ce33c12015-01-28 11:08:00 -080048 break;
49 case kRoundRect_ShapeType:
Brian Salomon8336e942017-03-15 15:37:43 -040050 canvas->drawRoundRect(kRect, 15.f, 15.f, *paint);
joshualitt5ce33c12015-01-28 11:08:00 -080051 break;
52 case kRect_ShapeType:
53 canvas->drawRect(kRect, *paint);
54 break;
55 case kConvexPath_ShapeType:
56 if (fConvexPath.isEmpty()) {
57 SkPoint points[4];
58 kRect.toQuad(points);
59 fConvexPath.moveTo(points[0]);
60 fConvexPath.quadTo(points[1], points[2]);
61 fConvexPath.quadTo(points[3], points[0]);
62 SkASSERT(fConvexPath.isConvex());
63 }
64 canvas->drawPath(fConvexPath, *paint);
65 break;
66 case kConcavePath_ShapeType:
67 if (fConcavePath.isEmpty()) {
Brian Salomon8336e942017-03-15 15:37:43 -040068 SkPoint points[5] = {{50.f, 0.f}};
joshualitt5ce33c12015-01-28 11:08:00 -080069 SkMatrix rot;
Brian Salomon8336e942017-03-15 15:37:43 -040070 rot.setRotate(360.f / 5, 50.f, 70.f);
joshualitt5ce33c12015-01-28 11:08:00 -080071 for (int i = 1; i < 5; ++i) {
72 rot.mapPoints(points + i, points + i - 1, 1);
73 }
74 fConcavePath.moveTo(points[0]);
75 for (int i = 0; i < 5; ++i) {
76 fConcavePath.lineTo(points[(2 * i) % 5]);
77 }
78 fConcavePath.setFillType(SkPath::kEvenOdd_FillType);
79 SkASSERT(!fConcavePath.isConvex());
80 }
81 canvas->drawPath(fConcavePath, *paint);
82 break;
83 case kText_ShapeType: {
Brian Salomon8336e942017-03-15 15:37:43 -040084 const char* text = "N";
85 paint->setTextSize(100.f);
86 paint->setFakeBoldText(true);
caryclarkf1f8bd52015-07-29 06:58:40 -070087 sk_tool_utils::set_portable_typeface(paint);
Cary Clark2a475ea2017-04-28 15:35:12 -040088 canvas->drawString(text, 0.f, 100.f, *paint);
joshualitt5ce33c12015-01-28 11:08:00 -080089 }
90 default:
91 break;
92 }
93 }
94
Brian Salomon8336e942017-03-15 15:37:43 -040095 static SkColor GetColor(SkRandom* random) {
96 SkColor color = sk_tool_utils::color_to_565(random->nextU() | 0xFF000000);
97 return SkColorSetA(color, 0x80);
joshualitt5ce33c12015-01-28 11:08:00 -080098 }
99
Brian Salomon8336e942017-03-15 15:37:43 -0400100 static void DrawHairlines(SkCanvas* canvas) {
Brian Salomond02b6f32017-03-17 10:41:39 -0400101 if (canvas->imageInfo().alphaType() == kOpaque_SkAlphaType) {
102 canvas->clear(kBackground);
103 } else {
104 canvas->clear(SK_ColorTRANSPARENT);
105 }
Brian Salomon8336e942017-03-15 15:37:43 -0400106 SkPaint hairPaint;
107 hairPaint.setStyle(SkPaint::kStroke_Style);
108 hairPaint.setStrokeWidth(0);
109 hairPaint.setAntiAlias(true);
110 static constexpr int kNumHairlines = 12;
111 SkPoint pts[] = {{3.f, 7.f}, {29.f, 7.f}};
112 SkRandom colorRandom;
113 SkMatrix rot;
114 rot.setRotate(360.f / kNumHairlines, 15.5f, 12.f);
115 rot.postTranslate(3.f, 0);
116 for (int i = 0; i < 12; ++i) {
117 hairPaint.setColor(GetColor(&colorRandom));
Hal Canary23e474c2017-05-15 13:35:35 -0400118 canvas->drawLine(pts[0], pts[1], hairPaint);
Brian Salomon8336e942017-03-15 15:37:43 -0400119 rot.mapPoints(pts, 2);
joshualitt5ce33c12015-01-28 11:08:00 -0800120 }
121 }
122
mtklein36352bf2015-03-25 18:17:31 -0700123 void onDraw(SkCanvas* canvas) override {
Brian Salomon8336e942017-03-15 15:37:43 -0400124 SkScalar y = 5;
joshualitt5ce33c12015-01-28 11:08:00 -0800125 for (int i = 0; i < kNumShapeTypes; i++) {
Brian Salomon8336e942017-03-15 15:37:43 -0400126 SkRandom colorRandom;
joshualitt5ce33c12015-01-28 11:08:00 -0800127 ShapeType shapeType = static_cast<ShapeType>(i);
Brian Salomon8336e942017-03-15 15:37:43 -0400128 SkScalar x = 5;
129 for (int r = 0; r <= 15; r++) {
130 SkPaint p;
131 p.setAntiAlias(true);
132 p.setColor(GetColor(&colorRandom));
133 // In order to get some op combining on the GPU backend we do 2 src over
134 // for each xfer mode which requires a dst read
135 p.setBlendMode(r % 3 == 0 ? SkBlendMode::kColorBurn : SkBlendMode::kSrcOver);
136 canvas->save();
137 canvas->translate(x, y);
138 this->drawShape(canvas, &p, shapeType);
139 canvas->restore();
140 x += 15;
joshualitt5ce33c12015-01-28 11:08:00 -0800141 }
Brian Salomon8336e942017-03-15 15:37:43 -0400142 y += 110;
joshualitt5ce33c12015-01-28 11:08:00 -0800143 }
Brian Salomon8336e942017-03-15 15:37:43 -0400144 // Draw hairlines to a surface and then draw that to the main canvas with a zoom so that
145 // it is easier to see how they blend.
146 SkImageInfo info;
Brian Salomond02b6f32017-03-17 10:41:39 -0400147 // Recording canvases don't have a color type.
148 if (SkColorType::kUnknown_SkColorType == canvas->imageInfo().colorType()) {
149 info = SkImageInfo::MakeN32Premul(35, 35);
150 } else {
Brian Salomon8336e942017-03-15 15:37:43 -0400151 info = SkImageInfo::Make(35, 35,
152 canvas->imageInfo().colorType(),
153 canvas->imageInfo().alphaType(),
154 canvas->imageInfo().refColorSpace());
Brian Salomon8336e942017-03-15 15:37:43 -0400155 }
156 auto surf = canvas->makeSurface(info);
157 if (!surf) {
Brian Salomond02b6f32017-03-17 10:41:39 -0400158 // Fall back to raster. Raster supports only one of the 8 bit per-channel RGBA or BGRA
159 // formats. This fall back happens when running with --preAbandonGpuContext.
160 if ((info.colorType() == kRGBA_8888_SkColorType ||
161 info.colorType() == kBGRA_8888_SkColorType) &&
162 info.colorType() != kN32_SkColorType) {
163 info = SkImageInfo::Make(35, 35,
164 kN32_SkColorType,
165 canvas->imageInfo().alphaType(),
166 canvas->imageInfo().refColorSpace());
167 }
Brian Salomon8336e942017-03-15 15:37:43 -0400168 surf = SkSurface::MakeRaster(info);
169 }
170 canvas->scale(5.f, 5.f);
171 canvas->translate(67.f, 10.f);
172 DrawHairlines(surf->getCanvas());
173 canvas->drawImage(surf->makeImageSnapshot(), 0.f, 0.f);
joshualitt5ce33c12015-01-28 11:08:00 -0800174 }
175
176private:
Brian Salomond02b6f32017-03-17 10:41:39 -0400177 static constexpr SkColor kBackground = SK_ColorLTGRAY;
Brian Salomon8336e942017-03-15 15:37:43 -0400178 SkPath fConcavePath;
179 SkPath fConvexPath;
joshualitt5ce33c12015-01-28 11:08:00 -0800180 typedef GM INHERITED;
181};
182
183//////////////////////////////////////////////////////////////////////////////
184
185static GM* MyFactory(void*) { return new DstReadShuffle; }
186static GMRegistry reg(MyFactory);
187
188}