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