2016-01-08 15:19:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GrAuditTrail_DEFINED
|
|
|
|
#define GrAuditTrail_DEFINED
|
|
|
|
|
2016-01-11 18:39:11 +00:00
|
|
|
#include "GrConfig.h"
|
2016-01-12 14:45:24 +00:00
|
|
|
#include "SkRect.h"
|
2016-01-08 15:19:47 +00:00
|
|
|
#include "SkString.h"
|
|
|
|
#include "SkTArray.h"
|
2016-02-26 16:07:50 +00:00
|
|
|
#include "SkTHash.h"
|
|
|
|
|
|
|
|
class GrBatch;
|
2016-01-08 15:19:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* GrAuditTrail collects a list of draw ops, detailed information about those ops, and can dump them
|
|
|
|
* to json.
|
2016-02-18 21:45:39 +00:00
|
|
|
*
|
|
|
|
* Capturing this information is expensive and consumes a lot of memory, therefore it is important
|
|
|
|
* to enable auditing only when required and disable it promptly. The AutoEnable class helps to
|
|
|
|
* ensure that the audit trail is disabled in a timely fashion. Once the information has been dealt
|
|
|
|
* with, be sure to call reset(), or the log will simply keep growing.
|
2016-01-08 15:19:47 +00:00
|
|
|
*/
|
|
|
|
class GrAuditTrail {
|
|
|
|
public:
|
2016-02-18 21:45:39 +00:00
|
|
|
GrAuditTrail()
|
2016-02-29 15:44:02 +00:00
|
|
|
: fClientID(kGrAuditTrailInvalidID)
|
|
|
|
, fEnabled(false) {}
|
2016-01-12 20:59:28 +00:00
|
|
|
|
2016-02-18 21:45:39 +00:00
|
|
|
class AutoEnable {
|
|
|
|
public:
|
|
|
|
AutoEnable(GrAuditTrail* auditTrail)
|
|
|
|
: fAuditTrail(auditTrail) {
|
|
|
|
SkASSERT(!fAuditTrail->isEnabled());
|
|
|
|
fAuditTrail->setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoEnable() {
|
|
|
|
SkASSERT(fAuditTrail->isEnabled());
|
|
|
|
fAuditTrail->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
GrAuditTrail* fAuditTrail;
|
|
|
|
};
|
|
|
|
|
2016-02-26 16:07:50 +00:00
|
|
|
class AutoManageBatchList {
|
|
|
|
public:
|
|
|
|
AutoManageBatchList(GrAuditTrail* auditTrail)
|
|
|
|
: fAutoEnable(auditTrail)
|
|
|
|
, fAuditTrail(auditTrail) {
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoManageBatchList() {
|
|
|
|
fAuditTrail->fullReset();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AutoEnable fAutoEnable;
|
|
|
|
GrAuditTrail* fAuditTrail;
|
|
|
|
};
|
|
|
|
|
2016-02-29 15:44:02 +00:00
|
|
|
class AutoCollectBatches {
|
|
|
|
public:
|
|
|
|
AutoCollectBatches(GrAuditTrail* auditTrail, int clientID)
|
|
|
|
: fAutoEnable(auditTrail)
|
|
|
|
, fAuditTrail(auditTrail) {
|
|
|
|
fAuditTrail->setClientID(clientID);
|
2016-01-12 20:59:28 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 15:44:02 +00:00
|
|
|
~AutoCollectBatches() { fAuditTrail->setClientID(kGrAuditTrailInvalidID); }
|
2016-01-08 15:19:47 +00:00
|
|
|
|
2016-02-29 15:44:02 +00:00
|
|
|
private:
|
|
|
|
AutoEnable fAutoEnable;
|
|
|
|
GrAuditTrail* fAuditTrail;
|
|
|
|
};
|
2016-01-12 20:59:28 +00:00
|
|
|
|
|
|
|
void addBatch(const char* name, const SkRect& bounds) {
|
2016-02-29 15:44:02 +00:00
|
|
|
SkASSERT(fEnabled);
|
2016-01-14 18:58:07 +00:00
|
|
|
Batch* batch = new Batch;
|
2016-02-29 15:44:02 +00:00
|
|
|
fBatchPool.emplace_back(batch);
|
2016-01-14 18:58:07 +00:00
|
|
|
batch->fName = name;
|
|
|
|
batch->fBounds = bounds;
|
2016-02-29 15:44:02 +00:00
|
|
|
batch->fClientID = kGrAuditTrailInvalidID;
|
|
|
|
batch->fBatchListID = kGrAuditTrailInvalidID;
|
|
|
|
batch->fChildID = kGrAuditTrailInvalidID;
|
2016-02-26 16:07:50 +00:00
|
|
|
fCurrentBatch = batch;
|
2016-02-29 15:44:02 +00:00
|
|
|
|
|
|
|
if (fClientID != kGrAuditTrailInvalidID) {
|
|
|
|
batch->fClientID = fClientID;
|
|
|
|
Batches** batchesLookup = fClientIDLookup.find(fClientID);
|
|
|
|
Batches* batches = nullptr;
|
|
|
|
if (!batchesLookup) {
|
|
|
|
batches = new Batches;
|
|
|
|
fClientIDLookup.set(fClientID, batches);
|
|
|
|
} else {
|
|
|
|
batches = *batchesLookup;
|
|
|
|
}
|
|
|
|
|
|
|
|
batches->push_back(fCurrentBatch);
|
|
|
|
}
|
2016-01-12 14:45:24 +00:00
|
|
|
}
|
|
|
|
|
2016-02-26 16:07:50 +00:00
|
|
|
void batchingResultCombined(GrBatch* combiner);
|
|
|
|
|
|
|
|
void batchingResultNew(GrBatch* batch);
|
|
|
|
|
2016-02-29 15:44:02 +00:00
|
|
|
// Because batching is heavily dependent on sequence of draw calls, these calls will only
|
|
|
|
// produce valid information for the given draw sequence which preceeded them.
|
|
|
|
// Specifically, future draw calls may change the batching and thus would invalidate
|
|
|
|
// the json. What this means is that for some sequence of draw calls N, the below toJson
|
|
|
|
// calls will only produce JSON which reflects N draw calls. This JSON may or may not be
|
|
|
|
// accurate for N + 1 or N - 1 draws depending on the actual batching algorithm used.
|
|
|
|
SkString toJson(bool prettyPrint = false) const;
|
|
|
|
|
|
|
|
// returns a json string of all of the batches associated with a given client id
|
|
|
|
SkString toJson(int clientID, bool prettyPrint = false) const;
|
2016-01-08 15:19:47 +00:00
|
|
|
|
2016-02-18 21:45:39 +00:00
|
|
|
bool isEnabled() { return fEnabled; }
|
|
|
|
void setEnabled(bool enabled) { fEnabled = enabled; }
|
|
|
|
|
2016-02-29 15:44:02 +00:00
|
|
|
void setClientID(int clientID) { fClientID = clientID; }
|
2016-02-26 16:07:50 +00:00
|
|
|
|
2016-02-29 19:15:06 +00:00
|
|
|
// We could just return our internal bookkeeping struct if copying the data out becomes
|
|
|
|
// a performance issue, but until then its nice to decouple
|
|
|
|
struct BatchInfo {
|
|
|
|
SkRect fBounds;
|
2016-03-01 15:15:52 +00:00
|
|
|
uint32_t fRenderTargetUniqueID;
|
2016-02-29 19:15:06 +00:00
|
|
|
struct Batch {
|
|
|
|
int fClientID;
|
|
|
|
SkRect fBounds;
|
|
|
|
};
|
|
|
|
SkTArray<Batch> fBatches;
|
|
|
|
};
|
|
|
|
|
|
|
|
void getBoundsByClientID(SkTArray<BatchInfo>* outInfo, int clientID);
|
|
|
|
|
2016-02-29 15:44:02 +00:00
|
|
|
void fullReset() {
|
2016-02-26 16:07:50 +00:00
|
|
|
SkASSERT(fEnabled);
|
2016-02-29 15:44:02 +00:00
|
|
|
fBatchList.reset();
|
2016-02-26 16:07:50 +00:00
|
|
|
fIDLookup.reset();
|
2016-02-29 15:44:02 +00:00
|
|
|
// free all client batches
|
|
|
|
fClientIDLookup.foreach([](const int&, Batches** batches) { delete *batches; });
|
|
|
|
fClientIDLookup.reset();
|
|
|
|
fBatchPool.reset(); // must be last, frees all of the memory
|
2016-02-26 16:07:50 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 15:44:02 +00:00
|
|
|
static const int kGrAuditTrailInvalidID;
|
2016-01-08 15:19:47 +00:00
|
|
|
|
|
|
|
private:
|
2016-01-14 18:58:07 +00:00
|
|
|
// TODO if performance becomes an issue, we can move to using SkVarAlloc
|
2016-02-29 15:44:02 +00:00
|
|
|
struct Batch {
|
|
|
|
SkString toJson() const;
|
|
|
|
SkString fName;
|
|
|
|
SkRect fBounds;
|
|
|
|
int fClientID;
|
|
|
|
int fBatchListID;
|
|
|
|
int fChildID;
|
2016-01-08 15:19:47 +00:00
|
|
|
};
|
2016-02-29 15:44:02 +00:00
|
|
|
typedef SkTArray<SkAutoTDelete<Batch>, true> BatchPool;
|
2016-01-08 15:19:47 +00:00
|
|
|
|
2016-02-29 15:44:02 +00:00
|
|
|
typedef SkTArray<Batch*> Batches;
|
2016-01-14 18:58:07 +00:00
|
|
|
|
2016-02-29 15:44:02 +00:00
|
|
|
struct BatchNode {
|
|
|
|
SkString toJson() const;
|
2016-01-14 18:58:07 +00:00
|
|
|
SkRect fBounds;
|
2016-02-29 15:44:02 +00:00
|
|
|
Batches fChildren;
|
2016-03-01 15:15:52 +00:00
|
|
|
uint32_t fRenderTargetUniqueID;
|
2016-01-14 18:58:07 +00:00
|
|
|
};
|
2016-02-29 15:44:02 +00:00
|
|
|
typedef SkTArray<SkAutoTDelete<BatchNode>, true> BatchList;
|
2016-01-14 18:58:07 +00:00
|
|
|
|
2016-02-26 16:07:50 +00:00
|
|
|
template <typename T>
|
|
|
|
static void JsonifyTArray(SkString* json, const char* name, const T& array,
|
2016-02-18 13:04:39 +00:00
|
|
|
bool addComma);
|
2016-01-14 18:58:07 +00:00
|
|
|
|
2016-02-26 16:07:50 +00:00
|
|
|
Batch* fCurrentBatch;
|
2016-02-29 15:44:02 +00:00
|
|
|
BatchPool fBatchPool;
|
2016-02-26 16:07:50 +00:00
|
|
|
SkTHashMap<GrBatch*, int> fIDLookup;
|
2016-02-29 15:44:02 +00:00
|
|
|
SkTHashMap<int, Batches*> fClientIDLookup;
|
|
|
|
BatchList fBatchList;
|
|
|
|
|
|
|
|
// The client cas pass in an optional client ID which we will use to mark the batches
|
|
|
|
int fClientID;
|
|
|
|
bool fEnabled;
|
2016-01-08 15:19:47 +00:00
|
|
|
};
|
|
|
|
|
2016-02-18 21:45:39 +00:00
|
|
|
#define GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, invoke, ...) \
|
|
|
|
if (audit_trail->isEnabled()) { \
|
|
|
|
audit_trail->invoke(__VA_ARGS__); \
|
2016-01-11 18:39:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 20:59:28 +00:00
|
|
|
#define GR_AUDIT_TRAIL_AUTO_FRAME(audit_trail, framename) \
|
2016-02-29 15:44:02 +00:00
|
|
|
// TODO fill out the frame stuff
|
|
|
|
//GrAuditTrail::AutoFrame SK_MACRO_APPEND_LINE(auto_frame)(audit_trail, framename);
|
2016-01-11 18:39:11 +00:00
|
|
|
|
|
|
|
#define GR_AUDIT_TRAIL_RESET(audit_trail) \
|
2016-02-29 15:44:02 +00:00
|
|
|
//GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, reset);
|
2016-01-11 18:39:11 +00:00
|
|
|
|
2016-01-12 14:45:24 +00:00
|
|
|
#define GR_AUDIT_TRAIL_ADDBATCH(audit_trail, batchname, bounds) \
|
2016-02-18 21:45:39 +00:00
|
|
|
GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, addBatch, batchname, bounds);
|
2016-01-12 14:45:24 +00:00
|
|
|
|
2016-02-26 16:07:50 +00:00
|
|
|
#define GR_AUDIT_TRAIL_BATCHING_RESULT_COMBINED(audit_trail, combiner) \
|
|
|
|
GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, batchingResultCombined, combiner);
|
|
|
|
|
|
|
|
#define GR_AUDIT_TRAIL_BATCHING_RESULT_NEW(audit_trail, batch) \
|
|
|
|
GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, batchingResultNew, batch);
|
|
|
|
|
2016-01-08 15:19:47 +00:00
|
|
|
#endif
|