aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/core/SkPixelSerializer.h
blob: 05054557f6180c4f40de5ca648f8a76c5bf19c70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkPixelSerializer_DEFINED
#define SkPixelSerializer_DEFINED

#include "SkRefCnt.h"
#include "SkPixmap.h"

class SkData;

/**
 *  Interface for serializing pixels, e.g. SkBitmaps in an SkPicture.
 */
class SkPixelSerializer : public SkRefCnt {
public:
    virtual ~SkPixelSerializer() {}

    /**
     *  Call to determine if the client wants to serialize the encoded data. If
     *  false, serialize another version (e.g. the result of encodePixels).
     */
    bool useEncodedData(const void* data, size_t len) {
        return this->onUseEncodedData(data, len);
    }

    /**
     *  Call to get the client's version of encoding these pixels. If it
     *  returns NULL, serialize the raw pixels.
     */
    sk_sp<SkData> encodeToData(const SkPixmap& pixmap) {
        return sk_sp<SkData>(this->onEncode(pixmap));
    }

#ifdef SK_SUPPORT_LEGACY_IMAGE_ENCODE_API
    SkData* encode(const SkPixmap& pixmap) {
        return this->encodeToData(pixmap).release();
    }
#endif

protected:
    /**
     *  Return true if you want to serialize the encoded data, false if you want
     *  another version serialized (e.g. the result of this->encode()).
     */
    virtual bool onUseEncodedData(const void* data, size_t len) = 0;

    /**
     *  If you want to encode these pixels, return the encoded data as an SkData
     *  Return null if you want to serialize the raw pixels.
     */
    virtual SkData* onEncode(const SkPixmap&) = 0;
};
#endif // SkPixelSerializer_DEFINED