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