blob: e653b78e235e10ceed2e5921675733e690bd635f [file] [log] [blame]
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/debug/picture_debug_util.h"
#include <vector>
#include "base/base64.h"
#include "base/memory/scoped_ptr.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkData.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "third_party/skia/include/core/SkPixelSerializer.h"
#include "third_party/skia/include/core/SkStream.h"
#include "ui/gfx/codec/jpeg_codec.h"
#include "ui/gfx/codec/png_codec.h"
namespace {
class BitmapSerializer : public SkPixelSerializer {
bool onUseEncodedData(const void* data, size_t len) override { return true; }
SkData* onEncodePixels(const SkImageInfo& info,
const void* pixels,
size_t row_bytes) override {
const int kJpegQuality = 80;
std::vector<unsigned char> data;
// If bitmap is opaque, encode as JPEG.
// Otherwise encode as PNG.
bool encoding_succeeded = false;
if (info.isOpaque()) {
encoding_succeeded =
gfx::JPEGCodec::Encode(reinterpret_cast<const unsigned char*>(pixels),
gfx::JPEGCodec::FORMAT_SkBitmap, info.width(),
info.height(), row_bytes, kJpegQuality, &data);
} else {
SkBitmap bm;
// The cast is ok, since we only read the bm.
if (!bm.installPixels(info, const_cast<void*>(pixels), row_bytes)) {
return nullptr;
encoding_succeeded = gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &data);
if (encoding_succeeded) {
return SkData::NewWithCopy(&data.front(), data.size());
return nullptr;
} // namespace
namespace cc {
void PictureDebugUtil::SerializeAsBase64(const SkPicture* picture,
std::string* output) {
SkDynamicMemoryWStream stream;
BitmapSerializer serializer;
picture->serialize(&stream, &serializer);
size_t serialized_size = stream.bytesWritten();
scoped_ptr<char[]> serialized_picture(new char[serialized_size]);
base::Base64Encode(std::string(serialized_picture.get(), serialized_size),
} // namespace cc