Skip to content

Commit

Permalink
aw: ReturnResource on draw and clean ups
Browse files Browse the repository at this point in the history
Instead of posting a task to return resources to child
compositor, just do it synchronously before each draw.
This allows the complex post task and return resource
logic to be greatly simplified.

BUG=388896

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@280240 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
boliu@chromium.org committed Jun 27, 2014
1 parent ef7d522 commit c62fc98
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 66 deletions.
48 changes: 16 additions & 32 deletions android_webview/browser/browser_view_renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ BrowserViewRenderer::BrowserViewRenderer(
: client_(client),
shared_renderer_state_(shared_renderer_state),
web_contents_(web_contents),
weak_factory_on_ui_thread_(this),
ui_thread_weak_ptr_(weak_factory_on_ui_thread_.GetWeakPtr()),
ui_task_runner_(ui_task_runner),
compositor_(NULL),
is_paused_(false),
Expand Down Expand Up @@ -247,7 +245,7 @@ bool BrowserViewRenderer::OnDrawHardware(jobject java_canvas) {
if (!hardware_enabled_)
return false;

ReturnResources();
ReturnResourceFromParent();
SynchronousCompositorMemoryPolicy new_policy = CalculateDesiredMemoryPolicy();
RequestMemoryPolicy(new_policy);
compositor_->SetMemoryPolicy(memory_policy_);
Expand All @@ -271,36 +269,25 @@ bool BrowserViewRenderer::OnDrawHardware(jobject java_canvas) {
GlobalTileManager::GetInstance()->DidUse(tile_manager_key_);

frame->AssignTo(&draw_gl_input->frame);
scoped_ptr<DrawGLInput> old_input = shared_renderer_state_->PassDrawGLInput();
if (old_input.get()) {
shared_renderer_state_->ReturnResources(
old_input->frame.delegated_frame_data->resource_list);
}
ReturnUnusedResource(shared_renderer_state_->PassDrawGLInput());
shared_renderer_state_->SetDrawGLInput(draw_gl_input.Pass());

DidComposite();
bool did_request = client_->RequestDrawGL(java_canvas, false);
if (did_request)
return true;

ReturnResources();
return false;
return client_->RequestDrawGL(java_canvas, false);
}

void BrowserViewRenderer::DidDrawDelegated() {
if (!ui_task_runner_->BelongsToCurrentThread()) {
// TODO(boliu): This should be a cancelable callback.
// TODO(boliu): Do this PostTask in AwContents instead so every method in
// this class is called by UI thread.
ui_task_runner_->PostTask(FROM_HERE,
base::Bind(&BrowserViewRenderer::DidDrawDelegated,
ui_thread_weak_ptr_));
void BrowserViewRenderer::ReturnUnusedResource(scoped_ptr<DrawGLInput> input) {
if (!input.get())
return;
}
ReturnResources();

cc::CompositorFrameAck frame_ack;
cc::TransferableResource::ReturnResources(
input->frame.delegated_frame_data->resource_list,
&frame_ack.resources);
if (!frame_ack.resources.empty())
compositor_->ReturnResources(frame_ack);
}

void BrowserViewRenderer::ReturnResources() {
void BrowserViewRenderer::ReturnResourceFromParent() {
cc::CompositorFrameAck frame_ack;
shared_renderer_state_->SwapReturnedResources(&frame_ack.resources);
if (!frame_ack.resources.empty()) {
Expand Down Expand Up @@ -426,12 +413,8 @@ void BrowserViewRenderer::OnDetachedFromWindow() {
TRACE_EVENT0("android_webview", "BrowserViewRenderer::OnDetachedFromWindow");
attached_to_window_ = false;
if (hardware_enabled_) {
scoped_ptr<DrawGLInput> input = shared_renderer_state_->PassDrawGLInput();
if (input.get()) {
shared_renderer_state_->ReturnResources(
input->frame.delegated_frame_data->resource_list);
}
ReturnResources();
ReturnUnusedResource(shared_renderer_state_->PassDrawGLInput());
ReturnResourceFromParent();
DCHECK(shared_renderer_state_->ReturnedResourcesEmpty());

compositor_->ReleaseHwDraw();
Expand Down Expand Up @@ -714,6 +697,7 @@ void BrowserViewRenderer::ForceFakeCompositeSW() {

bool BrowserViewRenderer::CompositeSW(SkCanvas* canvas) {
DCHECK(compositor_);
ReturnResourceFromParent();
bool result = compositor_->DemandDrawSw(canvas);
DidComposite();
return result;
Expand Down
14 changes: 4 additions & 10 deletions android_webview/browser/browser_view_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class BrowserViewRenderer : public content::SynchronousCompositorClient,
const gfx::Vector2d& scroll,
const gfx::Rect& global_visible_rect,
const gfx::Rect& clip);
void DidDrawDelegated();

// CapturePicture API methods.
skia::RefPtr<SkPicture> CapturePicture(int width, int height);
Expand Down Expand Up @@ -116,7 +115,7 @@ class BrowserViewRenderer : public content::SynchronousCompositorClient,

void TrimMemory(const int level, const bool visible);

// SynchronousCompositorClient overrides
// SynchronousCompositorClient overrides.
virtual void DidInitializeCompositor(
content::SynchronousCompositor* compositor) OVERRIDE;
virtual void DidDestroyCompositor(content::SynchronousCompositor* compositor)
Expand All @@ -136,7 +135,7 @@ class BrowserViewRenderer : public content::SynchronousCompositorClient,
gfx::Vector2dF latest_overscroll_delta,
gfx::Vector2dF current_fling_velocity) OVERRIDE;

// GlobalTileManagerClient overrides
// GlobalTileManagerClient overrides.
virtual size_t GetNumTiles() const OVERRIDE;
virtual void SetNumTiles(size_t num_tiles,
bool effective_immediately) OVERRIDE;
Expand All @@ -155,7 +154,8 @@ class BrowserViewRenderer : public content::SynchronousCompositorClient,
const gfx::SizeF& scrollable_size_dip);

bool OnDrawHardware(jobject java_canvas);
void ReturnResources();
void ReturnUnusedResource(scoped_ptr<DrawGLInput> input);
void ReturnResourceFromParent();

// If we call up view invalidate and OnDraw is not called before a deadline,
// then we keep ticking the SynchronousCompositor so it can make progress.
Expand All @@ -179,12 +179,6 @@ class BrowserViewRenderer : public content::SynchronousCompositorClient,
BrowserViewRendererClient* client_;
SharedRendererState* shared_renderer_state_;
content::WebContents* web_contents_;
// TODO(boliu): This class should only be used on the UI thread. However in
// short term to supporting HardwareRenderer, some callbacks on
// SynchronousCompositorClient may be called on non-UI thread. These are
// used to detect this and post them back to UI thread.
base::WeakPtrFactory<BrowserViewRenderer> weak_factory_on_ui_thread_;
base::WeakPtr<BrowserViewRenderer> ui_thread_weak_ptr_;
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;

content::SynchronousCompositor* compositor_;
Expand Down
6 changes: 2 additions & 4 deletions android_webview/browser/hardware_renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void HardwareRenderer::DidBeginMainFrame() {
output_surface_->SetDrawConstraints(viewport_, clip_);
}

bool HardwareRenderer::DrawGL(bool stencil_enabled,
void HardwareRenderer::DrawGL(bool stencil_enabled,
int framebuffer_binding_ext,
AwDrawGLInfo* draw_info) {
TRACE_EVENT0("android_webview", "HardwareRenderer::DrawGL");
Expand All @@ -136,7 +136,7 @@ bool HardwareRenderer::DrawGL(bool stencil_enabled,
EGLContext current_context = eglGetCurrentContext();
if (!current_context) {
DLOG(ERROR) << "DrawGL called without EGLContext";
return false;
return;
}

// TODO(boliu): Handle context loss.
Expand Down Expand Up @@ -206,8 +206,6 @@ bool HardwareRenderer::DrawGL(bool stencil_enabled,
layer_tree_host_->Composite(gfx::FrameTime::Now());
}
gl_surface_->ResetBackingFrameBufferObject();

return true;
}

scoped_ptr<cc::OutputSurface> HardwareRenderer::CreateOutputSurface(
Expand Down
2 changes: 1 addition & 1 deletion android_webview/browser/hardware_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class HardwareRenderer : public cc::LayerTreeHostClient,
explicit HardwareRenderer(SharedRendererState* state);
virtual ~HardwareRenderer();

bool DrawGL(bool stencil_enabled,
void DrawGL(bool stencil_enabled,
int framebuffer_binding_ext,
AwDrawGLInfo* draw_info);

Expand Down
11 changes: 4 additions & 7 deletions android_webview/browser/shared_renderer_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ SharedRendererState::SharedRendererState(
DCHECK(client_on_ui_);
}

SharedRendererState::~SharedRendererState() {}
SharedRendererState::~SharedRendererState() {
DCHECK(ui_loop_->BelongsToCurrentThread());
}

void SharedRendererState::ClientRequestDrawGL() {
if (ui_loop_->BelongsToCurrentThread()) {
Expand Down Expand Up @@ -82,12 +84,6 @@ gpu::GLInProcessContext* SharedRendererState::GetSharedContext() const {
return share_context_;
}

void SharedRendererState::ReturnResources(
const cc::TransferableResourceArray& input) {
base::AutoLock lock(lock_);
cc::TransferableResource::ReturnResources(input, &returned_resources_);
}

void SharedRendererState::InsertReturnedResources(
const cc::ReturnedResourceArray& resources) {
base::AutoLock lock(lock_);
Expand All @@ -97,6 +93,7 @@ void SharedRendererState::InsertReturnedResources(

void SharedRendererState::SwapReturnedResources(
cc::ReturnedResourceArray* resources) {
DCHECK(resources->empty());
base::AutoLock lock(lock_);
resources->swap(returned_resources_);
}
Expand Down
8 changes: 1 addition & 7 deletions android_webview/browser/shared_renderer_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ struct DrawGLInput {
~DrawGLInput();
};

// This class holds renderer state that is shared between UI and RT threads.
// Android framework will block the UI thread when RT is drawing, so no locking
// is needed in this class. In the interim, this class is also responsible for
// thread hopping that should eventually be removed once RT support work is
// complete.
// This class is used to pass data between UI thread and RenderThread.
class SharedRendererState {
public:
SharedRendererState(scoped_refptr<base::MessageLoopProxy> ui_loop,
Expand All @@ -59,7 +55,6 @@ class SharedRendererState {
void SetSharedContext(gpu::GLInProcessContext* context);
gpu::GLInProcessContext* GetSharedContext() const;

void ReturnResources(const cc::TransferableResourceArray& input);
void InsertReturnedResources(const cc::ReturnedResourceArray& resources);
void SwapReturnedResources(cc::ReturnedResourceArray* resources);
bool ReturnedResourcesEmpty() const;
Expand All @@ -68,7 +63,6 @@ class SharedRendererState {
void ClientRequestDrawGLOnUIThread();

scoped_refptr<base::MessageLoopProxy> ui_loop_;
// TODO(boliu): Remove |client_on_ui_| from shared state.
BrowserViewRendererClient* client_on_ui_;
base::WeakPtrFactory<SharedRendererState> weak_factory_on_ui_thread_;
base::WeakPtr<SharedRendererState> ui_thread_weak_ptr_;
Expand Down
8 changes: 3 additions & 5 deletions android_webview/native/aw_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,9 @@ void AwContents::DrawGL(AwDrawGLInfo* draw_info) {
hardware_renderer_.reset(new HardwareRenderer(&shared_renderer_state_));
}

if (hardware_renderer_->DrawGL(state_restore.stencil_enabled(),
state_restore.framebuffer_binding_ext(),
draw_info)) {
browser_view_renderer_.DidDrawDelegated();
}
hardware_renderer_->DrawGL(state_restore.stencil_enabled(),
state_restore.framebuffer_binding_ext(),
draw_info);
}

namespace {
Expand Down

0 comments on commit c62fc98

Please sign in to comment.