OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/client/plugin/pepper_view.h" | 5 #include "remoting/client/plugin/pepper_view.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "remoting/client/decoder_verbatim.h" | 8 #include "remoting/client/decoder_verbatim.h" |
| 9 #include "remoting/client/plugin/chromoting_plugin.h" |
| 10 #include "remoting/client/plugin/pepper_util.h" |
| 11 #include "third_party/ppapi/cpp/device_context_2d.h" |
| 12 #include "third_party/ppapi/cpp/image_data.h" |
9 | 13 |
10 namespace remoting { | 14 namespace remoting { |
11 | 15 |
12 PepperView::PepperView(MessageLoop* message_loop, NPDevice* rendering_device, | 16 PepperView::PepperView(ChromotingPlugin* plugin) |
13 NPP plugin_instance) | 17 : plugin_(plugin), |
14 : message_loop_(message_loop), | |
15 rendering_device_(rendering_device), | |
16 plugin_instance_(plugin_instance), | |
17 backing_store_width_(0), | 18 backing_store_width_(0), |
18 backing_store_height_(0), | 19 backing_store_height_(0), |
19 viewport_x_(0), | 20 viewport_x_(0), |
20 viewport_y_(0), | 21 viewport_y_(0), |
21 viewport_width_(0), | 22 viewport_width_(0), |
22 viewport_height_(0), | 23 viewport_height_(0), |
23 is_static_fill_(false), | 24 is_static_fill_(false), |
24 static_fill_color_(0) { | 25 static_fill_color_(0) { |
25 } | 26 } |
26 | 27 |
27 PepperView::~PepperView() { | 28 PepperView::~PepperView() { |
28 } | 29 } |
29 | 30 |
30 void PepperView::Paint() { | 31 void PepperView::Paint() { |
31 message_loop_->PostTask( | 32 if (!plugin_->CurrentlyOnPluginThread()) { |
32 FROM_HERE, | 33 RunTaskOnPluginThread(NewRunnableMethod(this, &PepperView::Paint)); |
33 NewRunnableMethod(this, &PepperView::DoPaint)); | 34 return; |
| 35 } |
| 36 |
| 37 // TODO(ajwong): We shouldn't assume the image data format. |
| 38 pp::ImageData image(PP_IMAGEDATAFORMAT_BGRA_PREMUL, viewport_width_, |
| 39 viewport_height_, false); |
| 40 if (image.is_null()) { |
| 41 LOG(ERROR) << "Unable to allocate image."; |
| 42 return; |
| 43 } |
| 44 |
| 45 if (is_static_fill_) { |
| 46 for (int y = 0; y < image.height(); y++) { |
| 47 for (int x = 0; x < image.width(); x++) { |
| 48 *image.GetAddr32(x, y) = static_fill_color_; |
| 49 } |
| 50 } |
| 51 } else if (frame_) { |
| 52 int32_t* frame_data = |
| 53 reinterpret_cast<int32_t*>(frame_->data(media::VideoFrame::kRGBPlane)); |
| 54 int max_height = std::min(backing_store_height_, image.height()); |
| 55 int max_width = std::min(backing_store_width_, image.width()); |
| 56 for (int y = 0; y < max_height; y++) { |
| 57 for (int x = 0; x < max_width; x++) { |
| 58 // Force alpha to be set to 255. |
| 59 *image.GetAddr32(x, y) = |
| 60 frame_data[y*backing_store_width_ + x] | 0xFF000000; |
| 61 } |
| 62 } |
| 63 } else { |
| 64 // Nothing to paint. escape! |
| 65 // |
| 66 // TODO(ajwong): This is an ugly control flow. fix. |
| 67 return; |
| 68 } |
| 69 device_context_.ReplaceContents(&image); |
| 70 device_context_.Flush(TaskToCompletionCallback( |
| 71 NewRunnableMethod(this, &PepperView::OnPaintDone))); |
34 } | 72 } |
35 | 73 |
36 void PepperView::SetSolidFill(uint32 color) { | 74 void PepperView::SetSolidFill(uint32 color) { |
37 message_loop_->PostTask( | 75 if (!plugin_->CurrentlyOnPluginThread()) { |
38 FROM_HERE, | 76 RunTaskOnPluginThread( |
39 NewRunnableMethod(this, &PepperView::DoSetSolidFill, color)); | 77 NewRunnableMethod(this, &PepperView::SetSolidFill, color)); |
40 } | 78 return; |
41 | |
42 void PepperView::UnsetSolidFill() { | |
43 message_loop_->PostTask( | |
44 FROM_HERE, | |
45 NewRunnableMethod(this, &PepperView::DoUnsetSolidFill)); | |
46 } | |
47 | |
48 void PepperView::SetViewport(int x, int y, int width, int height) { | |
49 message_loop_->PostTask( | |
50 FROM_HERE, | |
51 NewRunnableMethod(this, &PepperView::DoSetViewport, | |
52 x, y, width, height)); | |
53 } | |
54 | |
55 void PepperView::SetBackingStoreSize(int width, int height) { | |
56 message_loop_->PostTask( | |
57 FROM_HERE, | |
58 NewRunnableMethod(this, &PepperView::DoSetBackingStoreSize, | |
59 width, height)); | |
60 } | |
61 | |
62 void PepperView::HandleBeginUpdateStream(HostMessage* msg) { | |
63 message_loop_->PostTask( | |
64 FROM_HERE, | |
65 NewRunnableMethod(this, &PepperView::DoHandleBeginUpdateStream, msg)); | |
66 } | |
67 | |
68 void PepperView::HandleUpdateStreamPacket(HostMessage* msg) { | |
69 message_loop_->PostTask( | |
70 FROM_HERE, | |
71 NewRunnableMethod(this, &PepperView::DoHandleUpdateStreamPacket, msg)); | |
72 } | |
73 | |
74 void PepperView::HandleEndUpdateStream(HostMessage* msg) { | |
75 message_loop_->PostTask( | |
76 FROM_HERE, | |
77 NewRunnableMethod(this, &PepperView::DoHandleEndUpdateStream, msg)); | |
78 } | |
79 | |
80 void PepperView::DoPaint() { | |
81 DCHECK_EQ(message_loop_, MessageLoop::current()); | |
82 | |
83 LOG(INFO) << "Starting PepperView::DoPaint"; | |
84 | |
85 NPDeviceContext2D context; | |
86 NPDeviceContext2DConfig config; | |
87 rendering_device_->initializeContext(plugin_instance_, &config, &context); | |
88 | |
89 uint32* output_bitmap = static_cast<uint32*>(context.region); | |
90 | |
91 // TODO(ajwong): Remove debugging code and actually hook up real painting | |
92 // logic from the decoder. | |
93 LOG(INFO) << "Painting top: " << context.dirty.top | |
94 << " bottom: " << context.dirty.bottom | |
95 << " left: " << context.dirty.left | |
96 << " right: " << context.dirty.right; | |
97 for (int i = context.dirty.top; i < context.dirty.bottom; ++i) { | |
98 for (int j = context.dirty.left; j < context.dirty.right; ++j) { | |
99 *output_bitmap++ = static_fill_color_; | |
100 } | |
101 } | 79 } |
102 | 80 |
103 rendering_device_->flushContext(plugin_instance_, &context, NULL, NULL); | |
104 LOG(INFO) << "Finishing PepperView::DoPaint"; | |
105 } | |
106 | |
107 void PepperView::DoSetSolidFill(uint32 color) { | |
108 DCHECK_EQ(message_loop_, MessageLoop::current()); | |
109 | |
110 is_static_fill_ = true; | 81 is_static_fill_ = true; |
111 static_fill_color_ = color; | 82 static_fill_color_ = color; |
112 } | 83 } |
113 | 84 |
114 void PepperView::DoUnsetSolidFill() { | 85 void PepperView::UnsetSolidFill() { |
115 DCHECK_EQ(message_loop_, MessageLoop::current()); | 86 if (!plugin_->CurrentlyOnPluginThread()) { |
| 87 RunTaskOnPluginThread( |
| 88 NewRunnableMethod(this, &PepperView::UnsetSolidFill)); |
| 89 return; |
| 90 } |
116 | 91 |
117 is_static_fill_ = false; | 92 is_static_fill_ = false; |
118 } | 93 } |
119 | 94 |
120 void PepperView::DoSetViewport(int x, int y, int width, int height) { | 95 void PepperView::SetViewport(int x, int y, int width, int height) { |
121 DCHECK_EQ(message_loop_, MessageLoop::current()); | 96 if (!plugin_->CurrentlyOnPluginThread()) { |
| 97 RunTaskOnPluginThread(NewRunnableMethod(this, &PepperView::SetViewport, |
| 98 x, y, width, height)); |
| 99 return; |
| 100 } |
122 | 101 |
| 102 // TODO(ajwong): Should we ignore x & y updates? What do those even mean? |
| 103 |
| 104 // TODO(ajwong): What does viewport x, y mean to a plugin anyways? |
123 viewport_x_ = x; | 105 viewport_x_ = x; |
124 viewport_y_ = y; | 106 viewport_y_ = y; |
125 viewport_width_ = width; | 107 viewport_width_ = width; |
126 viewport_height_ = height; | 108 viewport_height_ = height; |
| 109 |
| 110 device_context_ = |
| 111 pp::DeviceContext2D(viewport_width_, viewport_height_, false); |
| 112 if (!plugin_->BindGraphicsDeviceContext(device_context_)) { |
| 113 LOG(ERROR) << "Couldn't bind the device context."; |
| 114 return; |
| 115 } |
127 } | 116 } |
128 | 117 |
129 void PepperView::DoSetBackingStoreSize(int width, int height) { | 118 void PepperView::SetBackingStoreSize(int width, int height) { |
130 DCHECK_EQ(message_loop_, MessageLoop::current()); | 119 if (!plugin_->CurrentlyOnPluginThread()) { |
| 120 RunTaskOnPluginThread(NewRunnableMethod(this, |
| 121 &PepperView::SetBackingStoreSize, |
| 122 width, height)); |
| 123 return; |
| 124 } |
131 | 125 |
132 backing_store_width_ = width; | 126 backing_store_width_ = width; |
133 backing_store_height_ = height; | 127 backing_store_height_ = height; |
134 } | 128 } |
135 | 129 |
136 void PepperView::DoHandleBeginUpdateStream(HostMessage* msg) { | 130 void PepperView::HandleBeginUpdateStream(HostMessage* msg) { |
137 DCHECK_EQ(message_loop_, MessageLoop::current()); | 131 if (!plugin_->CurrentlyOnPluginThread()) { |
| 132 RunTaskOnPluginThread( |
| 133 NewRunnableMethod(this, &PepperView::HandleBeginUpdateStream, |
| 134 msg)); |
| 135 return; |
| 136 } |
138 | 137 |
139 NOTIMPLEMENTED(); | 138 scoped_ptr<HostMessage> deleter(msg); |
| 139 |
| 140 // TODO(hclam): Use the information from the message to create the decoder. |
| 141 // We lazily construct the decoder. |
| 142 if (!decoder_.get()) { |
| 143 decoder_.reset(new DecoderVerbatim()); |
| 144 } |
| 145 |
| 146 if (!frame_) { |
| 147 media::VideoFrame::CreateFrame(media::VideoFrame::RGB32, |
| 148 backing_store_width_, |
| 149 backing_store_height_, |
| 150 base::TimeDelta(), base::TimeDelta(), |
| 151 &frame_); |
| 152 } |
| 153 |
| 154 // Tell the decoder to do start decoding. |
| 155 decoder_->BeginDecode(frame_, &update_rects_, |
| 156 NewRunnableMethod(this, &PepperView::OnPartialDecodeDone), |
| 157 NewRunnableMethod(this, &PepperView::OnDecodeDone)); |
140 } | 158 } |
141 | 159 |
142 void PepperView::DoHandleUpdateStreamPacket(HostMessage* msg) { | 160 void PepperView::HandleUpdateStreamPacket(HostMessage* msg) { |
143 DCHECK_EQ(message_loop_, MessageLoop::current()); | 161 if (!plugin_->CurrentlyOnPluginThread()) { |
| 162 RunTaskOnPluginThread( |
| 163 NewRunnableMethod(this, &PepperView::HandleUpdateStreamPacket, |
| 164 msg)); |
| 165 return; |
| 166 } |
144 | 167 |
145 NOTIMPLEMENTED(); | 168 decoder_->PartialDecode(msg); |
146 } | 169 } |
147 | 170 |
148 void PepperView::DoHandleEndUpdateStream(HostMessage* msg) { | 171 void PepperView::HandleEndUpdateStream(HostMessage* msg) { |
149 DCHECK_EQ(message_loop_, MessageLoop::current()); | 172 if (!plugin_->CurrentlyOnPluginThread()) { |
| 173 RunTaskOnPluginThread( |
| 174 NewRunnableMethod(this, &PepperView::HandleEndUpdateStream, |
| 175 msg)); |
| 176 return; |
| 177 } |
150 | 178 |
151 NOTIMPLEMENTED(); | 179 scoped_ptr<HostMessage> deleter(msg); |
| 180 decoder_->EndDecode(); |
| 181 } |
| 182 |
| 183 void PepperView::OnPaintDone() { |
| 184 // TODO(ajwong):Probably should set some variable to allow repaints to |
| 185 // actually paint. |
| 186 return; |
| 187 } |
| 188 |
| 189 void PepperView::OnPartialDecodeDone() { |
| 190 all_update_rects_.insert(all_update_rects_.begin() + |
| 191 all_update_rects_.size(), |
| 192 update_rects_.begin(), update_rects_.end()); |
| 193 Paint(); |
| 194 // TODO(ajwong): Need to block here to be synchronous. |
| 195 } |
| 196 |
| 197 |
| 198 void PepperView::OnDecodeDone() { |
152 } | 199 } |
153 | 200 |
154 } // namespace remoting | 201 } // namespace remoting |
OLD | NEW |