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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tools/gpu/ManagedBackendTexture.h"
|
|
|
|
|
2020-12-23 15:11:33 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
2020-10-13 00:45:06 +00:00
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/private/GrTypesPriv.h"
|
|
|
|
#include "src/core/SkMipmap.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct Context {
|
|
|
|
GrGpuFinishedProc fWrappedProc = nullptr;
|
|
|
|
GrGpuFinishedContext fWrappedContext = nullptr;
|
2020-10-16 14:05:21 +00:00
|
|
|
sk_sp<sk_gpu_test::ManagedBackendTexture> fMBETs[SkYUVAInfo::kMaxPlanes];
|
2020-10-13 00:45:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2020-10-08 20:00:14 +00:00
|
|
|
namespace sk_gpu_test {
|
|
|
|
|
2020-10-13 00:45:06 +00:00
|
|
|
void ManagedBackendTexture::ReleaseProc(void* ctx) {
|
|
|
|
std::unique_ptr<Context> context(static_cast<Context*>(ctx));
|
|
|
|
if (context->fWrappedProc) {
|
|
|
|
context->fWrappedProc(context->fWrappedContext);
|
|
|
|
}
|
2020-10-08 20:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ManagedBackendTexture::~ManagedBackendTexture() {
|
|
|
|
if (fDContext && fTexture.isValid()) {
|
|
|
|
fDContext->deleteBackendTexture(fTexture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 00:45:06 +00:00
|
|
|
void* ManagedBackendTexture::releaseContext(GrGpuFinishedProc wrappedProc,
|
|
|
|
GrGpuFinishedContext wrappedCtx) const {
|
|
|
|
// Make sure we don't get a wrapped ctx without a wrapped proc
|
|
|
|
SkASSERT(!wrappedCtx || wrappedProc);
|
2020-10-16 14:05:21 +00:00
|
|
|
return new Context{wrappedProc, wrappedCtx, {sk_ref_sp(this)}};
|
|
|
|
}
|
|
|
|
|
|
|
|
void* ManagedBackendTexture::MakeYUVAReleaseContext(
|
|
|
|
const sk_sp<ManagedBackendTexture> mbets[SkYUVAInfo::kMaxPlanes]) {
|
|
|
|
auto context = new Context;
|
|
|
|
for (int i = 0; i < SkYUVAInfo::kMaxPlanes; ++i) {
|
|
|
|
context->fMBETs[i] = mbets[i];
|
|
|
|
}
|
|
|
|
return context;
|
2020-10-13 00:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<GrRefCntedCallback> ManagedBackendTexture::refCountedCallback() const {
|
2020-11-04 21:54:28 +00:00
|
|
|
return GrRefCntedCallback::Make(ReleaseProc, this->releaseContext());
|
2020-10-13 00:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ManagedBackendTexture::wasAdopted() { fTexture = {}; }
|
|
|
|
|
|
|
|
sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromInfo(GrDirectContext* dContext,
|
|
|
|
const SkImageInfo& ii,
|
|
|
|
GrMipmapped mipmapped,
|
|
|
|
GrRenderable renderable,
|
|
|
|
GrProtected isProtected) {
|
|
|
|
return MakeWithoutData(
|
|
|
|
dContext, ii.width(), ii.height(), ii.colorType(), mipmapped, renderable, isProtected);
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromBitmap(GrDirectContext* dContext,
|
2020-12-08 16:51:02 +00:00
|
|
|
const SkBitmap& src,
|
2020-10-13 00:45:06 +00:00
|
|
|
GrMipmapped mipmapped,
|
|
|
|
GrRenderable renderable,
|
|
|
|
GrProtected isProtected) {
|
2020-12-08 16:51:02 +00:00
|
|
|
SkPixmap srcPixmap;
|
|
|
|
if (!src.peekPixels(&srcPixmap)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MakeFromPixmap(dContext, srcPixmap, mipmapped, renderable, isProtected);
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromPixmap(GrDirectContext* dContext,
|
|
|
|
const SkPixmap& src,
|
|
|
|
GrMipmapped mipmapped,
|
|
|
|
GrRenderable renderable,
|
|
|
|
GrProtected isProtected) {
|
|
|
|
std::vector<SkPixmap> levels({src});
|
2020-10-13 00:45:06 +00:00
|
|
|
std::unique_ptr<SkMipmap> mm;
|
|
|
|
|
|
|
|
if (mipmapped == GrMipmapped::kYes) {
|
2020-12-08 16:51:02 +00:00
|
|
|
mm.reset(SkMipmap::Build(src, nullptr));
|
2020-10-13 00:45:06 +00:00
|
|
|
if (!mm) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < mm->countLevels(); ++i) {
|
|
|
|
SkMipmap::Level level;
|
|
|
|
SkAssertResult(mm->getLevel(i, &level));
|
|
|
|
levels.push_back(level.fPixmap);
|
|
|
|
}
|
|
|
|
}
|
2020-12-07 16:30:16 +00:00
|
|
|
return MakeWithData(dContext,
|
|
|
|
levels.data(),
|
|
|
|
static_cast<int>(levels.size()),
|
|
|
|
kTopLeft_GrSurfaceOrigin,
|
|
|
|
renderable,
|
|
|
|
isProtected);
|
2020-10-08 20:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sk_gpu_test
|