2013-07-18 19:53:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 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 "include/core/SkBitmap.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
2020-07-14 21:16:32 +00:00
|
|
|
#include "src/core/SkMipmap.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
2020-07-14 15:29:38 +00:00
|
|
|
#include "tools/Resources.h"
|
2013-07-18 19:53:31 +00:00
|
|
|
|
2016-02-17 22:53:23 +00:00
|
|
|
static void make_bitmap(SkBitmap* bm, int width, int height) {
|
|
|
|
bm->allocN32Pixels(width, height);
|
2013-07-18 19:53:31 +00:00
|
|
|
bm->eraseColor(SK_ColorWHITE);
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(MipMap, reporter) {
|
2013-07-18 19:53:31 +00:00
|
|
|
SkBitmap bm;
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-07-18 19:53:31 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 500; ++i) {
|
2016-03-25 19:55:52 +00:00
|
|
|
int width = 1 + rand.nextU() % 1000;
|
|
|
|
int height = 1 + rand.nextU() % 1000;
|
2016-02-17 22:53:23 +00:00
|
|
|
make_bitmap(&bm, width, height);
|
2020-07-14 21:16:32 +00:00
|
|
|
sk_sp<SkMipmap> mm(SkMipmap::Build(bm, nullptr));
|
2013-07-19 07:00:57 +00:00
|
|
|
|
2020-07-14 21:16:32 +00:00
|
|
|
REPORTER_ASSERT(reporter, mm->countLevels() == SkMipmap::ComputeLevelCount(width, height));
|
2016-02-09 16:20:18 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1, SK_Scalar1),
|
|
|
|
nullptr));
|
|
|
|
REPORTER_ASSERT(reporter, !mm->extractLevel(SkSize::Make(SK_Scalar1 * 2, SK_Scalar1 * 2),
|
|
|
|
nullptr));
|
2013-07-19 07:00:57 +00:00
|
|
|
|
2020-07-14 21:16:32 +00:00
|
|
|
SkMipmap::Level prevLevel;
|
2013-07-24 14:55:00 +00:00
|
|
|
sk_bzero(&prevLevel, sizeof(prevLevel));
|
2013-07-18 19:53:31 +00:00
|
|
|
|
|
|
|
SkScalar scale = SK_Scalar1;
|
|
|
|
for (int j = 0; j < 30; ++j) {
|
|
|
|
scale = scale * 2 / 3;
|
|
|
|
|
2020-07-14 21:16:32 +00:00
|
|
|
SkMipmap::Level level;
|
2016-02-09 16:20:18 +00:00
|
|
|
if (mm->extractLevel(SkSize::Make(scale, scale), &level)) {
|
2016-01-17 02:50:35 +00:00
|
|
|
REPORTER_ASSERT(reporter, level.fPixmap.addr());
|
|
|
|
REPORTER_ASSERT(reporter, level.fPixmap.width() > 0);
|
|
|
|
REPORTER_ASSERT(reporter, level.fPixmap.height() > 0);
|
|
|
|
REPORTER_ASSERT(reporter, (int)level.fPixmap.rowBytes() >= level.fPixmap.width() * 4);
|
|
|
|
|
|
|
|
if (prevLevel.fPixmap.addr()) {
|
|
|
|
REPORTER_ASSERT(reporter, level.fPixmap.width() <= prevLevel.fPixmap.width());
|
|
|
|
REPORTER_ASSERT(reporter, level.fPixmap.height() <= prevLevel.fPixmap.height());
|
2013-07-18 19:53:31 +00:00
|
|
|
}
|
|
|
|
prevLevel = level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-17 22:53:23 +00:00
|
|
|
|
|
|
|
static void test_mipmap_generation(int width, int height, int expectedMipLevelCount,
|
|
|
|
skiatest::Reporter* reporter) {
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocN32Pixels(width, height);
|
|
|
|
bm.eraseColor(SK_ColorWHITE);
|
2020-07-14 21:16:32 +00:00
|
|
|
sk_sp<SkMipmap> mm(SkMipmap::Build(bm, nullptr));
|
2016-02-17 22:53:23 +00:00
|
|
|
|
|
|
|
const int mipLevelCount = mm->countLevels();
|
|
|
|
REPORTER_ASSERT(reporter, mipLevelCount == expectedMipLevelCount);
|
2020-07-14 21:16:32 +00:00
|
|
|
REPORTER_ASSERT(reporter, mipLevelCount == SkMipmap::ComputeLevelCount(width, height));
|
2016-02-17 22:53:23 +00:00
|
|
|
for (int i = 0; i < mipLevelCount; ++i) {
|
2020-07-14 21:16:32 +00:00
|
|
|
SkMipmap::Level level;
|
2016-02-17 22:53:23 +00:00
|
|
|
REPORTER_ASSERT(reporter, mm->getLevel(i, &level));
|
|
|
|
// Make sure the mipmaps contain valid data and that the sizes are correct
|
|
|
|
REPORTER_ASSERT(reporter, level.fPixmap.addr());
|
2020-07-14 21:16:32 +00:00
|
|
|
SkISize size = SkMipmap::ComputeLevelSize(width, height, i);
|
2016-06-09 16:44:33 +00:00
|
|
|
REPORTER_ASSERT(reporter, level.fPixmap.width() == size.width());
|
|
|
|
REPORTER_ASSERT(reporter, level.fPixmap.height() == size.height());
|
2016-02-17 22:53:23 +00:00
|
|
|
|
2020-07-14 21:16:32 +00:00
|
|
|
// + 1 because SkMipmap does not include the base mipmap level.
|
2016-02-17 22:53:23 +00:00
|
|
|
int twoToTheMipLevel = 1 << (i + 1);
|
|
|
|
int currentWidth = width / twoToTheMipLevel;
|
|
|
|
int currentHeight = height / twoToTheMipLevel;
|
|
|
|
REPORTER_ASSERT(reporter, level.fPixmap.width() == currentWidth);
|
|
|
|
REPORTER_ASSERT(reporter, level.fPixmap.height() == currentHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(MipMap_DirectLevelAccess, reporter) {
|
|
|
|
// create mipmap with invalid size
|
|
|
|
{
|
2020-07-14 21:16:32 +00:00
|
|
|
// SkMipmap current requires the dimensions be greater than 2x2
|
2016-02-17 22:53:23 +00:00
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocN32Pixels(1, 1);
|
|
|
|
bm.eraseColor(SK_ColorWHITE);
|
2020-07-14 21:16:32 +00:00
|
|
|
sk_sp<SkMipmap> mm(SkMipmap::Build(bm, nullptr));
|
2016-02-17 22:53:23 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, mm == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check small mipmap's count and levels
|
|
|
|
// There should be 5 mipmap levels generated:
|
|
|
|
// 16x16, 8x8, 4x4, 2x2, 1x1
|
|
|
|
test_mipmap_generation(32, 32, 5, reporter);
|
|
|
|
|
|
|
|
// check large mipmap's count and levels
|
|
|
|
// There should be 9 mipmap levels generated:
|
|
|
|
// 500x500, 250x250, 125x125, 62x62, 31x31, 15x15, 7x7, 3x3, 1x1
|
|
|
|
test_mipmap_generation(1000, 1000, 9, reporter);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct LevelCountScenario {
|
|
|
|
int fWidth;
|
|
|
|
int fHeight;
|
|
|
|
int fExpectedLevelCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_TEST(MipMap_ComputeLevelCount, reporter) {
|
|
|
|
const LevelCountScenario tests[] = {
|
|
|
|
// Test mipmaps with negative sizes
|
|
|
|
{-100, 100, 0},
|
|
|
|
{100, -100, 0},
|
|
|
|
{-100, -100, 0},
|
|
|
|
|
|
|
|
// Test mipmaps with 0, 1, 2 as dimensions
|
2020-07-14 21:16:32 +00:00
|
|
|
// (SkMipmap::Build requires a min size of 1)
|
2016-02-17 22:53:23 +00:00
|
|
|
//
|
|
|
|
// 0
|
|
|
|
{0, 100, 0},
|
|
|
|
{100, 0, 0},
|
|
|
|
{0, 0, 0},
|
|
|
|
// 1
|
2016-03-01 21:54:30 +00:00
|
|
|
{1, 100, 6},
|
|
|
|
{100, 1, 6},
|
2016-02-17 22:53:23 +00:00
|
|
|
{1, 1, 0},
|
|
|
|
// 2
|
2016-03-01 21:54:30 +00:00
|
|
|
{2, 100, 6},
|
|
|
|
{100, 2, 6},
|
2016-02-17 22:53:23 +00:00
|
|
|
{2, 2, 1},
|
|
|
|
|
|
|
|
// Test a handful of boundaries such as 63x63 and 64x64
|
|
|
|
{63, 63, 5},
|
|
|
|
{64, 64, 6},
|
|
|
|
{127, 127, 6},
|
|
|
|
{128, 128, 7},
|
|
|
|
{255, 255, 7},
|
|
|
|
{256, 256, 8},
|
|
|
|
|
|
|
|
// Test different dimensions, such as 256x64
|
2016-03-01 21:54:30 +00:00
|
|
|
{64, 129, 7},
|
|
|
|
{255, 32, 7},
|
|
|
|
{500, 1000, 9}
|
2016-02-17 22:53:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (auto& currentTest : tests) {
|
2020-07-14 21:16:32 +00:00
|
|
|
int levelCount = SkMipmap::ComputeLevelCount(currentTest.fWidth, currentTest.fHeight);
|
2016-02-17 22:53:23 +00:00
|
|
|
REPORTER_ASSERT(reporter, currentTest.fExpectedLevelCount == levelCount);
|
|
|
|
}
|
|
|
|
}
|
2016-06-02 16:01:48 +00:00
|
|
|
|
|
|
|
struct LevelSizeScenario {
|
|
|
|
int fBaseWidth;
|
|
|
|
int fBaseHeight;
|
|
|
|
int fLevel;
|
2016-06-03 18:59:50 +00:00
|
|
|
SkISize fExpectedMipMapLevelSize;
|
2016-06-02 16:01:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_TEST(MipMap_ComputeLevelSize, reporter) {
|
|
|
|
const LevelSizeScenario tests[] = {
|
|
|
|
// Test mipmaps with negative sizes
|
2016-06-09 16:44:33 +00:00
|
|
|
{-100, 100, 0, SkISize::Make(0, 0)},
|
|
|
|
{100, -100, 0, SkISize::Make(0, 0)},
|
|
|
|
{-100, -100, 0, SkISize::Make(0, 0)},
|
2016-06-02 16:01:48 +00:00
|
|
|
|
|
|
|
// Test mipmaps with 0, 1, 2 as dimensions
|
2020-07-14 21:16:32 +00:00
|
|
|
// (SkMipmap::Build requires a min size of 1)
|
2016-06-02 16:01:48 +00:00
|
|
|
//
|
|
|
|
// 0
|
2016-06-09 16:44:33 +00:00
|
|
|
{0, 100, 0, SkISize::Make(0, 0)},
|
|
|
|
{100, 0, 0, SkISize::Make(0, 0)},
|
|
|
|
{0, 0, 0, SkISize::Make(0, 0)},
|
2016-06-02 16:01:48 +00:00
|
|
|
// 1
|
|
|
|
|
2016-06-09 16:44:33 +00:00
|
|
|
{1, 100, 0, SkISize::Make(1, 50)},
|
|
|
|
{100, 1, 0, SkISize::Make(50, 1)},
|
|
|
|
{1, 1, 0, SkISize::Make(0, 0)},
|
2016-06-02 16:01:48 +00:00
|
|
|
// 2
|
2016-06-09 16:44:33 +00:00
|
|
|
{2, 100, 0, SkISize::Make(1, 50)},
|
|
|
|
{100, 2, 1, SkISize::Make(25, 1)},
|
|
|
|
{2, 2, 0, SkISize::Make(1, 1)},
|
2016-06-02 16:01:48 +00:00
|
|
|
|
|
|
|
// Test a handful of cases
|
2016-06-09 16:44:33 +00:00
|
|
|
{63, 63, 2, SkISize::Make(7, 7)},
|
|
|
|
{64, 64, 2, SkISize::Make(8, 8)},
|
|
|
|
{127, 127, 2, SkISize::Make(15, 15)},
|
|
|
|
{64, 129, 3, SkISize::Make(4, 8)},
|
|
|
|
{255, 32, 6, SkISize::Make(1, 1)},
|
|
|
|
{500, 1000, 1, SkISize::Make(125, 250)},
|
2016-06-02 16:01:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (auto& currentTest : tests) {
|
2020-07-14 21:16:32 +00:00
|
|
|
SkISize levelSize = SkMipmap::ComputeLevelSize(currentTest.fBaseWidth,
|
2016-06-03 18:59:50 +00:00
|
|
|
currentTest.fBaseHeight,
|
|
|
|
currentTest.fLevel);
|
2016-06-02 16:01:48 +00:00
|
|
|
REPORTER_ASSERT(reporter, currentTest.fExpectedMipMapLevelSize == levelSize);
|
|
|
|
}
|
|
|
|
}
|
2018-12-04 14:06:00 +00:00
|
|
|
|
|
|
|
DEF_TEST(MipMap_F16, reporter) {
|
|
|
|
SkBitmap bmp;
|
|
|
|
bmp.allocPixels(SkImageInfo::Make(10, 10, kRGBA_F16_SkColorType, kPremul_SkAlphaType));
|
|
|
|
bmp.eraseColor(0);
|
2020-07-14 21:16:32 +00:00
|
|
|
sk_sp<SkMipmap> mipmap(SkMipmap::Build(bmp, nullptr));
|
2018-12-04 14:06:00 +00:00
|
|
|
}
|
2020-07-14 15:29:38 +00:00
|
|
|
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
2020-11-30 18:09:24 +00:00
|
|
|
#include "src/core/SkMipmapBuilder.h"
|
2020-07-14 15:29:38 +00:00
|
|
|
|
2020-07-25 16:47:14 +00:00
|
|
|
static void fill_in_mips(SkMipmapBuilder* builder, sk_sp<SkImage> img) {
|
|
|
|
int count = builder->countLevels();
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
SkPixmap pm = builder->level(i);
|
|
|
|
auto surf = SkSurface::MakeRasterDirect(pm);
|
|
|
|
surf->getCanvas()->drawImageRect(img, SkRect::MakeIWH(pm.width(), pm.height()), nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 15:29:38 +00:00
|
|
|
DEF_TEST(image_mip_factory, reporter) {
|
|
|
|
// TODO: what do to about lazy images and mipmaps?
|
|
|
|
auto img = GetResourceAsImage("images/mandrill_128.png")->makeRasterImage();
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, !img->hasMipmaps());
|
2020-09-12 14:38:32 +00:00
|
|
|
auto img1 = img->withDefaultMipmaps();
|
2020-07-14 15:29:38 +00:00
|
|
|
REPORTER_ASSERT(reporter, img.get() != img1.get());
|
|
|
|
REPORTER_ASSERT(reporter, img1->hasMipmaps());
|
|
|
|
|
|
|
|
SkMipmapBuilder builder(img->imageInfo());
|
2020-07-25 16:47:14 +00:00
|
|
|
fill_in_mips(&builder, img);
|
|
|
|
|
2020-09-17 17:58:26 +00:00
|
|
|
auto img2 = builder.attachTo(img);
|
2020-07-14 15:29:38 +00:00
|
|
|
REPORTER_ASSERT(reporter, img.get() != img2.get());
|
|
|
|
REPORTER_ASSERT(reporter, img1.get() != img2.get());
|
|
|
|
REPORTER_ASSERT(reporter, img2->hasMipmaps());
|
|
|
|
}
|
2020-07-25 16:47:14 +00:00
|
|
|
|
|
|
|
// Ensure we can't attach mips that don't "match" the image
|
|
|
|
//
|
|
|
|
DEF_TEST(image_mip_mismatch, reporter) {
|
|
|
|
auto check_fails = [reporter](sk_sp<SkImage> img, const SkImageInfo& info) {
|
|
|
|
SkMipmapBuilder builder(info);
|
|
|
|
fill_in_mips(&builder, img);
|
2020-09-17 17:58:26 +00:00
|
|
|
auto img2 = builder.attachTo(img);
|
2020-07-25 16:47:14 +00:00
|
|
|
// if withMipmaps() succeeds, it returns a new image, otherwise it returns the original
|
|
|
|
REPORTER_ASSERT(reporter, img.get() == img2.get());
|
|
|
|
};
|
|
|
|
|
|
|
|
auto img = GetResourceAsImage("images/mandrill_128.png")->makeRasterImage();
|
|
|
|
|
|
|
|
// check size, colortype, and alphatype
|
|
|
|
|
|
|
|
check_fails(img, img->imageInfo().makeWH(img->width() + 2, img->height() - 3));
|
|
|
|
|
|
|
|
SkASSERT(img->imageInfo().colorType() != kRGB_565_SkColorType);
|
|
|
|
check_fails(img, img->imageInfo().makeColorType(kRGB_565_SkColorType));
|
|
|
|
|
|
|
|
SkASSERT(img->imageInfo().alphaType() != kUnpremul_SkAlphaType);
|
|
|
|
check_fails(img, img->imageInfo().makeAlphaType(kUnpremul_SkAlphaType));
|
|
|
|
}
|