Index: cc/resources/picture.cc |
diff --git a/cc/resources/picture.cc b/cc/resources/picture.cc |
index 64f29e4cfacb52c2fbd99a3d239fbc278832118c..a9e7a8b12fd8040459ef09f4c6f4a11ec7cccfc7 100644 |
--- a/cc/resources/picture.cc |
+++ b/cc/resources/picture.cc |
@@ -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,44 @@ 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 { |
+ // For now, always force our onEncodePixels by returning false. |
+ // If we have way to sniff the data to see that it is already PNG or JPEG |
+ // we could return true to skip re-encoding it. |
+ return false; |
scroggo
2014/12/19 20:02:18
This is a change in behavior. The old SkWriteBuffe
enne (OOO)
2014/12/19 21:15:13
If this can be done better, that'd be nice, but th
reed1
2014/12/19 21:30:01
Done.
reed1
2014/12/19 21:30:02
Acknowledged.
|
} |
- if (encoding_succeeded) { |
- *offset = 0; |
- return SkData::NewWithCopy(&data.front(), data.size()); |
+ SkData* onEncodePixels(const SkImageInfo& info, |
+ void* pixels, |
scroggo
2014/12/19 20:02:18
Once https://codereview.chromium.org/788143007/ la
reed1
2014/12/19 21:30:02
Acknowledged.
|
+ size_t rowBytes) override { |
enne (OOO)
2014/12/19 21:15:13
row_bytes
reed1
2014/12/19 21:30:01
Done.
|
+ 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<unsigned char*>(pixels), |
+ gfx::JPEGCodec::FORMAT_SkBitmap, info.width(), |
+ info.height(), rowBytes, kJpegQuality, &data); |
+ } else { |
+ SkBitmap bm; |
+ // The cast is ok, since we only read the bm. |
+ if (!bm.installPixels(info, const_cast<void*>(pixels), rowBytes)) { |
+ return NULL; |
enne (OOO)
2014/12/19 21:15:13
nullptr
reed1
2014/12/19 21:30:02
Done.
|
+ } |
+ encoding_succeeded = gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &data); |
+ } |
+ |
+ if (encoding_succeeded) { |
+ return SkData::NewWithCopy(&data.front(), data.size()); |
+ } |
+ return NULL; |
} |
- return NULL; |
-} |
+}; |
bool DecodeBitmap(const void* buffer, size_t size, SkBitmap* bm) { |
const unsigned char* data = static_cast<const unsigned char *>(buffer); |
@@ -367,7 +376,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()); |