Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; 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 "BasicLayersImpl.h" // for FillWithMask, etc |
michael@0 | 7 | #include "CopyableCanvasLayer.h" |
michael@0 | 8 | #include "GLContext.h" // for GLContext |
michael@0 | 9 | #include "GLScreenBuffer.h" // for GLScreenBuffer |
michael@0 | 10 | #include "SharedSurface.h" // for SharedSurface |
michael@0 | 11 | #include "SharedSurfaceGL.h" // for SharedSurface_GL, etc |
michael@0 | 12 | #include "SurfaceTypes.h" // for APITypeT, APITypeT::OpenGL, etc |
michael@0 | 13 | #include "gfxMatrix.h" // for gfxMatrix |
michael@0 | 14 | #include "gfxPattern.h" // for gfxPattern, etc |
michael@0 | 15 | #include "gfxPlatform.h" // for gfxPlatform, gfxImageFormat |
michael@0 | 16 | #include "gfxRect.h" // for gfxRect |
michael@0 | 17 | #include "gfxUtils.h" // for gfxUtils |
michael@0 | 18 | #include "gfx2DGlue.h" // for thebes --> moz2d transition |
michael@0 | 19 | #include "mozilla/gfx/BaseSize.h" // for BaseSize |
michael@0 | 20 | #include "nsDebug.h" // for NS_ASSERTION, NS_WARNING, etc |
michael@0 | 21 | #include "nsISupportsImpl.h" // for gfxContext::AddRef, etc |
michael@0 | 22 | #include "nsRect.h" // for nsIntRect |
michael@0 | 23 | #include "nsSize.h" // for nsIntSize |
michael@0 | 24 | #include "LayerUtils.h" |
michael@0 | 25 | |
michael@0 | 26 | using namespace mozilla::gfx; |
michael@0 | 27 | using namespace mozilla::gl; |
michael@0 | 28 | |
michael@0 | 29 | namespace mozilla { |
michael@0 | 30 | namespace layers { |
michael@0 | 31 | |
michael@0 | 32 | CopyableCanvasLayer::CopyableCanvasLayer(LayerManager* aLayerManager, void *aImplData) : |
michael@0 | 33 | CanvasLayer(aLayerManager, aImplData) |
michael@0 | 34 | , mStream(nullptr) |
michael@0 | 35 | { |
michael@0 | 36 | MOZ_COUNT_CTOR(CopyableCanvasLayer); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | CopyableCanvasLayer::~CopyableCanvasLayer() |
michael@0 | 40 | { |
michael@0 | 41 | MOZ_COUNT_DTOR(CopyableCanvasLayer); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | void |
michael@0 | 45 | CopyableCanvasLayer::Initialize(const Data& aData) |
michael@0 | 46 | { |
michael@0 | 47 | NS_ASSERTION(mSurface == nullptr, "BasicCanvasLayer::Initialize called twice!"); |
michael@0 | 48 | |
michael@0 | 49 | if (aData.mGLContext) { |
michael@0 | 50 | mGLContext = aData.mGLContext; |
michael@0 | 51 | mStream = aData.mStream; |
michael@0 | 52 | mIsGLAlphaPremult = aData.mIsGLAlphaPremult; |
michael@0 | 53 | mNeedsYFlip = true; |
michael@0 | 54 | MOZ_ASSERT(mGLContext->IsOffscreen(), "canvas gl context isn't offscreen"); |
michael@0 | 55 | |
michael@0 | 56 | // [Basic Layers, non-OMTC] WebGL layer init. |
michael@0 | 57 | // `GLScreenBuffer::Morph`ing is only needed in BasicShadowableCanvasLayer. |
michael@0 | 58 | } else if (aData.mDrawTarget) { |
michael@0 | 59 | mDrawTarget = aData.mDrawTarget; |
michael@0 | 60 | mSurface = mDrawTarget->Snapshot(); |
michael@0 | 61 | mNeedsYFlip = false; |
michael@0 | 62 | } else { |
michael@0 | 63 | NS_ERROR("CanvasLayer created without mSurface, mDrawTarget or mGLContext?"); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | mBounds.SetRect(0, 0, aData.mSize.width, aData.mSize.height); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | bool |
michael@0 | 70 | CopyableCanvasLayer::IsDataValid(const Data& aData) |
michael@0 | 71 | { |
michael@0 | 72 | return mGLContext == aData.mGLContext && mStream == aData.mStream; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | void |
michael@0 | 76 | CopyableCanvasLayer::UpdateTarget(DrawTarget* aDestTarget) |
michael@0 | 77 | { |
michael@0 | 78 | if (!IsDirty()) |
michael@0 | 79 | return; |
michael@0 | 80 | Painted(); |
michael@0 | 81 | |
michael@0 | 82 | if (mDrawTarget) { |
michael@0 | 83 | mDrawTarget->Flush(); |
michael@0 | 84 | mSurface = mDrawTarget->Snapshot(); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | if (!mGLContext && aDestTarget) { |
michael@0 | 88 | NS_ASSERTION(mSurface, "Must have surface to draw!"); |
michael@0 | 89 | if (mSurface) { |
michael@0 | 90 | aDestTarget->CopySurface(mSurface, |
michael@0 | 91 | IntRect(0, 0, mBounds.width, mBounds.height), |
michael@0 | 92 | IntPoint(0, 0)); |
michael@0 | 93 | } |
michael@0 | 94 | return; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | if (mGLContext) { |
michael@0 | 98 | RefPtr<DataSourceSurface> readSurf; |
michael@0 | 99 | RefPtr<SourceSurface> resultSurf; |
michael@0 | 100 | |
michael@0 | 101 | SharedSurface_GL* sharedSurf = nullptr; |
michael@0 | 102 | if (mStream) { |
michael@0 | 103 | sharedSurf = SharedSurface_GL::Cast(mStream->SwapConsumer()); |
michael@0 | 104 | } else { |
michael@0 | 105 | sharedSurf = mGLContext->RequestFrame(); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | if (!sharedSurf) { |
michael@0 | 109 | NS_WARNING("Null frame received."); |
michael@0 | 110 | return; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | IntSize readSize(sharedSurf->Size()); |
michael@0 | 114 | SurfaceFormat format = (GetContentFlags() & CONTENT_OPAQUE) |
michael@0 | 115 | ? SurfaceFormat::B8G8R8X8 |
michael@0 | 116 | : SurfaceFormat::B8G8R8A8; |
michael@0 | 117 | |
michael@0 | 118 | if (aDestTarget) { |
michael@0 | 119 | resultSurf = aDestTarget->Snapshot(); |
michael@0 | 120 | if (!resultSurf) { |
michael@0 | 121 | resultSurf = GetTempSurface(readSize, format); |
michael@0 | 122 | } |
michael@0 | 123 | } else { |
michael@0 | 124 | resultSurf = GetTempSurface(readSize, format); |
michael@0 | 125 | } |
michael@0 | 126 | MOZ_ASSERT(resultSurf); |
michael@0 | 127 | MOZ_ASSERT(sharedSurf->APIType() == APITypeT::OpenGL); |
michael@0 | 128 | SharedSurface_GL* surfGL = SharedSurface_GL::Cast(sharedSurf); |
michael@0 | 129 | |
michael@0 | 130 | if (surfGL->Type() == SharedSurfaceType::Basic) { |
michael@0 | 131 | // sharedSurf_Basic->mData must outlive readSurf. Alas, readSurf may not |
michael@0 | 132 | // leave the scope it was declared in. |
michael@0 | 133 | SharedSurface_Basic* sharedSurf_Basic = SharedSurface_Basic::Cast(surfGL); |
michael@0 | 134 | readSurf = sharedSurf_Basic->GetData(); |
michael@0 | 135 | } else { |
michael@0 | 136 | if (resultSurf->GetSize() != readSize || |
michael@0 | 137 | !(readSurf = resultSurf->GetDataSurface()) || |
michael@0 | 138 | readSurf->GetFormat() != format) |
michael@0 | 139 | { |
michael@0 | 140 | readSurf = GetTempSurface(readSize, format); |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | // Readback handles Flush/MarkDirty. |
michael@0 | 144 | mGLContext->Screen()->Readback(surfGL, readSurf); |
michael@0 | 145 | } |
michael@0 | 146 | MOZ_ASSERT(readSurf); |
michael@0 | 147 | |
michael@0 | 148 | bool needsPremult = surfGL->HasAlpha() && !mIsGLAlphaPremult; |
michael@0 | 149 | if (needsPremult) { |
michael@0 | 150 | PremultiplySurface(readSurf); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | if (readSurf != resultSurf) { |
michael@0 | 154 | RefPtr<DataSourceSurface> resultDataSurface = |
michael@0 | 155 | resultSurf->GetDataSurface(); |
michael@0 | 156 | RefPtr<DrawTarget> dt = |
michael@0 | 157 | Factory::CreateDrawTargetForData(BackendType::CAIRO, |
michael@0 | 158 | resultDataSurface->GetData(), |
michael@0 | 159 | resultDataSurface->GetSize(), |
michael@0 | 160 | resultDataSurface->Stride(), |
michael@0 | 161 | resultDataSurface->GetFormat()); |
michael@0 | 162 | IntSize readSize = readSurf->GetSize(); |
michael@0 | 163 | dt->CopySurface(readSurf, |
michael@0 | 164 | IntRect(0, 0, readSize.width, readSize.height), |
michael@0 | 165 | IntPoint(0, 0)); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | // If !aDestSurface then we will end up painting using mSurface, so |
michael@0 | 169 | // stick our surface into mSurface, so that the Paint() path is the same. |
michael@0 | 170 | if (!aDestTarget) { |
michael@0 | 171 | mSurface = resultSurf; |
michael@0 | 172 | } |
michael@0 | 173 | } |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | DataSourceSurface* |
michael@0 | 177 | CopyableCanvasLayer::GetTempSurface(const IntSize& aSize, |
michael@0 | 178 | const SurfaceFormat aFormat) |
michael@0 | 179 | { |
michael@0 | 180 | if (!mCachedTempSurface || |
michael@0 | 181 | aSize.width != mCachedSize.width || |
michael@0 | 182 | aSize.height != mCachedSize.height || |
michael@0 | 183 | aFormat != mCachedFormat) |
michael@0 | 184 | { |
michael@0 | 185 | mCachedTempSurface = Factory::CreateDataSourceSurface(aSize, aFormat); |
michael@0 | 186 | mCachedSize = aSize; |
michael@0 | 187 | mCachedFormat = aFormat; |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | return mCachedTempSurface; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | void |
michael@0 | 194 | CopyableCanvasLayer::DiscardTempSurface() |
michael@0 | 195 | { |
michael@0 | 196 | mCachedTempSurface = nullptr; |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | } |
michael@0 | 200 | } |