blob: 061ad89b3163897e6a44f482b6dd982dcd5488bf [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
Mike Reedac9f0c92020-12-23 10:11:33 -050010#include "include/core/SkBitmap.h"
Brian Salomon72050802020-10-12 20:45:06 -040011#include "include/core/SkImageInfo.h"
12#include "include/private/GrTypesPriv.h"
13#include "src/core/SkMipmap.h"
14
15namespace {
16
17struct Context {
Brian Salomon72050802020-10-12 20:45:06 -040018 GrGpuFinishedProc fWrappedProc = nullptr;
19 GrGpuFinishedContext fWrappedContext = nullptr;
Brian Salomon7db71392020-10-16 10:05:21 -040020 sk_sp<sk_gpu_test::ManagedBackendTexture> fMBETs[SkYUVAInfo::kMaxPlanes];
Brian Salomon72050802020-10-12 20:45:06 -040021};
22
23} // anonymous namespace
24
Brian Salomonf9b00422020-10-08 16:00:14 -040025namespace sk_gpu_test {
26
Brian Salomon72050802020-10-12 20:45:06 -040027void ManagedBackendTexture::ReleaseProc(void* ctx) {
28 std::unique_ptr<Context> context(static_cast<Context*>(ctx));
29 if (context->fWrappedProc) {
30 context->fWrappedProc(context->fWrappedContext);
31 }
Brian Salomonf9b00422020-10-08 16:00:14 -040032}
33
34ManagedBackendTexture::~ManagedBackendTexture() {
35 if (fDContext && fTexture.isValid()) {
36 fDContext->deleteBackendTexture(fTexture);
37 }
38}
39
Brian Salomon72050802020-10-12 20:45:06 -040040void* ManagedBackendTexture::releaseContext(GrGpuFinishedProc wrappedProc,
41 GrGpuFinishedContext wrappedCtx) const {
42 // Make sure we don't get a wrapped ctx without a wrapped proc
43 SkASSERT(!wrappedCtx || wrappedProc);
Brian Salomon7db71392020-10-16 10:05:21 -040044 return new Context{wrappedProc, wrappedCtx, {sk_ref_sp(this)}};
45}
46
47void* ManagedBackendTexture::MakeYUVAReleaseContext(
48 const sk_sp<ManagedBackendTexture> mbets[SkYUVAInfo::kMaxPlanes]) {
49 auto context = new Context;
50 for (int i = 0; i < SkYUVAInfo::kMaxPlanes; ++i) {
51 context->fMBETs[i] = mbets[i];
52 }
53 return context;
Brian Salomon72050802020-10-12 20:45:06 -040054}
55
56sk_sp<GrRefCntedCallback> ManagedBackendTexture::refCountedCallback() const {
Brian Salomon694ff172020-11-04 16:54:28 -050057 return GrRefCntedCallback::Make(ReleaseProc, this->releaseContext());
Brian Salomon72050802020-10-12 20:45:06 -040058}
59
60void ManagedBackendTexture::wasAdopted() { fTexture = {}; }
61
62sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromInfo(GrDirectContext* dContext,
63 const SkImageInfo& ii,
64 GrMipmapped mipmapped,
65 GrRenderable renderable,
66 GrProtected isProtected) {
67 return MakeWithoutData(
68 dContext, ii.width(), ii.height(), ii.colorType(), mipmapped, renderable, isProtected);
69}
70
71sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromBitmap(GrDirectContext* dContext,
Robert Phillipsb87975c2020-12-08 11:51:02 -050072 const SkBitmap& src,
Brian Salomon72050802020-10-12 20:45:06 -040073 GrMipmapped mipmapped,
74 GrRenderable renderable,
75 GrProtected isProtected) {
Robert Phillipsb87975c2020-12-08 11:51:02 -050076 SkPixmap srcPixmap;
77 if (!src.peekPixels(&srcPixmap)) {
78 return nullptr;
79 }
80
81 return MakeFromPixmap(dContext, srcPixmap, mipmapped, renderable, isProtected);
82}
83
84sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromPixmap(GrDirectContext* dContext,
85 const SkPixmap& src,
86 GrMipmapped mipmapped,
87 GrRenderable renderable,
88 GrProtected isProtected) {
89 std::vector<SkPixmap> levels({src});
Brian Salomon72050802020-10-12 20:45:06 -040090 std::unique_ptr<SkMipmap> mm;
91
92 if (mipmapped == GrMipmapped::kYes) {
Robert Phillipsb87975c2020-12-08 11:51:02 -050093 mm.reset(SkMipmap::Build(src, nullptr));
Brian Salomon72050802020-10-12 20:45:06 -040094 if (!mm) {
95 return nullptr;
96 }
97 for (int i = 0; i < mm->countLevels(); ++i) {
98 SkMipmap::Level level;
99 SkAssertResult(mm->getLevel(i, &level));
100 levels.push_back(level.fPixmap);
101 }
102 }
Brian Salomonb5f880a2020-12-07 11:30:16 -0500103 return MakeWithData(dContext,
104 levels.data(),
105 static_cast<int>(levels.size()),
106 kTopLeft_GrSurfaceOrigin,
107 renderable,
108 isProtected);
Brian Salomonf9b00422020-10-08 16:00:14 -0400109}
110
111} // namespace sk_gpu_test