blob: ebece67bc061f92250b0266454137fccdc41e58f [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"
14
15sk_sp<SkSurface> MakeBackendRenderTargetSurface(GrDirectContext* context,
16 SkISize dimensions,
17 int sampleCnt,
18 GrSurfaceOrigin origin,
19 SkColorType colorType,
20 sk_sp<SkColorSpace> colorSpace,
21 const SkSurfaceProps* props) {
22 auto ct = SkColorTypeToGrColorType(colorType);
23
24 struct ReleaseContext {
25 GrContext* fContext;
26 GrBackendRenderTarget fRenderTarget;
27 };
28
29 auto bert = context->priv().getGpu()->createTestingOnlyBackendRenderTarget(
30 dimensions, ct, sampleCnt);
31 auto rc = new ReleaseContext{context, bert};
32 SkASSERT(!bert.isValid() || bert.sampleCnt() >= sampleCnt);
33
34 auto proc = [](void* c) {
35 const auto* rc = static_cast<ReleaseContext*>(c);
36 if (auto gpu = rc->fContext->priv().getGpu(); gpu && rc->fRenderTarget.isValid()) {
37 gpu->deleteTestingOnlyBackendRenderTarget(rc->fRenderTarget);
38 }
39 delete rc;
40 };
41
42 return SkSurface::MakeFromBackendRenderTarget(
43 context, bert, origin, colorType, std::move(colorSpace), props, proc, rc);
44}