Skip to content

Commit

Permalink
Introduce PlatformBitmap, which is a minimal helper class that wraps …
Browse files Browse the repository at this point in the history
…an SkBitmap

and a PlatformSurface. This is used to replace the PlatformCanvas that was being
passed to BackingStore to return pixels.

The problem to solve is that PlatformCanvas is an extension of SkCanvas, and
SkCanvas is losing the ability to have its backend specified after its
constructor (for performance reasons).

The BackingStore interface only needs to return a copy of its pixels, and offer
a platform-specific way to draw into it (i.e. BitBlt). The PlatformSurface is
sufficient for this, so the larger infrastructure of PlatformCanvas/PlatformDevice
is not required.
Review URL: https://codereview.chromium.org/11031055

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@161163 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
reed@google.com committed Oct 10, 2012
1 parent b27ccd4 commit 3b79834
Show file tree
Hide file tree
Showing 38 changed files with 216 additions and 99 deletions.
10 changes: 5 additions & 5 deletions chrome/browser/extensions/api/tabs/tabs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1684,23 +1684,23 @@ bool CaptureVisibleTabFunction::RunImpl() {
error_ = keys::kInternalVisibleTabCaptureError;
return false;
}
skia::PlatformCanvas* temp_canvas = new skia::PlatformCanvas;
skia::PlatformBitmap* temp_bitmap = new skia::PlatformBitmap;
render_view_host->CopyFromBackingStore(
gfx::Rect(),
view->GetViewBounds().size(),
base::Bind(&CaptureVisibleTabFunction::CopyFromBackingStoreComplete,
this,
base::Owned(temp_canvas)),
temp_canvas);
base::Owned(temp_bitmap)),
temp_bitmap);
return true;
}

void CaptureVisibleTabFunction::CopyFromBackingStoreComplete(
skia::PlatformCanvas* canvas,
skia::PlatformBitmap* bitmap,
bool succeeded) {
if (succeeded) {
VLOG(1) << "captureVisibleTab() got image from backing store.";
SendResultFromBitmap(skia::GetTopDevice(*canvas)->accessBitmap(false));
SendResultFromBitmap(bitmap->GetBitmap());
return;
}

Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/extensions/api/tabs/tabs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class WebContents;
}

namespace skia {
class PlatformCanvas;
class PlatformBitmap;
}

// Windows
Expand Down Expand Up @@ -192,7 +192,7 @@ class CaptureVisibleTabFunction : public AsyncExtensionFunction,
void SendResultFromBitmap(const SkBitmap& screen_capture);

private:
void CopyFromBackingStoreComplete(skia::PlatformCanvas* canvas,
void CopyFromBackingStoreComplete(skia::PlatformBitmap* bitmap,
bool succeeded);

content::NotificationRegistrar registrar_;
Expand Down
11 changes: 5 additions & 6 deletions chrome/browser/prerender/prerender_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,26 @@ class PrerenderTabHelper::PixelStats {
return;
}

skia::PlatformCanvas* temp_canvas = new skia::PlatformCanvas;
skia::PlatformBitmap* temp_bitmap = new skia::PlatformBitmap;
web_contents->GetRenderViewHost()->CopyFromBackingStore(
gfx::Rect(),
gfx::Size(),
base::Bind(&PrerenderTabHelper::PixelStats::HandleBitmapResult,
weak_factory_.GetWeakPtr(),
bitmap_type,
web_contents,
base::Owned(temp_canvas)),
temp_canvas);
base::Owned(temp_bitmap)),
temp_bitmap);
}

