Merge remote-tracking branch 'origin/master' into blender2.8
[blender.git] / intern / cycles / render / image.h
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __IMAGE_H__
18 #define __IMAGE_H__
19
20 #include "device/device.h"
21 #include "device/device_memory.h"
22
23 #include "util/util_image.h"
24 #include "util/util_string.h"
25 #include "util/util_thread.h"
26 #include "util/util_unique_ptr.h"
27 #include "util/util_vector.h"
28
29 CCL_NAMESPACE_BEGIN
30
31 class Device;
32 class Progress;
33 class RenderStats;
34 class Scene;
35
36 class ImageMetaData {
37 public:
38         /* Must be set by image file or builtin callback. */
39         bool is_float, is_half;
40         int channels;
41         size_t width, height, depth;
42         bool builtin_free_cache;
43
44         /* Automatically set. */
45         ImageDataType type;
46         bool is_linear;
47 };
48
49 class ImageManager {
50 public:
51         explicit ImageManager(const DeviceInfo& info);
52         ~ImageManager();
53
54         int add_image(const string& filename,
55                       void *builtin_data,
56                       bool animated,
57                       float frame,
58                       InterpolationType interpolation,
59                       ExtensionType extension,
60                       bool use_alpha,
61                       ImageMetaData& metadata);
62         void remove_image(int flat_slot);
63         void remove_image(const string& filename,
64                           void *builtin_data,
65                           InterpolationType interpolation,
66                           ExtensionType extension,
67                           bool use_alpha);
68         void tag_reload_image(const string& filename,
69                               void *builtin_data,
70                               InterpolationType interpolation,
71                               ExtensionType extension,
72                               bool use_alpha);
73         bool get_image_metadata(const string& filename,
74                                 void *builtin_data,
75                                 ImageMetaData& metadata);
76         bool get_image_metadata(int flat_slot,
77                                 ImageMetaData& metadata);
78
79         void device_update(Device *device,
80                            Scene *scene,
81                            Progress& progress);
82         void device_update_slot(Device *device,
83                                 Scene *scene,
84                                 int flat_slot,
85                                 Progress *progress);
86         void device_free(Device *device);
87
88         void device_load_builtin(Device *device,
89                                  Scene *scene,
90                                  Progress& progress);
91         void device_free_builtin(Device *device);
92
93         void set_osl_texture_system(void *texture_system);
94         bool set_animation_frame_update(int frame);
95
96         device_memory *image_memory(int flat_slot);
97
98         void collect_statistics(RenderStats *stats);
99
100         bool need_update;
101
102         /* NOTE: Here pixels_size is a size of storage, which equals to
103          *       width * height * depth.
104          *       Use this to avoid some nasty memory corruptions.
105          */
106         function<void(const string &filename,
107                       void *data,
108                       ImageMetaData& metadata)> builtin_image_info_cb;
109         function<bool(const string &filename,
110                       void *data,
111                       unsigned char *pixels,
112                       const size_t pixels_size,
113                       const bool free_cache)> builtin_image_pixels_cb;
114         function<bool(const string &filename,
115                       void *data,
116                       float *pixels,
117                       const size_t pixels_size,
118                       const bool free_cache)> builtin_image_float_pixels_cb;
119
120         struct Image {
121                 string filename;
122                 void *builtin_data;
123                 ImageMetaData metadata;
124
125                 bool use_alpha;
126                 bool need_load;
127                 bool animated;
128                 float frame;
129                 InterpolationType interpolation;
130                 ExtensionType extension;
131
132                 string mem_name;
133                 device_memory *mem;
134
135                 int users;
136         };
137
138 private:
139         int tex_num_images[IMAGE_DATA_NUM_TYPES];
140         int max_num_images;
141         bool has_half_images;
142
143         thread_mutex device_mutex;
144         int animation_frame;
145
146         vector<Image*> images[IMAGE_DATA_NUM_TYPES];
147         void *osl_texture_system;
148
149         bool file_load_image_generic(Image *img, unique_ptr<ImageInput> *in);
150
151         template<TypeDesc::BASETYPE FileFormat,
152                  typename StorageType,
153                  typename DeviceType>
154         bool file_load_image(Image *img,
155                              ImageDataType type,
156                              int texture_limit,
157                              device_vector<DeviceType>& tex_img);
158
159         void device_load_image(Device *device,
160                                Scene *scene,
161                                ImageDataType type,
162                                int slot,
163                                Progress *progress);
164         void device_free_image(Device *device,
165                                ImageDataType type,
166                                int slot);
167 };
168
169 CCL_NAMESPACE_END
170
171 #endif  /* __IMAGE_H__ */