OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "cc/debug/picture_debug_util.h" | 5 #include "cc/debug/picture_debug_util.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 #include <memory> | 10 #include <memory> |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "ui/gfx/codec/jpeg_codec.h" | 21 #include "ui/gfx/codec/jpeg_codec.h" |
22 #include "ui/gfx/codec/png_codec.h" | 22 #include "ui/gfx/codec/png_codec.h" |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 class BitmapSerializer : public SkPixelSerializer { | 26 class BitmapSerializer : public SkPixelSerializer { |
27 protected: | 27 protected: |
28 bool onUseEncodedData(const void* data, size_t len) override { return true; } | 28 bool onUseEncodedData(const void* data, size_t len) override { return true; } |
29 | 29 |
30 SkData* onEncode(const SkPixmap& pixmap) override { | 30 SkData* onEncode(const SkPixmap& pixmap) override { |
31 const SkImageInfo& info = pixmap.info(); | |
32 const void* pixels = pixmap.addr(); | |
33 size_t row_bytes = pixmap.rowBytes(); | |
34 const int kJpegQuality = 80; | |
35 std::vector<unsigned char> data; | 31 std::vector<unsigned char> data; |
36 | 32 |
37 // If bitmap is opaque, encode as JPEG. | 33 // If bitmap is opaque, encode as JPEG. |
38 // Otherwise encode as PNG. | 34 // Otherwise encode as PNG. |
39 bool encoding_succeeded = false; | 35 bool encoding_succeeded = false; |
40 if (info.isOpaque()) { | 36 if (pixmap.isOpaque()) { |
41 DCHECK_LE(row_bytes, | 37 constexpr int kJpegQuality = 80; |
42 static_cast<size_t>(std::numeric_limits<int>::max())); | 38 encoding_succeeded = gfx::JPEGCodec::Encode(pixmap, kJpegQuality, &data); |
43 encoding_succeeded = gfx::JPEGCodec::Encode( | |
44 reinterpret_cast<const unsigned char*>(pixels), | |
45 gfx::JPEGCodec::FORMAT_SkBitmap, info.width(), info.height(), | |
46 static_cast<int>(row_bytes), kJpegQuality, &data); | |
47 } else { | 39 } else { |
| 40 const SkImageInfo& info = pixmap.info(); |
| 41 const void* pixels = pixmap.addr(); |
| 42 size_t row_bytes = pixmap.rowBytes(); |
| 43 |
48 SkBitmap bm; | 44 SkBitmap bm; |
49 // The cast is ok, since we only read the bm. | 45 // The cast is ok, since we only read the bm. |
50 if (!bm.installPixels(info, const_cast<void*>(pixels), row_bytes)) { | 46 if (!bm.installPixels(info, const_cast<void*>(pixels), row_bytes)) { |
51 return nullptr; | 47 return nullptr; |
52 } | 48 } |
53 encoding_succeeded = gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &data); | 49 encoding_succeeded = gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &data); |
54 } | 50 } |
55 | 51 |
56 if (encoding_succeeded) { | 52 if (encoding_succeeded) { |
57 return SkData::MakeWithCopy(&data.front(), data.size()).release(); | 53 return SkData::MakeWithCopy(&data.front(), data.size()).release(); |
(...skipping 13 matching lines...) Expand all Loading... |
71 picture->serialize(&stream, &serializer); | 67 picture->serialize(&stream, &serializer); |
72 | 68 |
73 size_t serialized_size = stream.bytesWritten(); | 69 size_t serialized_size = stream.bytesWritten(); |
74 std::unique_ptr<char[]> serialized_picture(new char[serialized_size]); | 70 std::unique_ptr<char[]> serialized_picture(new char[serialized_size]); |
75 stream.copyTo(serialized_picture.get()); | 71 stream.copyTo(serialized_picture.get()); |
76 base::Base64Encode( | 72 base::Base64Encode( |
77 base::StringPiece(serialized_picture.get(), serialized_size), output); | 73 base::StringPiece(serialized_picture.get(), serialized_size), output); |
78 } | 74 } |
79 | 75 |
80 } // namespace cc | 76 } // namespace cc |
OLD | NEW |