Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2014 Google Inc.
      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 
      8 #ifndef SkPixelSerializer_DEFINED
      9 #define SkPixelSerializer_DEFINED
     10 
     11 #include "SkData.h"
     12 #include "SkPixmap.h"
     13 #include "SkRefCnt.h"
     14 
     15 /**
     16  *  Interface for serializing pixels, e.g. SkBitmaps in an SkPicture.
     17  */
     18 class SkPixelSerializer : public SkRefCnt {
     19 public:
     20     virtual ~SkPixelSerializer() {}
     21 
     22     /**
     23      *  Call to determine if the client wants to serialize the encoded data. If
     24      *  false, serialize another version (e.g. the result of encodePixels).
     25      */
     26     bool useEncodedData(const void* data, size_t len) {
     27         return this->onUseEncodedData(data, len);
     28     }
     29 
     30     /**
     31      *  Call to get the client's version of encoding these pixels. If it
     32      *  returns NULL, serialize the raw pixels.
     33      */
     34     sk_sp<SkData> encodeToData(const SkPixmap& pixmap) {
     35         return sk_sp<SkData>(this->onEncode(pixmap));
     36     }
     37 
     38 protected:
     39     /**
     40      *  Return true if you want to serialize the encoded data, false if you want
     41      *  another version serialized (e.g. the result of this->encode()).
     42      */
     43     virtual bool onUseEncodedData(const void* data, size_t len) = 0;
     44 
     45     /**
     46      *  If you want to encode these pixels, return the encoded data as an SkData
     47      *  Return null if you want to serialize the raw pixels.
     48      */
     49     virtual SkData* onEncode(const SkPixmap&) = 0;
     50 };
     51 #endif // SkPixelSerializer_DEFINED
     52