• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include "include/core/SkRefCnt.h"
12 #include "include/gpu/GpuTypes.h"
13 
14 namespace skgpu {
15 /**
16  * Ref-counted object that calls a callback from its destructor.
17  */
18 class RefCntedCallback : public SkNVRefCnt<RefCntedCallback> {
19 public:
20     using Context = void*;
21     using Callback = void (*)(Context);
22     using ResultCallback = void (*)(Context, CallbackResult);
23 
Make(Callback proc,Context ctx)24     static sk_sp<RefCntedCallback> Make(Callback proc, Context ctx) {
25         if (!proc) {
26             return nullptr;
27         }
28         return sk_sp<RefCntedCallback>(new RefCntedCallback(proc, ctx));
29     }
30 
Make(ResultCallback proc,Context ctx)31     static sk_sp<RefCntedCallback> Make(ResultCallback proc, Context ctx) {
32         if (!proc) {
33             return nullptr;
34         }
35         return sk_sp<RefCntedCallback>(new RefCntedCallback(proc, ctx));
36     }
37 
~RefCntedCallback()38     ~RefCntedCallback() {
39         if (fReleaseProc) {
40             SkASSERT(!fResultReleaseProc);
41             fReleaseProc(fReleaseCtx);
42         } else {
43             SkASSERT(fResultReleaseProc);
44             fResultReleaseProc(fReleaseCtx, fResult);
45         }
46     }
47 
context()48     Context context() const { return fReleaseCtx; }
49 
setFailureResult()50     void setFailureResult() {
51         SkASSERT(fResultReleaseProc);
52         // Shouldn't really be calling this multiple times.
53         SkASSERT(fResult == CallbackResult::kSuccess);
54         fResult = CallbackResult::kFailed;
55     }
56 
57 private:
RefCntedCallback(Callback proc,Context ctx)58     RefCntedCallback(Callback proc, Context ctx) : fReleaseProc(proc), fReleaseCtx(ctx) {}
RefCntedCallback(ResultCallback proc,Context 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;
69     CallbackResult fResult = CallbackResult::kSuccess;
70 };
71 
72 } // namespace skgpu
73 
74 #endif // skgpu_RefCntedCallback_DEFINED
75 
76