Line data Source code
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 GrProgramElement_DEFINED
9 : #define GrProgramElement_DEFINED
10 :
11 : #include "../private/SkTArray.h"
12 : #include "SkRefCnt.h"
13 :
14 : class GrGpuResourceRef;
15 :
16 : /**
17 : * Note: We are converting GrProcessor from ref counting to a single owner model using move
18 : * semantics. This class will be removed.
19 : *
20 : * This is used to track "refs" for two separate types GrProcessor ownership. A regular ref is owned
21 : * by any client that may continue to issue draws that use the GrProgramElement. A recorded op or
22 : * GrPipeline uses "pending executions" instead of refs. A pending execution is cleared after the
23 : * draw is executed (or aborted).
24 : *
25 : * While a GrProgramElement is ref'ed any resources it owns are also ref'ed. However, once it gets
26 : * into the state where it has pending executions AND no refs then it converts its ownership of
27 : * its GrGpuResources from refs to pending IOs. The pending IOs allow the cache to track when it is
28 : * safe to recycle a resource even though we still have buffered GrOps that read or write to the
29 : * the resource.
30 : *
31 : * To make this work the subclass GrProcessor implements addPendingIOs, removeRefs, and
32 : * pendingIOComplete. addPendingIOs adds pending reads/writes to GrGpuResources owned by the
33 : * processor as appropriate when the processor is recorded in a GrOpList. removeRefs is called when
34 : * the ref count reaches 0 and the GrProcessor is only owned by "pending executions".
35 : * pendingIOComplete occurs if the resource is still owned by a ref but all recorded draws have been
36 : * completed. Whenever pending executions and refs reach zero the processor is deleted.
37 : *
38 : * The GrProcessor may also implement notifyRefCntIsZero in order to change its ownership of child
39 : * processors from ref to pending execution when the processor is first owned exclusively in pending
40 : * execution mode.
41 : */
42 : class GrProgramElement : public SkNoncopyable {
43 : public:
44 0 : virtual ~GrProgramElement() {
45 : // fRefCnt can be one when an effect is created statically using GR_CREATE_STATIC_EFFECT
46 0 : SkASSERT((0 == fRefCnt || 1 == fRefCnt) && 0 == fPendingExecutions);
47 : // Set to invalid values.
48 0 : SkDEBUGCODE(fRefCnt = fPendingExecutions = -10;)
49 0 : }
50 :
51 0 : void ref() const {
52 0 : this->validate();
53 : // Once the ref cnt reaches zero it should never be ref'ed again.
54 0 : SkASSERT(fRefCnt > 0);
55 0 : ++fRefCnt;
56 0 : this->validate();
57 0 : }
58 :
59 0 : void unref() const {
60 0 : this->validate();
61 0 : --fRefCnt;
62 0 : if (0 == fRefCnt) {
63 0 : this->notifyRefCntIsZero();
64 0 : if (0 == fPendingExecutions) {
65 0 : delete this;
66 0 : return;
67 : } else {
68 0 : this->removeRefs();
69 : }
70 : }
71 0 : this->validate();
72 : }
73 :
74 0 : void validate() const {
75 : #ifdef SK_DEBUG
76 0 : SkASSERT(fRefCnt >= 0);
77 0 : SkASSERT(fPendingExecutions >= 0);
78 0 : SkASSERT(fRefCnt + fPendingExecutions > 0);
79 : #endif
80 0 : }
81 :
82 : protected:
83 0 : GrProgramElement() : fRefCnt(1), fPendingExecutions(0) {}
84 :
85 0 : void addPendingExecution() const {
86 0 : this->validate();
87 0 : if (0 == fPendingExecutions) {
88 0 : this->addPendingIOs();
89 : }
90 0 : ++fPendingExecutions;
91 0 : this->validate();
92 0 : }
93 :
94 0 : void completedExecution() const {
95 0 : this->validate();
96 0 : --fPendingExecutions;
97 0 : if (0 == fPendingExecutions) {
98 0 : if (0 == fRefCnt) {
99 0 : delete this;
100 0 : return;
101 : } else {
102 0 : this->pendingIOComplete();
103 : }
104 : }
105 0 : this->validate();
106 : }
107 :
108 : private:
109 : virtual void addPendingIOs() const = 0;
110 : virtual void removeRefs() const = 0;
111 : virtual void pendingIOComplete() const = 0;
112 :
113 : /** This will be called when the ref cnt is zero. The object may or may not have pending
114 : executions. */
115 : virtual void notifyRefCntIsZero() const = 0;
116 :
117 : mutable int32_t fRefCnt;
118 : // Count of deferred executions not yet issued to the 3D API.
119 : mutable int32_t fPendingExecutions;
120 :
121 : // Only these classes can access addPendingExecution() and completedExecution().
122 : template <typename T> friend class GrPendingProgramElement;
123 : friend class GrProcessorSet;
124 :
125 : typedef SkNoncopyable INHERITED;
126 : };
127 :
128 : #endif
|