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"; |
21 | 24 |
22 class DisableLCDTextFilter : public SkDrawFilter { | 25 class DisableLCDTextFilter : public SkDrawFilter { |
23 public: | 26 public: |
24 // SkDrawFilter interface. | 27 // SkDrawFilter interface. |
25 virtual bool filter(SkPaint* paint, SkDrawFilter::Type type) OVERRIDE { | 28 virtual bool filter(SkPaint* paint, SkDrawFilter::Type type) OVERRIDE { |
26 if (type != SkDrawFilter::kText_Type) | 29 if (type != SkDrawFilter::kText_Type) |
27 return true; | 30 return true; |
28 | 31 |
29 paint->setLCDRenderText(false); | 32 paint->setLCDRenderText(false); |
30 return true; | 33 return true; |
31 } | 34 } |
32 }; | 35 }; |
33 } // namespace | 36 } // namespace |
34 | 37 |
35 namespace cc { | 38 namespace cc { |
36 | 39 |
37 scoped_refptr<Picture> Picture::Create(gfx::Rect layer_rect) { | 40 scoped_refptr<Picture> Picture::Create(gfx::Rect layer_rect) { |
38 return make_scoped_refptr(new Picture(layer_rect)); | 41 return make_scoped_refptr(new Picture(layer_rect)); |
39 } | 42 } |
40 | 43 |
44 scoped_refptr<Picture> Picture::CreateFromBase64String( | |
45 const std::string& encoded_string) { | |
46 bool success; | |
47 scoped_refptr<Picture> picture = | |
48 make_scoped_refptr(new Picture(encoded_string, &success)); | |
49 if (!success) | |
50 picture = NULL; | |
51 return picture; | |
52 } | |
53 | |
41 Picture::Picture(gfx::Rect layer_rect) | 54 Picture::Picture(gfx::Rect layer_rect) |
42 : layer_rect_(layer_rect) { | 55 : layer_rect_(layer_rect) { |
43 } | 56 } |
44 | 57 |
58 Picture::Picture(const std::string& encoded_string, bool* success) { | |
59 // Decode the picture from base64. | |
60 std::string decoded; | |
nduca
2013/04/18 21:33:04
put a cc picture number in this so that if we eve
| |
61 base::Base64Decode(encoded_string, &decoded); | |
62 SkMemoryStream stream(decoded.data(), decoded.size()); | |
63 | |
64 // First, read the layer and opaque rects. | |
65 int layer_rect_x = stream.readS32(); | |
66 int layer_rect_y = stream.readS32(); | |
67 int layer_rect_width = stream.readS32(); | |
68 int layer_rect_height = stream.readS32(); | |
69 layer_rect_ = gfx::Rect(layer_rect_x, | |
70 layer_rect_y, | |
71 layer_rect_width, | |
72 layer_rect_height); | |
73 int opaque_rect_x = stream.readS32(); | |
74 int opaque_rect_y = stream.readS32(); | |
75 int opaque_rect_width = stream.readS32(); | |
76 int opaque_rect_height = stream.readS32(); | |
77 opaque_rect_ = gfx::Rect(opaque_rect_x, | |
78 opaque_rect_y, | |
79 opaque_rect_width, | |
80 opaque_rect_height); | |
81 | |
82 // Read the picture. This creates an empty picture on failure. | |
83 picture_ = skia::AdoptRef(new SkPicture(&stream, success, NULL)); | |
84 } | |
85 | |
45 Picture::Picture(const skia::RefPtr<SkPicture>& picture, | 86 Picture::Picture(const skia::RefPtr<SkPicture>& picture, |
46 gfx::Rect layer_rect, | 87 gfx::Rect layer_rect, |
47 gfx::Rect opaque_rect) : | 88 gfx::Rect opaque_rect) : |
48 layer_rect_(layer_rect), | 89 layer_rect_(layer_rect), |
49 opaque_rect_(opaque_rect), | 90 opaque_rect_(opaque_rect), |
50 picture_(picture) { | 91 picture_(picture) { |
51 } | 92 } |
52 | 93 |
53 Picture::~Picture() { | 94 Picture::~Picture() { |
54 } | 95 } |
(...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) { | 212 for (size_t i = 0; i < pixel_refs->size() / sizeof(*refs); ++i) { |
172 if (*refs && (*refs)->getURI() && !strncmp( | 213 if (*refs && (*refs)->getURI() && !strncmp( |
173 (*refs)->getURI(), kLabelLazyDecoded, 4)) { | 214 (*refs)->getURI(), kLabelLazyDecoded, 4)) { |
174 pixel_ref_list.push_back(static_cast<skia::LazyPixelRef*>(*refs)); | 215 pixel_ref_list.push_back(static_cast<skia::LazyPixelRef*>(*refs)); |
175 } | 216 } |
176 refs++; | 217 refs++; |
177 } | 218 } |
178 pixel_refs->unref(); | 219 pixel_refs->unref(); |
179 } | 220 } |
180 | 221 |
222 void Picture::AsBase64String(std::string* output) const { | |
223 SkDynamicMemoryWStream stream; | |
224 | |
225 // First save layer_rect_ and opaque_rect. | |
226 stream.write32(layer_rect_.x()); | |
227 stream.write32(layer_rect_.y()); | |
228 stream.write32(layer_rect_.width()); | |
229 stream.write32(layer_rect_.height()); | |
230 stream.write32(opaque_rect_.x()); | |
231 stream.write32(opaque_rect_.y()); | |
232 stream.write32(opaque_rect_.width()); | |
233 stream.write32(opaque_rect_.height()); | |
234 | |
235 // Serialize the picture. | |
236 picture_->serialize(&stream); | |
237 | |
238 // Encode the picture as base64. | |
239 size_t serialized_size = stream.bytesWritten(); | |
240 scoped_ptr<char[]> serialized_picture(new char[serialized_size]); | |
241 stream.copyTo(serialized_picture.get()); | |
242 base::Base64Encode(std::string(serialized_picture.get(), serialized_size), | |
243 output); | |
244 } | |
245 | |
181 } // namespace cc | 246 } // namespace cc |
OLD | NEW |