2017-01-06 21:10:15 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2017-10-28 02:10:06 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2017-01-06 21:10:15 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#ifndef MOZILLA_LAYERS_RENDERTHREAD_H
|
|
|
|
#define MOZILLA_LAYERS_RENDERTHREAD_H
|
|
|
|
|
|
|
|
#include "base/basictypes.h" // for DISALLOW_EVIL_CONSTRUCTORS
|
|
|
|
#include "base/platform_thread.h" // for PlatformThreadId
|
|
|
|
#include "base/thread.h" // for Thread
|
|
|
|
#include "base/message_loop.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "ThreadSafeRefcountingWithMainThreadDestruction.h"
|
2018-10-17 01:54:01 +03:00
|
|
|
#include "mozilla/gfx/Point.h"
|
2018-09-08 02:03:13 +03:00
|
|
|
#include "mozilla/MozPromise.h"
|
2017-03-07 13:37:28 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2017-01-17 03:22:01 +03:00
|
|
|
#include "mozilla/webrender/webrender_ffi.h"
|
2017-01-06 21:10:15 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2017-01-17 03:21:52 +03:00
|
|
|
#include "mozilla/webrender/WebRenderTypes.h"
|
2017-07-03 15:08:22 +03:00
|
|
|
#include "mozilla/layers/SynchronousTask.h"
|
2018-10-04 05:54:50 +03:00
|
|
|
#include "GLContext.h"
|
2017-01-06 21:10:15 +03:00
|
|
|
|
2018-06-15 01:18:48 +03:00
|
|
|
#include <list>
|
2018-06-25 17:55:26 +03:00
|
|
|
#include <queue>
|
2018-06-25 17:55:19 +03:00
|
|
|
#include <unordered_map>
|
2018-06-15 01:18:48 +03:00
|
|
|
|
2017-01-06 21:10:15 +03:00
|
|
|
namespace mozilla {
|
2017-01-17 03:22:09 +03:00
|
|
|
namespace wr {
|
2017-01-06 21:10:15 +03:00
|
|
|
|
2018-09-08 02:03:13 +03:00
|
|
|
typedef MozPromise<MemoryReport, bool, true> MemoryReportPromise;
|
|
|
|
|
2017-01-06 21:10:15 +03:00
|
|
|
class RendererOGL;
|
2017-03-07 13:37:28 +03:00
|
|
|
class RenderTextureHost;
|
2017-01-06 21:10:15 +03:00
|
|
|
class RenderThread;
|
|
|
|
|
2017-06-13 18:57:16 +03:00
|
|
|
/// A rayon thread pool that is shared by all WebRender instances within a process.
|
|
|
|
class WebRenderThreadPool {
|
|
|
|
public:
|
|
|
|
WebRenderThreadPool();
|
|
|
|
|
|
|
|
~WebRenderThreadPool();
|
|
|
|
|
2017-06-28 02:20:36 +03:00
|
|
|
wr::WrThreadPool* Raw() { return mThreadPool; }
|
2017-06-13 18:57:16 +03:00
|
|
|
|
|
|
|
protected:
|
2017-06-28 02:20:36 +03:00
|
|
|
wr::WrThreadPool* mThreadPool;
|
2017-06-13 18:57:16 +03:00
|
|
|
};
|
|
|
|
|
2017-11-24 14:58:24 +03:00
|
|
|
class WebRenderProgramCache {
|
|
|
|
public:
|
2018-05-31 09:07:34 +03:00
|
|
|
explicit WebRenderProgramCache(wr::WrThreadPool* aThreadPool);
|
2017-11-24 14:58:24 +03:00
|
|
|
|
|
|
|
~WebRenderProgramCache();
|
|
|
|
|
|
|
|
wr::WrProgramCache* Raw() { return mProgramCache; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
wr::WrProgramCache* mProgramCache;
|
|
|
|
};
|
2017-06-13 18:57:16 +03:00
|
|
|
|
2018-10-10 06:20:20 +03:00
|
|
|
class WebRenderShaders {
|
|
|
|
public:
|
|
|
|
WebRenderShaders(gl::GLContext* gl, WebRenderProgramCache* programCache);
|
|
|
|
~WebRenderShaders();
|
|
|
|
|
|
|
|
wr::WrShaders* RawShaders() { return mShaders; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
RefPtr<gl::GLContext> mGL;
|
|
|
|
wr::WrShaders* mShaders;
|
|
|
|
};
|
|
|
|
|
2017-01-25 00:06:17 +03:00
|
|
|
/// Base class for an event that can be scheduled to run on the render thread.
|
2017-01-06 21:10:20 +03:00
|
|
|
///
|
|
|
|
/// The event can be passed through the same channels as regular WebRender messages
|
|
|
|
/// to preserve ordering.
|
2017-01-06 21:10:15 +03:00
|
|
|
class RendererEvent
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~RendererEvent() {}
|
2017-01-17 03:22:09 +03:00
|
|
|
virtual void Run(RenderThread& aRenderThread, wr::WindowId aWindow) = 0;
|
2017-01-06 21:10:15 +03:00
|
|
|
};
|
|
|
|
|
2017-01-06 21:10:20 +03:00
|
|
|
/// The render thread is where WebRender issues all of its GPU work, and as much
|
|
|
|
/// as possible this thread should only serve this purpose.
|
|
|
|
///
|
|
|
|
/// The render thread owns the different RendererOGLs (one per window) and implements
|
|
|
|
/// the RenderNotifier api exposed by the WebRender bindings.
|
|
|
|
///
|
2017-01-25 00:06:17 +03:00
|
|
|
/// We should generally avoid posting tasks to the render thread's event loop directly
|
2017-01-06 21:10:20 +03:00
|
|
|
/// and instead use the RendererEvent mechanism which avoids races between the events
|
|
|
|
/// and WebRender's own messages.
|
|
|
|
///
|
|
|
|
/// The GL context(s) should be created and used on this thread only.
|
|
|
|
/// XXX - I've tried to organize code so that we can potentially avoid making
|
2017-01-25 00:06:17 +03:00
|
|
|
/// this a singleton since this bad habit has a tendency to bite us later, but
|
|
|
|
/// I haven't gotten all the way there either, in order to focus on the more
|
2017-01-06 21:10:20 +03:00
|
|
|
/// important pieces first. So we are a bit in-between (this is totally a singleton
|
|
|
|
/// but in some places we pretend it's not). Hopefully we can evolve this in a way
|
|
|
|
/// that keeps the door open to removing the singleton bits.
|
2017-01-06 21:10:15 +03:00
|
|
|
class RenderThread final
|
|
|
|
{
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_MAIN_THREAD_DESTRUCTION(RenderThread)
|
|
|
|
|
|
|
|
public:
|
2017-01-06 21:10:20 +03:00
|
|
|
/// Can be called from any thread.
|
2017-01-06 21:10:15 +03:00
|
|
|
static RenderThread* Get();
|
|
|
|
|
2017-01-25 00:06:17 +03:00
|
|
|
/// Can only be called from the main thread.
|
2017-01-06 21:10:15 +03:00
|
|
|
static void Start();
|
|
|
|
|
2017-01-25 00:06:17 +03:00
|
|
|
/// Can only be called from the main thread.
|
2017-01-06 21:10:15 +03:00
|
|
|
static void ShutDown();
|
|
|
|
|
2017-01-06 21:10:20 +03:00
|
|
|
/// Can be called from any thread.
|
2017-01-25 00:06:17 +03:00
|
|
|
/// In most cases it is best to post RendererEvents through WebRenderAPI instead
|
2017-01-06 21:10:20 +03:00
|
|
|
/// of scheduling directly to this message loop (so as to preserve the ordering
|
|
|
|
/// of the messages).
|
2017-01-06 21:10:15 +03:00
|
|
|
static MessageLoop* Loop();
|
|
|
|
|
2017-01-06 21:10:20 +03:00
|
|
|
/// Can be called from any thread.
|
2017-01-06 21:10:15 +03:00
|
|
|
static bool IsInRenderThread();
|
|
|
|
|
2018-09-08 02:03:13 +03:00
|
|
|
// Can be called from any thread. Dispatches an event to the Renderer thread
|
|
|
|
// to iterate over all Renderers, accumulates memory statistics, and resolves
|
|
|
|
// the return promise.
|
|
|
|
static RefPtr<MemoryReportPromise> AccumulateMemoryReport(MemoryReport aInitial);
|
|
|
|
|
2017-01-06 21:10:20 +03:00
|
|
|
/// Can only be called from the render thread.
|
2017-01-17 03:22:09 +03:00
|
|
|
void AddRenderer(wr::WindowId aWindowId, UniquePtr<RendererOGL> aRenderer);
|
2017-01-06 21:10:15 +03:00
|
|
|
|
2017-01-06 21:10:20 +03:00
|
|
|
/// Can only be called from the render thread.
|
2017-01-17 03:22:09 +03:00
|
|
|
void RemoveRenderer(wr::WindowId aWindowId);
|
2017-01-06 21:10:15 +03:00
|
|
|
|
2017-01-16 17:22:47 +03:00
|
|
|
/// Can only be called from the render thread.
|
2017-01-17 03:22:09 +03:00
|
|
|
RendererOGL* GetRenderer(wr::WindowId aWindowId);
|
2017-01-16 17:22:47 +03:00
|
|
|
|
2017-01-06 21:10:20 +03:00
|
|
|
// RenderNotifier implementation
|
|
|
|
|
|
|
|
/// Automatically forwarded to the render thread.
|
2018-10-02 06:13:22 +03:00
|
|
|
void HandleFrame(wr::WindowId aWindowId, bool aRender);
|
2017-01-06 21:10:15 +03:00
|
|
|
|
2018-02-13 22:08:54 +03:00
|
|
|
/// Automatically forwarded to the render thread.
|
|
|
|
void WakeUp(wr::WindowId aWindowId);
|
|
|
|
|
2017-01-06 21:10:20 +03:00
|
|
|
/// Automatically forwarded to the render thread.
|
2017-01-17 03:22:09 +03:00
|
|
|
void PipelineSizeChanged(wr::WindowId aWindowId, uint64_t aPipelineId, float aWidth, float aHeight);
|
2017-01-06 21:10:15 +03:00
|
|
|
|
2017-01-06 21:10:20 +03:00
|
|
|
/// Automatically forwarded to the render thread.
|
2017-01-17 03:22:09 +03:00
|
|
|
void RunEvent(wr::WindowId aWindowId, UniquePtr<RendererEvent> aCallBack);
|
2017-01-06 21:10:15 +03:00
|
|
|
|
2017-01-25 07:29:34 +03:00
|
|
|
/// Can only be called from the render thread.
|
2018-11-04 23:35:36 +03:00
|
|
|
void UpdateAndRender(wr::WindowId aWindowId,
|
|
|
|
const TimeStamp& aStartTime,
|
|
|
|
bool aRender,
|
|
|
|
const Maybe<gfx::IntSize>& aReadbackSize,
|
|
|
|
const Maybe<Range<uint8_t>>& aReadbackBuffer,
|
|
|
|
bool aHadSlowFrame);
|
2017-03-07 13:37:28 +03:00
|
|
|
|
2017-04-05 17:12:11 +03:00
|
|
|
void Pause(wr::WindowId aWindowId);
|
|
|
|
bool Resume(wr::WindowId aWindowId);
|
|
|
|
|
2017-06-07 18:44:05 +03:00
|
|
|
/// Can be called from any thread.
|
|
|
|
void RegisterExternalImage(uint64_t aExternalImageId, already_AddRefed<RenderTextureHost> aTexture);
|
2017-03-07 13:37:28 +03:00
|
|
|
|
2017-06-07 18:44:05 +03:00
|
|
|
/// Can be called from any thread.
|
2017-03-07 13:37:28 +03:00
|
|
|
void UnregisterExternalImage(uint64_t aExternalImageId);
|
|
|
|
|
2018-07-25 17:30:08 +03:00
|
|
|
/// Can be called from any thread.
|
|
|
|
void UpdateRenderTextureHost(uint64_t aSrcExternalImageId, uint64_t aWrappedExternalImageId);
|
|
|
|
|
2018-04-23 14:57:17 +03:00
|
|
|
/// Can only be called from the render thread.
|
|
|
|
void UnregisterExternalImageDuringShutdown(uint64_t aExternalImageId);
|
|
|
|
|
2017-06-07 18:44:05 +03:00
|
|
|
/// Can only be called from the render thread.
|
2017-04-13 22:31:09 +03:00
|
|
|
RenderTextureHost* GetRenderTexture(WrExternalImageId aExternalImageId);
|
2017-03-07 13:37:28 +03:00
|
|
|
|
2017-11-24 12:34:50 +03:00
|
|
|
/// Can be called from any thread.
|
|
|
|
bool IsDestroyed(wr::WindowId aWindowId);
|
|
|
|
/// Can be called from any thread.
|
|
|
|
void SetDestroyed(wr::WindowId aWindowId);
|
2017-05-19 03:21:38 +03:00
|
|
|
/// Can be called from any thread.
|
2017-11-03 05:22:28 +03:00
|
|
|
bool TooManyPendingFrames(wr::WindowId aWindowId);
|
2017-05-19 03:21:38 +03:00
|
|
|
/// Can be called from any thread.
|
2018-06-25 17:55:26 +03:00
|
|
|
void IncPendingFrameCount(wr::WindowId aWindowId, const TimeStamp& aStartTime);
|
2017-05-19 03:21:38 +03:00
|
|
|
/// Can be called from any thread.
|
2018-04-27 14:41:03 +03:00
|
|
|
void DecPendingFrameCount(wr::WindowId aWindowId);
|
|
|
|
/// Can be called from any thread.
|
2017-11-03 05:22:28 +03:00
|
|
|
void IncRenderingFrameCount(wr::WindowId aWindowId);
|
|
|
|
/// Can be called from any thread.
|
2018-04-27 14:41:03 +03:00
|
|
|
void FrameRenderingComplete(wr::WindowId aWindowId);
|
2017-05-19 03:21:38 +03:00
|
|
|
|
2018-11-04 23:35:36 +03:00
|
|
|
void NotifySlowFrame(wr::WindowId aWindowId);
|
|
|
|
|
2017-06-13 18:57:16 +03:00
|
|
|
/// Can be called from any thread.
|
|
|
|
WebRenderThreadPool& ThreadPool() { return mThreadPool; }
|
|
|
|
|
2017-11-24 14:58:24 +03:00
|
|
|
/// Can only be called from the render thread.
|
|
|
|
WebRenderProgramCache* ProgramCache();
|
|
|
|
|
2018-10-04 05:54:50 +03:00
|
|
|
/// Can only be called from the render thread.
|
2018-10-10 06:20:20 +03:00
|
|
|
WebRenderShaders* Shaders() { return mShaders.get(); }
|
|
|
|
|
2018-10-04 05:54:50 +03:00
|
|
|
/// Can only be called from the render thread.
|
|
|
|
gl::GLContext* SharedGL();
|
|
|
|
|
|
|
|
void ClearSharedGL();
|
|
|
|
|
2018-06-13 18:01:06 +03:00
|
|
|
/// Can only be called from the render thread.
|
2018-06-19 13:27:37 +03:00
|
|
|
void HandleDeviceReset(const char* aWhere, bool aNotify);
|
2018-06-13 18:01:06 +03:00
|
|
|
/// Can only be called from the render thread.
|
|
|
|
bool IsHandlingDeviceReset();
|
2018-06-19 13:27:37 +03:00
|
|
|
/// Can be called from any thread.
|
|
|
|
void SimulateDeviceReset();
|
2018-06-13 18:01:06 +03:00
|
|
|
|
|
|
|
size_t RendererCount();
|
|
|
|
|
2017-01-06 21:10:15 +03:00
|
|
|
private:
|
|
|
|
explicit RenderThread(base::Thread* aThread);
|
|
|
|
|
2018-06-15 01:18:48 +03:00
|
|
|
void DeferredRenderTextureHostDestroy();
|
2017-07-03 15:08:22 +03:00
|
|
|
void ShutDownTask(layers::SynchronousTask* aTask);
|
2018-10-10 06:20:20 +03:00
|
|
|
void InitDeviceTask();
|
2017-06-07 18:44:05 +03:00
|
|
|
|
2018-09-08 02:03:13 +03:00
|
|
|
void DoAccumulateMemoryReport(MemoryReport, const RefPtr<MemoryReportPromise::Private>&);
|
|
|
|
|
2017-01-06 21:10:15 +03:00
|
|
|
~RenderThread();
|
|
|
|
|
|
|
|
base::Thread* const mThread;
|
|
|
|
|
2017-06-13 18:57:16 +03:00
|
|
|
WebRenderThreadPool mThreadPool;
|
2018-10-10 06:20:20 +03:00
|
|
|
|
2017-11-24 14:58:24 +03:00
|
|
|
UniquePtr<WebRenderProgramCache> mProgramCache;
|
2018-10-10 06:20:20 +03:00
|
|
|
UniquePtr<WebRenderShaders> mShaders;
|
2017-06-13 18:57:16 +03:00
|
|
|
|
2018-10-04 05:54:50 +03:00
|
|
|
// An optional shared GLContext to be used for all
|
|
|
|
// windows.
|
|
|
|
RefPtr<gl::GLContext> mSharedGL;
|
|
|
|
|
2017-01-17 03:22:09 +03:00
|
|
|
std::map<wr::WindowId, UniquePtr<RendererOGL>> mRenderers;
|
2017-03-07 13:37:28 +03:00
|
|
|
|
2017-11-24 12:34:50 +03:00
|
|
|
struct WindowInfo {
|
|
|
|
bool mIsDestroyed = false;
|
2017-11-03 05:22:28 +03:00
|
|
|
int64_t mPendingCount = 0;
|
|
|
|
int64_t mRenderingCount = 0;
|
2018-06-25 17:55:26 +03:00
|
|
|
// One entry in this queue for each pending frame, so the length
|
|
|
|
// should always equal mPendingCount
|
|
|
|
std::queue<TimeStamp> mStartTimes;
|
2018-11-04 23:35:36 +03:00
|
|
|
bool mHadSlowFrame = false;
|
2017-11-03 05:22:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
Mutex mFrameCountMapLock;
|
2018-06-25 17:55:19 +03:00
|
|
|
std::unordered_map<uint64_t, WindowInfo*> mWindowInfos;
|
2017-05-19 03:21:38 +03:00
|
|
|
|
2017-03-07 13:37:28 +03:00
|
|
|
Mutex mRenderTextureMapLock;
|
2018-07-10 12:15:47 +03:00
|
|
|
std::unordered_map<uint64_t, RefPtr<RenderTextureHost>> mRenderTextures;
|
2018-06-15 01:18:48 +03:00
|
|
|
// Used to remove all RenderTextureHost that are going to be removed by
|
|
|
|
// a deferred callback and remove them right away without waiting for the callback.
|
|
|
|
// On device reset we have to remove all GL related resources right away.
|
|
|
|
std::list<RefPtr<RenderTextureHost>> mRenderTexturesDeferred;
|
2017-07-03 15:08:22 +03:00
|
|
|
bool mHasShutdown;
|
2018-06-13 18:01:06 +03:00
|
|
|
|
|
|
|
bool mHandlingDeviceReset;
|
2017-01-06 21:10:15 +03:00
|
|
|
};
|
|
|
|
|
2017-01-25 00:06:17 +03:00
|
|
|
} // namespace wr
|
|
|
|
} // namespace mozilla
|
2017-01-06 21:10:15 +03:00
|
|
|
|
|
|
|
#endif
|