1 /* === S Y N F I G ========================================================= */
2 /*! \file synfig/rendering/surface.cpp
3 ** \brief Surface
4 **
5 ** $Id$
6 **
7 ** \legal
8 ** ......... ... 2015 Ivan Mahonin
9 **
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
14 **
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
19 ** \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 # include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31
32 #ifndef _WIN32
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <signal.h>
36 #endif
37
38 #include "surface.h"
39
40 #include "common/surfacememoryreadwrapper.h"
41
42 #endif
43
44 using namespace synfig;
45 using namespace rendering;
46
47 /* === M A C R O S ========================================================= */
48
49 /* === G L O B A L S ======================================================= */
50
51 /* === P R O C E D U R E S ================================================= */
52
53 /* === M E T H O D S ======================================================= */
54
Surface()55 rendering::Surface::Surface():
56 width(0),
57 height(0),
58 created(false),
59 is_temporary(false)
60 { }
61
~Surface()62 rendering::Surface::~Surface() { destroy(); }
63
64 void
mark_as_created(bool created)65 rendering::Surface::mark_as_created(bool created)
66 { this->created = created; }
67
68 void
set_size(int width,int height)69 rendering::Surface::set_size(int width, int height)
70 {
71 assert(!is_created());
72 unset_alternative();
73 this->width = width > 0 ? width : 0;
74 this->height = height > 0 ? height : 0;
75 }
76
77 bool
create()78 rendering::Surface::create()
79 {
80 unset_alternative();
81 if (!is_created() && !empty())
82 created = create_vfunc();
83 return is_created();
84 }
85
86
87 bool
assign(const Color * buffer)88 rendering::Surface::assign(const Color *buffer)
89 {
90 return assign(buffer, get_width(), get_height());
91 }
92
93 bool
assign(const Color * buffer,int width,int height)94 rendering::Surface::assign(const Color *buffer, int width, int height)
95 {
96 return assign(SurfaceMemoryReadWrapper(buffer, width, height));
97 }
98
99 bool
assign(const Surface & surface)100 rendering::Surface::assign(const Surface &surface)
101 {
102 destroy();
103 unset_alternative();
104 set_size(surface.get_width(), surface.get_height());
105 created = assign_vfunc(surface);
106 return is_created();
107 }
108
109
110 bool
assign(const Handle & surface)111 rendering::Surface::assign(const Handle &surface)
112 {
113 if (surface)
114 return assign(*surface);
115 destroy();
116 return false;
117 }
118
119 void
destroy()120 rendering::Surface::destroy()
121 {
122 if (is_created())
123 {
124 unset_alternative();
125 destroy_vfunc();
126 created = false;
127 }
128 }
129
130 bool
empty() const131 rendering::Surface::empty() const
132 {
133 return width <= 0 || height <= 0;
134 }
135
136 size_t
get_buffer_size() const137 rendering::Surface::get_buffer_size() const
138 {
139 // TODO: check limits
140 return empty() ? 0 : get_pixels_count() * sizeof(Color);
141 }
142
143 bool
get_pixels(Color * buffer) const144 rendering::Surface::get_pixels(Color *buffer) const
145 {
146 return is_created() && get_pixels_vfunc(buffer);
147 }
148
149 /* === E N T R Y P O I N T ================================================= */
150