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 "SkGradientShader.h" |
| 10 #include "SkImage.h" |
| 11 #include "SkPath.h" |
| 12 #include "SkSurface.h" |
| 13 |
| 14 static SkImage* make_image(SkCanvas* origCanvas, int w, int h) { |
| 15 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); |
| 16 SkAutoTUnref<SkSurface> surface(origCanvas->newSurface(info)); |
| 17 if (nullptr == surface) { |
| 18 surface.reset(SkSurface::NewRaster(info)); |
| 19 } |
| 20 SkCanvas* canvas = surface->getCanvas(); |
| 21 |
| 22 sk_tool_utils::draw_checkerboard(canvas, SK_ColorRED, SK_ColorGREEN, w/10); |
| 23 return surface->newImageSnapshot(); |
| 24 } |
| 25 |
| 26 static SkBitmap make_bitmap(int w, int h) { |
| 27 SkBitmap bitmap; |
| 28 bitmap.allocN32Pixels(w, h); |
| 29 SkCanvas canvas(bitmap); |
| 30 |
| 31 sk_tool_utils::draw_checkerboard(&canvas, SK_ColorBLUE, SK_ColorYELLOW, w/10
); |
| 32 return bitmap; |
| 33 } |
| 34 |
| 35 namespace skiagm { |
| 36 |
| 37 class PerspShadersGM : public GM { |
| 38 public: |
| 39 PerspShadersGM(bool doAA) : fDoAA(doAA) { } |
| 40 |
| 41 protected: |
| 42 SkString onShortName() override { |
| 43 SkString name; |
| 44 name.printf("persp_shaders_%s", |
| 45 fDoAA ? "aa" : "bw"); |
| 46 return name; |
| 47 } |
| 48 |
| 49 SkISize onISize() override { |
| 50 return SkISize::Make(kCellSize*kNumCols, kCellSize*kNumRows); |
| 51 } |
| 52 |
| 53 void onOnceBeforeDraw() override { |
| 54 fBitmap = make_bitmap(kCellSize, kCellSize); |
| 55 |
| 56 fBitmapShader.reset(SkShader::CreateBitmapShader(fBitmap, |
| 57 SkShader::kClamp_TileMo
de, |
| 58 SkShader::kClamp_TileMo
de)); |
| 59 SkPoint pts1[] = { |
| 60 { 0, 0 }, |
| 61 { SkIntToScalar(kCellSize), SkIntToScalar(kCellSize) } |
| 62 }; |
| 63 SkPoint pts2[] = { |
| 64 { 0, 0 }, |
| 65 { 0, SkIntToScalar(kCellSize) } |
| 66 }; |
| 67 static const SkColor colors[] = { |
| 68 SK_ColorRED, SK_ColorGREEN, SK_ColorRED, SK_ColorGREEN, SK_ColorRED |
| 69 }; |
| 70 static const SkScalar pos[] = { 0, 0.25f, 0.5f, 0.75f, SK_Scalar1 }; |
| 71 |
| 72 fLinearGrad1.reset(SkGradientShader::CreateLinear(pts1, colors, pos, |
| 73 SK_ARRAY_COUNT(colors)
, |
| 74 SkShader::kClamp_TileM
ode)); |
| 75 fLinearGrad2.reset(SkGradientShader::CreateLinear(pts2, colors, pos, |
| 76 SK_ARRAY_COUNT(colors)
, |
| 77 SkShader::kClamp_TileM
ode)); |
| 78 |
| 79 fPerspMatrix.reset(); |
| 80 fPerspMatrix.setPerspY(SK_Scalar1 / 50); |
| 81 |
| 82 fPath.moveTo(0, 0); |
| 83 fPath.lineTo(0, SkIntToScalar(kCellSize)); |
| 84 fPath.lineTo(kCellSize/2.0f, kCellSize/2.0f); |
| 85 fPath.lineTo(SkIntToScalar(kCellSize), SkIntToScalar(kCellSize)); |
| 86 fPath.lineTo(SkIntToScalar(kCellSize), 0); |
| 87 fPath.close(); |
| 88 } |
| 89 |
| 90 void drawRow(SkCanvas* canvas, SkFilterQuality filterQ) { |
| 91 SkPaint filterPaint; |
| 92 filterPaint.setFilterQuality(filterQ); |
| 93 filterPaint.setAntiAlias(fDoAA); |
| 94 |
| 95 SkPaint pathPaint; |
| 96 pathPaint.setShader(fBitmapShader); |
| 97 pathPaint.setFilterQuality(filterQ); |
| 98 pathPaint.setAntiAlias(fDoAA); |
| 99 |
| 100 SkPaint gradPaint1; |
| 101 gradPaint1.setShader(fLinearGrad1); |
| 102 gradPaint1.setAntiAlias(fDoAA); |
| 103 SkPaint gradPaint2; |
| 104 gradPaint2.setShader(fLinearGrad2); |
| 105 gradPaint2.setAntiAlias(fDoAA); |
| 106 |
| 107 SkRect r = SkRect::MakeWH(SkIntToScalar(kCellSize), SkIntToScalar(kCellS
ize)); |
| 108 |
| 109 canvas->save(); |
| 110 |
| 111 canvas->save(); |
| 112 canvas->concat(fPerspMatrix); |
| 113 canvas->drawBitmapRect(fBitmap, r, &filterPaint); |
| 114 canvas->restore(); |
| 115 |
| 116 canvas->translate(SkIntToScalar(kCellSize), 0); |
| 117 canvas->save(); |
| 118 canvas->concat(fPerspMatrix); |
| 119 canvas->drawImage(fImage, 0, 0, &filterPaint); |
| 120 canvas->restore(); |
| 121 |
| 122 canvas->translate(SkIntToScalar(kCellSize), 0); |
| 123 canvas->save(); |
| 124 canvas->concat(fPerspMatrix); |
| 125 canvas->drawRect(r, pathPaint); |
| 126 canvas->restore(); |
| 127 |
| 128 canvas->translate(SkIntToScalar(kCellSize), 0); |
| 129 canvas->save(); |
| 130 canvas->concat(fPerspMatrix); |
| 131 canvas->drawPath(fPath, pathPaint); |
| 132 canvas->restore(); |
| 133 |
| 134 canvas->translate(SkIntToScalar(kCellSize), 0); |
| 135 canvas->save(); |
| 136 canvas->concat(fPerspMatrix); |
| 137 canvas->drawRect(r, gradPaint1); |
| 138 canvas->restore(); |
| 139 |
| 140 canvas->translate(SkIntToScalar(kCellSize), 0); |
| 141 canvas->save(); |
| 142 canvas->concat(fPerspMatrix); |
| 143 canvas->drawPath(fPath, gradPaint2); |
| 144 canvas->restore(); |
| 145 |
| 146 canvas->restore(); |
| 147 } |
| 148 |
| 149 void onDraw(SkCanvas* canvas) override { |
| 150 if (!fImage) { |
| 151 fImage.reset(make_image(canvas, kCellSize, kCellSize)); |
| 152 } |
| 153 |
| 154 this->drawRow(canvas, kNone_SkFilterQuality); |
| 155 canvas->translate(0, SkIntToScalar(kCellSize)); |
| 156 this->drawRow(canvas, kLow_SkFilterQuality); |
| 157 canvas->translate(0, SkIntToScalar(kCellSize)); |
| 158 this->drawRow(canvas, kMedium_SkFilterQuality); |
| 159 canvas->translate(0, SkIntToScalar(kCellSize)); |
| 160 this->drawRow(canvas, kHigh_SkFilterQuality); |
| 161 canvas->translate(0, SkIntToScalar(kCellSize)); |
| 162 } |
| 163 private: |
| 164 static const int kCellSize = 50; |
| 165 static const int kNumRows = 4; |
| 166 static const int kNumCols = 6; |
| 167 |
| 168 bool fDoAA; |
| 169 SkPath fPath; |
| 170 SkAutoTUnref<SkShader> fBitmapShader; |
| 171 SkAutoTUnref<SkShader> fLinearGrad1; |
| 172 SkAutoTUnref<SkShader> fLinearGrad2; |
| 173 SkMatrix fPerspMatrix; |
| 174 SkAutoTUnref<SkImage> fImage; |
| 175 SkBitmap fBitmap; |
| 176 |
| 177 typedef GM INHERITED; |
| 178 }; |
| 179 |
| 180 ////////////////////////////////////////////////////////////////////////////// |
| 181 |
| 182 DEF_GM(return new PerspShadersGM(true);) |
| 183 DEF_GM(return new PerspShadersGM(false);) |
| 184 } |
OLD | NEW |