tor-browser

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

GPUVideoImage.h (4628B)


      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 #ifndef GFX_GPU_VIDEO_IMAGE_H
      8 #define GFX_GPU_VIDEO_IMAGE_H
      9 
     10 #include "mozilla/RefPtr.h"
     11 #include "ImageContainer.h"
     12 #include "mozilla/layers/GPUVideoTextureClient.h"
     13 #include "mozilla/layers/CompositableClient.h"
     14 #include "mozilla/layers/ImageBridgeChild.h"
     15 
     16 namespace mozilla {
     17 namespace gl {
     18 class GLBlitHelper;
     19 }
     20 namespace layers {
     21 
     22 class IGPUVideoSurfaceManager {
     23 protected:
     24  virtual ~IGPUVideoSurfaceManager() = default;
     25 
     26 public:
     27  NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
     28 
     29  virtual already_AddRefed<gfx::SourceSurface> Readback(
     30      const SurfaceDescriptorGPUVideo& aSD) = 0;
     31  virtual already_AddRefed<Image> TransferToImage(
     32      const SurfaceDescriptorGPUVideo& aSD, const gfx::IntSize& aSize,
     33      const gfx::ColorDepth& aColorDepth, gfx::YUVColorSpace aYUVColorSpace,
     34      gfx::ColorSpace2 aColorPrimaries, gfx::TransferFunction aTransferFunction,
     35      gfx::ColorRange aColorRange) = 0;
     36  virtual void DeallocateSurfaceDescriptor(
     37      const SurfaceDescriptorGPUVideo& aSD) = 0;
     38  virtual void OnSetCurrent(const SurfaceDescriptorGPUVideo& aSD) = 0;
     39 };
     40 
     41 // Represents an animated Image that is known to the GPU process.
     42 class GPUVideoImage final : public Image {
     43  friend class gl::GLBlitHelper;
     44 
     45 public:
     46  GPUVideoImage(IGPUVideoSurfaceManager* aManager,
     47                const SurfaceDescriptorGPUVideo& aSD, const gfx::IntSize& aSize,
     48                const gfx::ColorDepth& aColorDepth,
     49                gfx::YUVColorSpace aYUVColorSpace,
     50                gfx::ColorSpace2 aColorPrimaries,
     51                gfx::TransferFunction aTransferFunction,
     52                gfx::ColorRange aColorRange)
     53      : Image(nullptr, ImageFormat::GPU_VIDEO),
     54        mSize(aSize),
     55        mColorDepth(aColorDepth),
     56        mColorSpace(aColorPrimaries),
     57        mYUVColorSpace(aYUVColorSpace),
     58        mTransferFunction(aTransferFunction),
     59        mColorRange(aColorRange) {
     60    // Create the TextureClient immediately since the GPUVideoTextureData
     61    // is responsible for deallocating the SurfaceDescriptor.
     62    //
     63    // Use the RECYCLE texture flag, since it's likely that our 'real'
     64    // TextureData (in the decoder thread of the GPU process) is using
     65    // it too, and we want to make sure we don't send the delete message
     66    // until we've stopped being used on the compositor.
     67    mTextureClient = TextureClient::CreateWithData(
     68        new GPUVideoTextureData(aManager, aSD, aSize), TextureFlags::RECYCLE,
     69        ImageBridgeChild::GetSingleton().get());
     70  }
     71 
     72  virtual ~GPUVideoImage() = default;
     73 
     74  GPUVideoImage* AsGPUVideoImage() override { return this; }
     75 
     76  gfx::IntSize GetSize() const override { return mSize; }
     77 
     78  gfx::ColorDepth GetColorDepth() const override { return mColorDepth; }
     79  gfx::ColorSpace2 GetColorPrimaries() const { return mColorSpace; }
     80  gfx::YUVColorSpace GetYUVColorSpace() const { return mYUVColorSpace; }
     81  gfx::TransferFunction GetTransferFunction() const {
     82    return mTransferFunction;
     83  }
     84  gfx::ColorRange GetColorRange() const { return mColorRange; }
     85 
     86  Maybe<SurfaceDescriptor> GetDesc() override {
     87    return GetDescFromTexClient(mTextureClient);
     88  }
     89 
     90  void OnSetCurrent() override {
     91    GPUVideoTextureData* data = GetData();
     92    if (NS_WARN_IF(!data)) {
     93      return;
     94    }
     95    data->OnSetCurrent();
     96  }
     97 
     98 private:
     99  GPUVideoTextureData* GetData() const {
    100    if (!mTextureClient) {
    101      return nullptr;
    102    }
    103    TextureData* data = mTextureClient->GetInternalData();
    104    if (!data) {
    105      return nullptr;
    106    }
    107    return data->AsGPUVideoTextureData();
    108  }
    109 
    110 public:
    111  already_AddRefed<gfx::SourceSurface> GetAsSourceSurface() override {
    112    GPUVideoTextureData* data = GetData();
    113    if (!data) {
    114      return nullptr;
    115    }
    116    return data->GetAsSourceSurface();
    117  }
    118 
    119  TextureClient* GetTextureClient(KnowsCompositor* aKnowsCompositor) override {
    120    MOZ_ASSERT(aKnowsCompositor == ImageBridgeChild::GetSingleton(),
    121               "Must only use GPUVideo on ImageBridge");
    122    return mTextureClient;
    123  }
    124 
    125 private:
    126  gfx::IntSize mSize;
    127  gfx::ColorDepth mColorDepth;
    128  gfx::ColorSpace2 mColorSpace;
    129  gfx::YUVColorSpace mYUVColorSpace;
    130  RefPtr<TextureClient> mTextureClient;
    131  gfx::TransferFunction mTransferFunction;
    132  gfx::ColorRange mColorRange;
    133 };
    134 
    135 }  // namespace layers
    136 }  // namespace mozilla
    137 
    138 #endif  // GFX_GPU_VIDEO_IMAGE_H