OLD | NEW |
(Empty) | |
| 1 Flattenables |
| 2 ============ |
| 3 |
| 4 Many objects in Skia, such as SkShaders and other effects on SkPaint, need to be
flattened into a data stream for either transport |
| 5 or as part of the key to the font cache. Classes for these objects should derive
from SkFlattenable or one of its subclasses. If you |
| 6 create a new flattenable class, you need to make sure you do a few things so tha
t it will work on all platforms: |
| 7 |
| 8 1: Override the method flatten (the default scope is protected): |
| 9 |
| 10 ~~~~ |
| 11 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE { |
| 12 this->INHERITED::flatten(buffer); |
| 13 // Write any private data that needs to be stored to recreate this object |
| 14 } |
| 15 ~~~~ |
| 16 |
| 17 2: Override the (protected) constructor that creates an object from an SkFlatten
ableReadBuffer: |
| 18 |
| 19 ~~~~ |
| 20 SkNewClass(SkFlattenableReadBuffer& buffer) |
| 21 : INHERITED(buffer) { |
| 22 // Read the data from the buffer in the same order as it was written to the |
| 23 // SkFlattenableWriteBuffer and construct the new object |
| 24 } |
| 25 ~~~~ |
| 26 |
| 27 3: Declare a set of deserialization procs for your object in the class declarati
on: |
| 28 We have a macro for this: |
| 29 |
| 30 ~~~~ |
| 31 public: |
| 32 |
| 33 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkNewClass) |
| 34 ~~~~ |
| 35 |
| 36 4: If your class is declared in a .cpp file or in a private header file, create
a function |
| 37 to register its group: |
| 38 This occurs in cases where the classes are hidden behind a factory, like many ef
fects and shaders are. Then in the parent |
| 39 class header file (such as SkGradientShader) you need to add: |
| 40 |
| 41 ~~~~ |
| 42 public: |
| 43 |
| 44 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() |
| 45 |
| 46 |
| 47 Then in the cpp file you define all the members of the group together: |
| 48 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGroupClass) |
| 49 |
| 50 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkMemberClass1) |
| 51 |
| 52 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkMemberClass2) |
| 53 |
| 54 // etc |
| 55 |
| 56 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 57 ~~~~ |
| 58 |
| 59 |
| 60 5: Register your flattenable with the global registrar: |
| 61 You need to add one line to SkFlattenable::InitalizeFlattenables(). To register
the flattenable in a Skia build, that function is |
| 62 defined in SkGlobalInitialization_default.cpp. For Chromium, it is in SkGlobalIn
itialization_chromium.cpp. |
| 63 For a single flattenable add |
| 64 |
| 65 ~~~~ |
| 66 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkNewClass) |
| 67 ~~~~ |
| 68 |
| 69 For a group, add |
| 70 |
| 71 ~~~~ |
| 72 SkGroupClass::InitializeFlattenables(); |
| 73 ~~~~ |
| 74 |
OLD | NEW |