shuttlerc, boobytraps, vwindow hang, lock cleanup
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / bcbitmap.h
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #ifndef __BCBITMAP_H__
23 #define __BCBITMAP_H__
24
25 #include <stdint.h>
26 #include <X11/Xlib.h>
27 #include <sys/ipc.h>
28 #include <sys/shm.h>
29 #include <X11/extensions/XShm.h>
30 #include <X11/extensions/Xvlib.h>
31
32 #include "bcwindowbase.inc"
33 #include "bcbitmap.inc"
34 #include "bccmodels.h"
35 #include "bccolors.h"
36 #include "condition.h"
37 #include "linklist.h"
38 #include "mutex.h"
39 #include "sizes.h"
40 #include "vframe.inc"
41
42 #define MIN_BITMAP_BUFFERS 4
43 #define MAX_BITMAP_BUFFERS 32
44
45
46
47 class BC_BitmapImage : public ListItem<BC_BitmapImage> {
48         int index;
49         BC_Bitmap *bitmap;
50         union { XImage *ximage; XvImage *xv_image; };
51         BC_WindowBase *top_level;
52         Drawable drawable;
53         unsigned char *data;
54         unsigned char **row_data;
55         int bitsPerPixel;
56         long bytesPerLine;
57         long dataSize;
58         friend class BC_Bitmap;
59         friend class BC_XImage;
60         friend class BC_XShmImage;
61         friend class BC_XvImage;
62         friend class BC_XvShmImage;
63         friend class BC_ActiveBitmaps;
64 protected:
65         int read_frame_rgb(VFrame* frame);
66 public:
67         BC_BitmapImage(BC_Bitmap *bitmap, int index);
68         virtual ~BC_BitmapImage();
69         unsigned char *get_data() { return data; }
70         unsigned char **get_row_data() { return row_data; }
71         virtual long get_data_size() { return dataSize; }
72         int bits_per_pixel() { return bitsPerPixel; }
73         long bytes_per_line() { return bytesPerLine; }
74         virtual long xv_offset(int i) { return 0; }
75         virtual unsigned char* xv_plane(int i) { return 0; }
76         virtual int get_shmid() { return 0; }
77         virtual ShmSeg get_shmseg() { return 0; }
78         long get_y_offset() { return xv_offset(0); }
79         long get_u_offset() { return xv_offset(2); }
80         long get_v_offset() { return xv_offset(1); }
81         unsigned char *get_y_data() { return xv_plane(0); }
82         unsigned char *get_u_data() { return xv_plane(2); }
83         unsigned char *get_v_data() { return xv_plane(1); }
84         virtual int write_drawable(Drawable &pixmap, GC &gc,
85                 int source_x, int source_y, int source_w, int source_h,
86                 int dest_x, int dest_y, int dest_w, int dest_h) {
87                 return 0;
88         }
89         virtual int read_drawable(Drawable &pixmap, int source_x, int source_y) {
90                 return 0;
91         }
92         bool is_avail();
93         bool is_zombie() { return index < 0; }
94 };
95
96
97 class BC_XImage : public BC_BitmapImage {
98 public:
99         BC_XImage(BC_Bitmap *bitmap, int idx, int w,int h, int color_model);
100         ~BC_XImage();
101         int write_drawable(Drawable &pixmap, GC &gc,
102                 int source_x, int source_y, int source_w, int source_h,
103                 int dest_x, int dest_y, int dest_w, int dest_h);
104         int read_drawable(Drawable &pixmap, int source_x, int source_y);
105 };
106
107 class BC_XShmImage : public BC_BitmapImage {
108         XShmSegmentInfo shm_info;
109         long shm_offset(int i) { return 0; }
110         //long shm_offset(int i) {
111         //  return (h*ximage->bytes_per_line) * BC_BitmapImage::index;
112         //}
113 public:
114         BC_XShmImage(BC_Bitmap *bitmap, int idx, int w,int h, int color_model);
115         ~BC_XShmImage();
116         int get_shmid() { return shm_info.shmid; }
117         ShmSeg get_shmseg() { return shm_info.shmseg; }
118         int get_shm_size();
119         int write_drawable(Drawable &pixmap, GC &gc,
120                 int source_x, int source_y, int source_w, int source_h,
121                 int dest_x, int dest_y, int dest_w, int dest_h);
122         int read_drawable(Drawable &pixmap, int source_x, int source_y);
123 };
124
125 class BC_XvImage : public BC_BitmapImage {
126         long xv_offset(int i) { return xv_image->offsets[i]; }
127         unsigned char* xv_plane(int i) { return get_data() + xv_offset(i); }
128 public:
129         BC_XvImage(BC_Bitmap *bitmap, int idx, int w,int h, int color_model);
130         ~BC_XvImage();
131         int write_drawable(Drawable &pixmap, GC &gc,
132                 int source_x, int source_y, int source_w, int source_h,
133                 int dest_x, int dest_y, int dest_w, int dest_h);
134 };
135
136 class BC_XvShmImage : public BC_BitmapImage {
137         XShmSegmentInfo shm_info;
138         long shm_offset(int i) { return 0; }
139         //long shm_offset(int i) {
140         //      return xv_image->data_size*BC_BitmapImage::index;
141         //}
142         long xv_offset(int i) { return shm_offset(i) + xv_image->offsets[i]; }
143         unsigned char* xv_plane(int i) { return data + xv_offset(i); }
144 public:
145         BC_XvShmImage(BC_Bitmap *bitmap, int idx, int w,int h, int color_model);
146         ~BC_XvShmImage();
147         int get_shmid() { return shm_info.shmid; }
148         ShmSeg get_shmseg() { return shm_info.shmseg; }
149         int get_shm_size() { return xv_image->data_size; }
150         int write_drawable(Drawable &pixmap, GC &gc,
151                 int source_x, int source_y, int source_w, int source_h,
152                 int dest_x, int dest_y, int dest_w, int dest_h);
153 };
154
155
156
157 class BC_Bitmap
158 {
159         friend class BC_XImage;
160         friend class BC_XShmImage;
161         friend class BC_XvImage;
162         friend class BC_XvShmImage;
163         friend class BC_BitmapImage;
164         friend class BC_ActiveBitmaps;
165         int buffer_count, max_buffer_count;
166         int active_buffers;
167         static int max_active_buffers;
168         static int zombies;
169         int w, h;
170         int shm_reply, type;
171         int color_model;
172         int initialize(BC_WindowBase *parent_window, int w, int h, int color_model, int use_shm);
173         BC_BitmapImage *new_buffer(int type, int idx);
174         void update_buffers(int count, int lock_avail=1);
175         int allocate_data();
176         int delete_data();
177         int get_default_depth();
178         long best_buffer_size();
179         int need_shm();
180
181 // Override top_level for small bitmaps
182         int use_shm;
183         BC_WindowBase *top_level;
184         BC_WindowBase *parent_window;
185         int xv_portid;
186
187         static uint8_t bitswap[256];
188         void transparency_bitswap(uint8_t *buf, int w, int h);
189 public:
190         enum { bmXNone, bmXImage, bmXShmImage, bmXvImage, bmXvShmImage };
191
192         BC_Bitmap(BC_WindowBase *parent_window, unsigned char *png_data, double scale=1);
193         BC_Bitmap(BC_WindowBase *parent_window, VFrame *frame);
194
195 // Shared memory is a problem in X because it's asynchronous and there's
196 // no easy way to join with the blitting process.
197         BC_Bitmap(BC_WindowBase *parent_window, int w, int h,
198                 int color_model, int use_shm = 1);
199         virtual ~BC_Bitmap();
200
201 // transfer VFrame
202         int read_frame(VFrame *frame,
203                 int in_x, int in_y, int in_w, int in_h,
204                 int out_x, int out_y, int out_w, int out_h,
205                 int bg_color);
206 // x1, y1, x2, y2 dimensions of output area
207         int read_frame(VFrame *frame,
208                 int x1, int y1, int x2, int y2);
209         int read_frame(VFrame *frame,
210                 int x1, int y1, int x2, int y2, int bg_color);
211 // Reset bitmap to match the new parameters
212         int match_params(int w, int h, int color_model, int use_shm);
213 // Test if bitmap already matches parameters
214         int params_match(int w, int h, int color_model, int use_shm);
215
216 // If dont_wait is true, the XSync comes before the flash.
217 // For YUV bitmaps, the image is scaled to fill dest_x ... w * dest_y ... h
218         int write_drawable(Drawable &pixmap, GC &gc,
219                         int source_x, int source_y, int source_w, int source_h,
220                         int dest_x, int dest_y, int dest_w, int dest_h,
221                         int dont_wait);
222         int write_drawable(Drawable &pixmap, GC &gc,
223                         int dest_x, int dest_y, int source_x, int source_y,
224                         int dest_w, int dest_h, int dont_wait);
225 // the bitmap must be wholly contained in the source during a GetImage
226         int read_drawable(Drawable &pixmap, int source_x, int source_y, VFrame *frame=0);
227
228         int rotate_90(int side);
229 // Data pointers for current ring buffer
230         BC_BitmapImage **buffers;
231         Mutex *avail_lock;
232         List<BC_BitmapImage> avail;
233         void reque(BC_BitmapImage *bfr);
234         BC_BitmapImage *cur_bfr(), *active_bfr;
235         unsigned char* get_data() { return cur_bfr()->get_data(); }
236         unsigned char** get_row_pointers() { return cur_bfr()->get_row_data(); }
237         long get_data_size() { return cur_bfr()->get_data_size(); }
238         int get_shmid() { return cur_bfr()->get_shmid(); }
239         unsigned char *get_y_plane() { return cur_bfr()->get_y_data(); }
240         unsigned char *get_u_plane() { return cur_bfr()->get_u_data(); }
241         unsigned char *get_v_plane() { return cur_bfr()->get_v_data(); }
242         long get_y_offset() { return cur_bfr()->get_y_offset(); }
243         long get_u_offset() { return cur_bfr()->get_u_offset(); }
244         long get_v_offset() { return cur_bfr()->get_v_offset(); }
245         int get_color_model() { return color_model; }
246         int hardware_scaling() {
247                 return xv_portid < 0 ? 0 :
248                         (get_color_model() == BC_YUV420P ||
249         //              get_color_model() == BC_YUV422P ||  not in bc_to_x
250                         get_color_model() == BC_YUV422) ? 1 : 0;
251         }
252         int get_w() { return w; }
253         int get_h() { return h; }
254
255         int get_image_type() { return type; }
256         int is_xvideo() { return type==bmXvShmImage || type==bmXvImage; }
257         int is_xwindow() { return type==bmXShmImage || type==bmXImage; }
258         int is_shared() { return type==bmXvShmImage || type==bmXShmImage; }
259         int is_unshared() { return type==bmXvImage  || type==bmXImage; }
260         int is_zombie() { return cur_bfr()->is_zombie(); }
261
262         int invert();
263 };
264
265 #endif