• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 Vulkan protected memory specific test.
9 
10 #include "include/core/SkTypes.h"
11 
12 #if defined(SK_GANESH) && defined(SK_VULKAN)
13 
14 #include "include/core/SkAlphaType.h"
15 #include "include/core/SkBlendMode.h"
16 #include "include/core/SkBlurTypes.h"
17 #include "include/core/SkCanvas.h"
18 #include "include/core/SkColor.h"
19 #include "include/core/SkColorSpace.h"
20 #include "include/core/SkColorType.h"
21 #include "include/core/SkImageInfo.h"
22 #include "include/core/SkMaskFilter.h"
23 #include "include/core/SkPaint.h"
24 #include "include/core/SkPath.h"
25 #include "include/core/SkRect.h"
26 #include "include/core/SkRefCnt.h"
27 #include "include/core/SkSurface.h"
28 #include "include/core/SkSurfaceProps.h"
29 #include "include/core/SkTypes.h"
30 #include "include/gpu/GpuTypes.h"
31 #include "include/gpu/GrBackendSurface.h"
32 #include "include/gpu/GrDirectContext.h"
33 #include "include/gpu/GrTypes.h"
34 #include "tests/CtsEnforcement.h"
35 #include "tests/Test.h"
36 #include "tools/gpu/BackendSurfaceFactory.h"
37 #include "tools/gpu/vk/VkTestHelper.h"
38 
39 #include <memory>
40 #include <utility>
41 
42 struct GrContextOptions;
43 
create_protected_sksurface(GrDirectContext * dContext,skiatest::Reporter * reporter,bool textureable=true)44 static sk_sp<SkSurface> create_protected_sksurface(GrDirectContext* dContext,
45                                                    skiatest::Reporter* reporter,
46                                                    bool textureable = true) {
47     const int kW = 8;
48     const int kH = 8;
49     SkSurfaceProps surfaceProps = SkSurfaceProps(0, kRGB_H_SkPixelGeometry);
50     sk_sp<SkSurface> surface;
51     if (textureable) {
52         surface = sk_gpu_test::MakeBackendTextureSurface(dContext,
53                                                          {kW, kH},
54                                                          kTopLeft_GrSurfaceOrigin,
55                                                          1,
56                                                          kRGBA_8888_SkColorType,
57                                                          /* color space */ nullptr,
58                                                          GrMipmapped::kNo,
59                                                          GrProtected::kYes,
60                                                          &surfaceProps);
61     } else {
62         surface = sk_gpu_test::MakeBackendRenderTargetSurface(dContext,
63                                                               {kW, kH},
64                                                               kTopLeft_GrSurfaceOrigin,
65                                                               1,
66                                                               kRGBA_8888_SkColorType,
67                                                               /* color space */ nullptr,
68                                                               GrProtected::kYes,
69                                                               &surfaceProps);
70     }
71     if (!surface) {
72         ERRORF(reporter, "Could not create protected surface.");
73         return nullptr;
74     }
75     if (textureable) {
76         GrBackendTexture backendTex =
77                 surface->getBackendTexture(SkSurface::kFlushRead_BackendHandleAccess);
78         REPORTER_ASSERT(reporter, backendTex.isValid());
79         REPORTER_ASSERT(reporter, backendTex.isProtected());
80     } else {
81         GrBackendRenderTarget backendRT =
82                 surface->getBackendRenderTarget(SkSurface::kFlushRead_BackendHandleAccess);
83         REPORTER_ASSERT(reporter, backendRT.isValid());
84         REPORTER_ASSERT(reporter, backendRT.isProtected());
85     }
86     return surface;
87 }
88 
DEF_GANESH_TEST(VkProtectedContext_CreateNonprotectedContext,reporter,options,CtsEnforcement::kNever)89 DEF_GANESH_TEST(VkProtectedContext_CreateNonprotectedContext,
90                 reporter,
91                 options,
92                 CtsEnforcement::kNever) {
93     auto nonprotectedTestHelper = std::make_unique<VkTestHelper>(false);
94     REPORTER_ASSERT(reporter, nonprotectedTestHelper->init());
95 }
96 
DEF_GANESH_TEST(VkProtectedContext_CreateProtectedContext,reporter,options,CtsEnforcement::kNever)97 DEF_GANESH_TEST(VkProtectedContext_CreateProtectedContext,
98                 reporter,
99                 options,
100                 CtsEnforcement::kNever) {
101     auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
102     if (!protectedTestHelper->init()) {
103         return;
104     }
105 }
106 
DEF_GANESH_TEST(VkProtectedContext_CreateProtectedSkSurface,reporter,options,CtsEnforcement::kNever)107 DEF_GANESH_TEST(VkProtectedContext_CreateProtectedSkSurface,
108                 reporter,
109                 options,
110                 CtsEnforcement::kNever) {
111     auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
112     if (!protectedTestHelper->init()) {
113         return;
114     }
115 
116     auto dContext = protectedTestHelper->directContext();
117     REPORTER_ASSERT(reporter, dContext != nullptr);
118     create_protected_sksurface(dContext, reporter, /*textureable*/ true);
119     create_protected_sksurface(dContext, reporter, /*textureable*/ false);
120 }
121 
DEF_GANESH_TEST(VkProtectedContext_CreateNonprotectedTextureInProtectedContext,reporter,options,CtsEnforcement::kNever)122 DEF_GANESH_TEST(VkProtectedContext_CreateNonprotectedTextureInProtectedContext,
123                 reporter,
124                 options,
125                 CtsEnforcement::kNever) {
126     auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
127     if (!protectedTestHelper->init()) {
128         return;
129     }
130     REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
131 
132     const int kW = 8;
133     const int kH = 8;
134     GrBackendTexture backendTex =
135         protectedTestHelper->directContext()->createBackendTexture(
136             kW, kH, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo,
137             GrProtected::kNo);
138     REPORTER_ASSERT(reporter, !backendTex.isValid());
139 }
140 
DEF_GANESH_TEST(VkProtectedContext_CreateProtectedTextureInNonprotectedContext,reporter,options,CtsEnforcement::kNever)141 DEF_GANESH_TEST(VkProtectedContext_CreateProtectedTextureInNonprotectedContext,
142                 reporter,
143                 options,
144                 CtsEnforcement::kNever) {
145     auto protectedTestHelper = std::make_unique<VkTestHelper>(false);
146     if (!protectedTestHelper->init()) {
147         return;
148     }
149     REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
150 
151     const int kW = 8;
152     const int kH = 8;
153     GrBackendTexture backendTex =
154             protectedTestHelper->directContext()->createBackendTexture(kW,
155                                                                        kH,
156                                                                        kRGBA_8888_SkColorType,
157                                                                        GrMipmapped::kNo,
158                                                                        GrRenderable::kNo,
159                                                                        GrProtected::kYes);
160     REPORTER_ASSERT(reporter, !backendTex.isValid());
161 }
162 
DEF_GANESH_TEST(VkProtectedContext_ReadFromProtectedSurface,reporter,options,CtsEnforcement::kNever)163 DEF_GANESH_TEST(VkProtectedContext_ReadFromProtectedSurface,
164                 reporter,
165                 options,
166                 CtsEnforcement::kNever) {
167     auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
168     if (!protectedTestHelper->init()) {
169         return;
170     }
171     REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
172 
173     auto surface = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
174     REPORTER_ASSERT(reporter, surface);
175     REPORTER_ASSERT(reporter, !surface->readPixels(SkImageInfo(), nullptr, 8, 0, 0));
176 }
177 
178 namespace {
179 
180 struct AsyncContext {
181     bool fCalled = false;
182     std::unique_ptr<const SkSurface::AsyncReadResult> fResult;
183 };
184 
async_callback(void * c,std::unique_ptr<const SkSurface::AsyncReadResult> result)185 static void async_callback(void* c, std::unique_ptr<const SkSurface::AsyncReadResult> result) {
186     auto context = static_cast<AsyncContext*>(c);
187     context->fResult = std::move(result);
188     context->fCalled = true;
189 }
190 
191 }  // anonymous namespace
192 
DEF_GANESH_TEST(VkProtectedContext_AsyncReadFromProtectedSurface,reporter,options,CtsEnforcement::kNever)193 DEF_GANESH_TEST(VkProtectedContext_AsyncReadFromProtectedSurface,
194                 reporter,
195                 options,
196                 CtsEnforcement::kNever) {
197     auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
198     if (!protectedTestHelper->init()) {
199         return;
200     }
201 
202     auto dContext = protectedTestHelper->directContext();
203 
204     REPORTER_ASSERT(reporter, dContext != nullptr);
205 
206     auto surface = create_protected_sksurface(dContext, reporter);
207     REPORTER_ASSERT(reporter, surface);
208     AsyncContext cbContext;
209     const auto image_info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
210                                       SkColorSpace::MakeSRGB());
211     surface->asyncRescaleAndReadPixelsYUV420(kIdentity_SkYUVColorSpace, SkColorSpace::MakeSRGB(),
212                                              image_info.bounds(), image_info.dimensions(),
213                                              SkSurface::RescaleGamma::kSrc,
214                                              SkSurface::RescaleMode::kNearest,
215                                              &async_callback, &cbContext);
216     dContext->submit();
217     while (!cbContext.fCalled) {
218         dContext->checkAsyncWorkCompletion();
219     }
220     REPORTER_ASSERT(reporter, !cbContext.fResult);
221 }
222 
DEF_GANESH_TEST(VkProtectedContext_DrawRectangle,reporter,options,CtsEnforcement::kNever)223 DEF_GANESH_TEST(VkProtectedContext_DrawRectangle, reporter, options, CtsEnforcement::kNever) {
224     auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
225     if (!protectedTestHelper->init()) {
226         return;
227     }
228     REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
229 
230     auto surface = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
231     REPORTER_ASSERT(reporter, surface);
232     SkCanvas* canvas = surface->getCanvas();
233     REPORTER_ASSERT(reporter, canvas);
234     SkPaint paint;
235     paint.setColor(SK_ColorBLACK);
236     canvas->drawRect(SkRect::MakeWH(4, 4), paint);
237 }
238 
DEF_GANESH_TEST(VkProtectedContext_DrawRectangleWithAntiAlias,reporter,options,CtsEnforcement::kNever)239 DEF_GANESH_TEST(VkProtectedContext_DrawRectangleWithAntiAlias,
240                 reporter,
241                 options,
242                 CtsEnforcement::kNever) {
243     auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
244     if (!protectedTestHelper->init()) {
245         return;
246     }
247     REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
248 
249     auto surface = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
250     REPORTER_ASSERT(reporter, surface);
251     SkCanvas* canvas = surface->getCanvas();
252     REPORTER_ASSERT(reporter, canvas);
253     SkPaint paint;
254     paint.setColor(SK_ColorBLACK);
255     paint.setAntiAlias(true);
256     canvas->drawRect(SkRect::MakeWH(4, 4), paint);
257 }
258 
DEF_GANESH_TEST(VkProtectedContext_DrawRectangleWithBlendMode,reporter,options,CtsEnforcement::kNever)259 DEF_GANESH_TEST(VkProtectedContext_DrawRectangleWithBlendMode,
260                 reporter,
261                 options,
262                 CtsEnforcement::kNever) {
263     auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
264     if (!protectedTestHelper->init()) {
265         return;
266     }
267     REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
268 
269     auto surface = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
270     REPORTER_ASSERT(reporter, surface);
271     SkCanvas* canvas = surface->getCanvas();
272     REPORTER_ASSERT(reporter, canvas);
273     SkPaint paint;
274     paint.setColor(SK_ColorBLACK);
275     paint.setBlendMode(SkBlendMode::kColorDodge);
276     canvas->drawRect(SkRect::MakeWH(4, 4), paint);
277 }
278 
DEF_GANESH_TEST(VkProtectedContext_DrawRectangleWithFilter,reporter,options,CtsEnforcement::kNever)279 DEF_GANESH_TEST(VkProtectedContext_DrawRectangleWithFilter,
280                 reporter,
281                 options,
282                 CtsEnforcement::kNever) {
283     auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
284     if (!protectedTestHelper->init()) {
285         return;
286     }
287     REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
288 
289     auto surface = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
290     REPORTER_ASSERT(reporter, surface);
291     SkCanvas* canvas = surface->getCanvas();
292     REPORTER_ASSERT(reporter, canvas);
293     SkPaint paint;
294     paint.setColor(SK_ColorBLACK);
295     paint.setStyle(SkPaint::kFill_Style);
296     paint.setMaskFilter(SkMaskFilter::MakeBlur(
297           SkBlurStyle::kOuter_SkBlurStyle, 1.1f));
298     canvas->drawRect(SkRect::MakeWH(4, 4), paint);
299 }
300 
DEF_GANESH_TEST(VkProtectedContext_DrawThinPath,reporter,options,CtsEnforcement::kNever)301 DEF_GANESH_TEST(VkProtectedContext_DrawThinPath, reporter, options, CtsEnforcement::kNever) {
302     auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
303     if (!protectedTestHelper->init()) {
304         return;
305     }
306     REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
307 
308     auto surface = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
309     REPORTER_ASSERT(reporter, surface);
310     SkCanvas* canvas = surface->getCanvas();
311     REPORTER_ASSERT(reporter, canvas);
312     SkPaint paint;
313     paint.setColor(SK_ColorBLACK);
314     paint.setStyle(SkPaint::kStroke_Style);
315     paint.setAntiAlias(true);
316     paint.setStrokeWidth(.4f);
317     canvas->drawPath(SkPath().moveTo(4, 4).lineTo(6, 6), paint);
318 }
319 
DEF_GANESH_TEST(VkProtectedContext_SaveLayer,reporter,options,CtsEnforcement::kNever)320 DEF_GANESH_TEST(VkProtectedContext_SaveLayer, reporter, options, CtsEnforcement::kNever) {
321     auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
322     if (!protectedTestHelper->init()) {
323         return;
324     }
325     REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
326 
327     auto surface = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
328     REPORTER_ASSERT(reporter, surface);
329     SkCanvas* canvas = surface->getCanvas();
330     REPORTER_ASSERT(reporter, canvas);
331     canvas->saveLayer(nullptr, nullptr);
332     SkPaint paint;
333     paint.setColor(SK_ColorBLACK);
334     canvas->drawRect(SkRect::MakeWH(4, 4), paint);
335     canvas->restore();
336 }
337 
DEF_GANESH_TEST(VkProtectedContext_DrawProtectedImageOnProtectedSurface,reporter,options,CtsEnforcement::kNever)338 DEF_GANESH_TEST(VkProtectedContext_DrawProtectedImageOnProtectedSurface,
339                 reporter,
340                 options,
341                 CtsEnforcement::kNever) {
342     auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
343     if (!protectedTestHelper->init()) {
344         return;
345     }
346     REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
347 
348     // Create protected image.
349     auto surface1 = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
350     REPORTER_ASSERT(reporter, surface1);
351     auto image = surface1->makeImageSnapshot();
352     REPORTER_ASSERT(reporter, image);
353 
354     // Create protected canvas.
355     auto surface2 = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
356     REPORTER_ASSERT(reporter, surface2);
357     SkCanvas* canvas = surface2->getCanvas();
358     REPORTER_ASSERT(reporter, canvas);
359 
360     canvas->drawImage(image, 0, 0);
361 }
362 
363 #endif  // defined(SK_GANESH) && defined(SK_VULKAN)
364