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: |
| 22 | GrPendingIOResource(T* resource = nullptr) : fResource(nullptr) { this->reset(resource); } |
| 23 | |
| 24 | GrPendingIOResource(const GrPendingIOResource& that) : GrPendingIOResource(that.get()) {} |
| 25 | |
| 26 | void reset(T* resource = nullptr) { |
| 27 | if (resource) { |
| 28 | switch (IO_TYPE) { |
| 29 | case kRead_GrIOType: |
| 30 | resource->addPendingRead(); |
| 31 | break; |
| 32 | case kWrite_GrIOType: |
| 33 | resource->addPendingWrite(); |
| 34 | break; |
| 35 | case kRW_GrIOType: |
| 36 | resource->addPendingRead(); |
| 37 | resource->addPendingWrite(); |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | this->release(); |
| 42 | fResource = resource; |
| 43 | } |
| 44 | |
| 45 | ~GrPendingIOResource() { this->release(); } |
| 46 | |
| 47 | explicit operator bool() const { return SkToBool(fResource); } |
| 48 | |
| 49 | bool operator==(const GrPendingIOResource& other) const { return fResource == other.fResource; } |
| 50 | |
| 51 | T* get() const { return fResource; } |
| 52 | |
| 53 | private: |
| 54 | void release() { |
| 55 | if (fResource) { |
| 56 | switch (IO_TYPE) { |
| 57 | case kRead_GrIOType: |
| 58 | fResource->completedRead(); |
| 59 | break; |
| 60 | case kWrite_GrIOType: |
| 61 | fResource->completedWrite(); |
| 62 | break; |
| 63 | case kRW_GrIOType: |
| 64 | fResource->completedRead(); |
| 65 | fResource->completedWrite(); |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | T* fResource; |
| 72 | }; |
| 73 | |
| 74 | #endif |