OLD | NEW |
| (Empty) |
1 // Copyright 2010 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can | |
3 // be found in the LICENSE file. | |
4 | |
5 #include "c_salt/graphics_2d_context.h" | |
6 | |
7 #include <nacl/nacl_npapi.h> | |
8 #include <nacl/npapi_extensions.h> | |
9 | |
10 #include <algorithm> | |
11 | |
12 #include "c_salt/image.h" | |
13 #include "c_salt/instance.h" | |
14 | |
15 extern NPDevice* NPN_AcquireDevice(NPP instance, NPDeviceID device); | |
16 | |
17 namespace c_salt { | |
18 class Graphics2DContextImpl { | |
19 public: | |
20 Graphics2DContextImpl() | |
21 : npp_instance_(0), device2d_(NULL) { | |
22 memset(&context2d_, 0, sizeof(context2d_)); | |
23 } | |
24 ~Graphics2DContextImpl() { | |
25 if (NULL != device2d_) { | |
26 device2d_->destroyContext(npp_instance_, &context2d_); | |
27 device2d_ = NULL; | |
28 } | |
29 } | |
30 NPP npp_instance_; // Instance ID. | |
31 NPDevice* device2d_; // The Pepper1 2D device. | |
32 NPDeviceContext2D context2d_; // The Pepper1 2D drawing context. | |
33 }; | |
34 | |
35 Graphics2DContext::Graphics2DContext() : impl_(new Graphics2DContextImpl) { | |
36 } | |
37 | |
38 Graphics2DContext::~Graphics2DContext() { | |
39 } | |
40 | |
41 bool Graphics2DContext::Init(const c_salt::Instance& instance) { | |
42 impl_->npp_instance_ = instance.npp_instance(); | |
43 impl_->device2d_ = NPN_AcquireDevice(impl_->npp_instance_, NPPepper2DDevice); | |
44 | |
45 NPDeviceContext2DConfig config; | |
46 NPError init_err = | |
47 impl_->device2d_->initializeContext(impl_->npp_instance_, | |
48 &config, | |
49 &impl_->context2d_); | |
50 if (NPERR_NO_ERROR != init_err) | |
51 return false; | |
52 | |
53 size_ = Size(impl_->context2d_.dirty.right, impl_->context2d_.dirty.bottom); | |
54 return true; | |
55 } | |
56 | |
57 void Graphics2DContext::Update(const Image& img) { | |
58 if (NULL == impl_->device2d_) | |
59 return; | |
60 | |
61 int w = std::min(size_.width(), img.width()); | |
62 int h = std::min(size_.height(), img.height()); | |
63 | |
64 const uint32_t* img_pixels = img.PixelAddress(0, 0); | |
65 uint32_t* window_pixels = static_cast<uint32_t*>(impl_->context2d_.region); | |
66 int window_stride = impl_->context2d_.stride / sizeof(*window_pixels); | |
67 | |
68 for (int y = 0; y < h; ++y) { | |
69 memcpy(window_pixels, img_pixels, w * sizeof(*window_pixels)); | |
70 window_pixels += window_stride; | |
71 img_pixels += img.width(); | |
72 } | |
73 impl_->device2d_->flushContext(impl_->npp_instance_, | |
74 &impl_->context2d_, | |
75 NULL, | |
76 NULL); | |
77 } | |
78 } // namespace c_salt | |
79 | |
OLD | NEW |