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