blob: 69d5eded542a326f1b0cb5ccb5a3e3f5a7225e6c [file] [log] [blame]
piotaixrd2a35222014-08-19 14:29:02 -07001/*
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 Lubick8c73a592022-10-17 15:25:35 -04008#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 Lubickf437d012023-06-26 14:35:06 -040014#include "include/core/SkPicture.h" // IWYU pragma: keep
Kevin Lubick8c73a592022-10-17 15:25:35 -040015#include "include/core/SkPixmap.h"
16#include "include/core/SkRefCnt.h"
17#include "include/core/SkSurface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/core/SkTypes.h"
Kevin Lubickecd3a2f2023-01-05 08:17:45 -050019#include "include/gpu/GpuTypes.h"
Kevin Lubick8c73a592022-10-17 15:25:35 -040020#include "include/gpu/GrDirectContext.h"
Kevin Lubick5c93acf2023-05-09 12:11:43 -040021#include "include/gpu/ganesh/SkSurfaceGanesh.h"
Kevin Lubick8c73a592022-10-17 15:25:35 -040022#include "tests/CtsEnforcement.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tests/Test.h"
Kevin Lubick8b741882023-10-06 11:41:38 -040024#include "tools/DecodeUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "tools/Resources.h"
reed7c748852014-11-10 08:57:21 -080026
Kevin Lubick8c73a592022-10-17 15:25:35 -040027#include <cstdint>
28#include <initializer_list>
29
Kevin Lubick8c73a592022-10-17 15:25:35 -040030struct GrContextOptions;
piotaixrd2a35222014-08-19 14:29:02 -070031
reede8f30622016-03-23 18:59:25 -070032static void check_isopaque(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
33 bool expectedOpaque) {
reed9ce9d672016-03-17 10:51:11 -070034 sk_sp<SkImage> image(surface->makeImageSnapshot());
reedb8881362014-08-20 07:24:01 -070035 REPORTER_ASSERT(reporter, image->isOpaque() == expectedOpaque);
36}
37
piotaixrd2a35222014-08-19 14:29:02 -070038DEF_TEST(ImageIsOpaqueTest, reporter) {
39 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
Kevin Lubick5c93acf2023-05-09 12:11:43 -040040 auto surfaceTransparent(SkSurfaces::Raster(infoTransparent));
reedb8881362014-08-20 07:24:01 -070041 check_isopaque(reporter, surfaceTransparent, false);
piotaixrd2a35222014-08-19 14:29:02 -070042
43 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
Kevin Lubick5c93acf2023-05-09 12:11:43 -040044 auto surfaceOpaque(SkSurfaces::Raster(infoOpaque));
reedb8881362014-08-20 07:24:01 -070045 check_isopaque(reporter, surfaceOpaque, true);
piotaixrd2a35222014-08-19 14:29:02 -070046}
47
Kevin Lubickeed3b012022-09-21 11:33:19 -040048DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(ImageIsOpaqueTest_Gpu,
49 reporter,
50 ctxInfo,
51 CtsEnforcement::kApiLevel_T) {
Robert Phillips6d344c32020-07-06 10:56:46 -040052 auto context = ctxInfo.directContext();
kkinnunen15302832015-12-01 04:35:26 -080053 SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
Kevin Lubickecd3a2f2023-01-05 08:17:45 -050054 auto surfaceTransparent(
Kevin Lubick5c93acf2023-05-09 12:11:43 -040055 SkSurfaces::RenderTarget(context, skgpu::Budgeted::kNo, infoTransparent));
kkinnunen15302832015-12-01 04:35:26 -080056 check_isopaque(reporter, surfaceTransparent, false);
piotaixrd2a35222014-08-19 14:29:02 -070057
kkinnunen15302832015-12-01 04:35:26 -080058 SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
Kevin Lubick5c93acf2023-05-09 12:11:43 -040059 auto surfaceOpaque(SkSurfaces::RenderTarget(context, skgpu::Budgeted::kNo, infoOpaque));
piotaixrd2a35222014-08-19 14:29:02 -070060
kkinnunen15302832015-12-01 04:35:26 -080061 check_isopaque(reporter, surfaceOpaque, true);
piotaixrd2a35222014-08-19 14:29:02 -070062}
63
reed9e2ed832016-10-31 05:27:28 -070064///////////////////////////////////////////////////////////////////////////////////////////////////
Mike Kleinc0bd9f92019-04-23 12:05:21 -050065#include "include/core/SkPictureRecorder.h"
reed9e2ed832016-10-31 05:27:28 -070066
67static 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
74DEF_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 Lubick77472bf2023-03-24 07:11:17 -040082 SkImages::RasterFromPixmapCopy(pmap),
Kevin Lubick8b741882023-10-06 11:41:38 -040083 ToolUtils::GetResourceAsImage("images/mandrill_128.png"),
84 ToolUtils::GetResourceAsImage("images/color_wheel.jpg"),
Kevin Lubick77472bf2023-03-24 07:11:17 -040085 SkImages::DeferredFromPicture(make_picture(),
86 {10, 10},
87 nullptr,
88 nullptr,
89 SkImages::BitDepth::kU8,
90 SkColorSpace::MakeSRGB()),
91 }) {
reed9e2ed832016-10-31 05:27:28 -070092 REPORTER_ASSERT(reporter, image->isAlphaOnly() == false);
93 }
94
Kevin Lubick77472bf2023-03-24 07:11:17 -040095 REPORTER_ASSERT(
96 reporter,
97 SkImages::RasterFromPixmapCopy({SkImageInfo::MakeA8(1, 1), (uint8_t*)&pmColors, 1})
98 ->isAlphaOnly() == true);
reed9e2ed832016-10-31 05:27:28 -070099}