OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "cc/debug/picture_debug_util.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/base64.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "third_party/skia/include/core/SkBitmap.h" |
| 12 #include "third_party/skia/include/core/SkData.h" |
| 13 #include "third_party/skia/include/core/SkImageInfo.h" |
| 14 #include "third_party/skia/include/core/SkPicture.h" |
| 15 #include "third_party/skia/include/core/SkPixelSerializer.h" |
| 16 #include "third_party/skia/include/core/SkStream.h" |
| 17 #include "ui/gfx/codec/jpeg_codec.h" |
| 18 #include "ui/gfx/codec/png_codec.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 class BitmapSerializer : public SkPixelSerializer { |
| 23 protected: |
| 24 bool onUseEncodedData(const void* data, size_t len) override { return true; } |
| 25 |
| 26 SkData* onEncodePixels(const SkImageInfo& info, |
| 27 const void* pixels, |
| 28 size_t row_bytes) override { |
| 29 const int kJpegQuality = 80; |
| 30 std::vector<unsigned char> data; |
| 31 |
| 32 // If bitmap is opaque, encode as JPEG. |
| 33 // Otherwise encode as PNG. |
| 34 bool encoding_succeeded = false; |
| 35 if (info.isOpaque()) { |
| 36 encoding_succeeded = |
| 37 gfx::JPEGCodec::Encode(reinterpret_cast<const unsigned char*>(pixels), |
| 38 gfx::JPEGCodec::FORMAT_SkBitmap, info.width(), |
| 39 info.height(), row_bytes, kJpegQuality, &data); |
| 40 } else { |
| 41 SkBitmap bm; |
| 42 // The cast is ok, since we only read the bm. |
| 43 if (!bm.installPixels(info, const_cast<void*>(pixels), row_bytes)) { |
| 44 return nullptr; |
| 45 } |
| 46 encoding_succeeded = gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &data); |
| 47 } |
| 48 |
| 49 if (encoding_succeeded) { |
| 50 return SkData::NewWithCopy(&data.front(), data.size()); |
| 51 } |
| 52 return nullptr; |
| 53 } |
| 54 }; |
| 55 |
| 56 } // namespace |
| 57 |
| 58 namespace cc { |
| 59 |
| 60 void PictureDebugUtil::SerializeAsBase64(const SkPicture* picture, |
| 61 std::string* output) { |
| 62 SkDynamicMemoryWStream stream; |
| 63 BitmapSerializer serializer; |
| 64 picture->serialize(&stream, &serializer); |
| 65 |
| 66 size_t serialized_size = stream.bytesWritten(); |
| 67 scoped_ptr<char[]> serialized_picture(new char[serialized_size]); |
| 68 stream.copyTo(serialized_picture.get()); |
| 69 base::Base64Encode(std::string(serialized_picture.get(), serialized_size), |
| 70 output); |
| 71 } |
| 72 |
| 73 } // namespace cc |
OLD | NEW |