blob: 2cdd1cc5635014e81d3362b4fb8ca3a0600abca7 [file] [log] [blame]
bsalomon95740982014-09-04 13:12:37 -07001/*
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 "SkRefCnt.h"
12#include "SkTArray.h"
13
bsalomonf96ba022014-09-17 08:05:40 -070014class GrGpuResourceRef;
bsalomon95740982014-09-04 13:12:37 -070015
16/**
joshualittb0a8a372014-09-23 09:50:21 -070017 * Base class for GrProcessor. GrDrawState uses this to manage
18 * transitioning a GrProcessor from being owned by a client to being scheduled for execution. It
bsalomon95740982014-09-04 13:12:37 -070019 * converts resources owned by the effect from being ref'ed to having pending reads/writes.
20 *
21 * All GrGpuResource objects owned by a GrProgramElement or derived classes (either directly or
bsalomonf96ba022014-09-17 08:05:40 -070022 * indirectly) must be wrapped in a GrGpuResourceRef and registered with the GrProgramElement using
23 * addGpuResource(). This allows the regular refs to be converted to pending IO events
bsalomon95740982014-09-04 13:12:37 -070024 * when the program element is scheduled for deferred execution.
25 */
26class GrProgramElement : public SkNoncopyable {
27public:
28 SK_DECLARE_INST_COUNT_ROOT(GrProgramElement)
29
30 virtual ~GrProgramElement() {
31 // fRefCnt can be one when an effect is created statically using GR_CREATE_STATIC_EFFECT
32 SkASSERT((0 == fRefCnt || 1 == fRefCnt) && 0 == fPendingExecutions);
33 // Set to invalid values.
34 SkDEBUGCODE(fRefCnt = fPendingExecutions = -10;)
35 }
36
37 void ref() const {
38 // Once the ref cnt reaches zero it should never be ref'ed again.
39 SkASSERT(fRefCnt > 0);
40 this->validate();
41 ++fRefCnt;
42 }
43
44 void unref() const {
45 this->validate();
46 --fRefCnt;
bsalomond0128772014-10-03 05:31:41 -070047 if (0 == fRefCnt) {
48 if (0 == fPendingExecutions) {
49 SkDELETE(this);
50 } else {
51 this->removeRefs();
52 }
bsalomon95740982014-09-04 13:12:37 -070053 }
54 }
55
bsalomon52e9d632014-09-05 12:23:12 -070056 /**
57 * Gets an id that is unique for this GrProgramElement object. This will never return 0.
58 */
59 uint32_t getUniqueID() const { return fUniqueID; }
60
bsalomon95740982014-09-04 13:12:37 -070061 void validate() const {
62#ifdef SK_DEBUG
63 SkASSERT(fRefCnt >= 0);
64 SkASSERT(fPendingExecutions >= 0);
65 SkASSERT(fRefCnt + fPendingExecutions > 0);
66#endif
67 }
68
69protected:
bsalomon52e9d632014-09-05 12:23:12 -070070 GrProgramElement() : fRefCnt(1), fPendingExecutions(0), fUniqueID(CreateUniqueID()) {}
bsalomon95740982014-09-04 13:12:37 -070071
72 /** Subclasses registers their resources using this function. It is assumed the GrProgramResouce
73 is and will remain owned by the subclass and this function will retain a raw ptr. Once a
bsalomonf96ba022014-09-17 08:05:40 -070074 GrGpuResourceRef is registered its setResource must not be called.
bsalomon95740982014-09-04 13:12:37 -070075 */
bsalomonf96ba022014-09-17 08:05:40 -070076 void addGpuResource(const GrGpuResourceRef* res) {
77 fGpuResources.push_back(res);
bsalomon95740982014-09-04 13:12:37 -070078 }
79
80private:
bsalomon52e9d632014-09-05 12:23:12 -070081 static uint32_t CreateUniqueID();
82
bsalomon95740982014-09-04 13:12:37 -070083 void convertRefToPendingExecution() const;
84
bsalomonac8d6192014-09-04 14:13:44 -070085 void completedExecution() const;
bsalomon95740982014-09-04 13:12:37 -070086
bsalomond0128772014-10-03 05:31:41 -070087 void removeRefs() const;
88
bsalomon95740982014-09-04 13:12:37 -070089 mutable int32_t fRefCnt;
90 // Count of deferred executions not yet issued to the 3D API.
91 mutable int32_t fPendingExecutions;
bsalomon52e9d632014-09-05 12:23:12 -070092 uint32_t fUniqueID;
bsalomon95740982014-09-04 13:12:37 -070093
bsalomonf96ba022014-09-17 08:05:40 -070094 SkSTArray<4, const GrGpuResourceRef*, true> fGpuResources;
bsalomon95740982014-09-04 13:12:37 -070095
96 // Only this class can access convertRefToPendingExecution() and completedExecution().
97 template <typename T> friend class GrProgramElementRef;
98
99 typedef SkNoncopyable INHERITED;
100};
101
102#endif