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 | */ |
| 7 | #include "gm.h" |
| 8 | #include "SkBitmap.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 9 | #include "SkPath.h" |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 10 | #include "SkRandom.h" |
| 11 | #include "SkShader.h" |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 12 | |
| 13 | namespace skiagm { |
| 14 | |
| 15 | /** |
| 16 | * Renders overlapping shapes with colorburn against a checkerboard. |
| 17 | */ |
| 18 | class DstReadShuffle : public GM { |
| 19 | public: |
| 20 | DstReadShuffle() { |
| 21 | this->setBGColor(SkColorSetARGB(0xff, 0xff, 0, 0xff)); |
| 22 | } |
| 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 { |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 40 | return SkISize::Make(kWidth, kHeight); |
| 41 | } |
| 42 | |
| 43 | void drawShape(SkCanvas* canvas, |
| 44 | SkPaint* paint, |
| 45 | ShapeType type) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 46 | const SkRect kRect = SkRect::MakeXYWH(SkIntToScalar(-50), SkIntToScalar(-50), |
| 47 | SkIntToScalar(75), SkIntToScalar(105)); |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 48 | switch (type) { |
| 49 | case kCircle_ShapeType: |
| 50 | canvas->drawCircle(0, 0, 50, *paint); |
| 51 | break; |
| 52 | case kRoundRect_ShapeType: |
| 53 | canvas->drawRoundRect(kRect, SkIntToScalar(10), SkIntToScalar(20), *paint); |
| 54 | break; |
| 55 | case kRect_ShapeType: |
| 56 | canvas->drawRect(kRect, *paint); |
| 57 | break; |
| 58 | case kConvexPath_ShapeType: |
| 59 | if (fConvexPath.isEmpty()) { |
| 60 | SkPoint points[4]; |
| 61 | kRect.toQuad(points); |
| 62 | fConvexPath.moveTo(points[0]); |
| 63 | fConvexPath.quadTo(points[1], points[2]); |
| 64 | fConvexPath.quadTo(points[3], points[0]); |
| 65 | SkASSERT(fConvexPath.isConvex()); |
| 66 | } |
| 67 | canvas->drawPath(fConvexPath, *paint); |
| 68 | break; |
| 69 | case kConcavePath_ShapeType: |
| 70 | if (fConcavePath.isEmpty()) { |
| 71 | SkPoint points[5] = {{0, SkIntToScalar(-50)} }; |
| 72 | SkMatrix rot; |
| 73 | rot.setRotate(SkIntToScalar(360) / 5); |
| 74 | for (int i = 1; i < 5; ++i) { |
| 75 | rot.mapPoints(points + i, points + i - 1, 1); |
| 76 | } |
| 77 | fConcavePath.moveTo(points[0]); |
| 78 | for (int i = 0; i < 5; ++i) { |
| 79 | fConcavePath.lineTo(points[(2 * i) % 5]); |
| 80 | } |
| 81 | fConcavePath.setFillType(SkPath::kEvenOdd_FillType); |
| 82 | SkASSERT(!fConcavePath.isConvex()); |
| 83 | } |
| 84 | canvas->drawPath(fConcavePath, *paint); |
| 85 | break; |
| 86 | case kText_ShapeType: { |
| 87 | const char* text = "Hello!"; |
| 88 | paint->setTextSize(30); |
caryclark | f1f8bd5 | 2015-07-29 06:58:40 -0700 | [diff] [blame] | 89 | sk_tool_utils::set_portable_typeface(paint); |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 90 | canvas->drawText(text, strlen(text), 0, 0, *paint); |
| 91 | } |
| 92 | default: |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
joshualitt | ddb714b | 2015-04-15 08:08:28 -0700 | [diff] [blame] | 97 | static SkColor GetColor(SkRandom* random, int i, int nextColor) { |
| 98 | static SkColor colors[] = { SK_ColorRED, |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 99 | sk_tool_utils::color_to_565(0xFFFF7F00), // Orange |
joshualitt | ddb714b | 2015-04-15 08:08:28 -0700 | [diff] [blame] | 100 | SK_ColorYELLOW, |
| 101 | SK_ColorGREEN, |
| 102 | SK_ColorBLUE, |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 103 | sk_tool_utils::color_to_565(0xFF4B0082), // indigo |
| 104 | sk_tool_utils::color_to_565(0xFF7F00FF) }; // violet |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 105 | SkColor color; |
joshualitt | ddb714b | 2015-04-15 08:08:28 -0700 | [diff] [blame] | 106 | int index = nextColor % SK_ARRAY_COUNT(colors); |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 107 | switch (i) { |
| 108 | case 0: |
| 109 | color = SK_ColorTRANSPARENT; |
| 110 | break; |
| 111 | case 1: |
| 112 | color = SkColorSetARGB(0xff, |
joshualitt | ddb714b | 2015-04-15 08:08:28 -0700 | [diff] [blame] | 113 | SkColorGetR(colors[index]), |
| 114 | SkColorGetG(colors[index]), |
| 115 | SkColorGetB(colors[index])); |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 116 | break; |
| 117 | default: |
joshualitt | ddb714b | 2015-04-15 08:08:28 -0700 | [diff] [blame] | 118 | uint8_t alpha = 0x80; |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 119 | color = SkColorSetARGB(alpha, |
joshualitt | ddb714b | 2015-04-15 08:08:28 -0700 | [diff] [blame] | 120 | SkColorGetR(colors[index]), |
| 121 | SkColorGetG(colors[index]), |
| 122 | SkColorGetB(colors[index])); |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 123 | break; |
| 124 | } |
| 125 | return color; |
| 126 | } |
| 127 | |
| 128 | static void SetStyle(SkPaint* p, int style, int width) { |
| 129 | switch (style) { |
| 130 | case 0: |
| 131 | p->setStyle(SkPaint::kStroke_Style); |
| 132 | p->setStrokeWidth((SkScalar)width); |
| 133 | break; |
| 134 | case 1: |
| 135 | p->setStyle(SkPaint::kStrokeAndFill_Style); |
| 136 | p->setStrokeWidth((SkScalar)width); |
| 137 | break; |
| 138 | default: |
| 139 | p->setStyle(SkPaint::kFill_Style); |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 144 | void onDraw(SkCanvas* canvas) override { |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 145 | SkRandom random; |
| 146 | SkScalar y = 100; |
| 147 | for (int i = 0; i < kNumShapeTypes; i++) { |
| 148 | ShapeType shapeType = static_cast<ShapeType>(i); |
| 149 | SkScalar x = 25; |
| 150 | for (int style = 0; style < 3; style++) { |
| 151 | for (int width = 0; width <= 1; width++) { |
| 152 | for (int alpha = 0; alpha <= 2; alpha++) { |
| 153 | for (int r = 0; r <= 5; r++) { |
joshualitt | ddb714b | 2015-04-15 08:08:28 -0700 | [diff] [blame] | 154 | SkColor color = GetColor(&random, alpha, style + width + alpha + r); |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 155 | |
| 156 | SkPaint p; |
| 157 | p.setAntiAlias(true); |
| 158 | p.setColor(color); |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 159 | // In order to get some op combining on the GPU backend we do 2 src over |
| 160 | // for each xfer mode which requires a dst read |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 161 | p.setBlendMode(r % 3 == 0 ? SkBlendMode::kLighten : |
| 162 | SkBlendMode::kSrcOver); |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 163 | SetStyle(&p, style, width); |
| 164 | canvas->save(); |
| 165 | canvas->translate(x, y); |
| 166 | canvas->rotate((SkScalar)(r < 3 ? 10 : 0)); |
| 167 | this->drawShape(canvas, &p, shapeType); |
| 168 | canvas->restore(); |
| 169 | x += 8; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | y += 50; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | private: |
| 179 | enum { |
| 180 | kNumShapes = 100, |
| 181 | }; |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 182 | sk_sp<SkShader> fBG; |
| 183 | SkPath fConcavePath; |
| 184 | SkPath fConvexPath; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 185 | static constexpr int kWidth = 900; |
| 186 | static constexpr int kHeight = 400; |
joshualitt | 5ce33c1 | 2015-01-28 11:08:00 -0800 | [diff] [blame] | 187 | typedef GM INHERITED; |
| 188 | }; |
| 189 | |
| 190 | ////////////////////////////////////////////////////////////////////////////// |
| 191 | |
| 192 | static GM* MyFactory(void*) { return new DstReadShuffle; } |
| 193 | static GMRegistry reg(MyFactory); |
| 194 | |
| 195 | } |