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.
|
|
|
|
*/
|
|
|
|
|
2014-09-17 15:05:40 +00:00
|
|
|
#ifndef GrGpuResourceRef_DEFINED
|
|
|
|
#define GrGpuResourceRef_DEFINED
|
2014-09-04 20:12:37 +00:00
|
|
|
|
2014-09-19 18:48:02 +00:00
|
|
|
#include "GrGpuResource.h"
|
2014-11-03 16:47:23 +00:00
|
|
|
#include "GrRenderTarget.h"
|
|
|
|
#include "GrTexture.h"
|
2014-09-04 20:12:37 +00:00
|
|
|
#include "SkRefCnt.h"
|
|
|
|
|
|
|
|
/**
|
2014-09-19 18:10:40 +00:00
|
|
|
* This class is intended only for internal use in core Gr code.
|
|
|
|
*
|
2014-09-04 20:12:37 +00:00
|
|
|
* Class that wraps a resource referenced by a GrProgramElement or GrDrawState. It manages
|
2014-09-19 18:10:40 +00:00
|
|
|
* converting refs to pending IO operations. It allows a resource ownership to be in three
|
|
|
|
* states:
|
|
|
|
* 1. Owns a single ref
|
|
|
|
* 2. Owns a single ref and a pending IO operation (read, write, or read-write)
|
|
|
|
* 3. Owns a single pending IO operation.
|
|
|
|
*
|
|
|
|
* It is legal to destroy the GrGpuResourceRef in any of these states. It starts in state
|
|
|
|
* 1. Calling markPendingIO() converts it from state 1 to state 2. Calling removeRef() goes from
|
|
|
|
* state 2 to state 3. Calling pendingIOComplete() moves from state 2 to state 1. There is no
|
|
|
|
* valid way of going from state 3 back to 2 or 1.
|
|
|
|
*
|
2016-10-27 16:30:08 +00:00
|
|
|
* Like sk_sp, its constructor and setter adopt a ref from their caller.
|
2014-09-19 18:10:40 +00:00
|
|
|
*
|
|
|
|
* TODO: Once GrDODrawState no longer exists and therefore GrDrawState and GrOptDrawState no
|
|
|
|
* longer share an instance of this class, attempt to make the resource owned by GrGpuResourceRef
|
|
|
|
* only settable via the constructor.
|
2014-09-04 20:12:37 +00:00
|
|
|
*/
|
2014-09-17 15:05:40 +00:00
|
|
|
class GrGpuResourceRef : SkNoncopyable {
|
2014-09-04 20:12:37 +00:00
|
|
|
public:
|
2014-09-17 15:05:40 +00:00
|
|
|
~GrGpuResourceRef();
|
2014-09-04 20:12:37 +00:00
|
|
|
|
|
|
|
GrGpuResource* getResource() const { return fResource; }
|
|
|
|
|
|
|
|
/** Does this object own a pending read or write on the resource it is wrapping. */
|
|
|
|
bool ownsPendingIO() const { return fPendingIO; }
|
|
|
|
|
2016-11-29 16:59:17 +00:00
|
|
|
/** What type of IO does this represent? This is independent of whether a normal ref or a
|
|
|
|
pending IO is currently held. */
|
|
|
|
GrIOType ioType() const { return fIOType; }
|
|
|
|
|
2014-09-04 20:12:37 +00:00
|
|
|
/** Shortcut for calling setResource() with NULL. It cannot be called after markingPendingIO
|
|
|
|
is called. */
|
|
|
|
void reset();
|
|
|
|
|
2014-09-16 20:54:53 +00:00
|
|
|
protected:
|
2014-09-17 15:05:40 +00:00
|
|
|
GrGpuResourceRef();
|
2014-09-16 20:54:53 +00:00
|
|
|
|
|
|
|
/** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as
|
|
|
|
pending on the resource when markPendingIO is called. */
|
2014-10-08 15:40:09 +00:00
|
|
|
GrGpuResourceRef(GrGpuResource*, GrIOType);
|
2014-09-16 20:54:53 +00:00
|
|
|
|
|
|
|
/** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as
|
|
|
|
pending on the resource when markPendingIO is called. */
|
2014-10-08 15:40:09 +00:00
|
|
|
void setResource(GrGpuResource*, GrIOType);
|
2014-09-16 20:54:53 +00:00
|
|
|
|
2014-09-04 20:12:37 +00:00
|
|
|
private:
|
|
|
|
/** Called by owning GrProgramElement when the program element is first scheduled for
|
2014-09-19 18:10:40 +00:00
|
|
|
execution. It can only be called once. */
|
2014-09-04 20:12:37 +00:00
|
|
|
void markPendingIO() const;
|
|
|
|
|
2016-10-25 18:20:06 +00:00
|
|
|
/** Called when the program element/draw state is no longer owned by GrOpList-client code.
|
2014-09-04 20:12:37 +00:00
|
|
|
This lets the cache know that the drawing code will no longer schedule additional reads or
|
2014-09-19 18:10:40 +00:00
|
|
|
writes to the resource using the program element or draw state. It can only be called once.
|
|
|
|
*/
|
2014-09-04 20:12:37 +00:00
|
|
|
void removeRef() const;
|
|
|
|
|
2014-09-04 21:13:44 +00:00
|
|
|
/** Called to indicate that the previous pending IO is complete. Useful when the owning object
|
2014-09-17 15:05:40 +00:00
|
|
|
still has refs, so it is not about to destroy this GrGpuResourceRef, but its previously
|
2014-09-19 18:10:40 +00:00
|
|
|
pending executions have been complete. Can only be called if removeRef() was not previously
|
|
|
|
called. */
|
2014-09-04 21:13:44 +00:00
|
|
|
void pendingIOComplete() const;
|
|
|
|
|
2014-09-04 20:12:37 +00:00
|
|
|
friend class GrProgramElement;
|
|
|
|
|
2014-10-08 15:40:09 +00:00
|
|
|
GrGpuResource* fResource;
|
|
|
|
mutable bool fOwnRef;
|
|
|
|
mutable bool fPendingIO;
|
|
|
|
GrIOType fIOType;
|
2014-09-04 20:12:37 +00:00
|
|
|
|
|
|
|
typedef SkNoncopyable INHERITED;
|
|
|
|
};
|
|
|
|
|
2015-01-13 16:22:43 +00:00
|
|
|
/**
|
2014-09-19 18:10:40 +00:00
|
|
|
* Templated version of GrGpuResourceRef to enforce type safety.
|
|
|
|
*/
|
2014-09-17 15:05:40 +00:00
|
|
|
template <typename T> class GrTGpuResourceRef : public GrGpuResourceRef {
|
2014-09-16 20:54:53 +00:00
|
|
|
public:
|
2014-09-17 15:05:40 +00:00
|
|
|
GrTGpuResourceRef() {}
|
2014-09-16 20:54:53 +00:00
|
|
|
|
|
|
|
/** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as
|
|
|
|
pending on the resource when markPendingIO is called. */
|
2014-11-03 16:47:23 +00:00
|
|
|
GrTGpuResourceRef(T* resource, GrIOType ioType) : INHERITED(resource, ioType) { }
|
2014-09-16 20:54:53 +00:00
|
|
|
|
|
|
|
T* get() const { return static_cast<T*>(this->getResource()); }
|
|
|
|
|
|
|
|
/** Adopts a ref from the caller. ioType expresses what type of IO operations will be marked as
|
|
|
|
pending on the resource when markPendingIO is called. */
|
2014-10-08 15:40:09 +00:00
|
|
|
void set(T* resource, GrIOType ioType) { this->setResource(resource, ioType); }
|
2014-09-17 15:05:40 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GrGpuResourceRef INHERITED;
|
2014-09-16 20:54:53 +00:00
|
|
|
};
|
|
|
|
|
2014-11-03 16:47:23 +00:00
|
|
|
// Specializations for GrTexture and GrRenderTarget because they use virtual inheritance.
|
|
|
|
template<> class GrTGpuResourceRef<GrTexture> : public GrGpuResourceRef {
|
|
|
|
public:
|
|
|
|
GrTGpuResourceRef() {}
|
|
|
|
|
|
|
|
GrTGpuResourceRef(GrTexture* texture, GrIOType ioType) : INHERITED(texture, ioType) { }
|
|
|
|
|
|
|
|
GrTexture* get() const {
|
|
|
|
GrSurface* surface = static_cast<GrSurface*>(this->getResource());
|
|
|
|
if (surface) {
|
|
|
|
return surface->asTexture();
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(GrTexture* texture, GrIOType ioType) { this->setResource(texture, ioType); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GrGpuResourceRef INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> class GrTGpuResourceRef<GrRenderTarget> : public GrGpuResourceRef {
|
|
|
|
public:
|
|
|
|
GrTGpuResourceRef() {}
|
|
|
|
|
|
|
|
GrTGpuResourceRef(GrRenderTarget* rt, GrIOType ioType) : INHERITED(rt, ioType) { }
|
|
|
|
|
|
|
|
GrRenderTarget* get() const {
|
|
|
|
GrSurface* surface = static_cast<GrSurface*>(this->getResource());
|
|
|
|
if (surface) {
|
|
|
|
return surface->asRenderTarget();
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(GrRenderTarget* rt, GrIOType ioType) { this->setResource(rt, ioType); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GrGpuResourceRef INHERITED;
|
|
|
|
};
|
|
|
|
|
2014-09-19 18:10:40 +00:00
|
|
|
/**
|
|
|
|
* This is similar to GrTGpuResourceRef but can only be in the pending IO state. It never owns a
|
|
|
|
* ref.
|
|
|
|
*/
|
2014-10-08 15:40:09 +00:00
|
|
|
template <typename T, GrIOType IO_TYPE> class GrPendingIOResource : SkNoncopyable {
|
2014-09-19 18:10:40 +00:00
|
|
|
public:
|
2014-11-18 22:24:27 +00:00
|
|
|
GrPendingIOResource(T* resource = NULL) : fResource(NULL) {
|
|
|
|
this->reset(resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset(T* resource) {
|
|
|
|
if (resource) {
|
2014-09-19 18:48:02 +00:00
|
|
|
switch (IO_TYPE) {
|
2014-10-08 15:40:09 +00:00
|
|
|
case kRead_GrIOType:
|
2014-11-18 22:24:27 +00:00
|
|
|
resource->addPendingRead();
|
2014-09-19 18:10:40 +00:00
|
|
|
break;
|
2014-10-08 15:40:09 +00:00
|
|
|
case kWrite_GrIOType:
|
2014-11-18 22:24:27 +00:00
|
|
|
resource->addPendingWrite();
|
2014-09-19 18:10:40 +00:00
|
|
|
break;
|
2014-10-08 15:40:09 +00:00
|
|
|
case kRW_GrIOType:
|
2014-11-18 22:24:27 +00:00
|
|
|
resource->addPendingRead();
|
|
|
|
resource->addPendingWrite();
|
2014-09-19 18:10:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 22:24:27 +00:00
|
|
|
this->release();
|
|
|
|
fResource = resource;
|
2014-09-19 18:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~GrPendingIOResource() {
|
2014-11-18 22:24:27 +00:00
|
|
|
this->release();
|
|
|
|
}
|
|
|
|
|
2015-11-10 12:55:08 +00:00
|
|
|
explicit operator bool() const { return SkToBool(fResource); }
|
|
|
|
|
|
|
|
bool operator==(const GrPendingIOResource& other) const {
|
|
|
|
return fResource == other.fResource;
|
|
|
|
}
|
2014-11-20 17:56:11 +00:00
|
|
|
|
2014-11-18 22:24:27 +00:00
|
|
|
T* get() const { return fResource; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void release() {
|
|
|
|
if (fResource) {
|
2014-09-19 18:48:02 +00:00
|
|
|
switch (IO_TYPE) {
|
2014-10-08 15:40:09 +00:00
|
|
|
case kRead_GrIOType:
|
2014-09-19 18:10:40 +00:00
|
|
|
fResource->completedRead();
|
|
|
|
break;
|
2014-10-08 15:40:09 +00:00
|
|
|
case kWrite_GrIOType:
|
2014-09-19 18:10:40 +00:00
|
|
|
fResource->completedWrite();
|
|
|
|
break;
|
2014-10-08 15:40:09 +00:00
|
|
|
case kRW_GrIOType:
|
2014-09-19 18:10:40 +00:00
|
|
|
fResource->completedRead();
|
|
|
|
fResource->completedWrite();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 15:40:09 +00:00
|
|
|
T* fResource;
|
2014-09-19 18:10:40 +00:00
|
|
|
};
|
2014-09-04 20:12:37 +00:00
|
|
|
#endif
|