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