1 /* 2 * Copyright 2016 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/SkBitmap.h" 10 #include "include/core/SkCanvas.h" 11 #include "include/core/SkColor.h" 12 #include "include/core/SkColorPriv.h" 13 #include "include/core/SkImageInfo.h" 14 #include "include/core/SkPaint.h" 15 #include "include/core/SkPixmap.h" 16 #include "include/core/SkRect.h" 17 #include "include/core/SkRefCnt.h" 18 #include "include/core/SkScalar.h" 19 #include "include/core/SkSize.h" 20 #include "include/core/SkString.h" 21 #include "include/private/SkNx.h" 22 #include "src/core/SkMipmap.h" 23 #include "src/core/SkMipmapBuilder.h" 24 #include "tools/Resources.h" 25 #include "tools/ToolUtils.h" 26 27 #include <math.h> 28 29 class ShowMipLevels3 : public skiagm::GM { 30 sk_sp<SkImage> fImg; 31 onShortName()32 SkString onShortName() override { return SkString("showmiplevels_explicit"); } 33 onISize()34 SkISize onISize() override { return {1130, 970}; } 35 onOnceBeforeDraw()36 void onOnceBeforeDraw() override { 37 fImg = GetResourceAsImage("images/ship.png"); 38 fImg = fImg->makeRasterImage(); // makeWithMips only works on raster for now 39 40 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; 41 42 SkMipmapBuilder builder(fImg->imageInfo()); 43 for (int i = 0; i < builder.countLevels(); ++i) { 44 auto surf = SkSurface::MakeRasterDirect(builder.level(i)); 45 surf->getCanvas()->drawColor(colors[i % SK_ARRAY_COUNT(colors)]); 46 } 47 fImg = builder.attachTo(fImg.get()); 48 } 49 onDraw(SkCanvas * canvas,SkString *)50 DrawResult onDraw(SkCanvas* canvas, SkString*) override { 51 if (canvas->recordingContext()) { 52 // mips not supported yet 53 return DrawResult::kSkip; 54 } 55 56 canvas->drawColor(0xFFDDDDDD); 57 58 canvas->translate(10, 10); 59 for (auto mm : {SkMipmapMode::kNone, SkMipmapMode::kNearest, SkMipmapMode::kLinear}) { 60 for (auto fm : {SkFilterMode::kNearest, SkFilterMode::kLinear}) { 61 canvas->translate(0, draw_downscaling(canvas, {fm, mm})); 62 } 63 } 64 return DrawResult::kOk; 65 } 66 67 private: draw_downscaling(SkCanvas * canvas,SkSamplingOptions sampling)68 SkScalar draw_downscaling(SkCanvas* canvas, SkSamplingOptions sampling) { 69 SkAutoCanvasRestore acr(canvas, true); 70 71 SkPaint paint; 72 SkRect r = {0, 0, 150, 150}; 73 for (float scale = 1; scale >= 0.1f; scale *= 0.7f) { 74 SkMatrix matrix = SkMatrix::Scale(scale, scale); 75 paint.setShader(fImg->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, 76 sampling, &matrix)); 77 canvas->drawRect(r, paint); 78 canvas->translate(r.width() + 10, 0); 79 } 80 return r.height() + 10; 81 } 82 83 using INHERITED = skiagm::GM; 84 }; 85 DEF_GM( return new ShowMipLevels3; ) 86