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/SkPixmap.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkSurface.h"
17 #include "include/core/SkTypes.h"
18 #include "include/gpu/GpuTypes.h"
19 #include "include/gpu/GrDirectContext.h"
20 #include "tests/CtsEnforcement.h"
21 #include "tests/Test.h"
22 #include "tools/Resources.h"
23
24 #include <cstdint>
25 #include <initializer_list>
26
27 class SkPicture;
28 struct GrContextOptions;
29
check_isopaque(skiatest::Reporter * reporter,const sk_sp<SkSurface> & surface,bool expectedOpaque)30 static void check_isopaque(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
31 bool expectedOpaque) {
32 sk_sp<SkImage> image(surface->makeImageSnapshot());
33 REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque);
34 }
35
DEF_TEST(ImageIsOpaqueTest,reporter)36 DEF_TEST(ImageIsOpaqueTest, reporter) {
37 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
38 auto surfaceTransparent(SkSurface::MakeRaster(infoTransparent));
39 check_isopaque(reporter, surfaceTransparent, false);
40
41 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
42 auto surfaceOpaque(SkSurface::MakeRaster(infoOpaque));
43 check_isopaque(reporter, surfaceOpaque, true);
44 }
45
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)46 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu,
47 reporter,
48 ctxInfo,
49 CtsEnforcement::kApiLevel_T) {
50 auto context = ctxInfo.directContext();
51 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
52 auto surfaceTransparent(
53 SkSurface::MakeRenderTarget(context, skgpu::Budgeted::kNo, infoTransparent));
54 check_isopaque(reporter, surfaceTransparent, false);
55
56 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
57 auto surfaceOpaque(SkSurface::MakeRenderTarget(context, skgpu::Budgeted::kNo, infoOpaque));
58
59 check_isopaque(reporter, surfaceOpaque, true);
60 }
61
62 ///////////////////////////////////////////////////////////////////////////////////////////////////
63 #include "include/core/SkPictureRecorder.h"
64
make_picture()65 static sk_sp<SkPicture> make_picture() {
66 SkPictureRecorder recorder;
67 SkCanvas* canvas = recorder.beginRecording({ 0, 0, 10, 10 });
68 canvas->drawColor(SK_ColorRED);
69 return recorder.finishRecordingAsPicture();
70 }
71
DEF_TEST(Image_isAlphaOnly,reporter)72 DEF_TEST(Image_isAlphaOnly, reporter) {
73 SkPMColor pmColors = 0;
74 SkPixmap pmap = {
75 SkImageInfo::MakeN32Premul(1, 1),
76 &pmColors,
77 sizeof(pmColors)
78 };
79 for (auto& image : {
80 SkImage::MakeRasterCopy(pmap),
81 GetResourceAsImage("images/mandrill_128.png"),
82 GetResourceAsImage("images/color_wheel.jpg"),
83 SkImage::MakeFromPicture(make_picture(), { 10, 10 }, nullptr, nullptr,
84 SkImage::BitDepth::kU8,
85 SkColorSpace::MakeSRGB()),
86 })
87 {
88 REPORTER_ASSERT(reporter, image->isAlphaOnly() == false);
89 }
90
91 REPORTER_ASSERT(reporter, SkImage::MakeRasterCopy({
92 SkImageInfo::MakeA8(1, 1), (uint8_t*)&pmColors, 1})->isAlphaOnly() == true);
93 }
94