forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayerScope.cpp
1984 lines (1688 loc) · 56 KB
/
LayerScope.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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim:set ts=4 sw=4 sts=4 et: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* This must occur *after* layers/PLayers.h to avoid typedefs conflicts. */
#include "LayerScope.h"
#include "nsAppRunner.h"
#include "Composer2D.h"
#include "Effects.h"
#include "mozilla/Endian.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/Preferences.h"
#include "mozilla/TimeStamp.h"
#include "TexturePoolOGL.h"
#include "mozilla/layers/CompositorOGL.h"
#include "mozilla/layers/CompositorParent.h"
#include "mozilla/layers/LayerManagerComposite.h"
#include "mozilla/layers/TextureHostOGL.h"
#include "gfxContext.h"
#include "gfxUtils.h"
#include "gfxPrefs.h"
#include "nsIWidget.h"
#include "GLContext.h"
#include "GLContextProvider.h"
#include "GLReadTexImageHelper.h"
#include "nsIServiceManager.h"
#include "nsIConsoleService.h"
#include <memory>
#include "mozilla/LinkedList.h"
#include "mozilla/Base64.h"
#include "mozilla/SHA1.h"
#include "mozilla/StaticPtr.h"
#include "nsThreadUtils.h"
#include "nsISocketTransport.h"
#include "nsIServerSocket.h"
#include "nsReadLine.h"
#include "nsNetCID.h"
#include "nsIOutputStream.h"
#include "nsIAsyncInputStream.h"
#include "nsIEventTarget.h"
#include "nsProxyRelease.h"
#include <list>
// Undo the damage done by mozzconf.h
#undef compress
#include "mozilla/Compression.h"
// Protocol buffer (generated automatically)
#include "protobuf/LayerScopePacket.pb.h"
namespace mozilla {
namespace layers {
using namespace mozilla::Compression;
using namespace mozilla::gfx;
using namespace mozilla::gl;
using namespace mozilla;
using namespace layerscope;
class DebugDataSender;
class DebugGLData;
/*
* Manage Websocket connections
*/
class LayerScopeWebSocketManager {
public:
LayerScopeWebSocketManager();
~LayerScopeWebSocketManager();
void RemoveAllConnections()
{
MOZ_ASSERT(NS_IsMainThread());
MutexAutoLock lock(mHandlerMutex);
mHandlers.Clear();
}
bool WriteAll(void *ptr, uint32_t size)
{
for (int32_t i = mHandlers.Length() - 1; i >= 0; --i) {
if (!mHandlers[i]->WriteToStream(ptr, size)) {
// Send failed, remove this handler
RemoveConnection(i);
}
}
return true;
}
bool IsConnected()
{
// This funtion can be called in both main thread and compositor thread.
MutexAutoLock lock(mHandlerMutex);
return (mHandlers.Length() != 0) ? true : false;
}
void AppendDebugData(DebugGLData *aDebugData);
void CleanDebugData();
void DispatchDebugData();
private:
void AddConnection(nsISocketTransport *aTransport)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aTransport);
MutexAutoLock lock(mHandlerMutex);
RefPtr<SocketHandler> temp = new SocketHandler();
temp->OpenStream(aTransport);
mHandlers.AppendElement(temp.get());
}
void RemoveConnection(uint32_t aIndex)
{
// TBD: RemoveConnection is executed on the compositor thread and
// AddConntection is executed on the main thread, which might be
// a problem if a user disconnect and connect readlly quickly at
// viewer side.
// We should dispatch RemoveConnection onto main thead.
MOZ_ASSERT(aIndex < mHandlers.Length());
MutexAutoLock lock(mHandlerMutex);
mHandlers.RemoveElementAt(aIndex);
}
friend class SocketListener;
class SocketListener : public nsIServerSocketListener
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
SocketListener() { }
/* nsIServerSocketListener */
NS_IMETHODIMP OnSocketAccepted(nsIServerSocket *aServ,
nsISocketTransport *aTransport) override;
NS_IMETHODIMP OnStopListening(nsIServerSocket *aServ,
nsresult aStatus) override
{
return NS_OK;
}
private:
virtual ~SocketListener() { }
};
/*
* This class handle websocket protocol which included
* handshake and data frame's header
*/
class SocketHandler : public nsIInputStreamCallback {
public:
NS_DECL_THREADSAFE_ISUPPORTS
SocketHandler()
: mState(NoHandshake)
, mConnected(false)
{ }
void OpenStream(nsISocketTransport* aTransport);
bool WriteToStream(void *aPtr, uint32_t aSize);
// nsIInputStreamCallback
NS_IMETHODIMP OnInputStreamReady(nsIAsyncInputStream *aStream) override;
private:
virtual ~SocketHandler() { CloseConnection(); }
void ReadInputStreamData(nsTArray<nsCString>& aProtocolString);
bool WebSocketHandshake(nsTArray<nsCString>& aProtocolString);
void ApplyMask(uint32_t aMask, uint8_t *aData, uint64_t aLen);
bool HandleDataFrame(uint8_t *aData, uint32_t aSize);
void CloseConnection();
nsresult HandleSocketMessage(nsIAsyncInputStream *aStream);
nsresult ProcessInput(uint8_t *aBuffer, uint32_t aCount);
private:
enum SocketStateType {
NoHandshake,
HandshakeSuccess,
HandshakeFailed
};
SocketStateType mState;
nsCOMPtr<nsIOutputStream> mOutputStream;
nsCOMPtr<nsIAsyncInputStream> mInputStream;
nsCOMPtr<nsISocketTransport> mTransport;
bool mConnected;
};
nsTArray<RefPtr<SocketHandler> > mHandlers;
nsCOMPtr<nsIThread> mDebugSenderThread;
RefPtr<DebugDataSender> mCurrentSender;
nsCOMPtr<nsIServerSocket> mServerSocket;
// Keep mHandlers accessing thread safe.
Mutex mHandlerMutex;
};
NS_IMPL_ISUPPORTS(LayerScopeWebSocketManager::SocketListener,
nsIServerSocketListener);
NS_IMPL_ISUPPORTS(LayerScopeWebSocketManager::SocketHandler,
nsIInputStreamCallback);
class DrawSession {
public:
DrawSession()
: mOffsetX(0.0)
, mOffsetY(0.0)
, mRects(0)
{ }
float mOffsetX;
float mOffsetY;
gfx::Matrix4x4 mMVMatrix;
size_t mRects;
gfx::Rect mLayerRects[4];
gfx::Rect mTextureRects[4];
std::list<GLuint> mTexIDs;
};
class ContentMonitor {
public:
using THArray = nsTArray<const TextureHost *>;
// Notify the content of a TextureHost was changed.
void SetChangedHost(const TextureHost* host) {
if (THArray::NoIndex == mChangedHosts.IndexOf(host)) {
mChangedHosts.AppendElement(host);
}
}
// Clear changed flag of a host.
void ClearChangedHost(const TextureHost* host) {
if (THArray::NoIndex != mChangedHosts.IndexOf(host)) {
mChangedHosts.RemoveElement(host);
}
}
// Return true iff host is a new one or the content of it had been changed.
bool IsChangedOrNew(const TextureHost* host) {
if (THArray::NoIndex == mSeenHosts.IndexOf(host)) {
mSeenHosts.AppendElement(host);
return true;
}
if (decltype(mChangedHosts)::NoIndex != mChangedHosts.IndexOf(host)) {
return true;
}
return false;
}
void Empty() {
mSeenHosts.SetLength(0);
mChangedHosts.SetLength(0);
}
private:
THArray mSeenHosts;
THArray mChangedHosts;
};
/*
* Hold all singleton objects used by LayerScope.
*/
class LayerScopeManager
{
public:
void CreateServerSocket()
{
// WebSocketManager must be created on the main thread.
if (NS_IsMainThread()) {
mWebSocketManager = mozilla::MakeUnique<LayerScopeWebSocketManager>();
} else {
// Dispatch creation to main thread, and make sure we
// dispatch this only once after booting
static bool dispatched = false;
if (dispatched) {
return;
}
DebugOnly<nsresult> rv =
NS_DispatchToMainThread(new CreateServerSocketRunnable(this));
MOZ_ASSERT(NS_SUCCEEDED(rv),
"Failed to dispatch WebSocket Creation to main thread");
dispatched = true;
}
}
void DestroyServerSocket()
{
// Destroy Web Server Socket
if (mWebSocketManager) {
mWebSocketManager->RemoveAllConnections();
}
}
LayerScopeWebSocketManager* GetSocketManager()
{
return mWebSocketManager.get();
}
ContentMonitor* GetContentMonitor()
{
if (!mContentMonitor.get()) {
mContentMonitor = mozilla::MakeUnique<ContentMonitor>();
}
return mContentMonitor.get();
}
void NewDrawSession() {
mSession = mozilla::MakeUnique<DrawSession>();
}
DrawSession& CurrentSession() {
return *mSession;
}
void SetPixelScale(double scale) {
mScale = scale;
}
double GetPixelScale() const {
return mScale;
}
LayerScopeManager()
: mScale(1.0)
{
}
private:
friend class CreateServerSocketRunnable;
class CreateServerSocketRunnable : public nsRunnable
{
public:
explicit CreateServerSocketRunnable(LayerScopeManager *aLayerScopeManager)
: mLayerScopeManager(aLayerScopeManager)
{
}
NS_IMETHOD Run() {
mLayerScopeManager->mWebSocketManager =
mozilla::MakeUnique<LayerScopeWebSocketManager>();
return NS_OK;
}
private:
LayerScopeManager* mLayerScopeManager;
};
mozilla::UniquePtr<LayerScopeWebSocketManager> mWebSocketManager;
mozilla::UniquePtr<DrawSession> mSession;
mozilla::UniquePtr<ContentMonitor> mContentMonitor;
double mScale;
};
LayerScopeManager gLayerScopeManager;
/*
* The static helper functions that set data into the packet
* 1. DumpRect
* 2. DumpFilter
*/
template<typename T>
static void DumpRect(T* aPacketRect, const Rect& aRect)
{
aPacketRect->set_x(aRect.x);
aPacketRect->set_y(aRect.y);
aPacketRect->set_w(aRect.width);
aPacketRect->set_h(aRect.height);
}
static void DumpFilter(TexturePacket* aTexturePacket, const Filter& aFilter)
{
switch (aFilter) {
case Filter::GOOD:
aTexturePacket->set_mfilter(TexturePacket::GOOD);
break;
case Filter::LINEAR:
aTexturePacket->set_mfilter(TexturePacket::LINEAR);
break;
case Filter::POINT:
aTexturePacket->set_mfilter(TexturePacket::POINT);
break;
default:
MOZ_ASSERT(false, "Can't dump unexpected mFilter to texture packet!");
break;
}
}
/*
* DebugGLData is the base class of
* 1. DebugGLFrameStatusData (Frame start/end packet)
* 2. DebugGLColorData (Color data packet)
* 3. DebugGLTextureData (Texture data packet)
* 4. DebugGLLayersData (Layers Tree data packet)
* 5. DebugGLMetaData (Meta data packet)
*/
class DebugGLData: public LinkedListElement<DebugGLData> {
public:
explicit DebugGLData(Packet::DataType aDataType)
: mDataType(aDataType)
{ }
virtual ~DebugGLData() { }
virtual bool Write() = 0;
protected:
static bool WriteToStream(Packet& aPacket) {
if (!gLayerScopeManager.GetSocketManager())
return true;
uint32_t size = aPacket.ByteSize();
auto data = MakeUnique<uint8_t[]>(size);
aPacket.SerializeToArray(data.get(), size);
return gLayerScopeManager.GetSocketManager()->WriteAll(data.get(), size);
}
Packet::DataType mDataType;
};
class DebugGLFrameStatusData final: public DebugGLData
{
public:
DebugGLFrameStatusData(Packet::DataType aDataType,
int64_t aValue)
: DebugGLData(aDataType),
mFrameStamp(aValue)
{ }
explicit DebugGLFrameStatusData(Packet::DataType aDataType)
: DebugGLData(aDataType),
mFrameStamp(0)
{ }
virtual bool Write() override {
Packet packet;
packet.set_type(mDataType);
FramePacket* fp = packet.mutable_frame();
fp->set_value(static_cast<uint64_t>(mFrameStamp));
fp->set_scale(gLayerScopeManager.GetPixelScale());
return WriteToStream(packet);
}
protected:
int64_t mFrameStamp;
};
#ifdef MOZ_WIDGET_GONK
// B2G optimization.
class DebugGLGraphicBuffer final: public DebugGLData {
public:
DebugGLGraphicBuffer(void *layerRef,
GLenum target,
GLuint name,
const LayerRenderState &aState,
bool aIsMask,
UniquePtr<Packet> aPacket)
: DebugGLData(Packet::TEXTURE),
mLayerRef(reinterpret_cast<uint64_t>(layerRef)),
mTarget(target),
mName(name),
mState(aState),
mIsMask(aIsMask),
mPacket(Move(aPacket))
{
}
virtual bool Write() override {
return WriteToStream(*mPacket);
}
bool TryPack(bool packData) {
android::sp<android::GraphicBuffer> buffer = mState.mSurface;
MOZ_ASSERT(buffer.get());
mPacket->set_type(mDataType);
TexturePacket* tp = mPacket->mutable_texture();
tp->set_layerref(mLayerRef);
tp->set_name(mName);
tp->set_target(mTarget);
tp->set_ismask(mIsMask);
int pFormat = buffer->getPixelFormat();
if (HAL_PIXEL_FORMAT_RGBA_8888 != pFormat &&
HAL_PIXEL_FORMAT_RGBX_8888 != pFormat) {
return false;
}
int32_t stride = buffer->getStride() * 4;
int32_t height = buffer->getHeight();
int32_t width = buffer->getWidth();
int32_t sourceSize = stride * height;
if (sourceSize <= 0) {
return false;
}
uint32_t dFormat = mState.FormatRBSwapped() ?
LOCAL_GL_BGRA : LOCAL_GL_RGBA;
tp->set_dataformat(dFormat);
tp->set_dataformat((1 << 16 | tp->dataformat()));
tp->set_width(width);
tp->set_height(height);
tp->set_stride(stride);
if (packData) {
uint8_t* grallocData = nullptr;
if (BAD_VALUE == buffer->lock(GRALLOC_USAGE_SW_READ_OFTEN |
GRALLOC_USAGE_SW_WRITE_NEVER,
reinterpret_cast<void**>(&grallocData)))
{
return false;
}
// Do not return before buffer->unlock();
auto compressedData =
MakeUnique<char[]>(LZ4::maxCompressedSize(sourceSize));
int compressedSize = LZ4::compress((char*)grallocData,
sourceSize,
compressedData.get());
if (compressedSize > 0) {
tp->set_data(compressedData.get(), compressedSize);
} else {
buffer->unlock();
return false;
}
buffer->unlock();
}
return true;
}
private:
uint64_t mLayerRef;
GLenum mTarget;
GLuint mName;
const LayerRenderState &mState;
bool mIsMask;
UniquePtr<Packet> mPacket;
};
#endif
class DebugGLTextureData final: public DebugGLData {
public:
DebugGLTextureData(GLContext* cx,
void* layerRef,
GLenum target,
GLuint name,
DataSourceSurface* img,
bool aIsMask,
UniquePtr<Packet> aPacket)
: DebugGLData(Packet::TEXTURE),
mLayerRef(reinterpret_cast<uint64_t>(layerRef)),
mTarget(target),
mName(name),
mContextAddress(reinterpret_cast<intptr_t>(cx)),
mDatasize(0),
mIsMask(aIsMask),
mPacket(Move(aPacket))
{
// pre-packing
// DataSourceSurface may have locked buffer,
// so we should compress now, and then it could
// be unlocked outside.
pack(img);
}
virtual bool Write() override {
return WriteToStream(*mPacket);
}
private:
void pack(DataSourceSurface* aImage) {
mPacket->set_type(mDataType);
TexturePacket* tp = mPacket->mutable_texture();
tp->set_layerref(mLayerRef);
tp->set_name(mName);
tp->set_target(mTarget);
tp->set_dataformat(LOCAL_GL_RGBA);
tp->set_glcontext(static_cast<uint64_t>(mContextAddress));
tp->set_ismask(mIsMask);
if (aImage) {
tp->set_width(aImage->GetSize().width);
tp->set_height(aImage->GetSize().height);
tp->set_stride(aImage->Stride());
mDatasize = aImage->GetSize().height * aImage->Stride();
auto compresseddata = MakeUnique<char[]>(LZ4::maxCompressedSize(mDatasize));
if (compresseddata) {
int ndatasize = LZ4::compress((char*)aImage->GetData(),
mDatasize,
compresseddata.get());
if (ndatasize > 0) {
mDatasize = ndatasize;
tp->set_dataformat((1 << 16 | tp->dataformat()));
tp->set_data(compresseddata.get(), mDatasize);
} else {
NS_WARNING("Compress data failed");
tp->set_data(aImage->GetData(), mDatasize);
}
} else {
NS_WARNING("Couldn't new compressed data.");
tp->set_data(aImage->GetData(), mDatasize);
}
} else {
tp->set_width(0);
tp->set_height(0);
tp->set_stride(0);
}
}
protected:
uint64_t mLayerRef;
GLenum mTarget;
GLuint mName;
intptr_t mContextAddress;
uint32_t mDatasize;
bool mIsMask;
// Packet data
UniquePtr<Packet> mPacket;
};
class DebugGLColorData final: public DebugGLData {
public:
DebugGLColorData(void* layerRef,
const Color& color,
int width,
int height)
: DebugGLData(Packet::COLOR),
mLayerRef(reinterpret_cast<uint64_t>(layerRef)),
mColor(color.ToABGR()),
mSize(width, height)
{ }
virtual bool Write() override {
Packet packet;
packet.set_type(mDataType);
ColorPacket* cp = packet.mutable_color();
cp->set_layerref(mLayerRef);
cp->set_color(mColor);
cp->set_width(mSize.width);
cp->set_height(mSize.height);
return WriteToStream(packet);
}
protected:
uint64_t mLayerRef;
uint32_t mColor;
IntSize mSize;
};
class DebugGLLayersData final: public DebugGLData {
public:
explicit DebugGLLayersData(UniquePtr<Packet> aPacket)
: DebugGLData(Packet::LAYERS),
mPacket(Move(aPacket))
{ }
virtual bool Write() override {
mPacket->set_type(mDataType);
return WriteToStream(*mPacket);
}
protected:
UniquePtr<Packet> mPacket;
};
class DebugGLMetaData final: public DebugGLData
{
public:
DebugGLMetaData(Packet::DataType aDataType,
bool aValue)
: DebugGLData(aDataType),
mComposedByHwc(aValue)
{ }
explicit DebugGLMetaData(Packet::DataType aDataType)
: DebugGLData(aDataType),
mComposedByHwc(false)
{ }
virtual bool Write() override {
Packet packet;
packet.set_type(mDataType);
MetaPacket* mp = packet.mutable_meta();
mp->set_composedbyhwc(mComposedByHwc);
return WriteToStream(packet);
}
protected:
bool mComposedByHwc;
};
class DebugGLDrawData final: public DebugGLData {
public:
DebugGLDrawData(float aOffsetX,
float aOffsetY,
const gfx::Matrix4x4& aMVMatrix,
size_t aRects,
const gfx::Rect* aLayerRects,
const gfx::Rect* aTextureRects,
const std::list<GLuint> aTexIDs,
void* aLayerRef)
: DebugGLData(Packet::DRAW),
mOffsetX(aOffsetX),
mOffsetY(aOffsetY),
mMVMatrix(aMVMatrix),
mRects(aRects),
mTexIDs(aTexIDs),
mLayerRef(reinterpret_cast<uint64_t>(aLayerRef))
{
for (size_t i = 0; i < mRects; i++){
mLayerRects[i] = aLayerRects[i];
mTextureRects[i] = aTextureRects[i];
}
}
virtual bool Write() override {
Packet packet;
packet.set_type(mDataType);
DrawPacket* dp = packet.mutable_draw();
dp->set_layerref(mLayerRef);
dp->set_offsetx(mOffsetX);
dp->set_offsety(mOffsetY);
auto element = reinterpret_cast<Float *>(&mMVMatrix);
for (int i = 0; i < 16; i++) {
dp->add_mvmatrix(*element++);
}
dp->set_totalrects(mRects);
MOZ_ASSERT(mRects > 0 && mRects < 4);
for (size_t i = 0; i < mRects; i++) {
// Vertex
DumpRect(dp->add_layerrect(), mLayerRects[i]);
// UV
DumpRect(dp->add_texturerect(), mTextureRects[i]);
}
for (GLuint texId: mTexIDs) {
dp->add_texids(texId);
}
return WriteToStream(packet);
}
protected:
float mOffsetX;
float mOffsetY;
gfx::Matrix4x4 mMVMatrix;
size_t mRects;
gfx::Rect mLayerRects[4];
gfx::Rect mTextureRects[4];
std::list<GLuint> mTexIDs;
uint64_t mLayerRef;
};
class DebugDataSender
{
public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DebugDataSender)
// Append a DebugData into mList on mThread
class AppendTask: public nsIRunnable
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
AppendTask(DebugDataSender *host, DebugGLData *d)
: mData(d),
mHost(host)
{ }
NS_IMETHODIMP Run() override {
mHost->mList.insertBack(mData);
return NS_OK;
}
private:
virtual ~AppendTask() { }
DebugGLData *mData;
// Keep a strong reference to DebugDataSender to prevent this object
// accessing mHost on mThread, when it's been destroyed on the main
// thread.
RefPtr<DebugDataSender> mHost;
};
// Clear all DebugData in mList on mThead.
class ClearTask: public nsIRunnable
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
explicit ClearTask(DebugDataSender *host)
: mHost(host)
{ }
NS_IMETHODIMP Run() override {
mHost->RemoveData();
return NS_OK;
}
private:
virtual ~ClearTask() { }
RefPtr<DebugDataSender> mHost;
};
// Send all DebugData in mList via websocket, and then, clean up
// mList on mThread.
class SendTask: public nsIRunnable
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
explicit SendTask(DebugDataSender *host)
: mHost(host)
{ }
NS_IMETHODIMP Run() override {
// Sendout all appended debug data.
DebugGLData *d = nullptr;
while ((d = mHost->mList.popFirst()) != nullptr) {
UniquePtr<DebugGLData> cleaner(d);
if (!d->Write()) {
gLayerScopeManager.DestroyServerSocket();
break;
}
}
// Cleanup.
mHost->RemoveData();
return NS_OK;
}
private:
virtual ~SendTask() { }
RefPtr<DebugDataSender> mHost;
};
explicit DebugDataSender(nsIThread *thread)
: mThread(thread)
{ }
void Append(DebugGLData *d) {
mThread->Dispatch(new AppendTask(this, d), NS_DISPATCH_NORMAL);
}
void Cleanup() {
mThread->Dispatch(new ClearTask(this), NS_DISPATCH_NORMAL);
}
void Send() {
mThread->Dispatch(new SendTask(this), NS_DISPATCH_NORMAL);
}
protected:
virtual ~DebugDataSender() {}
void RemoveData() {
MOZ_ASSERT(NS_GetCurrentThread() == mThread);
if (mList.isEmpty())
return;
DebugGLData *d;
while ((d = mList.popFirst()) != nullptr)
delete d;
}
// We can only modify or aceess mList on mThread.
LinkedList<DebugGLData> mList;
nsCOMPtr<nsIThread> mThread;
};
NS_IMPL_ISUPPORTS(DebugDataSender::AppendTask, nsIRunnable);
NS_IMPL_ISUPPORTS(DebugDataSender::ClearTask, nsIRunnable);
NS_IMPL_ISUPPORTS(DebugDataSender::SendTask, nsIRunnable);
/*
* LayerScope SendXXX Structure
* 1. SendLayer
* 2. SendEffectChain
* 1. SendTexturedEffect
* -> SendTextureSource
* 2. SendMaskEffect
* -> SendTextureSource
* 3. SendYCbCrEffect
* -> SendTextureSource
* 4. SendColor
*/
class SenderHelper
{
// Sender public APIs
public:
static void SendLayer(LayerComposite* aLayer,
int aWidth,
int aHeight);
static void SendEffectChain(gl::GLContext* aGLContext,
const EffectChain& aEffectChain,
int aWidth = 0,
int aHeight = 0);
static void SetLayersTreeSendable(bool aSet) {sLayersTreeSendable = aSet;}
static void SetLayersBufferSendable(bool aSet) {sLayersBufferSendable = aSet;}
static bool GetLayersTreeSendable() {return sLayersTreeSendable;}
static void ClearSentTextureIds();
// Sender private functions
private:
static void SendColor(void* aLayerRef,
const Color& aColor,
int aWidth,
int aHeight);
static void SendTextureSource(GLContext* aGLContext,
void* aLayerRef,
TextureSourceOGL* aSource,
bool aFlipY,
bool aIsMask,
UniquePtr<Packet> aPacket);
#ifdef MOZ_WIDGET_GONK
static bool SendGraphicBuffer(GLContext* aGLContext,
void* aLayerRef,
TextureSourceOGL* aSource,
const TexturedEffect* aEffect,
bool aIsMask);
#endif
static void SetAndSendTexture(GLContext* aGLContext,
void* aLayerRef,
TextureSourceOGL* aSource,
const TexturedEffect* aEffect);
static void SendTexturedEffect(GLContext* aGLContext,
void* aLayerRef,
const TexturedEffect* aEffect);
static void SendMaskEffect(GLContext* aGLContext,
void* aLayerRef,
const EffectMask* aEffect);
static void SendYCbCrEffect(GLContext* aGLContext,
void* aLayerRef,
const EffectYCbCr* aEffect);
static GLuint GetTextureID(GLContext* aGLContext,
TextureSourceOGL* aSource);
static bool HasTextureIdBeenSent(GLuint aTextureId);
// Data fields
private:
static bool sLayersTreeSendable;
static bool sLayersBufferSendable;
static std::vector<GLuint> sSentTextureIds;
};
bool SenderHelper::sLayersTreeSendable = true;
bool SenderHelper::sLayersBufferSendable = true;
std::vector<GLuint> SenderHelper::sSentTextureIds;
// ----------------------------------------------
// SenderHelper implementation
// ----------------------------------------------
void
SenderHelper::ClearSentTextureIds()
{
sSentTextureIds.clear();
}
bool
SenderHelper::HasTextureIdBeenSent(GLuint aTextureId)
{
return std::find(sSentTextureIds.begin(), sSentTextureIds.end(), aTextureId) != sSentTextureIds.end();
}
void
SenderHelper::SendLayer(LayerComposite* aLayer,