| 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 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 // Version ID; to be used in serialization. | 35 // Version ID; to be used in serialization. |
| 36 const int kPictureVersion = 1; | 36 const int kPictureVersion = 1; |
| 37 | 37 |
| 38 // Minimum size of a decoded stream that we need. | 38 // Minimum size of a decoded stream that we need. |
| 39 // 4 bytes for version, 4 * 4 for each of the 2 rects. | 39 // 4 bytes for version, 4 * 4 for each of the 2 rects. |
| 40 const unsigned int kMinPictureSizeBytes = 36; | 40 const unsigned int kMinPictureSizeBytes = 36; |
| 41 | 41 |
| 42 bool EncodeBitmap(SkWStream* stream, const SkBitmap& bm) { | 42 SkData* EncodeBitmap(size_t* offset, const SkBitmap& bm) { |
| 43 const int kJpegQuality = 80; | 43 const int kJpegQuality = 80; |
| 44 std::vector<unsigned char> data; | 44 std::vector<unsigned char> data; |
| 45 | 45 |
| 46 // If bitmap is opaque, encode as JPEG. | 46 // If bitmap is opaque, encode as JPEG. |
| 47 // Otherwise encode as PNG. | 47 // Otherwise encode as PNG. |
| 48 bool encoding_succeeded = false; | 48 bool encoding_succeeded = false; |
| 49 if (bm.isOpaque()) { | 49 if (bm.isOpaque()) { |
| 50 SkAutoLockPixels lock_bitmap(bm); | 50 SkAutoLockPixels lock_bitmap(bm); |
| 51 if (bm.empty()) | 51 if (bm.empty()) |
| 52 return false; | 52 return NULL; |
| 53 | 53 |
| 54 encoding_succeeded = gfx::JPEGCodec::Encode( | 54 encoding_succeeded = gfx::JPEGCodec::Encode( |
| 55 reinterpret_cast<unsigned char*>(bm.getAddr32(0, 0)), | 55 reinterpret_cast<unsigned char*>(bm.getAddr32(0, 0)), |
| 56 gfx::JPEGCodec::FORMAT_SkBitmap, | 56 gfx::JPEGCodec::FORMAT_SkBitmap, |
| 57 bm.width(), | 57 bm.width(), |
| 58 bm.height(), | 58 bm.height(), |
| 59 bm.rowBytes(), | 59 bm.rowBytes(), |
| 60 kJpegQuality, | 60 kJpegQuality, |
| 61 &data); | 61 &data); |
| 62 } else { | 62 } else { |
| 63 encoding_succeeded = gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &data); | 63 encoding_succeeded = gfx::PNGCodec::EncodeBGRASkBitmap(bm, false, &data); |
| 64 } | 64 } |
| 65 | 65 |
| 66 if (encoding_succeeded) | 66 if (encoding_succeeded) { |
| 67 return stream->write(&data.front(), data.size()); | 67 *offset = 0; |
| 68 return false; | 68 return SkData::NewWithCopy(&data.front(), data.size()); |
| 69 } |
| 70 return NULL; |
| 69 } | 71 } |
| 70 | 72 |
| 71 bool DecodeBitmap(const void* buffer, size_t size, SkBitmap* bm) { | 73 bool DecodeBitmap(const void* buffer, size_t size, SkBitmap* bm) { |
| 72 const unsigned char* data = static_cast<const unsigned char *>(buffer); | 74 const unsigned char* data = static_cast<const unsigned char *>(buffer); |
| 73 | 75 |
| 74 // Try PNG first. | 76 // Try PNG first. |
| 75 if (gfx::PNGCodec::Decode(data, size, bm)) | 77 if (gfx::PNGCodec::Decode(data, size, bm)) |
| 76 return true; | 78 return true; |
| 77 | 79 |
| 78 // Try JPEG. | 80 // Try JPEG. |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 raster_data->Set("picture_id", TracedValue::CreateIDRef(this).release()); | 499 raster_data->Set("picture_id", TracedValue::CreateIDRef(this).release()); |
| 498 raster_data->SetDouble("scale", scale); | 500 raster_data->SetDouble("scale", scale); |
| 499 raster_data->SetDouble("rect_x", rect.x()); | 501 raster_data->SetDouble("rect_x", rect.x()); |
| 500 raster_data->SetDouble("rect_y", rect.y()); | 502 raster_data->SetDouble("rect_y", rect.y()); |
| 501 raster_data->SetDouble("rect_width", rect.width()); | 503 raster_data->SetDouble("rect_width", rect.width()); |
| 502 raster_data->SetDouble("rect_height", rect.height()); | 504 raster_data->SetDouble("rect_height", rect.height()); |
| 503 return TracedValue::FromValue(raster_data.release()); | 505 return TracedValue::FromValue(raster_data.release()); |
| 504 } | 506 } |
| 505 | 507 |
| 506 } // namespace cc | 508 } // namespace cc |
| OLD | NEW |