Chromium Code Reviews| 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 const int kJpegQuality = 80; |
| 38 DCHECK_LE(pixmap.rowBytes(), | |
| 42 static_cast<size_t>(std::numeric_limits<int>::max())); | 39 static_cast<size_t>(std::numeric_limits<int>::max())); |
| 43 encoding_succeeded = gfx::JPEGCodec::Encode( | 40 encoding_succeeded = gfx::JPEGCodec::Encode(pixmap, kJpegQuality, &data); |
| 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 { | 41 } else { |
| 42 const SkImageInfo& info = pixmap.info(); | |
| 43 const void* pixels = pixmap.addr(); | |
| 44 size_t row_bytes = pixmap.rowBytes(); | |
| 45 | |
| 48 SkBitmap bm; | 46 SkBitmap bm; |
| 49 // The cast is ok, since we only read the bm. | 47 // The cast is ok, since we only read the bm. |
| 50 if (!bm.installPixels(info, const_cast<void*>(pixels), row_bytes)) { | 48 if (!bm.installPixels(info, const_cast<void*>(pixels), row_bytes)) { |
| 51 return nullptr; | 49 return nullptr; |
| 52 } | 50 } |
| 53 encoding_succeeded = gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &data); | 51 encoding_succeeded = gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &data); |
|
scroggo_chromium
2017/06/07 18:15:13
Make this take an SkPixmap, too? (Or maybe do that
msarett1
2017/06/07 18:20:11
Definitely for a separate CL. This one already ha
| |
| 54 } | 52 } |
| 55 | 53 |
| 56 if (encoding_succeeded) { | 54 if (encoding_succeeded) { |
| 57 return SkData::MakeWithCopy(&data.front(), data.size()).release(); | 55 return SkData::MakeWithCopy(&data.front(), data.size()).release(); |
| 58 } | 56 } |
| 59 return nullptr; | 57 return nullptr; |
| 60 } | 58 } |
| 61 }; | 59 }; |
| 62 | 60 |
| 63 } // namespace | 61 } // namespace |
| 64 | 62 |
| 65 namespace cc { | 63 namespace cc { |
| 66 | 64 |
| 67 void PictureDebugUtil::SerializeAsBase64(const SkPicture* picture, | 65 void PictureDebugUtil::SerializeAsBase64(const SkPicture* picture, |
| 68 std::string* output) { | 66 std::string* output) { |
| 69 SkDynamicMemoryWStream stream; | 67 SkDynamicMemoryWStream stream; |
| 70 BitmapSerializer serializer; | 68 BitmapSerializer serializer; |
| 71 picture->serialize(&stream, &serializer); | 69 picture->serialize(&stream, &serializer); |
| 72 | 70 |
| 73 size_t serialized_size = stream.bytesWritten(); | 71 size_t serialized_size = stream.bytesWritten(); |
| 74 std::unique_ptr<char[]> serialized_picture(new char[serialized_size]); | 72 std::unique_ptr<char[]> serialized_picture(new char[serialized_size]); |
| 75 stream.copyTo(serialized_picture.get()); | 73 stream.copyTo(serialized_picture.get()); |
| 76 base::Base64Encode( | 74 base::Base64Encode( |
| 77 base::StringPiece(serialized_picture.get(), serialized_size), output); | 75 base::StringPiece(serialized_picture.get(), serialized_size), output); |
| 78 } | 76 } |
| 79 | 77 |
| 80 } // namespace cc | 78 } // namespace cc |
| OLD | NEW |