2014-09-04 20:12:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GrProgramElement_DEFINED
|
|
|
|
#define GrProgramElement_DEFINED
|
|
|
|
|
2016-02-17 21:13:44 +00:00
|
|
|
#include "../private/SkTArray.h"
|
2014-09-04 20:12:37 +00:00
|
|
|
#include "SkRefCnt.h"
|
|
|
|
|
2014-09-17 15:05:40 +00:00
|
|
|
class GrGpuResourceRef;
|
2014-09-04 20:12:37 +00:00
|
|
|
|
|
|
|
/**
|
2015-08-27 23:43:48 +00:00
|
|
|
* Base class for GrProcessor. This exists to manage transitioning a GrProcessor from being owned by
|
|
|
|
* a client to being scheduled for execution. While a GrProcessor is ref'ed by drawing code its
|
|
|
|
* GrGpu resources must also be ref'ed to prevent incorrectly recycling them through the cache.
|
|
|
|
* However, once the GrProcessor is baked into a GrPipeline and the drawing code has stopped ref'ing
|
|
|
|
* it, it's internal resources can be recycled in some cases.
|
2014-09-04 20:12:37 +00:00
|
|
|
*
|
2015-08-27 23:43:48 +00:00
|
|
|
* We track this using two types of refs on GrProgramElement. A regular ref is owned by any client
|
|
|
|
* that may continue to issue draws that use the GrProgramElement. The GrPipeline owns "pending
|
|
|
|
* executions" instead of refs. A pending execution is cleared by ~GrPipeline().
|
|
|
|
*
|
|
|
|
* While a GrProgramElement is ref'ed any resources it owns are also ref'ed. However, once it gets
|
|
|
|
* into the state where it has pending executions AND no refs then it converts its ownership of
|
|
|
|
* its GrGpuResources from refs to pending IOs. The pending IOs allow the cache to track when it is
|
2016-12-21 16:14:46 +00:00
|
|
|
* safe to recycle a resource even though we still have buffered GrOps that read or write to the
|
2015-08-27 23:43:48 +00:00
|
|
|
* the resource.
|
|
|
|
*
|
|
|
|
* To make this work all GrGpuResource objects owned by a GrProgramElement or derived classes
|
|
|
|
* (either directly or indirectly) must be wrapped in a GrGpuResourceRef and registered with the
|
|
|
|
* GrProgramElement using addGpuResource(). This allows the regular refs to be converted to pending
|
|
|
|
* IO events when the program element is scheduled for deferred execution.
|
|
|
|
*
|
|
|
|
* Moreover, a GrProgramElement that in turn owns other GrProgramElements must convert its ownership
|
|
|
|
* of its children to pending executions when its ref count reaches zero so that the GrGpuResources
|
|
|
|
* owned by the children GrProgramElements are correctly converted from ownership by ref to
|
|
|
|
* ownership by pending IO. Any GrProgramElement hierarchy is managed by subclasses which must
|
|
|
|
* implement notifyRefCntIsZero() in order to convert refs of children to pending executions.
|
2014-09-04 20:12:37 +00:00
|
|
|
*/
|
|
|
|
class GrProgramElement : public SkNoncopyable {
|
|
|
|
public:
|
|
|
|
virtual ~GrProgramElement() {
|
|
|
|
// fRefCnt can be one when an effect is created statically using GR_CREATE_STATIC_EFFECT
|
|
|
|
SkASSERT((0 == fRefCnt || 1 == fRefCnt) && 0 == fPendingExecutions);
|
|
|
|
// Set to invalid values.
|
|
|
|
SkDEBUGCODE(fRefCnt = fPendingExecutions = -10;)
|
|
|
|
}
|
|
|
|
|
|
|
|
void ref() const {
|
2014-11-19 16:23:49 +00:00
|
|
|
this->validate();
|
2014-09-04 20:12:37 +00:00
|
|
|
// Once the ref cnt reaches zero it should never be ref'ed again.
|
|
|
|
SkASSERT(fRefCnt > 0);
|
|
|
|
++fRefCnt;
|
2014-11-19 16:23:49 +00:00
|
|
|
this->validate();
|
2014-09-04 20:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void unref() const {
|
|
|
|
this->validate();
|
|
|
|
--fRefCnt;
|
2014-10-03 12:31:41 +00:00
|
|
|
if (0 == fRefCnt) {
|
2015-08-27 23:43:48 +00:00
|
|
|
this->notifyRefCntIsZero();
|
2014-10-03 12:31:41 +00:00
|
|
|
if (0 == fPendingExecutions) {
|
2015-08-26 20:07:48 +00:00
|
|
|
delete this;
|
2014-11-19 16:23:49 +00:00
|
|
|
return;
|
2014-10-03 12:31:41 +00:00
|
|
|
} else {
|
|
|
|
this->removeRefs();
|
|
|
|
}
|
2014-09-04 20:12:37 +00:00
|
|
|
}
|
2014-11-19 16:23:49 +00:00
|
|
|
this->validate();
|
2014-09-04 20:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void validate() const {
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
SkASSERT(fRefCnt >= 0);
|
|
|
|
SkASSERT(fPendingExecutions >= 0);
|
|
|
|
SkASSERT(fRefCnt + fPendingExecutions > 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2017-01-09 15:14:34 +00:00
|
|
|
GrProgramElement() : fRefCnt(1), fPendingExecutions(0) {}
|
2014-09-04 20:12:37 +00:00
|
|
|
|
|
|
|
/** Subclasses registers their resources using this function. It is assumed the GrProgramResouce
|
|
|
|
is and will remain owned by the subclass and this function will retain a raw ptr. Once a
|
2014-09-17 15:05:40 +00:00
|
|
|
GrGpuResourceRef is registered its setResource must not be called.
|
2014-09-04 20:12:37 +00:00
|
|
|
*/
|
2014-09-17 15:05:40 +00:00
|
|
|
void addGpuResource(const GrGpuResourceRef* res) {
|
|
|
|
fGpuResources.push_back(res);
|
2014-09-04 20:12:37 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 16:23:49 +00:00
|
|
|
void addPendingExecution() const {
|
|
|
|
this->validate();
|
|
|
|
SkASSERT(fRefCnt > 0);
|
|
|
|
if (0 == fPendingExecutions) {
|
|
|
|
this->addPendingIOs();
|
|
|
|
}
|
|
|
|
++fPendingExecutions;
|
|
|
|
this->validate();
|
|
|
|
}
|
2014-09-04 20:12:37 +00:00
|
|
|
|
2014-11-19 16:23:49 +00:00
|
|
|
void completedExecution() const {
|
|
|
|
this->validate();
|
|
|
|
--fPendingExecutions;
|
|
|
|
if (0 == fPendingExecutions) {
|
|
|
|
if (0 == fRefCnt) {
|
2015-08-26 20:07:48 +00:00
|
|
|
delete this;
|
2014-11-19 16:23:49 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
this->pendingIOComplete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this->validate();
|
|
|
|
}
|
2014-09-04 20:12:37 +00:00
|
|
|
|
2015-08-27 23:43:48 +00:00
|
|
|
private:
|
|
|
|
/** This will be called when the ref cnt is zero. The object may or may not have pending
|
|
|
|
executions. */
|
|
|
|
virtual void notifyRefCntIsZero() const = 0;
|
|
|
|
|
2014-10-03 12:31:41 +00:00
|
|
|
void removeRefs() const;
|
2014-11-19 16:23:49 +00:00
|
|
|
void addPendingIOs() const;
|
|
|
|
void pendingIOComplete() const;
|
2014-10-03 12:31:41 +00:00
|
|
|
|
2014-09-04 20:12:37 +00:00
|
|
|
mutable int32_t fRefCnt;
|
|
|
|
// Count of deferred executions not yet issued to the 3D API.
|
|
|
|
mutable int32_t fPendingExecutions;
|
|
|
|
|
2014-09-17 15:05:40 +00:00
|
|
|
SkSTArray<4, const GrGpuResourceRef*, true> fGpuResources;
|
2014-09-04 20:12:37 +00:00
|
|
|
|
2014-11-19 16:23:49 +00:00
|
|
|
// Only this class can access addPendingExecution() and completedExecution().
|
|
|
|
template <typename T> friend class GrPendingProgramElement;
|
2014-09-04 20:12:37 +00:00
|
|
|
|
|
|
|
typedef SkNoncopyable INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|