Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: ui/snapshot/snapshot_async.cc

Issue 2592983002: [devtools] Support different encodings for Page.CaptureScreenshot. (Closed)
Patch Set: Encode in ui snapshot methods instead. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "ui/snapshot/snapshot_async.h" 5 #include "ui/snapshot/snapshot_async.h"
6 6
7 #include "base/location.h" 7 #include "base/location.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/numerics/safe_conversions.h" 9 #include "base/numerics/safe_conversions.h"
10 #include "base/task_runner_util.h" 10 #include "base/task_runner_util.h"
11 #include "skia/ext/image_operations.h" 11 #include "skia/ext/image_operations.h"
12 #include "third_party/skia/include/core/SkBitmap.h" 12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "third_party/skia/include/core/SkPixelRef.h" 13 #include "third_party/skia/include/core/SkPixelRef.h"
14 #include "ui/gfx/codec/jpeg_codec.h"
14 #include "ui/gfx/codec/png_codec.h" 15 #include "ui/gfx/codec/png_codec.h"
15 #include "ui/gfx/image/image.h" 16 #include "ui/gfx/image/image.h"
16 #include "ui/gfx/image/image_skia.h" 17 #include "ui/gfx/image/image_skia.h"
17 #include "ui/gfx/skbitmap_operations.h" 18 #include "ui/gfx/skbitmap_operations.h"
18 19
19 namespace ui { 20 namespace ui {
20 21
21 namespace { 22 namespace {
22 23
23 void OnFrameScalingFinished(const GrabWindowSnapshotAsyncCallback& callback, 24 void OnFrameScalingFinished(const GrabWindowSnapshotAsyncCallback& callback,
24 const SkBitmap& scaled_bitmap) { 25 const SkBitmap& scaled_bitmap) {
25 callback.Run(gfx::Image(gfx::ImageSkia::CreateFrom1xBitmap(scaled_bitmap))); 26 callback.Run(gfx::Image(gfx::ImageSkia::CreateFrom1xBitmap(scaled_bitmap)));
26 } 27 }
27 28
28 SkBitmap ScaleBitmap(const SkBitmap& input_bitmap, 29 SkBitmap ScaleBitmap(const SkBitmap& input_bitmap,
29 const gfx::Size& target_size) { 30 const gfx::Size& target_size) {
30 return skia::ImageOperations::Resize(input_bitmap, 31 return skia::ImageOperations::Resize(input_bitmap,
31 skia::ImageOperations::RESIZE_GOOD, 32 skia::ImageOperations::RESIZE_GOOD,
32 target_size.width(), 33 target_size.width(),
33 target_size.height(), 34 target_size.height(),
34 static_cast<SkBitmap::Allocator*>(NULL)); 35 static_cast<SkBitmap::Allocator*>(NULL));
35 } 36 }
36 37
37 scoped_refptr<base::RefCountedBytes> EncodeBitmap(const SkBitmap& bitmap) { 38 scoped_refptr<base::RefCountedBytes> EncodeBitmap(const SkBitmap& bitmap,
38 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); 39 SnapshotEncoding encoding,
40 SnapshotQuality quality) {
41 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes);
39 SkAutoLockPixels lock(bitmap); 42 SkAutoLockPixels lock(bitmap);
40 unsigned char* pixels = reinterpret_cast<unsigned char*>(bitmap.getPixels()); 43 unsigned char* pixels = reinterpret_cast<unsigned char*>(bitmap.getPixels());
41 #if SK_A32_SHIFT == 24 && SK_R32_SHIFT == 16 && SK_G32_SHIFT == 8 44 bool encoded = false;
42 gfx::PNGCodec::ColorFormat kColorFormat = gfx::PNGCodec::FORMAT_BGRA; 45 switch (encoding) {
43 #elif SK_A32_SHIFT == 24 && SK_B32_SHIFT == 16 && SK_G32_SHIFT == 8 46 case SnapshotEncoding::PNG:
44 gfx::PNGCodec::ColorFormat kColorFormat = gfx::PNGCodec::FORMAT_RGBA; 47 encoded = gfx::PNGCodec::Encode(
45 #else 48 pixels, gfx::PNGCodec::FORMAT_SkBitmap,
46 #error Unknown color format 49 gfx::Size(bitmap.width(), bitmap.height()),
47 #endif 50 base::checked_cast<int>(bitmap.rowBytes()), true,
48 if (!gfx::PNGCodec::Encode(pixels, 51 std::vector<gfx::PNGCodec::Comment>(), &data->data());
49 kColorFormat, 52 break;
50 gfx::Size(bitmap.width(), bitmap.height()), 53 case SnapshotEncoding::JPEG:
51 base::checked_cast<int>(bitmap.rowBytes()), 54 encoded = gfx::JPEGCodec::Encode(
52 true, 55 pixels, gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(),
53 std::vector<gfx::PNGCodec::Comment>(), 56 bitmap.height(), base::checked_cast<int>(bitmap.rowBytes()), quality,
54 &png_data->data())) { 57 &data->data());
58 break;
59 default:
60 NOTREACHED();
61 }
62 if (!encoded)
55 return scoped_refptr<base::RefCountedBytes>(); 63 return scoped_refptr<base::RefCountedBytes>();
56 } 64 return data;
57 return png_data;
58 } 65 }
59 66
60 } // namespace 67 } // namespace
61 68
62 void SnapshotAsync::ScaleCopyOutputResult( 69 void SnapshotAsync::ScaleCopyOutputResult(
63 const GrabWindowSnapshotAsyncCallback& callback, 70 const GrabWindowSnapshotAsyncCallback& callback,
64 const gfx::Size& target_size, 71 const gfx::Size& target_size,
65 scoped_refptr<base::TaskRunner> background_task_runner, 72 scoped_refptr<base::TaskRunner> background_task_runner,
66 std::unique_ptr<cc::CopyOutputResult> result) { 73 std::unique_ptr<cc::CopyOutputResult> result) {
67 if (result->IsEmpty()) { 74 if (result->IsEmpty()) {
68 callback.Run(gfx::Image()); 75 callback.Run(gfx::Image());
69 return; 76 return;
70 } 77 }
71 78
72 // TODO(sergeyu): Potentially images can be scaled on GPU before reading it 79 // TODO(sergeyu): Potentially images can be scaled on GPU before reading it
73 // from GPU. Image scaling is implemented in content::GlHelper, but it's can't 80 // from GPU. Image scaling is implemented in content::GlHelper, but it's can't
74 // be used here because it's not in content/public. Move the scaling code 81 // be used here because it's not in content/public. Move the scaling code
75 // somewhere so that it can be reused here. 82 // somewhere so that it can be reused here.
76 base::PostTaskAndReplyWithResult( 83 base::PostTaskAndReplyWithResult(
77 background_task_runner.get(), 84 background_task_runner.get(),
78 FROM_HERE, 85 FROM_HERE,
79 base::Bind(ScaleBitmap, *result->TakeBitmap(), target_size), 86 base::Bind(ScaleBitmap, *result->TakeBitmap(), target_size),
80 base::Bind(&OnFrameScalingFinished, callback)); 87 base::Bind(&OnFrameScalingFinished, callback));
81 } 88 }
82 89
83 void SnapshotAsync::EncodeCopyOutputResult( 90 void SnapshotAsync::EncodeCopyOutputResult(
84 const GrabWindowSnapshotAsyncPNGCallback& callback, 91 const GrabWindowSnapshotAsyncEncodedCallback& callback,
85 scoped_refptr<base::TaskRunner> background_task_runner, 92 scoped_refptr<base::TaskRunner> background_task_runner,
93 SnapshotEncoding encoding,
94 SnapshotQuality quality,
86 std::unique_ptr<cc::CopyOutputResult> result) { 95 std::unique_ptr<cc::CopyOutputResult> result) {
87 if (result->IsEmpty()) { 96 if (result->IsEmpty()) {
88 callback.Run(scoped_refptr<base::RefCountedBytes>()); 97 callback.Run(scoped_refptr<base::RefCountedBytes>());
89 return; 98 return;
90 } 99 }
91 100
92 // TODO(sergeyu): Potentially images can be scaled on GPU before reading it 101 // TODO(sergeyu): Potentially images can be scaled on GPU before reading it
93 // from GPU. Image scaling is implemented in content::GlHelper, but it's can't 102 // from GPU. Image scaling is implemented in content::GlHelper, but it's can't
94 // be used here because it's not in content/public. Move the scaling code 103 // be used here because it's not in content/public. Move the scaling code
95 // somewhere so that it can be reused here. 104 // somewhere so that it can be reused here.
96 base::PostTaskAndReplyWithResult( 105 base::PostTaskAndReplyWithResult(
97 background_task_runner.get(), 106 background_task_runner.get(), FROM_HERE,
98 FROM_HERE, 107 base::Bind(EncodeBitmap, *result->TakeBitmap(), encoding, quality),
99 base::Bind(EncodeBitmap, *result->TakeBitmap()),
100 callback); 108 callback);
101 } 109 }
102 110
103 } // namespace ui 111 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698