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 | |
Kevin Lubick | 8c73a59 | 2022-10-17 15:25:35 -0400 | [diff] [blame] | 8 | #include "include/core/SkAlphaType.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkColorSpace.h" |
| 12 | #include "include/core/SkImage.h" |
| 13 | #include "include/core/SkImageInfo.h" |
Kevin Lubick | f437d01 | 2023-06-26 14:35:06 -0400 | [diff] [blame] | 14 | #include "include/core/SkPicture.h" // IWYU pragma: keep |
Kevin Lubick | 8c73a59 | 2022-10-17 15:25:35 -0400 | [diff] [blame] | 15 | #include "include/core/SkPixmap.h" |
| 16 | #include "include/core/SkRefCnt.h" |
| 17 | #include "include/core/SkSurface.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "include/core/SkTypes.h" |
Kevin Lubick | ecd3a2f | 2023-01-05 08:17:45 -0500 | [diff] [blame] | 19 | #include "include/gpu/GpuTypes.h" |
Kevin Lubick | 8c73a59 | 2022-10-17 15:25:35 -0400 | [diff] [blame] | 20 | #include "include/gpu/GrDirectContext.h" |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 21 | #include "include/gpu/ganesh/SkSurfaceGanesh.h" |
Kevin Lubick | 8c73a59 | 2022-10-17 15:25:35 -0400 | [diff] [blame] | 22 | #include "tests/CtsEnforcement.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "tests/Test.h" |
Kevin Lubick | 8b74188 | 2023-10-06 11:41:38 -0400 | [diff] [blame] | 24 | #include "tools/DecodeUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "tools/Resources.h" |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 26 | |
Kevin Lubick | 8c73a59 | 2022-10-17 15:25:35 -0400 | [diff] [blame] | 27 | #include <cstdint> |
| 28 | #include <initializer_list> |
| 29 | |
Kevin Lubick | 8c73a59 | 2022-10-17 15:25:35 -0400 | [diff] [blame] | 30 | struct GrContextOptions; |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 31 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 32 | static void check_isopaque(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface, |
| 33 | bool expectedOpaque) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 34 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 35 | REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque); |
| 36 | } |
| 37 | |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 38 | DEF_TEST(ImageIsOpaqueTest, reporter) { |
| 39 | SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5); |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 40 | auto surfaceTransparent(SkSurfaces::Raster(infoTransparent)); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 41 | check_isopaque(reporter, surfaceTransparent, false); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 42 | |
| 43 | SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType); |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 44 | auto surfaceOpaque(SkSurfaces::Raster(infoOpaque)); |
reed | b888136 | 2014-08-20 07:24:01 -0700 | [diff] [blame] | 45 | check_isopaque(reporter, surfaceOpaque, true); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Kevin Lubick | eed3b01 | 2022-09-21 11:33:19 -0400 | [diff] [blame] | 48 | DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu, |
| 49 | reporter, |
| 50 | ctxInfo, |
| 51 | CtsEnforcement::kApiLevel_T) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 52 | auto context = ctxInfo.directContext(); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 53 | SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5); |
Kevin Lubick | ecd3a2f | 2023-01-05 08:17:45 -0500 | [diff] [blame] | 54 | auto surfaceTransparent( |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 55 | SkSurfaces::RenderTarget(context, skgpu::Budgeted::kNo, infoTransparent)); |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 56 | check_isopaque(reporter, surfaceTransparent, false); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 57 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 58 | SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType); |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 59 | auto surfaceOpaque(SkSurfaces::RenderTarget(context, skgpu::Budgeted::kNo, infoOpaque)); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 60 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 61 | check_isopaque(reporter, surfaceOpaque, true); |
piotaixr | d2a3522 | 2014-08-19 14:29:02 -0700 | [diff] [blame] | 62 | } |
| 63 | |
reed | 9e2ed83 | 2016-10-31 05:27:28 -0700 | [diff] [blame] | 64 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 65 | #include "include/core/SkPictureRecorder.h" |
reed | 9e2ed83 | 2016-10-31 05:27:28 -0700 | [diff] [blame] | 66 | |
| 67 | static sk_sp<SkPicture> make_picture() { |
| 68 | SkPictureRecorder recorder; |
| 69 | SkCanvas* canvas = recorder.beginRecording({ 0, 0, 10, 10 }); |
| 70 | canvas->drawColor(SK_ColorRED); |
| 71 | return recorder.finishRecordingAsPicture(); |
| 72 | } |
| 73 | |
| 74 | DEF_TEST(Image_isAlphaOnly, reporter) { |
| 75 | SkPMColor pmColors = 0; |
| 76 | SkPixmap pmap = { |
| 77 | SkImageInfo::MakeN32Premul(1, 1), |
| 78 | &pmColors, |
| 79 | sizeof(pmColors) |
| 80 | }; |
| 81 | for (auto& image : { |
Kevin Lubick | 77472bf | 2023-03-24 07:11:17 -0400 | [diff] [blame] | 82 | SkImages::RasterFromPixmapCopy(pmap), |
Kevin Lubick | 8b74188 | 2023-10-06 11:41:38 -0400 | [diff] [blame] | 83 | ToolUtils::GetResourceAsImage("images/mandrill_128.png"), |
| 84 | ToolUtils::GetResourceAsImage("images/color_wheel.jpg"), |
Kevin Lubick | 77472bf | 2023-03-24 07:11:17 -0400 | [diff] [blame] | 85 | SkImages::DeferredFromPicture(make_picture(), |
| 86 | {10, 10}, |
| 87 | nullptr, |
| 88 | nullptr, |
| 89 | SkImages::BitDepth::kU8, |
| 90 | SkColorSpace::MakeSRGB()), |
| 91 | }) { |
reed | 9e2ed83 | 2016-10-31 05:27:28 -0700 | [diff] [blame] | 92 | REPORTER_ASSERT(reporter, image->isAlphaOnly() == false); |
| 93 | } |
| 94 | |
Kevin Lubick | 77472bf | 2023-03-24 07:11:17 -0400 | [diff] [blame] | 95 | REPORTER_ASSERT( |
| 96 | reporter, |
| 97 | SkImages::RasterFromPixmapCopy({SkImageInfo::MakeA8(1, 1), (uint8_t*)&pmColors, 1}) |
| 98 | ->isAlphaOnly() == true); |
reed | 9e2ed83 | 2016-10-31 05:27:28 -0700 | [diff] [blame] | 99 | } |