d5cd82855157f21cf46622d381d5398c8f6ec730
[blender-staging.git] / source / blender / editors / screen / screendump.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (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 Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  * Making screendumps.
27  */
28
29 #include <string.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "BLI_blenlib.h"
34
35 #include "IMB_imbuf_types.h"
36 #include "IMB_imbuf.h"
37
38 #include "DNA_scene_types.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_space_types.h"
41 #include "DNA_userdef_types.h"
42
43 #include "BKE_context.h"
44 #include "BKE_global.h"
45 #include "BKE_image.h"
46 #include "BKE_report.h"
47 #include "BKE_utildefines.h"
48 #include "BKE_writeavi.h"
49
50 #include "BIF_gl.h"
51
52 #include "RNA_access.h"
53 #include "RNA_define.h"
54
55 #include "WM_types.h"
56 #include "WM_api.h"
57
58 #include "PIL_time.h"
59
60 #include "ED_screen_types.h"
61
62 #include "screen_intern.h"
63
64 typedef struct ScreenshotData {
65         unsigned int *dumprect;
66         int dumpsx, dumpsy;
67 } ScreenshotData;
68
69 static int screenshot_exec(bContext *C, wmOperator *op)
70 {
71         ScreenshotData *scd= op->customdata;
72         
73         if(scd && scd->dumprect) {
74                 Scene *scene= CTX_data_scene(C);
75                 ImBuf *ibuf;
76                 char path[FILE_MAX];
77         
78                 RNA_string_get(op->ptr, "filepath", path);
79         
80                 strcpy(G.ima, path);
81                 BLI_path_abs(path, G.sce);
82                 
83                 /* BKE_add_image_extension() checks for if extension was already set */
84                 if(scene->r.scemode & R_EXTENSION) 
85                         if(strlen(path)<FILE_MAXDIR+FILE_MAXFILE-5)
86                                 BKE_add_image_extension(path, scene->r.imtype);
87                 
88                 ibuf= IMB_allocImBuf(scd->dumpsx, scd->dumpsy, 24, 0, 0);
89                 ibuf->rect= scd->dumprect;
90                 
91                 BKE_write_ibuf(scene, ibuf, path, scene->r.imtype, scene->r.subimtype, scene->r.quality);
92
93                 IMB_freeImBuf(ibuf);
94
95                 MEM_freeN(scd->dumprect);
96                 MEM_freeN(scd);
97                 op->customdata= NULL;
98         }
99         return OPERATOR_FINISHED;
100 }
101
102 /* get shot from frontbuffer */
103 static unsigned int *screenshot(bContext *C, int *dumpsx, int *dumpsy, int fscreen)
104 {
105         wmWindow *win= CTX_wm_window(C);
106         ScrArea *curarea= CTX_wm_area(C);
107         int x=0, y=0;
108         unsigned int *dumprect= NULL;
109         
110         if(fscreen) {   /* full screen */
111                 x= 0;
112                 y= 0;
113                 *dumpsx= win->sizex;
114                 *dumpsy= win->sizey;
115         } 
116         else {
117                 x= curarea->totrct.xmin;
118                 y= curarea->totrct.ymin;
119                 *dumpsx= curarea->totrct.xmax-x;
120                 *dumpsy= curarea->totrct.ymax-y;
121         }
122
123         if (*dumpsx && *dumpsy) {
124                 
125                 dumprect= MEM_mallocN(sizeof(int) * (*dumpsx) * (*dumpsy), "dumprect");
126                 glReadBuffer(GL_FRONT);
127                 glReadPixels(x, y, *dumpsx, *dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
128                 glFinish();
129                 glReadBuffer(GL_BACK);
130         }
131
132         return dumprect;
133 }
134
135
136 static int screenshot_invoke(bContext *C, wmOperator *op, wmEvent *event)
137 {
138         unsigned int *dumprect;
139         int dumpsx, dumpsy;
140         
141         dumprect= screenshot(C, &dumpsx, &dumpsy, RNA_boolean_get(op->ptr, "full"));
142         if(dumprect) {
143                 ScreenshotData *scd= MEM_callocN(sizeof(ScreenshotData), "screenshot");
144                 
145                 scd->dumpsx= dumpsx;
146                 scd->dumpsy= dumpsy;
147                 scd->dumprect= dumprect;
148                 op->customdata= scd;
149                 
150                 if(RNA_property_is_set(op->ptr, "filepath"))
151                         return screenshot_exec(C, op);
152                 
153                 RNA_string_set(op->ptr, "filepath", G.ima);
154                 
155                 WM_event_add_fileselect(C, op);
156         
157                 return OPERATOR_RUNNING_MODAL;
158         }       
159         return OPERATOR_CANCELLED;
160 }
161
162
163 void SCREEN_OT_screenshot(wmOperatorType *ot)
164 {
165         ot->name= "Save Screenshot"; /* weak: opname starting with 'save' makes filewindow give save-over */
166         ot->idname= "SCREEN_OT_screenshot";
167         
168         ot->invoke= screenshot_invoke;
169         ot->exec= screenshot_exec;
170         ot->poll= WM_operator_winactive;
171         
172         ot->flag= 0;
173         
174         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH);
175         RNA_def_boolean(ot->srna, "full", 1, "Full Screen", "");
176 }
177
178 /* *************** screenshot movie job ************************* */
179
180 typedef struct ScreenshotJob {
181         Scene *scene;
182         unsigned int *dumprect;
183         int x, y, dumpsx, dumpsy;
184         short *stop;
185         short *do_update;
186         ReportList reports;
187 } ScreenshotJob;
188
189
190 static void screenshot_freejob(void *sjv)
191 {
192         ScreenshotJob *sj= sjv;
193         
194         if(sj->dumprect)
195                 MEM_freeN(sj->dumprect);
196         
197         MEM_freeN(sj);
198 }
199
200
201 /* called before redraw notifiers, copies a new dumprect */
202 static void screenshot_updatejob(void *sjv)
203 {
204         ScreenshotJob *sj= sjv;
205         unsigned int *dumprect;
206         
207         if(sj->dumprect==NULL) {
208                 dumprect= MEM_mallocN(sizeof(int) * sj->dumpsx * sj->dumpsy, "dumprect");
209                 glReadPixels(sj->x, sj->y, sj->dumpsx, sj->dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
210                 glFinish();
211                 
212                 sj->dumprect= dumprect;
213         }
214 }
215
216
217 /* only this runs inside thread */
218 static void screenshot_startjob(void *sjv, short *stop, short *do_update, float *progress)
219 {
220         ScreenshotJob *sj= sjv;
221         RenderData rd= sj->scene->r;
222         bMovieHandle *mh= BKE_get_movie_handle(sj->scene->r.imtype);
223         int cfra= 1;
224         
225         /* we need this as local variables for renderdata */
226         rd.frs_sec= U.scrcastfps;
227         rd.frs_sec_base= 1.0f;
228         
229         if(BKE_imtype_is_movie(rd.imtype)) {
230                 if(!mh->start_movie(sj->scene, &rd, sj->dumpsx, sj->dumpsy, &sj->reports)) {
231                         printf("screencast job stopped\n");
232                         return;
233                 }
234         }
235         else
236                 mh= NULL;
237         
238         sj->stop= stop;
239         sj->do_update= do_update;
240         
241         *do_update= 1; // wait for opengl rect
242         
243         while(*stop==0) {
244                 
245                 if(sj->dumprect) {
246                         
247                         if(mh) {
248                                 if(mh->append_movie(&rd, cfra, (int *)sj->dumprect, sj->dumpsx, sj->dumpsy, &sj->reports)) {
249                                         BKE_reportf(&sj->reports, RPT_INFO, "Appended frame: %d", cfra);
250                                         printf("Appended frame %d\n", cfra);
251                                 } else
252                                         break;
253                         }
254                         else {
255                                 ImBuf *ibuf= IMB_allocImBuf(sj->dumpsx, sj->dumpsy, rd.planes, 0, 0);
256                                 char name[FILE_MAXDIR+FILE_MAXFILE];
257                                 int ok;
258                                 
259                                 BKE_makepicstring(name, rd.pic, cfra, rd.imtype, rd.scemode & R_EXTENSION);
260                                 
261                                 ibuf->rect= sj->dumprect;
262                                 ok= BKE_write_ibuf(sj->scene, ibuf, name, rd.imtype, rd.subimtype, rd.quality);
263                                 
264                                 if(ok==0) {
265                                         printf("Write error: cannot save %s\n", name);
266                                         BKE_reportf(&sj->reports, RPT_INFO, "Write error: cannot save %s\n", name);
267                                         break;
268                                 }
269                                 else {
270                                         printf("Saved file: %s\n", name);
271                                         BKE_reportf(&sj->reports, RPT_INFO, "Saved file: %s", name);
272                                 }
273                                 
274                                 /* imbuf knows which rects are not part of ibuf */
275                                 IMB_freeImBuf(ibuf);    
276                         }
277                         
278                         MEM_freeN(sj->dumprect);
279                         sj->dumprect= NULL;
280                         
281                         *do_update= 1;
282                         
283                         cfra++;
284
285                 }
286                 else 
287                         PIL_sleep_ms(U.scrcastwait);
288         }
289         
290         if(mh)
291                 mh->end_movie();
292
293         BKE_report(&sj->reports, RPT_INFO, "Screencast job stopped");
294 }
295
296 static int screencast_exec(bContext *C, wmOperator *op)
297 {
298         bScreen *screen= CTX_wm_screen(C);
299         wmJob *steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), screen, "Screencast", 0);
300         ScreenshotJob *sj= MEM_callocN(sizeof(ScreenshotJob), "screenshot job");
301
302         /* setup sj */
303         if(RNA_boolean_get(op->ptr, "full")) {
304                 wmWindow *win= CTX_wm_window(C);
305                 sj->x= 0;
306                 sj->y= 0;
307                 sj->dumpsx= win->sizex;
308                 sj->dumpsy= win->sizey;
309         } 
310         else {
311                 ScrArea *curarea= CTX_wm_area(C);
312                 sj->x= curarea->totrct.xmin;
313                 sj->y= curarea->totrct.ymin;
314                 sj->dumpsx= curarea->totrct.xmax - sj->x;
315                 sj->dumpsy= curarea->totrct.ymax - sj->y;
316         }
317         sj->scene= CTX_data_scene(C);
318
319         BKE_reports_init(&sj->reports, RPT_PRINT);
320
321         /* setup job */
322         WM_jobs_customdata(steve, sj, screenshot_freejob);
323         WM_jobs_timer(steve, 0.1, 0, NC_SCREEN|ND_SCREENCAST);
324         WM_jobs_callbacks(steve, screenshot_startjob, NULL, screenshot_updatejob, NULL);
325         
326         WM_jobs_start(CTX_wm_manager(C), steve);
327         
328         WM_event_add_notifier(C, NC_SCREEN|ND_SCREENCAST, screen);
329         
330         return OPERATOR_FINISHED;
331 }
332
333 void SCREEN_OT_screencast(wmOperatorType *ot)
334 {
335         ot->name= "Make Screencast";
336         ot->idname= "SCREEN_OT_screencast";
337         
338         ot->invoke= WM_operator_confirm;
339         ot->exec= screencast_exec;
340         ot->poll= WM_operator_winactive;
341         
342         ot->flag= 0;
343         
344         RNA_def_property(ot->srna, "filepath", PROP_STRING, PROP_FILEPATH);
345         RNA_def_boolean(ot->srna, "full", 1, "Full Screen", "");
346 }
347
348
349