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