piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 1 | /* |
| 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" |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 9 | #include "Resources.h" |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 10 | #include "Test.h" |
| 11 | |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 12 | #if SK_SUPPORT_GPU |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 13 | #include "GrContext.h" |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 14 | #endif |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 15 | #include "SkColorSpace_Base.h" |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 16 | #include "SkImage.h" |
| 17 | #include "SkSurface.h" |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 18 | #include "SkReadBuffer.h" |
| 19 | #include "SkWriteBuffer.h" |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 20 | |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 21 | static void test_flatten(skiatest::Reporter* reporter, const SkImageInfo& info) { |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 22 | // 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); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 27 | info.flatten(wb); |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 28 | SkASSERT(wb.bytesWritten() < storageBytes); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 29 | |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 30 | SkReadBuffer rb(storage.get(), wb.bytesWritten()); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 31 | |
| 32 | // pick a noisy byte pattern, so we ensure that unflatten sets all of our fields |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 33 | SkImageInfo info2 = SkImageInfo::Make(0xB8, 0xB8, (SkColorType) 0xB8, (SkAlphaType) 0xB8); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 34 | |
| 35 | info2.unflatten(rb); |
| 36 | REPORTER_ASSERT(reporter, rb.offset() == wb.bytesWritten()); |
msarett | ded0f26 | 2016-06-16 05:33:31 -0700 | [diff] [blame] | 37 | |
msarett | 111a42d | 2016-06-22 08:18:54 -0700 | [diff] [blame] | 38 | REPORTER_ASSERT(reporter, info == info2); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | DEF_TEST(ImageInfo_flattening, reporter) { |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 42 | sk_sp<SkData> data = |
| 43 | SkData::MakeFromFileName(GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str()); |
Brian Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 44 | sk_sp<SkColorSpace> space0 = SkColorSpace::MakeICC(data->data(), data->size()); |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 45 | data = SkData::MakeFromFileName( GetResourcePath("icc_profiles/HP_Z32x.icc").c_str()); |
Brian Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 46 | sk_sp<SkColorSpace> space1 = SkColorSpace::MakeICC(data->data(), data->size()); |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 47 | data = SkData::MakeFromFileName(GetResourcePath("icc_profiles/upperLeft.icc").c_str()); |
Brian Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 48 | sk_sp<SkColorSpace> space2 = SkColorSpace::MakeICC(data->data(), data->size()); |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 49 | data = SkData::MakeFromFileName(GetResourcePath("icc_profiles/upperRight.icc").c_str()); |
Brian Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 50 | sk_sp<SkColorSpace> space3 = SkColorSpace::MakeICC(data->data(), data->size()); |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 51 | |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 52 | sk_sp<SkColorSpace> spaces[] = { |
| 53 | nullptr, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 54 | SkColorSpace::MakeSRGB(), |
| 55 | SkColorSpace_Base::MakeNamed(SkColorSpace_Base::kAdobeRGB_Named), |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 56 | space0, |
| 57 | space1, |
| 58 | space2, |
| 59 | space3, |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 62 | for (int ct = 0; ct <= kLastEnum_SkColorType; ++ct) { |
| 63 | for (int at = 0; at <= kLastEnum_SkAlphaType; ++at) { |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 64 | for (auto& cs : spaces) { |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 65 | SkImageInfo info = SkImageInfo::Make(100, 200, |
| 66 | static_cast<SkColorType>(ct), |
| 67 | static_cast<SkAlphaType>(at), |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 68 | cs); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 69 | test_flatten(reporter, info); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 74 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 75 | static void check_isopaque(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface, |
| 76 | bool expectedOpaque) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 77 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 78 | REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque); |
| 79 | } |
| 80 | |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 81 | DEF_TEST(ImageIsOpaqueTest, reporter) { |
| 82 | SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 83 | auto surfaceTransparent(SkSurface::MakeRaster(infoTransparent)); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 84 | check_isopaque(reporter, surfaceTransparent, false); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 85 | |
| 86 | SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 87 | auto surfaceOpaque(SkSurface::MakeRaster(infoOpaque)); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 88 | check_isopaque(reporter, surfaceOpaque, true); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | #if SK_SUPPORT_GPU |
| 92 | |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 93 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 94 | GrContext* context = ctxInfo.grContext(); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 95 | SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 96 | auto surfaceTransparent(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, infoTransparent)); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 97 | check_isopaque(reporter, surfaceTransparent, false); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 98 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 99 | SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 100 | auto surfaceOpaque(SkSurface::MakeRenderTarget(context,SkBudgeted::kNo, infoOpaque)); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 101 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 102 | check_isopaque(reporter, surfaceOpaque, true); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | #endif |
reed | 9e2ed83 | 2016-10-31 05:27:28 -0700 | [diff] [blame] | 106 | |
| 107 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 108 | #include "SkPictureRecorder.h" |
| 109 | |
| 110 | static 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 | |
| 117 | DEF_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 Osman | 138ea97 | 2016-12-16 11:55:18 -0500 | [diff] [blame] | 128 | SkImage::MakeFromPicture(make_picture(), { 10, 10 }, nullptr, nullptr, |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 129 | SkImage::BitDepth::kU8, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 130 | SkColorSpace::MakeSRGB()), |
reed | 9e2ed83 | 2016-10-31 05:27:28 -0700 | [diff] [blame] | 131 | }) |
| 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 | } |