Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
PinnedImageTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2017 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// This is a GPU-backend specific test.
27#include "src/gpu/ganesh/GrFragmentProcessor.h" // IWYU pragma: keep
28#include "src/gpu/ganesh/SkGr.h"
31#include "tests/Test.h"
33#include "tools/gpu/FenceSync.h"
34
35#include <string>
36
38struct GrContextOptions;
39
40using namespace sk_gpu_test;
41
43 SkBitmap bm;
44 bm.allocPixels(ii);
45
46 surf->readPixels(bm, 0, 0);
47
48 for (int y = 0; y < bm.height(); ++y) {
49 for (int x = 0; x < bm.width(); ++x) {
50 if (bm.getColor(x, y) != color) {
51 return false;
52 }
53 }
54 }
55
56 return true;
57}
58
60 skiatest::ReporterContext subtest(reporter, "basic_test");
61 const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType);
62
63 SkBitmap bm;
64 bm.allocPixels(ii);
65
66 SkCanvas bmCanvas(bm);
67 bmCanvas.clear(SK_ColorRED);
68
69 // We start off with the raster image being all red.
71 REPORTER_ASSERT(reporter, img, "PinnableImageFromBitmap returned null");
72
74 SkCanvas* canvas = gpuSurface->getCanvas();
75
76 // w/o pinning - the gpu draw always reflects the current state of the underlying bitmap
77 {
78 canvas->drawImage(img, 0, 0);
80
81 bmCanvas.clear(SK_ColorGREEN);
82
83 canvas->drawImage(img, 0, 0);
85 }
86
87 // w/ pinning - the gpu draw is stuck at the pinned state
88 {
89 bool ok = skgpu::ganesh::PinAsTexture(rContext, img.get()); // pin at blue
90 REPORTER_ASSERT(reporter, ok, "PinAsTexture did not succeed");
91
92 canvas->drawImage(img, 0, 0);
94
95 bmCanvas.clear(SK_ColorBLUE);
96
97 canvas->drawImage(img, 0, 0);
99
100 skgpu::ganesh::UnpinTexture(rContext, img.get());
101 }
102
103 // once unpinned local changes will be picked up
104 {
105 canvas->drawImage(img, 0, 0);
107 }
108}
109
110// Deleting the context while there are still pinned images shouldn't result in a crash.
112 skiatest::ReporterContext subtest(reporter, "cleanup_test");
113 const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType);
114
115 SkBitmap bm;
116 bm.allocPixels(ii);
117
118 SkCanvas bmCanvas(bm);
119 bmCanvas.clear(SK_ColorRED);
120
123
124 for (int i = 0; i < skgpu::kContextTypeCount; ++i) {
125 auto ctxType = static_cast<skgpu::ContextType>(i);
126
127 {
128 sk_sp<SkImage> img;
129 GrDirectContext* dContext = nullptr;
130
131 {
132 GrContextFactory testFactory;
133 ContextInfo info = testFactory.getContextInfo(ctxType);
134 dContext = info.directContext();
135 if (!dContext) {
136 continue;
137 }
138
140 if (!skgpu::ganesh::PinAsTexture(dContext, img.get())) {
141 continue;
142 }
143 // Pinning on a second context should be blocked.
145 img.get()));
146 }
147
148 // The context used to pin the image is gone at this point!
149 // "context" isn't technically used in this call but it can't be null!
150 // We don't really want to support this use case but it currently happens.
151 skgpu::ganesh::UnpinTexture(dContext, img.get());
152 }
153 }
154}
155
157 reporter,
158 ctxInfo,
160
161 basic_test(reporter, ctxInfo.directContext());
163}
164
165DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(PinnedImageTest_AsGaneshView,
166 reporter,
167 ctxInfo,
169 GrRecordingContext* rContext = ctxInfo.directContext();
170 const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType);
171
172 SkBitmap bm;
173 bm.allocPixels(ii);
174
175 SkCanvas bmCanvas(bm);
176 bmCanvas.clear(SK_ColorMAGENTA); // arbitrary color
177
179 REPORTER_ASSERT(reporter, img, "PinnableImageFromBitmap returned null");
180
181 {
182 skiatest::ReporterContext subtest(reporter, "cached path");
183 auto [view, colortype] = skgpu::ganesh::AsView(rContext, img, skgpu::Mipmapped::kNo,
185 REPORTER_ASSERT(reporter, view, "AsView returned falsey view");
186 }
187
188 {
189 skiatest::ReporterContext subtest(reporter, "unncached path");
190 auto [view, colortype] = skgpu::ganesh::AsView(rContext, img, skgpu::Mipmapped::kNo,
192 REPORTER_ASSERT(reporter, view, "AsView returned falsey view");
193 }
194}
195
196DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(PinnedImageTest_AsFragmentProcessor,
197 reporter,
198 ctxInfo,
200 GrRecordingContext* rContext = ctxInfo.directContext();
201 const SkImageInfo ii = SkImageInfo::Make(64, 64, kN32_SkColorType, kPremul_SkAlphaType);
202
203 SkBitmap bm;
204 bm.allocPixels(ii);
205
206 SkCanvas bmCanvas(bm);
207 bmCanvas.clear(SK_ColorMAGENTA); // arbitrary color
208
210 REPORTER_ASSERT(reporter, img, "PinnableImageFromBitmap returned null");
211
213
215 rContext, img.get(), SkSamplingOptions({1/3, 1/3}), tm,
216 SkMatrix::I(), nullptr, nullptr);
217 REPORTER_ASSERT(reporter, fp, "AsFragmentProcessor returned falsey processor");
218}
const char * options
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
reporter
SkColor4f color
static void basic_test(skiatest::Reporter *reporter, GrRecordingContext *rContext)
static void cleanup_test(skiatest::Reporter *reporter)
static bool surface_is_expected_color(SkSurface *surf, const SkImageInfo &ii, SkColor color)
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
constexpr SkColor SK_ColorMAGENTA
Definition SkColor.h:147
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
static bool ok(int result)
SkTileMode
Definition SkTileMode.h:13
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
#define DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(name, reporter, context_info, ctsEnforcement)
Definition Test.h:434
static sk_sp< GrDirectContext > MakeMock(const GrMockOptions *, const GrContextOptions &)
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition SkBitmap.cpp:258
SkColor getColor(int x, int y) const
Definition SkBitmap.h:874
int width() const
Definition SkBitmap.h:149
int height() const
Definition SkBitmap.h:158
void clear(SkColor color)
Definition SkCanvas.h:1199
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
static const SkMatrix & I()
bool readPixels(const SkPixmap &dst, int srcX, int srcY)
ContextInfo getContextInfo(ContextType type, ContextOverrides=ContextOverrides::kNone)
T * get() const
Definition SkRefCnt.h:303
double y
double x
SK_API sk_sp< SkImage > PinnableRasterFromBitmap(const SkBitmap &)
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
std::unique_ptr< GrFragmentProcessor > AsFragmentProcessor(GrRecordingContext *rContext, const SkImage *img, SkSamplingOptions sampling, const SkTileMode tileModes[2], const SkMatrix &m, const SkRect *subset, const SkRect *domain)
bool PinAsTexture(GrRecordingContext *, SkImage *)
std::tuple< GrSurfaceProxyView, GrColorType > AsView(GrRecordingContext *rContext, const SkImage *img, skgpu::Mipmapped mipmapped, GrImageTexGenPolicy policy)
void UnpinTexture(GrRecordingContext *, SkImage *)
static const int kContextTypeCount
Definition ContextType.h:42
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)