blob: 81f1d7660976f36b72c7ea21ee456210c5538d55 [file] [log] [blame]
piotaixrd2a35222014-08-19 14:29:02 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkTypes.h"
msarettabbd6d52016-08-01 09:43:08 -07009#include "Resources.h"
reed7c748852014-11-10 08:57:21 -080010#include "Test.h"
11
piotaixrd2a35222014-08-19 14:29:02 -070012#if SK_SUPPORT_GPU
kkinnunen15302832015-12-01 04:35:26 -080013#include "GrContext.h"
piotaixrd2a35222014-08-19 14:29:02 -070014#endif
Matt Sarett77a7a1b2017-02-07 13:56:11 -050015#include "SkColorSpace_Base.h"
piotaixrd2a35222014-08-19 14:29:02 -070016#include "SkImage.h"
17#include "SkSurface.h"
reed7c748852014-11-10 08:57:21 -080018#include "SkReadBuffer.h"
19#include "SkWriteBuffer.h"
piotaixrd2a35222014-08-19 14:29:02 -070020
reed7c748852014-11-10 08:57:21 -080021static void test_flatten(skiatest::Reporter* reporter, const SkImageInfo& info) {
msarettabbd6d52016-08-01 09:43:08 -070022 // Need a safe amount of 4-byte aligned storage. Note that one of the test ICC profiles
23 // is ~7500 bytes.
24 const size_t storageBytes = 8000;
25 SkAutoTMalloc<uint32_t> storage(storageBytes / sizeof(uint32_t));
26 SkBinaryWriteBuffer wb(storage.get(), storageBytes);
reed7c748852014-11-10 08:57:21 -080027 info.flatten(wb);
msarettabbd6d52016-08-01 09:43:08 -070028 SkASSERT(wb.bytesWritten() < storageBytes);
reed7c748852014-11-10 08:57:21 -080029
msarettabbd6d52016-08-01 09:43:08 -070030 SkReadBuffer rb(storage.get(), wb.bytesWritten());
reed7c748852014-11-10 08:57:21 -080031
32 // pick a noisy byte pattern, so we ensure that unflatten sets all of our fields
reeddabe5d32016-06-21 10:28:14 -070033 SkImageInfo info2 = SkImageInfo::Make(0xB8, 0xB8, (SkColorType) 0xB8, (SkAlphaType) 0xB8);
reed7c748852014-11-10 08:57:21 -080034
35 info2.unflatten(rb);
36 REPORTER_ASSERT(reporter, rb.offset() == wb.bytesWritten());
msarettded0f262016-06-16 05:33:31 -070037
msarett111a42d2016-06-22 08:18:54 -070038 REPORTER_ASSERT(reporter, info == info2);
reed7c748852014-11-10 08:57:21 -080039}
40
41DEF_TEST(ImageInfo_flattening, reporter) {
msarettabbd6d52016-08-01 09:43:08 -070042 sk_sp<SkData> data =
43 SkData::MakeFromFileName(GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str());
Brian Osman526972e2016-10-24 09:24:02 -040044 sk_sp<SkColorSpace> space0 = SkColorSpace::MakeICC(data->data(), data->size());
msarettabbd6d52016-08-01 09:43:08 -070045 data = SkData::MakeFromFileName( GetResourcePath("icc_profiles/HP_Z32x.icc").c_str());
Brian Osman526972e2016-10-24 09:24:02 -040046 sk_sp<SkColorSpace> space1 = SkColorSpace::MakeICC(data->data(), data->size());
msarettabbd6d52016-08-01 09:43:08 -070047 data = SkData::MakeFromFileName(GetResourcePath("icc_profiles/upperLeft.icc").c_str());
Brian Osman526972e2016-10-24 09:24:02 -040048 sk_sp<SkColorSpace> space2 = SkColorSpace::MakeICC(data->data(), data->size());
msarettabbd6d52016-08-01 09:43:08 -070049 data = SkData::MakeFromFileName(GetResourcePath("icc_profiles/upperRight.icc").c_str());
Brian Osman526972e2016-10-24 09:24:02 -040050 sk_sp<SkColorSpace> space3 = SkColorSpace::MakeICC(data->data(), data->size());
msarettabbd6d52016-08-01 09:43:08 -070051
reeddabe5d32016-06-21 10:28:14 -070052 sk_sp<SkColorSpace> spaces[] = {
53 nullptr,
Matt Sarett77a7a1b2017-02-07 13:56:11 -050054 SkColorSpace::MakeSRGB(),
55 SkColorSpace_Base::MakeNamed(SkColorSpace_Base::kAdobeRGB_Named),
msarettabbd6d52016-08-01 09:43:08 -070056 space0,
57 space1,
58 space2,
59 space3,
reeddabe5d32016-06-21 10:28:14 -070060 };
61
reed7c748852014-11-10 08:57:21 -080062 for (int ct = 0; ct <= kLastEnum_SkColorType; ++ct) {
63 for (int at = 0; at <= kLastEnum_SkAlphaType; ++at) {
reeddabe5d32016-06-21 10:28:14 -070064 for (auto& cs : spaces) {
reed7c748852014-11-10 08:57:21 -080065 SkImageInfo info = SkImageInfo::Make(100, 200,
66 static_cast<SkColorType>(ct),
67 static_cast<SkAlphaType>(at),
reeddabe5d32016-06-21 10:28:14 -070068 cs);
reed7c748852014-11-10 08:57:21 -080069 test_flatten(reporter, info);
70 }
71 }
72 }
73}
piotaixrd2a35222014-08-19 14:29:02 -070074
reede8f30622016-03-23 18:59:25 -070075static void check_isopaque(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
76 bool expectedOpaque) {
reed9ce9d672016-03-17 10:51:11 -070077 sk_sp<SkImage> image(surface->makeImageSnapshot());
reedb8881362014-08-20 07:24:01 -070078 REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque);
79}
80
piotaixrd2a35222014-08-19 14:29:02 -070081DEF_TEST(ImageIsOpaqueTest, reporter) {
82 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
reede8f30622016-03-23 18:59:25 -070083 auto surfaceTransparent(SkSurface::MakeRaster(infoTransparent));
reedb8881362014-08-20 07:24:01 -070084 check_isopaque(reporter, surfaceTransparent, false);
piotaixrd2a35222014-08-19 14:29:02 -070085
86 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070087 auto surfaceOpaque(SkSurface::MakeRaster(infoOpaque));
reedb8881362014-08-20 07:24:01 -070088 check_isopaque(reporter, surfaceOpaque, true);
piotaixrd2a35222014-08-19 14:29:02 -070089}
90
91#if SK_SUPPORT_GPU
92
egdanielab527a52016-06-28 08:07:26 -070093DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070094 GrContext* context = ctxInfo.grContext();
kkinnunen15302832015-12-01 04:35:26 -080095 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
reede8f30622016-03-23 18:59:25 -070096 auto surfaceTransparent(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, infoTransparent));
kkinnunen15302832015-12-01 04:35:26 -080097 check_isopaque(reporter, surfaceTransparent, false);
piotaixrd2a35222014-08-19 14:29:02 -070098
kkinnunen15302832015-12-01 04:35:26 -080099 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -0700100 auto surfaceOpaque(SkSurface::MakeRenderTarget(context,SkBudgeted::kNo, infoOpaque));
piotaixrd2a35222014-08-19 14:29:02 -0700101
kkinnunen15302832015-12-01 04:35:26 -0800102 check_isopaque(reporter, surfaceOpaque, true);
piotaixrd2a35222014-08-19 14:29:02 -0700103}
104
105#endif
reed9e2ed832016-10-31 05:27:28 -0700106
107///////////////////////////////////////////////////////////////////////////////////////////////////
108#include "SkPictureRecorder.h"
109
110static sk_sp<SkPicture> make_picture() {
111 SkPictureRecorder recorder;
112 SkCanvas* canvas = recorder.beginRecording({ 0, 0, 10, 10 });
113 canvas->drawColor(SK_ColorRED);
114 return recorder.finishRecordingAsPicture();
115}
116
117DEF_TEST(Image_isAlphaOnly, reporter) {
118 SkPMColor pmColors = 0;
119 SkPixmap pmap = {
120 SkImageInfo::MakeN32Premul(1, 1),
121 &pmColors,
122 sizeof(pmColors)
123 };
124 for (auto& image : {
125 SkImage::MakeRasterCopy(pmap),
126 GetResourceAsImage("mandrill_128.png"),
127 GetResourceAsImage("color_wheel.jpg"),
Brian Osman138ea972016-12-16 11:55:18 -0500128 SkImage::MakeFromPicture(make_picture(), { 10, 10 }, nullptr, nullptr,
Matt Sarette94255d2017-01-09 12:38:59 -0500129 SkImage::BitDepth::kU8,
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500130 SkColorSpace::MakeSRGB()),
reed9e2ed832016-10-31 05:27:28 -0700131 })
132 {
133 REPORTER_ASSERT(reporter, image->isAlphaOnly() == false);
134 }
135
136 REPORTER_ASSERT(reporter, SkImage::MakeRasterCopy({
137 SkImageInfo::MakeA8(1, 1), (uint8_t*)&pmColors, 1})->isAlphaOnly() == true);
138}