]> git.sesse.net Git - casparcg/blob - core/producer/text/utils/texture_atlas.cpp
[text_producer] Don't upload texture atlas to GPU every time the text or tracking...
[casparcg] / core / producer / text / utils / texture_atlas.cpp
1 /* ============================================================================
2  * Freetype GL - A C OpenGL Freetype engine
3  * Platform:    Any
4  * WWW:         http://code.google.com/p/freetype-gl/
5  * ----------------------------------------------------------------------------
6  * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  *  1. Redistributions of source code must retain the above copyright notice,
12  *     this list of conditions and the following disclaimer.
13  *
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * The views and conclusions contained in the software and documentation are
30  * those of the authors and should not be interpreted as representing official
31  * policies, either expressed or implied, of Nicolas P. Rougier.
32  * ============================================================================
33  */
34
35 #include <list>
36 #include <vector>
37 #include "texture_atlas.h"
38
39 namespace caspar { namespace core { namespace text {
40
41
42 struct texture_atlas::impl
43 {
44 private:
45         struct node
46         {
47                 int x;
48                 int y;
49                 int width;
50         };
51
52         typedef std::list<node> node_list;
53         typedef std::list<node>::iterator node_iterator;
54
55         node_list nodes_;
56
57         size_t width_;
58         size_t height_;
59         size_t depth_;
60         std::vector<uint8_t> data_;
61         size_t used_                                            = 0;
62
63 public:
64         impl(const size_t width, const size_t height, const size_t depth) : width_(width), height_(height), depth_(depth), data_(width*height*depth, 0)
65         {
66                 // We want a one pixel border around the whole atlas to avoid any artefact when sampling texture
67                 node n = {1, 1, static_cast<int>(width_) - 2};
68                 nodes_.push_back(n);
69         }
70
71         rect get_region(int width, int height)
72         {
73                 rect region = { 0, 0, static_cast<int>(width), static_cast<int>(height) };
74
75                 int best_height = INT_MAX;
76                 int best_width = INT_MAX;
77                 node_iterator best_it = nodes_.end();
78
79                 for(auto it = nodes_.begin(); it != nodes_.end(); ++it)
80                 {
81                         int y = fit(it, width, height);
82                         if( y >= 0 )
83                         {
84                                 if( ( (y + height) < best_height ) ||
85                                         ( ((y + height) == best_height) && ((*it).width < best_width)) )
86                                 {
87                                         best_height = y + height;
88                                         best_it = it;
89                                         best_width = (*it).width;
90                                         region.x = (*it).x;
91                                         region.y = (int)y;
92                                 }
93                         }
94                 }
95
96                 if(best_it == nodes_.end())
97                 {
98                         region.x = -1;
99                         region.y = -1;
100                         region.width = 0;
101                         region.height = 0;
102                         return region;
103                 }
104
105                 node new_node;
106                 new_node.x = region.x;
107                 new_node.y = region.y + (int)height;
108                 new_node.width = (int)width;
109
110                 best_it = nodes_.insert(best_it, new_node);
111
112                 for(auto it = ++best_it; it != nodes_.end(); ++it)
113                 {
114                         auto prev = it; --prev;
115
116                         if ((*it).x < ((*prev).x + (*prev).width) )
117                         {
118                                 int shrink = (*prev).x + (*prev).width - (*it).x;
119                                 (*it).x += shrink;
120                                 (*it).width -= shrink;
121                                 if ((*it).width <= 0)
122                                 {
123                                         nodes_.erase(it);
124                                         it = prev;
125                                 }
126                                 else
127                                         break;
128                         }
129                         else
130                                 break;
131                 }
132
133                 merge();
134                 used_ += width * height;
135                 return region;
136         }
137
138         //the data parameter points to bitmap-data that is 8-bit grayscale.
139         void set_region(const size_t x, const size_t y, const size_t width, const size_t height, const unsigned char *src, const size_t stride, const color<double>& col)
140         {
141                 //this assumes depth_ is set to 4
142                 for(size_t i=0; i<height; ++i)
143                 {
144                         for(size_t j=0; j<width; ++j)
145                         {
146                                 unsigned char* pixel = &data_[((y+i)*width_ + x + j)*depth_];
147                                 unsigned char value = src[i*stride + j];
148                                 pixel[0] = (unsigned char)(value*col.b);
149                                 pixel[1] = (unsigned char)(value*col.g);
150                                 pixel[2] = (unsigned char)(value*col.r);
151                                 pixel[3] = (unsigned char)(value*col.a);
152                         }
153                 }
154         }
155
156         void clear()
157         {
158                 nodes_.clear();
159                 used_ = 0;
160
161                 node n = {1,1,(int)width_-2};
162                 nodes_.push_back(n);
163                 data_.assign(width_*height_*depth_, 0);
164         }
165
166         size_t depth() const { return depth_; }
167         size_t width() const { return width_; }
168         size_t height() const { return height_; }
169         const uint8_t* data() const { return data_.data(); }
170
171 private:
172         int fit(node_iterator it, const size_t width, const size_t height)
173         {
174                 int x = (*it).x;
175                 int y = (*it).y;
176                 int width_left = (int)width;
177
178                 if ((x + width) > (width_ - 1))
179                         return -1;
180
181                 while( width_left > 0 && it != nodes_.end())
182                 {
183                         if((*it).y > y)
184                                 y = (*it).y;
185                         if((y + height) > (height_ - 1))
186                                 return -1;
187
188                         width_left -= (*it).width;
189                         ++it;
190                 }
191
192                 return y;
193         }
194
195         void merge()
196         {
197                 auto it = nodes_.begin();
198                 while(true)
199                 {
200                         auto next = it; ++next;
201                         if(next == nodes_.end())
202                                 break;
203
204                         if((*it).y == (*next).y)
205                         {
206                                 (*it).width += (*next).width;
207                                 nodes_.erase(next);
208                         }
209                         else
210                                 ++it;
211                 }
212         }
213 };
214
215 texture_atlas::texture_atlas(const size_t w, const size_t h, const size_t d) : impl_(new impl(w, h, d)) {}
216 rect texture_atlas::get_region(int width, int height) const { return impl_->get_region(width, height); }
217 void texture_atlas::set_region(const size_t x, const size_t y, const size_t width, const size_t height, const unsigned char *src, const size_t stride, const color<double>& col)
218         { impl_->set_region(x, y, width, height, src, stride, col); }
219
220 void texture_atlas::clear() { impl_->clear(); }
221
222 size_t texture_atlas::width() const { return impl_->width(); }
223 size_t texture_atlas::height() const { return impl_->height(); }
224 size_t texture_atlas::depth() const { return impl_->depth(); }
225 const uint8_t* texture_atlas::data() const { return impl_->data(); }
226
227 }}}