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