gfx/layers/d3d9/DeviceManagerD3D9.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef GFX_DEVICEMANAGERD3D9_H
michael@0 7 #define GFX_DEVICEMANAGERD3D9_H
michael@0 8
michael@0 9 #include "gfxTypes.h"
michael@0 10 #include "nsAutoPtr.h"
michael@0 11 #include "d3d9.h"
michael@0 12 #include "nsTArray.h"
michael@0 13 #include "mozilla/layers/CompositorTypes.h"
michael@0 14 #include "mozilla/RefPtr.h"
michael@0 15
michael@0 16 struct nsIntRect;
michael@0 17
michael@0 18 namespace mozilla {
michael@0 19 namespace layers {
michael@0 20
michael@0 21 class DeviceManagerD3D9;
michael@0 22 class LayerD3D9;
michael@0 23 class Nv3DVUtils;
michael@0 24 class Layer;
michael@0 25 class TextureSourceD3D9;
michael@0 26
michael@0 27 // Shader Constant locations
michael@0 28 const int CBmLayerTransform = 0;
michael@0 29 const int CBmProjection = 4;
michael@0 30 const int CBvRenderTargetOffset = 8;
michael@0 31 const int CBvTextureCoords = 9;
michael@0 32 const int CBvLayerQuad = 10;
michael@0 33 // we don't use opacity with solid color shaders
michael@0 34 const int CBfLayerOpacity = 0;
michael@0 35 const int CBvColor = 0;
michael@0 36
michael@0 37 enum DeviceManagerState {
michael@0 38 // The device and swap chain are OK.
michael@0 39 DeviceOK,
michael@0 40 // The device or swap chain are in a bad state, and we should not render.
michael@0 41 DeviceFail,
michael@0 42 // The device is lost and cannot be reset, the user should forget the
michael@0 43 // current device manager and create a new one.
michael@0 44 DeviceMustRecreate,
michael@0 45 };
michael@0 46
michael@0 47
michael@0 48 /**
michael@0 49 * This structure is used to pass rectangles to our shader constant. We can use
michael@0 50 * this for passing rectangular areas to SetVertexShaderConstant. In the format
michael@0 51 * of a 4 component float(x,y,width,height). Our vertex shader can then use
michael@0 52 * this to construct rectangular positions from the 0,0-1,1 quad that we source
michael@0 53 * it with.
michael@0 54 */
michael@0 55 struct ShaderConstantRect
michael@0 56 {
michael@0 57 float mX, mY, mWidth, mHeight;
michael@0 58
michael@0 59 // Provide all the commonly used argument types to prevent all the local
michael@0 60 // casts in the code.
michael@0 61 ShaderConstantRect(float aX, float aY, float aWidth, float aHeight)
michael@0 62 : mX(aX), mY(aY), mWidth(aWidth), mHeight(aHeight)
michael@0 63 { }
michael@0 64
michael@0 65 ShaderConstantRect(int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight)
michael@0 66 : mX((float)aX), mY((float)aY)
michael@0 67 , mWidth((float)aWidth), mHeight((float)aHeight)
michael@0 68 { }
michael@0 69
michael@0 70 ShaderConstantRect(int32_t aX, int32_t aY, float aWidth, float aHeight)
michael@0 71 : mX((float)aX), mY((float)aY), mWidth(aWidth), mHeight(aHeight)
michael@0 72 { }
michael@0 73
michael@0 74 // For easy passing to SetVertexShaderConstantF.
michael@0 75 operator float* () { return &mX; }
michael@0 76 };
michael@0 77
michael@0 78 /**
michael@0 79 * SwapChain class, this class manages the swap chain belonging to a
michael@0 80 * LayerManagerD3D9.
michael@0 81 */
michael@0 82 class SwapChainD3D9 MOZ_FINAL
michael@0 83 {
michael@0 84 NS_INLINE_DECL_REFCOUNTING(SwapChainD3D9)
michael@0 85 public:
michael@0 86
michael@0 87 /**
michael@0 88 * This function will prepare the device this swap chain belongs to for
michael@0 89 * rendering to this swap chain. Only after calling this function can the
michael@0 90 * swap chain be drawn to, and only until this function is called on another
michael@0 91 * swap chain belonging to this device will the device draw to it. Passed in
michael@0 92 * is the size of the swap chain. If the window size differs from the size
michael@0 93 * during the last call to this function the swap chain will resize. Note that
michael@0 94 * in no case does this function guarantee the backbuffer to still have its
michael@0 95 * old content.
michael@0 96 */
michael@0 97 DeviceManagerState PrepareForRendering();
michael@0 98
michael@0 99 already_AddRefed<IDirect3DSurface9> GetBackBuffer();
michael@0 100
michael@0 101 /**
michael@0 102 * This function will present the selected rectangle of the swap chain to
michael@0 103 * its associated window.
michael@0 104 */
michael@0 105 void Present(const nsIntRect &aRect);
michael@0 106 void Present();
michael@0 107
michael@0 108 private:
michael@0 109 friend class DeviceManagerD3D9;
michael@0 110
michael@0 111 SwapChainD3D9(DeviceManagerD3D9 *aDeviceManager);
michael@0 112
michael@0 113 // Private destructor, to discourage deletion outside of Release():
michael@0 114 ~SwapChainD3D9();
michael@0 115
michael@0 116 bool Init(HWND hWnd);
michael@0 117
michael@0 118 /**
michael@0 119 * This causes us to release our swap chain, clearing out our resource usage
michael@0 120 * so the master device may reset.
michael@0 121 */
michael@0 122 void Reset();
michael@0 123
michael@0 124 nsRefPtr<IDirect3DSwapChain9> mSwapChain;
michael@0 125 nsRefPtr<DeviceManagerD3D9> mDeviceManager;
michael@0 126 HWND mWnd;
michael@0 127 };
michael@0 128
michael@0 129 /**
michael@0 130 * Device manager, this class is used by the layer managers to share the D3D9
michael@0 131 * device and create swap chains for the individual windows the layer managers
michael@0 132 * belong to.
michael@0 133 */
michael@0 134 class DeviceManagerD3D9 MOZ_FINAL
michael@0 135 {
michael@0 136 public:
michael@0 137 DeviceManagerD3D9();
michael@0 138 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DeviceManagerD3D9)
michael@0 139
michael@0 140 /**
michael@0 141 * Initialises the device manager, the underlying device, and everything else
michael@0 142 * the manager needs.
michael@0 143 * Returns true if initialisation succeeds, false otherwise.
michael@0 144 * Note that if initisalisation fails, you cannot try again - you must throw
michael@0 145 * away the DeviceManagerD3D9 and create a new one.
michael@0 146 */
michael@0 147 bool Init();
michael@0 148
michael@0 149 /**
michael@0 150 * Sets up the render state for the device for layer rendering.
michael@0 151 */
michael@0 152 void SetupRenderState();
michael@0 153
michael@0 154 /**
michael@0 155 * Create a swap chain setup to work with the specified window.
michael@0 156 */
michael@0 157 already_AddRefed<SwapChainD3D9> CreateSwapChain(HWND hWnd);
michael@0 158
michael@0 159 IDirect3DDevice9 *device() { return mDevice; }
michael@0 160
michael@0 161 bool IsD3D9Ex() { return mDeviceEx; }
michael@0 162
michael@0 163 bool HasDynamicTextures() { return mHasDynamicTextures; }
michael@0 164
michael@0 165 enum ShaderMode {
michael@0 166 RGBLAYER,
michael@0 167 RGBALAYER,
michael@0 168 COMPONENTLAYERPASS1,
michael@0 169 COMPONENTLAYERPASS2,
michael@0 170 YCBCRLAYER,
michael@0 171 SOLIDCOLORLAYER
michael@0 172 };
michael@0 173
michael@0 174 void SetShaderMode(ShaderMode aMode, Layer* aMask, bool aIs2D);
michael@0 175 // returns the register to be used for the mask texture, if appropriate
michael@0 176 uint32_t SetShaderMode(ShaderMode aMode, MaskType aMaskType);
michael@0 177
michael@0 178 /**
michael@0 179 * Return pointer to the Nv3DVUtils instance
michael@0 180 */
michael@0 181 Nv3DVUtils *GetNv3DVUtils() { return mNv3DVUtils; }
michael@0 182
michael@0 183 /**
michael@0 184 * Returns true if this device was removed.
michael@0 185 */
michael@0 186 bool DeviceWasRemoved() { return mDeviceWasRemoved; }
michael@0 187
michael@0 188 uint32_t GetDeviceResetCount() { return mDeviceResetCount; }
michael@0 189
michael@0 190 /**
michael@0 191 * We keep a list of all layers here that may have hardware resource allocated
michael@0 192 * so we can clean their resources on reset.
michael@0 193 */
michael@0 194 nsTArray<LayerD3D9*> mLayersWithResources;
michael@0 195
michael@0 196 int32_t GetMaxTextureSize() { return mMaxTextureSize; }
michael@0 197
michael@0 198 // Removes aHost from our list of texture hosts if it is the head.
michael@0 199 void RemoveTextureListHead(TextureSourceD3D9* aHost);
michael@0 200
michael@0 201 /**
michael@0 202 * Creates a texture using our device.
michael@0 203 * If needed, we keep a record of the new texture, so the texture can be
michael@0 204 * released. In this case, aTextureHostIDirect3DTexture9 must be non-null.
michael@0 205 */
michael@0 206 TemporaryRef<IDirect3DTexture9> CreateTexture(const gfx::IntSize &aSize,
michael@0 207 _D3DFORMAT aFormat,
michael@0 208 D3DPOOL aPool,
michael@0 209 TextureSourceD3D9* aTextureHostIDirect3DTexture9);
michael@0 210 #ifdef DEBUG
michael@0 211 // Looks for aFind in the list of texture hosts.
michael@0 212 // O(n) so only use for assertions.
michael@0 213 bool IsInTextureHostList(TextureSourceD3D9* aFind);
michael@0 214 #endif
michael@0 215
michael@0 216 /**
michael@0 217 * This function verifies the device is ready for rendering, internally this
michael@0 218 * will test the cooperative level of the device and reset the device if
michael@0 219 * needed. If this returns false subsequent rendering calls may return errors.
michael@0 220 */
michael@0 221 DeviceManagerState VerifyReadyForRendering();
michael@0 222
michael@0 223 static uint32_t sMaskQuadRegister;
michael@0 224
michael@0 225 private:
michael@0 226 friend class SwapChainD3D9;
michael@0 227
michael@0 228 ~DeviceManagerD3D9();
michael@0 229 void DestroyDevice();
michael@0 230
michael@0 231 /**
michael@0 232 * This will fill our vertex buffer with the data of our quad, it may be
michael@0 233 * called when the vertex buffer is recreated.
michael@0 234 */
michael@0 235 bool CreateVertexBuffer();
michael@0 236
michael@0 237 /**
michael@0 238 * Release all textures created by this device manager.
michael@0 239 */
michael@0 240 void ReleaseTextureResources();
michael@0 241 /**
michael@0 242 * Add aHost to our list of texture hosts.
michael@0 243 */
michael@0 244 void RegisterTextureHost(TextureSourceD3D9* aHost);
michael@0 245
michael@0 246 /* Array used to store all swap chains for device resets */
michael@0 247 nsTArray<SwapChainD3D9*> mSwapChains;
michael@0 248
michael@0 249 /* The D3D device we use */
michael@0 250 nsRefPtr<IDirect3DDevice9> mDevice;
michael@0 251
michael@0 252 /* The D3D9Ex device - only valid on Vista+ with WDDM */
michael@0 253 nsRefPtr<IDirect3DDevice9Ex> mDeviceEx;
michael@0 254
michael@0 255 /* An instance of the D3D9 object */
michael@0 256 nsRefPtr<IDirect3D9> mD3D9;
michael@0 257
michael@0 258 /* An instance of the D3D9Ex object - only valid on Vista+ with WDDM */
michael@0 259 nsRefPtr<IDirect3D9Ex> mD3D9Ex;
michael@0 260
michael@0 261 /* Vertex shader used for layer quads */
michael@0 262 nsRefPtr<IDirect3DVertexShader9> mLayerVS;
michael@0 263
michael@0 264 /* Pixel shader used for RGB textures */
michael@0 265 nsRefPtr<IDirect3DPixelShader9> mRGBPS;
michael@0 266
michael@0 267 /* Pixel shader used for RGBA textures */
michael@0 268 nsRefPtr<IDirect3DPixelShader9> mRGBAPS;
michael@0 269
michael@0 270 /* Pixel shader used for component alpha textures (pass 1) */
michael@0 271 nsRefPtr<IDirect3DPixelShader9> mComponentPass1PS;
michael@0 272
michael@0 273 /* Pixel shader used for component alpha textures (pass 2) */
michael@0 274 nsRefPtr<IDirect3DPixelShader9> mComponentPass2PS;
michael@0 275
michael@0 276 /* Pixel shader used for RGB textures */
michael@0 277 nsRefPtr<IDirect3DPixelShader9> mYCbCrPS;
michael@0 278
michael@0 279 /* Pixel shader used for solid colors */
michael@0 280 nsRefPtr<IDirect3DPixelShader9> mSolidColorPS;
michael@0 281
michael@0 282 /* As above, but using a mask layer */
michael@0 283 nsRefPtr<IDirect3DVertexShader9> mLayerVSMask;
michael@0 284 nsRefPtr<IDirect3DVertexShader9> mLayerVSMask3D;
michael@0 285 nsRefPtr<IDirect3DPixelShader9> mRGBPSMask;
michael@0 286 nsRefPtr<IDirect3DPixelShader9> mRGBAPSMask;
michael@0 287 nsRefPtr<IDirect3DPixelShader9> mRGBAPSMask3D;
michael@0 288 nsRefPtr<IDirect3DPixelShader9> mComponentPass1PSMask;
michael@0 289 nsRefPtr<IDirect3DPixelShader9> mComponentPass2PSMask;
michael@0 290 nsRefPtr<IDirect3DPixelShader9> mYCbCrPSMask;
michael@0 291 nsRefPtr<IDirect3DPixelShader9> mSolidColorPSMask;
michael@0 292
michael@0 293 /* Vertex buffer containing our basic vertex structure */
michael@0 294 nsRefPtr<IDirect3DVertexBuffer9> mVB;
michael@0 295
michael@0 296 /* Our vertex declaration */
michael@0 297 nsRefPtr<IDirect3DVertexDeclaration9> mVD;
michael@0 298
michael@0 299 /* We maintain a doubly linked list of all d3d9 texture hosts which host
michael@0 300 * d3d9 textures created by this device manager.
michael@0 301 * Texture hosts must remove themselves when they disappear (i.e., we
michael@0 302 * expect all hosts in the list to be valid).
michael@0 303 * The list is cleared when we release the textures.
michael@0 304 */
michael@0 305 TextureSourceD3D9* mTextureHostList;
michael@0 306
michael@0 307 /* Our focus window - this is really a dummy window we can associate our
michael@0 308 * device with.
michael@0 309 */
michael@0 310 HWND mFocusWnd;
michael@0 311
michael@0 312 /* we use this to help track if our device temporarily or permanently lost */
michael@0 313 HMONITOR mDeviceMonitor;
michael@0 314
michael@0 315 uint32_t mDeviceResetCount;
michael@0 316
michael@0 317 uint32_t mMaxTextureSize;
michael@0 318
michael@0 319 /**
michael@0 320 * Wrap (repeat) or clamp textures. We prefer the former so we can do buffer
michael@0 321 * rotation, but some older hardware doesn't support it.
michael@0 322 */
michael@0 323 D3DTEXTUREADDRESS mTextureAddressingMode;
michael@0 324
michael@0 325 /* If this device supports dynamic textures */
michael@0 326 bool mHasDynamicTextures;
michael@0 327
michael@0 328 /* If this device was removed */
michael@0 329 bool mDeviceWasRemoved;
michael@0 330
michael@0 331 /* Nv3DVUtils instance */
michael@0 332 nsAutoPtr<Nv3DVUtils> mNv3DVUtils;
michael@0 333
michael@0 334 /**
michael@0 335 * Verifies all required device capabilities are present.
michael@0 336 */
michael@0 337 bool VerifyCaps();
michael@0 338 };
michael@0 339
michael@0 340 } /* namespace layers */
michael@0 341 } /* namespace mozilla */
michael@0 342
michael@0 343 #endif /* GFX_DEVICEMANAGERD3D9_H */

mercurial