Welcome to mirror list, hosted at ThFree Co, Russian Federation.

screendump.c « screen « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a701fc9ccb79b2a8816d915b433e199485c76445 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * Contributor(s): Blender Foundation
 *
 * ***** END GPL LICENSE BLOCK *****
 * Making screendumps.
 */

/** \file blender/editors/screen/screendump.c
 *  \ingroup edscr
 */


#include <string.h>

#include "MEM_guardedalloc.h"

#include "BLI_blenlib.h"
#include "BLI_utildefines.h"
#include "BLI_math.h"

#include "IMB_imbuf_types.h"
#include "IMB_imbuf.h"

#include "DNA_scene_types.h"
#include "DNA_screen_types.h"
#include "DNA_space_types.h"
#include "DNA_userdef_types.h"

#include "BKE_context.h"
#include "BKE_global.h"
#include "BKE_main.h"
#include "BKE_image.h"
#include "BKE_report.h"
#include "BKE_writeavi.h"

#include "BIF_gl.h"
#include "BIF_glutil.h"

#include "RNA_access.h"
#include "RNA_define.h"

#include "UI_interface.h"

#include "WM_types.h"
#include "WM_api.h"

#include "PIL_time.h"


#include "screen_intern.h"

typedef struct ScreenshotData {
	unsigned int *dumprect;
	int dumpsx, dumpsy;
	rcti crop;

	ImageFormatData im_format;
} ScreenshotData;

static void screenshot_read_pixels(int x, int y, int w, int h, unsigned char *rect)
{
	int i;

	glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, rect);
	glFinish();

	/* clear alpha, it is not set to a meaningful value in opengl */
	for (i = 0, rect += 3; i < w * h; i++, rect += 4)
		*rect = 255;
}

/* get shot from frontbuffer */
static unsigned int *screenshot(bContext *C, int *dumpsx, int *dumpsy)
{
	wmWindow *win = CTX_wm_window(C);
	int x = 0, y = 0;
	unsigned int *dumprect = NULL;

	x = 0;
	y = 0;
	*dumpsx = WM_window_pixels_x(win);
	*dumpsy = WM_window_pixels_y(win);

	if (*dumpsx && *dumpsy) {
		
		dumprect = MEM_mallocN(sizeof(int) * (*dumpsx) * (*dumpsy), "dumprect");
		glReadBuffer(GL_FRONT);
		screenshot_read_pixels(x, y, *dumpsx, *dumpsy, (unsigned char *)dumprect);
		glReadBuffer(GL_BACK);
	}

	return dumprect;
}

/* call from both exec and invoke */
static int screenshot_data_create(bContext *C, wmOperator *op)
{
	unsigned int *dumprect;
	int dumpsx, dumpsy;

	/* do redraw so we don't show popups/menus */
	WM_redraw_windows(C);
	
	dumprect = screenshot(C, &dumpsx, &dumpsy);

	if (dumprect) {
		ScreenshotData *scd = MEM_callocN(sizeof(ScreenshotData), "screenshot");
		ScrArea *sa = CTX_wm_area(C);
		
		scd->dumpsx = dumpsx;
		scd->dumpsy = dumpsy;
		scd->dumprect = dumprect;
		if (sa) {
			scd->crop = sa->totrct;
		}

		BKE_imformat_defaults(&scd->im_format);

		op->customdata = scd;

		return true;
	}
	else {
		op->customdata = NULL;
		return false;
	}
}

static void screenshot_data_free(wmOperator *op)
{
	ScreenshotData *scd = op->customdata;

	if (scd) {
		if (scd->dumprect)
			MEM_freeN(scd->dumprect);
		MEM_freeN(scd);
		op->customdata = NULL;
	}
}

static void screenshot_crop(ImBuf *ibuf, rcti crop)
{
	unsigned int *to = ibuf->rect;
	unsigned int *from = ibuf->rect + crop.ymin * ibuf->x + crop.xmin;
	int crop_x = BLI_rcti_size_x(&crop);
	int crop_y = BLI_rcti_size_y(&crop);
	int y;

	if (crop_x > 0 && crop_y > 0) {
		for (y = 0; y < crop_y; y++, to += crop_x, from += ibuf->x)
			memmove(to, from, sizeof(unsigned int) * crop_x);

		ibuf->x = crop_x;
		ibuf->y = crop_y;
	}
}

