blob: 593fa16e56af37b04c819e0b14e2e4a5b205c6fa [file] [log] [blame]
Brian Salomonf9b00422020-10-08 16:00:14 -04001/*
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 Salomon72050802020-10-12 20:45:06 -040010#include "include/core/SkImageInfo.h"
11#include "include/private/GrTypesPriv.h"
12#include "src/core/SkMipmap.h"
13
14namespace {
15
16struct Context {
Brian Salomon72050802020-10-12 20:45:06 -040017 GrGpuFinishedProc fWrappedProc = nullptr;
18 GrGpuFinishedContext fWrappedContext = nullptr;
Brian Salomon7db71392020-10-16 10:05:21 -040019 sk_sp<sk_gpu_test::ManagedBackendTexture> fMBETs[SkYUVAInfo::kMaxPlanes];
Brian Salomon72050802020-10-12 20:45:06 -040020};
21
22} // anonymous namespace
23
Brian Salomonf9b00422020-10-08 16:00:14 -040024namespace sk_gpu_test {
25
Brian Salomon72050802020-10-12 20:45:06 -040026void 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 Salomonf9b00422020-10-08 16:00:14 -040031}
32
33ManagedBackendTexture::~ManagedBackendTexture() {
34 if (fDContext && fTexture.isValid()) {
35 fDContext->deleteBackendTexture(fTexture);
36 }
37}
38
Brian Salomon72050802020-10-12 20:45:06 -040039void* 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 Salomon7db71392020-10-16 10:05:21 -040043 return new Context{wrappedProc, wrappedCtx, {sk_ref_sp(this)}};
44}
45
46void* 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 Salomon72050802020-10-12 20:45:06 -040053}
54
55sk_sp<GrRefCntedCallback> ManagedBackendTexture::refCountedCallback() const {
Brian Salomon694ff172020-11-04 16:54:28 -050056 return GrRefCntedCallback::Make(ReleaseProc, this->releaseContext());
Brian Salomon72050802020-10-12 20:45:06 -040057}
58
59void ManagedBackendTexture::wasAdopted() { fTexture = {}; }
60
61sk_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
70sk_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 }
89 return MakeWithData(
90 dContext, levels.data(), static_cast<int>(levels.size()), renderable, isProtected);
Brian Salomonf9b00422020-10-08 16:00:14 -040091}
92
93} // namespace sk_gpu_test