#pragma once
#if USE(IOSURFACE)
#include <objc/objc.h>
#include "GraphicsContext.h"
#include "IntSize.h"
namespace WTF {
class TextStream;
}
namespace WebCore {
class MachSendRight;
class IOSurface final {
WTF_MAKE_FAST_ALLOCATED;
public:
enum class Format {
RGBA,
YUV422,
RGB10,
RGB10A8,
};
class Locker {
public:
enum class AccessMode {
ReadOnly,
ReadWrite
};
Locker(IOSurface& surface, AccessMode mode = AccessMode::ReadOnly)
: m_surface(surface)
, m_flags(flagsFromMode(mode))
{
IOSurfaceLock(m_surface.surface(), m_flags, nullptr);
}
~Locker()
{
IOSurfaceUnlock(m_surface.surface(), m_flags, nullptr);
}
void * surfaceBaseAddress() const
{
return IOSurfaceGetBaseAddress(m_surface.surface());
}
private:
static uint32_t flagsFromMode(AccessMode mode)
{
return mode == AccessMode::ReadOnly ? kIOSurfaceLockReadOnly : 0;
}
IOSurface& m_surface;
uint32_t m_flags;
};
WEBCORE_EXPORT static std::unique_ptr<IOSurface> create(IntSize, CGColorSpaceRef, Format = Format::RGBA);
WEBCORE_EXPORT static std::unique_ptr<IOSurface> create(IntSize, IntSize contextSize, CGColorSpaceRef, Format = Format::RGBA);
WEBCORE_EXPORT static std::unique_ptr<IOSurface> createFromSendRight(const MachSendRight&&, CGColorSpaceRef);
static std::unique_ptr<IOSurface> createFromSurface(IOSurfaceRef, CGColorSpaceRef);
WEBCORE_EXPORT static std::unique_ptr<IOSurface> createFromImage(CGImageRef);
static std::unique_ptr<IOSurface> createFromImageBuffer(std::unique_ptr<ImageBuffer>);
WEBCORE_EXPORT static void moveToPool(std::unique_ptr<IOSurface>&&);
static IntSize maximumSize();
WEBCORE_EXPORT MachSendRight createSendRight() const;
WEBCORE_EXPORT RetainPtr<CGImageRef> createImage();
WEBCORE_EXPORT static RetainPtr<CGImageRef> sinkIntoImage(std::unique_ptr<IOSurface>);
id asLayerContents() const { return reinterpret_cast<id>(m_surface.get()); }
IOSurfaceRef surface() const { return m_surface.get(); }
WEBCORE_EXPORT GraphicsContext& ensureGraphicsContext();
WEBCORE_EXPORT CGContextRef ensurePlatformContext();
enum class SurfaceState {
Valid,
Empty
};
SurfaceState state() const;
bool isVolatile() const;
WEBCORE_EXPORT SurfaceState setIsVolatile(bool);
IntSize size() const { return m_size; }
size_t totalBytes() const { return m_totalBytes; }
CGColorSpaceRef colorSpace() const { return m_colorSpace.get(); }
WEBCORE_EXPORT Format format() const;
IOSurfaceID surfaceID() const;
size_t bytesPerRow() const;
WEBCORE_EXPORT bool isInUse() const;
WEBCORE_EXPORT void releaseGraphicsContext();
#if PLATFORM(IOS)
WEBCORE_EXPORT static bool allowConversionFromFormatToFormat(Format, Format);
WEBCORE_EXPORT static void convertToFormat(std::unique_ptr<WebCore::IOSurface>&& inSurface, Format, WTF::Function<void(std::unique_ptr<WebCore::IOSurface>)>&&);
#endif
private:
IOSurface(IntSize, IntSize contextSize, CGColorSpaceRef, Format, bool& success);
IOSurface(IOSurfaceRef, CGColorSpaceRef);
static std::unique_ptr<IOSurface> surfaceFromPool(IntSize, IntSize contextSize, CGColorSpaceRef, Format);
IntSize contextSize() const { return m_contextSize; }
void setContextSize(IntSize);
RetainPtr<CGColorSpaceRef> m_colorSpace;
IntSize m_size;
IntSize m_contextSize;
size_t m_totalBytes;
std::unique_ptr<GraphicsContext> m_graphicsContext;
RetainPtr<CGContextRef> m_cgContext;
RetainPtr<IOSurfaceRef> m_surface;
};
WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const WebCore::IOSurface&);
}
#endif // USE(IOSURFACE)