static int screenshot_exec(bContext *C, wmOperator *op)
{
	ScreenshotData *scd = op->customdata;

	if (scd == NULL) {
		/* when running exec directly */
		screenshot_data_create(C, op);
		scd = op->customdata;
	}

	if (scd) {
		if (scd->dumprect) {
			ImBuf *ibuf;
			char path[FILE_MAX];

			RNA_string_get(op->ptr, "filepath", path);
			BLI_path_abs(path, G.main->name);

			/* operator ensures the extension */
			ibuf = IMB_allocImBuf(scd->dumpsx, scd->dumpsy, 24, 0);
			ibuf->rect = scd->dumprect;

			/* crop to show only single editor */
			if (!RNA_boolean_get(op->ptr, "full"))
				screenshot_crop(ibuf, scd->crop);

			if (scd->im_format.planes == R_IMF_PLANES_BW) {
				/* bw screenshot? - users will notice if it fails! */
				IMB_color_to_bw(ibuf);
			}
			BKE_imbuf_write(ibuf, path, &scd->im_format);

			IMB_freeImBuf(ibuf);
		}
	}

	screenshot_data_free(op);
	return OPERATOR_FINISHED;
}

static int screenshot_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
{
	if (screenshot_data_create(C, op)) {
		if (RNA_struct_property_is_set(op->ptr, "filepath"))
			return screenshot_exec(C, op);

		/* extension is added by 'screenshot_check' after */
		RNA_string_set(op->ptr, "filepath", G.relbase_valid ? G.main->name : "//screen");
		
		WM_event_add_fileselect(C, op);
	
		return OPERATOR_RUNNING_MODAL;
	}
	return OPERATOR_CANCELLED;
}

static bool screenshot_check(bContext *UNUSED(C), wmOperator *op)
{
	ScreenshotData *scd = op->customdata;
	return WM_operator_filesel_ensure_ext_imtype(op, &scd->im_format);
}

static void screenshot_cancel(bContext *UNUSED(C), wmOperator *op)
{
	screenshot_data_free(op);
}

static bool screenshot_draw_check_prop(PointerRNA *UNUSED(ptr), PropertyRNA *prop)
{
	const char *prop_id = RNA_property_identifier(prop);

	return !(STREQ(prop_id, "filepath"));
}

static void screenshot_draw(bContext *UNUSED(C), wmOperator *op)
{
	uiLayout *layout = op->layout;
	ScreenshotData *scd = op->customdata;
	PointerRNA ptr;

	/* image template */
	RNA_pointer_create(NULL, &RNA_ImageFormatSettings, &scd->im_format, &ptr);
	uiTemplateImageSettings(layout, &ptr, false);

	/* main draw call */
	RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
	uiDefAutoButsRNA(layout, &ptr, screenshot_draw_check_prop, '\0');
}

static int screenshot_poll(bContext *C)
{
	if (G.background)
		return false;

	return WM_operator_winactive(C);
}

