Brian Salomon | ae5f953 | 2018-07-31 11:03:40 -0400 | [diff] [blame] | 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: |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 22 | GrPendingIOResource() = default; |
Brian Salomon | 5fd1057 | 2019-04-01 12:07:05 -0400 | [diff] [blame^] | 23 | GrPendingIOResource(T* resource) { this->reset(resource); } |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 24 | GrPendingIOResource(sk_sp<T> resource) { *this = std::move(resource); } |
Brian Salomon | ae5f953 | 2018-07-31 11:03:40 -0400 | [diff] [blame] | 25 | GrPendingIOResource(const GrPendingIOResource& that) : GrPendingIOResource(that.get()) {} |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 26 | ~GrPendingIOResource() { this->release(); } |
| 27 | |
| 28 | GrPendingIOResource& operator=(sk_sp<T> resource) { |
| 29 | this->reset(resource.get()); |
| 30 | return *this; |
| 31 | } |
Brian Salomon | ae5f953 | 2018-07-31 11:03:40 -0400 | [diff] [blame] | 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 | |
Brian Salomon | ae5f953 | 2018-07-31 11:03:40 -0400 | [diff] [blame] | 52 | explicit operator bool() const { return SkToBool(fResource); } |
| 53 | |
| 54 | bool operator==(const GrPendingIOResource& other) const { return fResource == other.fResource; } |
| 55 | |
| 56 | T* get() const { return fResource; } |
Brian Salomon | 5fd1057 | 2019-04-01 12:07:05 -0400 | [diff] [blame^] | 57 | T* operator*() const { return *fResource; } |
| 58 | T* operator->() const { return fResource; } |
Brian Salomon | ae5f953 | 2018-07-31 11:03:40 -0400 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | void release() { |
| 62 | if (fResource) { |
| 63 | switch (IO_TYPE) { |
| 64 | case kRead_GrIOType: |
| 65 | fResource->completedRead(); |
| 66 | break; |
| 67 | case kWrite_GrIOType: |
| 68 | fResource->completedWrite(); |
| 69 | break; |
| 70 | case kRW_GrIOType: |
| 71 | fResource->completedRead(); |
| 72 | fResource->completedWrite(); |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 78 | T* fResource = nullptr; |
Brian Salomon | ae5f953 | 2018-07-31 11:03:40 -0400 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | #endif |