Source code

Revision control

Other Tools

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/. */
6
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
16
using namespace mozilla;
17
using namespace mozilla::gl;
18
19
namespace mozilla {
20
namespace layers {
21
22
static RefPtr<GLContext> sSnapshotContext;
23
24
already_AddRefed<gfx::SourceSurface> GLImage::GetAsSourceSurface() {
25
MOZ_ASSERT(NS_IsMainThread(), "Should be on the main thread");
26
27
if (!sSnapshotContext) {
28
nsCString discardFailureId;
29
sSnapshotContext = GLContextProvider::CreateHeadless(
30
CreateContextFlags::NONE, &discardFailureId);
31
if (!sSnapshotContext) {
32
NS_WARNING("Failed to create snapshot GLContext");
33
return nullptr;
34
}
35
}
36
37
sSnapshotContext->MakeCurrent();
38
ScopedTexture scopedTex(sSnapshotContext);
39
ScopedBindTexture boundTex(sSnapshotContext, scopedTex.Texture());
40
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);
45
46
ScopedFramebufferForTexture autoFBForTex(sSnapshotContext,
47
scopedTex.Texture());
48
if (!autoFBForTex.IsComplete()) {
49
gfxCriticalError()
50
<< "GetAsSourceSurface: ScopedFramebufferForTexture failed.";
51
return nullptr;
52
}
53
54
const gl::OriginPos destOrigin = gl::OriginPos::TopLeft;
55
{
56
const ScopedBindFramebuffer bindFB(sSnapshotContext, autoFBForTex.FB());
57
if (!sSnapshotContext->BlitHelper()->BlitImageToFramebuffer(this, size,
58
destOrigin)) {
59
return nullptr;
60
}
61
}
62
63
RefPtr<gfx::DataSourceSurface> source =
64
gfx::Factory::CreateDataSourceSurface(size, gfx::SurfaceFormat::B8G8R8A8);
65
if (NS_WARN_IF(!source)) {
66
return nullptr;
67
}
68
69
ScopedBindFramebuffer bind(sSnapshotContext, autoFBForTex.FB());
70
ReadPixelsIntoDataSurface(sSnapshotContext, source);
71
return source.forget();
72
}
73
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);
87
}
88
#endif
89
90
} // namespace layers
91
} // namespace mozilla