// Misc.
/**
- * Flags that affect flush() behavior.
- */
- enum FlushBits {
- /**
- * A client may reach a point where it has partially rendered a frame
- * through a GrContext that it knows the user will never see. This flag
- * causes the flush to skip submission of deferred content to the 3D API
- * during the flush.
- */
- kDiscard_FlushBit = 0x2,
- };
-
- /**
* Call to ensure all drawing to the context has been issued to the
* underlying 3D API.
- * @param flagsBitfield flags that control the flushing behavior. See
- * FlushBits.
*/
- void flush(int flagsBitfield = 0);
-
- void flushIfNecessary() {
- if (fFlushToReduceCacheSize || this->caps()->immediateFlush()) {
- this->flush();
- }
- }
+ void flush();
/**
* These flags can be used with the read/write pixels functions below.
GrBatchFontCache* fBatchFontCache;
SkAutoTDelete<GrTextBlobCache> fTextBlobCache;
- // Set by OverbudgetCB() to request that GrContext flush before exiting a draw.
- bool fFlushToReduceCacheSize;
bool fDidTestPMConversions;
int fPMToUPMConversion;
int fUPMToPMConversion;
bool didFailPMUPMConversionTest() const;
/**
- * This callback allows the resource cache to callback into the GrContext
- * when the cache is still over budget after a purge.
- */
- static void OverBudgetCB(void* data);
-
- /**
* A callback similar to the above for use by the TextBlobCache
* TODO move textblob draw calls below context so we can use the call above.
*/
fResourceCache = nullptr;
fResourceProvider = nullptr;
fBatchFontCache = nullptr;
- fFlushToReduceCacheSize = false;
}
bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
fCaps = SkRef(fGpu->caps());
fResourceCache = new GrResourceCache(fCaps);
- fResourceCache->setOverBudgetCallback(OverBudgetCB, this);
fResourceProvider = new GrResourceProvider(fGpu, fResourceCache, &fSingleOwner);
fDidTestPMConversions = false;
dtOptions.fDrawBatchBounds = options.fDrawBatchBounds;
dtOptions.fMaxBatchLookback = options.fMaxBatchLookback;
dtOptions.fMaxBatchLookahead = options.fMaxBatchLookahead;
- fDrawingManager.reset(new GrDrawingManager(this, dtOptions, &fSingleOwner));
+ fDrawingManager.reset(new GrDrawingManager(this, dtOptions, options.fImmediateMode,
+ &fSingleOwner));
// GrBatchFontCache will eventually replace GrFontCache
fBatchFontCache = new GrBatchFontCache(this);
////////////////////////////////////////////////////////////////////////////////
-void GrContext::OverBudgetCB(void* data) {
- SkASSERT(data);
-
- GrContext* context = reinterpret_cast<GrContext*>(data);
-
- // Flush the GrBufferedDrawTarget to possibly free up some textures
- context->fFlushToReduceCacheSize = true;
-}
-
void GrContext::TextBlobCacheOverBudgetCB(void* data) {
SkASSERT(data);
-
- // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they
- // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move
- // drawText calls to below the GrContext level, but this is not trivial because they call
- // drawPath on SkGpuDevice
+ // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on GrDrawContext
+ // to perform a necessary flush. The solution is to move drawText calls to below the GrContext
+ // level, but this is not trivial because they call drawPath on SkGpuDevice.
GrContext* context = reinterpret_cast<GrContext*>(data);
context->flush();
}
////////////////////////////////////////////////////////////////////////////////
-void GrContext::flush(int flagsBitfield) {
+void GrContext::flush() {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
- bool flushed = false;
- if (kDiscard_FlushBit & flagsBitfield) {
- fDrawingManager->reset();
- } else {
- flushed = fDrawingManager->flush();
- }
- if (flushed) {
- fResourceCache->notifyFlushOccurred();
- }
- fFlushToReduceCacheSize = false;
+ fDrawingManager->flush();
}
bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
SkASSERT(fDrawingManager);
}
- ~AutoCheckFlush() { fDrawingManager->getContext()->flushIfNecessary(); }
+ ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
private:
GrDrawingManager* fDrawingManager;
* found in the LICENSE file.
*/
-#include "GrDrawContext.h"
#include "GrDrawingManager.h"
+
+#include "GrContext.h"
+#include "GrDrawContext.h"
#include "GrDrawTarget.h"
#include "GrPathRenderingDrawContext.h"
#include "GrResourceProvider.h"
fFlushState.reset();
}
-bool GrDrawingManager::flush() {
+void GrDrawingManager::internalFlush(GrResourceCache::FlushType type) {
if (fFlushing || this->wasAbandoned()) {
- return false;
+ return;
}
fFlushing = true;
bool flushed = false;
#endif
fFlushState.reset();
+ // We always have to notify the cache when it requested a flush so it can reset its state.
+ if (flushed || type == GrResourceCache::FlushType::kCacheRequested) {
+ fContext->getResourceCache()->notifyFlushOccurred(type);
+ }
fFlushing = false;
- return flushed;
}
GrDrawTarget* GrDrawingManager::newDrawTarget(GrRenderTarget* rt) {
#include "GrBatchFlushState.h"
#include "GrPathRendererChain.h"
#include "GrPathRenderer.h"
+#include "GrResourceCache.h"
#include "SkTDArray.h"
class GrContext;
GrPathRendererChain::DrawType drawType,
GrPathRenderer::StencilSupport* stencilSupport = NULL);
+ void flushIfNecessary() {
+ if (fContext->getResourceCache()->requestsFlush()) {
+ this->internalFlush(GrResourceCache::kCacheRequested);
+ } else if (fIsImmediateMode) {
+ this->internalFlush(GrResourceCache::kImmediateMode);
+ }
+ }
+
static bool ProgramUnitTest(GrContext* context, int maxStages);
private:
GrDrawingManager(GrContext* context, const GrDrawTarget::Options& optionsForDrawTargets,
- GrSingleOwner* singleOwner)
+ bool isImmediateMode, GrSingleOwner* singleOwner)
: fContext(context)
, fOptionsForDrawTargets(optionsForDrawTargets)
, fSingleOwner(singleOwner)
, fPathRendererChain(nullptr)
, fSoftwarePathRenderer(nullptr)
, fFlushState(context->getGpu(), context->resourceProvider())
- , fFlushing(false) {
+ , fFlushing(false)
+ , fIsImmediateMode(isImmediateMode) {
}
void abandon();
void cleanup();
void reset();
- /** Returns true if there was anything to flush and false otherwise */
- bool flush();
+ void flush() { this->internalFlush(GrResourceCache::FlushType::kExternal); }
+ void internalFlush(GrResourceCache::FlushType);
friend class GrContext; // for access to: ctor, abandon, reset & flush
GrBatchFlushState fFlushState;
bool fFlushing;
+
+ bool fIsImmediateMode;
};
#endif
, fBytes(0)
, fBudgetedCount(0)
, fBudgetedBytes(0)
- , fOverBudgetCB(nullptr)
- , fOverBudgetData(nullptr)
+ , fRequestFlush(false)
, fFlushTimestamps(nullptr)
, fLastFlushTimestampIndex(0)
, fPreferVRAMUseOverFlushes(caps->preferVRAMUseOverFlushes()) {
this->validate();
if (stillOverbudget) {
- // Despite the purge we're still over budget. Call our over budget callback. If this frees
- // any resources then we'll get notified and take appropriate action.
- (*fOverBudgetCB)(fOverBudgetData);
- this->validate();
+ // Set this so that GrDrawingManager will issue a flush to free up resources with pending
+ // IO that we were unable to purge in this pass.
+ fRequestFlush = true;
}
}
return fTimestamp++;
}
-void GrResourceCache::notifyFlushOccurred() {
- if (fFlushTimestamps) {
- SkASSERT(SkIsPow2(fMaxUnusedFlushes));
- fLastFlushTimestampIndex = (fLastFlushTimestampIndex + 1) & (fMaxUnusedFlushes - 1);
- // get the timestamp before accessing fFlushTimestamps because getNextTimestamp will
- // reallocate fFlushTimestamps on timestamp overflow.
- uint32_t timestamp = this->getNextTimestamp();
- fFlushTimestamps[fLastFlushTimestampIndex] = timestamp;
- this->purgeAsNeeded();
+void GrResourceCache::notifyFlushOccurred(FlushType type) {
+ switch (type) {
+ case FlushType::kImmediateMode:
+ break;
+ case FlushType::kCacheRequested:
+ SkASSERT(fRequestFlush);
+ fRequestFlush = false;
+ break;
+ case FlushType::kExternal:
+ if (fFlushTimestamps) {
+ SkASSERT(SkIsPow2(fMaxUnusedFlushes));
+ fLastFlushTimestampIndex = (fLastFlushTimestampIndex + 1) & (fMaxUnusedFlushes - 1);
+ // get the timestamp before accessing fFlushTimestamps because getNextTimestamp will
+ // reallocate fFlushTimestamps on timestamp overflow.
+ uint32_t timestamp = this->getNextTimestamp();
+ fFlushTimestamps[fLastFlushTimestampIndex] = timestamp;
+ }
+ break;
}
+ this->purgeAsNeeded();
}
void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
#include "GrGpuResource.h"
#include "GrGpuResourceCacheAccess.h"
#include "GrGpuResourcePriv.h"
+#include "GrResourceCache.h"
#include "GrResourceKey.h"
#include "SkMessageBus.h"
#include "SkRefCnt.h"
/** Purges all resources that don't have external owners. */
void purgeAllUnlocked();
- /**
- * The callback function used by the cache when it is still over budget after a purge. The
- * passed in 'data' is the same 'data' handed to setOverbudgetCallback.
- */
- typedef void (*PFOverBudgetCB)(void* data);
+ /** Returns true if the cache would like a flush to occur in order to make more resources
+ purgeable. */
+ bool requestsFlush() const { return fRequestFlush; }
- /**
- * Set the callback the cache should use when it is still over budget after a purge. The 'data'
- * provided here will be passed back to the callback. Note that the cache will attempt to purge
- * any resources newly freed by the callback.
- */
- void setOverBudgetCallback(PFOverBudgetCB overBudgetCB, void* data) {
- fOverBudgetCB = overBudgetCB;
- fOverBudgetData = data;
- }
-
- void notifyFlushOccurred();
+ enum FlushType {
+ kExternal,
+ kImmediateMode,
+ kCacheRequested,
+ };
+ void notifyFlushOccurred(FlushType);
#if GR_CACHE_STATS
struct Stats {
int fBudgetedCount;
size_t fBudgetedBytes;
- PFOverBudgetCB fOverBudgetCB;
- void* fOverBudgetData;
+ bool fRequestFlush;
// We keep track of the "timestamps" of the last n flushes. If a resource hasn't been used in
// that time then we well preemptively purge it to reduce memory usage.
if (nullptr == texture) {
return;
}
-
sk_sp<GrColorSpaceXform> colorSpaceXform =
GrColorSpaceXform::Make(bitmap.colorSpace(), fDrawContext->getColorSpace(),
bitmap.alphaType());
make_unique_key<1>(&k, i);
r->resourcePriv().setUniqueKey(k);
r->unref();
- cache->notifyFlushOccurred();
+ cache->notifyFlushOccurred(GrResourceCache::kExternal);
}
// Send flush notifications to the cache. Each flush should purge the oldest resource.
REPORTER_ASSERT(reporter, !SkToBool(r));
SkSafeUnref(r);
}
- cache->notifyFlushOccurred();
+ cache->notifyFlushOccurred(GrResourceCache::kExternal);
}
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());
} else {
r->unref();
}
- cache->notifyFlushOccurred();
+ cache->notifyFlushOccurred(GrResourceCache::kExternal);
}
for (int i = 0; i < kFlushCount; ++i) {
// Should get a resource purged every other flush.
REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount());
- cache->notifyFlushOccurred();
+ cache->notifyFlushOccurred(GrResourceCache::kExternal);
}
// Unref all the resources that we kept refs on in the first loop.
// get kFlushCount additional flushes. Then everything should be purged.
for (int i = 0; i < kFlushCount; ++i) {
REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount());
- cache->notifyFlushOccurred();
+ cache->notifyFlushOccurred(GrResourceCache::kExternal);
}
REPORTER_ASSERT(reporter, 0 == cache->getResourceCount());