Thu, 22 Jan 2015 13:21:57 +0100
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 | #include "ContainerLayerD3D9.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "ThebesLayerD3D9.h" |
michael@0 | 9 | #include "ReadbackProcessor.h" |
michael@0 | 10 | |
michael@0 | 11 | using namespace mozilla::gfx; |
michael@0 | 12 | |
michael@0 | 13 | namespace mozilla { |
michael@0 | 14 | namespace layers { |
michael@0 | 15 | |
michael@0 | 16 | ContainerLayerD3D9::ContainerLayerD3D9(LayerManagerD3D9 *aManager) |
michael@0 | 17 | : ContainerLayer(aManager, nullptr) |
michael@0 | 18 | , LayerD3D9(aManager) |
michael@0 | 19 | { |
michael@0 | 20 | mImplData = static_cast<LayerD3D9*>(this); |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | ContainerLayerD3D9::~ContainerLayerD3D9() |
michael@0 | 24 | { |
michael@0 | 25 | while (mFirstChild) { |
michael@0 | 26 | RemoveChild(mFirstChild); |
michael@0 | 27 | } |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | Layer* |
michael@0 | 31 | ContainerLayerD3D9::GetLayer() |
michael@0 | 32 | { |
michael@0 | 33 | return this; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | LayerD3D9* |
michael@0 | 37 | ContainerLayerD3D9::GetFirstChildD3D9() |
michael@0 | 38 | { |
michael@0 | 39 | if (!mFirstChild) { |
michael@0 | 40 | return nullptr; |
michael@0 | 41 | } |
michael@0 | 42 | return static_cast<LayerD3D9*>(mFirstChild->ImplData()); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | void |
michael@0 | 46 | ContainerLayerD3D9::RenderLayer() |
michael@0 | 47 | { |
michael@0 | 48 | nsRefPtr<IDirect3DSurface9> previousRenderTarget; |
michael@0 | 49 | nsRefPtr<IDirect3DTexture9> renderTexture; |
michael@0 | 50 | float previousRenderTargetOffset[4]; |
michael@0 | 51 | float renderTargetOffset[] = { 0, 0, 0, 0 }; |
michael@0 | 52 | float oldViewMatrix[4][4]; |
michael@0 | 53 | |
michael@0 | 54 | RECT containerD3D9ClipRect; |
michael@0 | 55 | device()->GetScissorRect(&containerD3D9ClipRect); |
michael@0 | 56 | // Convert scissor to an nsIntRect. RECT's are exclusive on the bottom and |
michael@0 | 57 | // right values. |
michael@0 | 58 | nsIntRect oldScissor(containerD3D9ClipRect.left, |
michael@0 | 59 | containerD3D9ClipRect.top, |
michael@0 | 60 | containerD3D9ClipRect.right - containerD3D9ClipRect.left, |
michael@0 | 61 | containerD3D9ClipRect.bottom - containerD3D9ClipRect.top); |
michael@0 | 62 | |
michael@0 | 63 | ReadbackProcessor readback; |
michael@0 | 64 | readback.BuildUpdates(this); |
michael@0 | 65 | |
michael@0 | 66 | nsIntRect visibleRect = GetEffectiveVisibleRegion().GetBounds(); |
michael@0 | 67 | bool useIntermediate = UseIntermediateSurface(); |
michael@0 | 68 | |
michael@0 | 69 | mSupportsComponentAlphaChildren = false; |
michael@0 | 70 | if (useIntermediate) { |
michael@0 | 71 | nsRefPtr<IDirect3DSurface9> renderSurface; |
michael@0 | 72 | if (!mD3DManager->CompositingDisabled()) { |
michael@0 | 73 | device()->GetRenderTarget(0, getter_AddRefs(previousRenderTarget)); |
michael@0 | 74 | HRESULT hr = device()->CreateTexture(visibleRect.width, visibleRect.height, 1, |
michael@0 | 75 | D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, |
michael@0 | 76 | D3DPOOL_DEFAULT, getter_AddRefs(renderTexture), |
michael@0 | 77 | nullptr); |
michael@0 | 78 | if (FAILED(hr)) { |
michael@0 | 79 | ReportFailure(NS_LITERAL_CSTRING("ContainerLayerD3D9::ContainerRender(): Failed to create texture"), |
michael@0 | 80 | hr); |
michael@0 | 81 | return; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | nsRefPtr<IDirect3DSurface9> renderSurface; |
michael@0 | 85 | renderTexture->GetSurfaceLevel(0, getter_AddRefs(renderSurface)); |
michael@0 | 86 | device()->SetRenderTarget(0, renderSurface); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | if (mVisibleRegion.GetNumRects() == 1 && |
michael@0 | 90 | (GetContentFlags() & CONTENT_OPAQUE)) { |
michael@0 | 91 | // don't need a background, we're going to paint all opaque stuff |
michael@0 | 92 | mSupportsComponentAlphaChildren = true; |
michael@0 | 93 | } else { |
michael@0 | 94 | Matrix4x4 transform3D = GetEffectiveTransform(); |
michael@0 | 95 | Matrix transform; |
michael@0 | 96 | // If we have an opaque ancestor layer, then we can be sure that |
michael@0 | 97 | // all the pixels we draw into are either opaque already or will be |
michael@0 | 98 | // covered by something opaque. Otherwise copying up the background is |
michael@0 | 99 | // not safe. |
michael@0 | 100 | HRESULT hr = E_FAIL; |
michael@0 | 101 | if (HasOpaqueAncestorLayer(this) && |
michael@0 | 102 | transform3D.Is2D(&transform) && !ThebesMatrix(transform).HasNonIntegerTranslation()) { |
michael@0 | 103 | // Copy background up from below |
michael@0 | 104 | RECT dest = { 0, 0, visibleRect.width, visibleRect.height }; |
michael@0 | 105 | RECT src = dest; |
michael@0 | 106 | ::OffsetRect(&src, |
michael@0 | 107 | visibleRect.x + int32_t(transform._31), |
michael@0 | 108 | visibleRect.y + int32_t(transform._32)); |
michael@0 | 109 | if (!mD3DManager->CompositingDisabled()) { |
michael@0 | 110 | hr = device()-> |
michael@0 | 111 | StretchRect(previousRenderTarget, &src, renderSurface, &dest, D3DTEXF_NONE); |
michael@0 | 112 | } |
michael@0 | 113 | } |
michael@0 | 114 | if (hr == S_OK) { |
michael@0 | 115 | mSupportsComponentAlphaChildren = true; |
michael@0 | 116 | } else if (!mD3DManager->CompositingDisabled()) { |
michael@0 | 117 | device()-> |
michael@0 | 118 | Clear(0, 0, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 0, 0, 0), 0, 0); |
michael@0 | 119 | } |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | device()-> |
michael@0 | 123 | GetVertexShaderConstantF(CBvRenderTargetOffset, previousRenderTargetOffset, 1); |
michael@0 | 124 | renderTargetOffset[0] = (float)visibleRect.x; |
michael@0 | 125 | renderTargetOffset[1] = (float)visibleRect.y; |
michael@0 | 126 | device()-> |
michael@0 | 127 | SetVertexShaderConstantF(CBvRenderTargetOffset, renderTargetOffset, 1); |
michael@0 | 128 | |
michael@0 | 129 | gfx3DMatrix viewMatrix; |
michael@0 | 130 | /* |
michael@0 | 131 | * Matrix to transform to viewport space ( <-1.0, 1.0> topleft, |
michael@0 | 132 | * <1.0, -1.0> bottomright) |
michael@0 | 133 | */ |
michael@0 | 134 | viewMatrix._11 = 2.0f / visibleRect.width; |
michael@0 | 135 | viewMatrix._22 = -2.0f / visibleRect.height; |
michael@0 | 136 | viewMatrix._41 = -1.0f; |
michael@0 | 137 | viewMatrix._42 = 1.0f; |
michael@0 | 138 | |
michael@0 | 139 | device()-> |
michael@0 | 140 | GetVertexShaderConstantF(CBmProjection, &oldViewMatrix[0][0], 4); |
michael@0 | 141 | device()-> |
michael@0 | 142 | SetVertexShaderConstantF(CBmProjection, &viewMatrix._11, 4); |
michael@0 | 143 | } else { |
michael@0 | 144 | mSupportsComponentAlphaChildren = |
michael@0 | 145 | (GetContentFlags() & CONTENT_OPAQUE) || |
michael@0 | 146 | (mParent && |
michael@0 | 147 | mParent->SupportsComponentAlphaChildren()); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | nsAutoTArray<Layer*, 12> children; |
michael@0 | 151 | SortChildrenBy3DZOrder(children); |
michael@0 | 152 | |
michael@0 | 153 | /* |
michael@0 | 154 | * Render this container's contents. |
michael@0 | 155 | */ |
michael@0 | 156 | for (uint32_t i = 0; i < children.Length(); i++) { |
michael@0 | 157 | LayerD3D9* layerToRender = static_cast<LayerD3D9*>(children.ElementAt(i)->ImplData()); |
michael@0 | 158 | |
michael@0 | 159 | if (layerToRender->GetLayer()->GetEffectiveVisibleRegion().IsEmpty()) { |
michael@0 | 160 | continue; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | nsIntRect scissorRect = |
michael@0 | 164 | layerToRender->GetLayer()->CalculateScissorRect(oldScissor, nullptr); |
michael@0 | 165 | if (scissorRect.IsEmpty()) { |
michael@0 | 166 | continue; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | RECT d3drect; |
michael@0 | 170 | d3drect.left = scissorRect.x; |
michael@0 | 171 | d3drect.top = scissorRect.y; |
michael@0 | 172 | d3drect.right = scissorRect.x + scissorRect.width; |
michael@0 | 173 | d3drect.bottom = scissorRect.y + scissorRect.height; |
michael@0 | 174 | device()->SetScissorRect(&d3drect); |
michael@0 | 175 | |
michael@0 | 176 | if (layerToRender->GetLayer()->GetType() == TYPE_THEBES) { |
michael@0 | 177 | static_cast<ThebesLayerD3D9*>(layerToRender)->RenderThebesLayer(&readback); |
michael@0 | 178 | } else { |
michael@0 | 179 | layerToRender->RenderLayer(); |
michael@0 | 180 | } |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | if (useIntermediate && !mD3DManager->CompositingDisabled()) { |
michael@0 | 184 | device()->SetRenderTarget(0, previousRenderTarget); |
michael@0 | 185 | device()->SetVertexShaderConstantF(CBvRenderTargetOffset, previousRenderTargetOffset, 1); |
michael@0 | 186 | device()->SetVertexShaderConstantF(CBmProjection, &oldViewMatrix[0][0], 4); |
michael@0 | 187 | |
michael@0 | 188 | device()->SetVertexShaderConstantF(CBvLayerQuad, |
michael@0 | 189 | ShaderConstantRect(visibleRect.x, |
michael@0 | 190 | visibleRect.y, |
michael@0 | 191 | visibleRect.width, |
michael@0 | 192 | visibleRect.height), |
michael@0 | 193 | 1); |
michael@0 | 194 | |
michael@0 | 195 | SetShaderTransformAndOpacity(); |
michael@0 | 196 | mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBALAYER, |
michael@0 | 197 | GetMaskLayer(), |
michael@0 | 198 | GetTransform().CanDraw2D()); |
michael@0 | 199 | |
michael@0 | 200 | device()->SetTexture(0, renderTexture); |
michael@0 | 201 | device()->SetScissorRect(&containerD3D9ClipRect); |
michael@0 | 202 | device()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); |
michael@0 | 203 | } else { |
michael@0 | 204 | device()->SetScissorRect(&containerD3D9ClipRect); |
michael@0 | 205 | } |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | void |
michael@0 | 209 | ContainerLayerD3D9::LayerManagerDestroyed() |
michael@0 | 210 | { |
michael@0 | 211 | while (mFirstChild) { |
michael@0 | 212 | GetFirstChildD3D9()->LayerManagerDestroyed(); |
michael@0 | 213 | RemoveChild(mFirstChild); |
michael@0 | 214 | } |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | } /* layers */ |
michael@0 | 218 | } /* mozilla */ |