Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Unified Diff: cc/debug/picture_debug_util.cc

Issue 851503003: Update from https://crrev.com/311076 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « cc/debug/picture_debug_util.h ('k') | cc/layers/delegated_frame_provider.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cc/debug/picture_debug_util.cc
diff --git a/cc/debug/picture_debug_util.cc b/cc/debug/picture_debug_util.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e653b78e235e10ceed2e5921675733e690bd635f
--- /dev/null
+++ b/cc/debug/picture_debug_util.cc
@@ -0,0 +1,73 @@
+// 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 {
+ protected:
+ 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]);
+ stream.copyTo(serialized_picture.get());
+ base::Base64Encode(std::string(serialized_picture.get(), serialized_size),
+ output);
+}
+
+} // namespace cc
« no previous file with comments | « cc/debug/picture_debug_util.h ('k') | cc/layers/delegated_frame_provider.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698