Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 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" |
Adlai Holler | 0ce2c54 | 2020-10-06 14:04:35 -0400 | [diff] [blame^] | 11 | #include "include/gpu/GrDirectContext.h" |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrContextPriv.h" |
| 13 | #include "src/gpu/GrGpu.h" |
| 14 | |
| 15 | sk_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 | } |