Create debug only SkSingleOwner

This is so Gpu code can guard against improper multithreaded usage in
debug builds

TBR=bsalomon@google.com
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1555953004

Review URL: https://codereview.chromium.org/1555953004
This commit is contained in:
joshualitt 2016-01-06 08:26:09 -08:00 committed by Commit bot
parent e114dd6316
commit 1de610a528
6 changed files with 121 additions and 2 deletions

View File

@ -54,6 +54,9 @@
'<(skia_include_path)/gpu/gl/GrGLSLPrettyPrint.h',
'<(skia_include_path)/gpu/gl/GrGLTypes.h',
# Private includes
'<(skia_include_path)/private/GrSingleOwner.h',
'<(skia_src_path)/gpu/GrAutoLocaleSetter.h',
'<(skia_src_path)/gpu/GrAllocator.h',
'<(skia_src_path)/gpu/GrBatchAtlas.cpp',

View File

@ -15,9 +15,10 @@
#include "GrRenderTarget.h"
#include "GrTextureProvider.h"
#include "SkMatrix.h"
#include "../private/SkMutex.h"
#include "SkPathEffect.h"
#include "SkTypes.h"
#include "../private/GrSingleOwner.h"
#include "../private/SkMutex.h"
struct GrBatchAtlasConfig;
class GrBatchFontCache;
@ -390,6 +391,9 @@ private:
SkMutex fReadPixelsMutex;
SkMutex fTestPMConversionsMutex;
// In debug builds we guard against improper thread handling
SkDEBUGCODE(mutable GrSingleOwner fSingleOwner;)
struct CleanUpData {
PFCleanUpFunc fFunc;
void* fInfo;

View File

@ -12,6 +12,7 @@
#include "GrRenderTarget.h"
#include "SkRefCnt.h"
#include "SkSurfaceProps.h"
#include "../private/GrSingleOwner.h"
class GrClip;
class GrContext;
@ -306,6 +307,9 @@ private:
GrTextContext* fTextContext; // lazily gotten from GrContext::DrawingManager
SkSurfaceProps fSurfaceProps;
// In debug builds we guard against improper thread handling
SkDEBUGCODE(mutable GrSingleOwner fSingleOwner;)
};
#endif

View File

