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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkRefCnt.h" |
| 12 | #include "include/gpu/GrGpuResource.h" |
| 13 | #include "include/private/SkNoncopyable.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame^] | 14 | #include "src/gpu/GrSurfaceProxy.h" |
Brian Salomon | ae5f953 | 2018-07-31 11:03:40 -0400 | [diff] [blame] | 15 | |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 16 | class GrProxyPendingIO : SkNoncopyable { |
| 17 | public: |
| 18 | GrProxyPendingIO() = default; |
| 19 | GrProxyPendingIO(GrSurfaceProxy* resource) { this->reset(resource); } |
| 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 | |
| 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 | |
Brian Salomon | ae5f953 | 2018-07-31 11:03:40 -0400 | [diff] [blame] | 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: |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 55 | GrPendingIOResource() = default; |
Brian Salomon | 5fd1057 | 2019-04-01 12:07:05 -0400 | [diff] [blame] | 56 | GrPendingIOResource(T* resource) { this->reset(resource); } |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 57 | GrPendingIOResource(sk_sp<T> resource) { *this = std::move(resource); } |
Brian Salomon | ae5f953 | 2018-07-31 11:03:40 -0400 | [diff] [blame] | 58 | GrPendingIOResource(const GrPendingIOResource& that) : GrPendingIOResource(that.get()) {} |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 59 | ~GrPendingIOResource() { this->release(); } |
| 60 | |
| 61 | GrPendingIOResource& operator=(sk_sp<T> resource) { |
| 62 | this->reset(resource.get()); |
| 63 | return *this; |
| 64 | } |
Brian Salomon | ae5f953 | 2018-07-31 11:03:40 -0400 | [diff] [blame] | 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 | |
Brian Salomon | ae5f953 | 2018-07-31 11:03:40 -0400 | [diff] [blame] | 85 | explicit operator bool() const { return SkToBool(fResource); } |
| 86 | |
| 87 | bool operator==(const GrPendingIOResource& other) const { return fResource == other.fResource; } |
| 88 | |
| 89 | T* get() const { return fResource; } |
Brian Salomon | 5fd1057 | 2019-04-01 12:07:05 -0400 | [diff] [blame] | 90 | T* operator*() const { return *fResource; } |
| 91 | T* operator->() const { return fResource; } |
Brian Salomon | ae5f953 | 2018-07-31 11:03:40 -0400 | [diff] [blame] | 92 | |
| 93 | private: |
| 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 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 111 | T* fResource = nullptr; |
Brian Salomon | ae5f953 | 2018-07-31 11:03:40 -0400 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | #endif |