gfx/layers/d3d10/LayerManagerD3D10.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/layers/d3d10/LayerManagerD3D10.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,288 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     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 GFX_LAYERMANAGERD3D10_H
    1.10 +#define GFX_LAYERMANAGERD3D10_H
    1.11 +
    1.12 +#include "Layers.h"
    1.13 +
    1.14 +#include <windows.h>
    1.15 +#include <d3d10_1.h>
    1.16 +
    1.17 +#include "gfxContext.h"
    1.18 +#include "mozilla/gfx/UserData.h"
    1.19 +#include "nsIWidget.h"
    1.20 +
    1.21 +#include "ReadbackManagerD3D10.h"
    1.22 +
    1.23 +namespace mozilla {
    1.24 +namespace layers {
    1.25 +
    1.26 +class DummyRoot;
    1.27 +class Nv3DVUtils;
    1.28 +
    1.29 +/**
    1.30 + * This structure is used to pass rectangles to our shader constant. We can use
    1.31 + * this for passing rectangular areas to SetVertexShaderConstant. In the format
    1.32 + * of a 4 component float(x,y,width,height). Our vertex shader can then use
    1.33 + * this to construct rectangular positions from the 0,0-1,1 quad that we source
    1.34 + * it with.
    1.35 + */
    1.36 +struct ShaderConstantRectD3D10
    1.37 +{
    1.38 +  float mX, mY, mWidth, mHeight;
    1.39 +  ShaderConstantRectD3D10(float aX, float aY, float aWidth, float aHeight)
    1.40 +    : mX(aX), mY(aY), mWidth(aWidth), mHeight(aHeight)
    1.41 +  { }
    1.42 +
    1.43 +  // For easy passing to SetVertexShaderConstantF.
    1.44 +  operator float* () { return &mX; }
    1.45 +};
    1.46 +
    1.47 +/*
    1.48 + * This is the LayerManager used for Direct3D 10. For now this will
    1.49 + * render on the main thread.
    1.50 + *
    1.51 + * For the time being, LayerManagerD3D10 forwards layers
    1.52 + * transactions.
    1.53 + */
    1.54 +class LayerManagerD3D10 : public LayerManager {
    1.55 +  typedef mozilla::gfx::DrawTarget DrawTarget;
    1.56 +  typedef mozilla::gfx::IntSize IntSize;
    1.57 +  typedef mozilla::gfx::SurfaceFormat SurfaceFormat;
    1.58 +
    1.59 +public:
    1.60 +  LayerManagerD3D10(nsIWidget *aWidget);
    1.61 +  virtual ~LayerManagerD3D10();
    1.62 +
    1.63 +  /*
    1.64 +   * Initializes the layer manager, this is when the layer manager will
    1.65 +   * actually access the device and attempt to create the swap chain used
    1.66 +   * to draw to the window. If this method fails the device cannot be used.
    1.67 +   * This function is not threadsafe.
    1.68 +   *
    1.69 +   * return True is initialization was succesful, false when it was not.
    1.70 +   */
    1.71 +  bool Initialize(bool force = false, HRESULT* aHresultPtr = nullptr);
    1.72 +
    1.73 +  /*
    1.74 +   * LayerManager implementation.
    1.75 +   */
    1.76 +  virtual void Destroy();
    1.77 +
    1.78 +  virtual void SetRoot(Layer *aLayer);
    1.79 +
    1.80 +  virtual void BeginTransaction();
    1.81 +
    1.82 +  virtual void BeginTransactionWithTarget(gfxContext* aTarget);
    1.83 +
    1.84 +  virtual bool EndEmptyTransaction(EndTransactionFlags aFlags = END_DEFAULT);
    1.85 +
    1.86 +  struct CallbackInfo {
    1.87 +    DrawThebesLayerCallback Callback;
    1.88 +    void *CallbackData;
    1.89 +  };
    1.90 +
    1.91 +  virtual void EndTransaction(DrawThebesLayerCallback aCallback,
    1.92 +                              void* aCallbackData,
    1.93 +                              EndTransactionFlags aFlags = END_DEFAULT);
    1.94 +
    1.95 +  const CallbackInfo &GetCallbackInfo() { return mCurrentCallbackInfo; }
    1.96 +
    1.97 +  // D3D10 guarantees textures can be at least this size
    1.98 +  enum {
    1.99 +    MAX_TEXTURE_SIZE = 8192
   1.100 +  };
   1.101 +  virtual bool CanUseCanvasLayerForSize(const gfx::IntSize &aSize)
   1.102 +  {
   1.103 +    return aSize <= gfx::IntSize(MAX_TEXTURE_SIZE, MAX_TEXTURE_SIZE);
   1.104 +  }
   1.105 +
   1.106 +  virtual int32_t GetMaxTextureSize() const
   1.107 +  {
   1.108 +    return MAX_TEXTURE_SIZE;
   1.109 +  }
   1.110 +
   1.111 +  virtual already_AddRefed<ThebesLayer> CreateThebesLayer();
   1.112 +  virtual already_AddRefed<ContainerLayer> CreateContainerLayer();
   1.113 +  virtual already_AddRefed<ImageLayer> CreateImageLayer();
   1.114 +  virtual already_AddRefed<ColorLayer> CreateColorLayer();
   1.115 +  virtual already_AddRefed<CanvasLayer> CreateCanvasLayer();
   1.116 +  virtual already_AddRefed<ReadbackLayer> CreateReadbackLayer();
   1.117 +
   1.118 +  virtual TemporaryRef<DrawTarget>
   1.119 +    CreateOptimalDrawTarget(const IntSize &aSize,
   1.120 +                            SurfaceFormat aSurfaceFormat);
   1.121 +
   1.122 +  virtual TemporaryRef<DrawTarget>
   1.123 +    CreateOptimalMaskDrawTarget(const IntSize &aSize);
   1.124 +
   1.125 +  virtual TemporaryRef<mozilla::gfx::DrawTarget>
   1.126 +    CreateDrawTarget(const gfx::IntSize &aSize,
   1.127 +                     mozilla::gfx::SurfaceFormat aFormat);
   1.128 +
   1.129 +  virtual LayersBackend GetBackendType() { return LayersBackend::LAYERS_D3D10; }
   1.130 +  virtual void GetBackendName(nsAString& name) { name.AssignLiteral("Direct3D 10"); }
   1.131 +
   1.132 +  virtual const char* Name() const { return "D3D10"; }
   1.133 +
   1.134 +  // Public helpers
   1.135 +
   1.136 +  ID3D10Device1 *device() const { return mDevice; }
   1.137 +
   1.138 +  ID3D10Effect *effect() const { return mEffect; }
   1.139 +  IDXGISwapChain *SwapChain() const
   1.140 +  {
   1.141 +    return mSwapChain;
   1.142 +  }
   1.143 +  ReadbackManagerD3D10 *readbackManager();
   1.144 +
   1.145 +  void SetupInputAssembler();
   1.146 +  void SetViewport(const nsIntSize &aViewport);
   1.147 +  const nsIntSize &GetViewport() { return mViewport; }
   1.148 +
   1.149 +  /**
   1.150 +   * Return pointer to the Nv3DVUtils instance
   1.151 +   */
   1.152 +  Nv3DVUtils *GetNv3DVUtils()  { return mNv3DVUtils; }
   1.153 +
   1.154 +  static void ReportFailure(const nsACString &aMsg, HRESULT aCode);
   1.155 +
   1.156 +private:
   1.157 +  void SetupPipeline();
   1.158 +  void UpdateRenderTarget();
   1.159 +  void VerifyBufferSize();
   1.160 +  void EnsureReadbackManager();
   1.161 +
   1.162 +  void Render(EndTransactionFlags aFlags);
   1.163 +
   1.164 +  nsRefPtr<ID3D10Device1> mDevice;
   1.165 +
   1.166 +  nsRefPtr<ID3D10Effect> mEffect;
   1.167 +  nsRefPtr<ID3D10InputLayout> mInputLayout;
   1.168 +  nsRefPtr<ID3D10Buffer> mVertexBuffer;
   1.169 +  nsRefPtr<ReadbackManagerD3D10> mReadbackManager;
   1.170 +
   1.171 +  nsRefPtr<ID3D10RenderTargetView> mRTView;
   1.172 +
   1.173 +  nsRefPtr<IDXGISwapChain> mSwapChain;
   1.174 +
   1.175 +  nsIWidget *mWidget;
   1.176 +
   1.177 +  bool mDisableSequenceForNextFrame;
   1.178 +
   1.179 +  CallbackInfo mCurrentCallbackInfo;
   1.180 +
   1.181 +  nsIntSize mViewport;
   1.182 +
   1.183 +  /* Nv3DVUtils instance */ 
   1.184 +  nsAutoPtr<Nv3DVUtils> mNv3DVUtils; 
   1.185 +
   1.186 +  /*
   1.187 +   * Context target, nullptr when drawing directly to our swap chain.
   1.188 +   */
   1.189 +  nsRefPtr<gfxContext> mTarget;
   1.190 +
   1.191 +  /*
   1.192 +   * Copies the content of our backbuffer to the set transaction target.
   1.193 +   */
   1.194 +  void PaintToTarget();
   1.195 +};
   1.196 +
   1.197 +/*
   1.198 + * General information and tree management for OGL layers.
   1.199 + */
   1.200 +class LayerD3D10
   1.201 +{
   1.202 +public:
   1.203 +  LayerD3D10(LayerManagerD3D10 *aManager);
   1.204 +
   1.205 +  virtual LayerD3D10 *GetFirstChildD3D10() { return nullptr; }
   1.206 +
   1.207 +  void SetFirstChild(LayerD3D10 *aParent);
   1.208 +
   1.209 +  virtual Layer* GetLayer() = 0;
   1.210 +
   1.211 +  /**
   1.212 +   * This will render a child layer to whatever render target is currently
   1.213 +   * active.
   1.214 +   */
   1.215 +  virtual void RenderLayer() = 0;
   1.216 +  virtual void Validate() {}
   1.217 +
   1.218 +  ID3D10Device1 *device() const { return mD3DManager->device(); }
   1.219 +  ID3D10Effect *effect() const { return mD3DManager->effect(); }
   1.220 +
   1.221 +  /* Called by the layer manager when it's destroyed */
   1.222 +  virtual void LayerManagerDestroyed() {}
   1.223 +
   1.224 +  /**
   1.225 +   * Return pointer to the Nv3DVUtils instance. Calls equivalent method in LayerManager.
   1.226 +   */
   1.227 +  Nv3DVUtils *GetNv3DVUtils()  { return mD3DManager->GetNv3DVUtils(); }
   1.228 +
   1.229 +  /*
   1.230 +   * Returns a shader resource view of a texture containing the contents of this
   1.231 +   * layer. Will try to return an existing texture if possible, or a temporary
   1.232 +   * one if not. It is the callee's responsibility to release the shader
   1.233 +   * resource view. Will return null if a texture could not be constructed.
   1.234 +   * The texture will not be transformed, i.e., it will be in the same coord
   1.235 +   * space as this.
   1.236 +   * Any layer that can be used as a mask layer should override this method.
   1.237 +   * If aSize is non-null, it will contain the size of the texture.
   1.238 +   */
   1.239 +  virtual already_AddRefed<ID3D10ShaderResourceView> GetAsTexture(gfx::IntSize* aSize)
   1.240 +  {
   1.241 +    return nullptr;
   1.242 +  }
   1.243 +
   1.244 +  void SetEffectTransformAndOpacity()
   1.245 +  {
   1.246 +    Layer* layer = GetLayer();
   1.247 +    const gfx::Matrix4x4& transform = layer->GetEffectiveTransform();
   1.248 +    void* raw = &const_cast<gfx::Matrix4x4&>(transform)._11;
   1.249 +    effect()->GetVariableByName("mLayerTransform")->SetRawValue(raw, 0, 64);
   1.250 +    effect()->GetVariableByName("fLayerOpacity")->AsScalar()->SetFloat(layer->GetEffectiveOpacity());
   1.251 +  }
   1.252 +
   1.253 +protected:
   1.254 +  /*
   1.255 +   * Finds a texture for this layer's mask layer (if it has one) and sets it
   1.256 +   * as an input to the shaders.
   1.257 +   * Returns SHADER_MASK if a texture is loaded, SHADER_NO_MASK if there was no 
   1.258 +   * mask layer, or a texture for the mask layer could not be loaded.
   1.259 +   */
   1.260 +  uint8_t LoadMaskTexture();
   1.261 +
   1.262 +  /**
   1.263 +   * Select a shader technique using a combination of the following flags.
   1.264 +   * Not all combinations of flags are supported, and might cause an error,
   1.265 +   * check the fx file to see which shaders exist. In particular, aFlags should
   1.266 +   * include any combination of the 0x20 bit = 0 flags OR one of the 0x20 bit = 1
   1.267 +   * flags. Mask flags can be used in either case.
   1.268 +   */
   1.269 +  ID3D10EffectTechnique* SelectShader(uint8_t aFlags);
   1.270 +  const static uint8_t SHADER_NO_MASK = 0;
   1.271 +  const static uint8_t SHADER_MASK = 0x1;
   1.272 +  const static uint8_t SHADER_MASK_3D = 0x2;
   1.273 +  // 0x20 bit = 0
   1.274 +  const static uint8_t SHADER_RGB = 0;
   1.275 +  const static uint8_t SHADER_RGBA = 0x4;
   1.276 +  const static uint8_t SHADER_NON_PREMUL = 0;
   1.277 +  const static uint8_t SHADER_PREMUL = 0x8;
   1.278 +  const static uint8_t SHADER_LINEAR = 0;
   1.279 +  const static uint8_t SHADER_POINT = 0x10;
   1.280 +  // 0x20 bit = 1
   1.281 +  const static uint8_t SHADER_YCBCR = 0x20;
   1.282 +  const static uint8_t SHADER_COMPONENT_ALPHA = 0x24;
   1.283 +  const static uint8_t SHADER_SOLID = 0x28;
   1.284 +
   1.285 +  LayerManagerD3D10 *mD3DManager;
   1.286 +};
   1.287 +
   1.288 +} /* layers */
   1.289 +} /* mozilla */
   1.290 +
   1.291 +#endif /* GFX_LAYERMANAGERD3D9_H */

mercurial