2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkFlattenable_DEFINED
|
|
|
|
#define SkFlattenable_DEFINED
|
|
|
|
|
|
|
|
#include "SkRefCnt.h"
|
|
|
|
|
2014-01-30 18:58:24 +00:00
|
|
|
class SkReadBuffer;
|
|
|
|
class SkWriteBuffer;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-08-21 14:59:51 +00:00
|
|
|
/*
|
|
|
|
* Flattening is straight-forward:
|
|
|
|
* 1. call getFactory() so we have a function-ptr to recreate the subclass
|
|
|
|
* 2. call flatten(buffer) to write out enough data for the factory to read
|
|
|
|
*
|
|
|
|
* Unflattening is easy for the caller: new_instance = factory(buffer)
|
|
|
|
*
|
|
|
|
* The complexity of supporting this is as follows.
|
|
|
|
*
|
|
|
|
* If your subclass wants to control unflattening, use this macro in your declaration:
|
|
|
|
* SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS
|
|
|
|
* This will provide a getFactory(), and require that the subclass implements CreateProc.
|
|
|
|
*
|
|
|
|
* For older buffers (before the DEEPFLATTENING change, the macros below declare
|
|
|
|
* a thin factory DeepCreateProc. It checks the version of the buffer, and if it is pre-deep,
|
|
|
|
* then it calls through to a (usually protected) constructor, passing the buffer.
|
|
|
|
* If the buffer is newer, then it directly calls the "real" factory: CreateProc.
|
|
|
|
*/
|
2011-12-15 14:16:43 +00:00
|
|
|
|
2012-03-23 19:00:34 +00:00
|
|
|
#define SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() static void InitializeFlattenables();
|
2011-12-15 14:16:43 +00:00
|
|
|
|
|
|
|
#define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(flattenable) \
|
2012-03-23 19:00:34 +00:00
|
|
|
void flattenable::InitializeFlattenables() {
|
|
|
|
|
2011-12-15 14:16:43 +00:00
|
|
|
#define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END \
|
|
|
|
}
|
|
|
|
|
2012-03-26 17:57:35 +00:00
|
|
|
#define SK_DECLARE_UNFLATTENABLE_OBJECT() \
|
2013-10-21 12:26:10 +00:00
|
|
|
virtual Factory getFactory() const SK_OVERRIDE { return NULL; }
|
2012-03-26 17:57:35 +00:00
|
|
|
|
2014-08-21 14:59:51 +00:00
|
|
|
#define SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(flattenable) \
|
|
|
|
SkFlattenable::Registrar(#flattenable, flattenable::CreateProc, \
|
|
|
|
flattenable::GetFlattenableType());
|
|
|
|
|
|
|
|
#define SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(flattenable) \
|
|
|
|
private: \
|
|
|
|
static SkFlattenable* CreateProc(SkReadBuffer&); \
|
|
|
|
friend class SkPrivateEffectInitializer; \
|
|
|
|
public: \
|
|
|
|
virtual Factory getFactory() const SK_OVERRIDE { return CreateProc; }
|
|
|
|
|
|
|
|
// If your subclass will *never* need to be unflattened, declare this.
|
|
|
|
#define SK_DECLARE_NOT_FLATTENABLE_PROCS(flattenable) \
|
|
|
|
virtual Factory getFactory() const SK_OVERRIDE { return ReturnNullCreateProc; }
|
2012-03-26 17:57:35 +00:00
|
|
|
|
2013-10-23 17:06:21 +00:00
|
|
|
/** For SkFlattenable derived objects with a valid type
|
|
|
|
This macro should only be used in base class objects in core
|
|
|
|
*/
|
|
|
|
#define SK_DEFINE_FLATTENABLE_TYPE(flattenable) \
|
|
|
|
static Type GetFlattenableType() { \
|
|
|
|
return k##flattenable##_Type; \
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** \class SkFlattenable
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkFlattenable is the base class for objects that need to be flattened
|
|
|
|
into a data stream for either transport or as part of the key to the
|
|
|
|
font cache.
|
|
|
|
*/
|
2011-03-15 21:27:08 +00:00
|
|
|
class SK_API SkFlattenable : public SkRefCnt {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2013-10-23 17:06:21 +00:00
|
|
|
enum Type {
|
|
|
|
kSkColorFilter_Type,
|
|
|
|
kSkDrawLooper_Type,
|
|
|
|
kSkImageFilter_Type,
|
|
|
|
kSkMaskFilter_Type,
|
|
|
|
kSkPathEffect_Type,
|
|
|
|
kSkPixelRef_Type,
|
|
|
|
kSkRasterizer_Type,
|
|
|
|
kSkShader_Type,
|
2014-05-22 12:27:41 +00:00
|
|
|
kSkUnused_Type, // used to be SkUnitMapper
|
2013-10-23 17:06:21 +00:00
|
|
|
kSkXfermode_Type,
|
|
|
|
};
|
|
|
|
|
2012-06-21 20:25:03 +00:00
|
|
|
SK_DECLARE_INST_COUNT(SkFlattenable)
|
|
|
|
|
2014-01-30 18:58:24 +00:00
|
|
|
typedef SkFlattenable* (*Factory)(SkReadBuffer&);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkFlattenable() {}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Implement this to return a factory function pointer that can be called
|
|
|
|
to recreate your class given a buffer (previously written to by your
|
|
|
|
override of flatten().
|
|
|
|
*/
|
2013-10-21 12:26:10 +00:00
|
|
|
virtual Factory getFactory() const = 0;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2013-10-23 17:06:21 +00:00
|
|
|
/** Returns the name of the object's class
|
|
|
|
*/
|
|
|
|
const char* getTypeName() const { return FactoryToName(getFactory()); }
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
static Factory NameToFactory(const char name[]);
|
|
|
|
static const char* FactoryToName(Factory);
|
2013-10-23 17:06:21 +00:00
|
|
|
static bool NameToType(const char name[], Type* type);
|
|
|
|
|
|
|
|
static void Register(const char name[], Factory, Type);
|
2012-03-23 19:00:34 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
class Registrar {
|
|
|
|
public:
|
2013-10-23 17:06:21 +00:00
|
|
|
Registrar(const char name[], Factory factory, Type type) {
|
|
|
|
SkFlattenable::Register(name, factory, type);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
};
|
2012-03-23 19:00:34 +00:00
|
|
|
|
2014-08-21 14:59:51 +00:00
|
|
|
/**
|
|
|
|
* Override this if your subclass needs to record data that it will need to recreate itself
|
|
|
|
* from its CreateProc (returned by getFactory()).
|
2012-03-29 15:18:04 +00:00
|
|
|
*/
|
2014-08-21 14:59:51 +00:00
|
|
|
virtual void flatten(SkWriteBuffer&) const {}
|
2014-01-30 18:58:24 +00:00
|
|
|
|
|
|
|
protected:
|
2014-08-21 14:59:51 +00:00
|
|
|
static SkFlattenable* ReturnNullCreateProc(SkReadBuffer&) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-12-15 14:16:43 +00:00
|
|
|
|
|
|
|
private:
|
2013-10-23 17:06:21 +00:00
|
|
|
static void InitializeFlattenablesIfNeeded();
|
2011-12-20 20:26:56 +00:00
|
|
|
|
|
|
|
friend class SkGraphics;
|
2012-06-21 20:25:03 +00:00
|
|
|
|
|
|
|
typedef SkRefCnt INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|