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
|
|
|
|
|
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkTArray.h"
|
|
|
|
|
2014-09-17 15:05:40 +00:00
|
|
|
class GrGpuResourceRef;
|
2014-09-04 20:12:37 +00:00
|
|
|
|
|
|
|
/**
|
2014-09-23 16:50:21 +00:00
|
|
|
* Base class for GrProcessor. GrDrawState uses this to manage
|
|
|
|
* transitioning a GrProcessor from being owned by a client to being scheduled for execution. It
|
2014-09-04 20:12:37 +00:00
|
|
|
* converts resources owned by the effect from being ref'ed to having pending reads/writes.
|
|
|
|
*
|
|
|
|
* All GrGpuResource objects owned by a GrProgramElement or derived classes (either directly or
|
2014-09-17 15:05:40 +00:00
|
|
|
* 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
|
2014-09-04 20:12:37 +00:00
|
|
|
* when the program element is scheduled for deferred execution.
|
|
|
|
*/
|
|
|
|
class GrProgramElement : public SkNoncopyable {
|
|
|
|
public:
|
2015-01-13 16:22:43 +00:00
|
|
|
SK_DECLARE_INST_COUNT(GrProgramElement)
|
2014-09-04 20:12:37 +00:00
|
|
|
|
|
|
|
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) {
|
|
|
|
if (0 == fPendingExecutions) {
|
|
|
|
SkDELETE(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
|
|
|
}
|
|
|
|
|
2014-09-05 19:23:12 +00:00
|
|
|
/**
|
|
|
|
* Gets an id that is unique for this GrProgramElement object. This will never return 0.
|
|
|
|
*/
|
|
|
|
uint32_t getUniqueID() const { return fUniqueID; }
|
|
|
|
|
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:
|
2014-09-05 19:23:12 +00:00
|
|
|
GrProgramElement() : fRefCnt(1), fPendingExecutions(0), fUniqueID(CreateUniqueID()) {}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-09-05 19:23:12 +00:00
|
|
|
static uint32_t CreateUniqueID();
|
|
|
|
|
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) {
|
|
|
|
SkDELETE(this);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
this->pendingIOComplete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this->validate();
|
|
|
|
}
|
2014-09-04 20:12:37 +00:00
|
|
|
|
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-05 19:23:12 +00:00
|
|
|
uint32_t fUniqueID;
|
2014-09-04 20:12:37 +00:00
|
|
|
|
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
|