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