void SCREEN_OT_screenshot(wmOperatorType *ot)
{
	ot->name = "Save Screenshot"; /* weak: opname starting with 'save' makes filewindow give save-over */
	ot->idname = "SCREEN_OT_screenshot";
	ot->description = "Capture a picture of the active area or whole Blender window";
	
	ot->invoke = screenshot_invoke;
	ot->check = screenshot_check;
	ot->exec = screenshot_exec;
	ot->cancel = screenshot_cancel;
	ot->ui = screenshot_draw;
	ot->poll = screenshot_poll;
	
	ot->flag = 0;
	
	WM_operator_properties_filesel(ot, FILE_TYPE_FOLDER | FILE_TYPE_IMAGE, FILE_SPECIAL, FILE_SAVE,
	                               WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
	RNA_def_boolean(ot->srna, "full", 1, "Full Screen",
	                "Capture the whole window (otherwise only capture the active area)");
}

/* *************** screenshot movie job ************************* */

typedef struct ScreenshotJob {
	Main *bmain;
	Scene *scene;
	wmWindowManager *wm;
	unsigned int *dumprect;
	int x, y, dumpsx, dumpsy;
	const short *stop;
	const short *do_update;
	ReportList reports;

	bMovieHandle *movie_handle;
	void *movie_ctx;
} ScreenshotJob;


static void screenshot_freejob(void *sjv)
{
	ScreenshotJob *sj = sjv;
	
	if (sj->dumprect)
		MEM_freeN(sj->dumprect);

	if (sj->movie_handle) {
		bMovieHandle *mh = sj->movie_handle;
		mh->end_movie(sj->movie_ctx);
		mh->context_free(sj->movie_ctx);
	}

	MEM_freeN(sj);
}


/* called before redraw notifiers, copies a new dumprect */
static void screenshot_updatejob(void *sjv)
{
	ScreenshotJob *sj = sjv;
	unsigned int *dumprect;
	
	if (sj->dumprect == NULL) {
		dumprect = MEM_mallocN(sizeof(int) * sj->dumpsx * sj->dumpsy, "dumprect");
		screenshot_read_pixels(sj->x, sj->y, sj->dumpsx, sj->dumpsy, (unsigned char *)dumprect);
		
		sj->dumprect = dumprect;
	}
}


/* only this runs inside thread */
static void screenshot_startjob(void *sjv, short *stop, short *do_update, float *UNUSED(progress))
{
	ScreenshotJob *sj = sjv;
	RenderData rd = sj->scene->r;
	bMovieHandle *mh = NULL;

	/* we need this as local variables for renderdata */
	rd.frs_sec = U.scrcastfps;
	rd.frs_sec_base = 1.0f;
	
	if (BKE_imtype_is_movie(rd.im_format.imtype)) {
		mh = BKE_movie_handle_get(sj->scene->r.im_format.imtype);
		if (mh == NULL) {
			printf("Movie format unsupported\n");
			return;
		}
		sj->movie_ctx = mh->context_create();
		sj->movie_handle = mh;

		if (!mh->start_movie(sj->movie_ctx, sj->scene, &rd, sj->dumpsx, sj->dumpsy, &sj->reports, false, "")) {
			printf("screencast job stopped\n");
			return;
		}
	}
	
	sj->stop = stop;
	sj->do_update = do_update;
	
	*do_update = true; /* wait for opengl rect */
	
	while (*stop == 0) {
		
		if (sj->dumprect) {
			
			if (mh) {
				if (mh->append_movie(sj->movie_ctx, &rd, rd.sfra, rd.cfra, (int *)sj->dumprect,
				                     sj->dumpsx, sj->dumpsy, "", &sj->reports))
				{
					BKE_reportf(&sj->reports, RPT_INFO, "Appended frame: %d", rd.cfra);
					printf("Appended frame %d\n", rd.cfra);
				}
				else {
					break;
				}
			}
			else {
				ImBuf *ibuf = IMB_allocImBuf(sj->dumpsx, sj->dumpsy, rd.im_format.planes, 0);
				char name[FILE_MAX];
				int ok;
				
				BKE_image_path_from_imformat(
				        name, rd.pic, sj->bmain->name, rd.cfra,
				        &rd.im_format, (rd.scemode & R_EXTENSION) != 0, true, NULL);
				
				ibuf->rect = sj->dumprect;
				ok = BKE_imbuf_write(ibuf, name, &rd.im_format);
				
				if (ok == 0) {
					printf("Write error: cannot save %s\n", name);
					BKE_reportf(&sj->reports, RPT_INFO, "Write error: cannot save %s", name);
					break;
				}
				else {
					printf("Saved file: %s\n", name);
					BKE_reportf(&sj->reports, RPT_INFO, "Saved file: %s", name);
				}
				
				/* imbuf knows which rects are not part of ibuf */
				IMB_freeImBuf(ibuf);
			}
			
			MEM_freeN(sj->dumprect);
			sj->dumprect = NULL;
			
			*do_update = true;
			
			rd.cfra++;

		}
		else 
			PIL_sleep_ms(U.scrcastwait);
	}
	
	if (mh) {
		mh->end_movie(sj->movie_ctx);
		mh->context_free(sj->movie_ctx);
		sj->movie_handle = NULL;
	}

	BKE_report(&sj->reports, RPT_INFO, "Screencast job stopped");
}

/* Helper callback for drawing the cursor itself */
static void screencast_draw_cursor(bContext *UNUSED(C), int x, int y, void *UNUSED(p_ptr))
{
	
	glPushMatrix();
	
	glTranslatef((float)x, (float)y, 0.0f);
	
	
	glEnable(GL_LINE_SMOOTH);
	glEnable(GL_BLEND);
	
	glColor4ub(0, 0, 0, 32);
	glutil_draw_filled_arc(0.0, M_PI * 2.0, 20, 40);
	
	glColor4ub(255, 255, 255, 128);
	glutil_draw_lined_arc(0.0, M_PI * 2.0, 20, 40);
	
	glDisable(GL_BLEND);
	glDisable(GL_LINE_SMOOTH);
	
	glPopMatrix();
}

/* Turn brush cursor in 3D view on/off */
static void screencast_cursor_toggle(wmWindowManager *wm, short enable)
{
	static void *cursor = NULL;
	
	if (cursor && !enable) {
		/* clear cursor */
		WM_paint_cursor_end(wm, cursor);
		cursor = NULL;
	}
	else if (enable) {
		/* enable cursor */
		cursor = WM_paint_cursor_activate(wm, NULL, screencast_draw_cursor, NULL);
	}
}

static void screenshot_endjob(void *sjv)
{
	ScreenshotJob *sj = sjv;
	
	screencast_cursor_toggle(sj->wm, 0);
}


static int screencast_exec(bContext *C, wmOperator *op)
{
	wmWindowManager *wm = CTX_wm_manager(C);
	wmWindow *win = CTX_wm_window(C);
	bScreen *screen = CTX_wm_screen(C);
	wmJob *wm_job;
	ScreenshotJob *sj;

	/* if called again, stop the running job */
	if (WM_jobs_test(wm, screen, WM_JOB_TYPE_SCREENCAST))
		WM_jobs_stop(wm, screen, screenshot_startjob);
	
	wm_job = WM_jobs_get(wm, win, screen, "Screencast", 0, WM_JOB_TYPE_SCREENCAST);
	sj = MEM_callocN(sizeof(ScreenshotJob), "screenshot job");
	
	/* setup sj */
	if (RNA_boolean_get(op->ptr, "full")) {
		sj->x = 0;
		sj->y = 0;
		sj->dumpsx = WM_window_pixels_x(win);
		sj->dumpsy = WM_window_pixels_y(win);
	}
	else {
		ScrArea *curarea = CTX_wm_area(C);
		sj->x = curarea->totrct.xmin;
		sj->y = curarea->totrct.ymin;
		sj->dumpsx = curarea->totrct.xmax - sj->x;
		sj->dumpsy = curarea->totrct.ymax - sj->y;
	}
	sj->bmain = CTX_data_main(C);
	sj->scene = CTX_data_scene(C);
	sj->wm = wm;
	
	BKE_reports_init(&sj->reports, RPT_PRINT);

	/* setup job */
	WM_jobs_customdata_set(wm_job, sj, screenshot_freejob);
	WM_jobs_timer(wm_job, 0.1, 0, NC_SCREEN | ND_SCREENCAST);
	WM_jobs_callbacks(wm_job, screenshot_startjob, NULL, screenshot_updatejob, screenshot_endjob);
	
	WM_jobs_start(sj->wm, wm_job);
	
	screencast_cursor_toggle(sj->wm, 1);
	
	WM_event_add_notifier(C, NC_SCREEN | ND_SCREENCAST, screen);
	
	return OPERATOR_FINISHED;
}

void SCREEN_OT_screencast(wmOperatorType *ot)
{
	ot->name = "Make Screencast";
	ot->idname = "SCREEN_OT_screencast";
	ot->description = "Capture a video of the active area or whole Blender window";
	
	ot->invoke = WM_operator_confirm;
	ot->exec = screencast_exec;
	ot->poll = screenshot_poll;  /* shared poll */
	
	ot->flag = 0;
	
	RNA_def_property(ot->srna, "filepath", PROP_STRING, PROP_FILEPATH);
	RNA_def_boolean(ot->srna, "full", 1, "Full Screen",
	                "Capture the whole window (otherwise only capture the active area)");
}