skia2/tests/ImageGeneratorTest.cpp
Kevin Lubick 5e8f45faf1 [includes] Prepare for moving SkColorSpace to forward declare
This updates all our callsites in preparation for removing
the #include "include/core/SkColorSpace.h" from SkImageInfo.h

According to go/chrome-includes [1], this will save ~150MB
(0.07%) from the compilation size. I think SkColorSpace is
a big include because it loads the skcms header, which is
big.

The follow-on CL will remove that link, once clients have
been updated as well.

[1] https://commondatastorage.googleapis.com/chromium-browser-clang/chrome_includes_2022-03-31_124042.html#view=edges&filter=%5Ethird_party%2Fskia%2Finclude%2Fcore%2FSkImageInfo%5C.h%24&sort=asize&reverse=&includer=%5Ethird_party%2Fskia%2Finclude%2Fcore%2FSkImageInfo%5C.h%24&included=&limit=1000

Change-Id: I1b5ff491ac495317b0e5af3a2082b080d43697ae
Bug: skia:13052
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/525639
Reviewed-by: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Reviewed-by: Florin Malita <fmalita@google.com>
Commit-Queue: Kevin Lubick <kjlubick@google.com>
2022-03-31 19:50:10 +00:00

125 lines
4.5 KiB
C++

/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkCanvas.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkData.h"
#include "include/core/SkGraphics.h"
#include "include/core/SkImageGenerator.h"
#include "include/private/SkImageInfoPriv.h"
#include "tests/Test.h"
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
#include "include/ports/SkImageGeneratorCG.h"
#elif defined(SK_BUILD_FOR_WIN)
#include "include/ports/SkImageGeneratorWIC.h"
#endif
static bool gMyFactoryWasCalled;
static std::unique_ptr<SkImageGenerator> my_factory(sk_sp<SkData>) {
gMyFactoryWasCalled = true;
return nullptr;
}
static void test_imagegenerator_factory(skiatest::Reporter* reporter) {
// just need a non-empty data to test things
sk_sp<SkData> data(SkData::MakeWithCString("test_imagegenerator_factory"));
gMyFactoryWasCalled = false;
REPORTER_ASSERT(reporter, !gMyFactoryWasCalled);
std::unique_ptr<SkImageGenerator> gen = SkImageGenerator::MakeFromEncoded(data);
REPORTER_ASSERT(reporter, nullptr == gen);
REPORTER_ASSERT(reporter, !gMyFactoryWasCalled);
// Test is racy, in that it hopes no other thread is changing this global...
auto prev = SkGraphics::SetImageGeneratorFromEncodedDataFactory(my_factory);
gen = SkImageGenerator::MakeFromEncoded(data);
REPORTER_ASSERT(reporter, nullptr == gen);
REPORTER_ASSERT(reporter, gMyFactoryWasCalled);
// This just verifies that the signatures match.
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
SkGraphics::SetImageGeneratorFromEncodedDataFactory(SkImageGeneratorCG::MakeFromEncodedCG);
#elif defined(SK_BUILD_FOR_WIN)
SkGraphics::SetImageGeneratorFromEncodedDataFactory(SkImageGeneratorWIC::MakeFromEncodedWIC);
#endif
SkGraphics::SetImageGeneratorFromEncodedDataFactory(prev);
}
class MyImageGenerator : public SkImageGenerator {
public:
MyImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
};
DEF_TEST(ImageGenerator, reporter) {
MyImageGenerator ig;
SkYUVAPixmapInfo yuvaPixmapInfo;
// Check that the YUV decoding API does not cause any crashes
ig.queryYUVAInfo(SkYUVAPixmapInfo::SupportedDataTypes::All(), &yuvaPixmapInfo);
SkYUVAInfo yuvaInfo({250, 250},
SkYUVAInfo::PlaneConfig::kY_UV,
SkYUVAInfo::Subsampling::k420,
kJPEG_Full_SkYUVColorSpace);
yuvaPixmapInfo = SkYUVAPixmapInfo(yuvaInfo,
SkYUVAPixmapInfo::DataType::kUnorm8,
/*rowBytes[]*/ nullptr);
SkYUVAPixmaps yuvaPixmaps = SkYUVAPixmaps::Allocate(yuvaPixmapInfo);
ig.getYUVAPlanes(yuvaPixmaps);
// Suppressed due to https://code.google.com/p/skia/issues/detail?id=4339
if ((false)) {
test_imagegenerator_factory(reporter);
}
}
#include "include/core/SkPictureRecorder.h"
#include "src/core/SkAutoMalloc.h"
static sk_sp<SkPicture> make_picture() {
SkPictureRecorder recorder;
recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
return recorder.finishRecordingAsPicture();
}
DEF_TEST(PictureImageGenerator, reporter) {
const struct {
SkColorType fColorType;
SkAlphaType fAlphaType;
} recs[] = {
{ kRGBA_8888_SkColorType, kPremul_SkAlphaType },
{ kBGRA_8888_SkColorType, kPremul_SkAlphaType },
{ kRGBA_F16_SkColorType, kPremul_SkAlphaType },
{ kRGBA_F32_SkColorType, kPremul_SkAlphaType },
{ kRGBA_1010102_SkColorType, kPremul_SkAlphaType },
{ kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
{ kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
{ kRGBA_F16_SkColorType, kUnpremul_SkAlphaType },
{ kRGBA_F32_SkColorType, kUnpremul_SkAlphaType },
{ kRGBA_1010102_SkColorType, kUnpremul_SkAlphaType },
};
auto colorspace = SkColorSpace::MakeSRGB();
auto picture = make_picture();
auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr,
SkImage::BitDepth::kU8, colorspace);
// worst case for all requests
SkAutoMalloc storage(100 * 100 * SkColorTypeBytesPerPixel(kRGBA_F32_SkColorType));
for (const auto& rec : recs) {
SkImageInfo info = SkImageInfo::Make(100, 100, rec.fColorType, rec.fAlphaType, colorspace);
REPORTER_ASSERT(reporter, gen->getPixels(info, storage.get(), info.minRowBytes()));
}
}