• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 Google Inc.
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 GrPendingIOResource_DEFINED
9 #define GrPendingIOResource_DEFINED
10 
11 #include "include/core/SkRefCnt.h"
12 #include "include/gpu/GrGpuResource.h"
13 #include "include/private/SkNoncopyable.h"
14 #include "src/gpu/GrSurfaceProxy.h"
15 
16 class GrProxyPendingIO : SkNoncopyable {
17 public:
18     GrProxyPendingIO() = default;
GrProxyPendingIO(GrSurfaceProxy * resource)19     GrProxyPendingIO(GrSurfaceProxy* resource) { this->reset(resource); }
~GrProxyPendingIO()20     ~GrProxyPendingIO() { this->reset(nullptr); }
21 
22     void reset(GrSurfaceProxy* resource = nullptr) {
23         if (resource == fResource) {
24             return;
25         }
26 
27         if (fResource) {
28             fResource->unref();
29         }
30 
31         fResource = resource;
32         if (fResource) {
33             fResource->ref();
34         }
35     }
36 
37     explicit operator bool() const { return SkToBool(fResource); }
38 
get()39     GrSurfaceProxy* get() const { return fResource; }
40     GrSurfaceProxy* operator->() const { return fResource; }
41 
42 private:
43     bool operator==(const GrProxyPendingIO& other) const = delete;
44 
45     GrSurfaceProxy* fResource = nullptr;
46 };
47 
48 /**
49  * Helper for owning a pending read, write, read-write on a GrGpuResource. It never owns a regular
50  * ref.
51  */
52 template <typename T, GrIOType IO_TYPE>
53 class GrPendingIOResource : SkNoncopyable {
54 public:
55     GrPendingIOResource() = default;
GrPendingIOResource(T * resource)56     GrPendingIOResource(T* resource) { this->reset(resource); }
GrPendingIOResource(sk_sp<T> resource)57     GrPendingIOResource(sk_sp<T> resource) { *this = std::move(resource); }
GrPendingIOResource(const GrPendingIOResource & that)58     GrPendingIOResource(const GrPendingIOResource& that) : GrPendingIOResource(that.get()) {}
~GrPendingIOResource()59     ~GrPendingIOResource() { this->release(); }
60 
61     GrPendingIOResource& operator=(sk_sp<T> resource) {
62         this->reset(resource.get());
63         return *this;
64     }
65 
66     void reset(T* resource = nullptr) {
67         if (resource) {
68             switch (IO_TYPE) {
69                 case kRead_GrIOType:
70                     resource->addPendingRead();
71                     break;
72                 case kWrite_GrIOType:
73                     resource->addPendingWrite();
74                     break;
75                 case kRW_GrIOType:
76                     resource->addPendingRead();
77                     resource->addPendingWrite();
78                     break;
79             }
80         }
81         this->release();
82         fResource = resource;
83     }
84 
85     explicit operator bool() const { return SkToBool(fResource); }
86 
87     bool operator==(const GrPendingIOResource& other) const { return fResource == other.fResource; }
88 
get()89     T* get() const { return fResource; }
90     T* operator*() const { return *fResource; }
91     T* operator->() const { return fResource; }
92 
93 private:
release()94     void release() {
95         if (fResource) {
96             switch (IO_TYPE) {
97                 case kRead_GrIOType:
98                     fResource->completedRead();
99                     break;
100                 case kWrite_GrIOType:
101                     fResource->completedWrite();
102                     break;
103                 case kRW_GrIOType:
104                     fResource->completedRead();
105                     fResource->completedWrite();
106                     break;
107             }
108         }
109     }
110 
111     T* fResource = nullptr;
112 };
113 
114 #endif
115