Commit 3921d94f authored by reed's avatar reed Committed by Commit bot

use new PixelSerializer API, remove legacy flag

BUG=3268

Review URL: https://codereview.chromium.org/802533005

Cr-Commit-Position: refs/heads/master@{#309358}
parent 594d3131
......@@ -23,6 +23,7 @@
#include "third_party/skia/include/core/SkData.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"
#include "third_party/skia/include/core/SkPixelSerializer.h"
#include "third_party/skia/include/core/SkStream.h"
#include "third_party/skia/include/utils/SkNullCanvas.h"
#include "third_party/skia/include/utils/SkPictureUtils.h"
......@@ -35,36 +36,39 @@ namespace cc {
namespace {
SkData* EncodeBitmap(size_t* offset, const SkBitmap& bm) {
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 (bm.isOpaque()) {
SkAutoLockPixels lock_bitmap(bm);
if (bm.empty())
return NULL;
encoding_succeeded = gfx::JPEGCodec::Encode(
reinterpret_cast<unsigned char*>(bm.getAddr32(0, 0)),
gfx::JPEGCodec::FORMAT_SkBitmap,
bm.width(),
bm.height(),
bm.rowBytes(),
kJpegQuality,
&data);
} else {
encoding_succeeded = gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &data);
}
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) {
*offset = 0;
return SkData::NewWithCopy(&data.front(), data.size());
if (encoding_succeeded) {
return SkData::NewWithCopy(&data.front(), data.size());
}
return nullptr;
}
return NULL;
}
};
bool DecodeBitmap(const void* buffer, size_t size, SkBitmap* bm) {
const unsigned char* data = static_cast<const unsigned char *>(buffer);
......@@ -367,7 +371,8 @@ void Picture::Replay(SkCanvas* canvas) {
scoped_ptr<base::Value> Picture::AsValue() const {
SkDynamicMemoryWStream stream;
picture_->serialize(&stream, &EncodeBitmap);
BitmapSerializer serializer;
picture_->serialize(&stream, &serializer);
// Encode the picture as base64.
scoped_ptr<base::DictionaryValue> res(new base::DictionaryValue());
......
......@@ -34,6 +34,7 @@
#include "third_party/skia/include/core/SkGraphics.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "third_party/skia/include/core/SkPixelRef.h"
#include "third_party/skia/include/core/SkPixelSerializer.h"
#include "third_party/skia/include/core/SkStream.h"
#include "ui/gfx/codec/png_codec.h"
#include "v8/include/v8.h"
......@@ -49,21 +50,24 @@ namespace content {
namespace {
// offset parameter is deprecated/ignored, and will be remove from the
// signature in a future skia release. <reed@google.com>
SkData* EncodeBitmapToData(size_t* offset, const SkBitmap& bm) {
SkPixelRef* pr = bm.pixelRef();
if (pr != NULL) {
SkData* data = pr->refEncodedData();
if (data != NULL)
return data;
}
std::vector<unsigned char> vector;
if (gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &vector)) {
return SkData::NewWithCopy(&vector.front(), vector.size());
class PNGSerializer : 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 {
SkBitmap bm;
// The const_cast is fine, since we only read from the bitmap.
if (bm.installPixels(info, const_cast<void*>(pixels), row_bytes)) {
std::vector<unsigned char> vector;
if (gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &vector)) {
return SkData::NewWithCopy(&vector.front(), vector.size());
}
}
return nullptr;
}
return NULL;
}
};
class SkPictureSerializer {
public:
......@@ -97,7 +101,9 @@ class SkPictureSerializer {
DCHECK(!filepath.empty());
SkFILEWStream file(filepath.c_str());
DCHECK(file.isValid());
picture->serialize(&file, &EncodeBitmapToData);
PNGSerializer serializer;
picture->serialize(&file, &serializer);
}
private:
......
......@@ -269,10 +269,6 @@ SK_API void SkDebugf_FileLine(const char* file, int line, bool fatal,
# define SK_IGNORE_GPU_LAYER_HOISTING
#endif
#ifndef SK_LEGACY_ENCODE_BITMAP
# define SK_LEGACY_ENCODE_BITMAP
#endif
// If this goes well, we can have Skia respect DYNAMIC_ANNOTATIONS_ENABLED directly.
#if DYNAMIC_ANNOTATIONS_ENABLED
# define SK_DYNAMIC_ANNOTATIONS_ENABLED 1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment