Commit a6d1cf90 authored by mfomitchev's avatar mfomitchev Committed by Commit bot

Improving GestureNav screenshotting performance - part 2 - GestureNav

Utilize the new gl_helper functionality to capture GestureNav screenshots
in grayscale, which drastically improves screencapture performance,
especially on hardware configurations with expensive RGBA readback.

Part 1: https://codereview.chromium.org/388953002/
Original review: https://codereview.chromium.org/384453002/

BUG=379983, 380779

Review URL: https://codereview.chromium.org/389683002

Cr-Commit-Position: refs/heads/master@{#291992}
parent a5c2c1cc
......@@ -19,6 +19,9 @@
#include "media/base/video_frame.h"
#include "media/base/video_util.h"
#include "skia/ext/image_operations.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/effects/SkLumaColorFilter.h"
#include "ui/gfx/frame_time.h"
namespace content {
......@@ -136,7 +139,8 @@ void DelegatedFrameHost::CopyFromCompositingSurface(
const base::Callback<void(bool, const SkBitmap&)>& callback,
const SkColorType color_type) {
// Only ARGB888 and RGB565 supported as of now.
bool format_support = ((color_type == kRGB_565_SkColorType) ||
bool format_support = ((color_type == kAlpha_8_SkColorType) ||
(color_type == kRGB_565_SkColorType) ||
(color_type == kN32_SkColorType));
DCHECK(format_support);
if (!CanCopyToBitmap()) {
......@@ -519,6 +523,7 @@ void DelegatedFrameHost::CopyFromCompositingSurfaceHasResult(
}
if (result->HasTexture()) {
// GPU-accelerated path
PrepareTextureCopyOutputResult(dst_size_in_pixel, color_type,
callback,
result.Pass());
......@@ -526,6 +531,7 @@ void DelegatedFrameHost::CopyFromCompositingSurfaceHasResult(
}
DCHECK(result->HasBitmap());
// Software path
PrepareBitmapCopyOutputResult(dst_size_in_pixel, color_type, callback,
result.Pass());
}
......@@ -604,7 +610,7 @@ void DelegatedFrameHost::PrepareBitmapCopyOutputResult(
const SkColorType color_type,
const base::Callback<void(bool, const SkBitmap&)>& callback,
scoped_ptr<cc::CopyOutputResult> result) {
if (color_type != kN32_SkColorType) {
if (color_type != kN32_SkColorType && color_type != kAlpha_8_SkColorType) {
NOTIMPLEMENTED();
callback.Run(false, SkBitmap());
return;
......@@ -612,12 +618,41 @@ void DelegatedFrameHost::PrepareBitmapCopyOutputResult(
DCHECK(result->HasBitmap());
scoped_ptr<SkBitmap> source = result->TakeBitmap();
DCHECK(source);
SkBitmap bitmap = skia::ImageOperations::Resize(
*source,
skia::ImageOperations::RESIZE_BEST,
dst_size_in_pixel.width(),
dst_size_in_pixel.height());
callback.Run(true, bitmap);
SkBitmap scaled_bitmap;
if (source->width() != dst_size_in_pixel.width() ||
source->height() != dst_size_in_pixel.height()) {
scaled_bitmap =
skia::ImageOperations::Resize(*source,
skia::ImageOperations::RESIZE_BEST,
dst_size_in_pixel.width(),
dst_size_in_pixel.height());
} else {
scaled_bitmap = *source;
}
if (color_type == kN32_SkColorType) {
DCHECK_EQ(scaled_bitmap.colorType(), kN32_SkColorType);
callback.Run(true, scaled_bitmap);
return;
}
DCHECK_EQ(color_type, kAlpha_8_SkColorType);
// The software path currently always returns N32 bitmap regardless of the
// |color_type| we ask for.
DCHECK_EQ(scaled_bitmap.colorType(), kN32_SkColorType);
// Paint |scaledBitmap| to alpha-only |grayscale_bitmap|.
SkBitmap grayscale_bitmap;
bool success = grayscale_bitmap.allocPixels(
SkImageInfo::MakeA8(scaled_bitmap.width(), scaled_bitmap.height()));
if (!success) {
callback.Run(false, SkBitmap());
return;
}
SkCanvas canvas(grayscale_bitmap);
SkPaint paint;
skia::RefPtr<SkColorFilter> filter =
skia::AdoptRef(SkLumaColorFilter::Create());
paint.setColorFilter(filter.get());
canvas.drawBitmap(scaled_bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint);
callback.Run(true, grayscale_bitmap);
}
// static
......@@ -636,6 +671,7 @@ void DelegatedFrameHost::ReturnSubscriberTexture(
dfh->idle_frame_subscriber_textures_.push_back(subscriber_texture);
}
// static
void DelegatedFrameHost::CopyFromCompositingSurfaceFinishedForVideo(
base::WeakPtr<DelegatedFrameHost> dfh,
const base::Callback<void(bool)>& callback,
......
......@@ -79,7 +79,8 @@ class MockScreenshotManager : public content::NavigationEntryScreenshotManager {
void TakeScreenshotFor(content::NavigationEntryImpl* entry) {
SkBitmap bitmap;
bitmap.allocN32Pixels(1, 1);
bitmap.allocPixels(SkImageInfo::Make(
1, 1, kAlpha_8_SkColorType, kPremul_SkAlphaType));
bitmap.eraseARGB(0, 0, 0, 0);
encoding_screenshot_in_progress_ = true;
OnScreenshotTaken(entry->GetUniqueID(), true, bitmap);
......
......@@ -13,9 +13,6 @@
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/common/content_switches.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/effects/SkLumaColorFilter.h"
#include "ui/gfx/codec/png_codec.h"
namespace {
......@@ -27,7 +24,7 @@ const int kMinScreenshotIntervalMS = 1000;
namespace content {
// Converts SkBitmap to grayscale and encodes to PNG data in a worker thread.
// Encodes the A8 SkBitmap to grayscale PNG in a worker thread.
class ScreenshotData : public base::RefCountedThreadSafe<ScreenshotData> {
public:
ScreenshotData() {
......@@ -52,18 +49,10 @@ class ScreenshotData : public base::RefCountedThreadSafe<ScreenshotData> {
}
void EncodeOnWorker(const SkBitmap& bitmap) {
DCHECK_EQ(bitmap.colorType(), kAlpha_8_SkColorType);
// Encode the A8 bitmap to grayscale PNG treating alpha as color intensity.
std::vector<unsigned char> data;
// Paint |bitmap| to a kAlpha_8_SkColorType SkBitmap
SkBitmap a8Bitmap;
a8Bitmap.allocPixels(SkImageInfo::MakeA8(bitmap.width(), bitmap.height()));
SkCanvas canvas(a8Bitmap);
SkPaint paint;
SkColorFilter* filter = SkLumaColorFilter::Create();
paint.setColorFilter(filter);
filter->unref();
canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint);
// Encode the a8Bitmap to grayscale PNG treating alpha as color intensity
if (gfx::PNGCodec::EncodeA8SkBitmap(a8Bitmap, &data))
if (gfx::PNGCodec::EncodeA8SkBitmap(bitmap, &data))
data_ = new base::RefCountedBytes(data);
}
......@@ -130,14 +119,13 @@ void NavigationEntryScreenshotManager::TakeScreenshotImpl(
NavigationEntryImpl* entry) {
DCHECK(host && host->GetView());
DCHECK(entry);
SkColorType preferred_format = host->PreferredReadbackFormat();
host->CopyFromBackingStore(
gfx::Rect(),
host->GetView()->GetViewBounds().size(),
base::Bind(&NavigationEntryScreenshotManager::OnScreenshotTaken,
screenshot_factory_.GetWeakPtr(),
entry->GetUniqueID()),
preferred_format);
kAlpha_8_SkColorType);
}
void NavigationEntryScreenshotManager::SetMinScreenshotIntervalMS(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment