OLD | NEW |
(Empty) | |
| 1 |
| 2 /* |
| 3 * Copyright 2013 Google Inc. |
| 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. |
| 7 */ |
| 8 #include "gm.h" |
| 9 #include "SkBitmap.h" |
| 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 |
| 36 SkString onShortName() SK_OVERRIDE { |
| 37 return SkString("dstreadshuffle"); |
| 38 } |
| 39 |
| 40 SkISize onISize() SK_OVERRIDE { |
| 41 return SkISize::Make(kWidth, kHeight); |
| 42 } |
| 43 |
| 44 void drawShape(SkCanvas* canvas, |
| 45 SkPaint* paint, |
| 46 ShapeType type) { |
| 47 static const SkRect kRect = SkRect::MakeXYWH(SkIntToScalar(-50), SkIntTo
Scalar(-50), |
| 48 SkIntToScalar(75), SkIntToS
calar(105)); |
| 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); |
| 90 canvas->drawText(text, strlen(text), 0, 0, *paint); |
| 91 } |
| 92 default: |
| 93 break; |
| 94 } |
| 95 } |
| 96 |
| 97 static SkColor GetColor(SkRandom* random, int i) { |
| 98 SkColor color; |
| 99 switch (i) { |
| 100 case 0: |
| 101 color = SK_ColorTRANSPARENT; |
| 102 break; |
| 103 case 1: |
| 104 color = SkColorSetARGB(0xff, |
| 105 random->nextULessThan(256), |
| 106 random->nextULessThan(256), |
| 107 random->nextULessThan(256)); |
| 108 break; |
| 109 default: |
| 110 uint8_t alpha = random->nextULessThan(256); |
| 111 color = SkColorSetARGB(alpha, |
| 112 random->nextRangeU(0, alpha), |
| 113 random->nextRangeU(0, alpha), |
| 114 random->nextRangeU(0, alpha)); |
| 115 break; |
| 116 } |
| 117 return color; |
| 118 } |
| 119 |
| 120 static void SetStyle(SkPaint* p, int style, int width) { |
| 121 switch (style) { |
| 122 case 0: |
| 123 p->setStyle(SkPaint::kStroke_Style); |
| 124 p->setStrokeWidth((SkScalar)width); |
| 125 break; |
| 126 case 1: |
| 127 p->setStyle(SkPaint::kStrokeAndFill_Style); |
| 128 p->setStrokeWidth((SkScalar)width); |
| 129 break; |
| 130 default: |
| 131 p->setStyle(SkPaint::kFill_Style); |
| 132 break; |
| 133 } |
| 134 } |
| 135 |
| 136 void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 137 SkRandom random; |
| 138 SkScalar y = 100; |
| 139 for (int i = 0; i < kNumShapeTypes; i++) { |
| 140 ShapeType shapeType = static_cast<ShapeType>(i); |
| 141 SkScalar x = 25; |
| 142 for (int style = 0; style < 3; style++) { |
| 143 for (int width = 0; width <= 1; width++) { |
| 144 for (int alpha = 0; alpha <= 2; alpha++) { |
| 145 for (int r = 0; r <= 5; r++) { |
| 146 SkColor color = GetColor(&random, alpha); |
| 147 |
| 148 SkPaint p; |
| 149 p.setAntiAlias(true); |
| 150 p.setColor(color); |
| 151 p.setXfermodeMode(r % 3 == 0 ? SkXfermode::kHardLigh
t_Mode : |
| 152 SkXfermode::kSrcOver_
Mode); |
| 153 SetStyle(&p, style, width); |
| 154 canvas->save(); |
| 155 canvas->translate(x, y); |
| 156 canvas->rotate((SkScalar)(r < 3 ? 10 : 0)); |
| 157 this->drawShape(canvas, &p, shapeType); |
| 158 canvas->restore(); |
| 159 x += 8; |
| 160 } |
| 161 } |
| 162 } |
| 163 } |
| 164 y += 50; |
| 165 } |
| 166 } |
| 167 |
| 168 private: |
| 169 enum { |
| 170 kNumShapes = 100, |
| 171 }; |
| 172 SkAutoTUnref<SkShader> fBG; |
| 173 SkPath fConcavePath; |
| 174 SkPath fConvexPath; |
| 175 static const int kWidth = 900; |
| 176 static const int kHeight = 400; |
| 177 typedef GM INHERITED; |
| 178 }; |
| 179 |
| 180 ////////////////////////////////////////////////////////////////////////////// |
| 181 |
| 182 static GM* MyFactory(void*) { return new DstReadShuffle; } |
| 183 static GMRegistry reg(MyFactory); |
| 184 |
| 185 } |
OLD | NEW |