blob: f9c0acc4228093e6296e26a593a4376bf7fae2a3 [file] [log] [blame]
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001/*
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 SK_SUPPORT_GPU && defined(SK_VULKAN)
13
14#include "include/core/SkCanvas.h"
15#include "include/core/SkMaskFilter.h"
16#include "include/core/SkPaint.h"
17#include "include/core/SkSurface.h"
18#include "include/gpu/GrBackendSurface.h"
19#include "include/gpu/vk/GrVkBackendContext.h"
20#include "include/gpu/vk/GrVkExtensions.h"
21#include "tests/Test.h"
Brian Salomon60003f72020-10-08 16:05:35 -040022#include "tools/gpu/BackendSurfaceFactory.h"
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040023#include "tools/gpu/GrContextFactory.h"
Robert Phillips0d6ce7c2020-06-11 08:51:50 -040024#include "tools/gpu/vk/VkTestHelper.h"
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040025
Robert Phillipsc8ae4942020-07-20 10:56:01 -040026static sk_sp<SkSurface> create_protected_sksurface(GrDirectContext* dContext,
Brian Salomon60003f72020-10-08 16:05:35 -040027 skiatest::Reporter* reporter,
28 bool textureable = true) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040029 const int kW = 8;
30 const int kH = 8;
Ben Wagnerae4bb982020-09-24 14:49:00 -040031 SkSurfaceProps surfaceProps = SkSurfaceProps(0, kRGB_H_SkPixelGeometry);
Brian Salomon60003f72020-10-08 16:05:35 -040032 sk_sp<SkSurface> surface;
33 if (textureable) {
34 surface = sk_gpu_test::MakeBackendTextureSurface(dContext,
35 {kW, kH},
36 kTopLeft_GrSurfaceOrigin,
37 1,
38 kRGBA_8888_SkColorType,
39 /* color space */ nullptr,
40 GrMipmapped::kNo,
41 GrProtected::kYes,
42 &surfaceProps);
43 } else {
44 surface = sk_gpu_test::MakeBackendRenderTargetSurface(dContext,
45 {kW, kH},
46 kTopLeft_GrSurfaceOrigin,
47 1,
48 kRGBA_8888_SkColorType,
49 /* color space */ nullptr,
50 GrProtected::kYes,
51 &surfaceProps);
52 }
53 if (!surface) {
54 ERRORF(reporter, "Could not create protected surface.");
55 return nullptr;
56 }
57 if (textureable) {
58 GrBackendTexture backendTex =
59 surface->getBackendTexture(SkSurface::kFlushRead_BackendHandleAccess);
60 REPORTER_ASSERT(reporter, backendTex.isValid());
61 REPORTER_ASSERT(reporter, backendTex.isProtected());
62 } else {
63 GrBackendRenderTarget backendRT =
64 surface->getBackendRenderTarget(SkSurface::kFlushRead_BackendHandleAccess);
65 REPORTER_ASSERT(reporter, backendRT.isValid());
66 REPORTER_ASSERT(reporter, backendRT.isProtected());
67 }
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040068 return surface;
69}
70
71DEF_GPUTEST(VkProtectedContext_CreateNonprotectedContext, reporter, options) {
Robert Phillips0d6ce7c2020-06-11 08:51:50 -040072 auto nonprotectedTestHelper = std::make_unique<VkTestHelper>(false);
73 REPORTER_ASSERT(reporter, nonprotectedTestHelper->init());
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040074}
75
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040076DEF_GPUTEST(VkProtectedContext_CreateProtectedContext, reporter, options) {
Robert Phillips0d6ce7c2020-06-11 08:51:50 -040077 auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
78 if (!protectedTestHelper->init()) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040079 return;
80 }
81}
82
83DEF_GPUTEST(VkProtectedContext_CreateProtectedSkSurface, reporter, options) {
Robert Phillips0d6ce7c2020-06-11 08:51:50 -040084 auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
85 if (!protectedTestHelper->init()) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040086 return;
87 }
Robert Phillipsc8ae4942020-07-20 10:56:01 -040088
89 auto dContext = protectedTestHelper->directContext();
90 REPORTER_ASSERT(reporter, dContext != nullptr);
Brian Salomon60003f72020-10-08 16:05:35 -040091 create_protected_sksurface(dContext, reporter, /*textureable*/ true);
92 create_protected_sksurface(dContext, reporter, /*textureable*/ false);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040093}
94
95DEF_GPUTEST(VkProtectedContext_CreateNonprotectedTextureInProtectedContext, reporter, options) {
Robert Phillips0d6ce7c2020-06-11 08:51:50 -040096 auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
97 if (!protectedTestHelper->init()) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040098 return;
99 }
Robert Phillips2a4acf32020-07-06 15:50:15 -0400100 REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400101
102 const int kW = 8;
103 const int kH = 8;
104 GrBackendTexture backendTex =
Robert Phillips2a4acf32020-07-06 15:50:15 -0400105 protectedTestHelper->directContext()->createBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400106 kW, kH, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400107 GrProtected::kNo);
108 REPORTER_ASSERT(reporter, !backendTex.isValid());
109}
110
111DEF_GPUTEST(VkProtectedContext_CreateProtectedTextureInNonprotectedContext, reporter, options) {
Robert Phillips0d6ce7c2020-06-11 08:51:50 -0400112 auto protectedTestHelper = std::make_unique<VkTestHelper>(false);
113 if (!protectedTestHelper->init()) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400114 return;
115 }
Robert Phillips2a4acf32020-07-06 15:50:15 -0400116 REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400117
118 const int kW = 8;
119 const int kH = 8;
120 GrBackendTexture backendTex =
Robert Phillips2a4acf32020-07-06 15:50:15 -0400121 protectedTestHelper->directContext()->createBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -0400122 kW, kH, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400123 GrProtected::kYes);
124 REPORTER_ASSERT(reporter, !backendTex.isValid());
125}
126
127DEF_GPUTEST(VkProtectedContext_ReadFromProtectedSurface, reporter, options) {
Robert Phillips0d6ce7c2020-06-11 08:51:50 -0400128 auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
129 if (!protectedTestHelper->init()) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400130 return;
131 }
Robert Phillips2a4acf32020-07-06 15:50:15 -0400132 REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400133
Robert Phillips2a4acf32020-07-06 15:50:15 -0400134 auto surface = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400135 REPORTER_ASSERT(reporter, surface);
Robert Phillips0d6ce7c2020-06-11 08:51:50 -0400136 REPORTER_ASSERT(reporter, !surface->readPixels(SkImageInfo(), nullptr, 8, 0, 0));
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400137}
138
Emircan Uysaler76974762019-10-25 13:28:54 -0400139namespace {
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
154DEF_GPUTEST(VkProtectedContext_AsyncReadFromProtectedSurface, reporter, options) {
Robert Phillips0d6ce7c2020-06-11 08:51:50 -0400155 auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
156 if (!protectedTestHelper->init()) {
Emircan Uysaler76974762019-10-25 13:28:54 -0400157 return;
158 }
Emircan Uysaler76974762019-10-25 13:28:54 -0400159
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400160 auto dContext = protectedTestHelper->directContext();
Robert Phillipsb27b38b2020-07-10 16:23:47 -0400161
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400162 REPORTER_ASSERT(reporter, dContext != nullptr);
Robert Phillipsb27b38b2020-07-10 16:23:47 -0400163
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400164 auto surface = create_protected_sksurface(dContext, reporter);
Emircan Uysaler76974762019-10-25 13:28:54 -0400165 REPORTER_ASSERT(reporter, surface);
166 AsyncContext cbContext;
167 const auto image_info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
168 SkColorSpace::MakeSRGB());
169 surface->asyncRescaleAndReadPixelsYUV420(kIdentity_SkYUVColorSpace, SkColorSpace::MakeSRGB(),
170 image_info.bounds(), image_info.dimensions(),
Mike Reed1efa14d2021-01-02 21:44:59 -0500171 SkSurface::RescaleGamma::kSrc,
172 SkSurface::RescaleMode::kNearest,
Emircan Uysaler76974762019-10-25 13:28:54 -0400173 &async_callback, &cbContext);
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400174 dContext->submit();
Emircan Uysaler76974762019-10-25 13:28:54 -0400175 while (!cbContext.fCalled) {
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400176 dContext->checkAsyncWorkCompletion();
Emircan Uysaler76974762019-10-25 13:28:54 -0400177 }
178 REPORTER_ASSERT(reporter, !cbContext.fResult);
Emircan Uysaler76974762019-10-25 13:28:54 -0400179}
180
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400181DEF_GPUTEST(VkProtectedContext_DrawRectangle, reporter, options) {
Robert Phillips0d6ce7c2020-06-11 08:51:50 -0400182 auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
183 if (!protectedTestHelper->init()) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400184 return;
185 }
Robert Phillips2a4acf32020-07-06 15:50:15 -0400186 REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400187
Robert Phillips2a4acf32020-07-06 15:50:15 -0400188 auto surface = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400189 REPORTER_ASSERT(reporter, surface);
190 SkCanvas* canvas = surface->getCanvas();
191 REPORTER_ASSERT(reporter, canvas);
192 SkPaint paint;
193 paint.setColor(SK_ColorBLACK);
194 canvas->drawRect(SkRect::MakeWH(4, 4), paint);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400195}
196
197DEF_GPUTEST(VkProtectedContext_DrawRectangleWithAntiAlias, reporter, options) {
Robert Phillips0d6ce7c2020-06-11 08:51:50 -0400198 auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
199 if (!protectedTestHelper->init()) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400200 return;
201 }
Robert Phillips2a4acf32020-07-06 15:50:15 -0400202 REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400203
Robert Phillips2a4acf32020-07-06 15:50:15 -0400204 auto surface = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400205 REPORTER_ASSERT(reporter, surface);
206 SkCanvas* canvas = surface->getCanvas();
207 REPORTER_ASSERT(reporter, canvas);
208 SkPaint paint;
209 paint.setColor(SK_ColorBLACK);
210 paint.setAntiAlias(true);
211 canvas->drawRect(SkRect::MakeWH(4, 4), paint);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400212}
213
214DEF_GPUTEST(VkProtectedContext_DrawRectangleWithBlendMode, reporter, options) {
Robert Phillips0d6ce7c2020-06-11 08:51:50 -0400215 auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
216 if (!protectedTestHelper->init()) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400217 return;
218 }
Robert Phillips2a4acf32020-07-06 15:50:15 -0400219 REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400220
Robert Phillips2a4acf32020-07-06 15:50:15 -0400221 auto surface = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400222 REPORTER_ASSERT(reporter, surface);
223 SkCanvas* canvas = surface->getCanvas();
224 REPORTER_ASSERT(reporter, canvas);
225 SkPaint paint;
226 paint.setColor(SK_ColorBLACK);
227 paint.setBlendMode(SkBlendMode::kColorDodge);
228 canvas->drawRect(SkRect::MakeWH(4, 4), paint);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400229}
230
231DEF_GPUTEST(VkProtectedContext_DrawRectangleWithFilter, reporter, options) {
Robert Phillips0d6ce7c2020-06-11 08:51:50 -0400232 auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
233 if (!protectedTestHelper->init()) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400234 return;
235 }
Robert Phillips2a4acf32020-07-06 15:50:15 -0400236 REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400237
Robert Phillips2a4acf32020-07-06 15:50:15 -0400238 auto surface = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400239 REPORTER_ASSERT(reporter, surface);
240 SkCanvas* canvas = surface->getCanvas();
241 REPORTER_ASSERT(reporter, canvas);
242 SkPaint paint;
243 paint.setColor(SK_ColorBLACK);
244 paint.setStyle(SkPaint::kFill_Style);
245 paint.setMaskFilter(SkMaskFilter::MakeBlur(
246 SkBlurStyle::kOuter_SkBlurStyle, 1.1f));
247 canvas->drawRect(SkRect::MakeWH(4, 4), paint);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400248}
249
250DEF_GPUTEST(VkProtectedContext_DrawThinPath, reporter, options) {
Robert Phillips0d6ce7c2020-06-11 08:51:50 -0400251 auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
252 if (!protectedTestHelper->init()) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400253 return;
254 }
Robert Phillips2a4acf32020-07-06 15:50:15 -0400255 REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400256
Robert Phillips2a4acf32020-07-06 15:50:15 -0400257 auto surface = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400258 REPORTER_ASSERT(reporter, surface);
259 SkCanvas* canvas = surface->getCanvas();
260 REPORTER_ASSERT(reporter, canvas);
261 SkPaint paint;
262 paint.setColor(SK_ColorBLACK);
263 paint.setStyle(SkPaint::kStroke_Style);
264 paint.setAntiAlias(true);
265 paint.setStrokeWidth(.4f);
266 canvas->drawPath(SkPath().moveTo(4, 4).lineTo(6, 6), paint);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400267}
268
269DEF_GPUTEST(VkProtectedContext_SaveLayer, reporter, options) {
Robert Phillips0d6ce7c2020-06-11 08:51:50 -0400270 auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
271 if (!protectedTestHelper->init()) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400272 return;
273 }
Robert Phillips2a4acf32020-07-06 15:50:15 -0400274 REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400275
Robert Phillips2a4acf32020-07-06 15:50:15 -0400276 auto surface = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400277 REPORTER_ASSERT(reporter, surface);
278 SkCanvas* canvas = surface->getCanvas();
279 REPORTER_ASSERT(reporter, canvas);
280 canvas->saveLayer(nullptr, nullptr);
281 SkPaint paint;
282 paint.setColor(SK_ColorBLACK);
283 canvas->drawRect(SkRect::MakeWH(4, 4), paint);
284 canvas->restore();
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400285}
286
287
288DEF_GPUTEST(VkProtectedContext_DrawProtectedImageOnProtectedSurface, reporter, options) {
Robert Phillips0d6ce7c2020-06-11 08:51:50 -0400289 auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
290 if (!protectedTestHelper->init()) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400291 return;
292 }
Robert Phillips2a4acf32020-07-06 15:50:15 -0400293 REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400294
295 // Create protected image.
Robert Phillips2a4acf32020-07-06 15:50:15 -0400296 auto surface1 = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400297 REPORTER_ASSERT(reporter, surface1);
298 auto image = surface1->makeImageSnapshot();
299 REPORTER_ASSERT(reporter, image);
300
301 // Create protected canvas.
Robert Phillips2a4acf32020-07-06 15:50:15 -0400302 auto surface2 = create_protected_sksurface(protectedTestHelper->directContext(), reporter);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400303 REPORTER_ASSERT(reporter, surface2);
304 SkCanvas* canvas = surface2->getCanvas();
305 REPORTER_ASSERT(reporter, canvas);
306
307 canvas->drawImage(image, 0, 0);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400308}
309
Robert Phillips3cd54322019-07-10 09:28:59 -0400310//////////////////////////////////////////////////////////////////////////////////////////////////
311// Test out DDLs using a protected Vulkan context
312
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400313void DDLMakeRenderTargetTestImpl(GrDirectContext*, skiatest::Reporter*);
Robert Phillips3cd54322019-07-10 09:28:59 -0400314
315DEF_GPUTEST(VkProtectedContext_DDLMakeRenderTargetTest, reporter, ctxInfo) {
Robert Phillips0d6ce7c2020-06-11 08:51:50 -0400316 auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
317 if (!protectedTestHelper->init()) {
Robert Phillips3cd54322019-07-10 09:28:59 -0400318 return;
319 }
Robert Phillips2a4acf32020-07-06 15:50:15 -0400320 REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
Robert Phillips3cd54322019-07-10 09:28:59 -0400321
Robert Phillips2a4acf32020-07-06 15:50:15 -0400322 DDLMakeRenderTargetTestImpl(protectedTestHelper->directContext(), reporter);
Robert Phillips3cd54322019-07-10 09:28:59 -0400323}
324
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400325void DDLSurfaceCharacterizationTestImpl(GrDirectContext*, skiatest::Reporter*);
Robert Phillips3cd54322019-07-10 09:28:59 -0400326
327DEF_GPUTEST(VkProtectedContext_DDLSurfaceCharacterizationTest, reporter, ctxInfo) {
Robert Phillips0d6ce7c2020-06-11 08:51:50 -0400328 auto protectedTestHelper = std::make_unique<VkTestHelper>(true);
329 if (!protectedTestHelper->init()) {
Robert Phillips3cd54322019-07-10 09:28:59 -0400330 return;
331 }
Robert Phillips2a4acf32020-07-06 15:50:15 -0400332 REPORTER_ASSERT(reporter, protectedTestHelper->directContext() != nullptr);
Robert Phillips3cd54322019-07-10 09:28:59 -0400333
Robert Phillips2a4acf32020-07-06 15:50:15 -0400334 DDLSurfaceCharacterizationTestImpl(protectedTestHelper->directContext(), reporter);
Robert Phillips3cd54322019-07-10 09:28:59 -0400335}
336
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400337#endif // SK_SUPPORT_GPU && defined(SK_VULKAN)