blob: 0182cfe817fb4e60381feff84f84d1cf753f823c [file] [log] [blame]
reeda40be092016-01-14 09:11:51 -08001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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"
Mike Reed13711eb2020-07-14 17:16:32 -040022#include "src/core/SkMipmap.h"
Mike Reed6affa2c2020-11-30 13:09:24 -050023#include "src/core/SkMipmapBuilder.h"
Mike Reed607a3822021-01-24 19:49:21 -050024#include "tools/Resources.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "tools/ToolUtils.h"
reeda40be092016-01-14 09:11:51 -080026
Ben Wagner7fde8e12019-05-01 17:28:53 -040027#include <math.h>
reeda40be092016-01-14 09:11:51 -080028
Mike Reedf8a6b5b2020-07-10 08:36:42 -040029class ShowMipLevels3 : public skiagm::GM {
30 sk_sp<SkImage> fImg;
31
32 SkString onShortName() override { return SkString("showmiplevels_explicit"); }
33
Mike Reed2fe15692020-07-14 11:29:38 -040034 SkISize onISize() override { return {1130, 970}; }
Mike Reedf8a6b5b2020-07-10 08:36:42 -040035
36 void onOnceBeforeDraw() override {
37 fImg = GetResourceAsImage("images/ship.png");
Mike Reed2fe15692020-07-14 11:29:38 -040038 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 }
Mike Reedb4aa6392020-09-17 13:58:26 -040047 fImg = builder.attachTo(fImg.get());
Mike Reedf8a6b5b2020-07-10 08:36:42 -040048 }
49
Mike Reedfd6f7092020-07-16 16:29:16 -040050 DrawResult onDraw(SkCanvas* canvas, SkString*) override {
Adlai Holler302e8fb2020-09-14 11:58:06 -040051 if (canvas->recordingContext()) {
Mike Reedfd6f7092020-07-16 16:29:16 -040052 // mips not supported yet
Mike Reed2fe15692020-07-14 11:29:38 -040053 return DrawResult::kSkip;
54 }
55
Mike Reedf8a6b5b2020-07-10 08:36:42 -040056 canvas->drawColor(0xFFDDDDDD);
57
Mike Reedf8a6b5b2020-07-10 08:36:42 -040058 canvas->translate(10, 10);
Mike Reedfd6f7092020-07-16 16:29:16 -040059 for (auto mm : {SkMipmapMode::kNone, SkMipmapMode::kNearest, SkMipmapMode::kLinear}) {
Mike Reeda03f8bf2020-11-20 18:45:36 -050060 for (auto fm : {SkFilterMode::kNearest, SkFilterMode::kLinear}) {
61 canvas->translate(0, draw_downscaling(canvas, {fm, mm}));
Mike Reedf8a6b5b2020-07-10 08:36:42 -040062 }
Mike Reedf8a6b5b2020-07-10 08:36:42 -040063 }
Mike Reed2fe15692020-07-14 11:29:38 -040064 return DrawResult::kOk;
Mike Reedf8a6b5b2020-07-10 08:36:42 -040065 }
66
67private:
Mike Reeda03f8bf2020-11-20 18:45:36 -050068 SkScalar draw_downscaling(SkCanvas* canvas, SkSamplingOptions sampling) {
Mike Reed2fe15692020-07-14 11:29:38 -040069 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,
Mike Reeda03f8bf2020-11-20 18:45:36 -050076 sampling, &matrix));
Mike Reed2fe15692020-07-14 11:29:38 -040077 canvas->drawRect(r, paint);
78 canvas->translate(r.width() + 10, 0);
79 }
80 return r.height() + 10;
81 }
82
John Stiles7571f9e2020-09-02 22:42:33 -040083 using INHERITED = skiagm::GM;
Mike Reedf8a6b5b2020-07-10 08:36:42 -040084};
85DEF_GM( return new ShowMipLevels3; )