Allow GrRecordingContext to create surfaceContexts
This is pulled out of https://skia-review.googlesource.com/c/skia/+/191287 (Move DrawingManager to RecordingContext)
Change-Id: Iba16acd9295ef7ca9d8cc96d439a907050228b6b
Reviewed-on: https://skia-review.googlesource.com/c/192684
Reviewed-by: Greg Daniel <egdaniel@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/include/gpu/GrContext.h b/include/gpu/GrContext.h
index 3c99ed6..2060630 100644
--- a/include/gpu/GrContext.h
+++ b/include/gpu/GrContext.h
@@ -289,10 +289,10 @@
protected:
GrContext(GrBackendApi, const GrContextOptions&, int32_t contextID = SK_InvalidGenID);
- GrContext* asDirectContext() override { return this; }
-
bool init(sk_sp<const GrCaps>, sk_sp<GrSkSLFPFactoryCache>) override;
+ GrContext* asDirectContext() override { return this; }
+
virtual GrAtlasManager* onGetAtlasManager() = 0;
// CONTEXT TODO: move GrDrawingManager to GrRecordingContext for real
diff --git a/include/private/GrContext_Base.h b/include/private/GrContext_Base.h
index bc18ac0..141a6d5 100644
--- a/include/private/GrContext_Base.h
+++ b/include/private/GrContext_Base.h
@@ -37,6 +37,8 @@
GrContext_Base(GrBackendApi backend, const GrContextOptions& options, uint32_t contextID);
+ virtual bool init(sk_sp<const GrCaps>, sk_sp<GrSkSLFPFactoryCache>);
+
/**
* An identifier for this context. The id is used by all compatible contexts. For example,
* if SkImages are created on one thread using an image creation context, then fed into a
@@ -55,6 +57,8 @@
*/
const GrContextOptions& options() const { return fOptions; }
+ bool explicitlyAllocateGPUResources() const;
+
const GrCaps* caps() const;
sk_sp<const GrCaps> refCaps() const;
@@ -64,8 +68,6 @@
virtual GrRecordingContext* asRecordingContext() { return nullptr; }
virtual GrContext* asDirectContext() { return nullptr; }
- virtual bool init(sk_sp<const GrCaps>, sk_sp<GrSkSLFPFactoryCache>);
-
private:
const GrBackendApi fBackend;
const GrContextOptions fOptions;
diff --git a/include/private/GrRecordingContext.h b/include/private/GrRecordingContext.h
index 53ab2c7..f5ea036 100644
--- a/include/private/GrRecordingContext.h
+++ b/include/private/GrRecordingContext.h
@@ -27,6 +27,7 @@
friend class GrRecordingContextPriv; // for hidden functions
GrRecordingContext(GrBackendApi, const GrContextOptions&, uint32_t contextID);
+ bool init(sk_sp<const GrCaps>, sk_sp<GrSkSLFPFactoryCache>) override;
void abandonContext() override;
@@ -36,6 +37,19 @@
sk_sp<GrOpMemoryPool> refOpMemoryPool();
GrOpMemoryPool* opMemoryPool();
+ sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
+ sk_sp<SkColorSpace> = nullptr,
+ const SkSurfaceProps* = nullptr);
+
+ sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrBackendFormat&,
+ const GrSurfaceDesc&,
+ GrSurfaceOrigin,
+ GrMipMapped,
+ SkBackingFit,
+ SkBudgeted,
+ sk_sp<SkColorSpace> colorSpace = nullptr,
+ const SkSurfaceProps* = nullptr);
+
/*
* Create a new render target context backed by a deferred-style
* GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
diff --git a/include/private/GrSurfaceProxy.h b/include/private/GrSurfaceProxy.h
index 1c7b01f..a754e71 100644
--- a/include/private/GrSurfaceProxy.h
+++ b/include/private/GrSurfaceProxy.h
@@ -16,6 +16,7 @@
#include "SkRect.h"
class GrCaps;
+class GrContext_Base;
class GrOpList;
class GrProxyProvider;
class GrRenderTargetOpList;
@@ -409,7 +410,7 @@
bool isWrapped_ForTesting() const;
- SkDEBUGCODE(void validate(GrContext*) const;)
+ SkDEBUGCODE(void validate(GrContext_Base*) const;)
// Provides access to functions that aren't part of the public API.
inline GrSurfaceProxyPriv priv();
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index df85d55..5e86602 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -52,6 +52,17 @@
fGlyphCache = nullptr;
}
+GrContext::~GrContext() {
+ ASSERT_SINGLE_OWNER
+
+ if (fDrawingManager) {
+ fDrawingManager->cleanup();
+ }
+ delete fResourceProvider;
+ delete fResourceCache;
+ delete fGlyphCache;
+}
+
bool GrContext::init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) {
ASSERT_SINGLE_OWNER
SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes
@@ -66,7 +77,7 @@
if (fGpu) {
fResourceCache = new GrResourceCache(this->caps(), this->singleOwner(), this->contextID());
fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, this->singleOwner(),
- this->options().fExplicitlyAllocateGPUResources);
+ this->explicitlyAllocateGPUResources());
}
if (fResourceCache) {
@@ -128,17 +139,6 @@
return true;
}
-GrContext::~GrContext() {
- ASSERT_SINGLE_OWNER
-
- if (fDrawingManager) {
- fDrawingManager->cleanup();
- }
- delete fResourceProvider;
- delete fResourceCache;
- delete fGlyphCache;
-}
-
sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
return fThreadSafeProxy;
}
diff --git a/src/gpu/GrContextPriv.cpp b/src/gpu/GrContextPriv.cpp
index 1ba47c8..584c438 100644
--- a/src/gpu/GrContextPriv.cpp
+++ b/src/gpu/GrContextPriv.cpp
@@ -43,6 +43,26 @@
return fContext->refOpMemoryPool();
}
+sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(
+ sk_sp<GrSurfaceProxy> proxy,
+ sk_sp<SkColorSpace> colorSpace,
+ const SkSurfaceProps* props) {
+ return fContext->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace), props);
+}
+
+sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(
+ const GrBackendFormat& format,
+ const GrSurfaceDesc& dstDesc,
+ GrSurfaceOrigin origin,
+ GrMipMapped mipMapped,
+ SkBackingFit fit,
+ SkBudgeted isDstBudgeted,
+ sk_sp<SkColorSpace> colorSpace,
+ const SkSurfaceProps* props) {
+ return fContext->makeDeferredSurfaceContext(format, dstDesc, origin, mipMapped, fit,
+ isDstBudgeted, std::move(colorSpace), props);
+}
+
sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext(
const GrBackendFormat& format,
SkBackingFit fit,
@@ -76,50 +96,6 @@
budgeted);
}
-sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
- sk_sp<SkColorSpace> colorSpace,
- const SkSurfaceProps* props) {
- ASSERT_SINGLE_OWNER_PRIV
-
- if (proxy->asRenderTargetProxy()) {
- return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
- std::move(colorSpace), props);
- } else {
- SkASSERT(proxy->asTextureProxy());
- SkASSERT(!props);
- return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
- }
-}
-
-sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrBackendFormat& format,
- const GrSurfaceDesc& dstDesc,
- GrSurfaceOrigin origin,
- GrMipMapped mipMapped,
- SkBackingFit fit,
- SkBudgeted isDstBudgeted,
- sk_sp<SkColorSpace> colorSpace,
- const SkSurfaceProps* props) {
- sk_sp<GrTextureProxy> proxy;
- if (GrMipMapped::kNo == mipMapped) {
- proxy = this->proxyProvider()->createProxy(format, dstDesc, origin, fit, isDstBudgeted);
- } else {
- SkASSERT(SkBackingFit::kExact == fit);
- proxy = this->proxyProvider()->createMipMapProxy(format, dstDesc, origin, isDstBudgeted);
- }
- if (!proxy) {
- return nullptr;
- }
-
- sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy),
- std::move(colorSpace),
- props);
- if (sContext && sContext->asRenderTargetContext()) {
- sContext->asRenderTargetContext()->discard();
- }
-
- return sContext;
-}
-
sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
GrSurfaceOrigin origin,
sk_sp<SkColorSpace> colorSpace) {
@@ -214,11 +190,11 @@
RETURN_IF_ABANDONED_PRIV
ASSERT_OWNED_PROXY_PRIV(proxy);
- fContext->fDrawingManager->flush(proxy);
+ fContext->drawingManager()->flush(proxy);
}
void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
- fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
+ fContext->drawingManager()->addOnFlushCallbackObject(onFlushCBObject);
}
void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
@@ -246,7 +222,7 @@
RETURN_IF_ABANDONED_PRIV
SkASSERT(proxy);
ASSERT_OWNED_PROXY_PRIV(proxy);
- fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
+ fContext->drawingManager()->prepareSurfaceForExternalIO(proxy, 0, nullptr);
}
static bool valid_premul_color_type(GrColorType ct) {
@@ -695,7 +671,7 @@
}
void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
- fContext->fDrawingManager->moveOpListsToDDL(ddl);
+ fContext->drawingManager()->moveOpListsToDDL(ddl);
}
void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
@@ -816,7 +792,7 @@
void GrContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
fContext->flush();
- fContext->fDrawingManager->testingOnly_removeOnFlushCallbackObject(cb);
+ fContext->drawingManager()->testingOnly_removeOnFlushCallbackObject(cb);
}
#endif
diff --git a/src/gpu/GrContextPriv.h b/src/gpu/GrContextPriv.h
index f2c66db..3279420 100644
--- a/src/gpu/GrContextPriv.h
+++ b/src/gpu/GrContextPriv.h
@@ -60,6 +60,19 @@
sk_sp<GrOpMemoryPool> refOpMemoryPool();
GrOpMemoryPool* opMemoryPool() { return fContext->opMemoryPool(); }
+ sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
+ sk_sp<SkColorSpace> = nullptr,
+ const SkSurfaceProps* = nullptr);
+
+ sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrBackendFormat&,
+ const GrSurfaceDesc&,
+ GrSurfaceOrigin,
+ GrMipMapped,
+ SkBackingFit,
+ SkBudgeted,
+ sk_sp<SkColorSpace> colorSpace = nullptr,
+ const SkSurfaceProps* = nullptr);
+
/*
* Create a new render target context backed by a deferred-style
* GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
@@ -102,19 +115,6 @@
*/
static sk_sp<GrContext> MakeDDL(const sk_sp<GrContextThreadSafeProxy>&);
- sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
- sk_sp<SkColorSpace> = nullptr,
- const SkSurfaceProps* = nullptr);
-
- sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrBackendFormat&,
- const GrSurfaceDesc&,
- GrSurfaceOrigin,
- GrMipMapped,
- SkBackingFit,
- SkBudgeted,
- sk_sp<SkColorSpace> colorSpace = nullptr,
- const SkSurfaceProps* = nullptr);
-
sk_sp<GrTextureContext> makeBackendTextureContext(const GrBackendTexture& tex,
GrSurfaceOrigin origin,
sk_sp<SkColorSpace> colorSpace);
diff --git a/src/gpu/GrContext_Base.cpp b/src/gpu/GrContext_Base.cpp
index 122a281..7c45267 100644
--- a/src/gpu/GrContext_Base.cpp
+++ b/src/gpu/GrContext_Base.cpp
@@ -11,6 +11,12 @@
#include "GrCaps.h"
#include "GrSkSLFPFactoryCache.h"
+#ifdef SK_DISABLE_EXPLICIT_GPU_RESOURCE_ALLOCATION
+static const bool kDefaultExplicitlyAllocateGPUResources = false;
+#else
+static const bool kDefaultExplicitlyAllocateGPUResources = true;
+#endif
+
static int32_t next_id() {
static std::atomic<int32_t> nextID{1};
int32_t id;
@@ -30,11 +36,6 @@
GrContext_Base::~GrContext_Base() { }
-const GrCaps* GrContext_Base::caps() const { return fCaps.get(); }
-sk_sp<const GrCaps> GrContext_Base::refCaps() const { return fCaps; }
-
-sk_sp<GrSkSLFPFactoryCache> GrContext_Base::fpFactoryCache() { return fFPFactoryCache; }
-
bool GrContext_Base::init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) {
SkASSERT(caps && FPFactoryCache);
@@ -43,6 +44,23 @@
return true;
}
+bool GrContext_Base::explicitlyAllocateGPUResources() const {
+ if (GrContextOptions::Enable::kNo == fOptions.fExplicitlyAllocateGPUResources) {
+ return false;
+ }
+
+ if (GrContextOptions::Enable::kYes == fOptions.fExplicitlyAllocateGPUResources) {
+ return true;
+ }
+
+ return kDefaultExplicitlyAllocateGPUResources;
+}
+
+const GrCaps* GrContext_Base::caps() const { return fCaps.get(); }
+sk_sp<const GrCaps> GrContext_Base::refCaps() const { return fCaps; }
+
+sk_sp<GrSkSLFPFactoryCache> GrContext_Base::fpFactoryCache() { return fFPFactoryCache; }
+
///////////////////////////////////////////////////////////////////////////////////////////////////
sk_sp<const GrCaps> GrBaseContextPriv::refCaps() const {
return fContext->refCaps();
diff --git a/src/gpu/GrRecordingContext.cpp b/src/gpu/GrRecordingContext.cpp
index ebbb68c..615e0d7 100644
--- a/src/gpu/GrRecordingContext.cpp
+++ b/src/gpu/GrRecordingContext.cpp
@@ -14,8 +14,12 @@
#include "GrRecordingContextPriv.h"
#include "GrRenderTargetContext.h"
#include "GrSkSLFPFactoryCache.h"
+#include "GrTextureContext.h"
#include "SkGr.h"
+#define ASSERT_SINGLE_OWNER_PRIV \
+ SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
+
GrRecordingContext::GrRecordingContext(GrBackendApi backend,
const GrContextOptions& options,
uint32_t contextID)
@@ -24,6 +28,15 @@
GrRecordingContext::~GrRecordingContext() { }
+bool GrRecordingContext::init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> cache) {
+
+ if (!INHERITED::init(std::move(caps), std::move(cache))) {
+ return false;
+ }
+
+ return true;
+}
+
void GrRecordingContext::abandonContext() {
INHERITED::abandonContext();
}
@@ -44,6 +57,52 @@
return this->refOpMemoryPool().get();
}
+sk_sp<GrSurfaceContext> GrRecordingContext::makeWrappedSurfaceContext(
+ sk_sp<GrSurfaceProxy> proxy,
+ sk_sp<SkColorSpace> colorSpace,
+ const SkSurfaceProps* props) {
+ ASSERT_SINGLE_OWNER_PRIV
+
+ if (proxy->asRenderTargetProxy()) {
+ return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
+ std::move(colorSpace), props);
+ } else {
+ SkASSERT(proxy->asTextureProxy());
+ SkASSERT(!props);
+ return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
+ }
+}
+
+sk_sp<GrSurfaceContext> GrRecordingContext::makeDeferredSurfaceContext(
+ const GrBackendFormat& format,
+ const GrSurfaceDesc& dstDesc,
+ GrSurfaceOrigin origin,
+ GrMipMapped mipMapped,
+ SkBackingFit fit,
+ SkBudgeted isDstBudgeted,
+ sk_sp<SkColorSpace> colorSpace,
+ const SkSurfaceProps* props) {
+ sk_sp<GrTextureProxy> proxy;
+ if (GrMipMapped::kNo == mipMapped) {
+ proxy = this->proxyProvider()->createProxy(format, dstDesc, origin, fit, isDstBudgeted);
+ } else {
+ SkASSERT(SkBackingFit::kExact == fit);
+ proxy = this->proxyProvider()->createMipMapProxy(format, dstDesc, origin, isDstBudgeted);
+ }
+ if (!proxy) {
+ return nullptr;
+ }
+
+ sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy),
+ std::move(colorSpace),
+ props);
+ if (sContext && sContext->asRenderTargetContext()) {
+ sContext->asRenderTargetContext()->discard();
+ }
+
+ return sContext;
+}
+
sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContext(
const GrBackendFormat& format,
SkBackingFit fit,
@@ -161,6 +220,26 @@
return fContext->refOpMemoryPool();
}
+sk_sp<GrSurfaceContext> GrRecordingContextPriv::makeWrappedSurfaceContext(
+ sk_sp<GrSurfaceProxy> proxy,
+ sk_sp<SkColorSpace> colorSpace,
+ const SkSurfaceProps* props) {
+ return fContext->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace), props);
+}
+
+sk_sp<GrSurfaceContext> GrRecordingContextPriv::makeDeferredSurfaceContext(
+ const GrBackendFormat& format,
+ const GrSurfaceDesc& dstDesc,
+ GrSurfaceOrigin origin,
+ GrMipMapped mipMapped,
+ SkBackingFit fit,
+ SkBudgeted isDstBudgeted,
+ sk_sp<SkColorSpace> colorSpace,
+ const SkSurfaceProps* props) {
+ return fContext->makeDeferredSurfaceContext(format, dstDesc, origin, mipMapped, fit,
+ isDstBudgeted, std::move(colorSpace), props);
+}
+
sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContext(
const GrBackendFormat& format,
SkBackingFit fit,
diff --git a/src/gpu/GrRecordingContextPriv.h b/src/gpu/GrRecordingContextPriv.h
index e18035f..628c248 100644
--- a/src/gpu/GrRecordingContextPriv.h
+++ b/src/gpu/GrRecordingContextPriv.h
@@ -48,6 +48,19 @@
sk_sp<GrOpMemoryPool> refOpMemoryPool();
GrOpMemoryPool* opMemoryPool() { return fContext->opMemoryPool(); }
+ sk_sp<GrSurfaceContext> makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy>,
+ sk_sp<SkColorSpace> = nullptr,
+ const SkSurfaceProps* = nullptr);
+
+ sk_sp<GrSurfaceContext> makeDeferredSurfaceContext(const GrBackendFormat&,
+ const GrSurfaceDesc&,
+ GrSurfaceOrigin,
+ GrMipMapped,
+ SkBackingFit,
+ SkBudgeted,
+ sk_sp<SkColorSpace> colorSpace = nullptr,
+ const SkSurfaceProps* = nullptr);
+
/*
* Create a new render target context backed by a deferred-style
* GrRenderTargetProxy. We guarantee that "asTextureProxy" will succeed for
diff --git a/src/gpu/GrResourceProvider.cpp b/src/gpu/GrResourceProvider.cpp
index c6e30da..622cea2 100644
--- a/src/gpu/GrResourceProvider.cpp
+++ b/src/gpu/GrResourceProvider.cpp
@@ -27,31 +27,18 @@
const uint32_t GrResourceProvider::kMinScratchTextureSize = 16;
-#ifdef SK_DISABLE_EXPLICIT_GPU_RESOURCE_ALLOCATION
-static const bool kDefaultExplicitlyAllocateGPUResources = false;
-#else
-static const bool kDefaultExplicitlyAllocateGPUResources = true;
-#endif
-
#define ASSERT_SINGLE_OWNER \
SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
GrResourceProvider::GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner,
- GrContextOptions::Enable explicitlyAllocateGPUResources)
+ bool explicitlyAllocateGPUResources)
: fCache(cache)
, fGpu(gpu)
+ , fExplicitlyAllocateGPUResources(explicitlyAllocateGPUResources)
#ifdef SK_DEBUG
, fSingleOwner(owner)
#endif
{
- if (GrContextOptions::Enable::kNo == explicitlyAllocateGPUResources) {
- fExplicitlyAllocateGPUResources = false;
- } else if (GrContextOptions::Enable::kYes == explicitlyAllocateGPUResources) {
- fExplicitlyAllocateGPUResources = true;
- } else {
- fExplicitlyAllocateGPUResources = kDefaultExplicitlyAllocateGPUResources;
- }
-
fCaps = sk_ref_sp(fGpu->caps());
}
diff --git a/src/gpu/GrResourceProvider.h b/src/gpu/GrResourceProvider.h
index af3db69..1a9924b 100644
--- a/src/gpu/GrResourceProvider.h
+++ b/src/gpu/GrResourceProvider.h
@@ -54,7 +54,7 @@
};
GrResourceProvider(GrGpu*, GrResourceCache*, GrSingleOwner*,
- GrContextOptions::Enable explicitlyAllocateGPUResources);
+ bool explicitlyAllocateGPUResources);
/**
* Finds a resource in the cache, based on the specified key. Prior to calling this, the caller
diff --git a/src/gpu/GrSurfaceProxy.cpp b/src/gpu/GrSurfaceProxy.cpp
index f2e4485..de6bc8a 100644
--- a/src/gpu/GrSurfaceProxy.cpp
+++ b/src/gpu/GrSurfaceProxy.cpp
@@ -318,7 +318,7 @@
}
#ifdef SK_DEBUG
-void GrSurfaceProxy::validate(GrContext* context) const {
+void GrSurfaceProxy::validate(GrContext_Base* context) const {
if (fTarget) {
SkASSERT(fTarget->getContext() == context);
}