private:
void HandleBitmapResult(BitmapType bitmap_type,
WebContents* web_contents,
skia::PlatformCanvas* temp_canvas,
skia::PlatformBitmap* temp_bitmap,
bool succeeded) {
scoped_ptr<SkBitmap> bitmap;
if (succeeded) {
const SkBitmap& canvas_bitmap =
skia::GetTopDevice(*temp_canvas)->accessBitmap(false);
const SkBitmap& canvas_bitmap = temp_bitmap->GetBitmap();
bitmap.reset(new SkBitmap());
canvas_bitmap.copyTo(bitmap.get(), SkBitmap::kARGB_8888_Config);
}
Expand Down
10 changes: 5 additions & 5 deletions chrome/browser/thumbnails/thumbnail_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -406,16 +406,16 @@ void ThumbnailTabHelper::AsyncUpdateThumbnail(
gfx::Size(kThumbnailWidth, kThumbnailHeight),
&clip_result);
gfx::Size copy_size = GetCopySizeForThumbnail(view);
skia::PlatformCanvas* temp_canvas = new skia::PlatformCanvas;
skia::PlatformBitmap* temp_bitmap = new skia::PlatformBitmap;
render_widget_host->CopyFromBackingStore(
copy_rect,
copy_size,
base::Bind(&ThumbnailTabHelper::UpdateThumbnailWithCanvas,
weak_factory_.GetWeakPtr(),
web_contents,
clip_result,
base::Owned(temp_canvas)),
temp_canvas);
base::Owned(temp_bitmap)),
temp_bitmap);
}

void ThumbnailTabHelper::UpdateThumbnailWithBitmap(
Expand All @@ -435,13 +435,13 @@ void ThumbnailTabHelper::UpdateThumbnailWithBitmap(
void ThumbnailTabHelper::UpdateThumbnailWithCanvas(
WebContents* web_contents,
ClipResult clip_result,
skia::PlatformCanvas* temp_canvas,
skia::PlatformBitmap* temp_bitmap,
bool succeeded) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (!succeeded)
return;

SkBitmap bitmap = skia::GetTopDevice(*temp_canvas)->accessBitmap(false);
SkBitmap bitmap = temp_bitmap->GetBitmap();
UpdateThumbnailWithBitmap(web_contents, clip_result, bitmap);
}

Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/thumbnails/thumbnail_tab_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class TopSites;
}

namespace skia {
class PlatformCanvas;
class PlatformBitmap;
}

class ThumbnailTabHelper
Expand Down Expand Up @@ -120,7 +120,7 @@ class ThumbnailTabHelper
void UpdateThumbnailWithCanvas(
content::WebContents* web_contents,
ClipResult clip_result,
skia::PlatformCanvas* temp_canvas,
skia::PlatformBitmap* temp_bitmap,
bool result);

// Called when a render view host was created for a WebContents.
Expand Down
4 changes: 2 additions & 2 deletions content/browser/renderer_host/backing_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Rect;
}

namespace skia {
class PlatformCanvas;
class PlatformBitmap;
}

