Skip to content

Commit

Permalink
update functionalities to snapshot.cc.
Browse files Browse the repository at this point in the history
1. Accelerate GrabWindowSnapshotAsyncPNG by
   using gfx::PNGCodec::FastEncodeBGRASkBitmap,
   which halves down the encoding time
2. Add GrabWindowSnapshotAsyncJPEG for scenarios
   that require low latency since JPEG encoder
   typically is 5x faster than PNG encoder.

BUG=b/37167971
TEST=local benchmark GrabWindowSnapshotAsyncPNG
  indicates 2x speedup; GrabWindowSnapshotAsyncJPEG
  works.

Review-Url: https://codereview.chromium.org/2815913007
Cr-Commit-Position: refs/heads/master@{#465132}
  • Loading branch information
muyuanli authored and Commit bot committed Apr 18, 2017
1 parent 341a827 commit 1c304d5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
41 changes: 35 additions & 6 deletions ui/snapshot/snapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,40 @@
#include "base/bind.h"
#include "base/callback.h"
#include "base/task_runner_util.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_util.h"

namespace ui {

namespace {

scoped_refptr<base::RefCountedMemory> EncodeImage(const gfx::Image& image) {
return image.As1xPNGBytes();
scoped_refptr<base::RefCountedMemory> EncodeImageAsPNG(
const gfx::Image& image) {
std::vector<uint8_t> result;
DCHECK(!image.AsImageSkia().GetRepresentation(1.0f).is_null());
gfx::PNGCodec::FastEncodeBGRASkBitmap(image.AsBitmap(), true, &result);
return new base::RefCountedBytes(result);
}

scoped_refptr<base::RefCountedMemory> EncodeImageAsJPEG(
const gfx::Image& image) {
std::vector<uint8_t> result;
DCHECK(!image.AsImageSkia().GetRepresentation(1.0f).is_null());
gfx::JPEG1xEncodedDataFromImage(image, 100, &result);
return new base::RefCountedBytes(result);
}

void EncodeImageAndSchedulePNGCallback(
void EncodeImageAndScheduleCallback(
scoped_refptr<base::RefCountedMemory> (*encode_func)(const gfx::Image&),
scoped_refptr<base::TaskRunner> background_task_runner,
const GrabWindowSnapshotAsyncPNGCallback& callback,
const base::Callback<void(scoped_refptr<base::RefCountedMemory> data)>&
callback,
const gfx::Image& image) {
base::PostTaskAndReplyWithResult(background_task_runner.get(), FROM_HERE,
base::Bind(EncodeImage, image), callback);
base::Bind(encode_func, image), callback);
}

} // namespace
Expand All @@ -34,7 +52,18 @@ void GrabWindowSnapshotAsyncPNG(
const GrabWindowSnapshotAsyncPNGCallback& callback) {
GrabWindowSnapshotAsync(
window, source_rect,
base::Bind(EncodeImageAndSchedulePNGCallback,
base::Bind(EncodeImageAndScheduleCallback, &EncodeImageAsPNG,
std::move(background_task_runner), callback));
}

void GrabWindowSnapshotAsyncJPEG(
gfx::NativeWindow window,
const gfx::Rect& source_rect,
scoped_refptr<base::TaskRunner> background_task_runner,
const GrabWindowSnapshotAsyncJPEGCallback& callback) {
GrabWindowSnapshotAsync(
window, source_rect,
base::Bind(EncodeImageAndScheduleCallback, &EncodeImageAsJPEG,
std::move(background_task_runner), callback));
}

Expand Down
12 changes: 10 additions & 2 deletions ui/snapshot/snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,22 @@ SNAPSHOT_EXPORT void GrabViewSnapshotAsync(
const gfx::Rect& source_rect,
const GrabWindowSnapshotAsyncCallback& callback);

typedef base::Callback<void(scoped_refptr<base::RefCountedMemory> png_data)>
GrabWindowSnapshotAsyncPNGCallback;
using GrabWindowSnapshotAsyncPNGCallback =
base::Callback<void(scoped_refptr<base::RefCountedMemory> data)>;
SNAPSHOT_EXPORT void GrabWindowSnapshotAsyncPNG(
gfx::NativeWindow window,
const gfx::Rect& source_rect,
scoped_refptr<base::TaskRunner> background_task_runner,
const GrabWindowSnapshotAsyncPNGCallback& callback);

using GrabWindowSnapshotAsyncJPEGCallback =
base::Callback<void(scoped_refptr<base::RefCountedMemory> data)>;
SNAPSHOT_EXPORT void GrabWindowSnapshotAsyncJPEG(
gfx::NativeWindow window,
const gfx::Rect& source_rect,
scoped_refptr<base::TaskRunner> background_task_runner,
const GrabWindowSnapshotAsyncJPEGCallback& callback);

} // namespace ui

#endif // UI_SNAPSHOT_SNAPSHOT_H_

0 comments on commit 1c304d5

Please sign in to comment.