OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "content/renderer/skia_benchmarking_extension.h" | 5 #include "content/renderer/skia_benchmarking_extension.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 gfx::Rect layer_rect; | 42 gfx::Rect layer_rect; |
43 sk_sp<SkPicture> picture; | 43 sk_sp<SkPicture> picture; |
44 }; | 44 }; |
45 | 45 |
46 bool DecodeBitmap(const void* buffer, size_t size, SkBitmap* bm) { | 46 bool DecodeBitmap(const void* buffer, size_t size, SkBitmap* bm) { |
47 const unsigned char* data = static_cast<const unsigned char*>(buffer); | 47 const unsigned char* data = static_cast<const unsigned char*>(buffer); |
48 // Try PNG first. | 48 // Try PNG first. |
49 if (gfx::PNGCodec::Decode(data, size, bm)) | 49 if (gfx::PNGCodec::Decode(data, size, bm)) |
50 return true; | 50 return true; |
51 // Try JPEG. | 51 // Try JPEG. |
52 scoped_ptr<SkBitmap> decoded_jpeg(gfx::JPEGCodec::Decode(data, size)); | 52 std::unique_ptr<SkBitmap> decoded_jpeg(gfx::JPEGCodec::Decode(data, size)); |
53 if (decoded_jpeg) { | 53 if (decoded_jpeg) { |
54 *bm = *decoded_jpeg; | 54 *bm = *decoded_jpeg; |
55 return true; | 55 return true; |
56 } | 56 } |
57 return false; | 57 return false; |
58 } | 58 } |
59 | 59 |
60 scoped_ptr<base::Value> ParsePictureArg(v8::Isolate* isolate, | 60 std::unique_ptr<base::Value> ParsePictureArg(v8::Isolate* isolate, |
61 v8::Local<v8::Value> arg) { | 61 v8::Local<v8::Value> arg) { |
62 scoped_ptr<content::V8ValueConverter> converter( | 62 std::unique_ptr<content::V8ValueConverter> converter( |
63 content::V8ValueConverter::create()); | 63 content::V8ValueConverter::create()); |
64 return scoped_ptr<base::Value>( | 64 return std::unique_ptr<base::Value>( |
65 converter->FromV8Value(arg, isolate->GetCurrentContext())); | 65 converter->FromV8Value(arg, isolate->GetCurrentContext())); |
66 } | 66 } |
67 | 67 |
68 scoped_ptr<Picture> CreatePictureFromEncodedString(const std::string& encoded) { | 68 std::unique_ptr<Picture> CreatePictureFromEncodedString( |
| 69 const std::string& encoded) { |
69 std::string decoded; | 70 std::string decoded; |
70 base::Base64Decode(encoded, &decoded); | 71 base::Base64Decode(encoded, &decoded); |
71 SkMemoryStream stream(decoded.data(), decoded.size()); | 72 SkMemoryStream stream(decoded.data(), decoded.size()); |
72 | 73 |
73 sk_sp<SkPicture> skpicture = | 74 sk_sp<SkPicture> skpicture = |
74 SkPicture::MakeFromStream(&stream, &DecodeBitmap); | 75 SkPicture::MakeFromStream(&stream, &DecodeBitmap); |
75 if (!skpicture) | 76 if (!skpicture) |
76 return nullptr; | 77 return nullptr; |
77 | 78 |
78 scoped_ptr<Picture> picture(new Picture); | 79 std::unique_ptr<Picture> picture(new Picture); |
79 picture->layer_rect = gfx::SkIRectToRect(skpicture->cullRect().roundOut()); | 80 picture->layer_rect = gfx::SkIRectToRect(skpicture->cullRect().roundOut()); |
80 picture->picture = std::move(skpicture); | 81 picture->picture = std::move(skpicture); |
81 return picture; | 82 return picture; |
82 } | 83 } |
83 | 84 |
84 scoped_ptr<Picture> ParsePictureStr(v8::Isolate* isolate, | 85 std::unique_ptr<Picture> ParsePictureStr(v8::Isolate* isolate, |
85 v8::Local<v8::Value> arg) { | 86 v8::Local<v8::Value> arg) { |
86 scoped_ptr<base::Value> picture_value = ParsePictureArg(isolate, arg); | 87 std::unique_ptr<base::Value> picture_value = ParsePictureArg(isolate, arg); |
87 if (!picture_value) | 88 if (!picture_value) |
88 return nullptr; | 89 return nullptr; |
89 // Decode the picture from base64. | 90 // Decode the picture from base64. |
90 std::string encoded; | 91 std::string encoded; |
91 if (!picture_value->GetAsString(&encoded)) | 92 if (!picture_value->GetAsString(&encoded)) |
92 return nullptr; | 93 return nullptr; |
93 return CreatePictureFromEncodedString(encoded); | 94 return CreatePictureFromEncodedString(encoded); |
94 } | 95 } |
95 | 96 |
96 scoped_ptr<Picture> ParsePictureHash(v8::Isolate* isolate, | 97 std::unique_ptr<Picture> ParsePictureHash(v8::Isolate* isolate, |
97 v8::Local<v8::Value> arg) { | 98 v8::Local<v8::Value> arg) { |
98 scoped_ptr<base::Value> picture_value = ParsePictureArg(isolate, arg); | 99 std::unique_ptr<base::Value> picture_value = ParsePictureArg(isolate, arg); |
99 if (!picture_value) | 100 if (!picture_value) |
100 return nullptr; | 101 return nullptr; |
101 const base::DictionaryValue* value = nullptr; | 102 const base::DictionaryValue* value = nullptr; |
102 if (!picture_value->GetAsDictionary(&value)) | 103 if (!picture_value->GetAsDictionary(&value)) |
103 return nullptr; | 104 return nullptr; |
104 // Decode the picture from base64. | 105 // Decode the picture from base64. |
105 std::string encoded; | 106 std::string encoded; |
106 if (!value->GetString("skp64", &encoded)) | 107 if (!value->GetString("skp64", &encoded)) |
107 return nullptr; | 108 return nullptr; |
108 return CreatePictureFromEncodedString(encoded); | 109 return CreatePictureFromEncodedString(encoded); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 .SetMethod("getOpTimings", &SkiaBenchmarking::GetOpTimings) | 173 .SetMethod("getOpTimings", &SkiaBenchmarking::GetOpTimings) |
173 .SetMethod("getInfo", &SkiaBenchmarking::GetInfo); | 174 .SetMethod("getInfo", &SkiaBenchmarking::GetInfo); |
174 } | 175 } |
175 | 176 |
176 void SkiaBenchmarking::Rasterize(gin::Arguments* args) { | 177 void SkiaBenchmarking::Rasterize(gin::Arguments* args) { |
177 v8::Isolate* isolate = args->isolate(); | 178 v8::Isolate* isolate = args->isolate(); |
178 if (args->PeekNext().IsEmpty()) | 179 if (args->PeekNext().IsEmpty()) |
179 return; | 180 return; |
180 v8::Local<v8::Value> picture_handle; | 181 v8::Local<v8::Value> picture_handle; |
181 args->GetNext(&picture_handle); | 182 args->GetNext(&picture_handle); |
182 scoped_ptr<Picture> picture = ParsePictureHash(isolate, picture_handle); | 183 std::unique_ptr<Picture> picture = ParsePictureHash(isolate, picture_handle); |
183 if (!picture.get()) | 184 if (!picture.get()) |
184 return; | 185 return; |
185 | 186 |
186 double scale = 1.0; | 187 double scale = 1.0; |
187 gfx::Rect clip_rect(picture->layer_rect); | 188 gfx::Rect clip_rect(picture->layer_rect); |
188 int stop_index = -1; | 189 int stop_index = -1; |
189 bool overdraw = false; | 190 bool overdraw = false; |
190 | 191 |
191 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 192 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
192 if (!args->PeekNext().IsEmpty()) { | 193 if (!args->PeekNext().IsEmpty()) { |
193 v8::Local<v8::Value> params; | 194 v8::Local<v8::Value> params; |
194 args->GetNext(¶ms); | 195 args->GetNext(¶ms); |
195 scoped_ptr<content::V8ValueConverter> converter( | 196 std::unique_ptr<content::V8ValueConverter> converter( |
196 content::V8ValueConverter::create()); | 197 content::V8ValueConverter::create()); |
197 scoped_ptr<base::Value> params_value( | 198 std::unique_ptr<base::Value> params_value( |
198 converter->FromV8Value(params, context)); | 199 converter->FromV8Value(params, context)); |
199 | 200 |
200 const base::DictionaryValue* params_dict = NULL; | 201 const base::DictionaryValue* params_dict = NULL; |
201 if (params_value.get() && params_value->GetAsDictionary(¶ms_dict)) { | 202 if (params_value.get() && params_value->GetAsDictionary(¶ms_dict)) { |
202 params_dict->GetDouble("scale", &scale); | 203 params_dict->GetDouble("scale", &scale); |
203 params_dict->GetInteger("stop", &stop_index); | 204 params_dict->GetInteger("stop", &stop_index); |
204 params_dict->GetBoolean("overdraw", &overdraw); | 205 params_dict->GetBoolean("overdraw", &overdraw); |
205 | 206 |
206 const base::Value* clip_value = NULL; | 207 const base::Value* clip_value = NULL; |
207 if (params_dict->Get("clip", &clip_value)) | 208 if (params_dict->Get("clip", &clip_value)) |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 | 257 |
257 args->Return(result); | 258 args->Return(result); |
258 } | 259 } |
259 | 260 |
260 void SkiaBenchmarking::GetOps(gin::Arguments* args) { | 261 void SkiaBenchmarking::GetOps(gin::Arguments* args) { |
261 v8::Isolate* isolate = args->isolate(); | 262 v8::Isolate* isolate = args->isolate(); |
262 if (args->PeekNext().IsEmpty()) | 263 if (args->PeekNext().IsEmpty()) |
263 return; | 264 return; |
264 v8::Local<v8::Value> picture_handle; | 265 v8::Local<v8::Value> picture_handle; |
265 args->GetNext(&picture_handle); | 266 args->GetNext(&picture_handle); |
266 scoped_ptr<Picture> picture = ParsePictureHash(isolate, picture_handle); | 267 std::unique_ptr<Picture> picture = ParsePictureHash(isolate, picture_handle); |
267 if (!picture.get()) | 268 if (!picture.get()) |
268 return; | 269 return; |
269 | 270 |
270 SkCanvas canvas(picture->layer_rect.width(), picture->layer_rect.height()); | 271 SkCanvas canvas(picture->layer_rect.width(), picture->layer_rect.height()); |
271 skia::BenchmarkingCanvas benchmarking_canvas(&canvas); | 272 skia::BenchmarkingCanvas benchmarking_canvas(&canvas); |
272 picture->picture->playback(&benchmarking_canvas); | 273 picture->picture->playback(&benchmarking_canvas); |
273 | 274 |
274 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 275 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
275 scoped_ptr<content::V8ValueConverter> converter( | 276 std::unique_ptr<content::V8ValueConverter> converter( |
276 content::V8ValueConverter::create()); | 277 content::V8ValueConverter::create()); |
277 | 278 |
278 args->Return(converter->ToV8Value(&benchmarking_canvas.Commands(), context)); | 279 args->Return(converter->ToV8Value(&benchmarking_canvas.Commands(), context)); |
279 } | 280 } |
280 | 281 |
281 void SkiaBenchmarking::GetOpTimings(gin::Arguments* args) { | 282 void SkiaBenchmarking::GetOpTimings(gin::Arguments* args) { |
282 v8::Isolate* isolate = args->isolate(); | 283 v8::Isolate* isolate = args->isolate(); |
283 if (args->PeekNext().IsEmpty()) | 284 if (args->PeekNext().IsEmpty()) |
284 return; | 285 return; |
285 v8::Local<v8::Value> picture_handle; | 286 v8::Local<v8::Value> picture_handle; |
286 args->GetNext(&picture_handle); | 287 args->GetNext(&picture_handle); |
287 scoped_ptr<Picture> picture = ParsePictureHash(isolate, picture_handle); | 288 std::unique_ptr<Picture> picture = ParsePictureHash(isolate, picture_handle); |
288 if (!picture.get()) | 289 if (!picture.get()) |
289 return; | 290 return; |
290 | 291 |
291 gfx::Rect bounds = picture->layer_rect; | 292 gfx::Rect bounds = picture->layer_rect; |
292 | 293 |
293 // Measure the total time by drawing straight into a bitmap-backed canvas. | 294 // Measure the total time by drawing straight into a bitmap-backed canvas. |
294 SkBitmap bitmap; | 295 SkBitmap bitmap; |
295 bitmap.allocN32Pixels(bounds.width(), bounds.height()); | 296 bitmap.allocN32Pixels(bounds.width(), bounds.height()); |
296 SkCanvas bitmap_canvas(bitmap); | 297 SkCanvas bitmap_canvas(bitmap); |
297 bitmap_canvas.clear(SK_ColorTRANSPARENT); | 298 bitmap_canvas.clear(SK_ColorTRANSPARENT); |
(...skipping 20 matching lines...) Expand all Loading... |
318 | 319 |
319 args->Return(result); | 320 args->Return(result); |
320 } | 321 } |
321 | 322 |
322 void SkiaBenchmarking::GetInfo(gin::Arguments* args) { | 323 void SkiaBenchmarking::GetInfo(gin::Arguments* args) { |
323 v8::Isolate* isolate = args->isolate(); | 324 v8::Isolate* isolate = args->isolate(); |
324 if (args->PeekNext().IsEmpty()) | 325 if (args->PeekNext().IsEmpty()) |
325 return; | 326 return; |
326 v8::Local<v8::Value> picture_handle; | 327 v8::Local<v8::Value> picture_handle; |
327 args->GetNext(&picture_handle); | 328 args->GetNext(&picture_handle); |
328 scoped_ptr<Picture> picture = ParsePictureStr(isolate, picture_handle); | 329 std::unique_ptr<Picture> picture = ParsePictureStr(isolate, picture_handle); |
329 if (!picture.get()) | 330 if (!picture.get()) |
330 return; | 331 return; |
331 | 332 |
332 v8::Local<v8::Object> result = v8::Object::New(isolate); | 333 v8::Local<v8::Object> result = v8::Object::New(isolate); |
333 result->Set(v8::String::NewFromUtf8(isolate, "width"), | 334 result->Set(v8::String::NewFromUtf8(isolate, "width"), |
334 v8::Number::New(isolate, picture->layer_rect.width())); | 335 v8::Number::New(isolate, picture->layer_rect.width())); |
335 result->Set(v8::String::NewFromUtf8(isolate, "height"), | 336 result->Set(v8::String::NewFromUtf8(isolate, "height"), |
336 v8::Number::New(isolate, picture->layer_rect.height())); | 337 v8::Number::New(isolate, picture->layer_rect.height())); |
337 | 338 |
338 args->Return(result); | 339 args->Return(result); |
339 } | 340 } |
340 | 341 |
341 } // namespace content | 342 } // namespace content |
OLD | NEW |