Bug 1744524: part 2) Add `WindowContext::GetUserGestureStart` and remove `WindowConte...
[gecko.git] / gfx / layers / GLImages.cpp
blob64b2988a2c39c83d2d565c56a4925d45d3ddffb1
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/. */
7 #include "GLImages.h"
8 #include "GLContext.h"
9 #include "GLContextProvider.h"
10 #include "ScopedGLHelpers.h"
11 #include "GLImages.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;
20 namespace mozilla {
21 namespace layers {
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");
33 return nullptr;
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,
47 scopedTex.Texture());
48 if (!autoFBForTex.IsComplete()) {
49 gfxCriticalError()
50 << "GetAsSourceSurface: ScopedFramebufferForTexture failed.";
51 return nullptr;
54 const gl::OriginPos destOrigin = gl::OriginPos::TopLeft;
56 const ScopedBindFramebuffer bindFB(sSnapshotContext, autoFBForTex.FB());
57 if (!sSnapshotContext->BlitHelper()->BlitImageToFramebuffer(this, size,
58 destOrigin)) {
59 return nullptr;
63 RefPtr<gfx::DataSourceSurface> source =
64 gfx::Factory::CreateDataSourceSurface(size, gfx::SurfaceFormat::B8G8R8A8);
65 if (NS_WARN_IF(!source)) {
66 return nullptr;
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,
77 bool aContinuous,
78 gl::OriginPos aOriginPos,
79 bool aHasAlpha /* = true */)
80 : GLImage(ImageFormat::SURFACE_TEXTURE),
81 mHandle(aHandle),
82 mSize(aSize),
83 mContinuous(aContinuous),
84 mOriginPos(aOriginPos),
85 mHasAlpha(aHasAlpha) {
86 MOZ_ASSERT(mHandle);
89 Maybe<SurfaceDescriptor> SurfaceTextureImage::GetDesc() {
90 SurfaceDescriptor sd = SurfaceTextureDescriptor(
91 mHandle, mSize,
92 mHasAlpha ? gfx::SurfaceFormat::R8G8B8A8 : gfx::SurfaceFormat::R8G8B8X8,
93 false /* NOT continuous */, false /* do not ignore transform */);
94 return Some(sd);
96 #endif
98 } // namespace layers
99 } // namespace mozilla