1 /*
2 * Copyright 2023 Google LLC
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/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPicture.h"
13 #include "include/core/SkPictureRecorder.h"
14 #include "include/gpu/graphite/Context.h"
15 #include "include/gpu/graphite/ImageProvider.h"
16 #include "include/gpu/graphite/Recorder.h"
17 #include "tests/Test.h"
18 #include "tools/ToolUtils.h"
19 #include "tools/graphite/GraphiteToolUtils.h"
20
21 using namespace skgpu::graphite;
22
23 namespace {
24
create_picture(int width,int height)25 sk_sp<SkPicture> create_picture(int width, int height) {
26 const SkRect bounds = SkRect::MakeWH(width, height);
27
28 SkPictureRecorder recorder;
29 SkCanvas* canvas = recorder.beginRecording(bounds);
30
31 SkPaint paint;
32 paint.setColor(SK_ColorMAGENTA);
33
34 canvas->drawRect(bounds, paint);
35
36 return recorder.finishRecordingAsPicture();
37 }
38
39
create_bitmap(int width,int height)40 SkBitmap create_bitmap(int width, int height) {
41 SkImageInfo ii = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
42
43 SkBitmap bm;
44 bm.allocPixels(ii);
45
46 bm.eraseColor(SK_ColorMAGENTA);
47
48 bm.setImmutable();
49 return bm;
50 }
51
52 } // anonymous namespace
53
54 // In this test we just iterate through the cases we expect to work and verify that rewrapping the
55 // base SkPicture doesn't block finding the earlier cached image.
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(CacheKeyTest_Picture,reporter,context,CtsEnforcement::kApiLevel_V)56 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(CacheKeyTest_Picture, reporter, context,
57 CtsEnforcement::kApiLevel_V) {
58 std::unique_ptr<Recorder> recorder = context->makeRecorder();
59
60 RecorderOptions options = ToolUtils::CreateTestingRecorderOptions();
61
62 sk_sp<ImageProvider> provider = options.fImageProvider;
63
64 sk_sp<SkPicture> picture = create_picture(128, 128);
65 const SkMatrix xlate = SkMatrix::Translate(10.0f, -10.0f);
66 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
67
68 for (auto bitDepth : { SkImages::BitDepth::kU8, SkImages::BitDepth::kF16 }) {
69 for (const SkMatrix* mat : { static_cast<const SkMatrix*>(nullptr), &xlate }) {
70 for (bool mipmapped : { false, true }) {
71 for (uint32_t flags : { 0, int(SkSurfaceProps::kAlwaysDither_Flag) } ) {
72 for (SkPixelGeometry geometry : { kUnknown_SkPixelGeometry,
73 kRGB_H_SkPixelGeometry } ) {
74 sk_sp<SkImage> image1 = SkImages::DeferredFromPicture(picture, {128, 128},
75 mat, nullptr,
76 bitDepth, srgb,
77 { flags, geometry });
78
79 sk_sp<SkImage> result1 = provider->findOrCreate(recorder.get(),
80 image1.get(),
81 {mipmapped});
82
83 sk_sp<SkImage> image2 = SkImages::DeferredFromPicture(picture, {128, 128},
84 mat, nullptr,
85 bitDepth, srgb,
86 { flags, geometry });
87 REPORTER_ASSERT(reporter, image1->uniqueID() != image2->uniqueID());
88
89 sk_sp<SkImage> result2 = provider->findOrCreate(recorder.get(),
90 image2.get(),
91 {mipmapped});
92 REPORTER_ASSERT(reporter, result1 == result2);
93 }
94 }
95 }
96 }
97 }
98 }
99
100 // In this test we just iterate through the cases we expect to work and verify that rewrapping the
101 // subsetted SkBitmap doesn't block finding the earlier cached image.
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(CacheKeyTest_Bitmap,reporter,context,CtsEnforcement::kApiLevel_V)102 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(CacheKeyTest_Bitmap, reporter, context,
103 CtsEnforcement::kApiLevel_V) {
104 std::unique_ptr<Recorder> recorder = context->makeRecorder();
105
106 RecorderOptions options = ToolUtils::CreateTestingRecorderOptions();
107
108 sk_sp<ImageProvider> provider = options.fImageProvider;
109
110 SkBitmap orig = create_bitmap(128, 128);
111 SkBitmap subset;
112 orig.extractSubset(&subset, SkIRect::MakeXYWH(32, 32, 64, 64));
113 SkASSERT(orig.getGenerationID() == subset.getGenerationID());
114
115 for (bool mipmapped : { false, true }) {
116 sk_sp<SkImage> image1 = SkImages::RasterFromBitmap(subset);
117
118 sk_sp<SkImage> result1 = provider->findOrCreate(recorder.get(),
119 image1.get(),
120 {mipmapped});
121
122 sk_sp<SkImage> image2 = SkImages::RasterFromBitmap(subset);
123 REPORTER_ASSERT(reporter, image1->uniqueID() != image2->uniqueID());
124
125 sk_sp<SkImage> result2 = provider->findOrCreate(recorder.get(),
126 image2.get(),
127 {mipmapped});
128 REPORTER_ASSERT(reporter, result1 == result2);
129 }
130 }
131