blob: 09340fe140f254b8c2992569b6fd95c54ffeb5fa [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkRefCnt.h"
12#include "include/gpu/GrGpuResource.h"
13#include "include/private/SkNoncopyable.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040014#include "src/gpu/GrSurfaceProxy.h"
Brian Salomonae5f9532018-07-31 11:03:40 -040015
Robert Phillips3d4cac52019-06-11 08:08:08 -040016class GrProxyPendingIO : SkNoncopyable {
17public:
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
42private:
43 bool operator==(const GrProxyPendingIO& other) const = delete;
44
45 GrSurfaceProxy* fResource = nullptr;
46};
47
Brian Salomonae5f9532018-07-31 11:03:40 -040048/**
49 * Helper for owning a pending read, write, read-write on a GrGpuResource. It never owns a regular
50 * ref.
51 */
52template <typename T, GrIOType IO_TYPE>
53class GrPendingIOResource : SkNoncopyable {
54public:
Brian Salomon12d22642019-01-29 14:38:50 -050055 GrPendingIOResource() = default;
Brian Salomon5fd10572019-04-01 12:07:05 -040056 GrPendingIOResource(T* resource) { this->reset(resource); }
Brian Salomon12d22642019-01-29 14:38:50 -050057 GrPendingIOResource(sk_sp<T> resource) { *this = std::move(resource); }
Brian Salomonae5f9532018-07-31 11:03:40 -040058 GrPendingIOResource(const GrPendingIOResource& that) : GrPendingIOResource(that.get()) {}
Brian Salomon12d22642019-01-29 14:38:50 -050059 ~GrPendingIOResource() { this->release(); }
60
61 GrPendingIOResource& operator=(sk_sp<T> resource) {
62 this->reset(resource.get());
63 return *this;
64 }
Brian Salomonae5f9532018-07-31 11:03:40 -040065
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 Salomonae5f9532018-07-31 11:03:40 -040085 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 Salomon5fd10572019-04-01 12:07:05 -040090 T* operator*() const { return *fResource; }
91 T* operator->() const { return fResource; }
Brian Salomonae5f9532018-07-31 11:03:40 -040092
93private:
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 Salomon12d22642019-01-29 14:38:50 -0500111 T* fResource = nullptr;
Brian Salomonae5f9532018-07-31 11:03:40 -0400112};
113
114#endif