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 |
| 15 | #include "SkImage.h" |
| 16 | #include "SkSurface.h" |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 17 | #include "SkReadBuffer.h" |
| 18 | #include "SkWriteBuffer.h" |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 19 | |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 20 | static void test_flatten(skiatest::Reporter* reporter, const SkImageInfo& info) { |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 21 | // Need a safe amount of 4-byte aligned storage. Note that one of the test ICC profiles |
| 22 | // is ~7500 bytes. |
| 23 | const size_t storageBytes = 8000; |
| 24 | SkAutoTMalloc<uint32_t> storage(storageBytes / sizeof(uint32_t)); |
| 25 | SkBinaryWriteBuffer wb(storage.get(), storageBytes); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 26 | info.flatten(wb); |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 27 | SkASSERT(wb.bytesWritten() < storageBytes); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 28 | |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 29 | SkReadBuffer rb(storage.get(), wb.bytesWritten()); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 30 | |
| 31 | // 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] | 32 | SkImageInfo info2 = SkImageInfo::Make(0xB8, 0xB8, (SkColorType) 0xB8, (SkAlphaType) 0xB8); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 33 | |
| 34 | info2.unflatten(rb); |
| 35 | REPORTER_ASSERT(reporter, rb.offset() == wb.bytesWritten()); |
msarett | ded0f26 | 2016-06-16 05:33:31 -0700 | [diff] [blame] | 36 | |
msarett | 111a42d | 2016-06-22 08:18:54 -0700 | [diff] [blame] | 37 | REPORTER_ASSERT(reporter, info == info2); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | DEF_TEST(ImageInfo_flattening, reporter) { |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 41 | sk_sp<SkData> data = |
| 42 | SkData::MakeFromFileName(GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str()); |
Brian Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 43 | sk_sp<SkColorSpace> space0 = SkColorSpace::MakeICC(data->data(), data->size()); |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 44 | data = SkData::MakeFromFileName( GetResourcePath("icc_profiles/HP_Z32x.icc").c_str()); |
Brian Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 45 | sk_sp<SkColorSpace> space1 = SkColorSpace::MakeICC(data->data(), data->size()); |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 46 | data = SkData::MakeFromFileName(GetResourcePath("icc_profiles/upperLeft.icc").c_str()); |
Brian Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 47 | sk_sp<SkColorSpace> space2 = SkColorSpace::MakeICC(data->data(), data->size()); |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 48 | data = SkData::MakeFromFileName(GetResourcePath("icc_profiles/upperRight.icc").c_str()); |
Brian Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 49 | sk_sp<SkColorSpace> space3 = SkColorSpace::MakeICC(data->data(), data->size()); |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 50 | |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 51 | sk_sp<SkColorSpace> spaces[] = { |
| 52 | nullptr, |
Brian Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 53 | SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named), |
| 54 | SkColorSpace::MakeNamed(SkColorSpace::kAdobeRGB_Named), |
msarett | abbd6d5 | 2016-08-01 09:43:08 -0700 | [diff] [blame] | 55 | space0, |
| 56 | space1, |
| 57 | space2, |
| 58 | space3, |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 61 | for (int ct = 0; ct <= kLastEnum_SkColorType; ++ct) { |
| 62 | for (int at = 0; at <= kLastEnum_SkAlphaType; ++at) { |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 63 | for (auto& cs : spaces) { |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 64 | SkImageInfo info = SkImageInfo::Make(100, 200, |
| 65 | static_cast<SkColorType>(ct), |
| 66 | static_cast<SkAlphaType>(at), |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 67 | cs); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 68 | test_flatten(reporter, info); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 73 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 74 | static void check_isopaque(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface, |
| 75 | bool expectedOpaque) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 76 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 77 | REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque); |
| 78 | } |
| 79 | |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 80 | DEF_TEST(ImageIsOpaqueTest, reporter) { |
| 81 | SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 82 | auto surfaceTransparent(SkSurface::MakeRaster(infoTransparent)); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 83 | check_isopaque(reporter, surfaceTransparent, false); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 84 | |
| 85 | SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 86 | auto surfaceOpaque(SkSurface::MakeRaster(infoOpaque)); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 87 | check_isopaque(reporter, surfaceOpaque, true); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | #if SK_SUPPORT_GPU |
| 91 | |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 92 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 93 | GrContext* context = ctxInfo.grContext(); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 94 | SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 95 | auto surfaceTransparent(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, infoTransparent)); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 96 | check_isopaque(reporter, surfaceTransparent, false); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 97 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 98 | SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 99 | auto surfaceOpaque(SkSurface::MakeRenderTarget(context,SkBudgeted::kNo, infoOpaque)); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 100 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 101 | check_isopaque(reporter, surfaceOpaque, true); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | #endif |
reed | 9e2ed83 | 2016-10-31 05:27:28 -0700 | [diff] [blame] | 105 | |
| 106 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 107 | #include "SkPictureRecorder.h" |
| 108 | |
| 109 | static sk_sp<SkPicture> make_picture() { |
| 110 | SkPictureRecorder recorder; |
| 111 | SkCanvas* canvas = recorder.beginRecording({ 0, 0, 10, 10 }); |
| 112 | canvas->drawColor(SK_ColorRED); |
| 113 | return recorder.finishRecordingAsPicture(); |
| 114 | } |
| 115 | |
| 116 | DEF_TEST(Image_isAlphaOnly, reporter) { |
| 117 | SkPMColor pmColors = 0; |
| 118 | SkPixmap pmap = { |
| 119 | SkImageInfo::MakeN32Premul(1, 1), |
| 120 | &pmColors, |
| 121 | sizeof(pmColors) |
| 122 | }; |
| 123 | for (auto& image : { |
| 124 | SkImage::MakeRasterCopy(pmap), |
| 125 | GetResourceAsImage("mandrill_128.png"), |
| 126 | GetResourceAsImage("color_wheel.jpg"), |
Brian Osman | 138ea97 | 2016-12-16 11:55:18 -0500 | [diff] [blame^] | 127 | SkImage::MakeFromPicture(make_picture(), { 10, 10 }, nullptr, nullptr, |
| 128 | SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named)), |
reed | 9e2ed83 | 2016-10-31 05:27:28 -0700 | [diff] [blame] | 129 | }) |
| 130 | { |
| 131 | REPORTER_ASSERT(reporter, image->isAlphaOnly() == false); |
| 132 | } |
| 133 | |
| 134 | REPORTER_ASSERT(reporter, SkImage::MakeRasterCopy({ |
| 135 | SkImageInfo::MakeA8(1, 1), (uint8_t*)&pmColors, 1})->isAlphaOnly() == true); |
| 136 | } |