OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "blimp/common/compositor/blimp_image_serialization_processor.h" | 5 #include "blimp/common/compositor/blimp_image_serialization_processor.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "blimp/common/compositor/webp_decoder.h" | |
12 #include "third_party/libwebp/webp/encode.h" | |
13 #include "third_party/skia/include/core/SkData.h" | |
11 #include "third_party/skia/include/core/SkPicture.h" | 14 #include "third_party/skia/include/core/SkPicture.h" |
12 #include "third_party/skia/include/core/SkPixelSerializer.h" | 15 #include "third_party/skia/include/core/SkPixelSerializer.h" |
13 | 16 |
14 namespace { | 17 namespace { |
15 bool NoopDecoder(const void* input, size_t input_size, SkBitmap* bitmap) { | 18 // TODO(nyquist): Make sure encoder does not serialize images more than once. |
16 // TODO(nyquist): Add an image decoder. | 19 // See crbug.com/548434. |
17 return false; | 20 class WebPImageEncoder : public SkPixelSerializer { |
18 } | 21 public: |
22 WebPImageEncoder() {} | |
23 ~WebPImageEncoder() override{}; | |
24 | |
25 bool onUseEncodedData(const void* data, size_t len) override { | |
26 const unsigned char* cast_data = static_cast<const unsigned char*>(data); | |
27 if (len < 14) | |
28 return false; | |
29 return !memcmp(cast_data, "RIFF", 4) && !memcmp(cast_data + 8, "WEBPVP", 6); | |
30 } | |
31 | |
32 SkData* onEncode(const SkPixmap& pixmap) override { | |
33 auto pixel_chars = static_cast<const unsigned char*>(pixmap.addr()); | |
34 WebPConfig config; | |
35 if (!WebPConfigInit(&config)) | |
36 return nullptr; | |
37 WebPPicture picture; | |
38 if (!WebPPictureInit(&picture)) | |
39 return nullptr; | |
40 | |
41 if (!pixmap.width() || pixmap.width() > WEBP_MAX_DIMENSION) | |
42 return nullptr; | |
43 picture.width = pixmap.width(); | |
44 if (!pixmap.height() || pixmap.height() > WEBP_MAX_DIMENSION) | |
45 return nullptr; | |
46 picture.height = pixmap.height(); | |
47 | |
48 if (!PlatformImportPicture(pixel_chars, &picture)) | |
49 return nullptr; | |
50 | |
51 const int quality = 50; | |
52 std::vector<unsigned char> data; | |
53 picture.custom_ptr = &data; | |
54 picture.writer = &WebPImageEncoder::WriteOutput; | |
55 config.quality = quality; | |
56 config.method = 3; | |
57 | |
58 bool success = WebPEncode(&config, &picture); | |
59 WebPPictureFree(&picture); | |
60 if (!success) | |
61 return nullptr; | |
62 return SkData::NewWithCopy(&data.front(), data.size()); | |
63 } | |
64 | |
65 private: | |
66 static int WriteOutput(const uint8_t* data, | |
67 size_t size, | |
68 const WebPPicture* const picture) { | |
69 std::vector<unsigned char>* dest = | |
70 static_cast<std::vector<unsigned char>*>(picture->custom_ptr); | |
71 dest->insert(dest->end(), data, data + size); | |
72 return 1; | |
73 } | |
74 | |
75 typedef int (*WebPImporter)(WebPPicture* const, | |
76 const uint8_t* const data, | |
77 int row_stride); | |
78 | |
79 bool RgbPictureImport(const unsigned char* pixels, | |
80 WebPImporter import_RGBA, | |
81 WebPPicture* picture) { | |
82 return import_RGBA(picture, pixels, static_cast<int>(picture->width * 4)) != | |
83 0; | |
84 } | |
85 | |
86 bool PlatformImportPicture(const unsigned char* pixels, | |
87 WebPPicture* picture) { | |
88 if (SK_B32_SHIFT) // Android | |
89 return RgbPictureImport(pixels, &WebPPictureImportRGBA, picture); | |
urvang
2016/02/13 00:24:36
WebPPictureImportRGBA() and WebPPictureImportBGRA(
nyquist
2016/02/16 23:37:10
Always assuming premultiplied, and applying unprem
| |
90 | |
91 return RgbPictureImport(pixels, &WebPPictureImportBGRA, picture); | |
92 } | |
93 }; | |
19 | 94 |
20 } // namespace | 95 } // namespace |
21 | 96 |
22 namespace blimp { | 97 namespace blimp { |
23 | 98 |
24 BlimpImageSerializationProcessor::BlimpImageSerializationProcessor(Mode mode) { | 99 BlimpImageSerializationProcessor::BlimpImageSerializationProcessor(Mode mode) { |
25 switch (mode) { | 100 switch (mode) { |
26 case Mode::SERIALIZATION: | 101 case Mode::SERIALIZATION: |
27 pixel_serializer_ = nullptr; | 102 pixel_serializer_.reset(new WebPImageEncoder); |
28 pixel_deserializer_ = nullptr; | 103 pixel_deserializer_ = nullptr; |
29 break; | 104 break; |
30 case Mode::DESERIALIZATION: | 105 case Mode::DESERIALIZATION: |
31 pixel_serializer_ = nullptr; | 106 pixel_serializer_ = nullptr; |
32 pixel_deserializer_ = &NoopDecoder; | 107 pixel_deserializer_ = &WebPDecoder; |
33 break; | 108 break; |
34 default: | 109 default: |
35 NOTREACHED() << "Unknown serialization mode"; | 110 NOTREACHED() << "Unknown serialization mode"; |
36 } | 111 } |
37 } | 112 } |
38 | 113 |
39 BlimpImageSerializationProcessor::~BlimpImageSerializationProcessor() {} | 114 BlimpImageSerializationProcessor::~BlimpImageSerializationProcessor() {} |
40 | 115 |
41 SkPixelSerializer* BlimpImageSerializationProcessor::GetPixelSerializer() { | 116 SkPixelSerializer* BlimpImageSerializationProcessor::GetPixelSerializer() { |
42 return pixel_serializer_.get(); | 117 return pixel_serializer_.get(); |
43 } | 118 } |
44 | 119 |
45 SkPicture::InstallPixelRefProc | 120 SkPicture::InstallPixelRefProc |
46 BlimpImageSerializationProcessor::GetPixelDeserializer() { | 121 BlimpImageSerializationProcessor::GetPixelDeserializer() { |
47 return pixel_deserializer_; | 122 return pixel_deserializer_; |
48 } | 123 } |
49 | 124 |
50 } // namespace blimp | 125 } // namespace blimp |
OLD | NEW |