2016-01-14 17:11:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColorPriv.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageInfo.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPaint.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPixmap.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/private/SkNx.h"
|
2020-07-14 21:16:32 +00:00
|
|
|
#include "src/core/SkMipmap.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
2016-01-14 17:11:51 +00:00
|
|
|
|
2016-01-15 21:17:08 +00:00
|
|
|
#define SHOW_MIP_COLOR 0xFF000000
|
|
|
|
|
2016-01-15 18:51:08 +00:00
|
|
|
static SkBitmap make_bitmap(int w, int h) {
|
2016-01-14 17:11:51 +00:00
|
|
|
SkBitmap bm;
|
2016-01-15 18:51:08 +00:00
|
|
|
bm.allocN32Pixels(w, h);
|
2016-01-14 17:11:51 +00:00
|
|
|
SkCanvas canvas(bm);
|
|
|
|
canvas.clear(0xFFFFFFFF);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
2016-01-15 18:51:08 +00:00
|
|
|
paint.setStrokeWidth(w / 16.0f);
|
2016-01-15 21:17:08 +00:00
|
|
|
paint.setColor(SHOW_MIP_COLOR);
|
2016-01-15 18:51:08 +00:00
|
|
|
canvas.drawCircle(w/2.0f, h/2.0f, w/3.0f, paint);
|
2016-01-14 17:11:51 +00:00
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
2016-01-15 18:51:08 +00:00
|
|
|
static SkBitmap make_bitmap2(int w, int h) {
|
2016-01-14 17:11:51 +00:00
|
|
|
SkBitmap bm;
|
2016-01-15 18:51:08 +00:00
|
|
|
bm.allocN32Pixels(w, h);
|
2016-01-14 17:11:51 +00:00
|
|
|
SkCanvas canvas(bm);
|
|
|
|
canvas.clear(0xFFFFFFFF);
|
|
|
|
SkPaint paint;
|
2016-01-15 21:17:08 +00:00
|
|
|
paint.setColor(SHOW_MIP_COLOR);
|
2016-01-14 17:11:51 +00:00
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
SkScalar inset = 2;
|
2016-01-15 18:51:08 +00:00
|
|
|
SkRect r = SkRect::MakeIWH(w, h).makeInset(0.5f, 0.5f);
|
2016-01-14 17:11:51 +00:00
|
|
|
while (r.width() > 4) {
|
|
|
|
canvas.drawRect(r, paint);
|
|
|
|
r.inset(inset, inset);
|
|
|
|
inset += 1;
|
|
|
|
}
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
2016-01-15 18:51:08 +00:00
|
|
|
static SkBitmap make_bitmap3(int w, int h) {
|
2016-01-14 17:11:51 +00:00
|
|
|
SkBitmap bm;
|
2016-01-15 18:51:08 +00:00
|
|
|
bm.allocN32Pixels(w, h);
|
2016-01-14 17:11:51 +00:00
|
|
|
SkCanvas canvas(bm);
|
|
|
|
canvas.clear(0xFFFFFFFF);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(2.1f);
|
2016-01-15 21:17:08 +00:00
|
|
|
paint.setColor(SHOW_MIP_COLOR);
|
2016-01-14 17:11:51 +00:00
|
|
|
|
2016-01-15 18:51:08 +00:00
|
|
|
SkScalar s = SkIntToScalar(w);
|
2016-01-14 17:11:51 +00:00
|
|
|
Sk4f p(s, -s, -s, s);
|
|
|
|
Sk4f d(5);
|
2016-02-21 18:54:19 +00:00
|
|
|
while (p[1] < s) {
|
|
|
|
canvas.drawLine(p[0],p[1], p[2], p[3], paint);
|
2016-01-14 17:11:51 +00:00
|
|
|
p = p + d;
|
|
|
|
}
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ShowMipLevels : public skiagm::GM {
|
|
|
|
const int fN;
|
|
|
|
SkBitmap fBM[4];
|
|
|
|
|
|
|
|
public:
|
|
|
|
static unsigned gamma(unsigned n) {
|
|
|
|
float x = n / 255.0f;
|
|
|
|
#if 0
|
|
|
|
x = sqrtf(x);
|
|
|
|
#else
|
|
|
|
if (x > 0.0031308f) {
|
|
|
|
x = 1.055f * (powf(x, (1.0f / 2.4f))) - 0.055f;
|
|
|
|
} else {
|
|
|
|
x = 12.92f * x;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return (int)(x * 255);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void apply_gamma(const SkBitmap& bm) {
|
|
|
|
return; // below is our experiment for sRGB correction
|
|
|
|
for (int y = 0; y < bm.height(); ++y) {
|
|
|
|
for (int x = 0; x < bm.width(); ++x) {
|
|
|
|
SkPMColor c = *bm.getAddr32(x, y);
|
|
|
|
unsigned r = gamma(SkGetPackedR32(c));
|
|
|
|
unsigned g = gamma(SkGetPackedG32(c));
|
|
|
|
unsigned b = gamma(SkGetPackedB32(c));
|
|
|
|
*bm.getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 17:04:26 +00:00
|
|
|
ShowMipLevels(int N) : fN(N) { }
|
2016-01-14 17:11:51 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
|
|
|
SkString str;
|
|
|
|
str.printf("showmiplevels_%d", fN);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-07-19 21:20:37 +00:00
|
|
|
SkISize onISize() override { return { 150, 862 }; }
|
2016-01-14 17:11:51 +00:00
|
|
|
|
|
|
|
static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& orig, SkScalar x, SkScalar y) {
|
|
|
|
SkBitmap bm;
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::copy_to(&bm, orig.colorType(), orig);
|
2016-01-14 17:11:51 +00:00
|
|
|
apply_gamma(bm);
|
|
|
|
|
|
|
|
canvas->drawBitmap(bm, x, y, nullptr);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setColor(0xFFFFCCCC);
|
|
|
|
canvas->drawRect(SkRect::MakeIWH(bm.width(), bm.height()).makeOffset(x, y).makeOutset(0.5f, 0.5f), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename F> void drawLevels(SkCanvas* canvas, const SkBitmap& baseBM, F func) {
|
|
|
|
SkScalar x = 4;
|
|
|
|
SkScalar y = 4;
|
|
|
|
|
|
|
|
SkPixmap prevPM;
|
|
|
|
baseBM.peekPixels(&prevPM);
|
|
|
|
|
2020-07-14 21:16:32 +00:00
|
|
|
sk_sp<SkMipmap> mm(SkMipmap::Build(baseBM, nullptr));
|
2016-01-14 17:11:51 +00:00
|
|
|
|
|
|
|
int index = 0;
|
2020-07-14 21:16:32 +00:00
|
|
|
SkMipmap::Level level;
|
2016-01-14 17:11:51 +00:00
|
|
|
SkScalar scale = 0.5f;
|
2016-02-09 16:20:18 +00:00
|
|
|
while (mm->extractLevel(SkSize::Make(scale, scale), &level)) {
|
2016-01-17 02:50:35 +00:00
|
|
|
SkBitmap bm = func(prevPM, level.fPixmap);
|
2016-01-14 17:11:51 +00:00
|
|
|
DrawAndFrame(canvas, bm, x, y);
|
|
|
|
|
2016-01-17 02:50:35 +00:00
|
|
|
if (level.fPixmap.width() <= 2 || level.fPixmap.height() <= 2) {
|
2016-01-14 17:11:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (index & 1) {
|
2016-01-17 02:50:35 +00:00
|
|
|
x += level.fPixmap.width() + 4;
|
2016-01-14 17:11:51 +00:00
|
|
|
} else {
|
2016-01-17 02:50:35 +00:00
|
|
|
y += level.fPixmap.height() + 4;
|
2016-01-14 17:11:51 +00:00
|
|
|
}
|
|
|
|
scale /= 2;
|
2016-01-17 02:50:35 +00:00
|
|
|
prevPM = level.fPixmap;
|
2016-01-14 17:11:51 +00:00
|
|
|
index += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawSet(SkCanvas* canvas, const SkBitmap& orig) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
|
|
|
drawLevels(canvas, orig, [](const SkPixmap& prev, const SkPixmap& curr) {
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.installPixels(curr);
|
|
|
|
return bm;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-26 17:04:26 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2019-03-20 16:12:10 +00:00
|
|
|
fBM[0] = ToolUtils::create_checkerboard_bitmap(fN, fN, SK_ColorBLACK, SK_ColorWHITE, 2);
|
2016-08-26 17:04:26 +00:00
|
|
|
fBM[1] = make_bitmap(fN, fN);
|
|
|
|
fBM[2] = make_bitmap2(fN, fN);
|
|
|
|
fBM[3] = make_bitmap3(fN, fN);
|
|
|
|
}
|
|
|
|
|
2016-01-14 17:11:51 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
canvas->translate(4, 4);
|
|
|
|
for (const auto& bm : fBM) {
|
|
|
|
this->drawSet(canvas, bm);
|
2017-06-07 14:01:48 +00:00
|
|
|
// round so we always produce an integral translate, so the GOLD tool won't show
|
|
|
|
// unimportant diffs if this is drawn on a GPU with different rounding rules
|
|
|
|
// since we draw the bitmaps using nearest-neighbor
|
|
|
|
canvas->translate(0, SkScalarRoundToScalar(bm.height() * 0.85f));
|
2016-01-14 17:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2016-01-14 17:11:51 +00:00
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new ShowMipLevels(255); )
|
|
|
|
DEF_GM( return new ShowMipLevels(256); )
|
|
|
|
|
2016-01-15 18:51:08 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-01-17 15:48:53 +00:00
|
|
|
void copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
|
|
|
|
if (kGray_8_SkColorType == dstColorType) {
|
2019-03-20 16:12:10 +00:00
|
|
|
return ToolUtils::copy_to_g8(dst, src);
|
2017-01-17 15:48:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 15:15:22 +00:00
|
|
|
const SkBitmap* srcPtr = &src;
|
|
|
|
SkBitmap tmp(src);
|
|
|
|
if (kRGB_565_SkColorType == dstColorType) {
|
|
|
|
tmp.setAlphaType(kOpaque_SkAlphaType);
|
|
|
|
srcPtr = &tmp;
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::copy_to(dst, dstColorType, *srcPtr);
|
2017-01-17 15:48:53 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 18:51:08 +00:00
|
|
|
/**
|
|
|
|
* Show mip levels that were built, for all supported colortypes
|
|
|
|
*/
|
|
|
|
class ShowMipLevels2 : public skiagm::GM {
|
|
|
|
const int fW, fH;
|
|
|
|
SkBitmap fBM[4];
|
|
|
|
|
|
|
|
public:
|
2016-08-26 17:04:26 +00:00
|
|
|
ShowMipLevels2(int w, int h) : fW(w), fH(h) { }
|
2016-01-15 18:51:08 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
|
|
|
SkString str;
|
|
|
|
str.printf("showmiplevels2_%dx%d", fW, fH);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return { 824, 862 };
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& bm, SkScalar x, SkScalar y) {
|
|
|
|
canvas->drawBitmap(bm, x, y, nullptr);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setColor(0xFFFFCCCC);
|
|
|
|
canvas->drawRect(SkRect::MakeIWH(bm.width(), bm.height()).makeOffset(x, y).makeOutset(0.5f, 0.5f), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawLevels(SkCanvas* canvas, const SkBitmap& baseBM) {
|
|
|
|
SkScalar x = 4;
|
|
|
|
SkScalar y = 4;
|
|
|
|
|
2020-07-14 21:16:32 +00:00
|
|
|
sk_sp<SkMipmap> mm(SkMipmap::Build(baseBM, nullptr));
|
2016-01-15 18:51:08 +00:00
|
|
|
|
|
|
|
int index = 0;
|
2020-07-14 21:16:32 +00:00
|
|
|
SkMipmap::Level level;
|
2016-01-15 18:51:08 +00:00
|
|
|
SkScalar scale = 0.5f;
|
2016-02-09 16:20:18 +00:00
|
|
|
while (mm->extractLevel(SkSize::Make(scale, scale), &level)) {
|
2016-01-15 18:51:08 +00:00
|
|
|
SkBitmap bm;
|
2016-01-17 02:50:35 +00:00
|
|
|
bm.installPixels(level.fPixmap);
|
2016-01-15 18:51:08 +00:00
|
|
|
DrawAndFrame(canvas, bm, x, y);
|
|
|
|
|
2016-01-17 02:50:35 +00:00
|
|
|
if (level.fPixmap.width() <= 2 || level.fPixmap.height() <= 2) {
|
2016-01-15 18:51:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (index & 1) {
|
2016-01-17 02:50:35 +00:00
|
|
|
x += level.fPixmap.width() + 4;
|
2016-01-15 18:51:08 +00:00
|
|
|
} else {
|
2016-01-17 02:50:35 +00:00
|
|
|
y += level.fPixmap.height() + 4;
|
2016-01-15 18:51:08 +00:00
|
|
|
}
|
|
|
|
scale /= 2;
|
|
|
|
index += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawSet(SkCanvas* canvas, const SkBitmap& orig) {
|
|
|
|
const SkColorType ctypes[] = {
|
|
|
|
kN32_SkColorType, kRGB_565_SkColorType, kARGB_4444_SkColorType, kGray_8_SkColorType
|
|
|
|
};
|
|
|
|
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
|
|
|
for (auto ctype : ctypes) {
|
|
|
|
SkBitmap bm;
|
2017-01-17 15:48:53 +00:00
|
|
|
copy_to(&bm, ctype, orig);
|
2016-01-15 18:51:08 +00:00
|
|
|
drawLevels(canvas, bm);
|
|
|
|
canvas->translate(orig.width()/2 + 8.0f, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 17:04:26 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2019-03-20 16:12:10 +00:00
|
|
|
fBM[0] = ToolUtils::create_checkerboard_bitmap(fW, fH, SHOW_MIP_COLOR, SK_ColorWHITE, 2);
|
2016-08-26 17:04:26 +00:00
|
|
|
fBM[1] = make_bitmap(fW, fH);
|
|
|
|
fBM[2] = make_bitmap2(fW, fH);
|
|
|
|
fBM[3] = make_bitmap3(fW, fH);
|
|
|
|
}
|
|
|
|
|
2016-01-15 18:51:08 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
canvas->translate(4, 4);
|
|
|
|
for (const auto& bm : fBM) {
|
|
|
|
this->drawSet(canvas, bm);
|
|
|
|
// round so we always produce an integral translate, so the GOLD tool won't show
|
|
|
|
// unimportant diffs if this is drawn on a GPU with different rounding rules
|
|
|
|
// since we draw the bitmaps using nearest-neighbor
|
|
|
|
canvas->translate(0, SkScalarRoundToScalar(bm.height() * 0.85f));
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2016-01-15 18:51:08 +00:00
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new ShowMipLevels2(255, 255); )
|
|
|
|
DEF_GM( return new ShowMipLevels2(256, 255); )
|
|
|
|
DEF_GM( return new ShowMipLevels2(255, 256); )
|
|
|
|
DEF_GM( return new ShowMipLevels2(256, 256); )
|
2020-07-10 12:36:42 +00:00
|
|
|
|
|
|
|
#include "tools/Resources.h"
|
|
|
|
|
|
|
|
class ShowMipLevels3 : public skiagm::GM {
|
|
|
|
sk_sp<SkImage> fImg;
|
|
|
|
|
|
|
|
SkString onShortName() override { return SkString("showmiplevels_explicit"); }
|
|
|
|
|
2020-07-14 15:29:38 +00:00
|
|
|
SkISize onISize() override { return {1130, 970}; }
|
2020-07-10 12:36:42 +00:00
|
|
|
|
2020-07-14 15:29:38 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
fImg = GetResourceAsImage("images/ship.png");
|
|
|
|
fImg = fImg->makeRasterImage(); // makeWithMips only works on raster for now
|
2020-07-10 12:36:42 +00:00
|
|
|
|
2020-07-14 15:29:38 +00:00
|
|
|
const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
|
|
|
|
|
|
|
|
SkMipmapBuilder builder(fImg->imageInfo());
|
|
|
|
for (int i = 0; i < builder.countLevels(); ++i) {
|
|
|
|
auto surf = SkSurface::MakeRasterDirect(builder.level(i));
|
|
|
|
surf->getCanvas()->drawColor(colors[i % SK_ARRAY_COUNT(colors)]);
|
2020-07-10 12:36:42 +00:00
|
|
|
}
|
2020-07-14 15:29:38 +00:00
|
|
|
fImg = fImg->withMipmaps(builder.detach());
|
2020-07-10 12:36:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 20:29:16 +00:00
|
|
|
DrawResult onDraw(SkCanvas* canvas, SkString*) override {
|
|
|
|
if (canvas->getGrContext()) {
|
|
|
|
// mips not supported yet
|
2020-07-14 15:29:38 +00:00
|
|
|
return DrawResult::kSkip;
|
|
|
|
}
|
2020-07-10 12:36:42 +00:00
|
|
|
|
|
|
|
canvas->drawColor(0xFFDDDDDD);
|
|
|
|
|
|
|
|
canvas->translate(10, 10);
|
2020-07-16 20:29:16 +00:00
|
|
|
for (auto mm : {SkMipmapMode::kNone, SkMipmapMode::kNearest, SkMipmapMode::kLinear}) {
|
|
|
|
for (auto sa : {SkSamplingMode::kNearest, SkSamplingMode::kLinear}) {
|
2020-07-10 12:36:42 +00:00
|
|
|
canvas->translate(0, draw_downscaling(canvas, {sa, mm}));
|
|
|
|
}
|
|
|
|
}
|
2020-07-14 15:29:38 +00:00
|
|
|
return DrawResult::kOk;
|
2020-07-10 12:36:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-07-14 15:29:38 +00:00
|
|
|
SkScalar draw_downscaling(SkCanvas* canvas, SkFilterOptions options) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
SkRect r = {0, 0, 150, 150};
|
|
|
|
for (float scale = 1; scale >= 0.1f; scale *= 0.7f) {
|
|
|
|
SkMatrix matrix = SkMatrix::Scale(scale, scale);
|
|
|
|
paint.setShader(fImg->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
|
|
|
|
options, &matrix));
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
canvas->translate(r.width() + 10, 0);
|
|
|
|
}
|
|
|
|
return r.height() + 10;
|
|
|
|
}
|
|
|
|
|
2020-07-10 12:36:42 +00:00
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
DEF_GM( return new ShowMipLevels3; )
|