Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2015 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.h" | |
| 9 #include "SkColorPriv.h" | |
| 10 #include "SkGradientShader.h" | |
| 11 #include "SkImage.h" | |
| 12 #include "SkRandom.h" | |
| 13 #include "SkShader.h" | |
| 14 #include "SkSurface.h" | |
| 15 | |
| 16 static SkImage* makebm(SkCanvas* caller, int w, int h) { | |
| 17 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); | |
| 18 SkAutoTUnref<SkSurface> surface(caller->newSurface(info)); | |
| 19 if (NULL == surface) { | |
| 20 surface.reset(SkSurface::NewRaster(info)); | |
| 21 } | |
| 22 SkCanvas* canvas = surface->getCanvas(); | |
| 23 | |
| 24 const SkScalar wScalar = SkIntToScalar(w); | |
| 25 const SkScalar hScalar = SkIntToScalar(h); | |
| 26 | |
| 27 const SkPoint pt = { wScalar / 2, hScalar / 2 }; | |
| 28 | |
| 29 const SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar); | |
| 30 | |
| 31 static const SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW, | |
| 32 SK_ColorGREEN, SK_ColorMAGENTA, | |
| 33 SK_ColorBLUE, SK_ColorCYAN, | |
| 34 SK_ColorRED}; | |
| 35 | |
| 36 static const SkScalar pos[] = {0, | |
| 37 SK_Scalar1 / 6, | |
| 38 2 * SK_Scalar1 / 6, | |
| 39 3 * SK_Scalar1 / 6, | |
| 40 4 * SK_Scalar1 / 6, | |
| 41 5 * SK_Scalar1 / 6, | |
| 42 SK_Scalar1}; | |
| 43 | |
| 44 SkASSERT(SK_ARRAY_COUNT(colors) == SK_ARRAY_COUNT(pos)); | |
| 45 SkPaint paint; | |
| 46 SkRect rect = SkRect::MakeWH(wScalar, hScalar); | |
| 47 SkMatrix mat = SkMatrix::I(); | |
| 48 for (int i = 0; i < 4; ++i) { | |
| 49 paint.setShader(SkGradientShader::CreateRadial( | |
| 50 pt, radius, | |
| 51 colors, pos, | |
| 52 SK_ARRAY_COUNT(colors), | |
| 53 SkShader::kRepeat_TileMode, | |
| 54 0, &mat))->unref(); | |
| 55 canvas->drawRect(rect, paint); | |
| 56 rect.inset(wScalar / 8, hScalar / 8); | |
| 57 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4); | |
| 58 } | |
| 59 return surface->newImageSnapshot(); | |
| 60 } | |
| 61 | |
| 62 static const int gSize = 1024; | |
| 63 static const int gSurfaceSize = 2048; | |
| 64 | |
| 65 // This GM calls drawImageRect several times using the same texture. This is | |
| 66 // intended to exercise batching of these calls. | |
| 67 class DrawMiniBitmapRectGM : public skiagm::GM { | |
| 68 public: | |
| 69 DrawMiniBitmapRectGM(bool antiAlias) : fAA(antiAlias) { | |
| 70 fName.set("drawminibitmaprect"); | |
| 71 if (fAA) { | |
| 72 fName.appendf("_aa"); | |
| 73 } | |
| 74 } | |
| 75 | |
|
robertphillips
2015/08/14 12:43:36
Make these private ?
| |
| 76 bool fAA; | |
| 77 SkAutoTUnref<SkImage> fImage; | |
| 78 SkString fName; | |
| 79 | |
| 80 protected: | |
| 81 SkString onShortName() override { return fName; } | |
| 82 | |
| 83 SkISize onISize() override { return SkISize::Make(gSize, gSize); } | |
| 84 | |
| 85 void onDraw(SkCanvas* canvas) override { | |
| 86 if (NULL == fImage) { | |
| 87 fImage.reset(makebm(canvas, gSurfaceSize, gSurfaceSize)); | |
| 88 } | |
| 89 | |
| 90 const SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)}; | |
| 91 static const int kMaxSrcRectSize = 1 << (SkNextLog2(gSurfaceSize) + 2); | |
| 92 | |
| 93 static const int kPadX = 30; | |
| 94 static const int kPadY = 40; | |
| 95 | |
| 96 int rowCount = 0; | |
| 97 canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY)); | |
| 98 canvas->save(); | |
| 99 SkRandom random; | |
| 100 | |
| 101 SkPaint paint; | |
| 102 paint.setAntiAlias(fAA); | |
| 103 for (int w = 1; w <= kMaxSrcRectSize; w *= 3) { | |
| 104 for (int h = 1; h <= kMaxSrcRectSize; h *= 3) { | |
| 105 | |
| 106 const SkIRect srcRect = | |
| 107 SkIRect::MakeXYWH((gSurfaceSize - w) / 2, (gSurfaceSize - h) / 2, w, h); | |
| 108 canvas->save(); | |
| 109 switch (random.nextU() % 3) { | |
| 110 case 0: | |
| 111 canvas->rotate(random.nextF() * 10.f); | |
| 112 break; | |
| 113 case 1: | |
| 114 canvas->rotate(-random.nextF() * 10.f); | |
| 115 break; | |
| 116 case 2: | |
| 117 // rect stays rect | |
| 118 break; | |
| 119 } | |
| 120 canvas->drawImageRect(fImage, srcRect, dstRect, &paint, | |
| 121 SkCanvas::kFast_SrcRectConstraint); | |
| 122 canvas->restore(); | |
| 123 | |
| 124 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0); | |
| 125 ++rowCount; | |
| 126 if ((dstRect.width() + 2 * kPadX) * rowCount > gSize) { | |
| 127 canvas->restore(); | |
| 128 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY); | |
| 129 canvas->save(); | |
| 130 rowCount = 0; | |
| 131 } | |
| 132 } | |
| 133 } | |
| 134 canvas->restore(); | |
| 135 } | |
| 136 | |
| 137 private: | |
| 138 typedef skiagm::GM INHERITED; | |
| 139 }; | |
| 140 | |
| 141 DEF_GM( return new DrawMiniBitmapRectGM(true); ) | |
| 142 DEF_GM( return new DrawMiniBitmapRectGM(false); ) | |
| 143 | |
| OLD | NEW |