-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathmapnik_image_encode_chunked.cpp
More file actions
225 lines (186 loc) · 6.54 KB
/
mapnik_image_encode_chunked.cpp
File metadata and controls
225 lines (186 loc) · 6.54 KB
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
// mapnik
#include <mapnik/image.hpp> // for image types
#include <mapnik/image_util.hpp> // for save_to_stream
#include "mapnik_image_encode.hpp"
#include "mapnik_color.hpp"
#include "utils.hpp"
#include "callback_streambuf.hpp"
struct chunked_encode_image_baton_t
{
encode_image_baton_t image_baton;
uv_async_t async;
uv_mutex_t mutex;
using char_type = char;
using buffer_type = std::vector<char_type>;
using buffer_list_type = std::vector<buffer_type>;
buffer_list_type buffers;
const std::size_t buffer_size;
chunked_encode_image_baton_t(std::size_t buffer_size_)
: buffer_size(buffer_size_)
{
// The reinterpret_cast is for backward compatibility
// https://github.com/libuv/libuv/commit/db2a9072bce129630214904be5e2eedeaafc9835
if (uv_async_init(uv_default_loop(), &async,
reinterpret_cast<uv_async_cb>(yield_chunk)))
{
throw std::runtime_error("Cannot create async handler");
}
if (uv_mutex_init(&mutex))
{
uv_close(reinterpret_cast<uv_handle_t*>(&async), NULL);
throw std::runtime_error("Cannot create mutex");
}
async.data = this;
}
~chunked_encode_image_baton_t()
{
uv_mutex_destroy(&mutex);
}
template<class Char, class Size>
bool operator()(const Char* buffer, Size size)
{
uv_mutex_lock(&mutex);
buffers.emplace_back(buffer, buffer + size);
uv_mutex_unlock(&mutex);
return uv_async_send(&async) == 0;
}
static void yield_chunk(uv_async_t* handle)
{
using closure_type = chunked_encode_image_baton_t;
closure_type & closure = *reinterpret_cast<closure_type*>(handle->data);
if (closure.image_baton.error)
{
uv_close(reinterpret_cast<uv_handle_t*>(handle), async_close_cb);
return;
}
buffer_list_type local_buffers;
uv_mutex_lock(&closure.mutex);
closure.buffers.swap(local_buffers);
uv_mutex_unlock(&closure.mutex);
Nan::HandleScope scope;
bool done = false;
for (auto const & buffer : local_buffers)
{
v8::Local<v8::Value> argv[2] = {
Nan::Null(), Nan::CopyBuffer(buffer.data(),
buffer.size()).ToLocalChecked() };
Nan::MakeCallback(Nan::GetCurrentContext()->Global(),
Nan::New(closure.image_baton.cb), 2, argv);
done = buffer.empty();
}
if (done)
{
uv_close(reinterpret_cast<uv_handle_t*>(handle), async_close_cb);
}
}
static void async_close_cb(uv_handle_t* handle)
{
using closure_type = chunked_encode_image_baton_t;
closure_type & closure = *reinterpret_cast<closure_type*>(handle->data);
if (closure.image_baton.error)
{
Nan::HandleScope scope;
v8::Local<v8::Value> argv[1] = {
Nan::Error(closure.image_baton.error_name.c_str()) };
Nan::MakeCallback(Nan::GetCurrentContext()->Global(),
Nan::New(closure.image_baton.cb), 1, argv);
}
closure.image_baton.im->_unref();
closure.image_baton.cb.Reset();
delete &closure;
}
};
void Image::EIO_EncodeChunked(uv_work_t* work)
{
using closure_type = chunked_encode_image_baton_t;
closure_type & closure = *reinterpret_cast<closure_type*>(work->data);
try
{
callback_streambuf<closure_type&> streambuf(closure, closure.buffer_size);
std::ostream stream(&streambuf);
if (closure.image_baton.palette)
{
mapnik::save_to_stream(*closure.image_baton.im->this_,
stream,
closure.image_baton.format,
*closure.image_baton.palette);
}
else
{
mapnik::save_to_stream(*closure.image_baton.im->this_,
stream,
closure.image_baton.format);
}
stream.flush();
}
catch (std::exception const& ex)
{
closure.image_baton.error = true;
closure.image_baton.error_name = ex.what();
}
// Signalize end of stream
closure(static_cast<closure_type::char_type*>(NULL), 0);
}
NAN_METHOD(Image::encodeChunked)
{
Image* im = Nan::ObjectWrap::Unwrap<Image>(info.Holder());
std::string format = "png";
palette_ptr palette;
if (info.Length() != 4)
{
Nan::ThrowTypeError("Function requires four arguments");
return;
}
// accept custom format
if (!info[0]->IsString())
{
Nan::ThrowTypeError("first arg, 'format' must be a string");
return;
}
format = TOSTR(info[0]);
// options hash
if (!info[1]->IsObject())
{
Nan::ThrowTypeError("second arg must be an options object");
return;
}
v8::Local<v8::Object> options = info[1].As<v8::Object>();
if (options->Has(Nan::New("palette").ToLocalChecked()))
{
v8::Local<v8::Value> format_opt = options->Get(Nan::New("palette").ToLocalChecked());
if (!format_opt->IsObject())
{
Nan::ThrowTypeError("'palette' must be an object");
return;
}
v8::Local<v8::Object> obj = format_opt.As<v8::Object>();
if (obj->IsNull() || obj->IsUndefined() || !Nan::New(Palette::constructor)->HasInstance(obj))
{
Nan::ThrowTypeError("mapnik.Palette expected as second arg");
return;
}
palette = Nan::ObjectWrap::Unwrap<Palette>(obj)->palette();
}
int buffer_size;
if (!info[2]->IsNumber() || (buffer_size = info[2]->IntegerValue()) < 1)
{
Nan::ThrowTypeError("third arg must be a positive integer");
return;
}
// ensure callback is a function
v8::Local<v8::Value> callback = info[info.Length() - 1];
if (!callback->IsFunction())
{
Nan::ThrowTypeError("last argument must be a callback function");
return;
}
chunked_encode_image_baton_t *closure = new chunked_encode_image_baton_t(buffer_size);
closure->image_baton.request.data = closure;
closure->image_baton.im = im;
closure->image_baton.format = format;
closure->image_baton.palette = palette;
closure->image_baton.error = false;
closure->image_baton.cb.Reset(callback.As<v8::Function>());
uv_queue_work(uv_default_loop(), &closure->image_baton.request, EIO_EncodeChunked, NULL);
im->Ref();
}