1 /*
2 * Copyright 2020 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/BackendSurfaceFactory.h"
9
10 #include "include/core/SkSurface.h"
11 #include "include/gpu/GrDirectContext.h"
12 #include "src/gpu/GrDirectContextPriv.h"
13 #include "src/gpu/GrGpu.h"
14 #include "tools/gpu/ManagedBackendTexture.h"
15
16 namespace sk_gpu_test {
17
MakeBackendTextureSurface(GrDirectContext * dContext,const SkImageInfo & ii,GrSurfaceOrigin origin,int sampleCnt,GrMipmapped mipMapped,GrProtected isProtected,const SkSurfaceProps * props)18 sk_sp<SkSurface> MakeBackendTextureSurface(GrDirectContext* dContext,
19 const SkImageInfo& ii,
20 GrSurfaceOrigin origin,
21 int sampleCnt,
22 GrMipmapped mipMapped,
23 GrProtected isProtected,
24 const SkSurfaceProps* props) {
25 if (ii.alphaType() == kUnpremul_SkAlphaType) {
26 return nullptr;
27 }
28 auto mbet = ManagedBackendTexture::MakeWithoutData(dContext,
29 ii.width(),
30 ii.height(),
31 ii.colorType(),
32 mipMapped,
33 GrRenderable::kYes,
34 isProtected);
35 if (!mbet) {
36 return nullptr;
37 }
38 return SkSurface::MakeFromBackendTexture(dContext,
39 mbet->texture(),
40 origin,
41 sampleCnt,
42 ii.colorType(),
43 ii.refColorSpace(),
44 props,
45 ManagedBackendTexture::ReleaseProc,
46 mbet->releaseContext());
47 }
48
MakeBackendTextureSurface(GrDirectContext * dContext,SkISize dimensions,GrSurfaceOrigin origin,int sampleCnt,SkColorType colorType,sk_sp<SkColorSpace> colorSpace,GrMipmapped mipMapped,GrProtected isProtected,const SkSurfaceProps * props)49 sk_sp<SkSurface> MakeBackendTextureSurface(GrDirectContext* dContext,
50 SkISize dimensions,
51 GrSurfaceOrigin origin,
52 int sampleCnt,
53 SkColorType colorType,
54 sk_sp<SkColorSpace> colorSpace,
55 GrMipmapped mipMapped,
56 GrProtected isProtected,
57 const SkSurfaceProps* props) {
58 auto ii = SkImageInfo::Make(dimensions, colorType, kPremul_SkAlphaType, std::move(colorSpace));
59 return MakeBackendTextureSurface(
60 dContext, ii, origin, sampleCnt, mipMapped, isProtected, props);
61 }
MakeBackendRenderTargetSurface(GrDirectContext * dContext,const SkImageInfo & ii,GrSurfaceOrigin origin,int sampleCnt,GrProtected isProtected,const SkSurfaceProps * props)62 sk_sp<SkSurface> MakeBackendRenderTargetSurface(GrDirectContext* dContext,
63 const SkImageInfo& ii,
64 GrSurfaceOrigin origin,
65 int sampleCnt,
66 GrProtected isProtected,
67 const SkSurfaceProps* props) {
68 if (ii.alphaType() == kUnpremul_SkAlphaType || ii.alphaType() == kUnknown_SkAlphaType) {
69 return nullptr;
70 }
71 auto ct = SkColorTypeToGrColorType(ii.colorType());
72
73 struct ReleaseContext {
74 sk_sp<GrDirectContext> fContext;
75 GrBackendRenderTarget fRenderTarget;
76 };
77
78 auto bert = dContext->priv().getGpu()->createTestingOnlyBackendRenderTarget(
79 ii.dimensions(), ct, sampleCnt, isProtected);
80 auto rc = new ReleaseContext{sk_ref_sp(dContext), bert};
81 SkASSERT(!bert.isValid() || bert.sampleCnt() >= sampleCnt);
82
83 auto proc = [](void* c) {
84 const auto* rc = static_cast<ReleaseContext*>(c);
85 if (auto gpu = rc->fContext->priv().getGpu(); gpu && rc->fRenderTarget.isValid()) {
86 gpu->deleteTestingOnlyBackendRenderTarget(rc->fRenderTarget);
87 }
88 delete rc;
89 };
90
91 return SkSurface::MakeFromBackendRenderTarget(
92 dContext, bert, origin, ii.colorType(), ii.refColorSpace(), props, proc, rc);
93 }
94
MakeBackendRenderTargetSurface(GrDirectContext * dContext,SkISize dimensions,GrSurfaceOrigin origin,int sampleCnt,SkColorType colorType,sk_sp<SkColorSpace> colorSpace,GrProtected isProtected,const SkSurfaceProps * props)95 sk_sp<SkSurface> MakeBackendRenderTargetSurface(GrDirectContext* dContext,
96 SkISize dimensions,
97 GrSurfaceOrigin origin,
98 int sampleCnt,
99 SkColorType colorType,
100 sk_sp<SkColorSpace> colorSpace,
101 GrProtected isProtected,
102 const SkSurfaceProps* props) {
103 auto ii = SkImageInfo::Make(dimensions, colorType, kPremul_SkAlphaType, std::move(colorSpace));
104 return MakeBackendRenderTargetSurface(dContext, ii, origin, sampleCnt, isProtected, props);
105 }
106
107 } // namespace sk_gpu_test
108