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" |
| 6 |
| 7 #include "base/base64.h" |
5 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
6 #include "cc/debug/rendering_stats.h" | 9 #include "cc/debug/rendering_stats.h" |
7 #include "cc/layers/content_layer_client.h" | 10 #include "cc/layers/content_layer_client.h" |
8 #include "cc/resources/picture.h" | |
9 #include "skia/ext/analysis_canvas.h" | 11 #include "skia/ext/analysis_canvas.h" |
10 #include "third_party/skia/include/core/SkCanvas.h" | 12 #include "third_party/skia/include/core/SkCanvas.h" |
11 #include "third_party/skia/include/core/SkData.h" | 13 #include "third_party/skia/include/core/SkData.h" |
12 #include "third_party/skia/include/core/SkDrawFilter.h" | 14 #include "third_party/skia/include/core/SkDrawFilter.h" |
13 #include "third_party/skia/include/core/SkPaint.h" | 15 #include "third_party/skia/include/core/SkPaint.h" |
| 16 #include "third_party/skia/include/core/SkStream.h" |
14 #include "third_party/skia/include/utils/SkPictureUtils.h" | 17 #include "third_party/skia/include/utils/SkPictureUtils.h" |
15 #include "ui/gfx/rect_conversions.h" | 18 #include "ui/gfx/rect_conversions.h" |
16 #include "ui/gfx/skia_util.h" | 19 #include "ui/gfx/skia_util.h" |
17 | 20 |
18 namespace { | 21 namespace { |
19 // URI label for a lazily decoded SkPixelRef. | 22 // URI label for a lazily decoded SkPixelRef. |
20 const char kLabelLazyDecoded[] = "lazy"; | 23 const char kLabelLazyDecoded[] = "lazy"; |
| 24 // Version ID; to be used in serialization. |
| 25 const int kPictureVersion = 1; |
| 26 // Minimum size of a decoded stream that we need. |
| 27 // 4 bytes for version, 4 * 4 for each of the 2 rects. |
| 28 const unsigned int kMinPictureSizeBytes = 36; |
21 | 29 |
22 class DisableLCDTextFilter : public SkDrawFilter { | 30 class DisableLCDTextFilter : public SkDrawFilter { |
23 public: | 31 public: |
24 // SkDrawFilter interface. | 32 // SkDrawFilter interface. |
25 virtual bool filter(SkPaint* paint, SkDrawFilter::Type type) OVERRIDE { | 33 virtual bool filter(SkPaint* paint, SkDrawFilter::Type type) OVERRIDE { |
26 if (type != SkDrawFilter::kText_Type) | 34 if (type != SkDrawFilter::kText_Type) |
27 return true; | 35 return true; |
28 | 36 |
29 paint->setLCDRenderText(false); | 37 paint->setLCDRenderText(false); |
30 return true; | 38 return true; |
31 } | 39 } |
32 }; | 40 }; |
33 } // namespace | 41 } // namespace |
34 | 42 |
35 namespace cc { | 43 namespace cc { |
36 | 44 |
37 scoped_refptr<Picture> Picture::Create(gfx::Rect layer_rect) { | 45 scoped_refptr<Picture> Picture::Create(gfx::Rect layer_rect) { |
38 return make_scoped_refptr(new Picture(layer_rect)); | 46 return make_scoped_refptr(new Picture(layer_rect)); |
39 } | 47 } |
40 | 48 |
| 49 scoped_refptr<Picture> Picture::CreateFromBase64String( |
| 50 const std::string& encoded_string) { |
| 51 bool success; |
| 52 scoped_refptr<Picture> picture = |
| 53 make_scoped_refptr(new Picture(encoded_string, &success)); |
| 54 if (!success) |
| 55 picture = NULL; |
| 56 return picture; |
| 57 } |
| 58 |
41 Picture::Picture(gfx::Rect layer_rect) | 59 Picture::Picture(gfx::Rect layer_rect) |
42 : layer_rect_(layer_rect) { | 60 : layer_rect_(layer_rect) { |
43 } | 61 } |
44 | 62 |
| 63 Picture::Picture(const std::string& encoded_string, bool* success) { |
| 64 // Decode the picture from base64. |
| 65 std::string decoded; |
| 66 base::Base64Decode(encoded_string, &decoded); |
| 67 SkMemoryStream stream(decoded.data(), decoded.size()); |
| 68 |
| 69 if (decoded.size() < kMinPictureSizeBytes) { |
| 70 *success = false; |
| 71 return; |
| 72 } |
| 73 |
| 74 int version = stream.readS32(); |
| 75 if (version != kPictureVersion) { |
| 76 *success = false; |
| 77 return; |
| 78 } |
| 79 |
| 80 // First, read the layer and opaque rects. |
| 81 int layer_rect_x = stream.readS32(); |
| 82 int layer_rect_y = stream.readS32(); |
| 83 int layer_rect_width = stream.readS32(); |
| 84 int layer_rect_height = stream.readS32(); |
| 85 layer_rect_ = gfx::Rect(layer_rect_x, |
| 86 layer_rect_y, |
| 87 layer_rect_width, |
| 88 layer_rect_height); |
| 89 int opaque_rect_x = stream.readS32(); |
| 90 int opaque_rect_y = stream.readS32(); |
| 91 int opaque_rect_width = stream.readS32(); |
| 92 int opaque_rect_height = stream.readS32(); |
| 93 opaque_rect_ = gfx::Rect(opaque_rect_x, |
| 94 opaque_rect_y, |
| 95 opaque_rect_width, |
| 96 opaque_rect_height); |
| 97 |
| 98 // Read the picture. This creates an empty picture on failure. |
| 99 picture_ = skia::AdoptRef(new SkPicture(&stream, success, NULL)); |
| 100 } |
| 101 |
45 Picture::Picture(const skia::RefPtr<SkPicture>& picture, | 102 Picture::Picture(const skia::RefPtr<SkPicture>& picture, |
46 gfx::Rect layer_rect, | 103 gfx::Rect layer_rect, |
47 gfx::Rect opaque_rect) : | 104 gfx::Rect opaque_rect) : |
48 layer_rect_(layer_rect), | 105 layer_rect_(layer_rect), |
49 opaque_rect_(opaque_rect), | 106 opaque_rect_(opaque_rect), |
50 picture_(picture) { | 107 picture_(picture) { |
51 } | 108 } |
52 | 109 |
53 Picture::~Picture() { | 110 Picture::~Picture() { |
54 } | 111 } |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 for (size_t i = 0; i < pixel_refs->size() / sizeof(*refs); ++i) { | 228 for (size_t i = 0; i < pixel_refs->size() / sizeof(*refs); ++i) { |
172 if (*refs && (*refs)->getURI() && !strncmp( | 229 if (*refs && (*refs)->getURI() && !strncmp( |
173 (*refs)->getURI(), kLabelLazyDecoded, 4)) { | 230 (*refs)->getURI(), kLabelLazyDecoded, 4)) { |
174 pixel_ref_list.push_back(static_cast<skia::LazyPixelRef*>(*refs)); | 231 pixel_ref_list.push_back(static_cast<skia::LazyPixelRef*>(*refs)); |
175 } | 232 } |
176 refs++; | 233 refs++; |
177 } | 234 } |
178 pixel_refs->unref(); | 235 pixel_refs->unref(); |
179 } | 236 } |
180 | 237 |
| 238 void Picture::AsBase64String(std::string* output) const { |
| 239 SkDynamicMemoryWStream stream; |
| 240 |
| 241 // First save the version, layer_rect_ and opaque_rect. |
| 242 stream.write32(kPictureVersion); |
| 243 |
| 244 stream.write32(layer_rect_.x()); |
| 245 stream.write32(layer_rect_.y()); |
| 246 stream.write32(layer_rect_.width()); |
| 247 stream.write32(layer_rect_.height()); |
| 248 |
| 249 stream.write32(opaque_rect_.x()); |
| 250 stream.write32(opaque_rect_.y()); |
| 251 stream.write32(opaque_rect_.width()); |
| 252 stream.write32(opaque_rect_.height()); |
| 253 |
| 254 // Serialize the picture. |
| 255 picture_->serialize(&stream); |
| 256 |
| 257 // Encode the picture as base64. |
| 258 size_t serialized_size = stream.bytesWritten(); |
| 259 scoped_ptr<char[]> serialized_picture(new char[serialized_size]); |
| 260 stream.copyTo(serialized_picture.get()); |
| 261 base::Base64Encode(std::string(serialized_picture.get(), serialized_size), |
| 262 output); |
| 263 } |
| 264 |
181 } // namespace cc | 265 } // namespace cc |
OLD | NEW |