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 "CopyableCanvasRenderer.h"
8
9
#include "BasicLayersImpl.h" // for FillWithMask, etc
10
#include "GLContext.h" // for GLContext
11
#include "GLScreenBuffer.h" // for GLScreenBuffer
12
#include "SharedSurface.h" // for SharedSurface
13
#include "SharedSurfaceGL.h" // for SharedSurface
14
#include "gfxPattern.h" // for gfxPattern, etc
15
#include "gfxPlatform.h" // for gfxPlatform, gfxImageFormat
16
#include "gfxRect.h" // for gfxRect
17
#include "gfxUtils.h" // for gfxUtils
18
#include "gfx2DGlue.h" // for thebes --> moz2d transition
19
#include "mozilla/gfx/BaseSize.h" // for BaseSize
20
#include "mozilla/gfx/Tools.h"
21
#include "mozilla/gfx/Point.h" // for IntSize
22
#include "mozilla/layers/AsyncCanvasRenderer.h"
23
#include "mozilla/layers/PersistentBufferProvider.h"
24
#include "nsDebug.h" // for NS_ASSERTION, NS_WARNING, etc
25
#include "nsISupportsImpl.h" // for gfxContext::AddRef, etc
26
#include "nsRect.h" // for mozilla::gfx::IntRect
27
#include "gfxUtils.h"
28
#include "client/TextureClientSharedSurface.h"
29
30
namespace mozilla {
31
namespace layers {
32
33
using namespace mozilla::gfx;
34
using namespace mozilla::gl;
35
36
CopyableCanvasRenderer::CopyableCanvasRenderer()
37
: mGLContext(nullptr),
38
mBufferProvider(nullptr),
39
mAsyncRenderer(nullptr),
40
mIsAlphaPremultiplied(true),
41
mOriginPos(gl::OriginPos::TopLeft),
42
mOpaque(true),
43
mCachedTempSurface(nullptr) {
44
MOZ_COUNT_CTOR(CopyableCanvasRenderer);
45
}
46
47
CopyableCanvasRenderer::~CopyableCanvasRenderer() {
48
Destroy();
49
MOZ_COUNT_DTOR(CopyableCanvasRenderer);
50
}
51
52
void CopyableCanvasRenderer::Initialize(const CanvasInitializeData& aData) {
53
CanvasRenderer::Initialize(aData);
54
55
if (aData.mGLContext) {
56
if (aData.mGLContext->IsDestroyed()) {
57
return;
58
}
59
mGLContext = aData.mGLContext;
60
mIsAlphaPremultiplied = aData.mIsGLAlphaPremult;
61
mOriginPos = gl::OriginPos::BottomLeft;
62
63
MOZ_ASSERT(mGLContext->IsOffscreen(), "canvas gl context isn't offscreen");
64
65
} else if (aData.mBufferProvider) {
66
mBufferProvider = aData.mBufferProvider;
67
} else if (aData.mRenderer) {
68
mAsyncRenderer = aData.mRenderer;
69
mOriginPos = gl::OriginPos::BottomLeft;
70
} else if (aData.mOOPRenderer) {
71
mOOPRenderer = aData.mOOPRenderer;
72
}
73
74
mOpaque = !aData.mHasAlpha;
75
}
76
77
bool CopyableCanvasRenderer::IsDataValid(const CanvasInitializeData& aData) {
78
return mGLContext == aData.mGLContext &&
79
mBufferProvider == aData.mBufferProvider &&
80
mOOPRenderer == aData.mOOPRenderer;
81
}
82
83
void CopyableCanvasRenderer::ClearCachedResources() {
84
SetDirty();
85
86
if (mBufferProvider) {
87
mBufferProvider->ClearCachedResources();
88
}
89
90
mCachedTempSurface = nullptr;
91
}
92
93
void CopyableCanvasRenderer::Destroy() {
94
if (mBufferProvider) {
95
mBufferProvider->ClearCachedResources();
96
}
97
98
mBufferProvider = nullptr;
99
mCachedTempSurface = nullptr;
100
}
101
102
already_AddRefed<SourceSurface> CopyableCanvasRenderer::ReadbackSurface() {
103
struct ScopedFireTransactionCallback {
104
explicit ScopedFireTransactionCallback(CopyableCanvasRenderer* aRenderer)
105
: mRenderer(aRenderer) {
106
mRenderer->FirePreTransactionCallback();
107
}
108
109
~ScopedFireTransactionCallback() {
110
mRenderer->FireDidTransactionCallback();
111
}
112
113
CopyableCanvasRenderer* mRenderer;
114
};
115
116
ScopedFireTransactionCallback callback(this);
117
if (mAsyncRenderer) {
118
MOZ_ASSERT(!mBufferProvider);
119
MOZ_ASSERT(!mGLContext);
120
return mAsyncRenderer->GetSurface();
121
}
122
123
if (!mGLContext) {
124
return nullptr;
125
}
126
127
SharedSurface* frontbuffer = nullptr;
128
if (mGLContext->Screen()) {
129
const auto& front = mGLContext->Screen()->Front();
130
if (front) {
131
frontbuffer = front->Surf();
132
}
133
}
134
135
if (!frontbuffer) {
136
NS_WARNING("Null frame received.");
137
return nullptr;
138
}
139
140
IntSize readSize(frontbuffer->mSize);
141
SurfaceFormat format = frontbuffer->mHasAlpha ? SurfaceFormat::B8G8R8A8
142
: SurfaceFormat::B8G8R8X8;
143
bool needsPremult = frontbuffer->mHasAlpha && !mIsAlphaPremultiplied;
144
145
RefPtr<DataSourceSurface> resultSurf = GetTempSurface(readSize, format);
146
// There will already be a warning from inside of GetTempSurface, but
147
// it doesn't hurt to complain:
148
if (NS_WARN_IF(!resultSurf)) {
149
return nullptr;
150
}
151
152
// Readback handles Flush/MarkDirty.
153
if (!mGLContext->Readback(frontbuffer, resultSurf)) {
154
NS_WARNING("Failed to read back canvas surface.");
155
return nullptr;
156
}
157
if (needsPremult) {
158
gfxUtils::PremultiplyDataSurface(resultSurf, resultSurf);
159
}
160
MOZ_ASSERT(resultSurf);
161
162
return resultSurf.forget();
163
}
164
165
DataSourceSurface* CopyableCanvasRenderer::GetTempSurface(
166
const IntSize& aSize, const SurfaceFormat aFormat) {
167
MOZ_ASSERT(!mOOPRenderer);
168
169
if (!mCachedTempSurface || aSize != mCachedTempSurface->GetSize() ||
170
aFormat != mCachedTempSurface->GetFormat()) {
171
// Create a surface aligned to 8 bytes since that's the highest alignment
172
// WebGL can handle.
173
uint32_t stride = GetAlignedStride<8>(aSize.width, BytesPerPixel(aFormat));
174
mCachedTempSurface =
175
Factory::CreateDataSourceSurfaceWithStride(aSize, aFormat, stride);
176
}
177
178
return mCachedTempSurface;
179
}
180
181
} // namespace layers
182
} // namespace mozilla