Skip to content

Commit

Permalink
SkPixelSerializer is deprecated, update call-sites
Browse files Browse the repository at this point in the history
Skia's default serialization behavior is to encode "bare" images via PNG.
For these utilities, we can just rely on that (except in the case of the
command-line option).

Bug: 
Cq-Include-Trybots: master.tryserver.blink:linux_trusty_blink_rel;master.tryserver.chromium.android:android_optional_gpu_tests_rel
Change-Id: I439786209a59d5604a53d362e64703af430a8056
Reviewed-on: https://chromium-review.googlesource.com/824443
Reviewed-by: Florin Malita <fmalita@chromium.org>
Reviewed-by: vmpstr <vmpstr@chromium.org>
Commit-Queue: Mike Reed <reed@chromium.org>
Cr-Commit-Position: refs/heads/master@{#523826}
  • Loading branch information
reed-at-google authored and Commit Bot committed Dec 13, 2017
1 parent b48592c commit e78fb73
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 107 deletions.
52 changes: 3 additions & 49 deletions cc/debug/picture_debug_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,65 +12,19 @@

#include "base/base64.h"
#include "base/logging.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 {
std::vector<unsigned char> data;

// If bitmap is opaque, encode as JPEG.
// Otherwise encode as PNG.
bool encoding_succeeded = false;
if (pixmap.isOpaque()) {
constexpr int kJpegQuality = 80;
encoding_succeeded = gfx::JPEGCodec::Encode(pixmap, kJpegQuality, &data);
} else {
const SkImageInfo& info = pixmap.info();
const void* pixels = pixmap.addr();
size_t row_bytes = pixmap.rowBytes();

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::MakeWithCopy(&data.front(), data.size()).release();
}
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();
std::unique_ptr<char[]> serialized_picture(new char[serialized_size]);
stream.copyTo(serialized_picture.get());
sk_sp<SkData> data = picture->serialize();
base::Base64Encode(
base::StringPiece(serialized_picture.get(), serialized_size), output);
base::StringPiece(static_cast<const char*>(data->data()), data->size()),
output);
}

} // namespace cc
74 changes: 16 additions & 58 deletions content/renderer/gpu/gpu_benchmarking_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@
#include "third_party/skia/include/core/SkGraphics.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"
#include "third_party/skia/include/core/SkPixelRef.h"
#include "third_party/skia/include/core/SkPixelSerializer.h"
#include "third_party/skia/include/core/SkSerialProcs.h"
#include "third_party/skia/include/core/SkStream.h"
// Note that headers in third_party/skia/src are fragile. This is
// an experimental, fragile, and diagnostic-only document type.
#include "third_party/skia/src/utils/SkMultiPictureDocument.h"
#include "ui/events/base_event_utils.h"
#include "ui/gfx/codec/png_codec.h"
#include "v8/include/v8.h"

#if defined(OS_WIN) && !defined(NDEBUG)
Expand All @@ -80,59 +78,6 @@ using blink::WebView;
namespace content {

namespace {

class EncodingSerializer : public SkPixelSerializer {
protected:
bool onUseEncodedData(const void* data, size_t len) override { return true; }

SkData* onEncode(const SkPixmap& pixmap) override {
std::vector<uint8_t> vector;

const base::CommandLine& commandLine =
*base::CommandLine::ForCurrentProcess();
if (commandLine.HasSwitch(switches::kSkipReencodingOnSKPCapture)) {
// In this case, we just want to store some useful information
// about the image to replace the missing encoded data.

// First make sure that the data does not accidentally match any
// image signatures.
vector.push_back(0xFF);
vector.push_back(0xFF);
vector.push_back(0xFF);
vector.push_back(0xFF);

// Save the width and height.
uint32_t width = pixmap.width();
uint32_t height = pixmap.height();
vector.push_back(width & 0xFF);
vector.push_back((width >> 8) & 0xFF);
vector.push_back((width >> 16) & 0xFF);
vector.push_back((width >> 24) & 0xFF);
vector.push_back(height & 0xFF);
vector.push_back((height >> 8) & 0xFF);
vector.push_back((height >> 16) & 0xFF);
vector.push_back((height >> 24) & 0xFF);

// Save any additional information about the bitmap that may be
// interesting.
vector.push_back(pixmap.colorType());
vector.push_back(pixmap.alphaType());
return SkData::MakeWithCopy(&vector.front(), vector.size()).release();
} else {
SkBitmap bm;
// The const_cast is fine, since we only read from the bitmap.
if (bm.installPixels(pixmap.info(),
const_cast<void*>(pixmap.addr()),
pixmap.rowBytes())) {
if (gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &vector)) {
return SkData::MakeWithCopy(&vector.front(), vector.size()).release();
}
}
}
return nullptr;
}
};

class SkPictureSerializer {
public:
explicit SkPictureSerializer(const base::FilePath& dirpath)
Expand Down Expand Up @@ -163,8 +108,21 @@ class SkPictureSerializer {
SkFILEWStream file(filepath.c_str());
DCHECK(file.isValid());

EncodingSerializer serializer;
picture->serialize(&file, &serializer);
SkSerialProcs procs;
procs.fImageProc = [](SkImage* image, void*) {
auto data = image->refEncodedData();
if (!data) {
const base::CommandLine& commandLine =
*base::CommandLine::ForCurrentProcess();
if (commandLine.HasSwitch(switches::kSkipReencodingOnSKPCapture)) {
data = SkData::MakeEmpty();
}
// else data is null, which triggers skia's default PNG encode
}
return data;
};
auto data = picture->serialize(procs);
file.write(data->data(), data->size());
file.fsync();
}
}
Expand Down

0 comments on commit e78fb73

Please sign in to comment.