tor-browser

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

CompositableTransactionParent.cpp (6116B)


      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 "CompositableTransactionParent.h"
      8 #include "CompositableHost.h"        // for CompositableParent, etc
      9 #include "CompositorBridgeParent.h"  // for CompositorBridgeParent
     10 #include "mozilla/Assertions.h"      // for MOZ_ASSERT, etc
     11 #include "mozilla/Logging.h"
     12 #include "mozilla/RefPtr.h"  // for RefPtr
     13 #include "mozilla/layers/CompositorTypes.h"
     14 #include "mozilla/layers/ImageBridgeParent.h"  // for ImageBridgeParent
     15 #include "mozilla/layers/LayersSurfaces.h"     // for SurfaceDescriptor
     16 #include "mozilla/layers/LayersTypes.h"        // for MOZ_LAYERS_LOG
     17 #include "mozilla/layers/TextureHost.h"        // for TextureHost
     18 #include "mozilla/layers/WebRenderImageHost.h"
     19 #include "mozilla/mozalloc.h"  // for operator delete
     20 #include "nsDebug.h"           // for NS_WARNING, NS_ASSERTION
     21 #include "nsRegion.h"          // for nsIntRegion
     22 
     23 namespace mozilla {
     24 namespace layers {
     25 
     26 mozilla::LazyLogModule gCompositableTextureParentLog(
     27    "CompositableTextureParent");
     28 
     29 bool CompositableParentManager::ReceiveCompositableUpdate(
     30    const CompositableOperation& aEdit) {
     31  // Ignore all operations on compositables created on stale compositors. We
     32  // return true because the child is unable to handle errors.
     33  RefPtr<CompositableHost> compositable =
     34      FindCompositable(aEdit.compositable());
     35  if (!compositable) {
     36    return false;
     37  }
     38  return ReceiveCompositableUpdate(aEdit.detail(), WrapNotNull(compositable),
     39                                   aEdit.compositable());
     40 }
     41 
     42 bool CompositableParentManager::ReceiveCompositableUpdate(
     43    const CompositableOperationDetail& aDetail,
     44    NotNull<CompositableHost*> aCompositable,
     45    const CompositableHandle& aHandle) {
     46  switch (aDetail.type()) {
     47    case CompositableOperationDetail::TOpRemoveTexture: {
     48      const OpRemoveTexture& op = aDetail.get_OpRemoveTexture();
     49 
     50      RefPtr<TextureHost> tex =
     51          TextureHost::AsTextureHost(op.texture().AsParent());
     52 
     53      MOZ_ASSERT(tex.get());
     54      aCompositable->RemoveTextureHost(tex);
     55      break;
     56    }
     57    case CompositableOperationDetail::TOpClearImages: {
     58      const OpClearImages& op = aDetail.get_OpClearImages();
     59 
     60      aCompositable->ClearImages(op.type());
     61      break;
     62    }
     63    case CompositableOperationDetail::TOpUseTexture: {
     64      const OpUseTexture& op = aDetail.get_OpUseTexture();
     65 
     66      AutoTArray<CompositableHost::TimedTexture, 4> textures;
     67      for (auto& timedTexture : op.textures()) {
     68        MOZ_LOG_FMT(gCompositableTextureParentLog, LogLevel::Debug,
     69                    "ReceiveCompositableUpdate:TOpUseTexture id={}",
     70                    timedTexture.frameID());
     71        CompositableHost::TimedTexture* t = textures.AppendElement();
     72        t->mTexture =
     73            TextureHost::AsTextureHost(timedTexture.texture().AsParent());
     74        MOZ_ASSERT(t->mTexture);
     75        t->mTimeStamp = timedTexture.timeStamp();
     76        t->mPictureRect = timedTexture.picture();
     77        t->mFrameID = timedTexture.frameID();
     78        t->mProducerID = timedTexture.producerID();
     79        if (timedTexture.readLocked()) {
     80          t->mTexture->SetReadLocked();
     81        }
     82      }
     83      if (textures.Length() > 0) {
     84        aCompositable->UseTextureHost(textures);
     85 
     86        for (auto& timedTexture : op.textures()) {
     87          RefPtr<TextureHost> texture =
     88              TextureHost::AsTextureHost(timedTexture.texture().AsParent());
     89          if (texture) {
     90            texture->SetLastFwdTransactionId(mFwdTransactionId);
     91            // Make sure that each texture was handled by the compositable
     92            // because the recycling logic depends on it.
     93            MOZ_ASSERT(texture->NumCompositableRefs() > 0);
     94          }
     95        }
     96      }
     97      break;
     98    }
     99    case CompositableOperationDetail::TOpUseRemoteTexture: {
    100      const OpUseRemoteTexture& op = aDetail.get_OpUseRemoteTexture();
    101      auto* host = aCompositable->AsWebRenderImageHost();
    102      MOZ_ASSERT(host);
    103 
    104      host->PushPendingRemoteTexture(op.textureId(), op.ownerId(),
    105                                     GetChildProcessId(), op.size(),
    106                                     op.textureFlags());
    107      host->UseRemoteTexture();
    108      break;
    109    }
    110    default: {
    111      MOZ_ASSERT(false, "bad type");
    112    }
    113  }
    114 
    115  return true;
    116 }
    117 
    118 void CompositableParentManager::DestroyActor(const OpDestroy& aOp) {
    119  switch (aOp.type()) {
    120    case OpDestroy::TPTexture: {
    121      auto actor = aOp.get_PTexture().AsParent();
    122      TextureHost::ReceivedDestroy(actor);
    123      break;
    124    }
    125    case OpDestroy::TCompositableHandle: {
    126      ReleaseCompositable(aOp.get_CompositableHandle());
    127      break;
    128    }
    129    default: {
    130      MOZ_ASSERT(false, "unsupported type");
    131    }
    132  }
    133 }
    134 
    135 RefPtr<CompositableHost> CompositableParentManager::AddCompositable(
    136    const CompositableHandle& aHandle, const TextureInfo& aInfo) {
    137  if (mCompositables.find(aHandle.Value()) != mCompositables.end()) {
    138    NS_ERROR("Client should not allocate duplicate handles");
    139    return nullptr;
    140  }
    141  if (!aHandle) {
    142    NS_ERROR("Client should not allocate 0 as a handle");
    143    return nullptr;
    144  }
    145 
    146  RefPtr<CompositableHost> host = CompositableHost::Create(aInfo);
    147  if (!host) {
    148    return nullptr;
    149  }
    150 
    151  mCompositables[aHandle.Value()] = host;
    152  return host;
    153 }
    154 
    155 RefPtr<CompositableHost> CompositableParentManager::FindCompositable(
    156    const CompositableHandle& aHandle) {
    157  auto iter = mCompositables.find(aHandle.Value());
    158  if (iter == mCompositables.end()) {
    159    return nullptr;
    160  }
    161 
    162  return iter->second;
    163 }
    164 
    165 void CompositableParentManager::ReleaseCompositable(
    166    const CompositableHandle& aHandle) {
    167  auto iter = mCompositables.find(aHandle.Value());
    168  if (iter == mCompositables.end()) {
    169    return;
    170  }
    171  iter->second->OnReleased();
    172  mCompositables.erase(iter);
    173 }
    174 
    175 }  // namespace layers
    176 }  // namespace mozilla