blob: e56fa091b0249c0f1264a259672f8ac933f4ffeb [file] [log] [blame]
Brian Salomonae5f9532018-07-31 11:03:40 -04001/*
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 */
19template <typename T, GrIOType IO_TYPE>
20class GrPendingIOResource : SkNoncopyable {
21public:
Brian Salomon12d22642019-01-29 14:38:50 -050022 GrPendingIOResource() = default;
23 explicit GrPendingIOResource(T* resource) { this->reset(resource); }
24 GrPendingIOResource(sk_sp<T> resource) { *this = std::move(resource); }
Brian Salomonae5f9532018-07-31 11:03:40 -040025 GrPendingIOResource(const GrPendingIOResource& that) : GrPendingIOResource(that.get()) {}
Brian Salomon12d22642019-01-29 14:38:50 -050026 ~GrPendingIOResource() { this->release(); }
27
28 GrPendingIOResource& operator=(sk_sp<T> resource) {
29 this->reset(resource.get());
30 return *this;
31 }
Brian Salomonae5f9532018-07-31 11:03:40 -040032
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 Salomonae5f9532018-07-31 11:03:40 -040052 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; }
57
58private:
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
Brian Salomon12d22642019-01-29 14:38:50 -050076 T* fResource = nullptr;
Brian Salomonae5f9532018-07-31 11:03:40 -040077};
78
79#endif