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