Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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_LAYERMANAGERD3D9_H |
michael@0 | 7 | #define GFX_LAYERMANAGERD3D9_H |
michael@0 | 8 | |
michael@0 | 9 | #include "Layers.h" |
michael@0 | 10 | |
michael@0 | 11 | #include <windows.h> |
michael@0 | 12 | #include <d3d9.h> |
michael@0 | 13 | |
michael@0 | 14 | #include "gfxContext.h" |
michael@0 | 15 | #include "nsIWidget.h" |
michael@0 | 16 | |
michael@0 | 17 | #include "DeviceManagerD3D9.h" |
michael@0 | 18 | |
michael@0 | 19 | namespace mozilla { |
michael@0 | 20 | namespace layers { |
michael@0 | 21 | |
michael@0 | 22 | class LayerD3D9; |
michael@0 | 23 | class ThebesLayerD3D9; |
michael@0 | 24 | |
michael@0 | 25 | /* |
michael@0 | 26 | * This is the LayerManager used for Direct3D 9. For now this will render on |
michael@0 | 27 | * the main thread. |
michael@0 | 28 | */ |
michael@0 | 29 | class LayerManagerD3D9 : public LayerManager { |
michael@0 | 30 | public: |
michael@0 | 31 | LayerManagerD3D9(nsIWidget *aWidget); |
michael@0 | 32 | virtual ~LayerManagerD3D9(); |
michael@0 | 33 | |
michael@0 | 34 | /* |
michael@0 | 35 | * Initializes the layer manager, this is when the layer manager will |
michael@0 | 36 | * actually access the device and attempt to create the swap chain used |
michael@0 | 37 | * to draw to the window. If this method fails the device cannot be used. |
michael@0 | 38 | * This function is not threadsafe. |
michael@0 | 39 | * |
michael@0 | 40 | * \return True is initialization was succesful, false when it was not. |
michael@0 | 41 | */ |
michael@0 | 42 | bool Initialize(bool force = false); |
michael@0 | 43 | |
michael@0 | 44 | /* |
michael@0 | 45 | * Sets the clipping region for this layer manager. This is important on |
michael@0 | 46 | * windows because using OGL we no longer have GDI's native clipping. Therefor |
michael@0 | 47 | * widget must tell us what part of the screen is being invalidated, |
michael@0 | 48 | * and we should clip to this. |
michael@0 | 49 | * |
michael@0 | 50 | * \param aClippingRegion Region to clip to. Setting an empty region |
michael@0 | 51 | * will disable clipping. |
michael@0 | 52 | */ |
michael@0 | 53 | void SetClippingRegion(const nsIntRegion& aClippingRegion); |
michael@0 | 54 | |
michael@0 | 55 | /* |
michael@0 | 56 | * LayerManager implementation. |
michael@0 | 57 | */ |
michael@0 | 58 | virtual void Destroy(); |
michael@0 | 59 | |
michael@0 | 60 | virtual void BeginTransaction(); |
michael@0 | 61 | |
michael@0 | 62 | virtual void BeginTransactionWithTarget(gfxContext* aTarget); |
michael@0 | 63 | |
michael@0 | 64 | void EndConstruction(); |
michael@0 | 65 | |
michael@0 | 66 | virtual bool EndEmptyTransaction(EndTransactionFlags aFlags = END_DEFAULT); |
michael@0 | 67 | |
michael@0 | 68 | struct CallbackInfo { |
michael@0 | 69 | DrawThebesLayerCallback Callback; |
michael@0 | 70 | void *CallbackData; |
michael@0 | 71 | }; |
michael@0 | 72 | |
michael@0 | 73 | virtual void EndTransaction(DrawThebesLayerCallback aCallback, |
michael@0 | 74 | void* aCallbackData, |
michael@0 | 75 | EndTransactionFlags aFlags = END_DEFAULT); |
michael@0 | 76 | |
michael@0 | 77 | const CallbackInfo &GetCallbackInfo() { return mCurrentCallbackInfo; } |
michael@0 | 78 | |
michael@0 | 79 | void SetRoot(Layer* aLayer); |
michael@0 | 80 | |
michael@0 | 81 | virtual bool CanUseCanvasLayerForSize(const gfx::IntSize &aSize) |
michael@0 | 82 | { |
michael@0 | 83 | if (!mDeviceManager) |
michael@0 | 84 | return false; |
michael@0 | 85 | int32_t maxSize = mDeviceManager->GetMaxTextureSize(); |
michael@0 | 86 | return aSize <= gfx::IntSize(maxSize, maxSize); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | virtual int32_t GetMaxTextureSize() const |
michael@0 | 90 | { |
michael@0 | 91 | return mDeviceManager->GetMaxTextureSize(); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | virtual already_AddRefed<ThebesLayer> CreateThebesLayer(); |
michael@0 | 95 | |
michael@0 | 96 | virtual already_AddRefed<ContainerLayer> CreateContainerLayer(); |
michael@0 | 97 | |
michael@0 | 98 | virtual already_AddRefed<ImageLayer> CreateImageLayer(); |
michael@0 | 99 | |
michael@0 | 100 | virtual already_AddRefed<ColorLayer> CreateColorLayer(); |
michael@0 | 101 | |
michael@0 | 102 | virtual already_AddRefed<CanvasLayer> CreateCanvasLayer(); |
michael@0 | 103 | |
michael@0 | 104 | virtual already_AddRefed<ReadbackLayer> CreateReadbackLayer(); |
michael@0 | 105 | |
michael@0 | 106 | virtual LayersBackend GetBackendType() { return LayersBackend::LAYERS_D3D9; } |
michael@0 | 107 | virtual void GetBackendName(nsAString& name) { name.AssignLiteral("Direct3D 9"); } |
michael@0 | 108 | bool DeviceWasRemoved() { return deviceManager()->DeviceWasRemoved(); } |
michael@0 | 109 | |
michael@0 | 110 | /* |
michael@0 | 111 | * Helper methods. |
michael@0 | 112 | */ |
michael@0 | 113 | void SetClippingEnabled(bool aEnabled); |
michael@0 | 114 | |
michael@0 | 115 | void SetShaderMode(DeviceManagerD3D9::ShaderMode aMode, |
michael@0 | 116 | Layer* aMask, bool aIs2D = true) |
michael@0 | 117 | { mDeviceManager->SetShaderMode(aMode, aMask, aIs2D); } |
michael@0 | 118 | |
michael@0 | 119 | IDirect3DDevice9 *device() const { return mDeviceManager->device(); } |
michael@0 | 120 | DeviceManagerD3D9 *deviceManager() const { return mDeviceManager; } |
michael@0 | 121 | |
michael@0 | 122 | /** |
michael@0 | 123 | * Return pointer to the Nv3DVUtils instance. Re-direct to mDeviceManager. |
michael@0 | 124 | */ |
michael@0 | 125 | Nv3DVUtils *GetNv3DVUtils() { return mDeviceManager ? mDeviceManager->GetNv3DVUtils() : nullptr; } |
michael@0 | 126 | |
michael@0 | 127 | virtual const char* Name() const { return "D3D9"; } |
michael@0 | 128 | |
michael@0 | 129 | void ReportFailure(const nsACString &aMsg, HRESULT aCode); |
michael@0 | 130 | |
michael@0 | 131 | bool CompositingDisabled() { return mCompositingDisabled; } |
michael@0 | 132 | void SetCompositingDisabled(bool aCompositingDisabled) { mCompositingDisabled = aCompositingDisabled; } |
michael@0 | 133 | |
michael@0 | 134 | private: |
michael@0 | 135 | /* Device manager instance for this layer manager */ |
michael@0 | 136 | nsRefPtr<DeviceManagerD3D9> mDeviceManager; |
michael@0 | 137 | |
michael@0 | 138 | /* Swap chain associated with this layer manager */ |
michael@0 | 139 | nsRefPtr<SwapChainD3D9> mSwapChain; |
michael@0 | 140 | |
michael@0 | 141 | /* Widget associated with this layer manager */ |
michael@0 | 142 | nsIWidget *mWidget; |
michael@0 | 143 | |
michael@0 | 144 | /* |
michael@0 | 145 | * Context target, nullptr when drawing directly to our swap chain. |
michael@0 | 146 | */ |
michael@0 | 147 | nsRefPtr<gfxContext> mTarget; |
michael@0 | 148 | |
michael@0 | 149 | /* Callback info for current transaction */ |
michael@0 | 150 | CallbackInfo mCurrentCallbackInfo; |
michael@0 | 151 | |
michael@0 | 152 | /* |
michael@0 | 153 | * Region we're clipping our current drawing to. |
michael@0 | 154 | */ |
michael@0 | 155 | nsIntRegion mClippingRegion; |
michael@0 | 156 | |
michael@0 | 157 | /* |
michael@0 | 158 | * Device reset count at last paint. Whenever this changes, we need to |
michael@0 | 159 | * do a full layer tree update. |
michael@0 | 160 | */ |
michael@0 | 161 | uint32_t mDeviceResetCount; |
michael@0 | 162 | |
michael@0 | 163 | /* |
michael@0 | 164 | * True if we should only be drawing layer contents, not |
michael@0 | 165 | * compositing them to the target. |
michael@0 | 166 | */ |
michael@0 | 167 | bool mCompositingDisabled; |
michael@0 | 168 | |
michael@0 | 169 | /* |
michael@0 | 170 | * Render the current layer tree to the active target. |
michael@0 | 171 | */ |
michael@0 | 172 | void Render(); |
michael@0 | 173 | |
michael@0 | 174 | /* |
michael@0 | 175 | * Setup the pipeline. |
michael@0 | 176 | */ |
michael@0 | 177 | void SetupPipeline(); |
michael@0 | 178 | |
michael@0 | 179 | /* |
michael@0 | 180 | * Copies the content of our backbuffer to the set transaction target. |
michael@0 | 181 | */ |
michael@0 | 182 | void PaintToTarget(); |
michael@0 | 183 | |
michael@0 | 184 | }; |
michael@0 | 185 | |
michael@0 | 186 | /* |
michael@0 | 187 | * General information and tree management for OGL layers. |
michael@0 | 188 | */ |
michael@0 | 189 | class LayerD3D9 |
michael@0 | 190 | { |
michael@0 | 191 | public: |
michael@0 | 192 | LayerD3D9(LayerManagerD3D9 *aManager); |
michael@0 | 193 | |
michael@0 | 194 | virtual LayerD3D9 *GetFirstChildD3D9() { return nullptr; } |
michael@0 | 195 | |
michael@0 | 196 | void SetFirstChild(LayerD3D9 *aParent); |
michael@0 | 197 | |
michael@0 | 198 | virtual Layer* GetLayer() = 0; |
michael@0 | 199 | |
michael@0 | 200 | virtual void RenderLayer() = 0; |
michael@0 | 201 | |
michael@0 | 202 | /** |
michael@0 | 203 | /* This function may be used on device resets to clear all VRAM resources |
michael@0 | 204 | * that a layer might be using. |
michael@0 | 205 | */ |
michael@0 | 206 | virtual void CleanResources() {} |
michael@0 | 207 | |
michael@0 | 208 | IDirect3DDevice9 *device() const { return mD3DManager->device(); } |
michael@0 | 209 | |
michael@0 | 210 | /* Called by the layer manager when it's destroyed */ |
michael@0 | 211 | virtual void LayerManagerDestroyed() {} |
michael@0 | 212 | |
michael@0 | 213 | void ReportFailure(const nsACString &aMsg, HRESULT aCode) { |
michael@0 | 214 | return mD3DManager->ReportFailure(aMsg, aCode); |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | void SetShaderTransformAndOpacity() |
michael@0 | 218 | { |
michael@0 | 219 | Layer* layer = GetLayer(); |
michael@0 | 220 | const gfx::Matrix4x4& transform = layer->GetEffectiveTransform(); |
michael@0 | 221 | device()->SetVertexShaderConstantF(CBmLayerTransform, &transform._11, 4); |
michael@0 | 222 | |
michael@0 | 223 | float opacity[4]; |
michael@0 | 224 | /* |
michael@0 | 225 | * We always upload a 4 component float, but the shader will use only the |
michael@0 | 226 | * first component since it's declared as a 'float'. |
michael@0 | 227 | */ |
michael@0 | 228 | opacity[0] = layer->GetEffectiveOpacity(); |
michael@0 | 229 | device()->SetPixelShaderConstantF(CBfLayerOpacity, opacity, 1); |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | /* |
michael@0 | 233 | * Returns a texture containing the contents of this |
michael@0 | 234 | * layer. Will try to return an existing texture if possible, or a temporary |
michael@0 | 235 | * one if not. It is the callee's responsibility to release the shader |
michael@0 | 236 | * resource view. Will return null if a texture could not be constructed. |
michael@0 | 237 | * The texture will not be transformed, i.e., it will be in the same coord |
michael@0 | 238 | * space as this. |
michael@0 | 239 | * Any layer that can be used as a mask layer should override this method. |
michael@0 | 240 | * If aSize is non-null and a texture is successfully returned, aSize will |
michael@0 | 241 | * contain the size of the texture. |
michael@0 | 242 | */ |
michael@0 | 243 | virtual already_AddRefed<IDirect3DTexture9> GetAsTexture(gfx::IntSize* aSize) |
michael@0 | 244 | { |
michael@0 | 245 | return nullptr; |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | protected: |
michael@0 | 249 | LayerManagerD3D9 *mD3DManager; |
michael@0 | 250 | }; |
michael@0 | 251 | |
michael@0 | 252 | /* |
michael@0 | 253 | * RAII helper for locking D3D9 textures. |
michael@0 | 254 | */ |
michael@0 | 255 | class LockTextureRectD3D9 |
michael@0 | 256 | { |
michael@0 | 257 | public: |
michael@0 | 258 | LockTextureRectD3D9(IDirect3DTexture9* aTexture) |
michael@0 | 259 | : mTexture(aTexture) |
michael@0 | 260 | { |
michael@0 | 261 | mLockResult = mTexture->LockRect(0, &mR, nullptr, 0); |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | ~LockTextureRectD3D9() |
michael@0 | 265 | { |
michael@0 | 266 | mTexture->UnlockRect(0); |
michael@0 | 267 | } |
michael@0 | 268 | |
michael@0 | 269 | bool HasLock() { |
michael@0 | 270 | return SUCCEEDED(mLockResult); |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | D3DLOCKED_RECT GetLockRect() |
michael@0 | 274 | { |
michael@0 | 275 | return mR; |
michael@0 | 276 | } |
michael@0 | 277 | private: |
michael@0 | 278 | LockTextureRectD3D9 (const LockTextureRectD3D9&); |
michael@0 | 279 | LockTextureRectD3D9& operator= (const LockTextureRectD3D9&); |
michael@0 | 280 | |
michael@0 | 281 | IDirect3DTexture9* mTexture; |
michael@0 | 282 | D3DLOCKED_RECT mR; |
michael@0 | 283 | HRESULT mLockResult; |
michael@0 | 284 | }; |
michael@0 | 285 | |
michael@0 | 286 | } /* layers */ |
michael@0 | 287 | } /* mozilla */ |
michael@0 | 288 | |
michael@0 | 289 | #endif /* GFX_LAYERMANAGERD3D9_H */ |