1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2020 Google LLC 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#ifndef ManagedBackendTexture_DEFINED 9cb93a386Sopenharmony_ci#define ManagedBackendTexture_DEFINED 10cb93a386Sopenharmony_ci 11cb93a386Sopenharmony_ci#include "include/core/SkRefCnt.h" 12cb93a386Sopenharmony_ci#include "include/core/SkYUVAInfo.h" 13cb93a386Sopenharmony_ci#include "include/gpu/GrDirectContext.h" 14cb93a386Sopenharmony_ci 15cb93a386Sopenharmony_ciclass GrRefCntedCallback; 16cb93a386Sopenharmony_cistruct SkImageInfo; 17cb93a386Sopenharmony_ci 18cb93a386Sopenharmony_cinamespace sk_gpu_test { 19cb93a386Sopenharmony_ci 20cb93a386Sopenharmony_ciclass ManagedBackendTexture : public SkNVRefCnt<ManagedBackendTexture> { 21cb93a386Sopenharmony_cipublic: 22cb93a386Sopenharmony_ci /** 23cb93a386Sopenharmony_ci * Make a managed backend texture with initial pixmap/color data. The 'Args' are any valid set 24cb93a386Sopenharmony_ci * of arguments to GrDirectContext::createBackendTexture that takes data but with the release 25cb93a386Sopenharmony_ci * proc/context omitted as the ManagedBackendTexture will provide them. 26cb93a386Sopenharmony_ci */ 27cb93a386Sopenharmony_ci template <typename... Args> 28cb93a386Sopenharmony_ci static sk_sp<ManagedBackendTexture> MakeWithData(GrDirectContext*, Args&&...); 29cb93a386Sopenharmony_ci 30cb93a386Sopenharmony_ci /** 31cb93a386Sopenharmony_ci * Make a managed backend texture without initial data. The 'Args' are any valid set of 32cb93a386Sopenharmony_ci * arguments to GrDirectContext::createBackendTexture that does not take data. Because our 33cb93a386Sopenharmony_ci * createBackendTexture methods that *do* take data also use default args for the proc/context 34cb93a386Sopenharmony_ci * this can be used to make a texture with data but then the MBET won't be able to ensure that 35cb93a386Sopenharmony_ci * the upload has completed before the texture is deleted. Use the WithData variant instead to 36cb93a386Sopenharmony_ci * avoid this issue. 37cb93a386Sopenharmony_ci */ 38cb93a386Sopenharmony_ci template <typename... Args> 39cb93a386Sopenharmony_ci static sk_sp<ManagedBackendTexture> MakeWithoutData(GrDirectContext*, Args&&...); 40cb93a386Sopenharmony_ci 41cb93a386Sopenharmony_ci 42cb93a386Sopenharmony_ci static sk_sp<ManagedBackendTexture> MakeFromInfo(GrDirectContext* dContext, 43cb93a386Sopenharmony_ci const SkImageInfo&, 44cb93a386Sopenharmony_ci GrMipmapped = GrMipmapped::kNo, 45cb93a386Sopenharmony_ci GrRenderable = GrRenderable::kNo, 46cb93a386Sopenharmony_ci GrProtected = GrProtected::kNo); 47cb93a386Sopenharmony_ci 48cb93a386Sopenharmony_ci static sk_sp<ManagedBackendTexture> MakeFromBitmap(GrDirectContext*, 49cb93a386Sopenharmony_ci const SkBitmap&, 50cb93a386Sopenharmony_ci GrMipmapped, 51cb93a386Sopenharmony_ci GrRenderable, 52cb93a386Sopenharmony_ci GrProtected = GrProtected::kNo); 53cb93a386Sopenharmony_ci 54cb93a386Sopenharmony_ci static sk_sp<ManagedBackendTexture> MakeFromPixmap(GrDirectContext*, 55cb93a386Sopenharmony_ci const SkPixmap&, 56cb93a386Sopenharmony_ci GrMipmapped, 57cb93a386Sopenharmony_ci GrRenderable, 58cb93a386Sopenharmony_ci GrProtected = GrProtected::kNo); 59cb93a386Sopenharmony_ci 60cb93a386Sopenharmony_ci /** GrGpuFinishedProc or image/surface release proc. */ 61cb93a386Sopenharmony_ci static void ReleaseProc(void* context); 62cb93a386Sopenharmony_ci 63cb93a386Sopenharmony_ci ~ManagedBackendTexture(); 64cb93a386Sopenharmony_ci 65cb93a386Sopenharmony_ci /** 66cb93a386Sopenharmony_ci * The context to use with ReleaseProc. This adds a ref so it *must* be balanced by a call to 67cb93a386Sopenharmony_ci * ReleaseProc. If a wrappedProc is provided then it will be called by ReleaseProc. 68cb93a386Sopenharmony_ci */ 69cb93a386Sopenharmony_ci void* releaseContext(GrGpuFinishedProc wrappedProc = nullptr, 70cb93a386Sopenharmony_ci GrGpuFinishedContext wrappedContext = nullptr) const; 71cb93a386Sopenharmony_ci 72cb93a386Sopenharmony_ci sk_sp<GrRefCntedCallback> refCountedCallback() const; 73cb93a386Sopenharmony_ci 74cb93a386Sopenharmony_ci /** 75cb93a386Sopenharmony_ci * Call if the underlying GrBackendTexture was adopted by a GrContext. This clears this out the 76cb93a386Sopenharmony_ci * MBET without deleting the texture. 77cb93a386Sopenharmony_ci */ 78cb93a386Sopenharmony_ci void wasAdopted(); 79cb93a386Sopenharmony_ci 80cb93a386Sopenharmony_ci /** 81cb93a386Sopenharmony_ci * SkImage::MakeFromYUVATextures takes a single release proc that is called once for all the 82cb93a386Sopenharmony_ci * textures. This makes a single release context for the group of textures. It's used with the 83cb93a386Sopenharmony_ci * standard ReleaseProc. Like releaseContext(), it must be balanced by a ReleaseProc call for 84cb93a386Sopenharmony_ci * proper ref counting. 85cb93a386Sopenharmony_ci */ 86cb93a386Sopenharmony_ci static void* MakeYUVAReleaseContext(const sk_sp<ManagedBackendTexture>[SkYUVAInfo::kMaxPlanes]); 87cb93a386Sopenharmony_ci 88cb93a386Sopenharmony_ci const GrBackendTexture& texture() { return fTexture; } 89cb93a386Sopenharmony_ci 90cb93a386Sopenharmony_ciprivate: 91cb93a386Sopenharmony_ci ManagedBackendTexture() = default; 92cb93a386Sopenharmony_ci ManagedBackendTexture(const ManagedBackendTexture&) = delete; 93cb93a386Sopenharmony_ci ManagedBackendTexture(ManagedBackendTexture&&) = delete; 94cb93a386Sopenharmony_ci 95cb93a386Sopenharmony_ci sk_sp<GrDirectContext> fDContext; 96cb93a386Sopenharmony_ci GrBackendTexture fTexture; 97cb93a386Sopenharmony_ci}; 98cb93a386Sopenharmony_ci 99cb93a386Sopenharmony_citemplate <typename... Args> 100cb93a386Sopenharmony_ciinline sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeWithData(GrDirectContext* dContext, 101cb93a386Sopenharmony_ci Args&&... args) { 102cb93a386Sopenharmony_ci sk_sp<ManagedBackendTexture> mbet(new ManagedBackendTexture); 103cb93a386Sopenharmony_ci mbet->fDContext = sk_ref_sp(dContext); 104cb93a386Sopenharmony_ci mbet->fTexture = dContext->createBackendTexture(std::forward<Args>(args)..., 105cb93a386Sopenharmony_ci ReleaseProc, 106cb93a386Sopenharmony_ci mbet->releaseContext()); 107cb93a386Sopenharmony_ci if (!mbet->fTexture.isValid()) { 108cb93a386Sopenharmony_ci return nullptr; 109cb93a386Sopenharmony_ci } 110cb93a386Sopenharmony_ci return mbet; 111cb93a386Sopenharmony_ci} 112cb93a386Sopenharmony_ci 113cb93a386Sopenharmony_citemplate <typename... Args> 114cb93a386Sopenharmony_ciinline sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeWithoutData( 115cb93a386Sopenharmony_ci GrDirectContext* dContext, 116cb93a386Sopenharmony_ci Args&&... args) { 117cb93a386Sopenharmony_ci GrBackendTexture texture = 118cb93a386Sopenharmony_ci dContext->createBackendTexture(std::forward<Args>(args)...); 119cb93a386Sopenharmony_ci if (!texture.isValid()) { 120cb93a386Sopenharmony_ci return nullptr; 121cb93a386Sopenharmony_ci } 122cb93a386Sopenharmony_ci sk_sp<ManagedBackendTexture> mbet(new ManagedBackendTexture); 123cb93a386Sopenharmony_ci mbet->fDContext = sk_ref_sp(dContext); 124cb93a386Sopenharmony_ci mbet->fTexture = std::move(texture); 125cb93a386Sopenharmony_ci return mbet; 126cb93a386Sopenharmony_ci} 127cb93a386Sopenharmony_ci 128cb93a386Sopenharmony_ci} // namespace sk_gpu_test 129cb93a386Sopenharmony_ci 130cb93a386Sopenharmony_ci#endif 131