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" |
Brian Salomon | f9b0042 | 2020-10-08 16:00:14 -0400 | [diff] [blame^] | 14 | #include "tools/gpu/ManagedBackendTexture.h" |
| 15 | |
| 16 | namespace sk_gpu_test { |
| 17 | |
| 18 | sk_sp<SkSurface> MakeBackendTextureSurface(GrDirectContext* context, |
| 19 | 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) { |
| 27 | auto mbet = ManagedBackendTexture::MakeWithoutData(context, |
| 28 | dimensions.fWidth, |
| 29 | dimensions.fHeight, |
| 30 | colorType, |
| 31 | mipMapped, |
| 32 | GrRenderable::kYes, |
| 33 | isProtected); |
| 34 | if (!mbet) { |
| 35 | return nullptr; |
| 36 | } |
| 37 | 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 | } |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 47 | |
| 48 | sk_sp<SkSurface> MakeBackendRenderTargetSurface(GrDirectContext* context, |
| 49 | SkISize dimensions, |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 50 | GrSurfaceOrigin origin, |
Brian Salomon | f9b0042 | 2020-10-08 16:00:14 -0400 | [diff] [blame^] | 51 | int sampleCnt, |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 52 | SkColorType colorType, |
| 53 | sk_sp<SkColorSpace> colorSpace, |
Brian Salomon | f9b0042 | 2020-10-08 16:00:14 -0400 | [diff] [blame^] | 54 | GrProtected isProtected, |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 55 | const SkSurfaceProps* props) { |
| 56 | auto ct = SkColorTypeToGrColorType(colorType); |
| 57 | |
| 58 | struct ReleaseContext { |
Adlai Holler | 1fc76ce | 2020-10-07 11:36:49 -0400 | [diff] [blame] | 59 | GrDirectContext* fContext; |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 60 | GrBackendRenderTarget fRenderTarget; |
| 61 | }; |
| 62 | |
| 63 | auto bert = context->priv().getGpu()->createTestingOnlyBackendRenderTarget( |
Brian Salomon | f9b0042 | 2020-10-08 16:00:14 -0400 | [diff] [blame^] | 64 | dimensions, ct, sampleCnt, isProtected); |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 65 | auto rc = new ReleaseContext{context, bert}; |
| 66 | 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( |
| 77 | context, bert, origin, colorType, std::move(colorSpace), props, proc, rc); |
| 78 | } |
Brian Salomon | f9b0042 | 2020-10-08 16:00:14 -0400 | [diff] [blame^] | 79 | |
| 80 | } // namespace sk_gpu_test |