2014-08-19 21:29:02 +00:00
|
|
|
/*
|
|
|
|
* 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 "SkTypes.h"
|
2016-08-01 16:43:08 +00:00
|
|
|
#include "Resources.h"
|
2014-11-10 16:57:21 +00:00
|
|
|
#include "Test.h"
|
|
|
|
|
2014-08-19 21:29:02 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2015-12-01 12:35:26 +00:00
|
|
|
#include "GrContext.h"
|
2014-08-19 21:29:02 +00:00
|
|
|
#endif
|
|
|
|
#include "SkImage.h"
|
|
|
|
#include "SkSurface.h"
|
2014-11-10 16:57:21 +00:00
|
|
|
#include "SkReadBuffer.h"
|
|
|
|
#include "SkWriteBuffer.h"
|
2014-08-19 21:29:02 +00:00
|
|
|
|
2014-11-10 16:57:21 +00:00
|
|
|
static void test_flatten(skiatest::Reporter* reporter, const SkImageInfo& info) {
|
2016-08-01 16:43:08 +00:00
|
|
|
// Need a safe amount of 4-byte aligned storage. Note that one of the test ICC profiles
|
|
|
|
// is ~7500 bytes.
|
|
|
|
const size_t storageBytes = 8000;
|
|
|
|
SkAutoTMalloc<uint32_t> storage(storageBytes / sizeof(uint32_t));
|
|
|
|
SkBinaryWriteBuffer wb(storage.get(), storageBytes);
|
2014-11-10 16:57:21 +00:00
|
|
|
info.flatten(wb);
|
2016-08-01 16:43:08 +00:00
|
|
|
SkASSERT(wb.bytesWritten() < storageBytes);
|
2014-11-10 16:57:21 +00:00
|
|
|
|
2016-08-01 16:43:08 +00:00
|
|
|
SkReadBuffer rb(storage.get(), wb.bytesWritten());
|
2014-11-10 16:57:21 +00:00
|
|
|
|
|
|
|
// pick a noisy byte pattern, so we ensure that unflatten sets all of our fields
|
2016-06-21 17:28:14 +00:00
|
|
|
SkImageInfo info2 = SkImageInfo::Make(0xB8, 0xB8, (SkColorType) 0xB8, (SkAlphaType) 0xB8);
|
2014-11-10 16:57:21 +00:00
|
|
|
|
|
|
|
info2.unflatten(rb);
|
|
|
|
REPORTER_ASSERT(reporter, rb.offset() == wb.bytesWritten());
|
2016-06-16 12:33:31 +00:00
|
|
|
|
2016-06-22 15:18:54 +00:00
|
|
|
REPORTER_ASSERT(reporter, info == info2);
|
2014-11-10 16:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(ImageInfo_flattening, reporter) {
|
2016-08-01 16:43:08 +00:00
|
|
|
sk_sp<SkData> data =
|
|
|
|
SkData::MakeFromFileName(GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str());
|
2016-10-24 13:24:02 +00:00
|
|
|
sk_sp<SkColorSpace> space0 = SkColorSpace::MakeICC(data->data(), data->size());
|
2016-08-01 16:43:08 +00:00
|
|
|
data = SkData::MakeFromFileName( GetResourcePath("icc_profiles/HP_Z32x.icc").c_str());
|
2016-10-24 13:24:02 +00:00
|
|
|
sk_sp<SkColorSpace> space1 = SkColorSpace::MakeICC(data->data(), data->size());
|
2016-08-01 16:43:08 +00:00
|
|
|
data = SkData::MakeFromFileName(GetResourcePath("icc_profiles/upperLeft.icc").c_str());
|
2016-10-24 13:24:02 +00:00
|
|
|
sk_sp<SkColorSpace> space2 = SkColorSpace::MakeICC(data->data(), data->size());
|
2016-08-01 16:43:08 +00:00
|
|
|
data = SkData::MakeFromFileName(GetResourcePath("icc_profiles/upperRight.icc").c_str());
|
2016-10-24 13:24:02 +00:00
|
|
|
sk_sp<SkColorSpace> space3 = SkColorSpace::MakeICC(data->data(), data->size());
|
2016-08-01 16:43:08 +00:00
|
|
|
|
2016-06-21 17:28:14 +00:00
|
|
|
sk_sp<SkColorSpace> spaces[] = {
|
|
|
|
nullptr,
|
2016-10-24 13:24:02 +00:00
|
|
|
SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named),
|
|
|
|
SkColorSpace::MakeNamed(SkColorSpace::kAdobeRGB_Named),
|
2016-08-01 16:43:08 +00:00
|
|
|
space0,
|
|
|
|
space1,
|
|
|
|
space2,
|
|
|
|
space3,
|
2016-06-21 17:28:14 +00:00
|
|
|
};
|
|
|
|
|
2014-11-10 16:57:21 +00:00
|
|
|
for (int ct = 0; ct <= kLastEnum_SkColorType; ++ct) {
|
|
|
|
for (int at = 0; at <= kLastEnum_SkAlphaType; ++at) {
|
2016-06-21 17:28:14 +00:00
|
|
|
for (auto& cs : spaces) {
|
2014-11-10 16:57:21 +00:00
|
|
|
SkImageInfo info = SkImageInfo::Make(100, 200,
|
|
|
|
static_cast<SkColorType>(ct),
|
|
|
|
static_cast<SkAlphaType>(at),
|
2016-06-21 17:28:14 +00:00
|
|
|
cs);
|
2014-11-10 16:57:21 +00:00
|
|
|
test_flatten(reporter, info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-19 21:29:02 +00:00
|
|
|
|
2016-03-24 01:59:25 +00:00
|
|
|
static void check_isopaque(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
|
|
|
|
bool expectedOpaque) {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image(surface->makeImageSnapshot());
|
2014-08-20 14:24:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque);
|
|
|
|
}
|
|
|
|
|
2014-08-19 21:29:02 +00:00
|
|
|
DEF_TEST(ImageIsOpaqueTest, reporter) {
|
|
|
|
SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surfaceTransparent(SkSurface::MakeRaster(infoTransparent));
|
2014-08-20 14:24:01 +00:00
|
|
|
check_isopaque(reporter, surfaceTransparent, false);
|
2014-08-19 21:29:02 +00:00
|
|
|
|
|
|
|
SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surfaceOpaque(SkSurface::MakeRaster(infoOpaque));
|
2014-08-20 14:24:01 +00:00
|
|
|
check_isopaque(reporter, surfaceOpaque, true);
|
2014-08-19 21:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
2016-06-28 15:07:26 +00:00
|
|
|
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, reporter, ctxInfo) {
|
2016-05-11 13:33:06 +00:00
|
|
|
GrContext* context = ctxInfo.grContext();
|
2015-12-01 12:35:26 +00:00
|
|
|
SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surfaceTransparent(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, infoTransparent));
|
2015-12-01 12:35:26 +00:00
|
|
|
check_isopaque(reporter, surfaceTransparent, false);
|
2014-08-19 21:29:02 +00:00
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surfaceOpaque(SkSurface::MakeRenderTarget(context,SkBudgeted::kNo, infoOpaque));
|
2015-10-01 16:49:25 +00:00
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
check_isopaque(reporter, surfaceOpaque, true);
|
2014-08-19 21:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2016-10-31 12:27:28 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "SkPictureRecorder.h"
|
|
|
|
|
|
|
|
static sk_sp<SkPicture> make_picture() {
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
SkCanvas* canvas = recorder.beginRecording({ 0, 0, 10, 10 });
|
|
|
|
canvas->drawColor(SK_ColorRED);
|
|
|
|
return recorder.finishRecordingAsPicture();
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(Image_isAlphaOnly, reporter) {
|
|
|
|
SkPMColor pmColors = 0;
|
|
|
|
SkPixmap pmap = {
|
|
|
|
SkImageInfo::MakeN32Premul(1, 1),
|
|
|
|
&pmColors,
|
|
|
|
sizeof(pmColors)
|
|
|
|
};
|
|
|
|
for (auto& image : {
|
|
|
|
SkImage::MakeRasterCopy(pmap),
|
|
|
|
GetResourceAsImage("mandrill_128.png"),
|
|
|
|
GetResourceAsImage("color_wheel.jpg"),
|
2016-12-16 16:55:18 +00:00
|
|
|
SkImage::MakeFromPicture(make_picture(), { 10, 10 }, nullptr, nullptr,
|
2017-01-09 17:38:59 +00:00
|
|
|
SkImage::BitDepth::kU8,
|
2016-12-16 16:55:18 +00:00
|
|
|
SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named)),
|
2016-10-31 12:27:28 +00:00
|
|
|
})
|
|
|
|
{
|
|
|
|
REPORTER_ASSERT(reporter, image->isAlphaOnly() == false);
|
|
|
|
}
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, SkImage::MakeRasterCopy({
|
|
|
|
SkImageInfo::MakeA8(1, 1), (uint8_t*)&pmColors, 1})->isAlphaOnly() == true);
|
|
|
|
}
|