Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
ProtectedTest.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
8#include "tests/Test.h"
9
10#if defined(SK_GANESH)
11
20
21static const int kSize = 8;
22
24 auto dContext = ctxInfo.directContext();
25
26 if (!dContext->supportsProtectedContent()) {
27 // Protected content not supported
28 return;
29 }
30
31 for (bool textureable : { true, false }) {
32 for (bool isProtected : { true, false }) {
33 if (!isProtected && GrBackendApi::kVulkan == dContext->backend()) {
34 continue;
35 }
36
38 { kSize, kSize },
39 textureable,
40 isProtected);
41 if (!surface) {
42 continue;
43 }
44
45 sk_sp<SkImage> image = surface->makeImageSnapshot();
46 if (!image) {
47 ERRORF(reporter, "Could not makeImageSnapshot from a %s surface.",
48 isProtected ? "protected" : "unprotected");
49 continue;
50 }
51
52 dContext->submit(GrSyncCpu::kYes);
53
54 REPORTER_ASSERT(reporter, image->isProtected() == isProtected);
56 }
57 }
58
59 for (bool isProtected : { true, false }) {
60 if (!isProtected && GrBackendApi::kVulkan == dContext->backend()) {
61 continue;
62 }
63
65 { kSize, kSize },
67 isProtected);
68 if (!image) {
69 continue;
70 }
71
72 dContext->submit(GrSyncCpu::kYes);
73
74 REPORTER_ASSERT(reporter, image->isProtected() == isProtected);
76 }
77
78 for (bool renderable : { true, false }) {
79 for (bool isProtected : { true, false }) {
80 GrBackendTexture beTex = dContext->createBackendTexture(16,
81 16,
84 skgpu::Mipmapped::kNo,
85 GrRenderable(renderable),
86 GrProtected(isProtected));
87
89 REPORTER_ASSERT(reporter, beTex.isProtected() == isProtected);
90
91 dContext->flushAndSubmit(GrSyncCpu::kYes);
92
93 {
94 sk_sp<SkImage> img = SkImages::BorrowTextureFrom(dContext, beTex,
98 /* colorSpace= */ nullptr);
99
100 REPORTER_ASSERT(reporter, img->isProtected() == isProtected);
101 }
102
103 if (beTex.isValid()) {
104 dContext->deleteBackendTexture(beTex);
105 }
106 }
107 }
108}
109
110// Verify that readPixels fails on protected surfaces
111DEF_GANESH_TEST_FOR_ALL_CONTEXTS(Protected_readPixelsFromSurfaces, reporter, ctxInfo,
113 auto dContext = ctxInfo.directContext();
114
115 if (!dContext->supportsProtectedContent()) {
116 // Protected content not supported
117 return;
118 }
119
120 for (bool isProtected : { true, false }) {
121 if (!isProtected && GrBackendApi::kVulkan == dContext->backend()) {
122 continue;
123 }
124
126 { kSize, kSize },
127 /* textureable= */ true,
128 isProtected);
129 if (!surface) {
130 continue;
131 }
132
134 readback.allocPixels(surface->imageInfo());
135 REPORTER_ASSERT(reporter, isProtected != surface->readPixels(readback, 0, 0));
136 }
137}
138
139namespace {
140
141struct AsyncContext {
142 bool fCalled = false;
143 std::unique_ptr<const SkSurface::AsyncReadResult> fResult;
144};
145
146static void async_callback(void* c, std::unique_ptr<const SkSurface::AsyncReadResult> result) {
147 auto context = static_cast<AsyncContext*>(c);
148 context->fResult = std::move(result);
149 context->fCalled = true;
150}
151
152} // anonymous namespace
153
154// Verify that asyncRescaleAndReadPixels fails on protected surfaces
155DEF_GANESH_TEST_FOR_ALL_CONTEXTS(Protected_asyncRescaleAndReadPixelsFromSurfaces, reporter, ctxInfo,
157 auto dContext = ctxInfo.directContext();
158
159 if (!dContext->supportsProtectedContent()) {
160 // Protected content not supported
161 return;
162 }
163
164 for (bool isProtected : { true, false }) {
165 if (!isProtected && GrBackendApi::kVulkan == dContext->backend()) {
166 continue;
167 }
168
170 { kSize, kSize },
171 /* textureable= */ true,
172 isProtected);
173 if (!surface) {
174 continue;
175 }
176
177 AsyncContext cbContext;
178
180 SkIRect::MakeWH(surface->width(), surface->height()),
181 SkSurface::RescaleGamma::kSrc,
182 SkSurface::RescaleMode::kNearest,
183 async_callback, &cbContext);
184 dContext->submit();
185 while (!cbContext.fCalled) {
186 dContext->checkAsyncWorkCompletion();
187 }
188 REPORTER_ASSERT(reporter, isProtected != SkToBool(cbContext.fResult));
189 }
190}
191
192#endif // defined(SK_GANESH)
static void readback(const SkBitmap &src, int *result, int resultCount)
Definition BlurTest.cpp:264
reporter
skgpu::Protected GrProtected
Definition GrTypes.h:139
skgpu::Renderable GrRenderable
Definition GrTypes.h:134
@ kTopLeft_GrSurfaceOrigin
Definition GrTypes.h:148
static void async_callback(void *c, std::unique_ptr< const SkImage::AsyncReadResult > result)
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
static constexpr bool SkToBool(const T &x)
Definition SkTo.h:35
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
#define ERRORF(r,...)
Definition Test.h:293
#define DEF_GANESH_TEST_FOR_ALL_CONTEXTS(name, reporter, context_info, ctsEnforcement)
Definition Test.h:431
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition SkBitmap.cpp:258
bool isProtected() const
Definition SkImage.cpp:294
void asyncRescaleAndReadPixels(const SkImageInfo &info, const SkIRect &srcRect, RescaleGamma rescaleGamma, RescaleMode rescaleMode, ReadPixelsCallback callback, ReadPixelsContext context)
T * get() const
Definition SkRefCnt.h:303
static constexpr int kSize
VkSurfaceKHR surface
Definition main.cc:49
sk_sp< SkImage > image
Definition examples.cpp:29
GAsyncResult * result
sk_sp< SkSurface > CreateProtectedSkSurface(GrDirectContext *dContext, SkISize size, bool textureable, bool isProtected, const SkSurfaceProps *surfaceProps)
sk_sp< SkImage > CreateProtectedSkImage(GrDirectContext *dContext, SkISize size, SkColor4f color, bool isProtected)
void CheckImageBEProtection(SkImage *image, bool expectingProtected)
constexpr SkColor4f kTransparent
Definition SkColor.h:434
constexpr SkColor4f kBlue
Definition SkColor.h:442
SK_API sk_sp< SkImage > BorrowTextureFrom(GrRecordingContext *context, const GrBackendTexture &backendTexture, GrSurfaceOrigin origin, SkColorType colorType, SkAlphaType alphaType, sk_sp< SkColorSpace > colorSpace, TextureReleaseProc textureReleaseProc=nullptr, ReleaseContext releaseContext=nullptr)
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition SkRect.h:56