gfx/layers/d3d9/CanvasLayerD3D9.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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
michael@0 7 #include "mozilla/layers/PLayerTransaction.h"
michael@0 8
michael@0 9 #include "gfxWindowsSurface.h"
michael@0 10 #include "gfxWindowsPlatform.h"
michael@0 11 #include "SurfaceStream.h"
michael@0 12 #include "SharedSurfaceGL.h"
michael@0 13 #include "GLContext.h"
michael@0 14 #include "CanvasLayerD3D9.h"
michael@0 15
michael@0 16 using namespace mozilla::gfx;
michael@0 17 using namespace mozilla::gl;
michael@0 18
michael@0 19 namespace mozilla {
michael@0 20 namespace layers {
michael@0 21
michael@0 22 CanvasLayerD3D9::CanvasLayerD3D9(LayerManagerD3D9 *aManager)
michael@0 23 : CanvasLayer(aManager, nullptr)
michael@0 24 , LayerD3D9(aManager)
michael@0 25 , mDataIsPremultiplied(false)
michael@0 26 , mNeedsYFlip(false)
michael@0 27 , mHasAlpha(true)
michael@0 28 {
michael@0 29 mImplData = static_cast<LayerD3D9*>(this);
michael@0 30 aManager->deviceManager()->mLayersWithResources.AppendElement(this);
michael@0 31 }
michael@0 32
michael@0 33 CanvasLayerD3D9::~CanvasLayerD3D9()
michael@0 34 {
michael@0 35 if (mD3DManager) {
michael@0 36 mD3DManager->deviceManager()->mLayersWithResources.RemoveElement(this);
michael@0 37 }
michael@0 38 }
michael@0 39
michael@0 40 void
michael@0 41 CanvasLayerD3D9::Initialize(const Data& aData)
michael@0 42 {
michael@0 43 NS_ASSERTION(mDrawTarget == nullptr, "BasicCanvasLayer::Initialize called twice!");
michael@0 44
michael@0 45 if (aData.mDrawTarget) {
michael@0 46 mDrawTarget = aData.mDrawTarget;
michael@0 47 mNeedsYFlip = false;
michael@0 48 mDataIsPremultiplied = true;
michael@0 49 } else if (aData.mGLContext) {
michael@0 50 mGLContext = aData.mGLContext;
michael@0 51 NS_ASSERTION(mGLContext->IsOffscreen(), "Canvas GLContext must be offscreen.");
michael@0 52 mDataIsPremultiplied = aData.mIsGLAlphaPremult;
michael@0 53 mNeedsYFlip = true;
michael@0 54 } else {
michael@0 55 NS_ERROR("CanvasLayer created without mGLContext or mDrawTarget?");
michael@0 56 }
michael@0 57
michael@0 58 mBounds.SetRect(0, 0, aData.mSize.width, aData.mSize.height);
michael@0 59
michael@0 60 CreateTexture();
michael@0 61 }
michael@0 62
michael@0 63 void
michael@0 64 CanvasLayerD3D9::UpdateSurface()
michael@0 65 {
michael@0 66 if (!IsDirty() && mTexture)
michael@0 67 return;
michael@0 68 Painted();
michael@0 69
michael@0 70 if (!mTexture) {
michael@0 71 CreateTexture();
michael@0 72
michael@0 73 if (!mTexture) {
michael@0 74 NS_WARNING("CanvasLayerD3D9::Updated called but no texture present and creation failed!");
michael@0 75 return;
michael@0 76 }
michael@0 77 }
michael@0 78
michael@0 79 RefPtr<SourceSurface> surface;
michael@0 80
michael@0 81 if (mGLContext) {
michael@0 82 SharedSurface_GL* surf = mGLContext->RequestFrame();
michael@0 83 if (!surf)
michael@0 84 return;
michael@0 85
michael@0 86 SharedSurface_Basic* shareSurf = SharedSurface_Basic::Cast(surf);
michael@0 87 surface = shareSurf->GetData();
michael@0 88 } else {
michael@0 89 surface = mDrawTarget->Snapshot();
michael@0 90 }
michael@0 91
michael@0 92 // WebGL reads entire surface.
michael@0 93 LockTextureRectD3D9 textureLock(mTexture);
michael@0 94 if (!textureLock.HasLock()) {
michael@0 95 NS_WARNING("Failed to lock CanvasLayer texture.");
michael@0 96 return;
michael@0 97 }
michael@0 98
michael@0 99 D3DLOCKED_RECT rect = textureLock.GetLockRect();
michael@0 100 IntSize boundsSize(mBounds.width, mBounds.height);
michael@0 101 RefPtr<DrawTarget> rectDt = Factory::CreateDrawTargetForData(BackendType::CAIRO,
michael@0 102 (uint8_t*)rect.pBits,
michael@0 103 boundsSize,
michael@0 104 rect.Pitch,
michael@0 105 SurfaceFormat::B8G8R8A8);
michael@0 106
michael@0 107 Rect drawRect(0, 0, surface->GetSize().width, surface->GetSize().height);
michael@0 108 rectDt->DrawSurface(surface, drawRect, drawRect,
michael@0 109 DrawSurfaceOptions(), DrawOptions(1.0F, CompositionOp::OP_SOURCE));
michael@0 110 rectDt->Flush();
michael@0 111 }
michael@0 112
michael@0 113 Layer*
michael@0 114 CanvasLayerD3D9::GetLayer()
michael@0 115 {
michael@0 116 return this;
michael@0 117 }
michael@0 118
michael@0 119 void
michael@0 120 CanvasLayerD3D9::RenderLayer()
michael@0 121 {
michael@0 122 FirePreTransactionCallback();
michael@0 123 UpdateSurface();
michael@0 124 if (mD3DManager->CompositingDisabled()) {
michael@0 125 return;
michael@0 126 }
michael@0 127 FireDidTransactionCallback();
michael@0 128
michael@0 129 if (!mTexture)
michael@0 130 return;
michael@0 131
michael@0 132 /*
michael@0 133 * We flip the Y axis here, note we can only do this because we are in
michael@0 134 * CULL_NONE mode!
michael@0 135 */
michael@0 136
michael@0 137 ShaderConstantRect quad(0, 0, mBounds.width, mBounds.height);
michael@0 138 if (mNeedsYFlip) {
michael@0 139 quad.mHeight = (float)-mBounds.height;
michael@0 140 quad.mY = (float)mBounds.height;
michael@0 141 }
michael@0 142
michael@0 143 device()->SetVertexShaderConstantF(CBvLayerQuad, quad, 1);
michael@0 144
michael@0 145 SetShaderTransformAndOpacity();
michael@0 146
michael@0 147 if (mHasAlpha) {
michael@0 148 mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBALAYER, GetMaskLayer());
michael@0 149 } else {
michael@0 150 mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBLAYER, GetMaskLayer());
michael@0 151 }
michael@0 152
michael@0 153 if (mFilter == GraphicsFilter::FILTER_NEAREST) {
michael@0 154 device()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
michael@0 155 device()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
michael@0 156 }
michael@0 157 if (!mDataIsPremultiplied) {
michael@0 158 device()->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
michael@0 159 device()->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
michael@0 160 }
michael@0 161 device()->SetTexture(0, mTexture);
michael@0 162 device()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
michael@0 163 if (!mDataIsPremultiplied) {
michael@0 164 device()->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
michael@0 165 device()->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
michael@0 166 }
michael@0 167 if (mFilter == GraphicsFilter::FILTER_NEAREST) {
michael@0 168 device()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
michael@0 169 device()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
michael@0 170 }
michael@0 171 }
michael@0 172
michael@0 173 void
michael@0 174 CanvasLayerD3D9::CleanResources()
michael@0 175 {
michael@0 176 if (mD3DManager->deviceManager()->HasDynamicTextures()) {
michael@0 177 // In this case we have a texture in POOL_DEFAULT
michael@0 178 mTexture = nullptr;
michael@0 179 }
michael@0 180 }
michael@0 181
michael@0 182 void
michael@0 183 CanvasLayerD3D9::LayerManagerDestroyed()
michael@0 184 {
michael@0 185 mD3DManager->deviceManager()->mLayersWithResources.RemoveElement(this);
michael@0 186 mD3DManager = nullptr;
michael@0 187 }
michael@0 188
michael@0 189 void
michael@0 190 CanvasLayerD3D9::CreateTexture()
michael@0 191 {
michael@0 192 HRESULT hr;
michael@0 193 if (mD3DManager->deviceManager()->HasDynamicTextures()) {
michael@0 194 hr = device()->CreateTexture(mBounds.width, mBounds.height, 1, D3DUSAGE_DYNAMIC,
michael@0 195 D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT,
michael@0 196 getter_AddRefs(mTexture), nullptr);
michael@0 197 } else {
michael@0 198 // D3DPOOL_MANAGED is fine here since we require Dynamic Textures for D3D9Ex
michael@0 199 // devices.
michael@0 200 hr = device()->CreateTexture(mBounds.width, mBounds.height, 1, 0,
michael@0 201 D3DFMT_A8R8G8B8, D3DPOOL_MANAGED,
michael@0 202 getter_AddRefs(mTexture), nullptr);
michael@0 203 }
michael@0 204 if (FAILED(hr)) {
michael@0 205 mD3DManager->ReportFailure(NS_LITERAL_CSTRING("CanvasLayerD3D9::CreateTexture() failed"),
michael@0 206 hr);
michael@0 207 return;
michael@0 208 }
michael@0 209 }
michael@0 210
michael@0 211 } /* namespace layers */
michael@0 212 } /* namespace mozilla */

mercurial