joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #include "gm/gm.h" |
| 8 | #include "include/core/SkBitmap.h" |
| 9 | #include "include/core/SkPath.h" |
| 10 | #include "include/core/SkShader.h" |
| 11 | #include "include/core/SkSurface.h" |
| 12 | #include "include/utils/SkRandom.h" |
| 13 | #include "tools/ToolUtils.h" |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 14 | |
| 15 | namespace skiagm { |
| 16 | |
| 17 | /** |
| 18 | * Renders overlapping shapes with colorburn against a checkerboard. |
| 19 | */ |
| 20 | class DstReadShuffle : public GM { |
| 21 | public: |
Brian Salomon | d02b6f3 | 2017-03-17 10:41:39 -0400 | [diff] [blame] | 22 | DstReadShuffle() { this->setBGColor(kBackground); } |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 23 | |
| 24 | protected: |
| 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 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 35 | SkString onShortName() override { |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 36 | return SkString("dstreadshuffle"); |
| 37 | } |
| 38 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | SkISize onISize() override { |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 40 | return SkISize::Make(530, 680); |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 43 | void drawShape(SkCanvas* canvas, SkPaint* paint, ShapeType type) { |
| 44 | const SkRect kRect = SkRect::MakeXYWH(0, 0, 75.f, 85.f); |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 45 | switch (type) { |
| 46 | case kCircle_ShapeType: |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 47 | canvas->drawCircle(kRect.centerX(), kRect.centerY(), kRect.width() / 2.f, *paint); |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 48 | break; |
| 49 | case kRoundRect_ShapeType: |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 50 | canvas->drawRoundRect(kRect, 15.f, 15.f, *paint); |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 51 | 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 Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 68 | SkPoint points[5] = {{50.f, 0.f}}; |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 69 | SkMatrix rot; |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 70 | rot.setRotate(360.f / 5, 50.f, 70.f); |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 71 | 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 Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 84 | const char* text = "N"; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 85 | SkFont font(ToolUtils::create_portable_typeface(), 100); |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 86 | font.setEmbolden(true); |
| 87 | canvas->drawString(text, 0.f, 100.f, font, *paint); |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 88 | } |
| 89 | default: |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 94 | static SkColor GetColor(SkRandom* random) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 95 | SkColor color = ToolUtils::color_to_565(random->nextU() | 0xFF000000); |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 96 | return SkColorSetA(color, 0x80); |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 99 | static void DrawHairlines(SkCanvas* canvas) { |
Brian Salomon | d02b6f3 | 2017-03-17 10:41:39 -0400 | [diff] [blame] | 100 | if (canvas->imageInfo().alphaType() == kOpaque_SkAlphaType) { |
| 101 | canvas->clear(kBackground); |
| 102 | } else { |
| 103 | canvas->clear(SK_ColorTRANSPARENT); |
| 104 | } |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 105 | SkPaint hairPaint; |
| 106 | hairPaint.setStyle(SkPaint::kStroke_Style); |
| 107 | hairPaint.setStrokeWidth(0); |
| 108 | hairPaint.setAntiAlias(true); |
| 109 | static constexpr int kNumHairlines = 12; |
| 110 | SkPoint pts[] = {{3.f, 7.f}, {29.f, 7.f}}; |
| 111 | SkRandom colorRandom; |
| 112 | SkMatrix rot; |
| 113 | rot.setRotate(360.f / kNumHairlines, 15.5f, 12.f); |
| 114 | rot.postTranslate(3.f, 0); |
| 115 | for (int i = 0; i < 12; ++i) { |
| 116 | hairPaint.setColor(GetColor(&colorRandom)); |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 117 | canvas->drawLine(pts[0], pts[1], hairPaint); |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 118 | rot.mapPoints(pts, 2); |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 122 | void onDraw(SkCanvas* canvas) override { |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 123 | SkScalar y = 5; |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 124 | for (int i = 0; i < kNumShapeTypes; i++) { |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 125 | SkRandom colorRandom; |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 126 | ShapeType shapeType = static_cast<ShapeType>(i); |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 127 | SkScalar x = 5; |
| 128 | for (int r = 0; r <= 15; r++) { |
| 129 | SkPaint p; |
| 130 | p.setAntiAlias(true); |
| 131 | p.setColor(GetColor(&colorRandom)); |
| 132 | // In order to get some op combining on the GPU backend we do 2 src over |
| 133 | // for each xfer mode which requires a dst read |
| 134 | p.setBlendMode(r % 3 == 0 ? SkBlendMode::kColorBurn : SkBlendMode::kSrcOver); |
| 135 | canvas->save(); |
| 136 | canvas->translate(x, y); |
| 137 | this->drawShape(canvas, &p, shapeType); |
| 138 | canvas->restore(); |
| 139 | x += 15; |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 140 | } |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 141 | y += 110; |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 142 | } |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 143 | // Draw hairlines to a surface and then draw that to the main canvas with a zoom so that |
| 144 | // it is easier to see how they blend. |
| 145 | SkImageInfo info; |
Brian Salomon | d02b6f3 | 2017-03-17 10:41:39 -0400 | [diff] [blame] | 146 | // Recording canvases don't have a color type. |
| 147 | if (SkColorType::kUnknown_SkColorType == canvas->imageInfo().colorType()) { |
| 148 | info = SkImageInfo::MakeN32Premul(35, 35); |
| 149 | } else { |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 150 | info = SkImageInfo::Make(35, 35, |
| 151 | canvas->imageInfo().colorType(), |
| 152 | canvas->imageInfo().alphaType(), |
| 153 | canvas->imageInfo().refColorSpace()); |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 154 | } |
| 155 | auto surf = canvas->makeSurface(info); |
Cary Clark | a24712e | 2018-09-05 18:41:40 +0000 | [diff] [blame] | 156 | if (!surf) { |
| 157 | // Fall back to raster. Raster supports only one of the 8 bit per-channel RGBA or BGRA |
| 158 | // formats. This fall back happens when running with --preAbandonGpuContext. |
| 159 | if ((info.colorType() == kRGBA_8888_SkColorType || |
| 160 | info.colorType() == kBGRA_8888_SkColorType) && |
| 161 | info.colorType() != kN32_SkColorType) { |
| 162 | info = SkImageInfo::Make(35, 35, |
| 163 | kN32_SkColorType, |
| 164 | canvas->imageInfo().alphaType(), |
| 165 | canvas->imageInfo().refColorSpace()); |
| 166 | } |
| 167 | surf = SkSurface::MakeRaster(info); |
| 168 | SkASSERT(surf); |
| 169 | } |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 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); |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | private: |
Brian Salomon | d02b6f3 | 2017-03-17 10:41:39 -0400 | [diff] [blame] | 177 | static constexpr SkColor kBackground = SK_ColorLTGRAY; |
Brian Salomon | 8336e94 | 2017-03-15 15:37:43 -0400 | [diff] [blame] | 178 | SkPath fConcavePath; |
| 179 | SkPath fConvexPath; |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 180 | typedef GM INHERITED; |
| 181 | }; |
| 182 | |
| 183 | ////////////////////////////////////////////////////////////////////////////// |
| 184 | |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 185 | DEF_GM( return new DstReadShuffle; ) |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 186 | |
| 187 | } |