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 "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) { | |
|
joshualitt
2015/09/02 17:37:20
could we make these guys helpers in some way?
robertphillips
2015/09/03 15:25:08
Acknowledged. Will add in a separate patch.
| |
| 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 static const int kCellSize = 50; | |
| 43 | |
| 44 SkString onShortName() override { | |
| 45 SkString name; | |
| 46 name.printf("persp_shaders_%s", | |
| 47 fDoAA ? "aa" : "bw"); | |
| 48 return name; | |
| 49 } | |
| 50 | |
| 51 SkISize onISize() override { | |
| 52 return SkISize::Make(kCellSize*6, kCellSize*5); | |
|
joshualitt
2015/09/02 17:37:20
can these be constants as well?
robertphillips
2015/09/03 15:25:08
Done.
| |
| 53 } | |
| 54 | |
| 55 void onOnceBeforeDraw() override { | |
| 56 fBitmap = make_bitmap(kCellSize, kCellSize); | |
| 57 | |
| 58 fBitmapShader.reset(SkShader::CreateBitmapShader(fBitmap, | |
| 59 SkShader::kClamp_TileMo de, | |
| 60 SkShader::kClamp_TileMo de)); | |
| 61 SkPoint pts1[] = { | |
| 62 { 0, 0 }, | |
| 63 { SkIntToScalar(kCellSize), SkIntToScalar(kCellSize) } | |
| 64 }; | |
| 65 SkPoint pts2[] = { | |
| 66 { 0, 0 }, | |
| 67 { 0, SkIntToScalar(kCellSize) } | |
| 68 }; | |
| 69 static const SkColor colors[] = { | |
| 70 SK_ColorRED, SK_ColorGREEN, SK_ColorRED, SK_ColorGREEN, SK_ColorRED | |
| 71 }; | |
| 72 static const SkScalar pos[] = { 0, 0.25f, 0.5f, 0.75f, SK_Scalar1 }; | |
| 73 | |
| 74 fLinearGrad1.reset(SkGradientShader::CreateLinear(pts1, colors, pos, | |
| 75 SK_ARRAY_COUNT(colors) , | |
| 76 SkShader::kClamp_TileM ode)); | |
| 77 fLinearGrad2.reset(SkGradientShader::CreateLinear(pts2, colors, pos, | |
| 78 SK_ARRAY_COUNT(colors) , | |
| 79 SkShader::kClamp_TileM ode)); | |
| 80 | |
| 81 fPerspMatrix.reset(); | |
| 82 fPerspMatrix.setPerspY(SK_Scalar1 / 50); | |
| 83 | |
| 84 fPath.moveTo(0, 0); | |
| 85 fPath.lineTo(0, SkIntToScalar(kCellSize)); | |
| 86 fPath.lineTo(kCellSize/2.0f, kCellSize/2.0f); | |
| 87 fPath.lineTo(SkIntToScalar(kCellSize), SkIntToScalar(kCellSize)); | |
| 88 fPath.lineTo(SkIntToScalar(kCellSize), 0); | |
| 89 fPath.close(); | |
| 90 } | |
| 91 | |
| 92 void drawRow(SkCanvas* canvas, SkFilterQuality filterQ) { | |
| 93 SkPaint filterPaint; | |
| 94 filterPaint.setFilterQuality(filterQ); | |
| 95 filterPaint.setAntiAlias(fDoAA); | |
| 96 | |
| 97 SkPaint pathPaint; | |
| 98 pathPaint.setShader(fBitmapShader); | |
| 99 pathPaint.setFilterQuality(filterQ); | |
| 100 pathPaint.setAntiAlias(fDoAA); | |
| 101 | |
| 102 SkPaint gradPaint1; | |
| 103 gradPaint1.setShader(fLinearGrad1); | |
| 104 gradPaint1.setAntiAlias(fDoAA); | |
| 105 SkPaint gradPaint2; | |
| 106 gradPaint2.setShader(fLinearGrad2); | |
| 107 gradPaint2.setAntiAlias(fDoAA); | |
| 108 | |
| 109 SkRect r = SkRect::MakeWH(SkIntToScalar(kCellSize), SkIntToScalar(kCellS ize)); | |
| 110 | |
| 111 canvas->save(); | |
| 112 | |
| 113 canvas->save(); | |
| 114 canvas->concat(fPerspMatrix); | |
| 115 canvas->drawBitmapRect(fBitmap, r, &filterPaint); | |
| 116 canvas->restore(); | |
| 117 | |
| 118 canvas->translate(SkIntToScalar(kCellSize), 0); | |
| 119 canvas->save(); | |
| 120 canvas->concat(fPerspMatrix); | |
| 121 canvas->drawImage(fImage, 0, 0, &filterPaint); | |
| 122 canvas->restore(); | |
| 123 | |
| 124 canvas->translate(SkIntToScalar(kCellSize), 0); | |
| 125 canvas->save(); | |
| 126 canvas->concat(fPerspMatrix); | |
| 127 canvas->drawRect(r, pathPaint); | |
| 128 canvas->restore(); | |
| 129 | |
| 130 canvas->translate(SkIntToScalar(kCellSize), 0); | |
| 131 canvas->save(); | |
| 132 canvas->concat(fPerspMatrix); | |
| 133 canvas->drawPath(fPath, pathPaint); | |
| 134 canvas->restore(); | |
| 135 | |
| 136 canvas->translate(SkIntToScalar(kCellSize), 0); | |
| 137 canvas->save(); | |
| 138 canvas->concat(fPerspMatrix); | |
| 139 canvas->drawRect(r, gradPaint1); | |
| 140 canvas->restore(); | |
| 141 | |
| 142 canvas->translate(SkIntToScalar(kCellSize), 0); | |
| 143 canvas->save(); | |
| 144 canvas->concat(fPerspMatrix); | |
| 145 canvas->drawPath(fPath, gradPaint2); | |
| 146 canvas->restore(); | |
| 147 | |
| 148 canvas->restore(); | |
| 149 } | |
| 150 | |
| 151 void onDraw(SkCanvas* canvas) override { | |
| 152 if (!fImage) { | |
| 153 fImage.reset(make_image(canvas, kCellSize, kCellSize)); | |
| 154 } | |
| 155 | |
| 156 this->drawRow(canvas, kNone_SkFilterQuality); | |
| 157 canvas->translate(0, SkIntToScalar(kCellSize)); | |
| 158 this->drawRow(canvas, kLow_SkFilterQuality); | |
| 159 canvas->translate(0, SkIntToScalar(kCellSize)); | |
| 160 this->drawRow(canvas, kMedium_SkFilterQuality); | |
| 161 canvas->translate(0, SkIntToScalar(kCellSize)); | |
| 162 this->drawRow(canvas, kHigh_SkFilterQuality); | |
| 163 canvas->translate(0, SkIntToScalar(kCellSize)); | |
| 164 } | |
| 165 private: | |
| 166 bool fDoAA; | |
| 167 SkPath fPath; | |
| 168 SkAutoTUnref<SkShader> fBitmapShader; | |
| 169 SkAutoTUnref<SkShader> fLinearGrad1; | |
| 170 SkAutoTUnref<SkShader> fLinearGrad2; | |
| 171 SkMatrix fPerspMatrix; | |
| 172 SkAutoTUnref<SkImage> fImage; | |
| 173 SkBitmap fBitmap; | |
| 174 | |
| 175 typedef GM INHERITED; | |
| 176 }; | |
| 177 | |
| 178 ////////////////////////////////////////////////////////////////////////////// | |
| 179 | |
| 180 DEF_GM(return new PerspShadersGM(true);) | |
| 181 DEF_GM(return new PerspShadersGM(false);) | |
| 182 } | |
| OLD | NEW |