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