gfx/layers/d3d9/TextureD3D9.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/layers/d3d9/TextureD3D9.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,477 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef MOZILLA_GFX_TEXTURED3D9_H
    1.10 +#define MOZILLA_GFX_TEXTURED3D9_H
    1.11 +
    1.12 +#include "mozilla/layers/Compositor.h"
    1.13 +#include "mozilla/layers/TextureClient.h"
    1.14 +#include "mozilla/layers/TextureHost.h"
    1.15 +#include "mozilla/GfxMessageUtils.h"
    1.16 +#include "gfxWindowsPlatform.h"
    1.17 +#include "d3d9.h"
    1.18 +#include <vector>
    1.19 +#include "DeviceManagerD3D9.h"
    1.20 +
    1.21 +namespace mozilla {
    1.22 +namespace gfxs {
    1.23 +class DrawTarget;
    1.24 +}
    1.25 +}
    1.26 +
    1.27 +namespace mozilla {
    1.28 +namespace layers {
    1.29 +
    1.30 +class CompositorD3D9;
    1.31 +
    1.32 +class TextureSourceD3D9
    1.33 +{
    1.34 +  friend class DeviceManagerD3D9;
    1.35 +
    1.36 +public:
    1.37 +  TextureSourceD3D9()
    1.38 +    : mPreviousHost(nullptr)
    1.39 +    , mNextHost(nullptr)
    1.40 +    , mCreatingDeviceManager(nullptr)
    1.41 +  {}
    1.42 +  virtual ~TextureSourceD3D9();
    1.43 +
    1.44 +  virtual IDirect3DTexture9* GetD3D9Texture() { return mTexture; }
    1.45 +
    1.46 +  StereoMode GetStereoMode() const { return mStereoMode; };
    1.47 +
    1.48 +  // Release all texture memory resources held by the texture host.
    1.49 +  virtual void ReleaseTextureResources()
    1.50 +  {
    1.51 +    mTexture = nullptr;
    1.52 +  }
    1.53 +
    1.54 +protected:
    1.55 +  virtual gfx::IntSize GetSize() const { return mSize; }
    1.56 +  void SetSize(const gfx::IntSize& aSize) { mSize = aSize; }
    1.57 +
    1.58 +  // Helper methods for creating and copying textures.
    1.59 +  TemporaryRef<IDirect3DTexture9> InitTextures(
    1.60 +    DeviceManagerD3D9* aDeviceManager,
    1.61 +    const gfx::IntSize &aSize,
    1.62 +    _D3DFORMAT aFormat,
    1.63 +    RefPtr<IDirect3DSurface9>& aSurface,
    1.64 +    D3DLOCKED_RECT& aLockedRect);
    1.65 +
    1.66 +  TemporaryRef<IDirect3DTexture9> DataToTexture(
    1.67 +    DeviceManagerD3D9* aDeviceManager,
    1.68 +    unsigned char *aData,
    1.69 +    int aStride,
    1.70 +    const gfx::IntSize &aSize,
    1.71 +    _D3DFORMAT aFormat,
    1.72 +    uint32_t aBPP);
    1.73 +
    1.74 +  // aTexture should be in SYSTEMMEM, returns a texture in the default
    1.75 +  // pool (that is, in video memory).
    1.76 +  TemporaryRef<IDirect3DTexture9> TextureToTexture(
    1.77 +    DeviceManagerD3D9* aDeviceManager,
    1.78 +    IDirect3DTexture9* aTexture,
    1.79 +    const gfx::IntSize& aSize,
    1.80 +    _D3DFORMAT aFormat);
    1.81 +
    1.82 +  TemporaryRef<IDirect3DTexture9> SurfaceToTexture(
    1.83 +    DeviceManagerD3D9* aDeviceManager,
    1.84 +    gfxWindowsSurface* aSurface,
    1.85 +    const gfx::IntSize& aSize,
    1.86 +    _D3DFORMAT aFormat);
    1.87 +
    1.88 +  gfx::IntSize mSize;
    1.89 +
    1.90 +  // Linked list of all objects holding d3d9 textures.
    1.91 +  TextureSourceD3D9* mPreviousHost;
    1.92 +  TextureSourceD3D9* mNextHost;
    1.93 +  // The device manager that created our textures.
    1.94 +  DeviceManagerD3D9* mCreatingDeviceManager;
    1.95 +
    1.96 +  StereoMode mStereoMode;
    1.97 +  RefPtr<IDirect3DTexture9> mTexture;
    1.98 +};
    1.99 +
   1.100 +/**
   1.101 + * A TextureSource that implements the DataTextureSource interface.
   1.102 + * it can be used without a TextureHost and is able to upload texture data
   1.103 + * from a gfx::DataSourceSurface.
   1.104 + */
   1.105 +class DataTextureSourceD3D9 : public DataTextureSource
   1.106 +                            , public TextureSourceD3D9
   1.107 +                            , public TileIterator
   1.108 +{
   1.109 +public:
   1.110 +  DataTextureSourceD3D9(gfx::SurfaceFormat aFormat,
   1.111 +                        CompositorD3D9* aCompositor,
   1.112 +                        TextureFlags aFlags = TEXTURE_FLAGS_DEFAULT,
   1.113 +                        StereoMode aStereoMode = StereoMode::MONO);
   1.114 +
   1.115 +  DataTextureSourceD3D9(gfx::SurfaceFormat aFormat,
   1.116 +                        gfx::IntSize aSize,
   1.117 +                        CompositorD3D9* aCompositor,
   1.118 +                        IDirect3DTexture9* aTexture,
   1.119 +                        TextureFlags aFlags = TEXTURE_FLAGS_DEFAULT);
   1.120 +
   1.121 +  virtual ~DataTextureSourceD3D9();
   1.122 +
   1.123 +  // DataTextureSource
   1.124 +
   1.125 +  virtual bool Update(gfx::DataSourceSurface* aSurface,
   1.126 +                      nsIntRegion* aDestRegion = nullptr,
   1.127 +                      gfx::IntPoint* aSrcOffset = nullptr) MOZ_OVERRIDE;
   1.128 +
   1.129 +  // TextureSource
   1.130 +
   1.131 +  virtual TextureSourceD3D9* AsSourceD3D9() MOZ_OVERRIDE { return this; }
   1.132 +
   1.133 +  virtual IDirect3DTexture9* GetD3D9Texture() MOZ_OVERRIDE;
   1.134 +
   1.135 +  virtual DataTextureSource* AsDataTextureSource() MOZ_OVERRIDE { return this; }
   1.136 +
   1.137 +  virtual void DeallocateDeviceData() MOZ_OVERRIDE { mTexture = nullptr; }
   1.138 +
   1.139 +  virtual gfx::IntSize GetSize() const MOZ_OVERRIDE { return mSize; }
   1.140 +
   1.141 +  virtual gfx::SurfaceFormat GetFormat() const MOZ_OVERRIDE { return mFormat; }
   1.142 +
   1.143 +  virtual void SetCompositor(Compositor* aCompositor) MOZ_OVERRIDE;
   1.144 +
   1.145 +  // TileIterator
   1.146 +
   1.147 +  virtual TileIterator* AsTileIterator() MOZ_OVERRIDE { return mIsTiled ? this : nullptr; }
   1.148 +
   1.149 +  virtual size_t GetTileCount() MOZ_OVERRIDE { return mTileTextures.size(); }
   1.150 +
   1.151 +  virtual bool NextTile() MOZ_OVERRIDE { return (++mCurrentTile < mTileTextures.size()); }
   1.152 +
   1.153 +  virtual nsIntRect GetTileRect() MOZ_OVERRIDE;
   1.154 +
   1.155 +  virtual void EndTileIteration() MOZ_OVERRIDE { mIterating = false; }
   1.156 +
   1.157 +  virtual void BeginTileIteration() MOZ_OVERRIDE
   1.158 +  {
   1.159 +    mIterating = true;
   1.160 +    mCurrentTile = 0;
   1.161 +  }
   1.162 +
   1.163 +  /**
   1.164 +   * Copy the content of aTexture using the GPU.
   1.165 +   */
   1.166 +  bool UpdateFromTexture(IDirect3DTexture9* aTexture, const nsIntRegion* aRegion);
   1.167 +
   1.168 +  // To use with DIBTextureHostD3D9
   1.169 +
   1.170 +  bool Update(gfxWindowsSurface* aSurface);
   1.171 +
   1.172 +protected:
   1.173 +  gfx::IntRect GetTileRect(uint32_t aTileIndex) const;
   1.174 +
   1.175 +  void Reset();
   1.176 +
   1.177 +  std::vector< RefPtr<IDirect3DTexture9> > mTileTextures;
   1.178 +  RefPtr<CompositorD3D9> mCompositor;
   1.179 +  gfx::SurfaceFormat mFormat;
   1.180 +  uint32_t mCurrentTile;
   1.181 +  TextureFlags mFlags;
   1.182 +  bool mIsTiled;
   1.183 +  bool mIterating;
   1.184 +};
   1.185 +
   1.186 +/**
   1.187 + * Can only be drawn into through Cairo and need a D3D9 context on the client side.
   1.188 + * The corresponding TextureHost is TextureHostD3D9.
   1.189 + */
   1.190 +class CairoTextureClientD3D9 : public TextureClient
   1.191 +{
   1.192 +public:
   1.193 +  CairoTextureClientD3D9(gfx::SurfaceFormat aFormat, TextureFlags aFlags);
   1.194 +
   1.195 +  virtual ~CairoTextureClientD3D9();
   1.196 +
   1.197 +  // TextureClient
   1.198 +
   1.199 +  virtual bool IsAllocated() const MOZ_OVERRIDE { return !!mTexture; }
   1.200 +
   1.201 +  virtual bool Lock(OpenMode aOpenMode) MOZ_OVERRIDE;
   1.202 +
   1.203 +  virtual void Unlock() MOZ_OVERRIDE;
   1.204 +
   1.205 +  virtual bool IsLocked() const MOZ_OVERRIDE { return mIsLocked; }
   1.206 +
   1.207 +  virtual bool ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor) MOZ_OVERRIDE;
   1.208 +
   1.209 +  virtual gfx::IntSize GetSize() const { return mSize; }
   1.210 +
   1.211 +  virtual gfx::SurfaceFormat GetFormat() const { return mFormat; }
   1.212 +
   1.213 +  virtual TextureClientData* DropTextureData() MOZ_OVERRIDE;
   1.214 +
   1.215 +  virtual bool CanExposeDrawTarget() const MOZ_OVERRIDE { return true; }
   1.216 +
   1.217 +  virtual TemporaryRef<gfx::DrawTarget> GetAsDrawTarget() MOZ_OVERRIDE;
   1.218 +
   1.219 +  virtual bool AllocateForSurface(gfx::IntSize aSize,
   1.220 +                                  TextureAllocationFlags aFlags = ALLOC_DEFAULT) MOZ_OVERRIDE;
   1.221 +
   1.222 +  virtual bool HasInternalBuffer() const MOZ_OVERRIDE { return true; }
   1.223 +
   1.224 +private:
   1.225 +  RefPtr<IDirect3DTexture9> mTexture;
   1.226 +  nsRefPtr<IDirect3DSurface9> mD3D9Surface;
   1.227 +  RefPtr<gfx::DrawTarget> mDrawTarget;
   1.228 +  nsRefPtr<gfxASurface> mSurface;
   1.229 +  gfx::IntSize mSize;
   1.230 +  gfx::SurfaceFormat mFormat;
   1.231 +  bool mIsLocked;
   1.232 +  bool mNeedsClear;
   1.233 +  bool mLockRect;
   1.234 +};
   1.235 +
   1.236 +/**
   1.237 + * Can only be drawn into through Cairo.
   1.238 + * Prefer CairoTextureClientD3D9 when possible.
   1.239 + * The coresponding TextureHost is DIBTextureHostD3D9.
   1.240 + */
   1.241 +class DIBTextureClientD3D9 : public TextureClient
   1.242 +{
   1.243 +public:
   1.244 +  DIBTextureClientD3D9(gfx::SurfaceFormat aFormat, TextureFlags aFlags);
   1.245 +
   1.246 +  virtual ~DIBTextureClientD3D9();
   1.247 +
   1.248 +  // TextureClient
   1.249 +
   1.250 +  virtual bool IsAllocated() const MOZ_OVERRIDE { return !!mSurface; }
   1.251 +
   1.252 +  virtual bool Lock(OpenMode aOpenMode) MOZ_OVERRIDE;
   1.253 +
   1.254 +  virtual void Unlock() MOZ_OVERRIDE;
   1.255 +
   1.256 +  virtual bool IsLocked() const MOZ_OVERRIDE { return mIsLocked; }
   1.257 +
   1.258 +  virtual bool ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor) MOZ_OVERRIDE;
   1.259 +
   1.260 +  virtual gfx::IntSize GetSize() const { return mSize; }
   1.261 +
   1.262 +  virtual gfx::SurfaceFormat GetFormat() const { return mFormat; }
   1.263 +
   1.264 +  virtual TextureClientData* DropTextureData() MOZ_OVERRIDE;
   1.265 +
   1.266 +  virtual bool CanExposeDrawTarget() const MOZ_OVERRIDE { return true; }
   1.267 +
   1.268 +  virtual TemporaryRef<gfx::DrawTarget> GetAsDrawTarget() MOZ_OVERRIDE;
   1.269 +
   1.270 +  virtual bool AllocateForSurface(gfx::IntSize aSize,
   1.271 +                                  TextureAllocationFlags aFlags = ALLOC_DEFAULT) MOZ_OVERRIDE;
   1.272 +
   1.273 +  virtual bool HasInternalBuffer() const MOZ_OVERRIDE { return true; }
   1.274 +
   1.275 +protected:
   1.276 +  nsRefPtr<gfxWindowsSurface> mSurface;
   1.277 +  RefPtr<gfx::DrawTarget> mDrawTarget;
   1.278 +  gfx::IntSize mSize;
   1.279 +  gfx::SurfaceFormat mFormat;
   1.280 +  bool mIsLocked;
   1.281 +};
   1.282 +
   1.283 +/**
   1.284 + * Wraps a D3D9 texture, shared with the compositor though DXGI.
   1.285 + * At the moment it is only used with D3D11 compositing, and the corresponding
   1.286 + * TextureHost is DXGITextureHostD3D11.
   1.287 + */
   1.288 +class SharedTextureClientD3D9 : public TextureClient
   1.289 +{
   1.290 +public:
   1.291 +  SharedTextureClientD3D9(gfx::SurfaceFormat aFormat, TextureFlags aFlags);
   1.292 +
   1.293 +  virtual ~SharedTextureClientD3D9();
   1.294 +
   1.295 +  // TextureClient
   1.296 +
   1.297 +  virtual bool IsAllocated() const MOZ_OVERRIDE { return !!mTexture; }
   1.298 +
   1.299 +  virtual bool Lock(OpenMode aOpenMode) MOZ_OVERRIDE;
   1.300 +
   1.301 +  virtual void Unlock() MOZ_OVERRIDE;
   1.302 +
   1.303 +  virtual bool IsLocked() const MOZ_OVERRIDE { return mIsLocked; }
   1.304 +
   1.305 +  virtual bool ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor) MOZ_OVERRIDE;
   1.306 +
   1.307 +  void InitWith(IDirect3DTexture9* aTexture, HANDLE aSharedHandle, D3DSURFACE_DESC aDesc)
   1.308 +  {
   1.309 +    MOZ_ASSERT(!mTexture);
   1.310 +    mTexture = aTexture;
   1.311 +    mHandle = aSharedHandle;
   1.312 +    mDesc = aDesc;
   1.313 +  }
   1.314 +
   1.315 +  virtual gfx::IntSize GetSize() const
   1.316 +  {
   1.317 +    return gfx::IntSize(mDesc.Width, mDesc.Height);
   1.318 +  }
   1.319 +
   1.320 +  virtual TextureClientData* DropTextureData() MOZ_OVERRIDE;
   1.321 +
   1.322 +  virtual bool HasInternalBuffer() const MOZ_OVERRIDE { return true; }
   1.323 +
   1.324 +private:
   1.325 +  RefPtr<IDirect3DTexture9> mTexture;
   1.326 +  gfx::SurfaceFormat mFormat;
   1.327 +  HANDLE mHandle;
   1.328 +  D3DSURFACE_DESC mDesc;
   1.329 +  bool mIsLocked;
   1.330 +};
   1.331 +
   1.332 +class TextureHostD3D9 : public TextureHost
   1.333 +{
   1.334 +public:
   1.335 +  TextureHostD3D9(TextureFlags aFlags,
   1.336 +                  const SurfaceDescriptorD3D9& aDescriptor);
   1.337 +
   1.338 +  virtual NewTextureSource* GetTextureSources() MOZ_OVERRIDE;
   1.339 +
   1.340 +  virtual void DeallocateDeviceData() MOZ_OVERRIDE;
   1.341 +
   1.342 +  virtual void SetCompositor(Compositor* aCompositor) MOZ_OVERRIDE;
   1.343 +
   1.344 +  virtual gfx::SurfaceFormat GetFormat() const MOZ_OVERRIDE { return mFormat; }
   1.345 +
   1.346 +  virtual bool Lock() MOZ_OVERRIDE;
   1.347 +
   1.348 +  virtual void Unlock() MOZ_OVERRIDE;
   1.349 +
   1.350 +  virtual void Updated(const nsIntRegion* aRegion) MOZ_OVERRIDE;
   1.351 +
   1.352 +  virtual gfx::IntSize GetSize() const MOZ_OVERRIDE { return mSize; }
   1.353 +
   1.354 +  virtual TemporaryRef<gfx::DataSourceSurface> GetAsSurface() MOZ_OVERRIDE
   1.355 +  {
   1.356 +    return nullptr;
   1.357 +  }
   1.358 +
   1.359 +  virtual bool HasInternalBuffer() const MOZ_OVERRIDE { return true; }
   1.360 +
   1.361 +protected:
   1.362 +  TextureHostD3D9(TextureFlags aFlags);
   1.363 +  IDirect3DDevice9* GetDevice();
   1.364 +
   1.365 +  RefPtr<DataTextureSourceD3D9> mTextureSource;
   1.366 +  RefPtr<IDirect3DTexture9> mTexture;
   1.367 +  RefPtr<CompositorD3D9> mCompositor;
   1.368 +  gfx::IntSize mSize;
   1.369 +  gfx::SurfaceFormat mFormat;
   1.370 +  bool mIsLocked;
   1.371 +};
   1.372 +
   1.373 +class DIBTextureHostD3D9 : public TextureHost
   1.374 +{
   1.375 +public:
   1.376 +  DIBTextureHostD3D9(TextureFlags aFlags,
   1.377 +                     const SurfaceDescriptorDIB& aDescriptor);
   1.378 +
   1.379 +  virtual NewTextureSource* GetTextureSources() MOZ_OVERRIDE;
   1.380 +
   1.381 +  virtual void DeallocateDeviceData() MOZ_OVERRIDE;
   1.382 +
   1.383 +  virtual void SetCompositor(Compositor* aCompositor) MOZ_OVERRIDE;
   1.384 +
   1.385 +  virtual gfx::SurfaceFormat GetFormat() const MOZ_OVERRIDE { return mFormat; }
   1.386 +
   1.387 +  virtual gfx::IntSize GetSize() const MOZ_OVERRIDE { return mSize; }
   1.388 +
   1.389 +  virtual bool Lock() MOZ_OVERRIDE;
   1.390 +
   1.391 +  virtual void Unlock() MOZ_OVERRIDE;
   1.392 +
   1.393 +  virtual void Updated(const nsIntRegion* aRegion = nullptr);
   1.394 +
   1.395 +  virtual TemporaryRef<gfx::DataSourceSurface> GetAsSurface() MOZ_OVERRIDE
   1.396 +  {
   1.397 +    return nullptr; // TODO: cf bug 872568
   1.398 +  }
   1.399 +
   1.400 +protected:
   1.401 +  nsRefPtr<gfxWindowsSurface> mSurface;
   1.402 +  RefPtr<DataTextureSourceD3D9> mTextureSource;
   1.403 +  RefPtr<CompositorD3D9> mCompositor;
   1.404 +  gfx::SurfaceFormat mFormat;
   1.405 +  gfx::IntSize mSize;
   1.406 +  bool mIsLocked;
   1.407 +};
   1.408 +
   1.409 +class DXGITextureHostD3D9 : public TextureHost
   1.410 +{
   1.411 +public:
   1.412 +  DXGITextureHostD3D9(TextureFlags aFlags,
   1.413 +    const SurfaceDescriptorD3D10& aDescriptor);
   1.414 +
   1.415 +  virtual NewTextureSource* GetTextureSources() MOZ_OVERRIDE;
   1.416 +
   1.417 +  virtual void DeallocateDeviceData() MOZ_OVERRIDE;
   1.418 +
   1.419 +  virtual void SetCompositor(Compositor* aCompositor) MOZ_OVERRIDE;
   1.420 +
   1.421 +  virtual gfx::SurfaceFormat GetFormat() const MOZ_OVERRIDE { return mFormat; }
   1.422 +
   1.423 +  virtual gfx::IntSize GetSize() const MOZ_OVERRIDE { return mSize; }
   1.424 +
   1.425 +  virtual bool Lock() MOZ_OVERRIDE;
   1.426 +
   1.427 +  virtual void Unlock() MOZ_OVERRIDE;
   1.428 +
   1.429 +  virtual TemporaryRef<gfx::DataSourceSurface> GetAsSurface() MOZ_OVERRIDE
   1.430 +  {
   1.431 +    return nullptr; // TODO: cf bug 872568
   1.432 +  }
   1.433 +
   1.434 +protected:
   1.435 +  RefPtr<DataTextureSourceD3D9> mTextureSource;
   1.436 +  RefPtr<CompositorD3D9> mCompositor;
   1.437 +  WindowsHandle mHandle;
   1.438 +  gfx::SurfaceFormat mFormat;
   1.439 +  gfx::IntSize mSize;
   1.440 +  bool mIsLocked;
   1.441 +};
   1.442 +
   1.443 +class CompositingRenderTargetD3D9 : public CompositingRenderTarget,
   1.444 +                                    public TextureSourceD3D9
   1.445 +{
   1.446 +public:
   1.447 +  CompositingRenderTargetD3D9(IDirect3DTexture9* aTexture,
   1.448 +                              SurfaceInitMode aInit,
   1.449 +                              const gfx::IntRect& aRect);
   1.450 +  // use for rendering to the main window, cannot be rendered as a texture
   1.451 +  CompositingRenderTargetD3D9(IDirect3DSurface9* aSurface,
   1.452 +                              SurfaceInitMode aInit,
   1.453 +                              const gfx::IntRect& aRect);
   1.454 +  virtual ~CompositingRenderTargetD3D9();
   1.455 +
   1.456 +  virtual TextureSourceD3D9* AsSourceD3D9() MOZ_OVERRIDE
   1.457 +  {
   1.458 +    MOZ_ASSERT(mTexture,
   1.459 +               "No texture, can't be indirectly rendered. Is this the screen backbuffer?");
   1.460 +    return this;
   1.461 +  }
   1.462 +
   1.463 +  virtual gfx::IntSize GetSize() const MOZ_OVERRIDE;
   1.464 +
   1.465 +  void BindRenderTarget(IDirect3DDevice9* aDevice);
   1.466 +
   1.467 +  IDirect3DSurface9* GetD3D9Surface() const { return mSurface; }
   1.468 +
   1.469 +private:
   1.470 +  friend class CompositorD3D9;
   1.471 +
   1.472 +  nsRefPtr<IDirect3DSurface9> mSurface;
   1.473 +  SurfaceInitMode mInitMode;
   1.474 +  bool mInitialized;
   1.475 +};
   1.476 +
   1.477 +}
   1.478 +}
   1.479 +
   1.480 +#endif /* MOZILLA_GFX_TEXTURED3D9_H */

mercurial