Fix T61078: Cycles crash with adaptive smoke sim playback.
[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         bool operator==(const ImageMetaData& other) const
49         {
50                 return is_float == other.is_float &&
51                        is_half == other.is_half &&
52                        channels == other.channels &&
53                        width == other.width &&
54                        height == other.height &&
55                        depth == other.depth &&
56                        type == other.type &&
57                        is_linear == other.is_linear;
58         }
59 };
60
61 class ImageManager {
62 public:
63         explicit ImageManager(const DeviceInfo& info);
64         ~ImageManager();
65
66         int add_image(const string& filename,
67                       void *builtin_data,
68                       bool animated,
69                       float frame,
70                       InterpolationType interpolation,
71                       ExtensionType extension,
72                       bool use_alpha,
73                       ImageMetaData& metadata);
74         void remove_image(int flat_slot);
75         void remove_image(const string& filename,
76                           void *builtin_data,
77                           InterpolationType interpolation,
78                           ExtensionType extension,
79                           bool use_alpha);
80         void tag_reload_image(const string& filename,
81                               void *builtin_data,
82                               InterpolationType interpolation,
83                               ExtensionType extension,
84                               bool use_alpha);
85         bool get_image_metadata(const string& filename,
86                                 void *builtin_data,
87                                 ImageMetaData& metadata);
88         bool get_image_metadata(int flat_slot,
89                                 ImageMetaData& metadata);
90
91         void device_update(Device *device,
92                            Scene *scene,
93                            Progress& progress);
94         void device_update_slot(Device *device,
95                                 Scene *scene,
96                                 int flat_slot,
97                                 Progress *progress);
98         void device_free(Device *device);
99         void device_free_builtin(Device *device);
100
101         void set_osl_texture_system(void *texture_system);
102         bool set_animation_frame_update(int frame);
103
104         device_memory *image_memory(int flat_slot);
105
106         void collect_statistics(RenderStats *stats);
107
108         bool need_update;
109
110         /* NOTE: Here pixels_size is a size of storage, which equals to
111          *       width * height * depth.
112          *       Use this to avoid some nasty memory corruptions.
113          */
114         function<void(const string &filename,
115                       void *data,
116                       ImageMetaData& metadata)> builtin_image_info_cb;
117         function<bool(const string &filename,
118                       void *data,
119                       unsigned char *pixels,
120                       const size_t pixels_size,
121                       const bool free_cache)> builtin_image_pixels_cb;
122         function<bool(const string &filename,
123                       void *data,
124                       float *pixels,
125                       const size_t pixels_size,
126                       const bool free_cache)> builtin_image_float_pixels_cb;
127
128         struct Image {
129                 string filename;
130                 void *builtin_data;
131                 ImageMetaData metadata;
132
133                 bool use_alpha;
134                 bool need_load;
135                 bool animated;
136                 float frame;
137                 InterpolationType interpolation;
138                 ExtensionType extension;
139
140                 string mem_name;
141                 device_memory *mem;
142
143                 int users;
144         };
145
146 private:
147         int tex_num_images[IMAGE_DATA_NUM_TYPES];
148         int max_num_images;
149         bool has_half_images;
150
151         thread_mutex device_mutex;
152         int animation_frame;
153
154         vector<Image*> images[IMAGE_DATA_NUM_TYPES];
155         void *osl_texture_system;
156
157         bool file_load_image_generic(Image *img, unique_ptr<ImageInput> *in);
158
159         template<TypeDesc::BASETYPE FileFormat,
160                  typename StorageType,
161                  typename DeviceType>
162         bool file_load_image(Image *img,
163                              ImageDataType type,
164                              int texture_limit,
165                              device_vector<DeviceType>& tex_img);
166
167         void device_load_image(Device *device,
168                                Scene *scene,
169                                ImageDataType type,
170                                int slot,
171                                Progress *progress);
172         void device_free_image(Device *device,
173                                ImageDataType type,
174                                int slot);
175 };
176
177 CCL_NAMESPACE_END
178
179 #endif  /* __IMAGE_H__ */