tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

KnowsCompositor.cpp (3861B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      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 #include "KnowsCompositor.h"
      8 #include "mozilla/layers/ImageDataSerializer.h"
      9 #include "mozilla/layers/ImageBridgeChild.h"
     10 #include "mozilla/ipc/ProtocolUtils.h"
     11 
     12 namespace mozilla::layers {
     13 
     14 void KnowsCompositor::IdentifyTextureHost(
     15    const TextureFactoryIdentifier& aIdentifier) {
     16  auto lock = mData.Lock();
     17  lock.ref().mTextureFactoryIdentifier = aIdentifier;
     18 
     19  lock.ref().mSyncObject =
     20      SyncObjectClient::CreateSyncObjectClientForContentDevice(
     21          aIdentifier.mSyncHandle);
     22 }
     23 
     24 KnowsCompositor::KnowsCompositor()
     25    : mData("KnowsCompositorMutex"), mSerial(++sSerialCounter) {}
     26 
     27 KnowsCompositor::~KnowsCompositor() = default;
     28 
     29 KnowsCompositorMediaProxy::KnowsCompositorMediaProxy(
     30    const TextureFactoryIdentifier& aIdentifier) {
     31  auto lock = mData.Lock();
     32  lock.ref().mTextureFactoryIdentifier = aIdentifier;
     33  // overwrite mSerial's value set by the parent class because we use the same
     34  // serial as the KnowsCompositor we are proxying.
     35  mThreadSafeAllocator = ImageBridgeChild::GetSingleton();
     36  lock.ref().mSyncObject = mThreadSafeAllocator->GetSyncObject();
     37 }
     38 
     39 KnowsCompositorMediaProxy::~KnowsCompositorMediaProxy() = default;
     40 
     41 TextureForwarder* KnowsCompositorMediaProxy::GetTextureForwarder() {
     42  return mThreadSafeAllocator->GetTextureForwarder();
     43 }
     44 
     45 LayersIPCActor* KnowsCompositorMediaProxy::GetLayersIPCActor() {
     46  return mThreadSafeAllocator->GetLayersIPCActor();
     47 }
     48 
     49 void KnowsCompositorMediaProxy::SyncWithCompositor(
     50    const Maybe<uint64_t>& aWindowID) {
     51  mThreadSafeAllocator->SyncWithCompositor(aWindowID);
     52 }
     53 
     54 bool IsSurfaceDescriptorValid(const SurfaceDescriptor& aSurface) {
     55  return aSurface.type() != SurfaceDescriptor::T__None &&
     56         aSurface.type() != SurfaceDescriptor::Tnull_t;
     57 }
     58 
     59 uint8_t* GetAddressFromDescriptor(const SurfaceDescriptor& aDescriptor) {
     60  MOZ_ASSERT(IsSurfaceDescriptorValid(aDescriptor));
     61  MOZ_RELEASE_ASSERT(
     62      aDescriptor.type() == SurfaceDescriptor::TSurfaceDescriptorBuffer,
     63      "GFX: surface descriptor is not the right type.");
     64 
     65  auto memOrShmem = aDescriptor.get_SurfaceDescriptorBuffer().data();
     66  if (memOrShmem.type() == MemoryOrShmem::TShmem) {
     67    return memOrShmem.get_Shmem().get<uint8_t>();
     68  } else {
     69    return reinterpret_cast<uint8_t*>(memOrShmem.get_uintptr_t());
     70  }
     71 }
     72 
     73 already_AddRefed<gfx::DataSourceSurface> GetSurfaceForDescriptor(
     74    const SurfaceDescriptor& aDescriptor) {
     75  if (aDescriptor.type() != SurfaceDescriptor::TSurfaceDescriptorBuffer) {
     76    return nullptr;
     77  }
     78  uint8_t* data = GetAddressFromDescriptor(aDescriptor);
     79  auto rgb =
     80      aDescriptor.get_SurfaceDescriptorBuffer().desc().get_RGBDescriptor();
     81  uint32_t stride = ImageDataSerializer::GetRGBStride(rgb);
     82  return gfx::Factory::CreateWrappingDataSourceSurface(data, stride, rgb.size(),
     83                                                       rgb.format());
     84 }
     85 
     86 void DestroySurfaceDescriptor(ipc::IShmemAllocator* aAllocator,
     87                              SurfaceDescriptor* aSurface) {
     88  MOZ_ASSERT(aSurface);
     89 
     90  SurfaceDescriptorBuffer& desc = aSurface->get_SurfaceDescriptorBuffer();
     91  switch (desc.data().type()) {
     92    case MemoryOrShmem::TShmem: {
     93      aAllocator->DeallocShmem(desc.data().get_Shmem());
     94      break;
     95    }
     96    case MemoryOrShmem::Tuintptr_t: {
     97      uint8_t* ptr = (uint8_t*)desc.data().get_uintptr_t();
     98      GfxMemoryImageReporter::WillFree(ptr);
     99      delete[] ptr;
    100      break;
    101    }
    102    default:
    103      MOZ_CRASH("surface type not implemented!");
    104  }
    105  *aSurface = SurfaceDescriptor();
    106 }
    107 
    108 }  // namespace mozilla::layers