Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/layers/LayerScope.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* vim:set ts=4 sw=4 sts=4 et: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
/* This must occur *after* layers/PLayers.h to avoid typedefs conflicts. */
8
#include "LayerScope.h"
9
10
#include "nsAppRunner.h"
11
#include "Effects.h"
12
#include "mozilla/EndianUtils.h"
13
#include "mozilla/MathAlgorithms.h"
14
#include "mozilla/Preferences.h"
15
#include "mozilla/TimeStamp.h"
16
17
#include "mozilla/layers/CompositorOGL.h"
18
#include "mozilla/layers/CompositorThread.h"
19
#include "mozilla/layers/LayerManagerComposite.h"
20
#include "mozilla/layers/TextureHostOGL.h"
21
22
#include "gfxContext.h"
23
#include "gfxUtils.h"
24
#include "gfxPrefs.h"
25
#include "nsIWidget.h"
26
27
#include "GLContext.h"
28
#include "GLContextProvider.h"
29
#include "GLReadTexImageHelper.h"
30
31
#include "nsIServiceManager.h"
32
#include "nsIConsoleService.h"
33
34
#include <memory>
35
#include "mozilla/LinkedList.h"
36
#include "mozilla/Base64.h"
37
#include "mozilla/SHA1.h"
38
#include "mozilla/StaticPtr.h"
39
#include "nsThreadUtils.h"
40
#include "nsISocketTransport.h"
41
#include "nsIServerSocket.h"
42
#include "nsReadLine.h"
43
#include "nsNetCID.h"
44
#include "nsIOutputStream.h"
45
#include "nsIAsyncInputStream.h"
46
#include "nsIEventTarget.h"
47
#include "nsProxyRelease.h"
48
#include <list>
49
50
// Undo the damage done by mozzconf.h
51
#undef compress
52
#include "mozilla/Compression.h"
53
54
// Undo the damage done by X11
55
#ifdef Status
56
#  undef Status
57
#endif
58
// Protocol buffer (generated automatically)
59
#include "protobuf/LayerScopePacket.pb.h"
60
61
namespace mozilla {
62
namespace layers {
63
64
using namespace mozilla::Compression;
65
using namespace mozilla::gfx;
66
using namespace mozilla::gl;
67
using namespace mozilla;
68
using namespace layerscope;
69
70
class DebugDataSender;
71
class DebugGLData;
72
73
/*
74
 * Manage Websocket connections
75
 */
76
class LayerScopeWebSocketManager {
77
public:
78
    LayerScopeWebSocketManager();
79
    ~LayerScopeWebSocketManager();
80
81
    void RemoveAllConnections()
82
0
    {
83
0
        MOZ_ASSERT(NS_IsMainThread());
84
0
85
0
        MutexAutoLock lock(mHandlerMutex);
86
0
        mHandlers.Clear();
87
0
    }
88
89
    bool WriteAll(void *ptr, uint32_t size)
90
0
    {
91
0
        for (int32_t i = mHandlers.Length() - 1; i >= 0; --i) {
92
0
            if (!mHandlers[i]->WriteToStream(ptr, size)) {
93
0
                // Send failed, remove this handler
94
0
                RemoveConnection(i);
95
0
            }
96
0
        }
97
0
98
0
        return true;
99
0
    }
100
101
    bool IsConnected()
102
0
    {
103
0
        // This funtion can be called in both main thread and compositor thread.
104
0
        MutexAutoLock lock(mHandlerMutex);
105
0
        return (mHandlers.Length() != 0) ? true : false;
106
0
    }
107
108
    void AppendDebugData(DebugGLData *aDebugData);
109
    void CleanDebugData();
110
    void DispatchDebugData();
111
112
private:
113
    void AddConnection(nsISocketTransport *aTransport)
114
0
    {
115
0
        MOZ_ASSERT(NS_IsMainThread());
116
0
        MOZ_ASSERT(aTransport);
117
0
118
0
        MutexAutoLock lock(mHandlerMutex);
119
0
120
0
        RefPtr<SocketHandler> temp = new SocketHandler();
121
0
        temp->OpenStream(aTransport);
122
0
        mHandlers.AppendElement(temp.get());
123
0
    }
124
125
    void RemoveConnection(uint32_t aIndex)
126
0
    {
127
0
        // TBD: RemoveConnection is executed on the compositor thread and
128
0
        // AddConntection is executed on the main thread, which might be
129
0
        // a problem if a user disconnect and connect readlly quickly at
130
0
        // viewer side.
131
0
132
0
        // We should dispatch RemoveConnection onto main thead.
133
0
        MOZ_ASSERT(aIndex < mHandlers.Length());
134
0
135
0
        MutexAutoLock lock(mHandlerMutex);
136
0
        mHandlers.RemoveElementAt(aIndex);
137
0
    }
138
139
    friend class SocketListener;
140
    class SocketListener : public nsIServerSocketListener
141
    {
142
    public:
143
       NS_DECL_THREADSAFE_ISUPPORTS
144
145
0
       SocketListener() { }
146
147
       /* nsIServerSocketListener */
148
       NS_IMETHOD OnSocketAccepted(nsIServerSocket *aServ,
149
                                   nsISocketTransport *aTransport) override;
150
       NS_IMETHOD OnStopListening(nsIServerSocket *aServ,
151
                                  nsresult aStatus) override
152
0
       {
153
0
           return NS_OK;
154
0
       }
155
    private:
156
0
       virtual ~SocketListener() { }
157
    };
158
159
    /*
160
     * This class handle websocket protocol which included
161
     * handshake and data frame's header
162
     */
163
    class SocketHandler : public nsIInputStreamCallback {
164
    public:
165
        NS_DECL_THREADSAFE_ISUPPORTS
166
167
        SocketHandler()
168
            : mState(NoHandshake)
169
            , mConnected(false)
170
0
        { }
171
172
        void OpenStream(nsISocketTransport* aTransport);
173
        bool WriteToStream(void *aPtr, uint32_t aSize);
174
175
        // nsIInputStreamCallback
176
        NS_IMETHOD OnInputStreamReady(nsIAsyncInputStream *aStream) override;
177
178
    private:
179
0
        virtual ~SocketHandler() { CloseConnection(); }
180
181
        void ReadInputStreamData(nsTArray<nsCString>& aProtocolString);
182
        bool WebSocketHandshake(nsTArray<nsCString>& aProtocolString);
183
        void ApplyMask(uint32_t aMask, uint8_t *aData, uint64_t aLen);
184
        bool HandleDataFrame(uint8_t *aData, uint32_t aSize);
185
        void CloseConnection();
186
187
        nsresult HandleSocketMessage(nsIAsyncInputStream *aStream);
188
        nsresult ProcessInput(uint8_t *aBuffer, uint32_t aCount);
189
190
    private:
191
        enum SocketStateType {
192
            NoHandshake,
193
            HandshakeSuccess,
194
            HandshakeFailed
195
        };
196
        SocketStateType               mState;
197
198
        nsCOMPtr<nsIOutputStream>     mOutputStream;
199
        nsCOMPtr<nsIAsyncInputStream> mInputStream;
200
        nsCOMPtr<nsISocketTransport>  mTransport;
201
        bool                          mConnected;
202
    };
203
204
    nsTArray<RefPtr<SocketHandler> > mHandlers;
205
    nsCOMPtr<nsIThread>                   mDebugSenderThread;
206
    RefPtr<DebugDataSender>             mCurrentSender;
207
    nsCOMPtr<nsIServerSocket>             mServerSocket;
208
209
    // Keep mHandlers accessing thread safe.
210
    Mutex mHandlerMutex;
211
};
212
213
NS_IMPL_ISUPPORTS(LayerScopeWebSocketManager::SocketListener,
214
                  nsIServerSocketListener);
215
NS_IMPL_ISUPPORTS(LayerScopeWebSocketManager::SocketHandler,
216
                  nsIInputStreamCallback);
217
218
class DrawSession {
219
public:
220
    DrawSession()
221
      : mOffsetX(0.0)
222
      , mOffsetY(0.0)
223
      , mRects(0)
224
0
    { }
225
226
    float mOffsetX;
227
    float mOffsetY;
228
    gfx::Matrix4x4 mMVMatrix;
229
    size_t mRects;
230
    gfx::Rect mLayerRects[4];
231
    gfx::Rect mTextureRects[4];
232
    std::list<GLuint> mTexIDs;
233
};
234
235
class ContentMonitor {
236
public:
237
    using THArray = nsTArray<const TextureHost *>;
238
239
    // Notify the content of a TextureHost was changed.
240
0
    void SetChangedHost(const TextureHost* host) {
241
0
        if (THArray::NoIndex == mChangedHosts.IndexOf(host)) {
242
0
            mChangedHosts.AppendElement(host);
243
0
        }
244
0
    }
245
246
    // Clear changed flag of a host.
247
0
    void ClearChangedHost(const TextureHost* host) {
248
0
        if (THArray::NoIndex != mChangedHosts.IndexOf(host)) {
249
0
          mChangedHosts.RemoveElement(host);
250
0
        }
251
0
    }
252
253
    // Return true iff host is a new one or the content of it had been changed.
254
0
    bool IsChangedOrNew(const TextureHost* host) {
255
0
        if (THArray::NoIndex == mSeenHosts.IndexOf(host)) {
256
0
            mSeenHosts.AppendElement(host);
257
0
            return true;
258
0
        }
259
0
260
0
        if (decltype(mChangedHosts)::NoIndex != mChangedHosts.IndexOf(host)) {
261
0
            return true;
262
0
        }
263
0
264
0
        return false;
265
0
    }
266
267
0
    void Empty() {
268
0
        mSeenHosts.SetLength(0);
269
0
        mChangedHosts.SetLength(0);
270
0
    }
271
private:
272
    THArray mSeenHosts;
273
    THArray mChangedHosts;
274
};
275
276
/*
277
 * Hold all singleton objects used by LayerScope.
278
 */
279
class LayerScopeManager
280
{
281
public:
282
    void CreateServerSocket()
283
0
    {
284
0
        //  WebSocketManager must be created on the main thread.
285
0
        if (NS_IsMainThread()) {
286
0
            mWebSocketManager = mozilla::MakeUnique<LayerScopeWebSocketManager>();
287
0
        } else {
288
0
            // Dispatch creation to main thread, and make sure we
289
0
            // dispatch this only once after booting
290
0
            static bool dispatched = false;
291
0
            if (dispatched) {
292
0
                return;
293
0
            }
294
0
295
0
            DebugOnly<nsresult> rv =
296
0
              NS_DispatchToMainThread(new CreateServerSocketRunnable(this));
297
0
            MOZ_ASSERT(NS_SUCCEEDED(rv),
298
0
                  "Failed to dispatch WebSocket Creation to main thread");
299
0
            dispatched = true;
300
0
        }
301
0
    }
302
303
    void DestroyServerSocket()
304
0
    {
305
0
        // Destroy Web Server Socket
306
0
        if (mWebSocketManager) {
307
0
            mWebSocketManager->RemoveAllConnections();
308
0
        }
309
0
    }
310
311
    LayerScopeWebSocketManager* GetSocketManager()
312
0
    {
313
0
        return mWebSocketManager.get();
314
0
    }
315
316
    ContentMonitor* GetContentMonitor()
317
0
    {
318
0
        if (!mContentMonitor.get()) {
319
0
            mContentMonitor = mozilla::MakeUnique<ContentMonitor>();
320
0
        }
321
0
322
0
        return mContentMonitor.get();
323
0
    }
324
325
0
    void NewDrawSession() {
326
0
        mSession = mozilla::MakeUnique<DrawSession>();
327
0
    }
328
329
0
    DrawSession& CurrentSession() {
330
0
        return *mSession;
331
0
    }
332
333
0
    void SetPixelScale(double scale) {
334
0
        mScale = scale;
335
0
    }
336
337
0
    double GetPixelScale() const {
338
0
        return mScale;
339
0
    }
340
341
    LayerScopeManager()
342
        : mScale(1.0)
343
3
    {
344
3
    }
345
private:
346
    friend class CreateServerSocketRunnable;
347
    class CreateServerSocketRunnable : public Runnable
348
    {
349
    public:
350
      explicit CreateServerSocketRunnable(LayerScopeManager* aLayerScopeManager)
351
        : Runnable("layers::LayerScopeManager::CreateServerSocketRunnable")
352
        , mLayerScopeManager(aLayerScopeManager)
353
0
      {
354
0
        }
355
0
        NS_IMETHOD Run() override {
356
0
            mLayerScopeManager->mWebSocketManager =
357
0
                mozilla::MakeUnique<LayerScopeWebSocketManager>();
358
0
            return NS_OK;
359
0
        }
360
    private:
361
        LayerScopeManager* mLayerScopeManager;
362
    };
363
364
    mozilla::UniquePtr<LayerScopeWebSocketManager> mWebSocketManager;
365
    mozilla::UniquePtr<DrawSession> mSession;
366
    mozilla::UniquePtr<ContentMonitor> mContentMonitor;
367
    double mScale;
368
};
369
370
LayerScopeManager gLayerScopeManager;
371
372
/*
373
 * The static helper functions that set data into the packet
374
 * 1. DumpRect
375
 * 2. DumpFilter
376
 */
377
template<typename T>
378
static void DumpRect(T* aPacketRect, const Rect& aRect)
379
0
{
380
0
    aPacketRect->set_x(aRect.X());
381
0
    aPacketRect->set_y(aRect.Y());
382
0
    aPacketRect->set_w(aRect.Width());
383
0
    aPacketRect->set_h(aRect.Height());
384
0
}
Unexecuted instantiation: Unified_cpp_gfx_layers0.cpp:void mozilla::layers::DumpRect<mozilla::layers::layerscope::DrawPacket_Rect>(mozilla::layers::layerscope::DrawPacket_Rect*, mozilla::gfx::RectTyped<mozilla::gfx::UnknownUnits, float> const&)
Unexecuted instantiation: Unified_cpp_gfx_layers0.cpp:void mozilla::layers::DumpRect<mozilla::layers::layerscope::TexturePacket_Rect>(mozilla::layers::layerscope::TexturePacket_Rect*, mozilla::gfx::RectTyped<mozilla::gfx::UnknownUnits, float> const&)
385
386
static void DumpFilter(TexturePacket* aTexturePacket,
387
                       const SamplingFilter aSamplingFilter)
388
0
{
389
0
    switch (aSamplingFilter) {
390
0
        case SamplingFilter::GOOD:
391
0
            aTexturePacket->set_mfilter(TexturePacket::GOOD);
392
0
            break;
393
0
        case SamplingFilter::LINEAR:
394
0
            aTexturePacket->set_mfilter(TexturePacket::LINEAR);
395
0
            break;
396
0
        case SamplingFilter::POINT:
397
0
            aTexturePacket->set_mfilter(TexturePacket::POINT);
398
0
            break;
399
0
        default:
400
0
            MOZ_ASSERT(false, "Can't dump unexpected mSamplingFilter to texture packet!");
401
0
            break;
402
0
    }
403
0
}
404
405
/*
406
 * DebugGLData is the base class of
407
 * 1. DebugGLFrameStatusData (Frame start/end packet)
408
 * 2. DebugGLColorData (Color data packet)
409
 * 3. DebugGLTextureData (Texture data packet)
410
 * 4. DebugGLLayersData (Layers Tree data packet)
411
 * 5. DebugGLMetaData (Meta data packet)
412
 */
413
class DebugGLData: public LinkedListElement<DebugGLData> {
414
public:
415
    explicit DebugGLData(Packet::DataType aDataType)
416
        : mDataType(aDataType)
417
0
    { }
418
419
0
    virtual ~DebugGLData() { }
420
421
    virtual bool Write() = 0;
422
423
protected:
424
0
    static bool WriteToStream(Packet& aPacket) {
425
0
        if (!gLayerScopeManager.GetSocketManager())
426
0
            return true;
427
0
428
0
        uint32_t size = aPacket.ByteSize();
429
0
        auto data = MakeUnique<uint8_t[]>(size);
430
0
        aPacket.SerializeToArray(data.get(), size);
431
0
        return gLayerScopeManager.GetSocketManager()->WriteAll(data.get(), size);
432
0
    }
433
434
    Packet::DataType mDataType;
435
};
436
437
class DebugGLFrameStatusData final: public DebugGLData
438
{
439
public:
440
    DebugGLFrameStatusData(Packet::DataType aDataType,
441
                           int64_t aValue)
442
        : DebugGLData(aDataType),
443
          mFrameStamp(aValue)
444
0
    { }
445
446
    explicit DebugGLFrameStatusData(Packet::DataType aDataType)
447
        : DebugGLData(aDataType),
448
          mFrameStamp(0)
449
0
    { }
450
451
0
    virtual bool Write() override {
452
0
        Packet packet;
453
0
        packet.set_type(mDataType);
454
0
455
0
        FramePacket* fp = packet.mutable_frame();
456
0
        fp->set_value(static_cast<uint64_t>(mFrameStamp));
457
0
458
0
        fp->set_scale(gLayerScopeManager.GetPixelScale());
459
0
460
0
        return WriteToStream(packet);
461
0
    }
462
463
protected:
464
    int64_t mFrameStamp;
465
};
466
467
class DebugGLTextureData final: public DebugGLData {
468
public:
469
    DebugGLTextureData(GLContext* cx,
470
                       void* layerRef,
471
                       GLenum target,
472
                       GLuint name,
473
                       DataSourceSurface* img,
474
                       bool aIsMask,
475
                       UniquePtr<Packet> aPacket)
476
        : DebugGLData(Packet::TEXTURE),
477
          mLayerRef(reinterpret_cast<uint64_t>(layerRef)),
478
          mTarget(target),
479
          mName(name),
480
          mContextAddress(reinterpret_cast<intptr_t>(cx)),
481
          mDatasize(0),
482
          mIsMask(aIsMask),
483
          mPacket(std::move(aPacket))
484
0
    {
485
0
        // pre-packing
486
0
        // DataSourceSurface may have locked buffer,
487
0
        // so we should compress now, and then it could
488
0
        // be unlocked outside.
489
0
        pack(img);
490
0
    }
491
492
0
    virtual bool Write() override {
493
0
        return WriteToStream(*mPacket);
494
0
    }
495
496
private:
497
0
    void pack(DataSourceSurface* aImage) {
498
0
        mPacket->set_type(mDataType);
499
0
500
0
        TexturePacket* tp = mPacket->mutable_texture();
501
0
        tp->set_layerref(mLayerRef);
502
0
        tp->set_name(mName);
503
0
        tp->set_target(mTarget);
504
0
        tp->set_dataformat(LOCAL_GL_RGBA);
505
0
        tp->set_glcontext(static_cast<uint64_t>(mContextAddress));
506
0
        tp->set_ismask(mIsMask);
507
0
508
0
        if (aImage) {
509
0
            DataSourceSurface::ScopedMap map(aImage, DataSourceSurface::READ);
510
0
            tp->set_width(aImage->GetSize().width);
511
0
            tp->set_height(aImage->GetSize().height);
512
0
            tp->set_stride(map.GetStride());
513
0
514
0
            mDatasize = aImage->GetSize().height * map.GetStride();
515
0
516
0
            auto compresseddata = MakeUnique<char[]>(LZ4::maxCompressedSize(mDatasize));
517
0
            if (compresseddata) {
518
0
                int ndatasize = LZ4::compress((char*)map.GetData(),
519
0
                                              mDatasize,
520
0
                                              compresseddata.get());
521
0
                if (ndatasize > 0) {
522
0
                    mDatasize = ndatasize;
523
0
                    tp->set_dataformat((1 << 16 | tp->dataformat()));
524
0
                    tp->set_data(compresseddata.get(), mDatasize);
525
0
                } else {
526
0
                    NS_WARNING("Compress data failed");
527
0
                    tp->set_data(map.GetData(), mDatasize);
528
0
                }
529
0
            } else {
530
0
                NS_WARNING("Couldn't new compressed data.");
531
0
                tp->set_data(map.GetData(), mDatasize);
532
0
            }
533
0
        } else {
534
0
            tp->set_width(0);
535
0
            tp->set_height(0);
536
0
            tp->set_stride(0);
537
0
        }
538
0
    }
539
540
protected:
541
    uint64_t mLayerRef;
542
    GLenum mTarget;
543
    GLuint mName;
544
    intptr_t mContextAddress;
545
    uint32_t mDatasize;
546
    bool mIsMask;
547
548
    // Packet data
549
    UniquePtr<Packet> mPacket;
550
};
551
552
class DebugGLColorData final: public DebugGLData {
553
public:
554
    DebugGLColorData(void* layerRef,
555
                     const Color& color,
556
                     int width,
557
                     int height)
558
        : DebugGLData(Packet::COLOR),
559
          mLayerRef(reinterpret_cast<uint64_t>(layerRef)),
560
          mColor(color.ToABGR()),
561
          mSize(width, height)
562
0
    { }
563
564
0
    virtual bool Write() override {
565
0
        Packet packet;
566
0
        packet.set_type(mDataType);
567
0
568
0
        ColorPacket* cp = packet.mutable_color();
569
0
        cp->set_layerref(mLayerRef);
570
0
        cp->set_color(mColor);
571
0
        cp->set_width(mSize.width);
572
0
        cp->set_height(mSize.height);
573
0
574
0
        return WriteToStream(packet);
575
0
    }
576
577
protected:
578
    uint64_t mLayerRef;
579
    uint32_t mColor;
580
    IntSize mSize;
581
};
582
583
class DebugGLLayersData final: public DebugGLData {
584
public:
585
    explicit DebugGLLayersData(UniquePtr<Packet> aPacket)
586
        : DebugGLData(Packet::LAYERS),
587
          mPacket(std::move(aPacket))
588
0
    { }
589
590
0
    virtual bool Write() override {
591
0
        mPacket->set_type(mDataType);
592
0
        return WriteToStream(*mPacket);
593
0
    }
594
595
protected:
596
    UniquePtr<Packet> mPacket;
597
};
598
599
class DebugGLMetaData final: public DebugGLData
600
{
601
public:
602
    DebugGLMetaData(Packet::DataType aDataType,
603
                    bool aValue)
604
        : DebugGLData(aDataType),
605
          mComposedByHwc(aValue)
606
0
    { }
607
608
    explicit DebugGLMetaData(Packet::DataType aDataType)
609
        : DebugGLData(aDataType),
610
          mComposedByHwc(false)
611
0
    { }
612
613
0
    virtual bool Write() override {
614
0
        Packet packet;
615
0
        packet.set_type(mDataType);
616
0
617
0
        MetaPacket* mp = packet.mutable_meta();
618
0
        mp->set_composedbyhwc(mComposedByHwc);
619
0
620
0
        return WriteToStream(packet);
621
0
    }
622
623
protected:
624
    bool mComposedByHwc;
625
};
626
627
class DebugGLDrawData final: public DebugGLData {
628
public:
629
    DebugGLDrawData(float aOffsetX,
630
                    float aOffsetY,
631
                    const gfx::Matrix4x4& aMVMatrix,
632
                    size_t aRects,
633
                    const gfx::Rect* aLayerRects,
634
                    const gfx::Rect* aTextureRects,
635
                    const std::list<GLuint>& aTexIDs,
636
                    void* aLayerRef)
637
        : DebugGLData(Packet::DRAW),
638
          mOffsetX(aOffsetX),
639
          mOffsetY(aOffsetY),
640
          mMVMatrix(aMVMatrix),
641
          mRects(aRects),
642
          mTexIDs(aTexIDs),
643
          mLayerRef(reinterpret_cast<uint64_t>(aLayerRef))
644
0
    {
645
0
        for (size_t i = 0; i < mRects; i++){
646
0
            mLayerRects[i] = aLayerRects[i];
647
0
            mTextureRects[i] = aTextureRects[i];
648
0
        }
649
0
    }
650
651
0
    virtual bool Write() override {
652
0
        Packet packet;
653
0
        packet.set_type(mDataType);
654
0
655
0
        DrawPacket* dp = packet.mutable_draw();
656
0
        dp->set_layerref(mLayerRef);
657
0
658
0
        dp->set_offsetx(mOffsetX);
659
0
        dp->set_offsety(mOffsetY);
660
0
661
0
        auto element = reinterpret_cast<Float *>(&mMVMatrix);
662
0
        for (int i = 0; i < 16; i++) {
663
0
          dp->add_mvmatrix(*element++);
664
0
        }
665
0
        dp->set_totalrects(mRects);
666
0
667
0
        MOZ_ASSERT(mRects > 0 && mRects < 4);
668
0
        for (size_t i = 0; i < mRects; i++) {
669
0
            // Vertex
670
0
            DumpRect(dp->add_layerrect(), mLayerRects[i]);
671
0
            // UV
672
0
            DumpRect(dp->add_texturerect(), mTextureRects[i]);
673
0
        }
674
0
675
0
        for (GLuint texId: mTexIDs) {
676
0
            dp->add_texids(texId);
677
0
        }
678
0
679
0
        return WriteToStream(packet);
680
0
    }
681
682
protected:
683
    float mOffsetX;
684
    float mOffsetY;
685
    gfx::Matrix4x4 mMVMatrix;
686
    size_t mRects;
687
    gfx::Rect mLayerRects[4];
688
    gfx::Rect mTextureRects[4];
689
    std::list<GLuint> mTexIDs;
690
    uint64_t mLayerRef;
691
};
692
693
class DebugDataSender
694
{
695
public:
696
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DebugDataSender)
697
698
    // Append a DebugData into mList on mThread
699
    class AppendTask: public nsIRunnable
700
    {
701
    public:
702
        NS_DECL_THREADSAFE_ISUPPORTS
703
704
        AppendTask(DebugDataSender *host, DebugGLData *d)
705
            : mData(d),
706
              mHost(host)
707
0
        {  }
708
709
0
        NS_IMETHOD Run() override {
710
0
            mHost->mList.insertBack(mData);
711
0
            return NS_OK;
712
0
        }
713
714
    private:
715
0
        virtual ~AppendTask() { }
716
717
        DebugGLData *mData;
718
        // Keep a strong reference to DebugDataSender to prevent this object
719
        // accessing mHost on mThread, when it's been destroyed on the main
720
        // thread.
721
        RefPtr<DebugDataSender> mHost;
722
    };
723
724
    // Clear all DebugData in mList on mThead.
725
    class ClearTask: public nsIRunnable
726
    {
727
    public:
728
        NS_DECL_THREADSAFE_ISUPPORTS
729
        explicit ClearTask(DebugDataSender *host)
730
            : mHost(host)
731
0
        {  }
732
733
0
        NS_IMETHOD Run() override {
734
0
            mHost->RemoveData();
735
0
            return NS_OK;
736
0
        }
737
738
    private:
739
0
        virtual ~ClearTask() { }
740
741
        RefPtr<DebugDataSender> mHost;
742
    };
743
744
    // Send all DebugData in mList via websocket, and then, clean up
745
    // mList on mThread.
746
    class SendTask: public nsIRunnable
747
    {
748
    public:
749
        NS_DECL_THREADSAFE_ISUPPORTS
750
751
        explicit SendTask(DebugDataSender *host)
752
            : mHost(host)
753
0
        {  }
754
755
0
        NS_IMETHOD Run() override {
756
0
            // Sendout all appended debug data.
757
0
            DebugGLData *d = nullptr;
758
0
            while ((d = mHost->mList.popFirst()) != nullptr) {
759
0
                UniquePtr<DebugGLData> cleaner(d);
760
0
                if (!d->Write()) {
761
0
                    gLayerScopeManager.DestroyServerSocket();
762
0
                    break;
763
0
                }
764
0
            }
765
0
766
0
            // Cleanup.
767
0
            mHost->RemoveData();
768
0
            return NS_OK;
769
0
        }
770
    private:
771
0
        virtual ~SendTask() { }
772
773
        RefPtr<DebugDataSender> mHost;
774
    };
775
776
    explicit DebugDataSender(nsIThread *thread)
777
        : mThread(thread)
778
0
    {  }
779
780
0
    void Append(DebugGLData *d) {
781
0
        mThread->Dispatch(new AppendTask(this, d), NS_DISPATCH_NORMAL);
782
0
    }
783
784
0
    void Cleanup() {
785
0
        mThread->Dispatch(new ClearTask(this), NS_DISPATCH_NORMAL);
786
0
    }
787
788
0
    void Send() {
789
0
        mThread->Dispatch(new SendTask(this), NS_DISPATCH_NORMAL);
790
0
    }
791
792
protected:
793
0
    virtual ~DebugDataSender() {}
794
0
    void RemoveData() {
795
0
        MOZ_ASSERT(mThread->SerialEventTarget()->IsOnCurrentThread());
796
0
        if (mList.isEmpty())
797
0
            return;
798
0
799
0
        DebugGLData *d;
800
0
        while ((d = mList.popFirst()) != nullptr)
801
0
            delete d;
802
0
    }
803
804
    // We can only modify or aceess mList on mThread.
805
    LinkedList<DebugGLData> mList;
806
    nsCOMPtr<nsIThread>     mThread;
807
};
808
809
NS_IMPL_ISUPPORTS(DebugDataSender::AppendTask, nsIRunnable);
810
NS_IMPL_ISUPPORTS(DebugDataSender::ClearTask, nsIRunnable);
811
NS_IMPL_ISUPPORTS(DebugDataSender::SendTask, nsIRunnable);
812
813
814
/*
815
 * LayerScope SendXXX Structure
816
 * 1. SendLayer
817
 * 2. SendEffectChain
818
 *   1. SendTexturedEffect
819
 *      -> SendTextureSource
820
 *   2. SendMaskEffect
821
 *      -> SendTextureSource
822
 *   3. SendYCbCrEffect
823
 *      -> SendTextureSource
824
 *   4. SendColor
825
 */
826
class SenderHelper
827
{
828
// Sender public APIs
829
public:
830
    static void SendLayer(LayerComposite* aLayer,
831
                          int aWidth,
832
                          int aHeight);
833
834
    static void SendEffectChain(gl::GLContext* aGLContext,
835
                                const EffectChain& aEffectChain,
836
                                int aWidth = 0,
837
                                int aHeight = 0);
838
839
0
    static void SetLayersTreeSendable(bool aSet) {sLayersTreeSendable = aSet;}
840
841
0
    static void SetLayersBufferSendable(bool aSet) {sLayersBufferSendable = aSet;}
842
843
0
    static bool GetLayersTreeSendable() {return sLayersTreeSendable;}
844
845
    static void ClearSentTextureIds();
846
847
// Sender private functions
848
private:
849
    static void SendColor(void* aLayerRef,
850
                          const Color& aColor,
851
                          int aWidth,
852
                          int aHeight);
853
    static void SendTextureSource(GLContext* aGLContext,
854
                                  void* aLayerRef,
855
                                  TextureSourceOGL* aSource,
856
                                  bool aFlipY,
857
                                  bool aIsMask,
858
                                  UniquePtr<Packet> aPacket);
859
    static void SetAndSendTexture(GLContext* aGLContext,
860
                                  void* aLayerRef,
861
                                  TextureSourceOGL* aSource,
862
                                  const TexturedEffect* aEffect);
863
    static void SendTexturedEffect(GLContext* aGLContext,
864
                                   void* aLayerRef,
865
                                   const TexturedEffect* aEffect);
866
    static void SendMaskEffect(GLContext* aGLContext,
867
                                   void* aLayerRef,
868
                                   const EffectMask* aEffect);
869
    static void SendYCbCrEffect(GLContext* aGLContext,
870
                                void* aLayerRef,
871
                                const EffectYCbCr* aEffect);
872
    static GLuint GetTextureID(GLContext* aGLContext,
873
                               TextureSourceOGL* aSource);
874
    static bool HasTextureIdBeenSent(GLuint aTextureId);
875
// Data fields
876
private:
877
    static bool sLayersTreeSendable;
878
    static bool sLayersBufferSendable;
879
    static std::vector<GLuint> sSentTextureIds;
880
};
881
882
bool SenderHelper::sLayersTreeSendable = true;
883
bool SenderHelper::sLayersBufferSendable = true;
884
std::vector<GLuint> SenderHelper::sSentTextureIds;
885
886
887
// ----------------------------------------------
888
// SenderHelper implementation
889
// ----------------------------------------------
890
void
891
SenderHelper::ClearSentTextureIds()
892
0
{
893
0
    sSentTextureIds.clear();
894
0
}
895
896
bool
897
SenderHelper::HasTextureIdBeenSent(GLuint aTextureId)
898
0
{
899
0
    return std::find(sSentTextureIds.begin(), sSentTextureIds.end(), aTextureId) != sSentTextureIds.end();
900
0
}
901
902
void
903
SenderHelper::SendLayer(LayerComposite* aLayer,
904
                        int aWidth,
905
                        int aHeight)
906
0
{
907
0
    MOZ_ASSERT(aLayer && aLayer->GetLayer());
908
0
    if (!aLayer || !aLayer->GetLayer()) {
909
0
        return;
910
0
    }
911
0
912
0
    switch (aLayer->GetLayer()->GetType()) {
913
0
        case Layer::TYPE_COLOR: {
914
0
            EffectChain effect;
915
0
            aLayer->GenEffectChain(effect);
916
0
917
0
            LayerScope::DrawBegin();
918
0
            LayerScope::DrawEnd(nullptr, effect, aWidth, aHeight);
919
0
            break;
920
0
        }
921
0
        case Layer::TYPE_IMAGE:
922
0
        case Layer::TYPE_CANVAS:
923
0
        case Layer::TYPE_PAINTED: {
924
0
            // Get CompositableHost and Compositor
925
0
            CompositableHost* compHost = aLayer->GetCompositableHost();
926
0
            TextureSourceProvider* provider = compHost->GetTextureSourceProvider();
927
0
            Compositor* comp = provider->AsCompositor();
928
0
            // Send EffectChain only for CompositorOGL
929
0
            if (LayersBackend::LAYERS_OPENGL == comp->GetBackendType()) {
930
0
                CompositorOGL* compOGL = comp->AsCompositorOGL();
931
0
                EffectChain effect;
932
0
                // Generate primary effect (lock and gen)
933
0
                AutoLockCompositableHost lock(compHost);
934
0
                aLayer->GenEffectChain(effect);
935
0
936
0
                LayerScope::DrawBegin();
937
0
                LayerScope::DrawEnd(compOGL->gl(), effect, aWidth, aHeight);
938
0
            }
939
0
            break;
940
0
        }
941
0
        case Layer::TYPE_CONTAINER:
942
0
        default:
943
0
            break;
944
0
    }
945
0
}
946
947
void
948
SenderHelper::SendColor(void* aLayerRef,
949
                        const Color& aColor,
950
                        int aWidth,
951
                        int aHeight)
952
0
{
953
0
    gLayerScopeManager.GetSocketManager()->AppendDebugData(
954
0
        new DebugGLColorData(aLayerRef, aColor, aWidth, aHeight));
955
0
}
956
957
GLuint
958
SenderHelper::GetTextureID(GLContext* aGLContext,
959
0
                           TextureSourceOGL* aSource) {
960
0
    GLenum textureTarget = aSource->GetTextureTarget();
961
0
    aSource->BindTexture(LOCAL_GL_TEXTURE0, gfx::SamplingFilter::LINEAR);
962
0
963
0
    GLuint texID = 0;
964
0
    // This is horrid hack. It assumes that aGLContext matches the context
965
0
    // aSource has bound to.
966
0
    if (textureTarget == LOCAL_GL_TEXTURE_2D) {
967
0
        aGLContext->GetUIntegerv(LOCAL_GL_TEXTURE_BINDING_2D, &texID);
968
0
    } else if (textureTarget == LOCAL_GL_TEXTURE_EXTERNAL) {
969
0
        aGLContext->GetUIntegerv(LOCAL_GL_TEXTURE_BINDING_EXTERNAL, &texID);
970
0
    } else if (textureTarget == LOCAL_GL_TEXTURE_RECTANGLE) {
971
0
        aGLContext->GetUIntegerv(LOCAL_GL_TEXTURE_BINDING_RECTANGLE, &texID);
972
0
    }
973
0
974
0
    return texID;
975
0
}
976
977
void
978
SenderHelper::SendTextureSource(GLContext* aGLContext,
979
                                void* aLayerRef,
980
                                TextureSourceOGL* aSource,
981
                                bool aFlipY,
982
                                bool aIsMask,
983
                                UniquePtr<Packet> aPacket)
984
0
{
985
0
    MOZ_ASSERT(aGLContext);
986
0
    if (!aGLContext) {
987
0
        return;
988
0
    }
989
0
    GLuint texID = GetTextureID(aGLContext, aSource);
990
0
    if (HasTextureIdBeenSent(texID)) {
991
0
        return;
992
0
    }
993
0
994
0
    GLenum textureTarget = aSource->GetTextureTarget();
995
0
    ShaderConfigOGL config = ShaderConfigFromTargetAndFormat(textureTarget,
996
0
                                                             aSource->GetFormat());
997
0
    int shaderConfig = config.mFeatures;
998
0
999
0
    gfx::IntSize size = aSource->GetSize();
1000
0
1001
0
    // By sending 0 to ReadTextureImage rely upon aSource->BindTexture binding
1002
0
    // texture correctly. texID is used for tracking in DebugGLTextureData.
1003
0
    RefPtr<DataSourceSurface> img =
1004
0
        aGLContext->ReadTexImageHelper()->ReadTexImage(0, textureTarget,
1005
0
                                                         size,
1006
0
                                                         shaderConfig, aFlipY);
1007
0
    gLayerScopeManager.GetSocketManager()->AppendDebugData(
1008
0
        new DebugGLTextureData(aGLContext, aLayerRef, textureTarget,
1009
0
                               texID, img, aIsMask, std::move(aPacket)));
1010
0
1011
0
    sSentTextureIds.push_back(texID);
1012
0
    gLayerScopeManager.CurrentSession().mTexIDs.push_back(texID);
1013
0
1014
0
}
1015
1016
void
1017
SenderHelper::SetAndSendTexture(GLContext* aGLContext,
1018
                                void* aLayerRef,
1019
                                TextureSourceOGL* aSource,
1020
                                const TexturedEffect* aEffect)
1021
0
{
1022
0
    // Expose packet creation here, so we could dump primary texture effect attributes.
1023
0
    auto packet = MakeUnique<layerscope::Packet>();
1024
0
    layerscope::TexturePacket* texturePacket = packet->mutable_texture();
1025
0
    texturePacket->set_mpremultiplied(aEffect->mPremultiplied);
1026
0
    DumpFilter(texturePacket, aEffect->mSamplingFilter);
1027
0
    DumpRect(texturePacket->mutable_mtexturecoords(), aEffect->mTextureCoords);
1028
0
    SendTextureSource(aGLContext, aLayerRef, aSource, false, false, std::move(packet));
1029
0
}
1030
1031
void
1032
SenderHelper::SendTexturedEffect(GLContext* aGLContext,
1033
                                 void* aLayerRef,
1034
                                 const TexturedEffect* aEffect)
1035
0
{
1036
0
    TextureSourceOGL* source = aEffect->mTexture->AsSourceOGL();
1037
0
    if (!source) {
1038
0
        return;
1039
0
    }
1040
0
1041
0
    // Fallback texture sending path.
1042
0
    SetAndSendTexture(aGLContext, aLayerRef, source, aEffect);
1043
0
}
1044
1045
void
1046
SenderHelper::SendMaskEffect(GLContext* aGLContext,
1047
                                 void* aLayerRef,
1048
                                 const EffectMask* aEffect)
1049
0
{
1050
0
    TextureSourceOGL* source = aEffect->mMaskTexture->AsSourceOGL();
1051
0
    if (!source) {
1052
0
        return;
1053
0
    }
1054
0
1055
0
    // Expose packet creation here, so we could dump secondary mask effect attributes.
1056
0
    auto packet = MakeUnique<layerscope::Packet>();
1057
0
    TexturePacket::EffectMask* mask = packet->mutable_texture()->mutable_mask();
1058
0
    mask->mutable_msize()->set_w(aEffect->mSize.width);
1059
0
    mask->mutable_msize()->set_h(aEffect->mSize.height);
1060
0
    auto element = reinterpret_cast<const Float *>(&(aEffect->mMaskTransform));
1061
0
    for (int i = 0; i < 16; i++) {
1062
0
        mask->mutable_mmasktransform()->add_m(*element++);
1063
0
    }
1064
0
1065
0
    SendTextureSource(aGLContext, aLayerRef, source, false, true, std::move(packet));
1066
0
}
1067
1068
void
1069
SenderHelper::SendYCbCrEffect(GLContext* aGLContext,
1070
                              void* aLayerRef,
1071
                              const EffectYCbCr* aEffect)
1072
0
{
1073
0
    TextureSource* sourceYCbCr = aEffect->mTexture;
1074
0
    if (!sourceYCbCr)
1075
0
        return;
1076
0
1077
0
    const int Y = 0, Cb = 1, Cr = 2;
1078
0
    TextureSourceOGL *sources[] = {
1079
0
        sourceYCbCr->GetSubSource(Y)->AsSourceOGL(),
1080
0
        sourceYCbCr->GetSubSource(Cb)->AsSourceOGL(),
1081
0
        sourceYCbCr->GetSubSource(Cr)->AsSourceOGL()
1082
0
    };
1083
0
1084
0
    for (auto source: sources) {
1085
0
        SetAndSendTexture(aGLContext, aLayerRef, source, aEffect);
1086
0
    }
1087
0
}
1088
1089
void
1090
SenderHelper::SendEffectChain(GLContext* aGLContext,
1091
                              const EffectChain& aEffectChain,
1092
                              int aWidth,
1093
                              int aHeight)
1094
0
{
1095
0
    if (!sLayersBufferSendable) return;
1096
0
1097
0
    const Effect* primaryEffect = aEffectChain.mPrimaryEffect;
1098
0
    MOZ_ASSERT(primaryEffect);
1099
0
1100
0
    if (!primaryEffect) {
1101
0
      return;
1102
0
    }
1103
0
1104
0
    switch (primaryEffect->mType) {
1105
0
        case EffectTypes::RGB: {
1106
0
            const TexturedEffect* texturedEffect =
1107
0
                static_cast<const TexturedEffect*>(primaryEffect);
1108
0
            SendTexturedEffect(aGLContext, aEffectChain.mLayerRef, texturedEffect);
1109
0
            break;
1110
0
        }
1111
0
        case EffectTypes::YCBCR: {
1112
0
            const EffectYCbCr* yCbCrEffect =
1113
0
                static_cast<const EffectYCbCr*>(primaryEffect);
1114
0
            SendYCbCrEffect(aGLContext, aEffectChain.mLayerRef, yCbCrEffect);
1115
0
            break;
1116
0
        }
1117
0
        case EffectTypes::SOLID_COLOR: {
1118
0
            const EffectSolidColor* solidColorEffect =
1119
0
                static_cast<const EffectSolidColor*>(primaryEffect);
1120
0
            SendColor(aEffectChain.mLayerRef, solidColorEffect->mColor,
1121
0
                      aWidth, aHeight);
1122
0
            break;
1123
0
        }
1124
0
        case EffectTypes::COMPONENT_ALPHA:
1125
0
        case EffectTypes::RENDER_TARGET:
1126
0
        default:
1127
0
            break;
1128
0
    }
1129
0
1130
0
    if (aEffectChain.mSecondaryEffects[EffectTypes::MASK]) {
1131
0
        const EffectMask* effectMask =
1132
0
            static_cast<const EffectMask*>(aEffectChain.mSecondaryEffects[EffectTypes::MASK].get());
1133
0
        SendMaskEffect(aGLContext, aEffectChain.mLayerRef, effectMask);
1134
0
    }
1135
0
}
1136
1137
void
1138
LayerScope::ContentChanged(TextureHost *host)
1139
0
{
1140
0
    if (!CheckSendable()) {
1141
0
      return;
1142
0
    }
1143
0
1144
0
    gLayerScopeManager.GetContentMonitor()->SetChangedHost(host);
1145
0
}
1146
1147
// ----------------------------------------------
1148
// SocketHandler implementation
1149
// ----------------------------------------------
1150
void
1151
LayerScopeWebSocketManager::SocketHandler::OpenStream(nsISocketTransport* aTransport)
1152
0
{
1153
0
    MOZ_ASSERT(aTransport);
1154
0
1155
0
    mTransport = aTransport;
1156
0
    mTransport->OpenOutputStream(nsITransport::OPEN_BLOCKING,
1157
0
                                 0,
1158
0
                                 0,
1159
0
                                 getter_AddRefs(mOutputStream));
1160
0
1161
0
    nsCOMPtr<nsIInputStream> debugInputStream;
1162
0
    mTransport->OpenInputStream(0,
1163
0
                                0,
1164
0
                                0,
1165
0
                                getter_AddRefs(debugInputStream));
1166
0
    mInputStream = do_QueryInterface(debugInputStream);
1167
0
    mInputStream->AsyncWait(this, 0, 0, GetCurrentThreadEventTarget());
1168
0
}
1169
1170
bool
1171
LayerScopeWebSocketManager::SocketHandler::WriteToStream(void *aPtr,
1172
                                          uint32_t aSize)
1173
0
{
1174
0
    if (mState == NoHandshake) {
1175
0
        // Not yet handshake, just return true in case of
1176
0
        // LayerScope remove this handle
1177
0
        return true;
1178
0
    } else if (mState == HandshakeFailed) {
1179
0
        return false;
1180
0
    }
1181
0
1182
0
    if (!mOutputStream) {
1183
0
        return false;
1184
0
    }
1185
0
1186
0
    // Generate WebSocket header
1187
0
    uint8_t wsHeader[10];
1188
0
    int wsHeaderSize = 0;
1189
0
    const uint8_t opcode = 0x2;
1190
0
    wsHeader[0] = 0x80 | (opcode & 0x0f); // FIN + opcode;
1191
0
    if (aSize <= 125) {
1192
0
        wsHeaderSize = 2;
1193
0
        wsHeader[1] = aSize;
1194
0
    } else if (aSize < 65536) {
1195
0
        wsHeaderSize = 4;
1196
0
        wsHeader[1] = 0x7E;
1197
0
        NetworkEndian::writeUint16(wsHeader + 2, aSize);
1198
0
    } else {
1199
0
        wsHeaderSize = 10;
1200
0
        wsHeader[1] = 0x7F;
1201
0
        NetworkEndian::writeUint64(wsHeader + 2, aSize);
1202
0
    }
1203
0
1204
0
    // Send WebSocket header
1205
0
    nsresult rv;
1206
0
    uint32_t cnt;
1207
0
    rv = mOutputStream->Write(reinterpret_cast<char*>(wsHeader),
1208
0
                              wsHeaderSize, &cnt);
1209
0
    if (NS_FAILED(rv))
1210
0
        return false;
1211
0
1212
0
    uint32_t written = 0;
1213
0
    while (written < aSize) {
1214
0
        uint32_t cnt;
1215
0
        rv = mOutputStream->Write(reinterpret_cast<char*>(aPtr) + written,
1216
0
                                  aSize - written, &cnt);
1217
0
        if (NS_FAILED(rv))
1218
0
            return false;
1219
0
1220
0
        written += cnt;
1221
0
    }
1222
0
1223
0
    return true;
1224
0
}
1225
1226
NS_IMETHODIMP
1227
LayerScopeWebSocketManager::SocketHandler::OnInputStreamReady(nsIAsyncInputStream *aStream)
1228
0
{
1229
0
    MOZ_ASSERT(mInputStream);
1230
0
1231
0
    if (!mInputStream) {
1232
0
        return NS_OK;
1233
0
    }
1234
0
1235
0
    if (!mConnected) {
1236
0
        nsTArray<nsCString> protocolString;
1237
0
        ReadInputStreamData(protocolString);
1238
0
1239
0
        if (WebSocketHandshake(protocolString)) {
1240
0
            mState = HandshakeSuccess;
1241
0
            mConnected = true;
1242
0
            mInputStream->AsyncWait(this, 0, 0, GetCurrentThreadEventTarget());
1243
0
        } else {
1244
0
            mState = HandshakeFailed;
1245
0
        }
1246
0
        return NS_OK;
1247
0
    } else {
1248
0
        return HandleSocketMessage(aStream);
1249
0
    }
1250
0
}
1251
1252
void
1253
LayerScopeWebSocketManager::SocketHandler::ReadInputStreamData(nsTArray<nsCString>& aProtocolString)
1254
0
{
1255
0
    nsLineBuffer<char> lineBuffer;
1256
0
    nsCString line;
1257
0
    bool more = true;
1258
0
    do {
1259
0
        NS_ReadLine(mInputStream.get(), &lineBuffer, line, &more);
1260
0
1261
0
        if (line.Length() > 0) {
1262
0
            aProtocolString.AppendElement(line);
1263
0
        }
1264
0
    } while (more && line.Length() > 0);
1265
0
}
1266
1267
bool
1268
LayerScopeWebSocketManager::SocketHandler::WebSocketHandshake(nsTArray<nsCString>& aProtocolString)
1269
0
{
1270
0
    nsresult rv;
1271
0
    bool isWebSocket = false;
1272
0
    nsCString version;
1273
0
    nsCString wsKey;
1274
0
    nsCString protocol;
1275
0
1276
0
    // Validate WebSocket client request.
1277
0
    if (aProtocolString.Length() == 0)
1278
0
        return false;
1279
0
1280
0
    // Check that the HTTP method is GET
1281
0
    const char* HTTP_METHOD = "GET ";
1282
0
    if (strncmp(aProtocolString[0].get(), HTTP_METHOD, strlen(HTTP_METHOD)) != 0) {
1283
0
        return false;
1284
0
    }
1285
0
1286
0
    for (uint32_t i = 1; i < aProtocolString.Length(); ++i) {
1287
0
        const char* line = aProtocolString[i].get();
1288
0
        const char* prop_pos = strchr(line, ':');
1289
0
        if (prop_pos != nullptr) {
1290
0
            nsCString key(line, prop_pos - line);
1291
0
            nsCString value(prop_pos + 2);
1292
0
            if (key.EqualsIgnoreCase("upgrade") &&
1293
0
                value.EqualsIgnoreCase("websocket")) {
1294
0
                isWebSocket = true;
1295
0
            } else if (key.EqualsIgnoreCase("sec-websocket-version")) {
1296
0
                version = value;
1297
0
            } else if (key.EqualsIgnoreCase("sec-websocket-key")) {
1298
0
                wsKey = value;
1299
0
            } else if (key.EqualsIgnoreCase("sec-websocket-protocol")) {
1300
0
                protocol = value;
1301
0
            }
1302
0
        }
1303
0
    }
1304
0
1305
0
    if (!isWebSocket) {
1306
0
        return false;
1307
0
    }
1308
0
1309
0
    if (!(version.EqualsLiteral("7") ||
1310
0
          version.EqualsLiteral("8") ||
1311
0
          version.EqualsLiteral("13"))) {
1312
0
        return false;
1313
0
    }
1314
0
1315
0
    if (!(protocol.EqualsIgnoreCase("binary"))) {
1316
0
        return false;
1317
0
    }
1318
0
1319
0
    if (!mOutputStream) {
1320
0
        return false;
1321
0
    }
1322
0
1323
0
    // Client request is valid. Start to generate and send server response.
1324
0
    nsAutoCString guid("258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
1325
0
    nsAutoCString res;
1326
0
    SHA1Sum sha1;
1327
0
    nsCString combined(wsKey + guid);
1328
0
    sha1.update(combined.get(), combined.Length());
1329
0
    uint8_t digest[SHA1Sum::kHashSize]; // SHA1 digests are 20 bytes long.
1330
0
    sha1.finish(digest);
1331
0
    nsCString newString(reinterpret_cast<char*>(digest), SHA1Sum::kHashSize);
1332
0
    rv = Base64Encode(newString, res);
1333
0
    if (NS_FAILED(rv)) {
1334
0
        return false;
1335
0
    }
1336
0
    nsCString response("HTTP/1.1 101 Switching Protocols\r\n");
1337
0
    response.AppendLiteral("Upgrade: websocket\r\n");
1338
0
    response.AppendLiteral("Connection: Upgrade\r\n");
1339
0
    response.Append(nsCString("Sec-WebSocket-Accept: ") + res + nsCString("\r\n"));
1340
0
    response.AppendLiteral("Sec-WebSocket-Protocol: binary\r\n\r\n");
1341
0
    uint32_t written = 0;
1342
0
    uint32_t size = response.Length();
1343
0
    while (written < size) {
1344
0
        uint32_t cnt;
1345
0
        rv = mOutputStream->Write(const_cast<char*>(response.get()) + written,
1346
0
                                  size - written, &cnt);
1347
0
        if (NS_FAILED(rv))
1348
0
            return false;
1349
0
1350
0
        written += cnt;
1351
0
    }
1352
0
    mOutputStream->Flush();
1353
0
1354
0
    return true;
1355
0
}
1356
1357
nsresult
1358
LayerScopeWebSocketManager::SocketHandler::HandleSocketMessage(nsIAsyncInputStream *aStream)
1359
0
{
1360
0
    // The reading and parsing of this input stream is customized for layer viewer.
1361
0
    const uint32_t cPacketSize = 1024;
1362
0
    char buffer[cPacketSize];
1363
0
    uint32_t count = 0;
1364
0
    nsresult rv = NS_OK;
1365
0
1366
0
    do {
1367
0
        rv = mInputStream->Read((char *)buffer, cPacketSize, &count);
1368
0
1369
0
        // TODO: combine packets if we have to read more than once
1370
0
1371
0
        if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
1372
0
            mInputStream->AsyncWait(this, 0, 0, GetCurrentThreadEventTarget());
1373
0
            return NS_OK;
1374
0
        }
1375
0
1376
0
        if (NS_FAILED(rv)) {
1377
0
            break;
1378
0
        }
1379
0
1380
0
        if (count == 0) {
1381
0
            // NS_BASE_STREAM_CLOSED
1382
0
            CloseConnection();
1383
0
            break;
1384
0
        }
1385
0
1386
0
        rv = ProcessInput(reinterpret_cast<uint8_t *>(buffer), count);
1387
0
    } while (NS_SUCCEEDED(rv) && mInputStream);
1388
0
    return rv;
1389
0
}
1390
1391
nsresult
1392
LayerScopeWebSocketManager::SocketHandler::ProcessInput(uint8_t *aBuffer,
1393
                                         uint32_t aCount)
1394
0
{
1395
0
    uint32_t avail = aCount;
1396
0
1397
0
    // Decode Websocket data frame
1398
0
    if (avail <= 2) {
1399
0
        NS_WARNING("Packet size is less than 2 bytes");
1400
0
        return NS_OK;
1401
0
    }
1402
0
1403
0
    // First byte, data type, only care the opcode
1404
0
    // rsvBits: aBuffer[0] & 0x70 (0111 0000)
1405
0
    uint8_t finBit = aBuffer[0] & 0x80; // 1000 0000
1406
0
    uint8_t opcode = aBuffer[0] & 0x0F; // 0000 1111
1407
0
1408
0
    if (!finBit) {
1409
0
        NS_WARNING("We cannot handle multi-fragments messages in Layerscope websocket parser.");
1410
0
        return NS_OK;
1411
0
    }
1412
0
1413
0
    // Second byte, data length
1414
0
    uint8_t maskBit = aBuffer[1] & 0x80; // 1000 0000
1415
0
    int64_t payloadLength64 = aBuffer[1] & 0x7F; // 0111 1111
1416
0
1417
0
    if (!maskBit) {
1418
0
        NS_WARNING("Client to Server should set the mask bit");
1419
0
        return NS_OK;
1420
0
    }
1421
0
1422
0
    uint32_t framingLength = 2 + 4; // 4 for masks
1423
0
1424
0
    if (payloadLength64 < 126) {
1425
0
        if (avail < framingLength)
1426
0
            return NS_OK;
1427
0
    } else if (payloadLength64 == 126) {
1428
0
        // 16 bit length field
1429
0
        framingLength += 2;
1430
0
        if (avail < framingLength) {
1431
0
            return NS_OK;
1432
0
        }
1433
0
1434
0
        payloadLength64 = aBuffer[2] << 8 | aBuffer[3];
1435
0
    } else {
1436
0
        // 64 bit length
1437
0
        framingLength += 8;
1438
0
        if (avail < framingLength) {
1439
0
            return NS_OK;
1440
0
        }
1441
0
1442
0
        if (aBuffer[2] & 0x80) {
1443
0
            // Section 4.2 says that the most significant bit MUST be
1444
0
            // 0. (i.e. this is really a 63 bit value)
1445
0
            NS_WARNING("High bit of 64 bit length set");
1446
0
            return NS_ERROR_ILLEGAL_VALUE;
1447
0
        }
1448
0
1449
0
        // copy this in case it is unaligned
1450
0
        payloadLength64 = NetworkEndian::readInt64(aBuffer + 2);
1451
0
    }
1452
0
1453
0
    uint8_t *payload = aBuffer + framingLength;
1454
0
    avail -= framingLength;
1455
0
1456
0
    uint32_t payloadLength = static_cast<uint32_t>(payloadLength64);
1457
0
    if (avail < payloadLength) {
1458
0
        NS_WARNING("Packet size mismatch the payload length");
1459
0
        return NS_OK;
1460
0
    }
1461
0
1462
0
    // Apply mask
1463
0
    uint32_t mask = NetworkEndian::readUint32(payload - 4);
1464
0
    ApplyMask(mask, payload, payloadLength);
1465
0
1466
0
    if (opcode == 0x8) {
1467
0
        // opcode == 0x8 means connection close
1468
0
        CloseConnection();
1469
0
        return NS_BASE_STREAM_CLOSED;
1470
0
    }
1471
0
1472
0
    if (!HandleDataFrame(payload, payloadLength)) {
1473
0
        NS_WARNING("Cannot decode payload data by the protocol buffer");
1474
0
    }
1475
0
1476
0
    return NS_OK;
1477
0
}
1478
1479
void
1480
LayerScopeWebSocketManager::SocketHandler::ApplyMask(uint32_t aMask,
1481
                                      uint8_t *aData,
1482
                                      uint64_t aLen)
1483
0
{
1484
0
    if (!aData || aLen == 0) {
1485
0
        return;
1486
0
    }
1487
0
1488
0
    // Optimally we want to apply the mask 32 bits at a time,
1489
0
    // but the buffer might not be alligned. So we first deal with
1490
0
    // 0 to 3 bytes of preamble individually
1491
0
    while (aLen && (reinterpret_cast<uintptr_t>(aData) & 3)) {
1492
0
        *aData ^= aMask >> 24;
1493
0
        aMask = RotateLeft(aMask, 8);
1494
0
        aData++;
1495
0
        aLen--;
1496
0
    }
1497
0
1498
0
    // perform mask on full words of data
1499
0
    uint32_t *iData = reinterpret_cast<uint32_t *>(aData);
1500
0
    uint32_t *end = iData + (aLen >> 2);
1501
0
    NetworkEndian::writeUint32(&aMask, aMask);
1502
0
    for (; iData < end; iData++) {
1503
0
        *iData ^= aMask;
1504
0
    }
1505
0
    aMask = NetworkEndian::readUint32(&aMask);
1506
0
    aData = (uint8_t *)iData;
1507
0
    aLen  = aLen % 4;
1508
0
1509
0
    // There maybe up to 3 trailing bytes that need to be dealt with
1510
0
    // individually
1511
0
    while (aLen) {
1512
0
        *aData ^= aMask >> 24;
1513
0
        aMask = RotateLeft(aMask, 8);
1514
0
        aData++;
1515
0
        aLen--;
1516
0
    }
1517
0
}
1518
1519
bool
1520
LayerScopeWebSocketManager::SocketHandler::HandleDataFrame(uint8_t *aData,
1521
                                            uint32_t aSize)
1522
0
{
1523
0
    // Handle payload data by protocol buffer
1524
0
    auto p = MakeUnique<CommandPacket>();
1525
0
    p->ParseFromArray(static_cast<void*>(aData), aSize);
1526
0
1527
0
    if (!p->has_type()) {
1528
0
        MOZ_ASSERT(false, "Protocol buffer decoding failed or cannot recongize it");
1529
0
        return false;
1530
0
    }
1531
0
1532
0
    switch (p->type()) {
1533
0
        case CommandPacket::LAYERS_TREE:
1534
0
            if (p->has_value()) {
1535
0
                SenderHelper::SetLayersTreeSendable(p->value());
1536
0
            }
1537
0
            break;
1538
0
1539
0
        case CommandPacket::LAYERS_BUFFER:
1540
0
            if (p->has_value()) {
1541
0
                SenderHelper::SetLayersBufferSendable(p->value());
1542
0
            }
1543
0
            break;
1544
0
1545
0
        case CommandPacket::NO_OP:
1546
0
        default:
1547
0
            NS_WARNING("Invalid message type");
1548
0
            break;
1549
0
    }
1550
0
    return true;
1551
0
}
1552
1553
void
1554
LayerScopeWebSocketManager::SocketHandler::CloseConnection()
1555
0
{
1556
0
    gLayerScopeManager.GetSocketManager()->CleanDebugData();
1557
0
    if (mInputStream) {
1558
0
        mInputStream->AsyncWait(nullptr, 0, 0, nullptr);
1559
0
        mInputStream = nullptr;
1560
0
    }
1561
0
    if (mOutputStream) {
1562
0
        mOutputStream = nullptr;
1563
0
    }
1564
0
    if (mTransport) {
1565
0
        mTransport->Close(NS_BASE_STREAM_CLOSED);
1566
0
        mTransport = nullptr;
1567
0
    }
1568
0
    mConnected = false;
1569
0
}
1570
1571
// ----------------------------------------------
1572
// LayerScopeWebSocketManager implementation
1573
// ----------------------------------------------
1574
LayerScopeWebSocketManager::LayerScopeWebSocketManager()
1575
    : mHandlerMutex("LayerScopeWebSocketManager::mHandlerMutex")
1576
0
{
1577
0
    NS_NewNamedThread("LayerScope", getter_AddRefs(mDebugSenderThread));
1578
0
1579
0
    mServerSocket = do_CreateInstance(NS_SERVERSOCKET_CONTRACTID);
1580
0
    int port = gfxPrefs::LayerScopePort();
1581
0
    mServerSocket->Init(port, false, -1);
1582
0
    mServerSocket->AsyncListen(new SocketListener);
1583
0
}
1584
1585
LayerScopeWebSocketManager::~LayerScopeWebSocketManager()
1586
0
{
1587
0
    mServerSocket->Close();
1588
0
}
1589
1590
void
1591
LayerScopeWebSocketManager::AppendDebugData(DebugGLData *aDebugData)
1592
0
{
1593
0
    if (!mCurrentSender) {
1594
0
        mCurrentSender = new DebugDataSender(mDebugSenderThread);
1595
0
    }
1596
0
1597
0
    mCurrentSender->Append(aDebugData);
1598
0
}
1599
1600
void
1601
LayerScopeWebSocketManager::CleanDebugData()
1602
0
{
1603
0
    if (mCurrentSender) {
1604
0
        mCurrentSender->Cleanup();
1605
0
    }
1606
0
}
1607
1608
void
1609
LayerScopeWebSocketManager::DispatchDebugData()
1610
0
{
1611
0
    MOZ_ASSERT(mCurrentSender.get() != nullptr);
1612
0
1613
0
    mCurrentSender->Send();
1614
0
    mCurrentSender = nullptr;
1615
0
}
1616
1617
NS_IMETHODIMP LayerScopeWebSocketManager::SocketListener::OnSocketAccepted(
1618
                                     nsIServerSocket *aServ,
1619
                                     nsISocketTransport *aTransport)
1620
0
{
1621
0
    if (!gLayerScopeManager.GetSocketManager())
1622
0
        return NS_OK;
1623
0
1624
0
    printf_stderr("*** LayerScope: Accepted connection\n");
1625
0
    gLayerScopeManager.GetSocketManager()->AddConnection(aTransport);
1626
0
    gLayerScopeManager.GetContentMonitor()->Empty();
1627
0
    return NS_OK;
1628
0
}
1629
1630
// ----------------------------------------------
1631
// LayerScope implementation
1632
// ----------------------------------------------
1633
/*static*/
1634
void
1635
LayerScope::Init()
1636
0
{
1637
0
    if (!gfxPrefs::LayerScopeEnabled() || XRE_IsGPUProcess()) {
1638
0
        return;
1639
0
    }
1640
0
1641
0
    gLayerScopeManager.CreateServerSocket();
1642
0
}
1643
1644
/*static*/
1645
void
1646
LayerScope::DrawBegin()
1647
0
{
1648
0
    if (!CheckSendable()) {
1649
0
        return;
1650
0
    }
1651
0
1652
0
    gLayerScopeManager.NewDrawSession();
1653
0
}
1654
1655
/*static*/
1656
void
1657
LayerScope::SetRenderOffset(float aX, float aY)
1658
0
{
1659
0
    if (!CheckSendable()) {
1660
0
        return;
1661
0
    }
1662
0
1663
0
    gLayerScopeManager.CurrentSession().mOffsetX = aX;
1664
0
    gLayerScopeManager.CurrentSession().mOffsetY = aY;
1665
0
}
1666
1667
/*static*/
1668
void
1669
LayerScope::SetLayerTransform(const gfx::Matrix4x4& aMatrix)
1670
0
{
1671
0
    if (!CheckSendable()) {
1672
0
        return;
1673
0
    }
1674
0
1675
0
    gLayerScopeManager.CurrentSession().mMVMatrix = aMatrix;
1676
0
}
1677
1678
/*static*/
1679
void
1680
LayerScope::SetDrawRects(size_t aRects,
1681
                         const gfx::Rect* aLayerRects,
1682
                         const gfx::Rect* aTextureRects)
1683
0
{
1684
0
    if (!CheckSendable()) {
1685
0
        return;
1686
0
    }
1687
0
1688
0
    MOZ_ASSERT(aRects > 0 && aRects <= 4);
1689
0
    MOZ_ASSERT(aLayerRects);
1690
0
1691
0
    gLayerScopeManager.CurrentSession().mRects = aRects;
1692
0
1693
0
    for (size_t i = 0; i < aRects; i++){
1694
0
        gLayerScopeManager.CurrentSession().mLayerRects[i] = aLayerRects[i];
1695
0
        gLayerScopeManager.CurrentSession().mTextureRects[i] = aTextureRects[i];
1696
0
    }
1697
0
}
1698
1699
/*static*/
1700
void
1701
LayerScope::DrawEnd(gl::GLContext* aGLContext,
1702
                    const EffectChain& aEffectChain,
1703
                    int aWidth,
1704
                    int aHeight)
1705
0
{
1706
0
    // Protect this public function
1707
0
    if (!CheckSendable()) {
1708
0
        return;
1709
0
    }
1710
0
1711
0
    // 1. Send textures.
1712
0
    SenderHelper::SendEffectChain(aGLContext, aEffectChain, aWidth, aHeight);
1713
0
1714
0
    // 2. Send parameters of draw call, such as uniforms and attributes of
1715
0
    // vertex adnd fragment shader.
1716
0
    DrawSession& draws = gLayerScopeManager.CurrentSession();
1717
0
    gLayerScopeManager.GetSocketManager()->AppendDebugData(
1718
0
        new DebugGLDrawData(draws.mOffsetX, draws.mOffsetY,
1719
0
                            draws.mMVMatrix, draws.mRects,
1720
0
                            draws.mLayerRects,
1721
0
                            draws.mTextureRects,
1722
0
                            draws.mTexIDs,
1723
0
                            aEffectChain.mLayerRef));
1724
0
1725
0
}
1726
1727
/*static*/
1728
void
1729
LayerScope::SendLayer(LayerComposite* aLayer,
1730
                      int aWidth,
1731
                      int aHeight)
1732
0
{
1733
0
    // Protect this public function
1734
0
    if (!CheckSendable()) {
1735
0
        return;
1736
0
    }
1737
0
    SenderHelper::SendLayer(aLayer, aWidth, aHeight);
1738
0
}
1739
1740
/*static*/
1741
void
1742
LayerScope::SendLayerDump(UniquePtr<Packet> aPacket)
1743
0
{
1744
0
    // Protect this public function
1745
0
    if (!CheckSendable() || !SenderHelper::GetLayersTreeSendable()) {
1746
0
        return;
1747
0
    }
1748
0
    gLayerScopeManager.GetSocketManager()->AppendDebugData(
1749
0
        new DebugGLLayersData(std::move(aPacket)));
1750
0
}
1751
1752
/*static*/
1753
bool
1754
LayerScope::CheckSendable()
1755
0
{
1756
0
    // Only compositor threads check LayerScope status
1757
0
    MOZ_ASSERT(CompositorThreadHolder::IsInCompositorThread() || gIsGtest);
1758
0
1759
0
    if (!gfxPrefs::LayerScopeEnabled()) {
1760
0
        return false;
1761
0
    }
1762
0
    if (!gLayerScopeManager.GetSocketManager()) {
1763
0
        Init();
1764
0
        return false;
1765
0
    }
1766
0
    if (!gLayerScopeManager.GetSocketManager()->IsConnected()) {
1767
0
        return false;
1768
0
    }
1769
0
    return true;
1770
0
}
1771
1772
/*static*/
1773
void
1774
LayerScope::CleanLayer()
1775
0
{
1776
0
    if (CheckSendable()) {
1777
0
        gLayerScopeManager.GetSocketManager()->CleanDebugData();
1778
0
    }
1779
0
}
1780
1781
/*static*/
1782
void
1783
LayerScope::SetHWComposed()
1784
0
{
1785
0
    if (CheckSendable()) {
1786
0
        gLayerScopeManager.GetSocketManager()->AppendDebugData(
1787
0
            new DebugGLMetaData(Packet::META, true));
1788
0
    }
1789
0
}
1790
1791
/*static*/
1792
void
1793
LayerScope::SetPixelScale(double devPixelsPerCSSPixel)
1794
0
{
1795
0
    gLayerScopeManager.SetPixelScale(devPixelsPerCSSPixel);
1796
0
}
1797
1798
// ----------------------------------------------
1799
// LayerScopeAutoFrame implementation
1800
// ----------------------------------------------
1801
LayerScopeAutoFrame::LayerScopeAutoFrame(int64_t aFrameStamp)
1802
0
{
1803
0
    // Do Begin Frame
1804
0
    BeginFrame(aFrameStamp);
1805
0
}
1806
1807
LayerScopeAutoFrame::~LayerScopeAutoFrame()
1808
0
{
1809
0
    // Do End Frame
1810
0
    EndFrame();
1811
0
}
1812
1813
void
1814
LayerScopeAutoFrame::BeginFrame(int64_t aFrameStamp)
1815
0
{
1816
0
    if (!LayerScope::CheckSendable()) {
1817
0
        return;
1818
0
    }
1819
0
    SenderHelper::ClearSentTextureIds();
1820
0
1821
0
    gLayerScopeManager.GetSocketManager()->AppendDebugData(
1822
0
        new DebugGLFrameStatusData(Packet::FRAMESTART, aFrameStamp));
1823
0
}
1824
1825
void
1826
LayerScopeAutoFrame::EndFrame()
1827
0
{
1828
0
    if (!LayerScope::CheckSendable()) {
1829
0
        return;
1830
0
    }
1831
0
1832
0
    gLayerScopeManager.GetSocketManager()->AppendDebugData(
1833
0
        new DebugGLFrameStatusData(Packet::FRAMEEND));
1834
0
    gLayerScopeManager.GetSocketManager()->DispatchDebugData();
1835
0
}
1836
1837
} // namespace layers
1838
} // namespace mozilla