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