Brian Salomon | f9b0042 | 2020-10-08 16:00:14 -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/ManagedBackendTexture.h" |
| 9 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 10 | #include "include/core/SkImageInfo.h" |
| 11 | #include "include/private/GrTypesPriv.h" |
| 12 | #include "src/core/SkMipmap.h" |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | struct Context { |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 17 | GrGpuFinishedProc fWrappedProc = nullptr; |
| 18 | GrGpuFinishedContext fWrappedContext = nullptr; |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 19 | sk_sp<sk_gpu_test::ManagedBackendTexture> fMBETs[SkYUVAInfo::kMaxPlanes]; |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 20 | }; |
| 21 | |
| 22 | } // anonymous namespace |
| 23 | |
Brian Salomon | f9b0042 | 2020-10-08 16:00:14 -0400 | [diff] [blame] | 24 | namespace sk_gpu_test { |
| 25 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 26 | void ManagedBackendTexture::ReleaseProc(void* ctx) { |
| 27 | std::unique_ptr<Context> context(static_cast<Context*>(ctx)); |
| 28 | if (context->fWrappedProc) { |
| 29 | context->fWrappedProc(context->fWrappedContext); |
| 30 | } |
Brian Salomon | f9b0042 | 2020-10-08 16:00:14 -0400 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | ManagedBackendTexture::~ManagedBackendTexture() { |
| 34 | if (fDContext && fTexture.isValid()) { |
| 35 | fDContext->deleteBackendTexture(fTexture); |
| 36 | } |
| 37 | } |
| 38 | |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 39 | void* ManagedBackendTexture::releaseContext(GrGpuFinishedProc wrappedProc, |
| 40 | GrGpuFinishedContext wrappedCtx) const { |
| 41 | // Make sure we don't get a wrapped ctx without a wrapped proc |
| 42 | SkASSERT(!wrappedCtx || wrappedProc); |
Brian Salomon | 7db7139 | 2020-10-16 10:05:21 -0400 | [diff] [blame] | 43 | return new Context{wrappedProc, wrappedCtx, {sk_ref_sp(this)}}; |
| 44 | } |
| 45 | |
| 46 | void* ManagedBackendTexture::MakeYUVAReleaseContext( |
| 47 | const sk_sp<ManagedBackendTexture> mbets[SkYUVAInfo::kMaxPlanes]) { |
| 48 | auto context = new Context; |
| 49 | for (int i = 0; i < SkYUVAInfo::kMaxPlanes; ++i) { |
| 50 | context->fMBETs[i] = mbets[i]; |
| 51 | } |
| 52 | return context; |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | sk_sp<GrRefCntedCallback> ManagedBackendTexture::refCountedCallback() const { |
Brian Salomon | 694ff17 | 2020-11-04 16:54:28 -0500 | [diff] [blame] | 56 | return GrRefCntedCallback::Make(ReleaseProc, this->releaseContext()); |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void ManagedBackendTexture::wasAdopted() { fTexture = {}; } |
| 60 | |
| 61 | sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromInfo(GrDirectContext* dContext, |
| 62 | const SkImageInfo& ii, |
| 63 | GrMipmapped mipmapped, |
| 64 | GrRenderable renderable, |
| 65 | GrProtected isProtected) { |
| 66 | return MakeWithoutData( |
| 67 | dContext, ii.width(), ii.height(), ii.colorType(), mipmapped, renderable, isProtected); |
| 68 | } |
| 69 | |
| 70 | sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromBitmap(GrDirectContext* dContext, |
| 71 | const SkBitmap& bitmap, |
| 72 | GrMipmapped mipmapped, |
| 73 | GrRenderable renderable, |
| 74 | GrProtected isProtected) { |
| 75 | std::vector<SkPixmap> levels({bitmap.pixmap()}); |
| 76 | std::unique_ptr<SkMipmap> mm; |
| 77 | |
| 78 | if (mipmapped == GrMipmapped::kYes) { |
| 79 | mm.reset(SkMipmap::Build(bitmap, nullptr)); |
| 80 | if (!mm) { |
| 81 | return nullptr; |
| 82 | } |
| 83 | for (int i = 0; i < mm->countLevels(); ++i) { |
| 84 | SkMipmap::Level level; |
| 85 | SkAssertResult(mm->getLevel(i, &level)); |
| 86 | levels.push_back(level.fPixmap); |
| 87 | } |
| 88 | } |
Brian Salomon | b5f880a | 2020-12-07 11:30:16 -0500 | [diff] [blame^] | 89 | return MakeWithData(dContext, |
| 90 | levels.data(), |
| 91 | static_cast<int>(levels.size()), |
| 92 | kTopLeft_GrSurfaceOrigin, |
| 93 | renderable, |
| 94 | isProtected); |
Brian Salomon | f9b0042 | 2020-10-08 16:00:14 -0400 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | } // namespace sk_gpu_test |