blob: 192a57ca109e2b01b89e2e16b36535f374e97051 [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 Salomon839fb222020-10-16 13:30:54 +000017 sk_sp<sk_gpu_test::ManagedBackendTexture> fMBET;
Brian Salomon72050802020-10-12 20:45:06 -040018 GrGpuFinishedProc fWrappedProc = nullptr;
19 GrGpuFinishedContext fWrappedContext = nullptr;
20};
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 Salomon839fb222020-10-16 13:30:54 +000043 return new Context{sk_ref_sp(this), wrappedProc, wrappedCtx};
Brian Salomon72050802020-10-12 20:45:06 -040044}
45
46sk_sp<GrRefCntedCallback> ManagedBackendTexture::refCountedCallback() const {
47 return sk_make_sp<GrRefCntedCallback>(ReleaseProc, this->releaseContext());
48}
49
50void ManagedBackendTexture::wasAdopted() { fTexture = {}; }
51
52sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromInfo(GrDirectContext* dContext,
53 const SkImageInfo& ii,
54 GrMipmapped mipmapped,
55 GrRenderable renderable,
56 GrProtected isProtected) {
57 return MakeWithoutData(
58 dContext, ii.width(), ii.height(), ii.colorType(), mipmapped, renderable, isProtected);
59}
60
61sk_sp<ManagedBackendTexture> ManagedBackendTexture::MakeFromBitmap(GrDirectContext* dContext,
62 const SkBitmap& bitmap,
63 GrMipmapped mipmapped,
64 GrRenderable renderable,
65 GrProtected isProtected) {
66 std::vector<SkPixmap> levels({bitmap.pixmap()});
67 std::unique_ptr<SkMipmap> mm;
68
69 if (mipmapped == GrMipmapped::kYes) {
70 mm.reset(SkMipmap::Build(bitmap, nullptr));
71 if (!mm) {
72 return nullptr;
73 }
74 for (int i = 0; i < mm->countLevels(); ++i) {
75 SkMipmap::Level level;
76 SkAssertResult(mm->getLevel(i, &level));
77 levels.push_back(level.fPixmap);
78 }
79 }
80 return MakeWithData(
81 dContext, levels.data(), static_cast<int>(levels.size()), renderable, isProtected);
Brian Salomonf9b00422020-10-08 16:00:14 -040082}
83
84} // namespace sk_gpu_test