Skip to content

Commit

Permalink
Make ui::Snapshot asynchronous on Android, remove CompositeAndReadback.
Browse files Browse the repository at this point in the history
The android pixel tests use the ui::Snapshot code to get a pixel copy
of the content_shell's output. Make this behaviour match the aura
implementation of ui::Snapshot by returning false in the synchronous
verion, causing callsites to fall back to the async one.

This implements the async snapshots by moving the aura implementation
out into snapshot_async.cc and reusing that code (with one #if for
RGBA vs BGRA ordering).

After this, the pixel tests get a bitmap from cc instead of creating
their own bitmap. The result is that the bitmap has a slightly
different size (384x528) which is the size of the root layer. The
pixel tests were previously trying to crop to 400x300, which is
outside the bounds of this bitmap, so change them to crop to 300x300
instead, and increment their versions to pick up the new pngs.

BUG=252046, 371592

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@270778 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
danakj@chromium.org committed May 15, 2014
1 parent c276c7a commit 2e6f39e
Show file tree
Hide file tree
Showing 18 changed files with 225 additions and 159 deletions.
1 change: 0 additions & 1 deletion cc/output/copy_output_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "base/debug/trace_event.h"
#include "base/logging.h"
#include "cc/output/copy_output_result.h"
#include "cc/resources/single_release_callback.h"
#include "cc/resources/texture_mailbox.h"
#include "third_party/skia/include/core/SkBitmap.h"

Expand Down
2 changes: 1 addition & 1 deletion cc/output/copy_output_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "cc/base/cc_export.h"
#include "cc/resources/single_release_callback.h"
#include "cc/resources/texture_mailbox.h"
#include "ui/gfx/rect.h"

class SkBitmap;

