• 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 "GrGpuResource.h"
12 #include "SkNoncopyable.h"
13 #include "SkRefCnt.h"
14 
15 /**
16  * Helper for owning a pending read, write, read-write on a GrGpuResource. It never owns a regular
17  * ref.
18  */
19 template <typename T, GrIOType IO_TYPE>
20 class GrPendingIOResource : SkNoncopyable {
21 public:
22     GrPendingIOResource() = default;
GrPendingIOResource(T * resource)23     explicit GrPendingIOResource(T* resource) { this->reset(resource); }
GrPendingIOResource(sk_sp<T> resource)24     GrPendingIOResource(sk_sp<T> resource) { *this = std::move(resource); }
GrPendingIOResource(const GrPendingIOResource & that)25     GrPendingIOResource(const GrPendingIOResource& that) : GrPendingIOResource(that.get()) {}
~GrPendingIOResource()26     ~GrPendingIOResource() { this->release(); }
27 
28     GrPendingIOResource& operator=(sk_sp<T> resource) {
29         this->reset(resource.get());
30         return *this;
31     }
32 
33     void reset(T* resource = nullptr) {
34         if (resource) {
35             switch (IO_TYPE) {
36                 case kRead_GrIOType:
37                     resource->addPendingRead();
38                     break;
39                 case kWrite_GrIOType:
40                     resource->addPendingWrite();
41                     break;
42                 case kRW_GrIOType:
43                     resource->addPendingRead();
44                     resource->addPendingWrite();
45                     break;
46             }
47         }
48         this->release();
49         fResource = resource;
50     }
51 
52     explicit operator bool() const { return SkToBool(fResource); }
53 
54     bool operator==(const GrPendingIOResource& other) const { return fResource == other.fResource; }
55 
get()56     T* get() const { return fResource; }
57 
58 private:
release()59     void release() {
60         if (fResource) {
61             switch (IO_TYPE) {
62                 case kRead_GrIOType:
63                     fResource->completedRead();
64                     break;
65                 case kWrite_GrIOType:
66                     fResource->completedWrite();
67                     break;
68                 case kRW_GrIOType:
69                     fResource->completedRead();
70                     fResource->completedWrite();
71                     break;
72             }
73         }
74     }
75 
76     T* fResource = nullptr;
77 };
78 
79 #endif
80