Bumping manifests a=b2g-bump
[gecko.git] / gfx / layers / TextureDIB.cpp
blob80d3420f188253d937c1eb702e49152668516edf
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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 "TextureDIB.h"
7 #include "gfx2DGlue.h"
9 namespace mozilla {
11 using namespace gfx;
13 namespace layers {
15 DIBTextureClient::DIBTextureClient(gfx::SurfaceFormat aFormat, TextureFlags aFlags)
16 : TextureClient(aFlags)
17 , mFormat(aFormat)
18 , mIsLocked(false)
20 MOZ_COUNT_CTOR(DIBTextureClient);
23 DIBTextureClient::~DIBTextureClient()
25 MOZ_COUNT_DTOR(DIBTextureClient);
28 TemporaryRef<TextureClient>
29 DIBTextureClient::CreateSimilar(TextureFlags aFlags,
30 TextureAllocationFlags aAllocFlags) const
32 RefPtr<TextureClient> tex = new DIBTextureClient(mFormat, mFlags | aFlags);
34 if (!tex->AllocateForSurface(mSize, aAllocFlags)) {
35 return nullptr;
38 return tex;
41 bool
42 DIBTextureClient::Lock(OpenMode)
44 MOZ_ASSERT(!mIsLocked);
45 if (!IsValid()) {
46 return false;
48 mIsLocked = true;
49 return true;
52 void
53 DIBTextureClient::Unlock()
55 MOZ_ASSERT(mIsLocked, "Unlocked called while the texture is not locked!");
56 if (mDrawTarget) {
57 if (mReadbackSink) {
58 RefPtr<SourceSurface> snapshot = mDrawTarget->Snapshot();
59 RefPtr<DataSourceSurface> dataSurf = snapshot->GetDataSurface();
60 mReadbackSink->ProcessReadback(dataSurf);
63 mDrawTarget->Flush();
64 mDrawTarget = nullptr;
67 mIsLocked = false;
70 bool
71 DIBTextureClient::ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor)
73 MOZ_ASSERT(IsValid());
74 if (!IsAllocated()) {
75 return false;
77 MOZ_ASSERT(mSurface);
78 // The host will release this ref when it receives the surface descriptor.
79 // We AddRef in case we die before the host receives the pointer.
80 aOutDescriptor = SurfaceDescriptorDIB(reinterpret_cast<uintptr_t>(mSurface.get()));
81 mSurface->AddRef();
82 return true;
85 gfx::DrawTarget*
86 DIBTextureClient::BorrowDrawTarget()
88 MOZ_ASSERT(mIsLocked && IsAllocated());
90 if (!mDrawTarget) {
91 mDrawTarget =
92 gfxPlatform::GetPlatform()->CreateDrawTargetForSurface(mSurface, mSize);
95 return mDrawTarget;
98 bool
99 DIBTextureClient::AllocateForSurface(gfx::IntSize aSize, TextureAllocationFlags aFlags)
101 MOZ_ASSERT(!IsAllocated());
102 mSize = aSize;
104 mSurface = new gfxWindowsSurface(gfxIntSize(aSize.width, aSize.height),
105 SurfaceFormatToImageFormat(mFormat));
106 if (!mSurface || mSurface->CairoStatus())
108 NS_WARNING("Could not create surface");
109 mSurface = nullptr;
110 return false;
113 return true;
116 DIBTextureHost::DIBTextureHost(TextureFlags aFlags,
117 const SurfaceDescriptorDIB& aDescriptor)
118 : TextureHost(aFlags)
119 , mIsLocked(false)
121 // We added an extra ref for transport, so we shouldn't AddRef now.
122 mSurface =
123 dont_AddRef(reinterpret_cast<gfxWindowsSurface*>(aDescriptor.surface()));
124 MOZ_ASSERT(mSurface);
126 mSize = ToIntSize(mSurface->GetSize());
127 mFormat = ImageFormatToSurfaceFormat(
128 gfxPlatform::GetPlatform()->OptimalFormatForContent(mSurface->GetContentType()));
131 NewTextureSource*
132 DIBTextureHost::GetTextureSources()
134 if (!mTextureSource) {
135 Updated();
138 return mTextureSource;
141 void
142 DIBTextureHost::Updated(const nsIntRegion* aRegion)
144 if (!mTextureSource) {
145 mTextureSource = mCompositor->CreateDataTextureSource(mFlags);
148 nsRefPtr<gfxImageSurface> imgSurf = mSurface->GetAsImageSurface();
150 RefPtr<DataSourceSurface> surf = Factory::CreateWrappingDataSourceSurface(imgSurf->Data(), imgSurf->Stride(), mSize, mFormat);
152 if (!mTextureSource->Update(surf, const_cast<nsIntRegion*>(aRegion))) {
153 mTextureSource = nullptr;
157 bool
158 DIBTextureHost::Lock()
160 MOZ_ASSERT(!mIsLocked);
161 mIsLocked = true;
162 return true;
165 void
166 DIBTextureHost::Unlock()
168 MOZ_ASSERT(mIsLocked);
169 mIsLocked = false;
172 void
173 DIBTextureHost::SetCompositor(Compositor* aCompositor)
175 mCompositor = aCompositor;
178 void
179 DIBTextureHost::DeallocateDeviceData()
181 if (mTextureSource) {
182 mTextureSource->DeallocateDeviceData();