@ -0,0 +1,52 @@
/*
* 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 GrSingleOwner_DEFINED
#define GrSingleOwner_DEFINED
#include "SkTypes.h"
#ifdef SK_DEBUG
#include "SkMutex.h"
#include "SkThreadID.h"
// This is a debug tool to verify an object is only being used from one thread at a time.
class GrSingleOwner {
public:
GrSingleOwner() : fOwner(kIllegalThreadID), fReentranceCount(0) {}
struct AutoEnforce {
AutoEnforce(GrSingleOwner* so) : fSO(so) { fSO->enter(); }
~AutoEnforce() { fSO->exit(); }
GrSingleOwner* fSO;
};
private:
void enter() {
SkAutoMutexAcquire lock(fMutex);
SkThreadID self = SkGetThreadID();
SkASSERT(fOwner == self || fOwner == kIllegalThreadID);
fReentranceCount++;
fOwner = self;
}
void exit() {
SkAutoMutexAcquire lock(fMutex);
fReentranceCount--;
if (fReentranceCount == 0) {
fOwner = kIllegalThreadID;
}
}
SkMutex fMutex;
SkThreadID fOwner; // guarded by fMutex
int fReentranceCount; // guarded by fMutex
};
#endif
#endif

View File

@ -23,6 +23,8 @@
#include "text/GrTextBlobCache.h"
#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
#define ASSERT_SINGLE_OWNER \
SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
#define RETURN_IF_ABANDONED if (fDrawingManager->abandoned()) { return; }
#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->abandoned()) { return false; }
#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->abandoned()) { return nullptr; }
@ -66,6 +68,7 @@ GrContext::GrContext() : fUniqueID(next_id()) {
bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
const GrContextOptions& options) {
ASSERT_SINGLE_OWNER
SkASSERT(!fGpu);
fGpu = GrGpu::Create(backend, backendContext, options, this);
@ -77,6 +80,8 @@ bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
}
void GrContext::initCommon(const GrContextOptions& options) {
ASSERT_SINGLE_OWNER
fCaps = SkRef(fGpu->caps());
fResourceCache = new GrResourceCache(fCaps);
fResourceCache->setOverBudgetCallback(OverBudgetCB, this);
@ -99,6 +104,8 @@ void GrContext::initCommon(const GrContextOptions& options) {
}
GrContext::~GrContext() {
ASSERT_SINGLE_OWNER
if (!fGpu) {
SkASSERT(!fCaps);
return;
@ -121,6 +128,8 @@ GrContext::~GrContext() {
}
void GrContext::abandonContext() {
ASSERT_SINGLE_OWNER
fResourceProvider->abandon();
// Need to abandon the drawing manager first so all the render targets
@ -139,10 +148,13 @@ void GrContext::abandonContext() {
}
void GrContext::resetContext(uint32_t state) {
ASSERT_SINGLE_OWNER
fGpu->markContextDirty(state);
}
void GrContext::freeGpuResources() {
ASSERT_SINGLE_OWNER
this->flush();
fBatchFontCache->freeAll();
@ -154,6 +166,8 @@ void GrContext::freeGpuResources() {
}
void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
ASSERT_SINGLE_OWNER
if (resourceCount) {
*resourceCount = fResourceCache->getBudgetedResourceCount();
}
@ -187,6 +201,7 @@ void GrContext::TextBlobCacheOverBudgetCB(void* data) {
////////////////////////////////////////////////////////////////////////////////
void GrContext::flush(int flagsBitfield) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
if (kDiscard_FlushBit & flagsBitfield) {
@ -221,6 +236,7 @@ bool GrContext::writeSurfacePixels(GrSurface* surface,
int left, int top, int width, int height,
GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
uint32_t pixelOpsFlags) {
ASSERT_SINGLE_OWNER
RETURN_FALSE_IF_ABANDONED
ASSERT_OWNED_RESOURCE(surface);
SkASSERT(surface);
@ -359,6 +375,7 @@ bool GrContext::readSurfacePixels(GrSurface* src,
int left, int top, int width, int height,
GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
uint32_t flags) {
ASSERT_SINGLE_OWNER
RETURN_FALSE_IF_ABANDONED
ASSERT_OWNED_RESOURCE(src);
SkASSERT(src);
@ -486,6 +503,7 @@ bool GrContext::readSurfacePixels(GrSurface* src,
}
void GrContext::prepareSurfaceForExternalIO(GrSurface* surface) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkASSERT(surface);
ASSERT_OWNED_RESOURCE(surface);
@ -500,6 +518,7 @@ void GrContext::prepareSurfaceForExternalIO(GrSurface* surface) {
void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
if (!src || !dst) {
return;
@ -526,6 +545,7 @@ void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRe
}
void GrContext::flushSurfaceWrites(GrSurface* surface) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
if (surface->surfacePriv().hasPendingWrite()) {
this->flush();
@ -535,6 +555,8 @@ void GrContext::flushSurfaceWrites(GrSurface* surface) {
////////////////////////////////////////////////////////////////////////////////
int GrContext::getRecommendedSampleCount(GrPixelConfig config,
SkScalar dpi) const {
ASSERT_SINGLE_OWNER
if (!this->caps()->isConfigRenderable(config, true)) {
return 0;
}
@ -552,10 +574,12 @@ int GrContext::getRecommendedSampleCount(GrPixelConfig config,
GrDrawContext* GrContext::drawContext(GrRenderTarget* rt, const SkSurfaceProps* surfaceProps) {
ASSERT_SINGLE_OWNER
return fDrawingManager->drawContext(rt, surfaceProps);
}
bool GrContext::abandoned() const {
bool GrContext::abandoned() const {
ASSERT_SINGLE_OWNER
return fDrawingManager->abandoned();
}
@ -570,6 +594,7 @@ void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
}
void GrContext::testPMConversionsIfNecessary(uint32_t flags) {
ASSERT_SINGLE_OWNER
if (SkToBool(kUnpremul_PixelOpsFlag & flags)) {
SkAutoMutexAcquire ama(fTestPMConversionsMutex);
if (!fDidTestPMConversions) {
@ -582,6 +607,7 @@ void GrContext::testPMConversionsIfNecessary(uint32_t flags) {
const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
bool swapRAndB,
const SkMatrix& matrix) const {
ASSERT_SINGLE_OWNER
// We should have already called this->testPMConversionsIfNecessary().
SkASSERT(fDidTestPMConversions);
GrConfigConversionEffect::PMConversion pmToUPM =
@ -596,6 +622,7 @@ const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
bool swapRAndB,
const SkMatrix& matrix) const {
ASSERT_SINGLE_OWNER
// We should have already called this->testPMConversionsIfNecessary().
SkASSERT(fDidTestPMConversions);
GrConfigConversionEffect::PMConversion upmToPM =
@ -608,6 +635,7 @@ const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
}
bool GrContext::didFailPMUPMConversionTest() const {
ASSERT_SINGLE_OWNER
// We should have already called this->testPMConversionsIfNecessary().
SkASSERT(fDidTestPMConversions);
// The PM<->UPM tests fail or succeed together so we only need to check one.
@ -617,6 +645,7 @@ bool GrContext::didFailPMUPMConversionTest() const {
//////////////////////////////////////////////////////////////////////////////
void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
ASSERT_SINGLE_OWNER
if (maxTextures) {
*maxTextures = fResourceCache->getMaxResourceCount();
}
@ -626,11 +655,13 @@ void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes
}
void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
ASSERT_SINGLE_OWNER
fResourceCache->setLimits(maxTextures, maxTextureBytes);
}
//////////////////////////////////////////////////////////////////////////////
void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
ASSERT_SINGLE_OWNER
fResourceCache->dumpMemoryStatistics(traceMemoryDump);
}

View File

@ -27,6 +27,8 @@
#include "text/GrStencilAndCoverTextContext.h"
#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == fDrawingManager->getContext())
#define ASSERT_SINGLE_OWNER \
SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
#define RETURN_IF_ABANDONED if (fDrawingManager->abandoned()) { return; }
#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->abandoned()) { return false; }
#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->abandoned()) { return nullptr; }
@ -69,10 +71,12 @@ void GrDrawContext::validate() const {
#endif
GrDrawContext::~GrDrawContext() {
ASSERT_SINGLE_OWNER
SkSafeUnref(fDrawTarget);
}
GrDrawTarget* GrDrawContext::getDrawTarget() {
ASSERT_SINGLE_OWNER
SkDEBUGCODE(this->validate();)
if (!fDrawTarget || fDrawTarget->isClosed()) {
@ -83,6 +87,7 @@ GrDrawTarget* GrDrawContext::getDrawTarget() {
}
void GrDrawContext::copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -94,6 +99,7 @@ void GrDrawContext::drawText(const GrClip& clip, const GrPaint& grPaint,
const SkMatrix& viewMatrix,
const char text[], size_t byteLength,
SkScalar x, SkScalar y, const SkIRect& clipBounds) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -111,6 +117,7 @@ void GrDrawContext::drawPosText(const GrClip& clip, const GrPaint& grPaint,
const char text[], size_t byteLength,
const SkScalar pos[], int scalarsPerPosition,
const SkPoint& offset, const SkIRect& clipBounds) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -127,6 +134,7 @@ void GrDrawContext::drawTextBlob(const GrClip& clip, const SkPaint& skPaint,
const SkMatrix& viewMatrix, const SkTextBlob* blob,
SkScalar x, SkScalar y,
SkDrawFilter* filter, const SkIRect& clipBounds) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -138,6 +146,7 @@ void GrDrawContext::drawTextBlob(const GrClip& clip, const SkPaint& skPaint,
}
void GrDrawContext::discard() {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -148,6 +157,7 @@ void GrDrawContext::discard() {
void GrDrawContext::clear(const SkIRect* rect,
const GrColor color,
bool canIgnoreRect) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -159,6 +169,7 @@ void GrDrawContext::clear(const SkIRect* rect,
void GrDrawContext::drawPaint(const GrClip& clip,
const GrPaint& origPaint,
const SkMatrix& viewMatrix) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -224,6 +235,7 @@ void GrDrawContext::drawRect(const GrClip& clip,
const SkMatrix& viewMatrix,
const SkRect& rect,
const GrStrokeInfo* strokeInfo) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -323,6 +335,7 @@ void GrDrawContext::fillRectToRect(const GrClip& clip,
const SkMatrix& viewMatrix,
const SkRect& rectToDraw,
const SkRect& localRect) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -350,6 +363,7 @@ void GrDrawContext::fillRectWithLocalMatrix(const GrClip& clip,
const SkMatrix& viewMatrix,
const SkRect& rectToDraw,
const SkMatrix& localMatrix) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -381,6 +395,7 @@ void GrDrawContext::drawVertices(const GrClip& clip,
const GrColor colors[],
const uint16_t indices[],
int indexCount) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -424,6 +439,7 @@ void GrDrawContext::drawAtlas(const GrClip& clip,
const SkRSXform xform[],
const SkRect texRect[],
const SkColor colors[]) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -446,6 +462,7 @@ void GrDrawContext::drawRRect(const GrClip& clip,
const SkMatrix& viewMatrix,
const SkRRect& rrect,
const GrStrokeInfo& strokeInfo) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -482,6 +499,7 @@ void GrDrawContext::drawDRRect(const GrClip& clip,
const SkMatrix& viewMatrix,
const SkRRect& outer,
const SkRRect& inner) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -519,6 +537,7 @@ void GrDrawContext::drawOval(const GrClip& clip,
const SkMatrix& viewMatrix,
const SkRect& oval,
const GrStrokeInfo& strokeInfo) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -555,6 +574,7 @@ void GrDrawContext::drawImageNine(const GrClip& clip,
int imageHeight,
const SkIRect& center,
const SkRect& dst) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -621,6 +641,7 @@ static bool is_nested_rects(const SkMatrix& viewMatrix,
void GrDrawContext::drawBatch(const GrClip& clip,
const GrPaint& paint, GrDrawBatch* batch) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -632,6 +653,7 @@ void GrDrawContext::drawBatch(const GrClip& clip,
void GrDrawContext::drawPathBatch(const GrPipelineBuilder& pipelineBuilder,
GrDrawPathBatchBase* batch) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -645,6 +667,7 @@ void GrDrawContext::drawPath(const GrClip& clip,
const SkMatrix& viewMatrix,
const SkPath& path,
const GrStrokeInfo& strokeInfo) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)
@ -704,6 +727,7 @@ void GrDrawContext::internalDrawPath(GrPipelineBuilder* pipelineBuilder,
bool useAA,
const SkPath& path,
const GrStrokeInfo& strokeInfo) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkASSERT(!path.isEmpty());
@ -800,6 +824,7 @@ void GrDrawContext::internalDrawPath(GrPipelineBuilder* pipelineBuilder,
}
void GrDrawContext::drawBatch(GrPipelineBuilder* pipelineBuilder, GrDrawBatch* batch) {
ASSERT_SINGLE_OWNER
RETURN_IF_ABANDONED
SkDEBUGCODE(this->validate();)