Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
CacheKeyTest.cpp
Go to the documentation of this file.
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
17#include "tests/Test.h"
18#include "tools/GpuToolUtils.h"
19#include "tools/ToolUtils.h"
20
21using namespace skgpu::graphite;
22
23namespace {
24
25sk_sp<SkPicture> create_picture(int width, int height) {
27
28 SkPictureRecorder recorder;
29 SkCanvas* canvas = recorder.beginRecording(bounds);
30
32 paint.setColor(SK_ColorMAGENTA);
33
34 canvas->drawRect(bounds, paint);
35
36 return recorder.finishRecordingAsPicture();
37}
38
39
42
43 SkBitmap bm;
44 bm.allocPixels(ii);
45
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.
56DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(CacheKeyTest_Picture, reporter, context,
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);
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) } ) {
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.
102DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(CacheKeyTest_Bitmap, reporter, context,
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 }) {
117
118 sk_sp<SkImage> result1 = provider->findOrCreate(recorder.get(),
119 image1.get(),
120 {mipmapped});
121
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}
const char * options
reporter
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkASSERT(cond)
Definition SkAssert.h:116
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
constexpr SkColor SK_ColorMAGENTA
Definition SkColor.h:147
SkPixelGeometry
@ kUnknown_SkPixelGeometry
@ kRGB_H_SkPixelGeometry
#define DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(name, reporter, graphite_ctx, ctsEnforcement)
Definition Test.h:373
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
Type::kYUV Type::kRGBA() int(0.7 *637)
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition SkBitmap.cpp:258
void setImmutable()
Definition SkBitmap.cpp:400
bool extractSubset(SkBitmap *dst, const SkIRect &subset) const
Definition SkBitmap.cpp:453
uint32_t getGenerationID() const
Definition SkBitmap.cpp:361
void eraseColor(SkColor4f) const
Definition SkBitmap.cpp:442
void drawRect(const SkRect &rect, const SkPaint &paint)
static sk_sp< SkColorSpace > MakeSRGB()
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition SkMatrix.h:91
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkPicture > finishRecordingAsPicture()
T * get() const
Definition SkRefCnt.h:303
const Paint & paint
FlutterSemanticsFlag flags
static SkBitmap create_bitmap(SkIRect contentRect, SkISize fullSize, GrSurfaceOrigin origin)
SK_API sk_sp< SkImage > DeferredFromPicture(sk_sp< SkPicture > picture, const SkISize &dimensions, const SkMatrix *matrix, const SkPaint *paint, BitDepth bitDepth, sk_sp< SkColorSpace > colorSpace, SkSurfaceProps props)
SK_API sk_sp< SkImage > RasterFromBitmap(const SkBitmap &bitmap)
@ kF16
uses 16-bit float per color component
@ kU8
uses 8-bit unsigned int per color component
Optional< SkRect > bounds
Definition SkRecords.h:189
int32_t height
int32_t width
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition SkRect.h:104
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609