namespace content {
Expand Down Expand Up @@ -66,7 +66,7 @@ class CONTENT_EXPORT BackingStore {
// will call initialize() with the correct size. The return value indicates
// success.
virtual bool CopyFromBackingStore(const gfx::Rect& rect,
skia::PlatformCanvas* output) = 0;
skia::PlatformBitmap* output) = 0;

// Scrolls the contents of clip_rect in the backing store by dx or dy (but dx
// and dy cannot both be non-zero).
Expand Down
7 changes: 3 additions & 4 deletions content/browser/renderer_host/backing_store_aura.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,19 @@ void BackingStoreAura::ScrollBackingStore(int dx, int dy,
}

bool BackingStoreAura::CopyFromBackingStore(const gfx::Rect& rect,
skia::PlatformCanvas* output) {
skia::PlatformBitmap* output) {
const int width =
std::min(size().width(), rect.width()) * device_scale_factor_;
const int height =
std::min(size().height(), rect.height()) * device_scale_factor_;
if (!output->initialize(width, height, true))
if (!output->Allocate(width, height, true))
return false;

SkBitmap bitmap = skia::GetTopDevice(*output)->accessBitmap(true);
SkIRect skrect = SkIRect::MakeXYWH(rect.x(), rect.y(), width, height);
SkBitmap b;
if (!canvas_->readPixels(skrect, &b))
return false;
output->writePixels(b, rect.x(), rect.y());
SkCanvas(output->GetBitmap()).writePixels(b, rect.x(), rect.y());
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion content/browser/renderer_host/backing_store_aura.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class BackingStoreAura : public BackingStore {
const base::Closure& completion_callback,
bool* scheduled_completion_callback) OVERRIDE;
virtual bool CopyFromBackingStore(const gfx::Rect& rect,
skia::PlatformCanvas* output) OVERRIDE;
skia::PlatformBitmap* output) OVERRIDE;
virtual void ScrollBackingStore(int dx, int dy,
const gfx::Rect& clip_rect,
const gfx::Size& view_size) OVERRIDE;
Expand Down
6 changes: 3 additions & 3 deletions content/browser/renderer_host/backing_store_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ void BackingStoreGtk::PaintToBackingStore(
}

bool BackingStoreGtk::CopyFromBackingStore(const gfx::Rect& rect,
skia::PlatformCanvas* output) {
skia::PlatformBitmap* output) {
base::TimeTicks begin_time = base::TimeTicks::Now();

if (visual_depth_ < 24) {
Expand Down Expand Up @@ -571,7 +571,7 @@ bool BackingStoreGtk::CopyFromBackingStore(const gfx::Rect& rect,
// TODO(jhawkins): Need to convert the image data if the image bits per pixel
// is not 32.
// Note that this also initializes the output bitmap as opaque.
if (!output->initialize(width, height, true) ||
if (!output->Allocate(width, height, true) ||
image->bits_per_pixel != 32) {
if (shared_memory_support_ != ui::SHARED_MEMORY_NONE)
DestroySharedImage(display_, image, &shminfo);
Expand All @@ -584,7 +584,7 @@ bool BackingStoreGtk::CopyFromBackingStore(const gfx::Rect& rect,
// it and copy each row out, only up to the pixels we're actually
// using. This code assumes a visual mode where a pixel is
// represented using a 32-bit unsigned int, with a byte per component.
SkBitmap bitmap = skia::GetTopDevice(*output)->accessBitmap(true);
const SkBitmap& bitmap = output->GetBitmap();
SkAutoLockPixels alp(bitmap);

for (int y = 0; y < height; y++) {
Expand Down
2 changes: 1 addition & 1 deletion content/browser/renderer_host/backing_store_gtk.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class CONTENT_EXPORT BackingStoreGtk : public BackingStore {
const base::Closure& completion_callback,
bool* scheduled_completion_callback) OVERRIDE;
virtual bool CopyFromBackingStore(const gfx::Rect& rect,
skia::PlatformCanvas* output) OVERRIDE;
skia::PlatformBitmap* output) OVERRIDE;
virtual void ScrollBackingStore(int dx, int dy,
const gfx::Rect& clip_rect,
const gfx::Size& view_size) OVERRIDE;
Expand Down
2 changes: 1 addition & 1 deletion content/browser/renderer_host/backing_store_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class BackingStoreMac : public BackingStore {
const base::Closure& completion_callback,
bool* scheduled_completion_callback) OVERRIDE;
virtual bool CopyFromBackingStore(const gfx::Rect& rect,
skia::PlatformCanvas* output) OVERRIDE;
skia::PlatformBitmap* output) OVERRIDE;
virtual void ScrollBackingStore(int dx, int dy,
const gfx::Rect& clip_rect,
const gfx::Size& view_size) OVERRIDE;
Expand Down
7 changes: 3 additions & 4 deletions content/browser/renderer_host/backing_store_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,12 @@
}

bool BackingStoreMac::CopyFromBackingStore(const gfx::Rect& rect,
skia::PlatformCanvas* output) {
skia::PlatformBitmap* output) {
// TODO(thakis): Make sure this works with HiDPI backing stores.
if (!output->initialize(rect.width(), rect.height(), true))
if (!output->Allocate(rect.width(), rect.height(), true))
return false;

skia::ScopedPlatformPaint scoped_platform_paint(output);
CGContextRef temp_context = scoped_platform_paint.GetPlatformSurface();
CGContextRef temp_context = output->GetSurface();
gfx::ScopedCGContextSaveGState save_gstate(temp_context);
CGContextTranslateCTM(temp_context, 0.0, size().height());
CGContextScaleCTM(temp_context, 1.0, -1.0);
Expand Down
7 changes: 3 additions & 4 deletions content/browser/renderer_host/backing_store_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,11 @@ void BackingStoreWin::PaintToBackingStore(
}

bool BackingStoreWin::CopyFromBackingStore(const gfx::Rect& rect,
skia::PlatformCanvas* output) {
if (!output->initialize(rect.width(), rect.height(), true))
skia::PlatformBitmap* output) {
if (!output->Allocate(rect.width(), rect.height(), true))
return false;

skia::ScopedPlatformPaint scoped_platform_paint(output);
HDC temp_dc = scoped_platform_paint.GetPlatformSurface();
HDC temp_dc = output->GetSurface();
BitBlt(temp_dc, 0, 0, rect.width(), rect.height(),
hdc(), rect.x(), rect.y(), SRCCOPY);
return true;
Expand Down
2 changes: 1 addition & 1 deletion content/browser/renderer_host/backing_store_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class BackingStoreWin : public BackingStore {
const base::Closure& completion_callback,
bool* scheduled_completion_callback) OVERRIDE;
virtual bool CopyFromBackingStore(const gfx::Rect& rect,
skia::PlatformCanvas* output) OVERRIDE;
skia::PlatformBitmap* output) OVERRIDE;
virtual void ScrollBackingStore(int dx, int dy,
const gfx::Rect& clip_rect,
const gfx::Size& view_size) OVERRIDE;
Expand Down
2 changes: 1 addition & 1 deletion content/browser/renderer_host/render_widget_host_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ void RenderWidgetHostImpl::CopyFromBackingStore(
const gfx::Rect& src_subrect,
const gfx::Size& accelerated_dst_size,
const base::Callback<void(bool)>& callback,
skia::PlatformCanvas* output) {
skia::PlatformBitmap* output) {
if (view_ && is_accelerated_compositing_active_) {
TRACE_EVENT0("browser",
"RenderWidgetHostImpl::CopyFromBackingStore::FromCompositingSurface");
Expand Down
2 changes: 1 addition & 1 deletion content/browser/renderer_host/render_widget_host_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class CONTENT_EXPORT RenderWidgetHostImpl : virtual public RenderWidgetHost,
const gfx::Rect& src_rect,
const gfx::Size& accelerated_dst_size,
const base::Callback<void(bool)>& callback,
skia::PlatformCanvas* output) OVERRIDE;
skia::PlatformBitmap* output) OVERRIDE;
#if defined(TOOLKIT_GTK)
virtual bool CopyFromBackingStoreToGtkWindow(const gfx::Rect& dest_rect,
GdkWindow* target) OVERRIDE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ void RenderWidgetHostViewAndroid::CopyFromCompositingSurface(
const gfx::Rect& src_subrect,
const gfx::Size& dst_size,
const base::Callback<void(bool)>& callback,
skia::PlatformCanvas* output) {
skia::PlatformBitmap* output) {
NOTIMPLEMENTED();
callback.Run(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class RenderWidgetHostViewAndroid : public RenderWidgetHostViewBase {
const gfx::Rect& src_subrect,
const gfx::Size& dst_size,
const base::Callback<void(bool)>& callback,
skia::PlatformCanvas* output) OVERRIDE;
skia::PlatformBitmap* output) OVERRIDE;
virtual BackingStore* AllocBackingStore(const gfx::Size& size) OVERRIDE;
virtual gfx::GLSurfaceHandle GetCompositingSurface() OVERRIDE;
virtual void GetScreenInfo(WebKit::WebScreenInfo* results) OVERRIDE;
Expand Down
6 changes: 3 additions & 3 deletions content/browser/renderer_host/render_widget_host_view_aura.cc
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ void RenderWidgetHostViewAura::CopyFromCompositingSurface(
const gfx::Rect& src_subrect,
const gfx::Size& dst_size,
const base::Callback<void(bool)>& callback,
skia::PlatformCanvas* output) {
skia::PlatformBitmap* output) {
base::ScopedClosureRunner scoped_callback_runner(base::Bind(callback, false));

std::map<uint64, scoped_refptr<ui::Texture> >::iterator it =
Expand All @@ -646,7 +646,7 @@ void RenderWidgetHostViewAura::CopyFromCompositingSurface(
DCHECK(container);

gfx::Size dst_size_in_pixel = ConvertSizeToPixel(this, dst_size);
if (!output->initialize(
if (!output->Allocate(
dst_size_in_pixel.width(), dst_size_in_pixel.height(), true))
return;

Expand All @@ -656,7 +656,7 @@ void RenderWidgetHostViewAura::CopyFromCompositingSurface(
return;

unsigned char* addr = static_cast<unsigned char*>(
output->getTopDevice()->accessBitmap(true).getPixels());
output->GetBitmap().getPixels());
scoped_callback_runner.Release();
// Wrap the callback with an internal handler so that we can inject our
// own completion handlers (where we can call AdjustSurfaceProtection).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class RenderWidgetHostViewAura
const gfx::Rect& src_subrect,
const gfx::Size& dst_size,
const base::Callback<void(bool)>& callback,
skia::PlatformCanvas* output) OVERRIDE;
skia::PlatformBitmap* output) OVERRIDE;
virtual void OnAcceleratedCompositingStateChange() OVERRIDE;
virtual void AcceleratedSurfaceBuffersSwapped(
const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params_in_pixel,
Expand Down
6 changes: 3 additions & 3 deletions content/browser/renderer_host/render_widget_host_view_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ void RenderWidgetHostViewGtk::CopyFromCompositingSurface(
const gfx::Rect& src_subrect,
const gfx::Size& /* dst_size */,
const base::Callback<void(bool)>& callback,
skia::PlatformCanvas* output) {
skia::PlatformBitmap* output) {
base::ScopedClosureRunner scoped_callback_runner(base::Bind(callback, false));

const gfx::Rect bounds = GetViewBounds();
Expand All @@ -1051,10 +1051,10 @@ void RenderWidgetHostViewGtk::CopyFromCompositingSurface(
if (!image.get())
return;

if (!output->initialize(src_subrect.width(), src_subrect.height(), true))
if (!output->Allocate(src_subrect.width(), src_subrect.height(), true))
return;

const SkBitmap& bitmap = output->getTopDevice()->accessBitmap(true);
const SkBitmap& bitmap = output->GetBitmap();
const size_t bitmap_size = bitmap.getSize();
DCHECK_EQ(bitmap_size,
static_cast<size_t>(image->height * image->bytes_per_line));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class CONTENT_EXPORT RenderWidgetHostViewGtk
const gfx::Rect& src_subrect,
const gfx::Size& dst_size,
const base::Callback<void(bool)>& callback,
skia::PlatformCanvas* output) OVERRIDE;
skia::PlatformBitmap* output) OVERRIDE;
virtual void OnAcceleratedCompositingStateChange() OVERRIDE;
virtual void AcceleratedSurfaceBuffersSwapped(
const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class RenderWidgetHostViewMac : public RenderWidgetHostViewBase {
const gfx::Rect& src_subrect,
const gfx::Size& dst_size,
const base::Callback<void(bool)>& callback,
skia::PlatformCanvas* output) OVERRIDE;
skia::PlatformBitmap* output) OVERRIDE;
virtual void OnAcceleratedCompositingStateChange() OVERRIDE;

virtual void OnAccessibilityNotifications(
Expand Down
Loading

0 comments on commit 3b79834

Please sign in to comment.