[graphite] Add ContextFactory

Bug: skia:12466
Change-Id: I3299940af72cffde3904cf5f6262955807d6d1bc
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/453637
Reviewed-by: Jim Van Verth <jvanverth@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
This commit is contained in:
Robert Phillips 2021-09-30 11:41:16 -04:00 committed by SkCQ
parent 67f443beb4
commit 091694fe16
14 changed files with 192 additions and 25 deletions

View File

@ -1800,6 +1800,8 @@ if (skia_enable_tools) {
sources -= [ "tools/gpu/TestOps.h" ]
}
if (skia_enable_graphite) {
sources += [ "tools/graphite/ContextFactory.h" ]
sources += [ "tools/graphite/ContextFactory.cpp" ]
sources += [ "tools/graphite/GraphiteTestContext.h" ]
sources += [ "tools/graphite/GraphiteTestContext.cpp" ]
if (skia_use_metal) {

View File

@ -80,7 +80,10 @@
#endif
#ifdef SK_GRAPHITE_ENABLED
#include "experimental/graphite/include/Context.h"
#include "experimental/graphite/include/SkStuff.h"
#include "tools/graphite/ContextFactory.h"
#include "tools/graphite/GraphiteTestContext.h"
#endif
#if defined(SK_ENABLE_ANDROID_UTILS)
@ -2122,9 +2125,14 @@ Result GraphiteSink::draw(const Src& src,
SkBitmap* dst,
SkWStream* dstStream,
SkString* log) const {
using ContextType = sk_graphite_test::ContextFactory::ContextType;
SkImageInfo ii = SkImageInfo::Make(src.size(), kRGBA_8888_SkColorType, kPremul_SkAlphaType);
sk_sp<SkSurface> surface = MakeGraphite(ii);
sk_graphite_test::ContextFactory factory;
auto [_, context] = factory.getContextInfo(ContextType::kMetal);
sk_sp<SkSurface> surface = MakeGraphite(std::move(context), ii);
if (!surface) {
return Result::Fatal("Could not create a surface.");
}

View File

@ -13,7 +13,11 @@
struct SkImageInfo;
class SkSurface;
// TODO: Should be SkSurface.h
sk_sp<SkSurface> MakeGraphite(const SkImageInfo&);
namespace skgpu {
class Context;
}
// TODO: Should be in SkSurface.h
sk_sp<SkSurface> MakeGraphite(sk_sp<skgpu::Context>, const SkImageInfo&);
#endif // SkStuff_DEFINED

View File

@ -7,6 +7,7 @@
#include "experimental/graphite/src/Device.h"
#include "experimental/graphite/include/Context.h"
#include "experimental/graphite/include/SkStuff.h"
#include "experimental/graphite/src/DrawList.h"
#include "experimental/graphite/src/SurfaceDrawContext.h"
@ -18,20 +19,22 @@
#include "src/core/SkMatrixPriv.h"
#include "src/core/SkPaintPriv.h"
#include "src/core/SkSpecialImage.h"
namespace skgpu {
sk_sp<Device> Device::Make(const SkImageInfo& ii) {
sk_sp<Device> Device::Make(sk_sp<Context> context, const SkImageInfo& ii) {
sk_sp<SurfaceDrawContext> sdc = SurfaceDrawContext::Make(ii);
if (!sdc) {
return nullptr;
}
return sk_sp<Device>(new Device(std::move(sdc)));
return sk_sp<Device>(new Device(std::move(context), std::move(sdc)));
}
Device::Device(sk_sp<SurfaceDrawContext> sdc)
Device::Device(sk_sp<Context> context, sk_sp<SurfaceDrawContext> sdc)
: SkBaseDevice(sdc->imageInfo(), SkSurfaceProps())
, fContext(std::move(context))
, fSDC(std::move(sdc)) {
SkASSERT(SkToBool(fSDC));
}
@ -40,11 +43,11 @@ SkBaseDevice* Device::onCreateDevice(const CreateInfo& info, const SkPaint*) {
// TODO: Inspect the paint and create info to determine if there's anything that has to be
// modified to support inline subpasses.
// TODO: onCreateDevice really should return sk_sp<SkBaseDevice>...
return Make(info.fInfo).release();
return Make(fContext, info.fInfo).release();
}
sk_sp<SkSurface> Device::makeSurface(const SkImageInfo& ii, const SkSurfaceProps& /* props */) {
return MakeGraphite(ii);
return MakeGraphite(fContext, ii);
}
bool Device::onReadPixels(const SkPixmap& pm, int x, int y) {
@ -202,4 +205,16 @@ void Device::drawPath(const SkPath& path, const SkPaint& paint, bool pathIsMutab
}
}
sk_sp<SkSpecialImage> Device::makeSpecial(const SkBitmap&) {
return nullptr;
}
sk_sp<SkSpecialImage> Device::makeSpecial(const SkImage*) {
return nullptr;
}
sk_sp<SkSpecialImage> Device::snapSpecial(const SkIRect& subset, bool forceCopy) {
return nullptr;
}
} // namespace skgpu

View File

@ -12,11 +12,14 @@
namespace skgpu {
class Context;
class SurfaceDrawContext;
class Device final : public SkBaseDevice {
public:
static sk_sp<Device> Make(const SkImageInfo&);
static sk_sp<Device> Make(sk_sp<Context>, const SkImageInfo&);
sk_sp<Context> refContext() { return fContext; }
protected:
// Clipping
@ -93,15 +96,14 @@ protected:
void drawSpecial(SkSpecialImage*, const SkMatrix& localToDevice,
const SkSamplingOptions&, const SkPaint&) override {}
sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override { return nullptr; }
sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override { return nullptr; }
sk_sp<SkSpecialImage> snapSpecial(const SkIRect& subset, bool forceCopy = false) override {
return nullptr;
}
sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
sk_sp<SkSpecialImage> snapSpecial(const SkIRect& subset, bool forceCopy = false) override;
private:
Device(sk_sp<SurfaceDrawContext>);
Device(sk_sp<Context>, sk_sp<SurfaceDrawContext>);
sk_sp<Context> fContext;
sk_sp<SurfaceDrawContext> fSDC;
};

View File

@ -34,4 +34,8 @@ Pipeline* ResourceProvider::findOrCreatePipeline(const PipelineDesc& desc) {
return pso;
}
std::unique_ptr<CommandBuffer> onCreateCommandBuffer() {
return nullptr;
}
} // namespace skgpu

View File

@ -26,7 +26,7 @@ public:
protected:
ResourceProvider();
virtual std::unique_ptr<CommandBuffer> onCreateCommandBuffer() { return nullptr; }
virtual std::unique_ptr<CommandBuffer> onCreateCommandBuffer();
virtual Pipeline* onCreatePipeline() { return nullptr; }
private:

View File

@ -7,12 +7,13 @@
#include "experimental/graphite/include/SkStuff.h"
#include "experimental/graphite/include/Context.h"
#include "experimental/graphite/src/Device.h"
#include "experimental/graphite/src/Surface_Graphite.h"
sk_sp<SkSurface> MakeGraphite(const SkImageInfo& ii) {
sk_sp<SkSurface> MakeGraphite(sk_sp<skgpu::Context> context, const SkImageInfo& ii) {
sk_sp<skgpu::Device> device = skgpu::Device::Make(ii);
sk_sp<skgpu::Device> device = skgpu::Device::Make(std::move(context), ii);
if (!device) {
return nullptr;
}

View File

@ -7,6 +7,7 @@
#include "experimental/graphite/src/Surface_Graphite.h"
#include "experimental/graphite/include/Context.h"
#include "experimental/graphite/include/SkStuff.h"
#include "experimental/graphite/src/Device.h"
#include "experimental/graphite/src/Image_Graphite.h"
@ -23,7 +24,7 @@ Surface_Graphite::~Surface_Graphite() {}
SkCanvas* Surface_Graphite::onNewCanvas() { return new SkCanvas(fDevice); }
sk_sp<SkSurface> Surface_Graphite::onNewSurface(const SkImageInfo& ii) {
return MakeGraphite(ii);
return MakeGraphite(fDevice->refContext(), ii);
}
sk_sp<SkImage> Surface_Graphite::onNewImageSnapshot(const SkIRect* subset) {

View File

@ -0,0 +1,54 @@
/*
* Copyright 2021 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "tools/graphite/ContextFactory.h"
#include "experimental/graphite/include/Context.h"
#ifdef SK_METAL
#include "tools/graphite/mtl/GraphiteMtlTestContext.h"
#endif
namespace sk_graphite_test {
std::tuple<GraphiteTestContext*, sk_sp<skgpu::Context>> ContextFactory::getContextInfo(
ContextType type) {
for (ContextInfo& c : fContexts) {
if (c.type() == type) {
return { c.testContext(), c.refContext() };
}
}
std::unique_ptr<GraphiteTestContext> testCtx;
switch (type) {
case ContextType::kMetal: {
#ifdef SK_METAL
testCtx = mtl::TestContext::Make();
#endif
} break;
default:
break;
}
if (!testCtx) {
return {};
}
sk_sp<skgpu::Context> context = testCtx->makeContext();
if (!context) {
return {};
}
fContexts.push_back({ type, std::move(testCtx), std::move(context) });
return { fContexts.back().testContext(), fContexts.back().refContext() };
}
} // namespace sk_graphite_test

View File

@ -0,0 +1,76 @@
/*
* Copyright 2021 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef sk_graphite_test_ContextFactory_DEFINED
#define sk_graphite_test_ContextFactory_DEFINED
#include <vector>
#include "experimental/graphite/include/GraphiteTypes.h"
#include "include/core/SkRefCnt.h"
#include "tools/graphite/GraphiteTestContext.h"
namespace skgpu {
class Context;
};
namespace sk_graphite_test {
class ContextFactory {
public:
enum class ContextType {
kMetal,
kMock,
};
class ContextInfo {
public:
ContextInfo() = default;
ContextInfo(ContextInfo&& other)
: fType(other.fType)
, fTestContext(std::move(other.fTestContext))
, fContext(std::move(other.fContext)) {
}
~ContextInfo() = default;
ContextFactory::ContextType type() const { return fType; }
skgpu::Context* context() const { return fContext.get(); }
sk_sp<skgpu::Context> refContext() const { return fContext; }
GraphiteTestContext* testContext() const { return fTestContext.get(); }
private:
friend class ContextFactory; // for ctor
ContextInfo(ContextFactory::ContextType type,
std::unique_ptr<GraphiteTestContext> testContext,
sk_sp<skgpu::Context> context)
: fType(type)
, fTestContext(std::move(testContext))
, fContext(std::move(context)) {
}
ContextType fType = ContextType::kMock;
std::unique_ptr<GraphiteTestContext> fTestContext;
sk_sp<skgpu::Context> fContext;
};
ContextFactory() = default;
ContextFactory(const ContextFactory&) = delete;
ContextFactory& operator=(const ContextFactory&) = delete;
~ContextFactory() = default;
std::tuple<GraphiteTestContext*, sk_sp<skgpu::Context>> getContextInfo(ContextType);
private:
std::vector<ContextInfo> fContexts;
};
} // namespace sk_graphite_test
#endif // sk_graphite_test_ContextFactory_DEFINED

View File

@ -5,8 +5,8 @@
* found in the LICENSE file.
*/
#ifndef skgpu_GraphiteTestContext_DEFINED
#define skgpu_GraphiteTestContext_DEFINED
#ifndef sk_graphite_test_GraphiteTestContext_DEFINED
#define sk_graphite_test_GraphiteTestContext_DEFINED
#include "experimental/graphite/include/GraphiteTypes.h"
#include "include/core/SkRefCnt.h"
@ -37,4 +37,4 @@ protected:
} // namespace sk_graphite_test
#endif // skgpu_GraphiteTestContext_DEFINED
#endif // sk_graphite_test_GraphiteTestContext_DEFINED

View File

@ -20,7 +20,7 @@ class TestContext : public GraphiteTestContext {
public:
~TestContext() override {}
static GraphiteTestContext* Make();
static std::unique_ptr<GraphiteTestContext> Make();
skgpu::BackendApi backend() override { return skgpu::BackendApi::kMetal; }

View File

@ -16,7 +16,7 @@
namespace sk_graphite_test::mtl {
GraphiteTestContext* TestContext::Make() {
std::unique_ptr<GraphiteTestContext> TestContext::Make() {
sk_cfp<id<MTLDevice>> device;
#ifdef SK_BUILD_FOR_MAC
sk_cfp<NSArray<id <MTLDevice>>*> availableDevices(MTLCopyAllDevices());
@ -44,7 +44,7 @@ GraphiteTestContext* TestContext::Make() {
backendContext.fDevice.retain(device.get());
backendContext.fQueue.retain([*device newCommandQueue]);
return new TestContext(backendContext);
return std::unique_ptr<GraphiteTestContext>(new TestContext(backendContext));
}
sk_sp<skgpu::Context> TestContext::makeContext() {