Make Flattenable not virtual

Fallout from the Flattenable change, update all its uses.

Additionnaly, fix/tighten size checks when (un)flatten()ing
things.

Removed the assumption by some flattenables (e.g.: Fence)
that the size passed to them would be exact (it can
and will be larger in some cases)

The code in Parcel is a bit complicated so that we don't
have to expose the full implementation (and also to
keep the code smallish).

Change-Id: I0bf1c8aca2a3128491b4f45510bc46667e566dde
diff --git a/include/ui/Fence.h b/include/ui/Fence.h
index 60156e7..20466b6 100644
--- a/include/ui/Fence.h
+++ b/include/ui/Fence.h
@@ -36,7 +36,7 @@
 // ===========================================================================
 
 class Fence
-    : public LightRefBase<Fence>, public Flattenable
+    : public LightRefBase<Fence>, public Flattenable<Fence>
 {
 public:
     static const sp<Fence> NO_FENCE;
@@ -96,15 +96,13 @@
     // Flattenable interface
     size_t getFlattenedSize() const;
     size_t getFdCount() const;
-    status_t flatten(void* buffer, size_t size,
-            int fds[], size_t count) const;
-    status_t unflatten(void const* buffer, size_t size,
-            int fds[], size_t count);
+    status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
+    status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
 
 private:
     // Only allow instantiation using ref counting.
     friend class LightRefBase<Fence>;
-    virtual ~Fence();
+    ~Fence();
 
     // Disallow copying
     Fence(const Fence& rhs);
diff --git a/include/ui/GraphicBuffer.h b/include/ui/GraphicBuffer.h
index e5ad1e0..8184b7d 100644
--- a/include/ui/GraphicBuffer.h
+++ b/include/ui/GraphicBuffer.h
@@ -39,8 +39,9 @@
 
 class GraphicBuffer
     : public ANativeObjectBase< ANativeWindowBuffer, GraphicBuffer, RefBase >,
-      public Flattenable
+      public Flattenable<GraphicBuffer>
 {
+    friend class Flattenable<GraphicBuffer>;
 public:
 
     enum {
@@ -106,7 +107,7 @@
     static void dumpAllocationsToSystemLog();
 
 private:
-    virtual ~GraphicBuffer();
+    ~GraphicBuffer();
 
     enum {
         ownNone   = 0,
@@ -136,13 +137,11 @@
 
     void free_handle();
 
-    // Flattenable interface
+    // Flattenable protocol
     size_t getFlattenedSize() const;
     size_t getFdCount() const;
-    status_t flatten(void* buffer, size_t size,
-            int fds[], size_t count) const;
-    status_t unflatten(void const* buffer, size_t size,
-            int fds[], size_t count);
+    status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
+    status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
 
 
     GraphicBufferMapper& mBufferMapper;
diff --git a/include/ui/Region.h b/include/ui/Region.h
index ce91f3b..d906dbb 100644
--- a/include/ui/Region.h
+++ b/include/ui/Region.h
@@ -136,8 +136,8 @@
             void        addRectUnchecked(int l, int t, int r, int b);
 
     inline  bool        isFixedSize() const { return false; }
-            size_t      getSize() const;
-            status_t    flatten(void* buffer) const;
+            size_t      getFlattenedSize() const;
+            status_t    flatten(void* buffer, size_t size) const;
             status_t    unflatten(void const* buffer, size_t size);
 
     void        dump(String8& out, const char* what, uint32_t flags=0) const;