Flutter Engine
The Flutter Engine
RefCntedCallback.h
Go to the documentation of this file.
1/*
2 * Copyright 2022 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#ifndef skgpu_RefCntedCallback_DEFINED
9#define skgpu_RefCntedCallback_DEFINED
10
13
14namespace skgpu {
15/**
16 * Ref-counted object that calls a callback from its destructor.
17 */
18class RefCntedCallback : public SkNVRefCnt<RefCntedCallback> {
19public:
20 using Context = void*;
21 using Callback = void (*)(Context);
23
25 if (!proc) {
26 return nullptr;
27 }
28 return sk_sp<RefCntedCallback>(new RefCntedCallback(proc, ctx));
29 }
30
32 if (!proc) {
33 return nullptr;
34 }
35 return sk_sp<RefCntedCallback>(new RefCntedCallback(proc, ctx));
36 }
37
39 if (fReleaseProc) {
40 SkASSERT(!fResultReleaseProc);
41 fReleaseProc(fReleaseCtx);
42 } else {
43 SkASSERT(fResultReleaseProc);
44 fResultReleaseProc(fReleaseCtx, fResult);
45 }
46 }
47
48 Context context() const { return fReleaseCtx; }
49
51 SkASSERT(fResultReleaseProc);
52 // Shouldn't really be calling this multiple times.
55 }
56
57private:
58 RefCntedCallback(Callback proc, Context ctx) : fReleaseProc(proc), fReleaseCtx(ctx) {}
59 RefCntedCallback(ResultCallback proc, Context ctx)
60 : fResultReleaseProc(proc), fReleaseCtx(ctx) {}
61 RefCntedCallback(const RefCntedCallback&) = delete;
62 RefCntedCallback(RefCntedCallback&&) = delete;
63 RefCntedCallback& operator=(const RefCntedCallback&) = delete;
64 RefCntedCallback& operator=(RefCntedCallback&&) = delete;
65
66 Callback fReleaseProc = nullptr;
67 ResultCallback fResultReleaseProc = nullptr;
68 Context fReleaseCtx;
70};
71
72} // namespace skgpu
73
74#endif // skgpu_RefCntedCallback_DEFINED
75
#define SkASSERT(cond)
Definition: SkAssert.h:116
void(*)(Context, CallbackResult) ResultCallback
static sk_sp< RefCntedCallback > Make(Callback proc, Context ctx)
static sk_sp< RefCntedCallback > Make(ResultCallback proc, Context ctx)
void(*)(Context) Callback
Definition: GpuTools.h:21
CallbackResult
Definition: GpuTypes.h:45