blob: 6b3937b5c73066e393a5110327ec1512e633d12f [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
bsalomonf96ba022014-09-17 08:05:40 -07008#ifndef GrGpuResourceRef_DEFINED
9#define GrGpuResourceRef_DEFINED
bsalomon95740982014-09-04 13:12:37 -070010
bsalomon45725db2014-09-19 11:48:02 -070011#include "GrGpuResource.h"
bsalomon95740982014-09-04 13:12:37 -070012#include "SkRefCnt.h"
13
bsalomon95740982014-09-04 13:12:37 -070014/**
bsalomonb3e3a952014-09-19 11:10:40 -070015 * This class is intended only for internal use in core Gr code.
16 *
bsalomon95740982014-09-04 13:12:37 -070017 * Class that wraps a resource referenced by a GrProgramElement or GrDrawState. It manages
bsalomonb3e3a952014-09-19 11:10:40 -070018 * converting refs to pending IO operations. It allows a resource ownership to be in three
19 * states:
20 * 1. Owns a single ref
21 * 2. Owns a single ref and a pending IO operation (read, write, or read-write)
22 * 3. Owns a single pending IO operation.
23 *
24 * It is legal to destroy the GrGpuResourceRef in any of these states. It starts in state
25 * 1. Calling markPendingIO() converts it from state 1 to state 2. Calling removeRef() goes from
26 * state 2 to state 3. Calling pendingIOComplete() moves from state 2 to state 1. There is no
27 * valid way of going from state 3 back to 2 or 1.
28 *
29 * Like SkAutoTUnref, its constructor and setter adopt a ref from their caller.
30 *
31 * TODO: Once GrDODrawState no longer exists and therefore GrDrawState and GrOptDrawState no
32 * longer share an instance of this class, attempt to make the resource owned by GrGpuResourceRef
33 * only settable via the constructor.
bsalomon95740982014-09-04 13:12:37 -070034 */
bsalomonf96ba022014-09-17 08:05:40 -070035class GrGpuResourceRef : SkNoncopyable {
bsalomon95740982014-09-04 13:12:37 -070036public:
bsalomonf96ba022014-09-17 08:05:40 -070037 SK_DECLARE_INST_COUNT_ROOT(GrGpuResourceRef);
bsalomonc4923342014-09-16 13:54:53 -070038
bsalomonf96ba022014-09-17 08:05:40 -070039 ~GrGpuResourceRef();
bsalomon95740982014-09-04 13:12:37 -070040
41 GrGpuResource* getResource() const { return fResource; }
42
bsalomon95740982014-09-04 13:12:37 -070043 /** Does this object own a pending read or write on the resource it is wrapping. */
44 bool ownsPendingIO() const { return fPendingIO; }
45
46 /** Shortcut for calling setResource() with NULL. It cannot be called after markingPendingIO
47 is called. */
48 void reset();
49
bsalomonc4923342014-09-16 13:54:53 -070050protected:
bsalomonf96ba022014-09-17 08:05:40 -070051 GrGpuResourceRef();
bsalomonc4923342014-09-16 13:54:53 -070052
53 /** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as
54 pending on the resource when markPendingIO is called. */
bsalomon45725db2014-09-19 11:48:02 -070055 GrGpuResourceRef(GrGpuResource*, GrIORef::IOType);
bsalomonc4923342014-09-16 13:54:53 -070056
57 /** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as
58 pending on the resource when markPendingIO is called. */
bsalomon45725db2014-09-19 11:48:02 -070059 void setResource(GrGpuResource*, GrIORef::IOType);
bsalomonc4923342014-09-16 13:54:53 -070060
bsalomon95740982014-09-04 13:12:37 -070061private:
62 /** Called by owning GrProgramElement when the program element is first scheduled for
bsalomonb3e3a952014-09-19 11:10:40 -070063 execution. It can only be called once. */
bsalomon95740982014-09-04 13:12:37 -070064 void markPendingIO() const;
65
66 /** Called when the program element/draw state is no longer owned by GrDrawTarget-client code.
67 This lets the cache know that the drawing code will no longer schedule additional reads or
bsalomonb3e3a952014-09-19 11:10:40 -070068 writes to the resource using the program element or draw state. It can only be called once.
69 */
bsalomon95740982014-09-04 13:12:37 -070070 void removeRef() const;
71
bsalomonac8d6192014-09-04 14:13:44 -070072 /** Called to indicate that the previous pending IO is complete. Useful when the owning object
bsalomonf96ba022014-09-17 08:05:40 -070073 still has refs, so it is not about to destroy this GrGpuResourceRef, but its previously
bsalomonb3e3a952014-09-19 11:10:40 -070074 pending executions have been complete. Can only be called if removeRef() was not previously
75 called. */
bsalomonac8d6192014-09-04 14:13:44 -070076 void pendingIOComplete() const;
77
78 friend class GrRODrawState;
bsalomon95740982014-09-04 13:12:37 -070079 friend class GrProgramElement;
80
81 GrGpuResource* fResource;
82 mutable bool fOwnRef;
83 mutable bool fPendingIO;
bsalomon45725db2014-09-19 11:48:02 -070084 GrIORef::IOType fIOType;
bsalomon95740982014-09-04 13:12:37 -070085
86 typedef SkNoncopyable INHERITED;
87};
88
bsalomonb3e3a952014-09-19 11:10:40 -070089/**
90 * Templated version of GrGpuResourceRef to enforce type safety.
91 */
bsalomonf96ba022014-09-17 08:05:40 -070092template <typename T> class GrTGpuResourceRef : public GrGpuResourceRef {
bsalomonc4923342014-09-16 13:54:53 -070093public:
bsalomonf96ba022014-09-17 08:05:40 -070094 GrTGpuResourceRef() {}
bsalomonc4923342014-09-16 13:54:53 -070095
96 /** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as
97 pending on the resource when markPendingIO is called. */
bsalomon45725db2014-09-19 11:48:02 -070098 GrTGpuResourceRef(T* resource, GrIORef::IOType ioType) : INHERITED(resource, ioType) {}
bsalomonc4923342014-09-16 13:54:53 -070099
100 T* get() const { return static_cast<T*>(this->getResource()); }
101
102 /** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as
103 pending on the resource when markPendingIO is called. */
bsalomon45725db2014-09-19 11:48:02 -0700104 void set(T* resource, GrIORef::IOType ioType) { this->setResource(resource, ioType); }
bsalomonf96ba022014-09-17 08:05:40 -0700105
106private:
107 typedef GrGpuResourceRef INHERITED;
bsalomonc4923342014-09-16 13:54:53 -0700108};
109
bsalomonb3e3a952014-09-19 11:10:40 -0700110/**
111 * This is similar to GrTGpuResourceRef but can only be in the pending IO state. It never owns a
112 * ref.
113 */
bsalomon45725db2014-09-19 11:48:02 -0700114template <typename T, GrIORef::IOType IO_TYPE> class GrPendingIOResource : SkNoncopyable {
bsalomonb3e3a952014-09-19 11:10:40 -0700115public:
bsalomon45725db2014-09-19 11:48:02 -0700116 GrPendingIOResource(T* resource) : fResource(resource) {
bsalomonb3e3a952014-09-19 11:10:40 -0700117 if (NULL != fResource) {
bsalomon45725db2014-09-19 11:48:02 -0700118 switch (IO_TYPE) {
119 case GrIORef::kRead_IOType:
bsalomonb3e3a952014-09-19 11:10:40 -0700120 fResource->addPendingRead();
121 break;
bsalomon45725db2014-09-19 11:48:02 -0700122 case GrIORef::kWrite_IOType:
bsalomonb3e3a952014-09-19 11:10:40 -0700123 fResource->addPendingWrite();
124 break;
bsalomon45725db2014-09-19 11:48:02 -0700125 case GrIORef::kRW_IOType:
bsalomonb3e3a952014-09-19 11:10:40 -0700126 fResource->addPendingRead();
127 fResource->addPendingWrite();
128 break;
129 }
130 }
131 }
bsalomonc4923342014-09-16 13:54:53 -0700132
bsalomonb3e3a952014-09-19 11:10:40 -0700133 ~GrPendingIOResource() {
134 if (NULL != fResource) {
bsalomon45725db2014-09-19 11:48:02 -0700135 switch (IO_TYPE) {
136 case GrIORef::kRead_IOType:
bsalomonb3e3a952014-09-19 11:10:40 -0700137 fResource->completedRead();
138 break;
bsalomon45725db2014-09-19 11:48:02 -0700139 case GrIORef::kWrite_IOType:
bsalomonb3e3a952014-09-19 11:10:40 -0700140 fResource->completedWrite();
141 break;
bsalomon45725db2014-09-19 11:48:02 -0700142 case GrIORef::kRW_IOType:
bsalomonb3e3a952014-09-19 11:10:40 -0700143 fResource->completedRead();
144 fResource->completedWrite();
145 break;
146 }
147 }
148 }
149
150 T* get() const { return fResource; }
151
152private:
153 T* fResource;
bsalomonb3e3a952014-09-19 11:10:40 -0700154};
bsalomon95740982014-09-04 13:12:37 -0700155#endif