Bumping manifests a=b2g-bump
[gecko.git] / gfx / layers / GLImages.cpp
blob66fa218df96112b3829d92f41f1c7f2693787145
2 #include "GLImages.h"
3 #include "GLContext.h"
4 #include "GLContextProvider.h"
5 #include "ScopedGLHelpers.h"
6 #include "GLImages.h"
7 #include "GLBlitHelper.h"
8 #include "GLReadTexImageHelper.h"
9 #include "GLLibraryEGL.h"
11 using namespace mozilla;
12 using namespace mozilla::gl;
14 namespace mozilla {
15 namespace layers {
17 static nsRefPtr<GLContext> sSnapshotContext;
19 EGLImageImage::~EGLImageImage()
21 if (!mData.mOwns) {
22 return;
25 if (mData.mImage) {
26 sEGLLibrary.fDestroyImage(EGL_DISPLAY(), mData.mImage);
27 mData.mImage = nullptr;
30 if (mData.mSync) {
31 sEGLLibrary.fDestroySync(EGL_DISPLAY(), mData.mSync);
32 mData.mSync = nullptr;
36 TemporaryRef<gfx::SourceSurface>
37 GLImage::GetAsSourceSurface()
39 MOZ_ASSERT(NS_IsMainThread(), "Should be on the main thread");
41 if (!sSnapshotContext) {
42 sSnapshotContext = GLContextProvider::CreateHeadless();
43 if (!sSnapshotContext) {
44 NS_WARNING("Failed to create snapshot GLContext");
45 return nullptr;
49 sSnapshotContext->MakeCurrent();
50 ScopedTexture scopedTex(sSnapshotContext);
51 ScopedBindTexture boundTex(sSnapshotContext, scopedTex.Texture());
53 gfx::IntSize size = GetSize();
54 sSnapshotContext->fTexImage2D(LOCAL_GL_TEXTURE_2D, 0, LOCAL_GL_RGBA,
55 size.width, size.height, 0,
56 LOCAL_GL_RGBA,
57 LOCAL_GL_UNSIGNED_BYTE,
58 nullptr);
60 ScopedFramebufferForTexture fb(sSnapshotContext, scopedTex.Texture());
62 GLBlitHelper helper(sSnapshotContext);
64 if (!helper.BlitImageToFramebuffer(this, size, fb.FB(), true)) {
65 return nullptr;
68 RefPtr<gfx::DataSourceSurface> source =
69 gfx::Factory::CreateDataSourceSurface(size, gfx::SurfaceFormat::B8G8R8A8);
70 if (NS_WARN_IF(!source)) {
71 return nullptr;
74 ScopedBindFramebuffer bind(sSnapshotContext, fb.FB());
75 ReadPixelsIntoDataSurface(sSnapshotContext, source);
76 return source.forget();
79 } // layers
80 } // mozilla