summaryrefslogtreecommitdiffstats
path: root/cc/debug/picture_debug_util.cc
blob: 50ae60b9840731191c8aca8fb37981457f869922 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// 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 <stddef.h>

#include <limits>
#include <vector>

#include "base/base64.h"
#include "base/logging.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 {
 protected:
  bool onUseEncodedData(const void* data, size_t len) override { return true; }

  SkData* onEncode(const SkPixmap& pixmap) override {
    const SkImageInfo& info = pixmap.info();
    const void* pixels = pixmap.addr();
    size_t row_bytes = pixmap.rowBytes();
    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()) {
      DCHECK_LE(row_bytes,
                static_cast<size_t>(std::numeric_limits<int>::max()));
      encoding_succeeded = gfx::JPEGCodec::Encode(
          reinterpret_cast<const unsigned char*>(pixels),
          gfx::JPEGCodec::FORMAT_SkBitmap, info.width(), info.height(),
          static_cast<int>(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]);
  stream.copyTo(serialized_picture.get());
  base::Base64Encode(std::string(serialized_picture.get(), serialized_size),
                     output);
}

}  // namespace cc