SkFlattenable.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkFlattenable_DEFINED
  8. #define SkFlattenable_DEFINED
  9. #include "include/core/SkRefCnt.h"
  10. class SkData;
  11. class SkReadBuffer;
  12. class SkWriteBuffer;
  13. struct SkSerialProcs;
  14. struct SkDeserialProcs;
  15. /** \class SkFlattenable
  16. SkFlattenable is the base class for objects that need to be flattened
  17. into a data stream for either transport or as part of the key to the
  18. font cache.
  19. */
  20. class SK_API SkFlattenable : public SkRefCnt {
  21. public:
  22. enum Type {
  23. kSkColorFilter_Type,
  24. kSkDrawable_Type,
  25. kSkDrawLooper_Type,
  26. kSkImageFilter_Type,
  27. kSkMaskFilter_Type,
  28. kSkPathEffect_Type,
  29. kSkPixelRef_Type,
  30. kSkUnused_Type4, // used to be SkRasterizer
  31. kSkShaderBase_Type,
  32. kSkUnused_Type, // used to be SkUnitMapper
  33. kSkUnused_Type2,
  34. kSkNormalSource_Type,
  35. };
  36. typedef sk_sp<SkFlattenable> (*Factory)(SkReadBuffer&);
  37. SkFlattenable() {}
  38. /** Implement this to return a factory function pointer that can be called
  39. to recreate your class given a buffer (previously written to by your
  40. override of flatten().
  41. */
  42. virtual Factory getFactory() const = 0;
  43. /**
  44. * Returns the name of the object's class.
  45. */
  46. virtual const char* getTypeName() const = 0;
  47. static Factory NameToFactory(const char name[]);
  48. static const char* FactoryToName(Factory);
  49. static void Register(const char name[], Factory);
  50. /**
  51. * Override this if your subclass needs to record data that it will need to recreate itself
  52. * from its CreateProc (returned by getFactory()).
  53. *
  54. * DEPRECATED public : will move to protected ... use serialize() instead
  55. */
  56. virtual void flatten(SkWriteBuffer&) const {}
  57. virtual Type getFlattenableType() const = 0;
  58. //
  59. // public ways to serialize / deserialize
  60. //
  61. sk_sp<SkData> serialize(const SkSerialProcs* = nullptr) const;
  62. size_t serialize(void* memory, size_t memory_size,
  63. const SkSerialProcs* = nullptr) const;
  64. static sk_sp<SkFlattenable> Deserialize(Type, const void* data, size_t length,
  65. const SkDeserialProcs* procs = nullptr);
  66. protected:
  67. class PrivateInitializer {
  68. public:
  69. static void InitEffects();
  70. static void InitImageFilters();
  71. };
  72. private:
  73. static void RegisterFlattenablesIfNeeded();
  74. static void Finalize();
  75. friend class SkGraphics;
  76. typedef SkRefCnt INHERITED;
  77. };
  78. #define SK_REGISTER_FLATTENABLE(type) SkFlattenable::Register(#type, type::CreateProc)
  79. #define SK_FLATTENABLE_HOOKS(type) \
  80. static sk_sp<SkFlattenable> CreateProc(SkReadBuffer&); \
  81. friend class SkFlattenable::PrivateInitializer; \
  82. Factory getFactory() const override { return type::CreateProc; } \
  83. const char* getTypeName() const override { return #type; }
  84. #endif