2020-10-08 20:00:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2020 Google LLC
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ManagedBackendTexture_DEFINED
|
|
|
|
#define ManagedBackendTexture_DEFINED
|
|
|
|
|
|
|
|
#include "include/core/SkRefCnt.h"
|
2020-10-16 14:05:21 +00:00
|
|
|
#include "include/core/SkYUVAInfo.h"
|
2020-10-08 20:00:14 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
|
|
|
|
2022-03-23 20:37:01 +00:00
|
|
|
namespace skgpu {
|
|
|
|
class RefCntedCallback;
|
|
|
|
}
|
2020-10-13 00:45:06 +00:00
|
|
|
struct SkImageInfo;
|
|
|
|
|
2020-10-08 20:00:14 +00:00
|
|
|
namespace sk_gpu_test {
|
|
|
|
|
|
|
|
class ManagedBackendTexture : public SkNVRefCnt<ManagedBackendTexture> {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Make a managed backend texture with initial pixmap/color data. The 'Args' are any valid set
|
|
|
|
* of arguments to GrDirectContext::createBackendTexture that takes data but with the release
|
|
|
|
* proc/context omitted as the ManagedBackendTexture will provide them.
|
|
|
|
*/
|
|
|
|
template <typename... Args>
|
|
|
|
static sk_sp<ManagedBackendTexture> MakeWithData(GrDirectContext*, Args&&...);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a managed backend texture without initial data. The 'Args' are any valid set of
|
|
|
|
* arguments to GrDirectContext::createBackendTexture that does not take data. Because our
|
|
|
|
* createBackendTexture methods that *do* take data also use default args for the proc/context
|
|
|
|
* this can be used to make a texture with data but then the MBET won't be able to ensure that
|
|
|
|
* the upload has completed before the texture is deleted. Use the WithData variant instead to
|
|
|
|
* avoid this issue.
|
|
|
|
*/
|
|
|
|
template <typename... Args>
|
|
|
|
static sk_sp<ManagedBackendTexture> MakeWithoutData(GrDirectContext*, Args&&...);
|
|
|
|
|
2020-10-13 00:45:06 +00:00
|
|
|
|
|
|
|
static sk_sp<ManagedBackendTexture> MakeFromInfo(GrDirectContext* dContext,
|
|
|
|
const SkImageInfo&,
|
|
|
|
GrMipmapped = GrMipmapped::kNo,
|
|
|
|
GrRenderable = GrRenderable::kNo,
|
|
|
|
GrProtected = GrProtected::kNo);
|
|
|
|
|
|
|
|
static sk_sp<ManagedBackendTexture> MakeFromBitmap(GrDirectContext*,
|
|
|
|
const SkBitmap&,
|
|
|
|
GrMipmapped,
|
|
|
|
GrRenderable,
|
|
|
|
GrProtected = GrProtected::kNo);
|
|
|
|
|
2020-12-08 16:51:02 +00:00
|
|
|
static sk_sp<ManagedBackendTexture> MakeFromPixmap(GrDirectContext*,
|
|
|
|
const SkPixmap&,
|
|
|
|
GrMipmapped,
|
|
|
|
GrRenderable,
|
|
|
|
GrProtected = GrProtected::kNo);
|
|
|
|
|
2020-10-08 20:00:14 +00:00
|
|
|
/** GrGpuFinishedProc or image/surface release proc. */
|
|
|
|
static void ReleaseProc(void* context);
|
|
|
|
|
|
|
|
~ManagedBackendTexture();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The context to use with ReleaseProc. This adds a ref so it *must* be balanced by a call to
|
2020-10-13 00:45:06 +00:00
|
|
|
* ReleaseProc. If a wrappedProc is provided then it will be called by ReleaseProc.
|
|
|
|
*/
|
|
|
|
void* releaseContext(GrGpuFinishedProc wrappedProc = nullptr,
|
|
|
|
GrGpuFinishedContext wrappedContext = nullptr) const;
|
|
|
|
|
2022-03-23 20:37:01 +00:00
|
|
|
sk_sp<skgpu::RefCntedCallback> refCountedCallback() const;
|
2020-10-13 00:45:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Call if the underlying GrBackendTexture was adopted by a GrContext. This clears this out the
|
|
|
|
* MBET without deleting the texture.
|
2020-10-08 20:00:14 +00:00
|
|
|
*/
|
2020-10-13 00:45:06 +00:00
|
|
|
void wasAdopted();
|
2020-10-08 20:00:14 +00:00
|
|
|
|
2020-10-16 14:05:21 +00:00
|
|
|
/**
|
|
|
|
* SkImage::MakeFromYUVATextures takes a single release proc that is called once for all the
|
|
|
|
* textures. This makes a single release context for the group of textures. It's used with the
|
|
|
|
* standard ReleaseProc. Like releaseContext(), it must be balanced by a ReleaseProc call for
|
|
|
|
* proper ref counting.
|
|
|
|
*/
|
|
|
|
static void* MakeYUVAReleaseContext(const sk_sp<ManagedBackendTexture>[SkYUVAInfo::kMaxPlanes]);
|
|
|
|
|
2020-10-08 20:00:14 +00:00
|
|
|
const GrBackendTexture& texture() { return fTexture; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
ManagedBackendTexture() = default;
|
|
|
|
ManagedBackendTexture(const ManagedBackendTexture&) = delete;
|
|
|
|
ManagedBackendTexture(ManagedBackendTexture&&) = delete;
|
|
|
|
|
2020-10-13 00:45:06 +00:00
|
|
|
sk_sp<GrDirectContext> fDContext;
|
2020-10-08 20:00:14 +00:00
|
|
|
GrBackendTexture fTexture;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
inline sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeWithData(GrDirectContext* dContext,
|
|
|
|
Args&&... args) {
|
|
|
|
sk_sp<ManagedBackendTexture> mbet(new ManagedBackendTexture);
|
2020-10-13 00:45:06 +00:00
|
|
|
mbet->fDContext = sk_ref_sp(dContext);
|
2020-10-08 20:00:14 +00:00
|
|
|
mbet->fTexture = dContext->createBackendTexture(std::forward<Args>(args)...,
|
|
|
|
ReleaseProc,
|
|
|
|
mbet->releaseContext());
|
2020-11-05 16:11:20 +00:00
|
|
|
if (!mbet->fTexture.isValid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-10-08 20:00:14 +00:00
|
|
|
return mbet;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
inline sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeWithoutData(
|
|
|
|
GrDirectContext* dContext,
|
|
|
|
Args&&... args) {
|
2020-11-05 16:11:20 +00:00
|
|
|
GrBackendTexture texture =
|
|
|
|
dContext->createBackendTexture(std::forward<Args>(args)...);
|
|
|
|
if (!texture.isValid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-10-08 20:00:14 +00:00
|
|
|
sk_sp<ManagedBackendTexture> mbet(new ManagedBackendTexture);
|
2020-10-13 00:45:06 +00:00
|
|
|
mbet->fDContext = sk_ref_sp(dContext);
|
2020-11-05 16:11:20 +00:00
|
|
|
mbet->fTexture = std::move(texture);
|
2020-10-08 20:00:14 +00:00
|
|
|
return mbet;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sk_gpu_test
|
|
|
|
|
|
|
|
#endif
|