Move DeviceFlags and CheckAlphaTypeAndGetFlags to skgpu::BaseDevice
skgpu::v2::Device will also need both of these so move them somewhere accessible
Bug: skia:11837
Change-Id: Iacaf0332da933515451056eeda62949b049378b7
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/446180
Reviewed-by: Michael Ludwig <michaelludwig@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/core/SkDeferredDisplayListRecorder.cpp b/src/core/SkDeferredDisplayListRecorder.cpp
index dfcfdab..5dda5ef 100644
--- a/src/core/SkDeferredDisplayListRecorder.cpp
+++ b/src/core/SkDeferredDisplayListRecorder.cpp
@@ -187,7 +187,7 @@
fCharacterization.refColorSpace(),
fCharacterization.origin(),
fCharacterization.surfaceProps(),
- skgpu::BaseDevice::kUninit_InitContents);
+ skgpu::BaseDevice::InitContents::kUninit);
if (!device) {
return false;
}
diff --git a/src/core/SkSpecialSurface.cpp b/src/core/SkSpecialSurface.cpp
index 2234b3c..d0b9bb2 100644
--- a/src/core/SkSpecialSurface.cpp
+++ b/src/core/SkSpecialSurface.cpp
@@ -165,7 +165,7 @@
auto device = rContext->priv().createDevice(SkBudgeted::kYes, ii, SkBackingFit::kApprox, 1,
GrMipmapped::kNo, GrProtected::kNo,
kBottomLeft_GrSurfaceOrigin, props,
- skgpu::BaseDevice::kUninit_InitContents);
+ skgpu::BaseDevice::InitContents::kUninit);
if (!device) {
return nullptr;
}
diff --git a/src/gpu/BaseDevice.cpp b/src/gpu/BaseDevice.cpp
index efeea35..8b01c2c 100644
--- a/src/gpu/BaseDevice.cpp
+++ b/src/gpu/BaseDevice.cpp
@@ -23,6 +23,27 @@
, fContext(std::move(rContext)) {
}
+/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
+ should fail. */
+bool BaseDevice::CheckAlphaTypeAndGetFlags(SkAlphaType alphaType,
+ InitContents init,
+ DeviceFlags* flags) {
+ *flags = DeviceFlags::kNone;
+ switch (alphaType) {
+ case kPremul_SkAlphaType:
+ break;
+ case kOpaque_SkAlphaType:
+ *flags |= DeviceFlags::kIsOpaque;
+ break;
+ default: // If it is unpremul or unknown don't try to render
+ return false;
+ }
+ if (InitContents::kClear == init) {
+ *flags |= DeviceFlags::kNeedClear;
+ }
+ return true;
+}
+
GrRenderTargetProxy* BaseDevice::targetProxy() {
return this->readSurfaceView().asRenderTargetProxy();
}
diff --git a/src/gpu/BaseDevice.h b/src/gpu/BaseDevice.h
index fe88870..0a7cbf4 100644
--- a/src/gpu/BaseDevice.h
+++ b/src/gpu/BaseDevice.h
@@ -24,9 +24,9 @@
class BaseDevice : public SkBaseDevice {
public:
- enum InitContents {
- kClear_InitContents,
- kUninit_InitContents
+ enum class InitContents {
+ kClear,
+ kUninit
};
BaseDevice(sk_sp<GrRecordingContext>, const SkImageInfo&, const SkSurfaceProps&);
@@ -78,12 +78,24 @@
ReadPixelsContext context) = 0;
protected:
+ enum class DeviceFlags {
+ kNone = 0,
+ kNeedClear = 1 << 0, //!< Surface requires an initial clear
+ kIsOpaque = 1 << 1, //!< Hint from client that rendering to this device will be
+ // opaque even if the config supports alpha.
+ };
+ GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(DeviceFlags);
+
+ static bool CheckAlphaTypeAndGetFlags(SkAlphaType, InitContents, DeviceFlags*);
+
sk_sp<GrRecordingContext> fContext;
private:
using INHERITED = SkBaseDevice;
};
+GR_MAKE_BITFIELD_CLASS_OPS(BaseDevice::DeviceFlags)
+
} // namespace skgpu
#endif // BaseDevice_DEFINED
diff --git a/src/gpu/GrRecordingContextPriv.cpp b/src/gpu/GrRecordingContextPriv.cpp
index ed867fc..8099076 100644
--- a/src/gpu/GrRecordingContextPriv.cpp
+++ b/src/gpu/GrRecordingContextPriv.cpp
@@ -108,8 +108,8 @@
#if SK_GPU_V1
// It is probably not necessary to check if the context is abandoned here since uses of the
// SurfaceContext which need the context will mostly likely fail later on w/o an issue.
- // However having this hear adds some reassurance in case there is a path doesn't handle an
- // abandoned context correctly. It also lets us early out of some extra work.
+ // However having this here adds some reassurance in case there is a path that doesn't
+ // handle an abandoned context correctly. It also lets us early out of some extra work.
if (this->context()->abandoned()) {
return nullptr;
}
diff --git a/src/gpu/v1/Device.cpp b/src/gpu/v1/Device.cpp
index 2cef9fb..f4c924d 100644
--- a/src/gpu/v1/Device.cpp
+++ b/src/gpu/v1/Device.cpp
@@ -123,29 +123,6 @@
namespace skgpu::v1 {
-/** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
- should fail. */
-bool Device::CheckAlphaTypeAndGetFlags(const SkImageInfo* info,
- Device::InitContents init,
- unsigned* flags) {
- *flags = 0;
- if (info) {
- switch (info->alphaType()) {
- case kPremul_SkAlphaType:
- break;
- case kOpaque_SkAlphaType:
- *flags |= Device::kIsOpaque_Flag;
- break;
- default: // If it is unpremul or unknown don't try to render
- return false;
- }
- }
- if (kClear_InitContents == init) {
- *flags |= kNeedClear_Flag;
- }
- return true;
-}
-
sk_sp<BaseDevice> Device::Make(GrRecordingContext* rContext,
GrColorType colorType,
sk_sp<GrSurfaceProxy> proxy,
@@ -160,11 +137,11 @@
origin,
surfaceProps);
- return Device::Make(std::move(sdc), nullptr, init);
+ return Device::Make(std::move(sdc), kPremul_SkAlphaType, init);
}
sk_sp<BaseDevice> Device::Make(std::unique_ptr<SurfaceDrawContext> sdc,
- const SkImageInfo* ii,
+ SkAlphaType alphaType,
InitContents init) {
if (!sdc) {
return nullptr;
@@ -177,9 +154,9 @@
SkColorType ct = GrColorTypeToSkColorType(sdc->colorInfo().colorType());
- unsigned flags;
+ DeviceFlags flags;
if (!rContext->colorTypeSupportedAsSurface(ct) ||
- !CheckAlphaTypeAndGetFlags(ii, init, &flags)) {
+ !CheckAlphaTypeAndGetFlags(alphaType, init, &flags)) {
return nullptr;
}
return sk_sp<Device>(new Device(std::move(sdc), flags));
@@ -205,18 +182,18 @@
origin,
props);
- return Device::Make(std::move(sdc), &ii, init);
+ return Device::Make(std::move(sdc), ii.alphaType(), init);
}
-Device::Device(std::unique_ptr<SurfaceDrawContext> sdc, unsigned flags)
+Device::Device(std::unique_ptr<SurfaceDrawContext> sdc, DeviceFlags flags)
: INHERITED(sk_ref_sp(sdc->recordingContext()),
- make_info(sdc.get(), SkToBool(flags & kIsOpaque_Flag)),
+ make_info(sdc.get(), SkToBool(flags & DeviceFlags::kIsOpaque)),
sdc->surfaceProps())
, fSurfaceDrawContext(std::move(sdc))
, fClip(SkIRect::MakeSize(fSurfaceDrawContext->dimensions()),
&this->asMatrixProvider(),
force_aa_clip(fSurfaceDrawContext.get())) {
- if (flags & kNeedClear_Flag) {
+ if (flags & DeviceFlags::kNeedClear) {
this->clearAll();
}
}
@@ -1081,9 +1058,9 @@
}
// Skia's convention is to only clear a device if it is non-opaque.
- InitContents init = cinfo.fInfo.isOpaque() ? kUninit_InitContents : kClear_InitContents;
+ InitContents init = cinfo.fInfo.isOpaque() ? InitContents::kUninit : InitContents::kClear;
- return Device::Make(std::move(sdc), &cinfo.fInfo, init).release();
+ return Device::Make(std::move(sdc), cinfo.fInfo.alphaType(), init).release();
}
sk_sp<SkSurface> Device::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
diff --git a/src/gpu/v1/Device_v1.h b/src/gpu/v1/Device_v1.h
index e8faea9..0a15c4d 100644
--- a/src/gpu/v1/Device_v1.h
+++ b/src/gpu/v1/Device_v1.h
@@ -187,19 +187,11 @@
ClipStack fClip;
- enum Flags {
- kNeedClear_Flag = 1 << 0, //!< Surface requires an initial clear
- kIsOpaque_Flag = 1 << 1, //!< Hint from client that rendering to this device will be
- // opaque even if the config supports alpha.
- };
- static bool CheckAlphaTypeAndGetFlags(const SkImageInfo* info, InitContents init,
- unsigned* flags);
-
static sk_sp<BaseDevice> Make(std::unique_ptr<SurfaceDrawContext>,
- const SkImageInfo*,
+ SkAlphaType,
InitContents);
- Device(std::unique_ptr<SurfaceDrawContext>, unsigned flags);
+ Device(std::unique_ptr<SurfaceDrawContext>, DeviceFlags);
SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
diff --git a/src/gpu/vk/GrVkSecondaryCBDrawContext.cpp b/src/gpu/vk/GrVkSecondaryCBDrawContext.cpp
index 4293b58..77499d2 100644
--- a/src/gpu/vk/GrVkSecondaryCBDrawContext.cpp
+++ b/src/gpu/vk/GrVkSecondaryCBDrawContext.cpp
@@ -47,7 +47,7 @@
imageInfo.refColorSpace(),
kTopLeft_GrSurfaceOrigin,
SkSurfacePropsCopyOrDefault(props),
- skgpu::BaseDevice::kUninit_InitContents);
+ skgpu::BaseDevice::InitContents::kUninit);
if (!device) {
return nullptr;
}
diff --git a/src/image/SkSurface_Gpu.cpp b/src/image/SkSurface_Gpu.cpp
index 6a7662e..70c4f9f 100644
--- a/src/image/SkSurface_Gpu.cpp
+++ b/src/image/SkSurface_Gpu.cpp
@@ -407,7 +407,7 @@
auto device = rContext->priv().createDevice(budgeted, c.imageInfo(), SkBackingFit::kExact,
c.sampleCount(), GrMipmapped(c.isMipMapped()),
c.isProtected(), c.origin(), c.surfaceProps(),
- skgpu::BaseDevice::kClear_InitContents);
+ skgpu::BaseDevice::InitContents::kClear);
if (!device) {
return nullptr;
}
@@ -466,7 +466,7 @@
auto device = rContext->priv().createDevice(budgeted, info, SkBackingFit::kExact,
sampleCount, mipMapped, GrProtected::kNo, origin,
SkSurfacePropsCopyOrDefault(props),
- skgpu::BaseDevice::kClear_InitContents);
+ skgpu::BaseDevice::InitContents::kClear);
if (!device) {
return nullptr;
}
@@ -508,7 +508,7 @@
auto device = rContext->priv().createDevice(grColorType, std::move(proxy),
std::move(colorSpace), origin,
SkSurfacePropsCopyOrDefault(props),
- skgpu::BaseDevice::kUninit_InitContents);
+ skgpu::BaseDevice::InitContents::kUninit);
if (!device) {
return nullptr;
}
@@ -624,7 +624,7 @@
auto device = rContext->priv().createDevice(grColorType, std::move(proxy),
std::move(colorSpace), origin,
SkSurfacePropsCopyOrDefault(props),
- skgpu::BaseDevice::kUninit_InitContents);
+ skgpu::BaseDevice::InitContents::kUninit);
if (!device) {
return nullptr;
}
diff --git a/src/image/SkSurface_GpuMtl.mm b/src/image/SkSurface_GpuMtl.mm
index a56b041..7136442 100644
--- a/src/image/SkSurface_GpuMtl.mm
+++ b/src/image/SkSurface_GpuMtl.mm
@@ -88,7 +88,7 @@
std::move(colorSpace),
origin,
SkSurfacePropsCopyOrDefault(surfaceProps),
- skgpu::BaseDevice::kUninit_InitContents);
+ skgpu::BaseDevice::InitContents::kUninit);
if (!device) {
return nullptr;
}
@@ -157,7 +157,7 @@
std::move(colorSpace),
origin,
SkSurfacePropsCopyOrDefault(surfaceProps),
- skgpu::BaseDevice::kUninit_InitContents);
+ skgpu::BaseDevice::InitContents::kUninit);
if (!device) {
return nullptr;
}
diff --git a/tests/DeviceTest.cpp b/tests/DeviceTest.cpp
index 8311303..6eff303 100644
--- a/tests/DeviceTest.cpp
+++ b/tests/DeviceTest.cpp
@@ -87,7 +87,7 @@
auto device = dContext->priv().createDevice(SkBudgeted::kNo, ii, SkBackingFit::kExact,
1, GrMipmapped::kNo, GrProtected::kNo,
kBottomLeft_GrSurfaceOrigin, SkSurfaceProps(),
- skgpu::BaseDevice::kClear_InitContents);
+ skgpu::BaseDevice::InitContents::kClear);
SkBitmap bm;
SkAssertResult(bm.tryAllocN32Pixels(kWidth, kHeight));