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" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame^] | 12 | #include "src/gpu/GrDirectContextPriv.h" |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 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 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 18 | sk_sp<SkSurface> MakeBackendTextureSurface(GrDirectContext* dContext, |
| 19 | const SkImageInfo& ii, |
| 20 | GrSurfaceOrigin origin, |
| 21 | int sampleCnt, |
| 22 | GrMipmapped mipMapped, |
| 23 | GrProtected isProtected, |
| 24 | const SkSurfaceProps* props) { |
| 25 | if (ii.alphaType() == kUnpremul_SkAlphaType) { |
| 26 | return nullptr; |
| 27 | } |
| 28 | auto mbet = ManagedBackendTexture::MakeWithoutData(dContext, |
| 29 | ii.width(), |
| 30 | ii.height(), |
| 31 | ii.colorType(), |
| 32 | mipMapped, |
| 33 | GrRenderable::kYes, |
| 34 | isProtected); |
| 35 | if (!mbet) { |
| 36 | return nullptr; |
| 37 | } |
| 38 | return SkSurface::MakeFromBackendTexture(dContext, |
| 39 | mbet->texture(), |
| 40 | origin, |
| 41 | sampleCnt, |
| 42 | ii.colorType(), |
| 43 | ii.refColorSpace(), |
| 44 | props, |
| 45 | ManagedBackendTexture::ReleaseProc, |
| 46 | mbet->releaseContext()); |
| 47 | } |
| 48 | |
| 49 | sk_sp<SkSurface> MakeBackendTextureSurface(GrDirectContext* dContext, |
Brian Salomon | f9b0042 | 2020-10-08 16:00:14 -0400 | [diff] [blame] | 50 | SkISize dimensions, |
| 51 | GrSurfaceOrigin origin, |
| 52 | int sampleCnt, |
| 53 | SkColorType colorType, |
| 54 | sk_sp<SkColorSpace> colorSpace, |
| 55 | GrMipmapped mipMapped, |
| 56 | GrProtected isProtected, |
| 57 | const SkSurfaceProps* props) { |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 58 | auto ii = SkImageInfo::Make(dimensions, colorType, kPremul_SkAlphaType, std::move(colorSpace)); |
| 59 | return MakeBackendTextureSurface( |
| 60 | dContext, ii, origin, sampleCnt, mipMapped, isProtected, props); |
Brian Salomon | 4efaf5e | 2020-10-12 21:30:00 +0000 | [diff] [blame] | 61 | } |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 62 | sk_sp<SkSurface> MakeBackendRenderTargetSurface(GrDirectContext* dContext, |
| 63 | const SkImageInfo& ii, |
Brian Salomon | 4efaf5e | 2020-10-12 21:30:00 +0000 | [diff] [blame] | 64 | GrSurfaceOrigin origin, |
| 65 | int sampleCnt, |
Brian Salomon | 4efaf5e | 2020-10-12 21:30:00 +0000 | [diff] [blame] | 66 | GrProtected isProtected, |
| 67 | const SkSurfaceProps* props) { |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 68 | if (ii.alphaType() == kUnpremul_SkAlphaType) { |
| 69 | return nullptr; |
| 70 | } |
| 71 | auto ct = SkColorTypeToGrColorType(ii.colorType()); |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 72 | |
| 73 | struct ReleaseContext { |
Adlai Holler | 1fc76ce | 2020-10-07 11:36:49 -0400 | [diff] [blame] | 74 | GrDirectContext* fContext; |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 75 | GrBackendRenderTarget fRenderTarget; |
| 76 | }; |
| 77 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 78 | auto bert = dContext->priv().getGpu()->createTestingOnlyBackendRenderTarget( |
| 79 | ii.dimensions(), ct, sampleCnt, isProtected); |
| 80 | auto rc = new ReleaseContext{dContext, bert}; |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 81 | SkASSERT(!bert.isValid() || bert.sampleCnt() >= sampleCnt); |
| 82 | |
| 83 | auto proc = [](void* c) { |
| 84 | const auto* rc = static_cast<ReleaseContext*>(c); |
| 85 | if (auto gpu = rc->fContext->priv().getGpu(); gpu && rc->fRenderTarget.isValid()) { |
| 86 | gpu->deleteTestingOnlyBackendRenderTarget(rc->fRenderTarget); |
| 87 | } |
| 88 | delete rc; |
| 89 | }; |
| 90 | |
| 91 | return SkSurface::MakeFromBackendRenderTarget( |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 92 | dContext, bert, origin, ii.colorType(), ii.refColorSpace(), props, proc, rc); |
| 93 | } |
| 94 | |
| 95 | sk_sp<SkSurface> MakeBackendRenderTargetSurface(GrDirectContext* dContext, |
| 96 | SkISize dimensions, |
| 97 | GrSurfaceOrigin origin, |
| 98 | int sampleCnt, |
| 99 | SkColorType colorType, |
| 100 | sk_sp<SkColorSpace> colorSpace, |
| 101 | GrProtected isProtected, |
| 102 | const SkSurfaceProps* props) { |
| 103 | auto ii = SkImageInfo::Make(dimensions, colorType, kPremul_SkAlphaType, std::move(colorSpace)); |
| 104 | return MakeBackendRenderTargetSurface(dContext, ii, origin, sampleCnt, isProtected, props); |
Brian Salomon | 72c7b98 | 2020-10-06 10:07:38 -0400 | [diff] [blame] | 105 | } |
Brian Salomon | f9b0042 | 2020-10-08 16:00:14 -0400 | [diff] [blame] | 106 | |
| 107 | } // namespace sk_gpu_test |