gfxPlatformWorker.cpp (1883B)
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #include "gfxPlatformWorker.h" 7 #include "mozilla/dom/WorkerCommon.h" 8 #include "mozilla/dom/WorkerRef.h" 9 #include "mozilla/gfx/2D.h" 10 #include "mozilla/ThreadLocal.h" 11 12 using namespace mozilla; 13 using namespace mozilla::dom; 14 using namespace mozilla::gfx; 15 16 MOZ_THREAD_LOCAL(gfxPlatformWorker*) gfxPlatformWorker::sInstance; 17 18 /* static */ gfxPlatformWorker* gfxPlatformWorker::Get() { 19 if (!sInstance.init()) { 20 return nullptr; 21 } 22 23 gfxPlatformWorker* instance = sInstance.get(); 24 if (instance) { 25 return instance; 26 } 27 28 WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); 29 if (!workerPrivate) { 30 return nullptr; 31 } 32 33 RefPtr<WeakWorkerRef> workerRef = WeakWorkerRef::Create( 34 workerPrivate, []() { gfxPlatformWorker::Shutdown(); }); 35 if (!workerRef) { 36 return nullptr; 37 } 38 39 instance = new gfxPlatformWorker(std::move(workerRef)); 40 sInstance.set(instance); 41 return instance; 42 } 43 44 /* static */ void gfxPlatformWorker::Shutdown() { 45 if (!sInstance.init()) { 46 return; 47 } 48 49 gfxPlatformWorker* instance = sInstance.get(); 50 if (!instance) { 51 return; 52 } 53 54 sInstance.set(nullptr); 55 delete instance; 56 } 57 58 gfxPlatformWorker::gfxPlatformWorker(RefPtr<WeakWorkerRef>&& aWorkerRef) 59 : mWorkerRef(std::move(aWorkerRef)) {} 60 61 gfxPlatformWorker::~gfxPlatformWorker() = default; 62 63 RefPtr<mozilla::gfx::DrawTarget> 64 gfxPlatformWorker::ScreenReferenceDrawTarget() { 65 if (!mScreenReferenceDrawTarget) { 66 mScreenReferenceDrawTarget = Factory::CreateDrawTarget( 67 BackendType::SKIA, IntSize(1, 1), SurfaceFormat::B8G8R8A8); 68 } 69 return mScreenReferenceDrawTarget; 70 }