scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 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 | |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 11 | #include "SkData.h" |
halcanary | 6b28017 | 2015-12-07 12:42:24 -0800 | [diff] [blame] | 12 | #include "SkPixmap.h" |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 13 | #include "SkRefCnt.h" |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * Interface for serializing pixels, e.g. SkBitmaps in an SkPicture. |
| 17 | */ |
| 18 | class SkPixelSerializer : public SkRefCnt { |
| 19 | public: |
reed | c9e190d | 2015-09-28 09:58:41 -0700 | [diff] [blame] | 20 | virtual ~SkPixelSerializer() {} |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 21 | |
| 22 | /** |
reed | c9e190d | 2015-09-28 09:58:41 -0700 | [diff] [blame] | 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). |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 25 | */ |
reed | c9e190d | 2015-09-28 09:58:41 -0700 | [diff] [blame] | 26 | bool useEncodedData(const void* data, size_t len) { |
| 27 | return this->onUseEncodedData(data, len); |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 28 | } |
| 29 | |
reed | c9e190d | 2015-09-28 09:58:41 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Call to get the client's version of encoding these pixels. If it |
| 32 | * returns NULL, serialize the raw pixels. |
| 33 | */ |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 34 | sk_sp<SkData> encodeToData(const SkPixmap& pixmap) { |
| 35 | return sk_sp<SkData>(this->onEncode(pixmap)); |
| 36 | } |
| 37 | |
reed | c9e190d | 2015-09-28 09:58:41 -0700 | [diff] [blame] | 38 | protected: |
| 39 | /** |
| 40 | * Return true if you want to serialize the encoded data, false if you want |
halcanary | 9907371 | 2015-12-10 09:30:57 -0800 | [diff] [blame] | 41 | * another version serialized (e.g. the result of this->encode()). |
reed | c9e190d | 2015-09-28 09:58:41 -0700 | [diff] [blame] | 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 | */ |
halcanary | 9907371 | 2015-12-10 09:30:57 -0800 | [diff] [blame] | 49 | virtual SkData* onEncode(const SkPixmap&) = 0; |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 50 | }; |
| 51 | #endif // SkPixelSerializer_DEFINED |