Commit e2a0d629 authored by ben@chromium.org's avatar ben@chromium.org

Implement SetViewContents in the client lib.

Updates the view manager sample app too.\

R=sky@chromium.org
http://crbug.com/365012

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@272389 0039d316-1c4b-4281-b951-d872f2087c98
parent 219fbb41
include_rules = [
"+ui/gfx",
]
......@@ -11,8 +11,10 @@
#include "mojo/public/cpp/system/core.h"
#include "mojo/public/cpp/system/macros.h"
#include "mojo/public/cpp/utility/run_loop.h"
#include "mojo/services/public/cpp/view_manager/view.h"
#include "mojo/services/public/cpp/view_manager/view_manager.h"
#include "mojo/services/public/cpp/view_manager/view_tree_node.h"
#include "ui/gfx/canvas.h"
#if defined(WIN32)
#if !defined(CDECL)
......@@ -37,7 +39,18 @@ class SampleApp : public Application {
view_manager::ViewTreeNode::Create(view_manager_.get());
view_manager::ViewTreeNode* node11 =
view_manager::ViewTreeNode::Create(view_manager_.get());
node11->SetBounds(gfx::Rect(800, 600));
view_manager::View* view11 =
view_manager::View::Create(view_manager_.get());
node11->SetActiveView(view11);
view_manager_->tree()->AddChild(node1);
node1->AddChild(node11);
gfx::Canvas canvas(gfx::Size(800, 600), 1.0f, true);
canvas.DrawColor(SK_ColorRED);
view11->SetContents(
skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(true));
}
virtual ~SampleApp() {
......
......@@ -150,6 +150,8 @@
'type': 'static_library',
'dependencies': [
'../base/base.gyp:base',
'../skia/skia.gyp:skia',
'../ui/gfx/gfx.gyp:gfx',
'../ui/gfx/gfx.gyp:gfx_geometry',
'mojo_geometry_bindings',
'mojo_geometry_lib',
......
......@@ -20,7 +20,14 @@ gfx::Rect GetWindowBoundsForClientBounds(DWORD style, DWORD ex_style,
wr.right = bounds.x() + bounds.width();
wr.bottom = bounds.y() + bounds.height();
AdjustWindowRectEx(&wr, style, FALSE, ex_style);
return gfx::Rect(wr.left, wr.top, wr.right - wr.left, wr.bottom - wr.top);
// Make sure to keep the window onscreen, as AdjustWindowRectEx() may have
// moved part of it offscreen.
gfx::Rect window_bounds(wr.left, wr.top,
wr.right - wr.left, wr.bottom - wr.top);
window_bounds.set_x(std::max(0, window_bounds.x()));
window_bounds.set_y(std::max(0, window_bounds.y()));
return window_bounds;
}
}
......
include_rules = [
"+mojo/geometry",
"+ui/gfx/geometry"
"+third_party/skia",
"+ui/gfx",
]
specific_include_rules = {
......
......@@ -56,6 +56,11 @@ void View::RemoveObserver(ViewObserver* observer) {
observers_.RemoveObserver(observer);
}
void View::SetContents(const SkBitmap& contents) {
if (manager_)
ViewManagerPrivate(manager_).synchronizer()->SetViewContents(id_, contents);
}
View::View(ViewManager* manager)
: id_(ViewManagerPrivate(manager).synchronizer()->CreateView()),
node_(NULL),
......
......@@ -12,6 +12,8 @@
#include "mojo/services/public/cpp/view_manager/lib/view_private.h"
#include "mojo/services/public/cpp/view_manager/lib/view_tree_node_private.h"
#include "mojo/services/public/cpp/view_manager/util.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
namespace mojo {
namespace view_manager {
......@@ -101,7 +103,9 @@ class ViewManagerTransaction {
// View replacement.
TYPE_SET_ACTIVE_VIEW,
// Node bounds.
TYPE_SET_BOUNDS
TYPE_SET_BOUNDS,
// View contents
TYPE_SET_VIEW_CONTENTS
};
ViewManagerTransaction(TransactionType transaction_type,
......@@ -333,6 +337,71 @@ class SetBoundsTransaction : public ViewManagerTransaction {
DISALLOW_COPY_AND_ASSIGN(SetBoundsTransaction);
};
class SetViewContentsTransaction : public ViewManagerTransaction {
public:
SetViewContentsTransaction(TransportViewId view_id,
const SkBitmap& contents,
ViewManagerSynchronizer* synchronizer)
: ViewManagerTransaction(TYPE_SET_VIEW_CONTENTS, synchronizer),
view_id_(view_id),
contents_(contents) {}
virtual ~SetViewContentsTransaction() {}
private:
// Overridden from ViewManagerTransaction:
virtual void DoCommit() OVERRIDE {
std::vector<unsigned char> data;
gfx::PNGCodec::EncodeBGRASkBitmap(contents_, false, &data);
void* memory = NULL;
ScopedSharedBufferHandle duped;
bool result = CreateMapAndDupSharedBuffer(data.size(),
&memory,
&shared_state_handle_,
&duped);
if (!result)
return;
memcpy(memory, &data[0], data.size());
AllocationScope scope;
service()->SetViewContents(view_id_, duped.Pass(), data.size(),
ActionCompletedCallback());
}
virtual void DoActionCompleted(bool success) OVERRIDE {
// TODO(beng): recovery?
}
bool CreateMapAndDupSharedBuffer(size_t size,
void** memory,
ScopedSharedBufferHandle* handle,
ScopedSharedBufferHandle* duped) {
MojoResult result = CreateSharedBuffer(NULL, size, handle);
if (result != MOJO_RESULT_OK)
return false;
DCHECK(handle->is_valid());
result = DuplicateBuffer(handle->get(), NULL, duped);
if (result != MOJO_RESULT_OK)
return false;
DCHECK(duped->is_valid());
result = MapBuffer(
handle->get(), 0, size, memory, MOJO_MAP_BUFFER_FLAG_NONE);
if (result != MOJO_RESULT_OK)
return false;
DCHECK(*memory);
return true;
}
const TransportViewId view_id_;
const SkBitmap contents_;
ScopedSharedBufferHandle shared_state_handle_;
DISALLOW_COPY_AND_ASSIGN(SetViewContentsTransaction);
};
ViewManagerSynchronizer::ViewManagerSynchronizer(ViewManager* view_manager)
: view_manager_(view_manager),
connected_(false),
......@@ -435,6 +504,14 @@ void ViewManagerSynchronizer::SetBounds(TransportNodeId node_id,
Sync();
}
void ViewManagerSynchronizer::SetViewContents(TransportViewId view_id,
const SkBitmap& contents) {
DCHECK(connected_);
pending_transactions_.push_back(
new SetViewContentsTransaction(view_id, contents, this));
Sync();
}
////////////////////////////////////////////////////////////////////////////////
// ViewManagerSynchronizer, IViewManagerClient implementation:
......
......@@ -13,6 +13,8 @@
#include "mojo/services/public/cpp/view_manager/view_manager_types.h"
#include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h"
class SkBitmap;
namespace base {
class RunLoop;
}
......@@ -50,6 +52,7 @@ class ViewManagerSynchronizer : public IViewManagerClient {
void SetActiveView(TransportNodeId node_id, TransportViewId view_id);
void SetBounds(TransportNodeId node_id, const gfx::Rect& bounds);
void SetViewContents(TransportViewId view_id, const SkBitmap& contents);
void set_changes_acked_callback(const base::Callback<void(void)>& callback) {
changes_acked_callback_ = callback;
......
......@@ -9,6 +9,8 @@
#include "base/observer_list.h"
#include "mojo/services/public/cpp/view_manager/view_manager_types.h"
class SkBitmap;
namespace mojo {
namespace view_manager {
......@@ -29,6 +31,8 @@ class View {
void AddObserver(ViewObserver* observer);
void RemoveObserver(ViewObserver* observer);
void SetContents(const SkBitmap& contents);
private:
friend class ViewPrivate;
......
......@@ -85,7 +85,7 @@ interface IViewManager {
// Shows the specified image (png encoded) in the specified view.
SetViewContents(uint32 view_id,
handle<shared_buffer> buffer,
uint32 buffer_size);
uint32 buffer_size) => (bool success);
// Sets the ids of the roots for the specified connection.
// TODO(sky): this is temporary for testing. This needs to be conveyed at
......
......@@ -571,13 +571,17 @@ void ViewManagerConnection::SetView(
void ViewManagerConnection::SetViewContents(
TransportViewId view_id,
ScopedSharedBufferHandle buffer,
uint32_t buffer_size) {
uint32_t buffer_size,
const Callback<void(bool)>& callback) {
View* view = GetView(ViewIdFromTransportId(view_id));
if (!view)
if (!view) {
callback.Run(false);
return;
}
void* handle_data;
if (MapBuffer(buffer.get(), 0, buffer_size, &handle_data,
MOJO_MAP_BUFFER_FLAG_NONE) != MOJO_RESULT_OK) {
callback.Run(false);
return;
}
SkBitmap bitmap;
......@@ -585,6 +589,7 @@ void ViewManagerConnection::SetViewContents(
buffer_size, &bitmap);
view->SetBitmap(bitmap);
UnmapBuffer(handle_data);
callback.Run(true);
}
void ViewManagerConnection::SetRoots(
......
......@@ -160,7 +160,8 @@ class MOJO_VIEW_MANAGER_EXPORT ViewManagerConnection
const Callback<void(bool)>& callback) OVERRIDE;
virtual void SetViewContents(TransportViewId view_id,
ScopedSharedBufferHandle buffer,
uint32_t buffer_size) OVERRIDE;
uint32_t buffer_size,
const Callback<void(bool)>& callback) OVERRIDE;
virtual void SetRoots(
TransportConnectionId connection_id,
const Array<TransportNodeId>& transport_node_ids,
......
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