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: 2 -*- |
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 "CanvasLayerD3D10.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "../d3d9/Nv3DVUtils.h" |
michael@0 | 9 | #include "gfxWindowsSurface.h" |
michael@0 | 10 | #include "gfxWindowsPlatform.h" |
michael@0 | 11 | #include "SurfaceStream.h" |
michael@0 | 12 | #include "SharedSurfaceANGLE.h" |
michael@0 | 13 | #include "SharedSurfaceGL.h" |
michael@0 | 14 | #include "gfxContext.h" |
michael@0 | 15 | #include "GLContext.h" |
michael@0 | 16 | #include "gfxPrefs.h" |
michael@0 | 17 | |
michael@0 | 18 | using namespace mozilla::gl; |
michael@0 | 19 | using namespace mozilla::gfx; |
michael@0 | 20 | |
michael@0 | 21 | namespace mozilla { |
michael@0 | 22 | namespace layers { |
michael@0 | 23 | |
michael@0 | 24 | CanvasLayerD3D10::CanvasLayerD3D10(LayerManagerD3D10 *aManager) |
michael@0 | 25 | : CanvasLayer(aManager, nullptr) |
michael@0 | 26 | , LayerD3D10(aManager) |
michael@0 | 27 | , mDataIsPremultiplied(false) |
michael@0 | 28 | , mNeedsYFlip(false) |
michael@0 | 29 | , mHasAlpha(true) |
michael@0 | 30 | { |
michael@0 | 31 | mImplData = static_cast<LayerD3D10*>(this); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | CanvasLayerD3D10::~CanvasLayerD3D10() |
michael@0 | 35 | { |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | void |
michael@0 | 39 | CanvasLayerD3D10::Initialize(const Data& aData) |
michael@0 | 40 | { |
michael@0 | 41 | NS_ASSERTION(mSurface == nullptr, "BasicCanvasLayer::Initialize called twice!"); |
michael@0 | 42 | |
michael@0 | 43 | if (aData.mGLContext) { |
michael@0 | 44 | mGLContext = aData.mGLContext; |
michael@0 | 45 | NS_ASSERTION(mGLContext->IsOffscreen(), "Canvas GLContext must be offscreen."); |
michael@0 | 46 | mDataIsPremultiplied = aData.mIsGLAlphaPremult; |
michael@0 | 47 | mNeedsYFlip = true; |
michael@0 | 48 | |
michael@0 | 49 | GLScreenBuffer* screen = mGLContext->Screen(); |
michael@0 | 50 | SurfaceStreamType streamType = |
michael@0 | 51 | SurfaceStream::ChooseGLStreamType(SurfaceStream::MainThread, |
michael@0 | 52 | screen->PreserveBuffer()); |
michael@0 | 53 | |
michael@0 | 54 | SurfaceFactory_GL* factory = nullptr; |
michael@0 | 55 | if (!gfxPrefs::WebGLForceLayersReadback()) { |
michael@0 | 56 | if (mGLContext->IsANGLE()) { |
michael@0 | 57 | factory = SurfaceFactory_ANGLEShareHandle::Create(mGLContext, |
michael@0 | 58 | device(), |
michael@0 | 59 | screen->Caps()); |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | if (factory) { |
michael@0 | 64 | screen->Morph(factory, streamType); |
michael@0 | 65 | } |
michael@0 | 66 | } else if (aData.mDrawTarget) { |
michael@0 | 67 | mDrawTarget = aData.mDrawTarget; |
michael@0 | 68 | mNeedsYFlip = false; |
michael@0 | 69 | mDataIsPremultiplied = true; |
michael@0 | 70 | void *texture = mDrawTarget->GetNativeSurface(NativeSurfaceType::D3D10_TEXTURE); |
michael@0 | 71 | |
michael@0 | 72 | if (texture) { |
michael@0 | 73 | mTexture = static_cast<ID3D10Texture2D*>(texture); |
michael@0 | 74 | |
michael@0 | 75 | NS_ASSERTION(!aData.mGLContext, |
michael@0 | 76 | "CanvasLayer can't have both DrawTarget and WebGLContext/Surface"); |
michael@0 | 77 | |
michael@0 | 78 | mBounds.SetRect(0, 0, aData.mSize.width, aData.mSize.height); |
michael@0 | 79 | device()->CreateShaderResourceView(mTexture, nullptr, getter_AddRefs(mSRView)); |
michael@0 | 80 | return; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | // XXX we should store mDrawTarget and use it directly in UpdateSurface, |
michael@0 | 84 | // bypassing Thebes |
michael@0 | 85 | mSurface = mDrawTarget->Snapshot(); |
michael@0 | 86 | } else { |
michael@0 | 87 | NS_ERROR("CanvasLayer created without mSurface, mDrawTarget or mGLContext?"); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | mBounds.SetRect(0, 0, aData.mSize.width, aData.mSize.height); |
michael@0 | 91 | mIsD2DTexture = false; |
michael@0 | 92 | |
michael@0 | 93 | // Create a texture in case we need to readback. |
michael@0 | 94 | CD3D10_TEXTURE2D_DESC desc(DXGI_FORMAT_B8G8R8A8_UNORM, mBounds.width, mBounds.height, 1, 1); |
michael@0 | 95 | desc.Usage = D3D10_USAGE_DYNAMIC; |
michael@0 | 96 | desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; |
michael@0 | 97 | |
michael@0 | 98 | HRESULT hr = device()->CreateTexture2D(&desc, nullptr, getter_AddRefs(mTexture)); |
michael@0 | 99 | if (FAILED(hr)) { |
michael@0 | 100 | NS_WARNING("Failed to create texture for CanvasLayer!"); |
michael@0 | 101 | return; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | device()->CreateShaderResourceView(mTexture, nullptr, getter_AddRefs(mUploadSRView)); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | void |
michael@0 | 108 | CanvasLayerD3D10::UpdateSurface() |
michael@0 | 109 | { |
michael@0 | 110 | if (!IsDirty()) |
michael@0 | 111 | return; |
michael@0 | 112 | Painted(); |
michael@0 | 113 | |
michael@0 | 114 | if (mDrawTarget) { |
michael@0 | 115 | mDrawTarget->Flush(); |
michael@0 | 116 | } else if (mIsD2DTexture) { |
michael@0 | 117 | return; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | if (!mTexture) { |
michael@0 | 121 | return; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | if (mGLContext) { |
michael@0 | 125 | SharedSurface_GL* surf = mGLContext->RequestFrame(); |
michael@0 | 126 | if (!surf) { |
michael@0 | 127 | return; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | switch (surf->Type()) { |
michael@0 | 131 | case SharedSurfaceType::EGLSurfaceANGLE: { |
michael@0 | 132 | SharedSurface_ANGLEShareHandle* shareSurf = SharedSurface_ANGLEShareHandle::Cast(surf); |
michael@0 | 133 | |
michael@0 | 134 | mSRView = shareSurf->GetSRV(); |
michael@0 | 135 | return; |
michael@0 | 136 | } |
michael@0 | 137 | case SharedSurfaceType::Basic: { |
michael@0 | 138 | SharedSurface_Basic* shareSurf = SharedSurface_Basic::Cast(surf); |
michael@0 | 139 | // WebGL reads entire surface. |
michael@0 | 140 | D3D10_MAPPED_TEXTURE2D map; |
michael@0 | 141 | |
michael@0 | 142 | HRESULT hr = mTexture->Map(0, D3D10_MAP_WRITE_DISCARD, 0, &map); |
michael@0 | 143 | |
michael@0 | 144 | if (FAILED(hr)) { |
michael@0 | 145 | NS_WARNING("Failed to map CanvasLayer texture."); |
michael@0 | 146 | return; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | DataSourceSurface* frameData = shareSurf->GetData(); |
michael@0 | 150 | // Scope for DrawTarget, so it's destroyed before Unmap. |
michael@0 | 151 | { |
michael@0 | 152 | IntSize boundsSize(mBounds.width, mBounds.height); |
michael@0 | 153 | RefPtr<DrawTarget> mapDt = Factory::CreateDrawTargetForData(BackendType::CAIRO, |
michael@0 | 154 | (uint8_t*)map.pData, |
michael@0 | 155 | boundsSize, |
michael@0 | 156 | map.RowPitch, |
michael@0 | 157 | SurfaceFormat::B8G8R8A8); |
michael@0 | 158 | |
michael@0 | 159 | Rect drawRect(0, 0, frameData->GetSize().width, frameData->GetSize().height); |
michael@0 | 160 | mapDt->DrawSurface(frameData, drawRect, drawRect, |
michael@0 | 161 | DrawSurfaceOptions(), DrawOptions(1.0F, CompositionOp::OP_SOURCE)); |
michael@0 | 162 | mapDt->Flush(); |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | mTexture->Unmap(0); |
michael@0 | 166 | mSRView = mUploadSRView; |
michael@0 | 167 | break; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | default: |
michael@0 | 171 | MOZ_CRASH("Unhandled SharedSurfaceType."); |
michael@0 | 172 | } |
michael@0 | 173 | } else if (mSurface) { |
michael@0 | 174 | D3D10_MAPPED_TEXTURE2D map; |
michael@0 | 175 | HRESULT hr = mTexture->Map(0, D3D10_MAP_WRITE_DISCARD, 0, &map); |
michael@0 | 176 | |
michael@0 | 177 | if (FAILED(hr)) { |
michael@0 | 178 | NS_WARNING("Failed to lock CanvasLayer texture."); |
michael@0 | 179 | return; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | RefPtr<DrawTarget> destTarget = |
michael@0 | 183 | Factory::CreateDrawTargetForD3D10Texture(mTexture, |
michael@0 | 184 | SurfaceFormat::R8G8B8A8); |
michael@0 | 185 | Rect r(Point(0, 0), ToRect(mBounds).Size()); |
michael@0 | 186 | destTarget->DrawSurface(mSurface, r, r, DrawSurfaceOptions(), |
michael@0 | 187 | DrawOptions(1.0F, CompositionOp::OP_SOURCE)); |
michael@0 | 188 | |
michael@0 | 189 | mTexture->Unmap(0); |
michael@0 | 190 | mSRView = mUploadSRView; |
michael@0 | 191 | } |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | Layer* |
michael@0 | 195 | CanvasLayerD3D10::GetLayer() |
michael@0 | 196 | { |
michael@0 | 197 | return this; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | void |
michael@0 | 201 | CanvasLayerD3D10::RenderLayer() |
michael@0 | 202 | { |
michael@0 | 203 | FirePreTransactionCallback(); |
michael@0 | 204 | UpdateSurface(); |
michael@0 | 205 | FireDidTransactionCallback(); |
michael@0 | 206 | |
michael@0 | 207 | if (!mTexture) |
michael@0 | 208 | return; |
michael@0 | 209 | |
michael@0 | 210 | nsIntRect visibleRect = mVisibleRegion.GetBounds(); |
michael@0 | 211 | |
michael@0 | 212 | SetEffectTransformAndOpacity(); |
michael@0 | 213 | |
michael@0 | 214 | uint8_t shaderFlags = 0; |
michael@0 | 215 | shaderFlags |= LoadMaskTexture(); |
michael@0 | 216 | shaderFlags |= mDataIsPremultiplied |
michael@0 | 217 | ? SHADER_PREMUL : SHADER_NON_PREMUL | SHADER_RGBA; |
michael@0 | 218 | shaderFlags |= mHasAlpha ? SHADER_RGBA : SHADER_RGB; |
michael@0 | 219 | shaderFlags |= mFilter == GraphicsFilter::FILTER_NEAREST |
michael@0 | 220 | ? SHADER_POINT : SHADER_LINEAR; |
michael@0 | 221 | ID3D10EffectTechnique* technique = SelectShader(shaderFlags); |
michael@0 | 222 | |
michael@0 | 223 | if (mSRView) { |
michael@0 | 224 | effect()->GetVariableByName("tRGB")->AsShaderResource()->SetResource(mSRView); |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | effect()->GetVariableByName("vLayerQuad")->AsVector()->SetFloatVector( |
michael@0 | 228 | ShaderConstantRectD3D10( |
michael@0 | 229 | (float)mBounds.x, |
michael@0 | 230 | (float)mBounds.y, |
michael@0 | 231 | (float)mBounds.width, |
michael@0 | 232 | (float)mBounds.height) |
michael@0 | 233 | ); |
michael@0 | 234 | |
michael@0 | 235 | if (mNeedsYFlip) { |
michael@0 | 236 | effect()->GetVariableByName("vTextureCoords")->AsVector()->SetFloatVector( |
michael@0 | 237 | ShaderConstantRectD3D10( |
michael@0 | 238 | 0, |
michael@0 | 239 | 1.0f, |
michael@0 | 240 | 1.0f, |
michael@0 | 241 | -1.0f) |
michael@0 | 242 | ); |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | technique->GetPassByIndex(0)->Apply(0); |
michael@0 | 246 | device()->Draw(4, 0); |
michael@0 | 247 | |
michael@0 | 248 | if (mNeedsYFlip) { |
michael@0 | 249 | effect()->GetVariableByName("vTextureCoords")->AsVector()-> |
michael@0 | 250 | SetFloatVector(ShaderConstantRectD3D10(0, 0, 1.0f, 1.0f)); |
michael@0 | 251 | } |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | } /* namespace layers */ |
michael@0 | 255 | } /* namespace mozilla */ |