2015-08-17 19:55:38 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2016-12-21 16:14:46 +00:00
|
|
|
#ifndef GrOpFlushState_DEFINED
|
|
|
|
#define GrOpFlushState_DEFINED
|
2015-08-17 19:55:38 +00:00
|
|
|
|
|
|
|
#include "GrBufferAllocPool.h"
|
2016-10-18 15:48:51 +00:00
|
|
|
#include "GrGpu.h"
|
2017-05-03 21:06:09 +00:00
|
|
|
#include "SkArenaAlloc.h"
|
2016-12-16 14:52:16 +00:00
|
|
|
#include "ops/GrMeshDrawOp.h"
|
2015-08-17 19:55:38 +00:00
|
|
|
|
2016-06-23 15:37:05 +00:00
|
|
|
class GrGpuCommandBuffer;
|
2015-08-17 19:55:38 +00:00
|
|
|
class GrResourceProvider;
|
|
|
|
|
2016-12-07 22:06:19 +00:00
|
|
|
/** Tracks the state across all the GrOps (really just the GrDrawOps) in a GrOpList flush. */
|
|
|
|
class GrOpFlushState {
|
2015-08-17 19:55:38 +00:00
|
|
|
public:
|
2016-12-07 22:06:19 +00:00
|
|
|
GrOpFlushState(GrGpu*, GrResourceProvider*);
|
2015-08-17 19:55:38 +00:00
|
|
|
|
2016-12-07 22:06:19 +00:00
|
|
|
~GrOpFlushState() { this->reset(); }
|
2015-08-17 19:55:38 +00:00
|
|
|
|
2016-12-07 22:06:19 +00:00
|
|
|
/** Inserts an upload to be executed after all ops in the flush prepared their draws but before
|
|
|
|
the draws are executed to the backend 3D API. */
|
2016-12-01 15:59:09 +00:00
|
|
|
void addASAPUpload(GrDrawOp::DeferredUploadFn&& upload) {
|
2016-04-01 13:06:20 +00:00
|
|
|
fAsapUploads.emplace_back(std::move(upload));
|
2015-08-17 19:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const GrCaps& caps() const { return *fGpu->caps(); }
|
|
|
|
GrResourceProvider* resourceProvider() const { return fResourceProvider; }
|
|
|
|
|
|
|
|
/** Has the token been flushed to the backend 3D API. */
|
2016-12-01 15:59:09 +00:00
|
|
|
bool hasDrawBeenFlushed(GrDrawOpUploadToken token) const {
|
2016-04-01 13:06:20 +00:00
|
|
|
return token.fSequenceNumber <= fLastFlushedToken.fSequenceNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Issue a token to an operation that is being enqueued. */
|
2016-12-01 15:59:09 +00:00
|
|
|
GrDrawOpUploadToken issueDrawToken() {
|
|
|
|
return GrDrawOpUploadToken(++fLastIssuedToken.fSequenceNumber);
|
2016-04-01 13:06:20 +00:00
|
|
|
}
|
2015-08-17 19:55:38 +00:00
|
|
|
|
2016-04-01 13:06:20 +00:00
|
|
|
/** Call every time a draw that was issued a token is flushed */
|
|
|
|
void flushToken() { ++fLastFlushedToken.fSequenceNumber; }
|
2015-08-17 19:55:38 +00:00
|
|
|
|
2016-04-01 13:06:20 +00:00
|
|
|
/** Gets the next draw token that will be issued. */
|
2016-12-01 15:59:09 +00:00
|
|
|
GrDrawOpUploadToken nextDrawToken() const {
|
|
|
|
return GrDrawOpUploadToken(fLastIssuedToken.fSequenceNumber + 1);
|
2016-04-01 13:06:20 +00:00
|
|
|
}
|
2015-08-17 19:55:38 +00:00
|
|
|
|
2016-04-01 13:06:20 +00:00
|
|
|
/** The last token flushed to all the way to the backend API. */
|
2016-12-01 15:59:09 +00:00
|
|
|
GrDrawOpUploadToken nextTokenToFlush() const {
|
|
|
|
return GrDrawOpUploadToken(fLastFlushedToken.fSequenceNumber + 1);
|
2016-04-01 13:06:20 +00:00
|
|
|
}
|
2015-08-17 19:55:38 +00:00
|
|
|
|
|
|
|
void* makeVertexSpace(size_t vertexSize, int vertexCount,
|
2016-03-25 19:15:03 +00:00
|
|
|
const GrBuffer** buffer, int* startVertex);
|
|
|
|
uint16_t* makeIndexSpace(int indexCount, const GrBuffer** buffer, int* startIndex);
|
2015-08-17 19:55:38 +00:00
|
|
|
|
2016-12-07 22:06:19 +00:00
|
|
|
/** This is called after each op has a chance to prepare its draws and before the draws are
|
|
|
|
issued. */
|
2015-08-17 19:55:38 +00:00
|
|
|
void preIssueDraws() {
|
|
|
|
fVertexPool.unmap();
|
|
|
|
fIndexPool.unmap();
|
|
|
|
int uploadCount = fAsapUploads.count();
|
2016-04-01 13:06:20 +00:00
|
|
|
|
2015-08-17 19:55:38 +00:00
|
|
|
for (int i = 0; i < uploadCount; i++) {
|
2016-04-01 13:06:20 +00:00
|
|
|
this->doUpload(fAsapUploads[i]);
|
2015-08-17 19:55:38 +00:00
|
|
|
}
|
|
|
|
fAsapUploads.reset();
|
|
|
|
}
|
|
|
|
|
2017-06-12 16:09:30 +00:00
|
|
|
void doUpload(GrDrawOp::DeferredUploadFn&);
|
2016-04-01 13:06:20 +00:00
|
|
|
|
2015-08-17 19:55:38 +00:00
|
|
|
void putBackIndices(size_t indices) { fIndexPool.putBack(indices * sizeof(uint16_t)); }
|
|
|
|
|
|
|
|
void putBackVertexSpace(size_t sizeInBytes) { fVertexPool.putBack(sizeInBytes); }
|
|
|
|
|
2016-06-23 15:37:05 +00:00
|
|
|
GrGpuCommandBuffer* commandBuffer() { return fCommandBuffer; }
|
|
|
|
void setCommandBuffer(GrGpuCommandBuffer* buffer) { fCommandBuffer = buffer; }
|
|
|
|
|
2015-08-17 19:55:38 +00:00
|
|
|
GrGpu* gpu() { return fGpu; }
|
|
|
|
|
2015-10-02 18:27:14 +00:00
|
|
|
void reset() {
|
|
|
|
fVertexPool.reset();
|
|
|
|
fIndexPool.reset();
|
2017-05-03 21:06:09 +00:00
|
|
|
fPipelines.reset();
|
2015-10-02 18:27:14 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 18:22:38 +00:00
|
|
|
/** Additional data required on a per-op basis when executing GrDrawOps. */
|
|
|
|
struct DrawOpArgs {
|
2017-05-29 19:05:15 +00:00
|
|
|
GrRenderTarget* fRenderTarget;
|
|
|
|
const GrAppliedClip* fAppliedClip;
|
|
|
|
GrXferProcessor::DstProxy fDstProxy;
|
2017-03-21 18:22:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void setDrawOpArgs(DrawOpArgs* opArgs) { fOpArgs = opArgs; }
|
|
|
|
|
|
|
|
const DrawOpArgs& drawOpArgs() const {
|
|
|
|
SkASSERT(fOpArgs);
|
|
|
|
return *fOpArgs;
|
|
|
|
}
|
2017-03-16 19:51:42 +00:00
|
|
|
|
2017-05-03 21:06:09 +00:00
|
|
|
template <typename... Args>
|
|
|
|
GrPipeline* allocPipeline(Args... args) {
|
|
|
|
return fPipelines.make<GrPipeline>(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2017-03-21 18:22:38 +00:00
|
|
|
private:
|
2017-05-03 21:06:09 +00:00
|
|
|
GrGpu* fGpu;
|
|
|
|
GrResourceProvider* fResourceProvider;
|
|
|
|
GrGpuCommandBuffer* fCommandBuffer;
|
|
|
|
GrVertexBufferAllocPool fVertexPool;
|
|
|
|
GrIndexBufferAllocPool fIndexPool;
|
|
|
|
SkSTArray<4, GrDrawOp::DeferredUploadFn> fAsapUploads;
|
|
|
|
GrDrawOpUploadToken fLastIssuedToken;
|
|
|
|
GrDrawOpUploadToken fLastFlushedToken;
|
|
|
|
DrawOpArgs* fOpArgs;
|
|
|
|
SkArenaAlloc fPipelines{sizeof(GrPipeline) * 100};
|
2015-08-17 19:55:38 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 13:06:20 +00:00
|
|
|
/**
|
2016-12-07 22:06:19 +00:00
|
|
|
* A word about uploads and tokens: Ops should usually schedule their uploads to occur at the
|
2016-04-01 13:06:20 +00:00
|
|
|
* begining of a frame whenever possible. These are called ASAP uploads. Of course, this requires
|
|
|
|
* that there are no draws that have yet to be flushed that rely on the old texture contents. In
|
|
|
|
* that case the ASAP upload would happen prior to the previous draw causing the draw to read the
|
|
|
|
* new (wrong) texture data. In that case they should schedule an inline upload.
|
|
|
|
*
|
2016-12-16 23:59:19 +00:00
|
|
|
* Ops, in conjunction with helpers such as GrDrawOpAtlas, can use the token system to know
|
2016-04-01 13:06:20 +00:00
|
|
|
* what the most recent draw was that referenced a resource (or portion of a resource). Each draw
|
|
|
|
* is assigned a token. A resource (or portion) can be tagged with the most recent draw's
|
|
|
|
* token. The target provides a facility for testing whether the draw corresponding to the token
|
2016-12-07 22:06:19 +00:00
|
|
|
* has been flushed. If it has not been flushed then the op must perform an inline upload instead.
|
|
|
|
* When scheduling an inline upload the op provides the token of the draw that the upload must occur
|
|
|
|
* before. The upload will then occur between the draw that requires the new data but after the
|
|
|
|
* token that requires the old data.
|
2016-04-01 13:06:20 +00:00
|
|
|
*
|
2016-12-01 21:40:24 +00:00
|
|
|
* TODO: Currently the token/upload interface is spread over GrDrawOp, GrMeshDrawOp,
|
|
|
|
* GrDrawOp::Target, and GrMeshDrawOp::Target. However, the interface at the GrDrawOp level is not
|
|
|
|
* complete and isn't useful. We should push it down to GrMeshDrawOp until it is required at the
|
|
|
|
* GrDrawOp level.
|
2016-04-01 13:06:20 +00:00
|
|
|
*/
|
2016-12-01 21:40:24 +00:00
|
|
|
|
2015-08-17 19:55:38 +00:00
|
|
|
/**
|
2016-12-01 15:59:09 +00:00
|
|
|
* GrDrawOp instances use this object to allocate space for their geometry and to issue the draws
|
2016-12-07 22:06:19 +00:00
|
|
|
* that render their op.
|
2015-08-17 19:55:38 +00:00
|
|
|
*/
|
2016-12-01 15:59:09 +00:00
|
|
|
class GrDrawOp::Target {
|
2015-08-17 19:55:38 +00:00
|
|
|
public:
|
2016-12-07 22:06:19 +00:00
|
|
|
Target(GrOpFlushState* state, GrDrawOp* op) : fState(state), fOp(op) {}
|
2015-08-17 19:55:38 +00:00
|
|
|
|
2016-04-01 13:06:20 +00:00
|
|
|
/** Returns the token of the draw that this upload will occur before. */
|
2016-12-01 15:59:09 +00:00
|
|
|
GrDrawOpUploadToken addInlineUpload(DeferredUploadFn&& upload) {
|
2016-12-07 22:06:19 +00:00
|
|
|
fOp->fInlineUploads.emplace_back(std::move(upload), fState->nextDrawToken());
|
|
|
|
return fOp->fInlineUploads.back().fUploadBeforeToken;
|
2016-04-01 13:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the token of the draw that this upload will occur before. Since ASAP uploads
|
|
|
|
are done first during a flush, this will be the first token since the most recent
|
|
|
|
flush. */
|
2016-12-01 15:59:09 +00:00
|
|
|
GrDrawOpUploadToken addAsapUpload(DeferredUploadFn&& upload) {
|
2016-04-01 13:06:20 +00:00
|
|
|
fState->addASAPUpload(std::move(upload));
|
|
|
|
return fState->nextTokenToFlush();
|
2015-08-17 19:55:38 +00:00
|
|
|
}
|
|
|
|
|
2016-12-01 15:59:09 +00:00
|
|
|
bool hasDrawBeenFlushed(GrDrawOpUploadToken token) const {
|
2016-04-01 13:06:20 +00:00
|
|
|
return fState->hasDrawBeenFlushed(token);
|
2015-08-17 19:55:38 +00:00
|
|
|
}
|
2016-04-01 13:06:20 +00:00
|
|
|
|
2016-12-07 22:06:19 +00:00
|
|
|
/** Gets the next draw token that will be issued by this target. This can be used by an op
|
2016-04-01 13:06:20 +00:00
|
|
|
to record that the next draw it issues will use a resource (e.g. texture) while preparing
|
|
|
|
that draw. */
|
2016-12-01 15:59:09 +00:00
|
|
|
GrDrawOpUploadToken nextDrawToken() const { return fState->nextDrawToken(); }
|
2015-08-17 19:55:38 +00:00
|
|
|
|
|
|
|
const GrCaps& caps() const { return fState->caps(); }
|
|
|
|
|
|
|
|
GrResourceProvider* resourceProvider() const { return fState->resourceProvider(); }
|
|
|
|
|
|
|
|
protected:
|
2016-12-07 22:06:19 +00:00
|
|
|
GrDrawOp* op() { return fOp; }
|
|
|
|
GrOpFlushState* state() { return fState; }
|
2017-05-03 21:06:09 +00:00
|
|
|
const GrOpFlushState* state() const { return fState; }
|
2015-08-17 19:55:38 +00:00
|
|
|
|
|
|
|
private:
|
2016-12-07 22:06:19 +00:00
|
|
|
GrOpFlushState* fState;
|
|
|
|
GrDrawOp* fOp;
|
2015-08-17 19:55:38 +00:00
|
|
|
};
|
|
|
|
|
2016-12-01 21:40:24 +00:00
|
|
|
/** Extension of GrDrawOp::Target for use by GrMeshDrawOp. Adds the ability to create vertex
|
2015-08-17 19:55:38 +00:00
|
|
|
draws. */
|
2016-12-01 21:40:24 +00:00
|
|
|
class GrMeshDrawOp::Target : public GrDrawOp::Target {
|
2015-08-17 19:55:38 +00:00
|
|
|
public:
|
2016-12-07 22:06:19 +00:00
|
|
|
Target(GrOpFlushState* state, GrMeshDrawOp* op) : INHERITED(state, op) {}
|
2015-08-17 19:55:38 +00:00
|
|
|
|
2017-04-03 14:38:00 +00:00
|
|
|
void draw(const GrGeometryProcessor* gp, const GrPipeline* pipeline, const GrMesh& mesh);
|
2015-08-17 19:55:38 +00:00
|
|
|
|
|
|
|
void* makeVertexSpace(size_t vertexSize, int vertexCount,
|
2016-03-25 19:15:03 +00:00
|
|
|
const GrBuffer** buffer, int* startVertex) {
|
2015-08-17 19:55:38 +00:00
|
|
|
return this->state()->makeVertexSpace(vertexSize, vertexCount, buffer, startVertex);
|
|
|
|
}
|
|
|
|
|
2016-03-25 19:15:03 +00:00
|
|
|
uint16_t* makeIndexSpace(int indexCount, const GrBuffer** buffer, int* startIndex) {
|
2015-08-17 19:55:38 +00:00
|
|
|
return this->state()->makeIndexSpace(indexCount, buffer, startIndex);
|
|
|
|
}
|
|
|
|
|
2016-12-07 22:06:19 +00:00
|
|
|
/** Helpers for ops which over-allocate and then return data to the pool. */
|
2015-08-17 19:55:38 +00:00
|
|
|
void putBackIndices(int indices) { this->state()->putBackIndices(indices); }
|
|
|
|
void putBackVertices(int vertices, size_t vertexStride) {
|
|
|
|
this->state()->putBackVertexSpace(vertices * vertexStride);
|
|
|
|
}
|
|
|
|
|
2017-05-03 21:06:09 +00:00
|
|
|
GrRenderTarget* renderTarget() const { return this->state()->drawOpArgs().fRenderTarget; }
|
|
|
|
|
|
|
|
const GrAppliedClip* clip() const { return this->state()->drawOpArgs().fAppliedClip; }
|
|
|
|
|
2017-05-29 19:05:15 +00:00
|
|
|
const GrXferProcessor::DstProxy& dstProxy() const {
|
|
|
|
return this->state()->drawOpArgs().fDstProxy;
|
2017-05-03 21:06:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
GrPipeline* allocPipeline(Args... args) {
|
|
|
|
return this->state()->allocPipeline(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2015-08-17 19:55:38 +00:00
|
|
|
private:
|
2016-12-07 22:06:19 +00:00
|
|
|
GrMeshDrawOp* meshDrawOp() { return static_cast<GrMeshDrawOp*>(this->op()); }
|
2016-12-01 15:59:09 +00:00
|
|
|
typedef GrDrawOp::Target INHERITED;
|
2015-08-17 19:55:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|