namespace cc {
class CopyOutputResult;
class SingleReleaseCallback;

class CC_EXPORT CopyOutputRequest {
public:
Expand Down
1 change: 0 additions & 1 deletion cc/output/copy_output_result.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "cc/output/copy_output_result.h"

#include "base/logging.h"
#include "cc/resources/single_release_callback.h"
#include "cc/resources/texture_mailbox.h"
#include "third_party/skia/include/core/SkBitmap.h"

Expand Down
2 changes: 1 addition & 1 deletion cc/output/copy_output_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

#include "base/memory/scoped_ptr.h"
#include "cc/base/cc_export.h"
#include "cc/resources/single_release_callback.h"
#include "cc/resources/texture_mailbox.h"
#include "ui/gfx/size.h"

class SkBitmap;

namespace cc {
class SingleReleaseCallback;
class TextureMailbox;

class CC_EXPORT CopyOutputResult {
Expand Down
9 changes: 0 additions & 9 deletions content/browser/android/content_view_render_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,6 @@ void ContentViewRenderView::SurfaceChanged(JNIEnv* env, jobject obj,
compositor_->SetWindowBounds(gfx::Size(width, height));
}

jboolean ContentViewRenderView::CompositeToBitmap(JNIEnv* env, jobject obj,
jobject java_bitmap) {
gfx::JavaBitmap bitmap(java_bitmap);
if (!compositor_ || bitmap.format() != ANDROID_BITMAP_FORMAT_RGBA_8888)
return false;
return compositor_->CompositeAndReadback(bitmap.pixels(),
gfx::Rect(bitmap.size()));
}

void ContentViewRenderView::SetOverlayVideoMode(
JNIEnv* env, jobject obj, bool enabled) {
compositor_->SetHasTransparentBackground(enabled);
Expand Down
1 change: 0 additions & 1 deletion content/browser/android/content_view_render_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class ContentViewRenderView : public CompositorClient {
void SurfaceChanged(JNIEnv* env, jobject obj,
jint format, jint width, jint height, jobject surface);
jboolean Composite(JNIEnv* env, jobject obj);
jboolean CompositeToBitmap(JNIEnv* env, jobject obj, jobject java_bitmap);
void SetOverlayVideoMode(JNIEnv* env, jobject obj, bool enabled);

// CompositorClient implementation
Expand Down
10 changes: 6 additions & 4 deletions content/browser/renderer_host/compositor_impl_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,7 @@ void CompositorImpl::SetHasTransparentBackground(bool flag) {
}

bool CompositorImpl::CompositeAndReadback(void *pixels, const gfx::Rect& rect) {
if (host_)
return host_->CompositeAndReadback(pixels, rect);
else
return false;
return false;
}

void CompositorImpl::SetNeedsComposite() {
Expand Down Expand Up @@ -623,6 +620,11 @@ void CompositorImpl::AttachLayerForReadback(scoped_refptr<cc::Layer> layer) {
root_layer_->AddChild(layer);
}

void CompositorImpl::RequestCopyOfOutputOnRootLayer(
scoped_ptr<cc::CopyOutputRequest> request) {
root_layer_->RequestCopyOfOutput(request.Pass());
}

void CompositorImpl::OnVSync(base::TimeTicks frame_time,
base::TimeDelta vsync_period) {
vsync_period_ = vsync_period;
Expand Down
2 changes: 2 additions & 0 deletions content/browser/renderer_host/compositor_impl_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class CONTENT_EXPORT CompositorImpl

// WindowAndroidCompositor implementation.
virtual void AttachLayerForReadback(scoped_refptr<cc::Layer> layer) OVERRIDE;
virtual void RequestCopyOfOutputOnRootLayer(
scoped_ptr<cc::CopyOutputRequest> request) OVERRIDE;
virtual void OnVSync(base::TimeTicks frame_time,
base::TimeDelta vsync_period) OVERRIDE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package org.chromium.content.browser;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.view.Surface;
Expand Down Expand Up @@ -150,18 +148,7 @@ protected void onReadyToRender() {
* @return The created SurfaceView object.
*/
protected SurfaceView createSurfaceView(Context context) {
return new SurfaceView(context) {
@Override
public void onDraw(Canvas canvas) {
// We only need to draw to software canvases, which are used for taking screenshots.
if (canvas.isHardwareAccelerated()) return;
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
Bitmap.Config.ARGB_8888);
if (nativeCompositeToBitmap(mNativeContentViewRenderView, bitmap)) {
canvas.drawBitmap(bitmap, 0, 0, null);
}
}
};
return new SurfaceView(context);
}

/**
Expand Down Expand Up @@ -214,7 +201,6 @@ private native void nativeSetLayerTreeBuildHelper(long nativeContentViewRenderVi
private native void nativeSurfaceDestroyed(long nativeContentViewRenderView);
private native void nativeSurfaceChanged(long nativeContentViewRenderView,
int format, int width, int height, Surface surface);
private native boolean nativeCompositeToBitmap(long nativeContentViewRenderView, Bitmap bitmap);
private native void nativeSetOverlayVideoMode(long nativeContentViewRenderView,
boolean enabled);
}
4 changes: 4 additions & 0 deletions content/test/gpu/gpu_tests/pixel_expectations.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ def SetExpectations(self):
# self.Fail('Pixel.Canvas2DRedBox',
# ['mac', 'amd', ('nvidia', 0x1234)], bug=123)

self.Fail('Pixel.Canvas2DRedBox', bug=371592)
self.Fail('Pixel.CSS3DBlueBox', bug=371592)
self.Fail('Pixel.WebGLGreenTriangle', bug=371592)

pass
12 changes: 6 additions & 6 deletions content/test/gpu/page_sets/pixel_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ def __init__(self):
self.AddPage(PixelTestsPage(
url='file://../../data/gpu/pixel_canvas2d.html',
name='Pixel.Canvas2DRedBox',
test_rect=[0, 0, 400, 300],
revision=2,
test_rect=[0, 0, 300, 300],
revision=3,
page_set=self))

self.AddPage(PixelTestsPage(
url='file://../../data/gpu/pixel_css3d.html',
name='Pixel.CSS3DBlueBox',
test_rect=[0, 0, 400, 300],
revision=5,
test_rect=[0, 0, 300, 300],
revision=6,
page_set=self))

self.AddPage(PixelTestsPage(
url='file://../../data/gpu/pixel_webgl.html',
name='Pixel.WebGLGreenTriangle',
test_rect=[0, 0, 400, 300],
revision=6,
test_rect=[0, 0, 300, 300],
revision=7,
page_set=self))
1 change: 1 addition & 0 deletions ui/base/DEPS
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include_rules = [
"+cc",
"+grit/app_locale_settings.h",
"+grit/ui_resources.h",
"+grit/ui_strings.h",
Expand Down
3 changes: 3 additions & 0 deletions ui/base/android/window_android_compositor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef UI_BASE_ANDROID_WINDOW_ANDROID_COMPOSITOR_H_
#define UI_BASE_ANDROID_WINDOW_ANDROID_COMPOSITOR_H_

#include "cc/output/copy_output_request.h"
#include "ui/base/ui_base_export.h"

namespace cc {
Expand All @@ -19,6 +20,8 @@ class UI_BASE_EXPORT WindowAndroidCompositor {
virtual ~WindowAndroidCompositor() {}

virtual void AttachLayerForReadback(scoped_refptr<cc::Layer> layer) = 0;
virtual void RequestCopyOfOutputOnRootLayer(
scoped_ptr<cc::CopyOutputRequest> request) = 0;
virtual void OnVSync(base::TimeTicks frame_time,
base::TimeDelta vsync_period) = 0;
};
Expand Down
17 changes: 15 additions & 2 deletions ui/snapshot/snapshot.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
'target_name': 'snapshot',
'type': '<(component)',
'dependencies': [
'../../skia/skia.gyp:skia',
'../../base/base.gyp:base',
'../../skia/skia.gyp:skia',
'../base/ui_base.gyp:ui_base',
'../gfx/gfx.gyp:gfx',
'../gfx/gfx.gyp:gfx_geometry',
Expand All @@ -23,6 +23,8 @@
'sources': [
'snapshot.h',
'snapshot_android.cc',
'snapshot_async.cc',
'snapshot_async.h',
'snapshot_aura.cc',
'snapshot_export.h',
'snapshot_ios.mm',
Expand All @@ -34,9 +36,20 @@
'..',
],
'conditions': [
['use_aura==1', {
['use_aura==1 or OS=="android"', {
'dependencies': [
'../../cc/cc.gyp:cc',
'../../gpu/gpu.gyp:command_buffer_common',
],
}],
['use_aura!=1 and OS!="android"', {
'sources!': [
'snapshot_async.cc',
'snapshot_async.h',
],
}],
['use_aura==1', {
'dependencies': [
'../aura/aura.gyp:aura',
'../compositor/compositor.gyp:compositor',
],
Expand Down
58 changes: 36 additions & 22 deletions ui/snapshot/snapshot_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

#include "ui/snapshot/snapshot.h"

#include "base/callback.h"
#include "base/bind.h"
#include "cc/output/copy_output_request.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/android/view_android.h"
#include "ui/base/android/window_android.h"
#include "ui/gfx/display.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/screen.h"
#include "ui/base/android/window_android_compositor.h"
#include "ui/snapshot/snapshot_async.h"

namespace ui {

Expand All @@ -24,39 +24,53 @@ bool GrabViewSnapshot(gfx::NativeView view,
bool GrabWindowSnapshot(gfx::NativeWindow window,
std::vector<unsigned char>* png_representation,
const gfx::Rect& snapshot_bounds) {
gfx::Display display =
gfx::Screen::GetNativeScreen()->GetPrimaryDisplay();
gfx::Rect scaled_bounds =
gfx::ScaleToEnclosingRect(snapshot_bounds,
display.device_scale_factor());
return window->GrabSnapshot(
scaled_bounds.x(), scaled_bounds.y(), scaled_bounds.width(),
scaled_bounds.height(), png_representation);
// Not supported in Android. Callers should fall back to the async version.
return false;
}

static void MakeAsyncCopyRequest(
gfx::NativeWindow window,
const gfx::Rect& source_rect,
const cc::CopyOutputRequest::CopyOutputRequestCallback& callback) {
scoped_ptr<cc::CopyOutputRequest> request =
cc::CopyOutputRequest::CreateBitmapRequest(callback);
request->set_area(source_rect);
window->GetCompositor()->RequestCopyOfOutputOnRootLayer(request.Pass());
}

void GrabWindowSnapshotAndScaleAsync(
gfx::NativeWindow window,
const gfx::Rect& snapshot_bounds,
const gfx::Rect& source_rect,
const gfx::Size& target_size,
scoped_refptr<base::TaskRunner> background_task_runner,
GrabWindowSnapshotAsyncCallback callback) {
callback.Run(gfx::Image());
const GrabWindowSnapshotAsyncCallback& callback) {
MakeAsyncCopyRequest(window,
source_rect,
base::Bind(&SnapshotAsync::ScaleCopyOutputResult,
callback,
target_size,
background_task_runner));
}

void GrabViewSnapshotAsync(
gfx::NativeView view,
void GrabWindowSnapshotAsync(
gfx::NativeWindow window,
const gfx::Rect& source_rect,
scoped_refptr<base::TaskRunner> background_task_runner,
const GrabWindowSnapshotAsyncPNGCallback& callback) {
callback.Run(scoped_refptr<base::RefCountedBytes>());
MakeAsyncCopyRequest(window,
source_rect,
base::Bind(&SnapshotAsync::EncodeCopyOutputResult,
callback,
background_task_runner));
}

void GrabWindowSnapshotAsync(
gfx::NativeWindow window,
void GrabViewSnapshotAsync(
gfx::NativeView view,
const gfx::Rect& source_rect,
scoped_refptr<base::TaskRunner> background_task_runner,
const GrabWindowSnapshotAsyncPNGCallback& callback) {
callback.Run(scoped_refptr<base::RefCountedBytes>());
GrabWindowSnapshotAsync(
view->GetWindowAndroid(), source_rect, background_task_runner, callback);
}

} // namespace ui
Loading

0 comments on commit 2e6f39e

Please sign in to comment.