#pragma once
#include "FloatRect.h"
#include "GraphicsContext.h"
#include <wtf/FastMalloc.h>
#include <wtf/Noncopyable.h>
#include <wtf/text/WTFString.h>
namespace WTF {
class TextStream;
}
namespace WebCore {
namespace DisplayList {
enum class ItemType : uint8_t {
Save,
Restore,
Translate,
Rotate,
Scale,
ConcatenateCTM,
SetCTM,
SetState,
SetLineCap,
SetLineDash,
SetLineJoin,
SetMiterLimit,
ClearShadow,
Clip,
ClipOut,
ClipOutToPath,
ClipPath,
DrawGlyphs,
DrawImage,
DrawTiledImage,
DrawTiledScaledImage,
#if USE(CG) || USE(CAIRO) || USE(DIRECT2D)
DrawNativeImage,
#endif
DrawPattern,
DrawRect,
DrawLine,
DrawLinesForText,
DrawDotsForDocumentMarker,
DrawEllipse,
DrawPath,
DrawFocusRingPath,
DrawFocusRingRects,
FillRect,
FillRectWithColor,
FillRectWithGradient,
FillCompositedRect,
FillRoundedRect,
FillRectWithRoundedHole,
FillPath,
FillEllipse,
StrokeRect,
StrokePath,
StrokeEllipse,
ClearRect,
BeginTransparencyLayer,
EndTransparencyLayer,
#if USE(CG)
ApplyStrokePattern, ApplyFillPattern, #endif
ApplyDeviceScaleFactor,
};
class Item : public RefCounted<Item> {
public:
Item() = delete;
WEBCORE_EXPORT Item(ItemType);
WEBCORE_EXPORT virtual ~Item();
ItemType type() const
{
return m_type;
}
virtual void apply(GraphicsContext&) const = 0;
static constexpr bool isDisplayListItem = true;
virtual bool isDrawingItem() const { return false; }
bool isStateItem() const
{
return isStateItemType(m_type);
}
static bool isStateItemType(ItemType itemType)
{
switch (itemType) {
case ItemType::Translate:
case ItemType::Rotate:
case ItemType::Scale:
case ItemType::ConcatenateCTM:
case ItemType::SetCTM:
case ItemType::SetState:
case ItemType::SetLineCap:
case ItemType::SetLineDash:
case ItemType::SetLineJoin:
case ItemType::SetMiterLimit:
case ItemType::ClearShadow:
return true;
default:
return false;
}
return false;
}
#if !defined(NDEBUG) || !LOG_DISABLED
WTF::CString description() const;
#endif
static size_t sizeInBytes(const Item&);
template<class Encoder> void encode(Encoder&) const;
template<class Decoder> static Optional<Ref<Item>> decode(Decoder&);
private:
ItemType m_type;
};
enum AsTextFlag {
None = 0,
IncludesPlatformOperations = 1 << 0,
};
typedef unsigned AsTextFlags;
class DisplayList {
WTF_MAKE_NONCOPYABLE(DisplayList); WTF_MAKE_FAST_ALLOCATED;
friend class Recorder;
friend class Replayer;
public:
DisplayList() = default;
DisplayList(DisplayList&&) = default;
DisplayList& operator=(DisplayList&&) = default;
void dump(WTF::TextStream&) const;
const Vector<Ref<Item>>& list() const { return m_list; }
Item& itemAt(size_t index)
{
ASSERT(index < m_list.size());
return m_list[index].get();
}
void clear();
void removeItemsFromIndex(size_t);
size_t itemCount() const { return m_list.size(); }
size_t sizeInBytes() const;
String asText(AsTextFlags) const;
#if !defined(NDEBUG) || !LOG_DISABLED
WTF::CString description() const;
WEBCORE_EXPORT void dump() const;
#endif
template<class Encoder> void encode(Encoder&) const;
template<class Decoder> static Optional<DisplayList> decode(Decoder&);
private:
Item& append(Ref<Item>&& item)
{
m_list.append(WTFMove(item));
return m_list.last().get();
}
void appendItem(Item& item)
{
m_list.append(item);
}
static bool shouldDumpForFlags(AsTextFlags, const Item&);
Vector<Ref<Item>>& list() { return m_list; }
Vector<Ref<Item>> m_list;
};
template<class Encoder>
void DisplayList::encode(Encoder& encoder) const
{
encoder << static_cast<uint64_t>(m_list.size());
for (auto& item : m_list)
encoder << item.get();
}
template<class Decoder>
Optional<DisplayList> DisplayList::decode(Decoder& decoder)
{
Optional<uint64_t> itemCount;
decoder >> itemCount;
if (!itemCount)
return WTF::nullopt;
DisplayList displayList;
for (uint64_t i = 0; i < *itemCount; i++) {
auto item = Item::decode(decoder);
if (!item)
continue;
displayList.append(WTFMove(*item));
}
return displayList;
}
}
WTF::TextStream& operator<<(WTF::TextStream&, const DisplayList::DisplayList&);
}
using WebCore::DisplayList::DisplayList;