1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "GLContextProvider.h"
10 #include "ScopedGLHelpers.h"
12 #include "GLBlitHelper.h"
13 #include "GLReadTexImageHelper.h"
14 #include "GLLibraryEGL.h"
15 #include "mozilla/gfx/Logging.h"
17 using namespace mozilla
;
18 using namespace mozilla::gl
;
23 static RefPtr
<GLContext
> sSnapshotContext
;
25 already_AddRefed
<gfx::SourceSurface
> GLImage::GetAsSourceSurface() {
26 MOZ_ASSERT(NS_IsMainThread(), "Should be on the main thread");
28 if (!sSnapshotContext
) {
29 nsCString discardFailureId
;
30 sSnapshotContext
= GLContextProvider::CreateHeadless({}, &discardFailureId
);
31 if (!sSnapshotContext
) {
32 NS_WARNING("Failed to create snapshot GLContext");
37 sSnapshotContext
->MakeCurrent();
38 ScopedTexture
scopedTex(sSnapshotContext
);
39 ScopedBindTexture
boundTex(sSnapshotContext
, scopedTex
.Texture());
41 gfx::IntSize size
= GetSize();
42 sSnapshotContext
->fTexImage2D(LOCAL_GL_TEXTURE_2D
, 0, LOCAL_GL_RGBA
,
43 size
.width
, size
.height
, 0, LOCAL_GL_RGBA
,
44 LOCAL_GL_UNSIGNED_BYTE
, nullptr);
46 ScopedFramebufferForTexture
autoFBForTex(sSnapshotContext
,
48 if (!autoFBForTex
.IsComplete()) {
50 << "GetAsSourceSurface: ScopedFramebufferForTexture failed.";
54 const gl::OriginPos destOrigin
= gl::OriginPos::TopLeft
;
56 const ScopedBindFramebuffer
bindFB(sSnapshotContext
, autoFBForTex
.FB());
57 if (!sSnapshotContext
->BlitHelper()->BlitImageToFramebuffer(this, size
,
63 RefPtr
<gfx::DataSourceSurface
> source
=
64 gfx::Factory::CreateDataSourceSurface(size
, gfx::SurfaceFormat::B8G8R8A8
);
65 if (NS_WARN_IF(!source
)) {
69 ScopedBindFramebuffer
bind(sSnapshotContext
, autoFBForTex
.FB());
70 ReadPixelsIntoDataSurface(sSnapshotContext
, source
);
71 return source
.forget();
74 #ifdef MOZ_WIDGET_ANDROID
75 SurfaceTextureImage::SurfaceTextureImage(AndroidSurfaceTextureHandle aHandle
,
76 const gfx::IntSize
& aSize
,
78 gl::OriginPos aOriginPos
,
79 bool aHasAlpha
/* = true */)
80 : GLImage(ImageFormat::SURFACE_TEXTURE
),
83 mContinuous(aContinuous
),
84 mOriginPos(aOriginPos
),
85 mHasAlpha(aHasAlpha
) {
89 Maybe
<SurfaceDescriptor
> SurfaceTextureImage::GetDesc() {
90 SurfaceDescriptor sd
= SurfaceTextureDescriptor(
92 mHasAlpha
? gfx::SurfaceFormat::R8G8B8A8
: gfx::SurfaceFormat::R8G8B8X8
,
93 false /* NOT continuous */, false /* do not ignore transform */);
99 } // namespace mozilla