blob: e80429a56ddfaa489ccad1b4be3e63ef53558570 [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;
Brian Salomon5fd10572019-04-01 12:07:05 -040023 GrPendingIOResource(T* resource) { this->reset(resource); }
Brian Salomon12d22642019-01-29 14:38:50 -050024 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; }
Brian Salomon5fd10572019-04-01 12:07:05 -040057 T* operator*() const { return *fResource; }
58 T* operator->() const { return fResource; }
Brian Salomonae5f9532018-07-31 11:03:40 -040059
60private:
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 Salomon12d22642019-01-29 14:38:50 -050078 T* fResource = nullptr;
Brian Salomonae5f9532018-07-31 11:03:40 -040079};
80
81#endif