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" |
16 #include "third_party/skia/include/core/SkUnPreMultiply.h" | |
13 | 17 |
14 namespace { | 18 namespace { |
15 bool NoopDecoder(const void* input, size_t input_size, SkBitmap* bitmap) { | 19 // TODO(nyquist): Make sure encoder does not serialize images more than once. |
16 // TODO(nyquist): Add an image decoder. | 20 // See crbug.com/548434. |
17 return false; | 21 class WebPImageEncoder : public SkPixelSerializer { |
18 } | 22 public: |
23 WebPImageEncoder() {} | |
24 ~WebPImageEncoder() override{}; | |
25 | |
26 bool onUseEncodedData(const void* data, size_t len) override { | |
27 const unsigned char* cast_data = static_cast<const unsigned char*>(data); | |
28 if (len < 14) | |
29 return false; | |
30 return !memcmp(cast_data, "RIFF", 4) && !memcmp(cast_data + 8, "WEBPVP", 6); | |
31 } | |
32 | |
33 SkData* onEncode(const SkPixmap& pixmap) override { | |
34 // Initialize an empty WebPConfig. | |
35 WebPConfig config; | |
36 if (!WebPConfigInit(&config)) | |
37 return nullptr; | |
38 | |
39 // Initialize an empty WebPPicture. | |
40 WebPPicture picture; | |
41 if (!WebPPictureInit(&picture)) | |
42 return nullptr; | |
43 | |
44 // Ensure width and height are valid dimensions. | |
45 if (!pixmap.width() || pixmap.width() > WEBP_MAX_DIMENSION) | |
46 return nullptr; | |
47 picture.width = pixmap.width(); | |
48 if (!pixmap.height() || pixmap.height() > WEBP_MAX_DIMENSION) | |
49 return nullptr; | |
50 picture.height = pixmap.height(); | |
51 | |
52 // Import picture from raw pixels. | |
53 DCHECK(pixmap.alphaType() == kPremul_SkAlphaType); | |
54 auto pixel_chars = static_cast<const unsigned char*>(pixmap.addr()); | |
55 if (!PlatformPictureImport(pixel_chars, &picture)) | |
56 return nullptr; | |
57 | |
58 // Create a buffer for where to store the output data. | |
59 std::vector<unsigned char> data; | |
60 picture.custom_ptr = &data; | |
61 | |
62 // Use our own WebPWriterFunction implementation. | |
63 picture.writer = &WebPImageEncoder::WriteOutput; | |
64 | |
65 // Setup the configuration for the output WebP picture. | |
66 config.quality = 100.0; // between 0 (smallest file) and 100 (biggest). | |
urvang
2016/02/17 01:49:08
You should stick to the default quality (=75) & me
nyquist
2016/02/17 16:56:44
OK. Kept at the defaults, but hard-coded them for
| |
67 config.method = 6; // quality/speed trade-off (0=fast, 6=slower-better). | |
68 | |
69 // Encode the picture using the given configuration. | |
70 bool success = WebPEncode(&config, &picture); | |
71 | |
72 // Release the memory allocated by WebPPictureImport*(). This does not free | |
73 // the memory used by the picture object itself. | |
74 WebPPictureFree(&picture); | |
75 | |
76 if (!success) | |
77 return nullptr; | |
78 | |
79 // Copy WebP data into SkData. |data| is allocated only on the stack, so | |
80 // it is automatically deleted after this. | |
81 return SkData::NewWithCopy(&data.front(), data.size()); | |
82 } | |
83 | |
84 private: | |
85 // WebPWriterFunction implementation. | |
86 static int WriteOutput(const uint8_t* data, | |
87 size_t size, | |
88 const WebPPicture* const picture) { | |
89 std::vector<unsigned char>* dest = | |
90 static_cast<std::vector<unsigned char>*>(picture->custom_ptr); | |
91 dest->insert(dest->end(), data, data + size); | |
92 return 1; | |
93 } | |
94 | |
95 void UnPremultiply(const unsigned char* in_pixels, | |
96 unsigned char* out_pixels, | |
97 size_t pixel_count) { | |
98 const SkUnPreMultiply::Scale* table = SkUnPreMultiply::GetScaleTable(); | |
99 for (; pixel_count-- > 0; in_pixels += 4) { | |
100 unsigned char alpha = in_pixels[3]; | |
101 if (alpha == 255) { // Full opacity, just blindly copy. | |
102 *out_pixels++ = in_pixels[0]; | |
103 *out_pixels++ = in_pixels[1]; | |
104 *out_pixels++ = in_pixels[2]; | |
105 *out_pixels++ = alpha; | |
106 } else { | |
107 SkUnPreMultiply::Scale scale = table[alpha]; | |
108 *out_pixels++ = SkUnPreMultiply::ApplyScale(scale, in_pixels[0]); | |
109 *out_pixels++ = SkUnPreMultiply::ApplyScale(scale, in_pixels[1]); | |
110 *out_pixels++ = SkUnPreMultiply::ApplyScale(scale, in_pixels[2]); | |
111 *out_pixels++ = alpha; | |
112 } | |
113 } | |
114 } | |
115 | |
116 bool PlatformPictureImport(const unsigned char* pixels, | |
117 WebPPicture* picture) { | |
118 // Need to unpremultiply each pixel. | |
119 size_t pixel_count = picture->height * picture->width; | |
120 std::vector<unsigned char> unpremul_pixels(pixel_count * 4); | |
121 UnPremultiply(pixels, unpremul_pixels.data(), pixel_count); | |
122 | |
123 int row_stride = static_cast<int>(picture->width * 4); | |
urvang
2016/02/17 01:49:08
Isn't picture->width already an int?
nyquist
2016/02/17 16:56:44
Doh. My bad. Done.
| |
124 | |
125 if (SK_B32_SHIFT) // Android | |
126 return WebPPictureImportRGBA(picture, unpremul_pixels.data(), row_stride); | |
127 return WebPPictureImportBGRA(picture, unpremul_pixels.data(), row_stride); | |
128 } | |
129 }; | |
19 | 130 |
20 } // namespace | 131 } // namespace |
21 | 132 |
22 namespace blimp { | 133 namespace blimp { |
23 | 134 |
24 BlimpImageSerializationProcessor::BlimpImageSerializationProcessor(Mode mode) { | 135 BlimpImageSerializationProcessor::BlimpImageSerializationProcessor(Mode mode) { |
25 switch (mode) { | 136 switch (mode) { |
26 case Mode::SERIALIZATION: | 137 case Mode::SERIALIZATION: |
27 pixel_serializer_ = nullptr; | 138 pixel_serializer_.reset(new WebPImageEncoder); |
28 pixel_deserializer_ = nullptr; | 139 pixel_deserializer_ = nullptr; |
29 break; | 140 break; |
30 case Mode::DESERIALIZATION: | 141 case Mode::DESERIALIZATION: |
31 pixel_serializer_ = nullptr; | 142 pixel_serializer_ = nullptr; |
32 pixel_deserializer_ = &NoopDecoder; | 143 pixel_deserializer_ = &WebPDecoder; |
33 break; | 144 break; |
34 default: | 145 default: |
35 NOTREACHED() << "Unknown serialization mode"; | 146 NOTREACHED() << "Unknown serialization mode"; |
36 } | 147 } |
37 } | 148 } |
38 | 149 |
39 BlimpImageSerializationProcessor::~BlimpImageSerializationProcessor() {} | 150 BlimpImageSerializationProcessor::~BlimpImageSerializationProcessor() {} |
40 | 151 |
41 SkPixelSerializer* BlimpImageSerializationProcessor::GetPixelSerializer() { | 152 SkPixelSerializer* BlimpImageSerializationProcessor::GetPixelSerializer() { |
42 return pixel_serializer_.get(); | 153 return pixel_serializer_.get(); |
43 } | 154 } |
44 | 155 |
45 SkPicture::InstallPixelRefProc | 156 SkPicture::InstallPixelRefProc |
46 BlimpImageSerializationProcessor::GetPixelDeserializer() { | 157 BlimpImageSerializationProcessor::GetPixelDeserializer() { |
47 return pixel_deserializer_; | 158 return pixel_deserializer_; |
48 } | 159 } |
49 | 160 |
50 } // namespace blimp | 161 } // namespace blimp |
OLD | NEW |