Commit 2453e479 authored by Yuri Wiitala's avatar Yuri Wiitala Committed by Commit Bot

Move grayscale conversion into NavigationEntryScreenshotManager.

Changes the location of the RGB→Grayscale conversion of the
CopyFromSurface() result SkBitmap to be in
NavigationEntryScreenshotManager, as it is the only use case. This
unblocks a massive clean-up effort for bug 759310.

For reference, the following was the code that was doing the conversion
before (and is a part of a major chunk of code that we want to delete):
https://cs.chromium.org/chromium/src/content/browser/compositor/surface_utils.cc?rcl=2fe7073e23caa42cbd01575ab206ba8928ccd645&l=146

Bug: 759310
Change-Id: I2687e69b284d5c24ead10b05b49f678cf8375651
Reviewed-on: https://chromium-review.googlesource.com/919603
Commit-Queue: Yuri Wiitala <miu@chromium.org>
Reviewed-by: default avatarNick Carter <nick@chromium.org>
Cr-Commit-Position: refs/heads/master@{#536929}
parent 4ccaa77a
...@@ -14,6 +14,10 @@ ...@@ -14,6 +14,10 @@
#include "content/public/browser/render_widget_host.h" #include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h" #include "content/public/browser/render_widget_host_view.h"
#include "content/public/common/content_switches.h" #include "content/public/common/content_switches.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColorFilter.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/effects/SkLumaColorFilter.h"
#include "ui/gfx/codec/png_codec.h" #include "ui/gfx/codec/png_codec.h"
namespace { namespace {
...@@ -31,11 +35,11 @@ class ScreenshotData : public base::RefCountedThreadSafe<ScreenshotData> { ...@@ -31,11 +35,11 @@ class ScreenshotData : public base::RefCountedThreadSafe<ScreenshotData> {
ScreenshotData() { ScreenshotData() {
} }
void EncodeScreenshot(const SkBitmap& bitmap, base::Closure callback) { void EncodeScreenshot(const SkBitmap& bitmap, base::OnceClosure callback) {
base::PostTaskWithTraitsAndReply( base::PostTaskWithTraitsAndReply(
FROM_HERE, {base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, FROM_HERE, {base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce(&ScreenshotData::EncodeOnWorker, this, bitmap), base::BindOnce(&ScreenshotData::EncodeOnWorker, this, bitmap),
callback); std::move(callback));
} }
scoped_refptr<base::RefCountedBytes> data() const { return data_; } scoped_refptr<base::RefCountedBytes> data() const { return data_; }
...@@ -46,10 +50,22 @@ class ScreenshotData : public base::RefCountedThreadSafe<ScreenshotData> { ...@@ -46,10 +50,22 @@ class ScreenshotData : public base::RefCountedThreadSafe<ScreenshotData> {
} }
void EncodeOnWorker(const SkBitmap& bitmap) { void EncodeOnWorker(const SkBitmap& bitmap) {
DCHECK_EQ(bitmap.colorType(), kAlpha_8_SkColorType); // Convert |bitmap| to alpha-only |grayscale_bitmap|.
SkBitmap grayscale_bitmap;
if (grayscale_bitmap.tryAllocPixels(
SkImageInfo::MakeA8(bitmap.width(), bitmap.height()))) {
SkCanvas canvas(grayscale_bitmap);
SkPaint paint;
paint.setColorFilter(SkLumaColorFilter::Make());
canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint);
canvas.flush();
}
if (!grayscale_bitmap.readyToDraw())
return;
// Encode the A8 bitmap to grayscale PNG treating alpha as color intensity. // Encode the A8 bitmap to grayscale PNG treating alpha as color intensity.
std::vector<unsigned char> data; std::vector<unsigned char> data;
if (gfx::PNGCodec::EncodeA8SkBitmap(bitmap, &data)) if (gfx::PNGCodec::EncodeA8SkBitmap(grayscale_bitmap, &data))
data_ = new base::RefCountedBytes(data); data_ = new base::RefCountedBytes(data);
} }
...@@ -105,9 +121,12 @@ void NavigationEntryScreenshotManager::TakeScreenshot() { ...@@ -105,9 +121,12 @@ void NavigationEntryScreenshotManager::TakeScreenshot() {
const gfx::Size view_size_on_screen = view->GetViewBounds().size(); const gfx::Size view_size_on_screen = view->GetViewBounds().size();
view->CopyFromSurface( view->CopyFromSurface(
gfx::Rect(), view_size_on_screen, gfx::Rect(), view_size_on_screen,
base::Bind(&NavigationEntryScreenshotManager::OnScreenshotTaken, // TODO(crbug/759310): This should be BindOnce, but requires a public
screenshot_factory_.GetWeakPtr(), entry->GetUniqueID()), // interface change.
kAlpha_8_SkColorType); base::BindRepeating(&NavigationEntryScreenshotManager::OnScreenshotTaken,
screenshot_factory_.GetWeakPtr(),
entry->GetUniqueID()),
kN32_SkColorType);
} }
// Implemented here and not in NavigationEntry because this manager keeps track // Implemented here and not in NavigationEntry because this manager keeps track
...@@ -144,11 +163,9 @@ void NavigationEntryScreenshotManager::OnScreenshotTaken( ...@@ -144,11 +163,9 @@ void NavigationEntryScreenshotManager::OnScreenshotTaken(
scoped_refptr<ScreenshotData> screenshot = new ScreenshotData(); scoped_refptr<ScreenshotData> screenshot = new ScreenshotData();
screenshot->EncodeScreenshot( screenshot->EncodeScreenshot(
bitmap, bitmap, base::BindOnce(
base::Bind(&NavigationEntryScreenshotManager::OnScreenshotEncodeComplete, &NavigationEntryScreenshotManager::OnScreenshotEncodeComplete,
screenshot_factory_.GetWeakPtr(), screenshot_factory_.GetWeakPtr(), unique_id, screenshot));
unique_id,
screenshot));
} }
int NavigationEntryScreenshotManager::GetScreenshotCount() const { int NavigationEntryScreenshotManager::GetScreenshotCount() const {
...@@ -168,7 +185,10 @@ void NavigationEntryScreenshotManager::OnScreenshotEncodeComplete( ...@@ -168,7 +185,10 @@ void NavigationEntryScreenshotManager::OnScreenshotEncodeComplete(
NavigationEntryImpl* entry = owner_->GetEntryWithUniqueID(unique_id); NavigationEntryImpl* entry = owner_->GetEntryWithUniqueID(unique_id);
if (!entry) if (!entry)
return; return;
entry->SetScreenshotPNGData(screenshot->data()); scoped_refptr<base::RefCountedBytes> data = screenshot->data();
if (!data)
return;
entry->SetScreenshotPNGData(std::move(data));
OnScreenshotSet(entry); OnScreenshotSet(entry);
} }
......
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