-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
Picture.cpp
690 lines (510 loc) · 12.2 KB
/
Picture.cpp
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*
* Copyright 2001-2014 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Marc Flerackers, mflerackers@androme.be
*/
// Records a series of drawing instructions that can be "replayed" later.
#include <Picture.h>
#include <new>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#define DEBUG 1
#include <ByteOrder.h>
#include <Debug.h>
#include <List.h>
#include <Message.h>
#include <AppServerLink.h>
#include <Autolock.h>
#include <ObjectList.h>
#include <PicturePlayer.h>
#include <ServerProtocol.h>
#include "PicturePrivate.h"
static BObjectList<BPicture> sPictureList;
static BLocker sPictureListLock;
void
reconnect_pictures_to_app_server()
{
BAutolock _(sPictureListLock);
for (int32 i = 0; i < sPictureList.CountItems(); i++) {
BPicture::Private picture(sPictureList.ItemAt(i));
picture.ReconnectToAppServer();
}
}
BPicture::Private::Private(BPicture* picture)
:
fPicture(picture)
{
}
void
BPicture::Private::ReconnectToAppServer()
{
fPicture->_Upload();
}
struct _BPictureExtent_ {
_BPictureExtent_(int32 size = 0);
~_BPictureExtent_();
const void* Data() const { return fNewData; }
status_t ImportData(const void* data, int32 size);
status_t Flatten(BDataIO* stream);
status_t Unflatten(BDataIO* stream);
int32 Size() const { return fNewSize; }
status_t SetSize(int32 size);
bool AddPicture(BPicture* picture)
{ return fPictures.AddItem(picture); }
void DeletePicture(int32 index)
{ delete static_cast<BPicture*>
(fPictures.RemoveItem(index)); }
BList* Pictures() { return &fPictures; }
BPicture* PictureAt(int32 index)
{ return static_cast<BPicture*>
(fPictures.ItemAt(index)); }
int32 CountPictures() const
{ return fPictures.CountItems(); }
private:
void* fNewData;
int32 fNewSize;
BList fPictures;
// In R5 this is a BArray<BPicture*>
// which is completely inline.
};
struct picture_header {
int32 magic1; // version ?
int32 magic2; // endianess ?
};
BPicture::BPicture()
:
fToken(-1),
fExtent(NULL),
fUsurped(NULL)
{
_InitData();
}
BPicture::BPicture(const BPicture& otherPicture)
:
fToken(-1),
fExtent(NULL),
fUsurped(NULL)
{
_InitData();
if (otherPicture.fToken != -1) {
BPrivate::AppServerLink link;
link.StartMessage(AS_CLONE_PICTURE);
link.Attach<int32>(otherPicture.fToken);
status_t status = B_ERROR;
if (link.FlushWithReply(status) == B_OK && status == B_OK)
link.Read<int32>(&fToken);
if (status < B_OK)
return;
}
if (otherPicture.fExtent->Size() > 0) {
fExtent->ImportData(otherPicture.fExtent->Data(),
otherPicture.fExtent->Size());
for (int32 i = 0; i < otherPicture.fExtent->CountPictures(); i++) {
BPicture* picture
= new BPicture(*otherPicture.fExtent->PictureAt(i));
fExtent->AddPicture(picture);
}
}
}
BPicture::BPicture(BMessage* data)
:
fToken(-1),
fExtent(NULL),
fUsurped(NULL)
{
_InitData();
int32 version;
if (data->FindInt32("_ver", &version) != B_OK)
version = 0;
int8 endian;
if (data->FindInt8("_endian", &endian) != B_OK)
endian = 0;
const void* pictureData;
ssize_t size;
if (data->FindData("_data", B_RAW_TYPE, &pictureData, &size) != B_OK)
return;
// Load sub pictures
BMessage pictureMessage;
int32 i = 0;
while (data->FindMessage("piclib", i++, &pictureMessage) == B_OK) {
BPicture* picture = new BPicture(&pictureMessage);
fExtent->AddPicture(picture);
}
if (version == 0) {
// TODO: For now. We'll see if it's worth to support old style data
debugger("old style BPicture data is not supported");
} else if (version == 1) {
fExtent->ImportData(pictureData, size);
// swap_data(fExtent->fNewData, fExtent->fNewSize);
if (fExtent->Size() > 0)
_AssertServerCopy();
}
// Do we just free the data now?
if (fExtent->Size() > 0)
fExtent->SetSize(0);
// What with the sub pictures?
for (i = fExtent->CountPictures() - 1; i >= 0; i--)
fExtent->DeletePicture(i);
}
BPicture::BPicture(const void* data, int32 size)
{
_InitData();
// TODO: For now. We'll see if it's worth to support old style data
debugger("old style BPicture data is not supported");
}
void
BPicture::_InitData()
{
fToken = -1;
fUsurped = NULL;
fExtent = new (std::nothrow) _BPictureExtent_;
BAutolock _(sPictureListLock);
sPictureList.AddItem(this);
}
BPicture::~BPicture()
{
BAutolock _(sPictureListLock);
sPictureList.RemoveItem(this, false);
_DisposeData();
}
void
BPicture::_DisposeData()
{
if (fToken != -1) {
BPrivate::AppServerLink link;
link.StartMessage(AS_DELETE_PICTURE);
link.Attach<int32>(fToken);
link.Flush();
SetToken(-1);
}
delete fExtent;
fExtent = NULL;
}
BArchivable*
BPicture::Instantiate(BMessage* data)
{
if (validate_instantiation(data, "BPicture"))
return new BPicture(data);
return NULL;
}
status_t
BPicture::Archive(BMessage* data, bool deep) const
{
if (!const_cast<BPicture*>(this)->_AssertLocalCopy())
return B_ERROR;
status_t err = BArchivable::Archive(data, deep);
if (err != B_OK)
return err;
err = data->AddInt32("_ver", 1);
if (err != B_OK)
return err;
err = data->AddInt8("_endian", B_HOST_IS_BENDIAN);
if (err != B_OK)
return err;
err = data->AddData("_data", B_RAW_TYPE, fExtent->Data(), fExtent->Size());
if (err != B_OK)
return err;
for (int32 i = 0; i < fExtent->CountPictures(); i++) {
BMessage pictureMessage;
err = fExtent->PictureAt(i)->Archive(&pictureMessage, deep);
if (err != B_OK)
break;
err = data->AddMessage("piclib", &pictureMessage);
if (err != B_OK)
break;
}
return err;
}
status_t
BPicture::Perform(perform_code code, void* arg)
{
return BArchivable::Perform(code, arg);
}
status_t
BPicture::Play(void** callBackTable, int32 tableEntries, void* user)
{
if (!_AssertLocalCopy())
return B_ERROR;
BPrivate::PicturePlayer player(fExtent->Data(), fExtent->Size(),
fExtent->Pictures());
return player.Play(callBackTable, tableEntries, user);
}
status_t
BPicture::Flatten(BDataIO* stream)
{
// TODO: what about endianess?
if (!_AssertLocalCopy())
return B_ERROR;
const picture_header header = { 2, 0 };
ssize_t bytesWritten = stream->Write(&header, sizeof(header));
if (bytesWritten < B_OK)
return bytesWritten;
if (bytesWritten != (ssize_t)sizeof(header))
return B_IO_ERROR;
return fExtent->Flatten(stream);
}
status_t
BPicture::Unflatten(BDataIO* stream)
{
// TODO: clear current picture data?
picture_header header;
ssize_t bytesRead = stream->Read(&header, sizeof(header));
if (bytesRead < B_OK)
return bytesRead;
if (bytesRead != (ssize_t)sizeof(header)
|| header.magic1 != 2 || header.magic2 != 0)
return B_BAD_TYPE;
status_t status = fExtent->Unflatten(stream);
if (status < B_OK)
return status;
// swap_data(fExtent->fNewData, fExtent->fNewSize);
if (!_AssertServerCopy())
return B_ERROR;
// Data is now kept server side, remove the local copy
if (fExtent->Data() != NULL)
fExtent->SetSize(0);
return status;
}
void
BPicture::_ImportOldData(const void* data, int32 size)
{
// TODO: We don't support old data for now
}
void
BPicture::SetToken(int32 token)
{
fToken = token;
}
int32
BPicture::Token() const
{
return fToken;
}
bool
BPicture::_AssertLocalCopy()
{
if (fExtent->Data() != NULL)
return true;
if (fToken == -1)
return false;
return _Download() == B_OK;
}
bool
BPicture::_AssertOldLocalCopy()
{
// TODO: We don't support old data for now
return false;
}
bool
BPicture::_AssertServerCopy()
{
if (fToken != -1)
return true;
if (fExtent->Data() == NULL)
return false;
for (int32 i = 0; i < fExtent->CountPictures(); i++) {
if (!fExtent->PictureAt(i)->_AssertServerCopy())
return false;
}
return _Upload() == B_OK;
}
status_t
BPicture::_Upload()
{
if (fExtent == NULL || fExtent->Data() == NULL)
return B_BAD_VALUE;
BPrivate::AppServerLink link;
link.StartMessage(AS_CREATE_PICTURE);
link.Attach<int32>(fExtent->CountPictures());
for (int32 i = 0; i < fExtent->CountPictures(); i++) {
BPicture* picture = fExtent->PictureAt(i);
if (picture != NULL)
link.Attach<int32>(picture->fToken);
else
link.Attach<int32>(-1);
}
link.Attach<int32>(fExtent->Size());
link.Attach(fExtent->Data(), fExtent->Size());
status_t status = B_ERROR;
if (link.FlushWithReply(status) == B_OK
&& status == B_OK) {
link.Read<int32>(&fToken);
}
return status;
}
status_t
BPicture::_Download()
{
ASSERT(fExtent->Data() == NULL);
ASSERT(fToken != -1);
BPrivate::AppServerLink link;
link.StartMessage(AS_DOWNLOAD_PICTURE);
link.Attach<int32>(fToken);
status_t status = B_ERROR;
if (link.FlushWithReply(status) == B_OK && status == B_OK) {
int32 count = 0;
link.Read<int32>(&count);
// Read sub picture tokens
for (int32 i = 0; i < count; i++) {
BPicture* picture = new BPicture;
link.Read<int32>(&picture->fToken);
fExtent->AddPicture(picture);
}
int32 size;
link.Read<int32>(&size);
status = fExtent->SetSize(size);
if (status == B_OK)
link.Read(const_cast<void*>(fExtent->Data()), size);
}
return status;
}
const void*
BPicture::Data() const
{
if (fExtent->Data() == NULL)
const_cast<BPicture*>(this)->_AssertLocalCopy();
return fExtent->Data();
}
int32
BPicture::DataSize() const
{
if (fExtent->Data() == NULL)
const_cast<BPicture*>(this)->_AssertLocalCopy();
return fExtent->Size();
}
void
BPicture::Usurp(BPicture* lameDuck)
{
_DisposeData();
// Reinitializes the BPicture
_InitData();
// Do the Usurping
fUsurped = lameDuck;
}
BPicture*
BPicture::StepDown()
{
BPicture* lameDuck = fUsurped;
fUsurped = NULL;
return lameDuck;
}
void BPicture::_ReservedPicture1() {}
void BPicture::_ReservedPicture2() {}
void BPicture::_ReservedPicture3() {}
BPicture&
BPicture::operator=(const BPicture&)
{
return* this;
}
// _BPictureExtent_
_BPictureExtent_::_BPictureExtent_(int32 size)
:
fNewData(NULL),
fNewSize(0)
{
SetSize(size);
}
_BPictureExtent_::~_BPictureExtent_()
{
free(fNewData);
for (int32 i = 0; i < fPictures.CountItems(); i++)
delete static_cast<BPicture*>(fPictures.ItemAtFast(i));
}
status_t
_BPictureExtent_::ImportData(const void* data, int32 size)
{
if (data == NULL)
return B_BAD_VALUE;
status_t status = B_OK;
if (Size() != size)
status = SetSize(size);
if (status == B_OK)
memcpy(fNewData, data, size);
return status;
}
status_t
_BPictureExtent_::Unflatten(BDataIO* stream)
{
if (stream == NULL)
return B_BAD_VALUE;
int32 count = 0;
ssize_t bytesRead = stream->Read(&count, sizeof(count));
if (bytesRead < B_OK)
return bytesRead;
if (bytesRead != (ssize_t)sizeof(count))
return B_BAD_DATA;
for (int32 i = 0; i < count; i++) {
BPicture* picture = new BPicture;
status_t status = picture->Unflatten(stream);
if (status < B_OK) {
delete picture;
return status;
}
AddPicture(picture);
}
int32 size;
bytesRead = stream->Read(&size, sizeof(size));
if (bytesRead < B_OK)
return bytesRead;
if (bytesRead != (ssize_t)sizeof(size))
return B_IO_ERROR;
status_t status = B_OK;
if (Size() != size)
status = SetSize(size);
if (status < B_OK)
return status;
bytesRead = stream->Read(fNewData, size);
if (bytesRead < B_OK)
return bytesRead;
if (bytesRead != (ssize_t)size)
return B_IO_ERROR;
return B_OK;
}
status_t
_BPictureExtent_::Flatten(BDataIO* stream)
{
int32 count = fPictures.CountItems();
ssize_t bytesWritten = stream->Write(&count, sizeof(count));
if (bytesWritten < B_OK)
return bytesWritten;
if (bytesWritten != (ssize_t)sizeof(count))
return B_IO_ERROR;
for (int32 i = 0; i < count; i++) {
status_t status = PictureAt(i)->Flatten(stream);
if (status < B_OK)
return status;
}
bytesWritten = stream->Write(&fNewSize, sizeof(fNewSize));
if (bytesWritten < B_OK)
return bytesWritten;
if (bytesWritten != (ssize_t)sizeof(fNewSize))
return B_IO_ERROR;
bytesWritten = stream->Write(fNewData, fNewSize);
if (bytesWritten < B_OK)
return bytesWritten;
if (bytesWritten != fNewSize)
return B_IO_ERROR;
return B_OK;
}
status_t
_BPictureExtent_::SetSize(int32 size)
{
if (size < 0)
return B_BAD_VALUE;
if (size == fNewSize)
return B_OK;
if (size == 0) {
free(fNewData);
fNewData = NULL;
} else {
void* data = realloc(fNewData, size);
if (data == NULL)
return B_NO_MEMORY;
fNewData = data;
}
fNewSize = size;
return B_OK;
}