blob: ee7a4337100abbbe2724dc564754543f64b6c5d4 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkFlattenable_DEFINED
11#define SkFlattenable_DEFINED
12
13#include "SkRefCnt.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000015class SkReadBuffer;
16class SkWriteBuffer;
reed@android.com8a1c16f2008-12-17 15:59:43 +000017
caryclark@google.comd26147a2011-12-15 14:16:43 +000018#define SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(flattenable) \
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000019 SkFlattenable::Registrar(#flattenable, flattenable::CreateProc, \
20 flattenable::GetFlattenableType());
djsollen@google.coma2ca41e2012-03-23 19:00:34 +000021
22#define SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() static void InitializeFlattenables();
23
24#define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(flattenable) \
25 void flattenable::InitializeFlattenables() {
26
caryclark@google.comd26147a2011-12-15 14:16:43 +000027#define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END \
28 }
29
djsollen@google.comba28d032012-03-26 17:57:35 +000030#define SK_DECLARE_UNFLATTENABLE_OBJECT() \
robertphillips@google.comc2eae472013-10-21 12:26:10 +000031 virtual Factory getFactory() const SK_OVERRIDE { return NULL; }
djsollen@google.comba28d032012-03-26 17:57:35 +000032
33#define SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(flattenable) \
robertphillips@google.comc2eae472013-10-21 12:26:10 +000034 virtual Factory getFactory() const SK_OVERRIDE { return CreateProc; } \
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000035 static SkFlattenable* CreateProc(SkReadBuffer& buffer) { \
djsollen@google.comba28d032012-03-26 17:57:35 +000036 return SkNEW_ARGS(flattenable, (buffer)); \
37 }
38
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000039/** For SkFlattenable derived objects with a valid type
40 This macro should only be used in base class objects in core
41 */
42#define SK_DEFINE_FLATTENABLE_TYPE(flattenable) \
43 static Type GetFlattenableType() { \
44 return k##flattenable##_Type; \
45 }
46
reed@android.com8a1c16f2008-12-17 15:59:43 +000047/** \class SkFlattenable
rmistry@google.comfbfcd562012-08-23 18:09:54 +000048
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 SkFlattenable is the base class for objects that need to be flattened
50 into a data stream for either transport or as part of the key to the
51 font cache.
52 */
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000053class SK_API SkFlattenable : public SkRefCnt {
reed@android.com8a1c16f2008-12-17 15:59:43 +000054public:
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000055 enum Type {
56 kSkColorFilter_Type,
57 kSkDrawLooper_Type,
58 kSkImageFilter_Type,
59 kSkMaskFilter_Type,
60 kSkPathEffect_Type,
61 kSkPixelRef_Type,
62 kSkRasterizer_Type,
63 kSkShader_Type,
commit-bot@chromium.org3339ac52014-05-22 02:55:59 +000064 kSkUnitMapper_Type,
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000065 kSkXfermode_Type,
66 };
67
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000068 SK_DECLARE_INST_COUNT(SkFlattenable)
69
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000070 typedef SkFlattenable* (*Factory)(SkReadBuffer&);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000071
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 SkFlattenable() {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +000073
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 /** Implement this to return a factory function pointer that can be called
75 to recreate your class given a buffer (previously written to by your
76 override of flatten().
77 */
robertphillips@google.comc2eae472013-10-21 12:26:10 +000078 virtual Factory getFactory() const = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000079
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000080 /** Returns the name of the object's class
81 */
82 const char* getTypeName() const { return FactoryToName(getFactory()); }
83
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 static Factory NameToFactory(const char name[]);
85 static const char* FactoryToName(Factory);
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000086 static bool NameToType(const char name[], Type* type);
87
88 static void Register(const char name[], Factory, Type);
djsollen@google.coma2ca41e2012-03-23 19:00:34 +000089
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 class Registrar {
91 public:
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +000092 Registrar(const char name[], Factory factory, Type type) {
93 SkFlattenable::Register(name, factory, type);
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 }
95 };
djsollen@google.coma2ca41e2012-03-23 19:00:34 +000096
djsollen@google.com54924242012-03-29 15:18:04 +000097 /** Override this to write data specific to your subclass into the buffer,
98 being sure to call your super-class' version first. This data will later
99 be passed to your Factory function, returned by getFactory().
100 */
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000101 virtual void flatten(SkWriteBuffer&) const;
102
103protected:
104 SkFlattenable(SkReadBuffer&) {}
caryclark@google.comd26147a2011-12-15 14:16:43 +0000105
106private:
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000107 static void InitializeFlattenablesIfNeeded();
caryclark@google.com9d0c6ec2011-12-20 20:26:56 +0000108
109 friend class SkGraphics;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000110
111 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112};
113
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114#endif