OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef LOCKING_IMAGE_DATA_H_ | |
6 #define LOCKING_IMAGE_DATA_H_ | |
7 | |
8 #include "ppapi/cpp/image_data.h" | |
9 #include "ppapi/cpp/instance.h" | |
10 #include "ppapi/cpp/size.h" | |
11 #include "threading/pthread_ext.h" | |
12 | |
13 namespace flocking_geese { | |
14 | |
15 // An extension of ImageData that can protect direct pixel access thorugh a | |
16 // mutex lock. | |
17 class LockingImageData : public pp::ImageData { | |
18 public: | |
19 LockingImageData(pp::Instance* instance, | |
20 PP_ImageDataFormat format, | |
21 const pp::Size& size, | |
22 bool init_to_zero) | |
23 : ImageData(instance, format, size, init_to_zero) { | |
24 pthread_mutex_init(&pixel_buffer_mutex_, NULL); | |
25 } | |
26 | |
27 virtual ~LockingImageData() { | |
28 UnlockPixels(); | |
29 pthread_mutex_destroy(&pixel_buffer_mutex_); | |
30 } | |
31 | |
32 // Acquire the lock in |pixel_owner_| that governs the pixel buffer. Return | |
33 // a pointer to the locked pixel buffer if successful; return NULL otherwise. | |
34 uint32_t* LockPixels() { | |
35 uint32_t* pixels = NULL; | |
36 if (pthread_mutex_lock(&pixel_buffer_mutex_) == PTHREAD_MUTEX_SUCCESS) { | |
37 pixels = PixelBufferNoLock(); | |
38 } | |
39 return pixels; | |
40 } | |
41 | |
42 // Release the lock governing the pixel bugger in |pixel_owner_|. | |
43 void UnlockPixels() { | |
44 pthread_mutex_unlock(&pixel_buffer_mutex_); | |
45 } | |
46 | |
47 uint32_t* PixelBufferNoLock() { | |
48 return is_null() ? NULL : static_cast<uint32_t*>(data()); | |
49 } | |
50 | |
51 private: | |
52 mutable pthread_mutex_t pixel_buffer_mutex_; | |
53 }; | |
54 } // namespace flocking_geese | |
55 | |
56 #endif // LOCKING_IMAGE_DATA_H_ | |
57 | |
OLD | NEW |