Flutter Engine
The Flutter Engine
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,
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
121 { kSize, kSize },
122 /* textureable= */ true,
123 /* isProtected= */ true);
124 if (!surface) {
125 return;
126 }
127
129 readback.allocPixels(surface->imageInfo());
130 REPORTER_ASSERT(reporter, !surface->readPixels(readback, 0, 0));
131}
132
133namespace {
134
135struct AsyncContext {
136 bool fCalled = false;
137 std::unique_ptr<const SkSurface::AsyncReadResult> fResult;
138};
139
140static void async_callback(void* c, std::unique_ptr<const SkSurface::AsyncReadResult> result) {
141 auto context = static_cast<AsyncContext*>(c);
142 context->fResult = std::move(result);
143 context->fCalled = true;
144}
145
146} // anonymous namespace
147
148// Verify that asyncRescaleAndReadPixels fails on protected surfaces
149DEF_GANESH_TEST_FOR_ALL_CONTEXTS(Protected_asyncRescaleAndReadPixelsFromSurfaces, reporter, ctxInfo,
151 auto dContext = ctxInfo.directContext();
152
153 if (!dContext->supportsProtectedContent()) {
154 // Protected content not supported
155 return;
156 }
157
159 { kSize, kSize },
160 /* textureable= */ true,
161 /* isProtected= */ true);
162 if (!surface) {
163 return;
164 }
165
166 AsyncContext cbContext;
167
168 surface->asyncRescaleAndReadPixels(surface->imageInfo(),
169 SkIRect::MakeWH(surface->width(), surface->height()),
172 async_callback, &cbContext);
173 dContext->submit();
174 while (!cbContext.fCalled) {
175 dContext->checkAsyncWorkCompletion();
176 }
177 REPORTER_ASSERT(reporter, !cbContext.fResult);
178}
179
180#endif // defined(SK_GANESH)
static void readback(const SkBitmap &src, int *result, int resultCount)
Definition: BlurTest.cpp:264
reporter
Definition: FontMgrTest.cpp:39
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
LoopControlFlowInfo fResult
#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
bool isValid() const
bool isProtected() const
bool isProtected() const
Definition: SkImage.cpp:294
T * get() const
Definition: SkRefCnt.h:303
VkSurfaceKHR surface
Definition: main.cc:49
GAsyncResult * result
constexpr int kSize
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)
sk_sp< const SkImage > image
Definition: SkRecords.h:269
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56