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