blob: bbb13ea545f871ba2e4055f10f4a21a304a29be0 [file] [log] [blame]
Brian Salomon72c7b982020-10-06 10:07:38 -04001/*
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"
Adlai Holler0ce2c542020-10-06 14:04:35 -040011#include "include/gpu/GrDirectContext.h"
Adlai Hollera0693042020-10-14 11:23:11 -040012#include "src/gpu/GrDirectContextPriv.h"
Brian Salomon72c7b982020-10-06 10:07:38 -040013#include "src/gpu/GrGpu.h"
Brian Salomonf9b00422020-10-08 16:00:14 -040014#include "tools/gpu/ManagedBackendTexture.h"
15
16namespace sk_gpu_test {
17
Brian Salomon72050802020-10-12 20:45:06 -040018sk_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
49sk_sp<SkSurface> MakeBackendTextureSurface(GrDirectContext* dContext,
Brian Salomonf9b00422020-10-08 16:00:14 -040050 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) {
Brian Salomon72050802020-10-12 20:45:06 -040058 auto ii = SkImageInfo::Make(dimensions, colorType, kPremul_SkAlphaType, std::move(colorSpace));
59 return MakeBackendTextureSurface(
60 dContext, ii, origin, sampleCnt, mipMapped, isProtected, props);
Brian Salomon4efaf5e2020-10-12 21:30:00 +000061}
Brian Salomon72050802020-10-12 20:45:06 -040062sk_sp<SkSurface> MakeBackendRenderTargetSurface(GrDirectContext* dContext,
63 const SkImageInfo& ii,
Brian Salomon4efaf5e2020-10-12 21:30:00 +000064 GrSurfaceOrigin origin,
65 int sampleCnt,
Brian Salomon4efaf5e2020-10-12 21:30:00 +000066 GrProtected isProtected,
67 const SkSurfaceProps* props) {
Brian Salomon72050802020-10-12 20:45:06 -040068 if (ii.alphaType() == kUnpremul_SkAlphaType) {
69 return nullptr;
70 }
71 auto ct = SkColorTypeToGrColorType(ii.colorType());
Brian Salomon72c7b982020-10-06 10:07:38 -040072
73 struct ReleaseContext {
Adlai Holler1fc76ce2020-10-07 11:36:49 -040074 GrDirectContext* fContext;
Brian Salomon72c7b982020-10-06 10:07:38 -040075 GrBackendRenderTarget fRenderTarget;
76 };
77
Brian Salomon72050802020-10-12 20:45:06 -040078 auto bert = dContext->priv().getGpu()->createTestingOnlyBackendRenderTarget(
79 ii.dimensions(), ct, sampleCnt, isProtected);
80 auto rc = new ReleaseContext{dContext, bert};
Brian Salomon72c7b982020-10-06 10:07:38 -040081 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(
Brian Salomon72050802020-10-12 20:45:06 -040092 dContext, bert, origin, ii.colorType(), ii.refColorSpace(), props, proc, rc);
93}
94
95sk_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);
Brian Salomon72c7b982020-10-06 10:07:38 -0400105}
Brian Salomonf9b00422020-10-08 16:00:14 -0400106
107} // namespace sk_gpu_test