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 "tools/gpu/ProtectedUtils.h"
9
10 #include "include/gpu/ganesh/SkImageGanesh.h"
11 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
12 #include "src/gpu/ganesh/GrDirectContextPriv.h"
13 #include "tools/gpu/BackendSurfaceFactory.h"
14 #include "tools/gpu/BackendTextureImageFactory.h"
15
16 namespace ProtectedUtils {
17
CreateProtectedSkSurface(GrDirectContext * dContext,SkISize size,bool textureable,bool isProtected,const SkSurfaceProps * surfaceProps)18 sk_sp<SkSurface> CreateProtectedSkSurface(GrDirectContext* dContext,
19 SkISize size,
20 bool textureable,
21 bool isProtected,
22 const SkSurfaceProps* surfaceProps) {
23 sk_sp<SkSurface> surface;
24 if (textureable) {
25 surface = sk_gpu_test::MakeBackendTextureSurface(dContext,
26 size,
27 kTopLeft_GrSurfaceOrigin,
28 1,
29 kRGBA_8888_SkColorType,
30 /* colorSpace= */ nullptr,
31 skgpu::Mipmapped::kNo,
32 skgpu::Protected(isProtected),
33 surfaceProps);
34 } else {
35 surface = sk_gpu_test::MakeBackendRenderTargetSurface(dContext,
36 size,
37 kTopLeft_GrSurfaceOrigin,
38 1,
39 kRGBA_8888_SkColorType,
40 /* colorSpace= */ nullptr,
41 skgpu::Protected(isProtected),
42 surfaceProps);
43 }
44 if (!surface) {
45 SK_ABORT("Could not create %s surface.", isProtected ? "protected" : "unprotected");
46 return nullptr;
47 }
48
49 SkCanvas* canvas = surface->getCanvas();
50
51 canvas->clear(SkColors::kBlue);
52
53 if (textureable) {
54 GrBackendTexture backendTex = SkSurfaces::GetBackendTexture(
55 surface.get(), SkSurfaces::BackendHandleAccess::kFlushRead);
56 SkASSERT(backendTex.isValid());
57 SkASSERT(backendTex.isProtected() == isProtected);
58 } else {
59 GrBackendRenderTarget backendRT = SkSurfaces::GetBackendRenderTarget(
60 surface.get(), SkSurfaces::BackendHandleAccess::kFlushRead);
61 SkASSERT(backendRT.isValid());
62 SkASSERT(backendRT.isProtected() == isProtected);
63 }
64
65 return surface;
66 }
67
CheckImageBEProtection(SkImage * image,bool expectingProtected)68 void CheckImageBEProtection(SkImage* image, bool expectingProtected) {
69 GrBackendTexture beTex;
70 GrSurfaceOrigin origin;
71 bool result = SkImages::GetBackendTextureFromImage(image,
72 &beTex,
73 /* flushPendingGrContextIO= */ true,
74 &origin);
75 if (!result) {
76 SK_ABORT("GetBackendTextureFromImage failed");
77 return;
78 }
79
80 SkASSERT(beTex.isValid());
81 SkASSERT(beTex.isProtected() == expectingProtected);
82 }
83
CreateProtectedSkImage(GrDirectContext * dContext,SkISize size,SkColor4f color,bool isProtected)84 sk_sp<SkImage> CreateProtectedSkImage(GrDirectContext* dContext,
85 SkISize size,
86 SkColor4f color,
87 bool isProtected) {
88 SkImageInfo ii = SkImageInfo::Make(size, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
89
90 sk_sp<SkImage> image = sk_gpu_test::MakeBackendTextureImage(dContext,
91 ii,
92 color,
93 skgpu::Mipmapped::kNo,
94 GrRenderable::kNo,
95 kTopLeft_GrSurfaceOrigin,
96 skgpu::Protected(isProtected));
97 if (!image) {
98 SK_ABORT("Could not create %s image.", isProtected ? "protected" : "unprotected");
99 return nullptr;
100 }
101
102 CheckImageBEProtection(image.get(), isProtected);
103
104 return image;
105 }
106
107 } // namespace ProtectedUtils
108