xref: /aosp_15_r20/external/skia/tests/ImageIsOpaqueTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "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"
14 #include "include/core/SkPicture.h"  // IWYU pragma: keep
15 #include "include/core/SkPixmap.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkSurface.h"
18 #include "include/core/SkTypes.h"
19 #include "include/gpu/GpuTypes.h"
20 #include "include/gpu/ganesh/GrDirectContext.h"
21 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
22 #include "tests/CtsEnforcement.h"
23 #include "tests/Test.h"
24 #include "tools/DecodeUtils.h"
25 #include "tools/Resources.h"
26 
27 #include <cstdint>
28 #include <initializer_list>
29 
30 struct GrContextOptions;
31 
check_isopaque(skiatest::Reporter * reporter,const sk_sp<SkSurface> & surface,bool expectedOpaque)32 static void check_isopaque(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
33                            bool expectedOpaque) {
34     sk_sp<SkImage> image(surface->makeImageSnapshot());
35     REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque);
36 }
37 
DEF_TEST(ImageIsOpaqueTest,reporter)38 DEF_TEST(ImageIsOpaqueTest, reporter) {
39     SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
40     auto surfaceTransparent(SkSurfaces::Raster(infoTransparent));
41     check_isopaque(reporter, surfaceTransparent, false);
42 
43     SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
44     auto surfaceOpaque(SkSurfaces::Raster(infoOpaque));
45     check_isopaque(reporter, surfaceOpaque, true);
46 }
47 
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)48 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu,
49                                        reporter,
50                                        ctxInfo,
51                                        CtsEnforcement::kApiLevel_T) {
52     auto context = ctxInfo.directContext();
53     SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
54     auto surfaceTransparent(
55             SkSurfaces::RenderTarget(context, skgpu::Budgeted::kNo, infoTransparent));
56     check_isopaque(reporter, surfaceTransparent, false);
57 
58     SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
59     auto surfaceOpaque(SkSurfaces::RenderTarget(context, skgpu::Budgeted::kNo, infoOpaque));
60 
61     check_isopaque(reporter, surfaceOpaque, true);
62 }
63 
64 ///////////////////////////////////////////////////////////////////////////////////////////////////
65 #include "include/core/SkPictureRecorder.h"
66 
make_picture()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 
DEF_TEST(Image_isAlphaOnly,reporter)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 : {
82                  SkImages::RasterFromPixmapCopy(pmap),
83                  ToolUtils::GetResourceAsImage("images/mandrill_128.png"),
84                  ToolUtils::GetResourceAsImage("images/color_wheel.jpg"),
85                  SkImages::DeferredFromPicture(make_picture(),
86                                                {10, 10},
87                                                nullptr,
88                                                nullptr,
89                                                SkImages::BitDepth::kU8,
90                                                SkColorSpace::MakeSRGB()),
91          }) {
92         REPORTER_ASSERT(reporter, image->isAlphaOnly() == false);
93     }
94 
95     REPORTER_ASSERT(
96             reporter,
97             SkImages::RasterFromPixmapCopy({SkImageInfo::MakeA8(1, 1), (uint8_t*)&pmColors, 1})
98                             ->isAlphaOnly() == true);
99 }
100