OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/resources/picture.h" | 5 #include "cc/resources/picture.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <set> | 9 #include <set> |
10 | 10 |
11 #include "base/base64.h" | 11 #include "base/base64.h" |
12 #include "base/debug/trace_event.h" | 12 #include "base/debug/trace_event.h" |
13 #include "base/debug/trace_event_argument.h" | 13 #include "base/debug/trace_event_argument.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "cc/base/math_util.h" | 15 #include "cc/base/math_util.h" |
16 #include "cc/base/util.h" | 16 #include "cc/base/util.h" |
17 #include "cc/debug/traced_picture.h" | 17 #include "cc/debug/traced_picture.h" |
18 #include "cc/debug/traced_value.h" | 18 #include "cc/debug/traced_value.h" |
19 #include "cc/layers/content_layer_client.h" | 19 #include "cc/layers/content_layer_client.h" |
20 #include "skia/ext/pixel_ref_utils.h" | 20 #include "skia/ext/pixel_ref_utils.h" |
21 #include "third_party/skia/include/core/SkBBHFactory.h" | 21 #include "third_party/skia/include/core/SkBBHFactory.h" |
22 #include "third_party/skia/include/core/SkCanvas.h" | 22 #include "third_party/skia/include/core/SkCanvas.h" |
23 #include "third_party/skia/include/core/SkData.h" | 23 #include "third_party/skia/include/core/SkData.h" |
24 #include "third_party/skia/include/core/SkPaint.h" | 24 #include "third_party/skia/include/core/SkPaint.h" |
25 #include "third_party/skia/include/core/SkPictureRecorder.h" | 25 #include "third_party/skia/include/core/SkPictureRecorder.h" |
26 #include "third_party/skia/include/core/SkPixelSerializer.h" | |
26 #include "third_party/skia/include/core/SkStream.h" | 27 #include "third_party/skia/include/core/SkStream.h" |
27 #include "third_party/skia/include/utils/SkNullCanvas.h" | 28 #include "third_party/skia/include/utils/SkNullCanvas.h" |
28 #include "third_party/skia/include/utils/SkPictureUtils.h" | 29 #include "third_party/skia/include/utils/SkPictureUtils.h" |
29 #include "ui/gfx/codec/jpeg_codec.h" | 30 #include "ui/gfx/codec/jpeg_codec.h" |
30 #include "ui/gfx/codec/png_codec.h" | 31 #include "ui/gfx/codec/png_codec.h" |
31 #include "ui/gfx/geometry/rect_conversions.h" | 32 #include "ui/gfx/geometry/rect_conversions.h" |
32 #include "ui/gfx/skia_util.h" | 33 #include "ui/gfx/skia_util.h" |
33 | 34 |
34 namespace cc { | 35 namespace cc { |
35 | 36 |
36 namespace { | 37 namespace { |
37 | 38 |
38 SkData* EncodeBitmap(size_t* offset, const SkBitmap& bm) { | 39 class BitmapSerializer : public SkPixelSerializer { |
39 const int kJpegQuality = 80; | 40 protected: |
40 std::vector<unsigned char> data; | 41 bool onUseEncodedData(const void* data, size_t len) override { |
41 | 42 // For now, always force our onEncodePixels by returning false. |
42 // If bitmap is opaque, encode as JPEG. | 43 // If we have way to sniff the data to see that it is already PNG or JPEG |
43 // Otherwise encode as PNG. | 44 // we could return true to skip re-encoding it. |
44 bool encoding_succeeded = false; | 45 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.
| |
45 if (bm.isOpaque()) { | |
46 SkAutoLockPixels lock_bitmap(bm); | |
47 if (bm.empty()) | |
48 return NULL; | |
49 | |
50 encoding_succeeded = gfx::JPEGCodec::Encode( | |
51 reinterpret_cast<unsigned char*>(bm.getAddr32(0, 0)), | |
52 gfx::JPEGCodec::FORMAT_SkBitmap, | |
53 bm.width(), | |
54 bm.height(), | |
55 bm.rowBytes(), | |
56 kJpegQuality, | |
57 &data); | |
58 } else { | |
59 encoding_succeeded = gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &data); | |
60 } | 46 } |
61 | 47 |
62 if (encoding_succeeded) { | 48 SkData* onEncodePixels(const SkImageInfo& info, |
63 *offset = 0; | 49 void* pixels, |
scroggo
2014/12/19 20:02:18
Once https://codereview.chromium.org/788143007/ la
reed1
2014/12/19 21:30:02
Acknowledged.
| |
64 return SkData::NewWithCopy(&data.front(), data.size()); | 50 size_t rowBytes) override { |
enne (OOO)
2014/12/19 21:15:13
row_bytes
reed1
2014/12/19 21:30:01
Done.
| |
51 const int kJpegQuality = 80; | |
52 std::vector<unsigned char> data; | |
53 | |
54 // If bitmap is opaque, encode as JPEG. | |
55 // Otherwise encode as PNG. | |
56 bool encoding_succeeded = false; | |
57 if (info.isOpaque()) { | |
58 encoding_succeeded = | |
59 gfx::JPEGCodec::Encode(reinterpret_cast<unsigned char*>(pixels), | |
60 gfx::JPEGCodec::FORMAT_SkBitmap, info.width(), | |
61 info.height(), rowBytes, kJpegQuality, &data); | |
62 } else { | |
63 SkBitmap bm; | |
64 // The cast is ok, since we only read the bm. | |
65 if (!bm.installPixels(info, const_cast<void*>(pixels), rowBytes)) { | |
66 return NULL; | |
enne (OOO)
2014/12/19 21:15:13
nullptr
reed1
2014/12/19 21:30:02
Done.
| |
67 } | |
68 encoding_succeeded = gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &data); | |
69 } | |
70 | |
71 if (encoding_succeeded) { | |
72 return SkData::NewWithCopy(&data.front(), data.size()); | |
73 } | |
74 return NULL; | |
65 } | 75 } |
66 return NULL; | 76 }; |
67 } | |
68 | 77 |
69 bool DecodeBitmap(const void* buffer, size_t size, SkBitmap* bm) { | 78 bool DecodeBitmap(const void* buffer, size_t size, SkBitmap* bm) { |
70 const unsigned char* data = static_cast<const unsigned char *>(buffer); | 79 const unsigned char* data = static_cast<const unsigned char *>(buffer); |
71 | 80 |
72 // Try PNG first. | 81 // Try PNG first. |
73 if (gfx::PNGCodec::Decode(data, size, bm)) | 82 if (gfx::PNGCodec::Decode(data, size, bm)) |
74 return true; | 83 return true; |
75 | 84 |
76 // Try JPEG. | 85 // Try JPEG. |
77 scoped_ptr<SkBitmap> decoded_jpeg(gfx::JPEGCodec::Decode(data, size)); | 86 scoped_ptr<SkBitmap> decoded_jpeg(gfx::JPEGCodec::Decode(data, size)); |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
360 DCHECK(picture_); | 369 DCHECK(picture_); |
361 picture_->playback(canvas); | 370 picture_->playback(canvas); |
362 SkIRect bounds; | 371 SkIRect bounds; |
363 canvas->getClipDeviceBounds(&bounds); | 372 canvas->getClipDeviceBounds(&bounds); |
364 TRACE_EVENT_END1("cc", "Picture::Replay", | 373 TRACE_EVENT_END1("cc", "Picture::Replay", |
365 "num_pixels_replayed", bounds.width() * bounds.height()); | 374 "num_pixels_replayed", bounds.width() * bounds.height()); |
366 } | 375 } |
367 | 376 |
368 scoped_ptr<base::Value> Picture::AsValue() const { | 377 scoped_ptr<base::Value> Picture::AsValue() const { |
369 SkDynamicMemoryWStream stream; | 378 SkDynamicMemoryWStream stream; |
370 picture_->serialize(&stream, &EncodeBitmap); | 379 BitmapSerializer serializer; |
380 picture_->serialize(&stream, &serializer); | |
371 | 381 |
372 // Encode the picture as base64. | 382 // Encode the picture as base64. |
373 scoped_ptr<base::DictionaryValue> res(new base::DictionaryValue()); | 383 scoped_ptr<base::DictionaryValue> res(new base::DictionaryValue()); |
374 res->Set("params.layer_rect", MathUtil::AsValue(layer_rect_).release()); | 384 res->Set("params.layer_rect", MathUtil::AsValue(layer_rect_).release()); |
375 | 385 |
376 size_t serialized_size = stream.bytesWritten(); | 386 size_t serialized_size = stream.bytesWritten(); |
377 scoped_ptr<char[]> serialized_picture(new char[serialized_size]); | 387 scoped_ptr<char[]> serialized_picture(new char[serialized_size]); |
378 stream.copyTo(serialized_picture.get()); | 388 stream.copyTo(serialized_picture.get()); |
379 std::string b64_picture; | 389 std::string b64_picture; |
380 base::Base64Encode(std::string(serialized_picture.get(), serialized_size), | 390 base::Base64Encode(std::string(serialized_picture.get(), serialized_size), |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
515 scoped_refptr<base::debug::TracedValue> record_data = | 525 scoped_refptr<base::debug::TracedValue> record_data = |
516 new base::debug::TracedValue(); | 526 new base::debug::TracedValue(); |
517 TracedValue::SetIDRef(this, record_data.get(), "picture_id"); | 527 TracedValue::SetIDRef(this, record_data.get(), "picture_id"); |
518 record_data->BeginArray("layer_rect"); | 528 record_data->BeginArray("layer_rect"); |
519 MathUtil::AddToTracedValue(layer_rect_, record_data.get()); | 529 MathUtil::AddToTracedValue(layer_rect_, record_data.get()); |
520 record_data->EndArray(); | 530 record_data->EndArray(); |
521 return record_data; | 531 return record_data; |
522 } | 532 } |
523 | 533 |
524 } // namespace cc | 534 } // namespace cc |
OLD | NEW |