blob: 32a6500692b1a3fdb12d870dcde1041872c0ec29 [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"
Brian Salomon72c7b982020-10-06 10:07:38 -040012#include "src/gpu/GrContextPriv.h"
13#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 Salomon4efaf5e2020-10-12 21:30:00 +000018sk_sp<SkSurface> MakeBackendTextureSurface(GrDirectContext* context,
Brian Salomonf9b00422020-10-08 16:00:14 -040019 SkISize dimensions,
20 GrSurfaceOrigin origin,
21 int sampleCnt,
22 SkColorType colorType,
23 sk_sp<SkColorSpace> colorSpace,
24 GrMipmapped mipMapped,
25 GrProtected isProtected,
26 const SkSurfaceProps* props) {
Brian Salomon4efaf5e2020-10-12 21:30:00 +000027 auto mbet = ManagedBackendTexture::MakeWithoutData(context,
28 dimensions.fWidth,
29 dimensions.fHeight,
30 colorType,
31 mipMapped,
32 GrRenderable::kYes,
33 isProtected);
34 if (!mbet) {
Brian Salomon21f8b512020-10-12 14:45:12 -040035 return nullptr;
36 }
Brian Salomon4efaf5e2020-10-12 21:30:00 +000037 return SkSurface::MakeFromBackendTexture(context,
38 mbet->texture(),
39 origin,
40 sampleCnt,
41 colorType,
42 std::move(colorSpace),
43 props,
44 ManagedBackendTexture::ReleaseProc,
45 mbet->releaseContext());
46}
47
48sk_sp<SkSurface> MakeBackendRenderTargetSurface(GrDirectContext* context,
49 SkISize dimensions,
50 GrSurfaceOrigin origin,
51 int sampleCnt,
52 SkColorType colorType,
53 sk_sp<SkColorSpace> colorSpace,
54 GrProtected isProtected,
55 const SkSurfaceProps* props) {
56 auto ct = SkColorTypeToGrColorType(colorType);
Brian Salomon72c7b982020-10-06 10:07:38 -040057
58 struct ReleaseContext {
Adlai Holler1fc76ce2020-10-07 11:36:49 -040059 GrDirectContext* fContext;
Brian Salomon72c7b982020-10-06 10:07:38 -040060 GrBackendRenderTarget fRenderTarget;
61 };
62
Brian Salomon4efaf5e2020-10-12 21:30:00 +000063 auto bert = context->priv().getGpu()->createTestingOnlyBackendRenderTarget(
64 dimensions, ct, sampleCnt, isProtected);
65 auto rc = new ReleaseContext{context, bert};
Brian Salomon72c7b982020-10-06 10:07:38 -040066 SkASSERT(!bert.isValid() || bert.sampleCnt() >= sampleCnt);
67
68 auto proc = [](void* c) {
69 const auto* rc = static_cast<ReleaseContext*>(c);
70 if (auto gpu = rc->fContext->priv().getGpu(); gpu && rc->fRenderTarget.isValid()) {
71 gpu->deleteTestingOnlyBackendRenderTarget(rc->fRenderTarget);
72 }
73 delete rc;
74 };
75
76 return SkSurface::MakeFromBackendRenderTarget(
Brian Salomon4efaf5e2020-10-12 21:30:00 +000077 context, bert, origin, colorType, std::move(colorSpace), props, proc, rc);
Brian Salomon72c7b982020-10-06 10:07:38 -040078}
Brian Salomonf9b00422020-10-08 16:00:14 -040079
80} // namespace sk_gpu_test