2015-02-19 19:39:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 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"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColorSpace.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2015-02-19 19:39:46 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> make_image() {
|
2015-02-19 19:39:46 +00:00
|
|
|
const SkImageInfo info = SkImageInfo::MakeN32Premul(319, 52);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRaster(info));
|
2015-02-19 19:39:46 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
2018-08-16 14:17:03 +00:00
|
|
|
canvas->drawColor(0xFFF8F8F8);
|
2015-02-19 19:39:46 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
for (int i = 0; i < 20; ++i) {
|
|
|
|
canvas->drawCircle(-4, 25, 20, paint);
|
|
|
|
canvas->translate(25, 0);
|
|
|
|
}
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-02-19 19:39:46 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 15:16:41 +00:00
|
|
|
DEF_SIMPLE_GM(mipmap, canvas, 400, 200) {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> img(make_image());//SkImage::NewFromEncoded(data));
|
2015-02-19 19:39:46 +00:00
|
|
|
|
|
|
|
const SkRect dst = SkRect::MakeWH(177, 15);
|
|
|
|
|
|
|
|
SkString str;
|
|
|
|
str.printf("scale %g %g", dst.width() / img->width(), dst.height() / img->height());
|
2019-01-08 14:38:02 +00:00
|
|
|
// canvas->drawString(str, 300, 100, SkFont(nullptr, 30), paint);
|
2015-02-19 19:39:46 +00:00
|
|
|
|
2021-01-23 17:23:23 +00:00
|
|
|
const SkSamplingOptions samplings[] = {
|
|
|
|
SkSamplingOptions(SkFilterMode::kNearest),
|
|
|
|
SkSamplingOptions(SkFilterMode::kLinear),
|
|
|
|
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
|
2021-02-05 17:55:38 +00:00
|
|
|
SkSamplingOptions(SkCubicResampler::Mitchell()),
|
2021-01-23 17:23:23 +00:00
|
|
|
};
|
|
|
|
|
2015-02-19 19:39:46 +00:00
|
|
|
canvas->translate(20, 20);
|
2021-01-23 17:23:23 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(samplings); ++i) {
|
|
|
|
canvas->drawImageRect(img.get(), dst, samplings[i], nullptr);
|
2015-02-19 19:39:46 +00:00
|
|
|
canvas->translate(0, 20);
|
|
|
|
}
|
2021-01-23 17:23:23 +00:00
|
|
|
canvas->drawImage(img.get(), 20, 20);
|
2015-02-19 19:39:46 +00:00
|
|
|
}
|
2016-06-10 18:41:47 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// create a circle image computed raw, so we can wrap it as a linear or srgb image
|
|
|
|
static sk_sp<SkImage> make(sk_sp<SkColorSpace> cs) {
|
|
|
|
const int N = 100;
|
|
|
|
SkImageInfo info = SkImageInfo::Make(N, N, kN32_SkColorType, kPremul_SkAlphaType, cs);
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocPixels(info);
|
|
|
|
|
|
|
|
for (int y = 0; y < N; ++y) {
|
|
|
|
for (int x = 0; x < N; ++x) {
|
|
|
|
*bm.getAddr32(x, y) = (x ^ y) & 1 ? 0xFFFFFFFF : 0xFF000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bm.setImmutable();
|
2020-12-23 15:11:33 +00:00
|
|
|
return bm.asImage();
|
2016-06-10 18:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void show_mips(SkCanvas* canvas, SkImage* img) {
|
2021-01-23 17:23:23 +00:00
|
|
|
SkSamplingOptions sampling(SkFilterMode::kLinear,
|
|
|
|
SkMipmapMode::kLinear);
|
2016-06-10 18:41:47 +00:00
|
|
|
|
2016-06-11 11:48:12 +00:00
|
|
|
// Want to ensure we never draw fractional pixels, so we use an IRect
|
|
|
|
SkIRect dst = SkIRect::MakeWH(img->width(), img->height());
|
2016-06-10 18:41:47 +00:00
|
|
|
while (dst.width() > 5) {
|
2021-01-23 17:23:23 +00:00
|
|
|
canvas->drawImageRect(img, SkRect::Make(dst), sampling, nullptr);
|
2016-06-10 18:41:47 +00:00
|
|
|
dst.offset(dst.width() + 10, 0);
|
2016-06-11 11:48:12 +00:00
|
|
|
dst.fRight = dst.fLeft + dst.width()/2;
|
|
|
|
dst.fBottom = dst.fTop + dst.height()/2;
|
2016-06-10 18:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure that in L32 drawing mode, both images/mips look the same as each other, and
|
|
|
|
* their mips are darker than the original (since the mips should ignore the gamma in L32).
|
|
|
|
*
|
|
|
|
* Ensure that in S32 drawing mode, all images/mips look the same, and look correct (i.e.
|
|
|
|
* the mip levels match the original in brightness).
|
|
|
|
*/
|
|
|
|
DEF_SIMPLE_GM(mipmap_srgb, canvas, 260, 230) {
|
|
|
|
sk_sp<SkImage> limg = make(nullptr);
|
2017-02-07 18:56:11 +00:00
|
|
|
sk_sp<SkImage> simg = make(SkColorSpace::MakeSRGB());
|
2016-06-10 18:41:47 +00:00
|
|
|
|
|
|
|
canvas->translate(10, 10);
|
|
|
|
show_mips(canvas, limg.get());
|
|
|
|
canvas->translate(0, limg->height() + 10.0f);
|
|
|
|
show_mips(canvas, simg.get());
|
|
|
|
}
|
|
|
|
|
2016-06-21 20:40:12 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// create a gradient image computed raw, so we can wrap it as a linear or srgb image
|
|
|
|
static sk_sp<SkImage> make_g8_gradient(sk_sp<SkColorSpace> cs) {
|
|
|
|
const int N = 100;
|
|
|
|
SkImageInfo info = SkImageInfo::Make(N, N, kGray_8_SkColorType, kOpaque_SkAlphaType, cs);
|
|
|
|
SkBitmap bm;
|
|
|
|
bm.allocPixels(info);
|
|
|
|
|
|
|
|
for (int y = 0; y < N; ++y) {
|
|
|
|
for (int x = 0; x < N; ++x) {
|
|
|
|
*bm.getAddr8(x, y) = static_cast<uint8_t>(255.0f * ((x + y) / (2.0f * (N - 1))));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bm.setImmutable();
|
2020-12-23 15:11:33 +00:00
|
|
|
return bm.asImage();
|
2016-06-21 20:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void show_mips_only(SkCanvas* canvas, SkImage* img) {
|
2021-01-23 17:23:23 +00:00
|
|
|
SkSamplingOptions sampling(SkFilterMode::kLinear,
|
|
|
|
SkMipmapMode::kLinear);
|
2016-06-21 20:40:12 +00:00
|
|
|
|
|
|
|
// Want to ensure we never draw fractional pixels, so we use an IRect
|
|
|
|
SkIRect dst = SkIRect::MakeWH(img->width() / 2, img->height() / 2);
|
|
|
|
while (dst.width() > 5) {
|
2021-01-23 17:23:23 +00:00
|
|
|
canvas->drawImageRect(img, SkRect::Make(dst), sampling, nullptr);
|
2016-06-21 20:40:12 +00:00
|
|
|
dst.offset(dst.width() + 10, 0);
|
|
|
|
dst.fRight = dst.fLeft + dst.width() / 2;
|
|
|
|
dst.fBottom = dst.fTop + dst.height() / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure that in L32 drawing mode, both images/mips look the same as each other, and
|
|
|
|
* their mips are darker than the original (since the mips should ignore the gamma in L32).
|
|
|
|
*
|
|
|
|
* Ensure that in S32 drawing mode, all images/mips look the same, and look correct (i.e.
|
|
|
|
* the mip levels match the original in brightness).
|
|
|
|
*/
|
|
|
|
DEF_SIMPLE_GM(mipmap_gray8_srgb, canvas, 260, 230) {
|
|
|
|
sk_sp<SkImage> limg = make_g8_gradient(nullptr);
|
2017-02-07 18:56:11 +00:00
|
|
|
sk_sp<SkImage> simg = make_g8_gradient(SkColorSpace::MakeSRGB());
|
2016-06-21 20:40:12 +00:00
|
|
|
|
|
|
|
canvas->translate(10, 10);
|
|
|
|
show_mips_only(canvas, limg.get());
|
|
|
|
canvas->translate(0, limg->height() + 10.0f);
|
|
|
|
show_mips_only(canvas, simg.get());
|
|
|
|
}
|