Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
VkProtectedContextTest.cpp
Go to the documentation of this file.
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
11
12#if defined(SK_VULKAN)
13
24#include "include/core/SkPath.h"
25#include "include/core/SkRect.h"
29#include "include/gpu/GrTypes.h"
31#include "tests/Test.h"
33
34#if defined(SK_GANESH)
37#endif
38
39#if defined(SK_GRAPHITE)
43#endif
44
45#include <memory>
46#include <utility>
47
48struct GrContextOptions;
49
50static const int kSize = 8;
51
52namespace {
53
54void create_nonprotected_context(skiatest::Reporter* reporter, skiatest::TestType testType) {
55 std::unique_ptr<VkTestHelper> helper = VkTestHelper::Make(testType, /* isProtected= */ false);
57}
58
59} // anonymous namespace
60
61DEF_GANESH_TEST(VkProtectedContext_CreateNonprotectedContext,
63 options,
65 create_nonprotected_context(reporter, skiatest::TestType::kGanesh);
66}
67
68#if defined(SK_GRAPHITE)
69
70DEF_GRAPHITE_TEST(VkProtectedContext_CreateNonprotectedContext_Graphite,
73 create_nonprotected_context(reporter, skiatest::TestType::kGraphite);
74}
75
76#endif
77
78DEF_GANESH_TEST(VkProtectedContext_CreateProtectedContext,
80 options,
82 std::unique_ptr<VkTestHelper> helper = VkTestHelper::Make(skiatest::TestType::kGanesh,
83 /* isProtected= */ true);
84}
85
86DEF_GRAPHITE_TEST(VkProtectedContext_CreateProtectedContext_Graphite,
89 std::unique_ptr<VkTestHelper> helper = VkTestHelper::Make(skiatest::TestType::kGraphite,
90 /* isProtected= */ true);
91}
92
93namespace {
94
95void create_protected_surface(skiatest::Reporter* reporter, skiatest::TestType testType) {
96
97 std::unique_ptr<VkTestHelper> helper = VkTestHelper::Make(testType, /* isProtected= */ true);
98 if (!helper) {
99 return;
100 }
101
102 REPORTER_ASSERT(reporter, helper->isValid());
103
104 sk_sp<SkSurface> textureable = helper->createSurface({ kSize, kSize },
105 /* textureable= */ true,
106 /* isProtected= */ true);
107 REPORTER_ASSERT(reporter, textureable);
108
109 sk_sp<SkSurface> untextureable = helper->createSurface({ kSize, kSize },
110 /* textureable= */ false,
111 /* isProtected= */ true);
112 REPORTER_ASSERT(reporter, untextureable);
113}
114
115} // anonymous namespace
116
117DEF_GANESH_TEST(VkProtectedContext_CreateProtectedSkSurface,
118 reporter,
119 options,
121 create_protected_surface(reporter, skiatest::TestType::kGanesh);
122}
123
124DEF_GRAPHITE_TEST(VkProtectedContext_CreateProtectedSkSurface_Graphite,
125 reporter,
127 create_protected_surface(reporter, skiatest::TestType::kGraphite);
128}
129
130namespace {
131
132void create_backend_texture_ganesh(skiatest::Reporter* reporter,
133 bool contextIsProtected,
134 bool beTexIsProtected) {
135
136 std::unique_ptr<VkTestHelper> helper = VkTestHelper::Make(skiatest::TestType::kGanesh,
137 contextIsProtected);
138 if (!helper) {
139 return;
140 }
141
142 REPORTER_ASSERT(reporter, helper->isValid());
143 GrDirectContext* dContext = helper->directContext();
144
145 GrBackendTexture backendTex = dContext->createBackendTexture(kSize,
146 kSize,
148 skgpu::Mipmapped::kNo,
149 GrRenderable::kNo,
150 GrProtected(beTexIsProtected));
151
152 REPORTER_ASSERT(reporter, backendTex.isValid() == (contextIsProtected == beTexIsProtected));
153 if (backendTex.isValid()) {
154 REPORTER_ASSERT(reporter, backendTex.isProtected() == beTexIsProtected);
155 }
156
157 dContext->deleteBackendTexture(backendTex);
158}
159
160} // anonymous namespace
161
162DEF_GANESH_TEST(VkProtectedContext_CreateBackendTextures,
163 reporter,
164 options,
166 for (bool contextIsProtected : { true, false }) {
167 for (bool beTexIsProtected : { true, false }) {
168 create_backend_texture_ganesh(reporter, contextIsProtected, beTexIsProtected);
169 }
170 }
171}
172
173#if defined(SK_GRAPHITE)
174
175namespace {
176
177void create_backend_texture_graphite(skiatest::Reporter* reporter,
178 bool contextIsProtected,
179 bool beTexIsProtected) {
180 using namespace skgpu::graphite;
181
182 std::unique_ptr<VkTestHelper> helper = VkTestHelper::Make(skiatest::TestType::kGraphite,
183 contextIsProtected);
184 if (!helper) {
185 return;
186 }
187
188 REPORTER_ASSERT(reporter, helper->isValid());
189
190 VulkanTextureInfo vkTextureInfo;
191 vkTextureInfo.fFlags = beTexIsProtected ? VK_IMAGE_CREATE_PROTECTED_BIT : 0;
192 vkTextureInfo.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
196 TextureInfo textureInfo(vkTextureInfo);
197
198 BackendTexture backendTex = helper->recorder()->createBackendTexture({ kSize, kSize },
199 textureInfo);
200 REPORTER_ASSERT(reporter, backendTex.isValid() == (contextIsProtected == beTexIsProtected));
201 if (backendTex.isValid()) {
203 backendTex.info().isProtected() == skgpu::Protected(beTexIsProtected));
204 }
205
206 helper->recorder()->deleteBackendTexture(backendTex);
207}
208
209} // anonymous namespace
210
211DEF_GRAPHITE_TEST(VkProtectedContext_CreateBackendTextures_Graphite,
212 reporter,
214 for (bool contextIsProtected : { true, false }) {
215 for (bool beTexIsProtected : { true, false }) {
216 create_backend_texture_graphite(reporter, contextIsProtected, beTexIsProtected);
217 }
218 }
219}
220
221#endif // SK_GRAPHITE
222
223DEF_GANESH_TEST(VkProtectedContext_ReadFromProtectedSurface,
224 reporter,
225 options,
227 std::unique_ptr<VkTestHelper> helper = VkTestHelper::Make(skiatest::TestType::kGanesh,
228 /* isProtected= */ true);
229 if (!helper) {
230 return;
231 }
232
233 REPORTER_ASSERT(reporter, helper->isValid());
234
235 sk_sp<SkSurface> surface = helper->createSurface({ kSize, kSize },
236 /* textureable= */ true,
237 /* isProtected= */ true);
239 REPORTER_ASSERT(reporter, !surface->readPixels(SkImageInfo(), nullptr, 8, 0, 0));
240}
241
242namespace {
243
244struct AsyncContext {
245 bool fCalled = false;
246 std::unique_ptr<const SkSurface::AsyncReadResult> fResult;
247};
248
249void async_callback(void* c, std::unique_ptr<const SkSurface::AsyncReadResult> result) {
250 auto context = static_cast<AsyncContext*>(c);
251 context->fResult = std::move(result);
252 context->fCalled = true;
253}
254
255void async_read_from_protected_surface(skiatest::Reporter* reporter,
256 skiatest::TestType testType) {
257 std::unique_ptr<VkTestHelper> helper = VkTestHelper::Make(testType, /* isProtected= */ true);
258 if (!helper) {
259 return;
260 }
261
262 REPORTER_ASSERT(reporter, helper->isValid());
263
264 sk_sp<SkSurface> surface = helper->createSurface({ kSize, kSize },
265 /* textureable= */ true,
266 /* isProtected= */ true);
268 AsyncContext cbContext;
269 const SkImageInfo imageInfo = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType,
271 surface->asyncRescaleAndReadPixelsYUV420(kIdentity_SkYUVColorSpace, SkColorSpace::MakeSRGB(),
272 imageInfo.bounds(), imageInfo.dimensions(),
273 SkSurface::RescaleGamma::kSrc,
274 SkSurface::RescaleMode::kNearest,
275 &async_callback, &cbContext);
276
277 helper->submitAndWaitForCompletion(&cbContext.fCalled);
278 REPORTER_ASSERT(reporter, !cbContext.fResult);
279}
280
281} // anonymous namespace
282
283DEF_GANESH_TEST(VkProtectedContext_AsyncReadFromProtectedSurface,
284 reporter,
285 options,
287 async_read_from_protected_surface(reporter, skiatest::TestType::kGanesh);
288}
289
290DEF_GRAPHITE_TEST(VkProtectedContext_AsyncReadFromProtectedSurface_Graphite,
291 reporter,
293 async_read_from_protected_surface(reporter, skiatest::TestType::kGraphite);
294}
295
296namespace {
297
298void draw_rectangle(skiatest::Reporter* reporter, skiatest::TestType testType) {
299 std::unique_ptr<VkTestHelper> helper = VkTestHelper::Make(testType, /* isProtected= */ true);
300 if (!helper) {
301 return;
302 }
303
304 REPORTER_ASSERT(reporter, helper->isValid());
305
306 sk_sp<SkSurface> surface = helper->createSurface({ kSize, kSize },
307 /* textureable= */ true,
308 /* isProtected= */ true);
310 SkCanvas* canvas = surface->getCanvas();
311 REPORTER_ASSERT(reporter, canvas);
313 paint.setColor(SK_ColorBLACK);
314 canvas->drawRect(SkRect::MakeWH(4, 4), paint);
315}
316
317} // anonymous namespace
318
319DEF_GANESH_TEST(VkProtectedContext_DrawRectangle,
320 reporter,
321 options,
323 draw_rectangle(reporter, skiatest::TestType::kGanesh);
324}
325
326DEF_GRAPHITE_TEST(VkProtectedContext_DrawRectangle_Graphite,
327 reporter,
330}
331
332namespace {
333
334void draw_rectangle_with_aa(skiatest::Reporter* reporter, skiatest::TestType testType) {
335 std::unique_ptr<VkTestHelper> helper = VkTestHelper::Make(testType, /* isProtected= */ true);
336 if (!helper) {
337 return;
338 }
339
340 REPORTER_ASSERT(reporter, helper->isValid());
341
342 sk_sp<SkSurface> surface = helper->createSurface({ kSize, kSize },
343 /* textureable= */ true,
344 /* isProtected= */ true);
346 SkCanvas* canvas = surface->getCanvas();
347 REPORTER_ASSERT(reporter, canvas);
349 paint.setColor(SK_ColorBLACK);
350 paint.setAntiAlias(true);
351 canvas->drawRect(SkRect::MakeWH(4, 4), paint);
352}
353
354} // anonymous namespace
355
356DEF_GANESH_TEST(VkProtectedContext_DrawRectangleWithAntiAlias,
357 reporter,
358 options,
360 draw_rectangle_with_aa(reporter, skiatest::TestType::kGanesh);
361}
362
363DEF_GRAPHITE_TEST(VkProtectedContext_DrawRectangleWithAntiAlias_Graphite,
364 reporter,
366 draw_rectangle_with_aa(reporter, skiatest::TestType::kGraphite);
367}
368
369namespace {
370
371void draw_rectangle_with_blendmode(skiatest::Reporter* reporter, skiatest::TestType testType) {
372 std::unique_ptr<VkTestHelper> helper = VkTestHelper::Make(testType, /* isProtected= */ true);
373 if (!helper) {
374 return;
375 }
376
377 REPORTER_ASSERT(reporter, helper->isValid());
378
379 sk_sp<SkSurface> surface = helper->createSurface({ kSize, kSize },
380 /* textureable= */ true,
381 /* isProtected= */ true);
383 SkCanvas* canvas = surface->getCanvas();
384 REPORTER_ASSERT(reporter, canvas);
386 paint.setColor(SK_ColorBLACK);
387 paint.setBlendMode(SkBlendMode::kColorDodge);
388 canvas->drawRect(SkRect::MakeWH(4, 4), paint);
389}
390
391} // anonymous namespace
392
393DEF_GANESH_TEST(VkProtectedContext_DrawRectangleWithBlendMode,
394 reporter,
395 options,
397 draw_rectangle_with_blendmode(reporter, skiatest::TestType::kGanesh);
398}
399
400DEF_GRAPHITE_TEST(VkProtectedContext_DrawRectangleWithBlendMode_Graphite,
401 reporter,
403 draw_rectangle_with_blendmode(reporter, skiatest::TestType::kGraphite);
404}
405
406namespace {
407
408void draw_rectangle_with_filter(skiatest::Reporter* reporter, skiatest::TestType testType) {
409 std::unique_ptr<VkTestHelper> helper = VkTestHelper::Make(testType, /* isProtected= */ true);
410 if (!helper) {
411 return;
412 }
413
414 REPORTER_ASSERT(reporter, helper->isValid());
415
416 sk_sp<SkSurface> surface = helper->createSurface({ kSize, kSize },
417 /* textureable= */ true,
418 /* isProtected= */ true);
420 SkCanvas* canvas = surface->getCanvas();
421 REPORTER_ASSERT(reporter, canvas);
423 paint.setColor(SK_ColorBLACK);
424 paint.setStyle(SkPaint::kFill_Style);
426 canvas->drawRect(SkRect::MakeWH(4, 4), paint);
427}
428
429} // anonymous namespace
430
431DEF_GANESH_TEST(VkProtectedContext_DrawRectangleWithFilter,
432 reporter,
433 options,
435 draw_rectangle_with_filter(reporter, skiatest::TestType::kGanesh);
436}
437
438DEF_GRAPHITE_TEST(VkProtectedContext_DrawRectangleWithFilter_Graphite,
439 reporter,
441 draw_rectangle_with_filter(reporter, skiatest::TestType::kGraphite);
442}
443
444namespace {
445
446void draw_thin_path(skiatest::Reporter* reporter, skiatest::TestType testType) {
447 constexpr bool kIsProtected = true;
448
449 std::unique_ptr<VkTestHelper> helper = VkTestHelper::Make(testType, kIsProtected);
450 if (!helper) {
451 return;
452 }
453
454 REPORTER_ASSERT(reporter, helper->isValid());
455
456 sk_sp<SkSurface> surface = helper->createSurface({ kSize, kSize },
457 /* textureable= */ true,
458 kIsProtected);
460 SkCanvas* canvas = surface->getCanvas();
461 REPORTER_ASSERT(reporter, canvas);
463 paint.setColor(SK_ColorBLACK);
465 paint.setAntiAlias(true);
466 paint.setStrokeWidth(.4f);
467 canvas->drawPath(SkPath().moveTo(4, 4).lineTo(6, 6), paint);
468}
469
470} // anonymous namespace
471
472DEF_GANESH_TEST(VkProtectedContext_DrawThinPath,
473 reporter,
474 options,
476 draw_thin_path(reporter, skiatest::TestType::kGanesh);
477}
478
479DEF_GRAPHITE_TEST(VkProtectedContext_DrawThinPath_Graphite,
480 reporter,
483}
484
485namespace {
486
487void save_layer(skiatest::Reporter* reporter, skiatest::TestType testType) {
488 std::unique_ptr<VkTestHelper> helper = VkTestHelper::Make(testType, /* isProtected= */ true);
489 if (!helper) {
490 return;
491 }
492
493 REPORTER_ASSERT(reporter, helper->isValid());
494
495 sk_sp<SkSurface> surface = helper->createSurface({ kSize, kSize },
496 /* textureable= */ true,
497 /* isProtected= */ true);
499 SkCanvas* canvas = surface->getCanvas();
500 REPORTER_ASSERT(reporter, canvas);
501 canvas->saveLayer(nullptr, nullptr);
503 paint.setColor(SK_ColorBLACK);
504 canvas->drawRect(SkRect::MakeWH(4, 4), paint);
505 canvas->restore();
506}
507
508} // anonymous namespace
509
510DEF_GANESH_TEST(VkProtectedContext_SaveLayer,
511 reporter,
512 options,
515}
516
517DEF_GRAPHITE_TEST(VkProtectedContext_SaveLayer_Graphite,
518 reporter,
521}
522
523namespace {
524
525void draw_protected_image_on_protected_surface(skiatest::Reporter* reporter,
526 skiatest::TestType testType) {
527 constexpr bool kIsProtected = true;
528
529 std::unique_ptr<VkTestHelper> helper = VkTestHelper::Make(testType, kIsProtected);
530 if (!helper) {
531 return;
532 }
533
534 REPORTER_ASSERT(reporter, helper->isValid());
535
536 // Create protected image.
537 sk_sp<SkSurface> surface1 = helper->createSurface({ kSize, kSize },
538 /* textureable= */ true,
539 kIsProtected);
540 REPORTER_ASSERT(reporter, surface1);
541 sk_sp<SkImage> image = surface1->makeImageSnapshot();
543 REPORTER_ASSERT(reporter, image->isProtected() == kIsProtected);
544
545 // Create protected canvas.
546 sk_sp<SkSurface> surface2 = helper->createSurface({ kSize, kSize },
547 /* textureable= */ true,
548 kIsProtected);
549 REPORTER_ASSERT(reporter, surface2);
550 SkCanvas* canvas = surface2->getCanvas();
551 REPORTER_ASSERT(reporter, canvas);
552
553 canvas->drawImage(image, 0, 0);
554}
555
556} // anonymous namespace
557
558DEF_GANESH_TEST(VkProtectedContext_DrawProtectedImageOnProtectedSurface,
559 reporter,
560 options,
562 draw_protected_image_on_protected_surface(reporter, skiatest::TestType::kGanesh);
563}
564
565DEF_GRAPHITE_TEST(VkProtectedContext_DrawProtectedImageOnProtectedSurface_Graphite,
566 reporter,
568 draw_protected_image_on_protected_surface(reporter, skiatest::TestType::kGraphite);
569}
570
571#endif // SK_VULKAN
const char * options
reporter
skgpu::Protected GrProtected
Definition GrTypes.h:139
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
@ kColorDodge
brighten destination to reflect source
@ kOuter_SkBlurStyle
nothing inside, fuzzy outside
Definition SkBlurTypes.h:14
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
@ kIdentity_SkYUVColorSpace
maps Y->R, U->G, V->B
Definition SkImageInfo.h:93
#define DEF_GANESH_TEST(name, reporter, options, ctsEnforcement)
Definition Test.h:393
#define DEF_GRAPHITE_TEST(name, reporter, ctsEnforcement)
Definition Test.h:327
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
void deleteBackendTexture(const GrBackendTexture &)
GrBackendTexture createBackendTexture(int width, int height, const GrBackendFormat &, skgpu::Mipmapped, GrRenderable, GrProtected=GrProtected::kNo, std::string_view label={})
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition SkCanvas.cpp:500
void drawRect(const SkRect &rect, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void drawPath(const SkPath &path, const SkPaint &paint)
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
static sk_sp< SkColorSpace > MakeSRGB()
bool isProtected() const
Definition SkImage.cpp:294
static sk_sp< SkMaskFilter > MakeBlur(SkBlurStyle style, SkScalar sigma, bool respectCTM=true)
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
const TextureInfo & info() const
Protected isProtected() const
Definition TextureInfo.h:80
static constexpr int kSize
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
sk_sp< SkImage > image
Definition examples.cpp:29
GAsyncResult * result
Protected
Definition GpuTypes.h:61
SkIRect bounds() const
SkISize dimensions() const
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
@ VK_IMAGE_CREATE_PROTECTED_BIT
@ VK_IMAGE_USAGE_TRANSFER_DST_BIT
@ VK_IMAGE_USAGE_SAMPLED_BIT
@ VK_IMAGE_USAGE_TRANSFER_SRC_BIT
@ VK_FORMAT_R8G8B8A8_UNORM