/src/mozilla-central/gfx/layers/CopyableCanvasRenderer.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | , mGLFrontbuffer(nullptr) |
40 | | , mAsyncRenderer(nullptr) |
41 | | , mIsAlphaPremultiplied(true) |
42 | | , mOriginPos(gl::OriginPos::TopLeft) |
43 | | , mOpaque(true) |
44 | | , mCachedTempSurface(nullptr) |
45 | 0 | { |
46 | 0 | MOZ_COUNT_CTOR(CopyableCanvasRenderer); |
47 | 0 | } |
48 | | |
49 | | CopyableCanvasRenderer::~CopyableCanvasRenderer() |
50 | 0 | { |
51 | 0 | Destroy(); |
52 | 0 | MOZ_COUNT_DTOR(CopyableCanvasRenderer); |
53 | 0 | } |
54 | | |
55 | | void |
56 | | CopyableCanvasRenderer::Initialize(const CanvasInitializeData& aData) |
57 | 0 | { |
58 | 0 | CanvasRenderer::Initialize(aData); |
59 | 0 |
|
60 | 0 | if (aData.mGLContext) { |
61 | 0 | mGLContext = aData.mGLContext; |
62 | 0 | mIsAlphaPremultiplied = aData.mIsGLAlphaPremult; |
63 | 0 | mOriginPos = gl::OriginPos::BottomLeft; |
64 | 0 |
|
65 | 0 | MOZ_ASSERT(mGLContext->IsOffscreen(), "canvas gl context isn't offscreen"); |
66 | 0 |
|
67 | 0 | if (aData.mFrontbufferGLTex) { |
68 | 0 | gfx::IntSize size(aData.mSize.width, aData.mSize.height); |
69 | 0 | mGLFrontbuffer = SharedSurface_Basic::Wrap(aData.mGLContext, size, aData.mHasAlpha, |
70 | 0 | aData.mFrontbufferGLTex); |
71 | 0 | mBufferProvider = aData.mBufferProvider; |
72 | 0 | } |
73 | 0 | } else if (aData.mBufferProvider) { |
74 | 0 | mBufferProvider = aData.mBufferProvider; |
75 | 0 | } else if (aData.mRenderer) { |
76 | 0 | mAsyncRenderer = aData.mRenderer; |
77 | 0 | mOriginPos = gl::OriginPos::BottomLeft; |
78 | 0 | } else { |
79 | 0 | MOZ_CRASH("GFX: CanvasRenderer created without BufferProvider, DrawTarget or GLContext?"); |
80 | 0 | } |
81 | 0 |
|
82 | 0 | mOpaque = !aData.mHasAlpha; |
83 | 0 | } |
84 | | |
85 | | bool |
86 | | CopyableCanvasRenderer::IsDataValid(const CanvasInitializeData& aData) |
87 | 0 | { |
88 | 0 | return mGLContext == aData.mGLContext && mBufferProvider == aData.mBufferProvider; |
89 | 0 | } |
90 | | |
91 | | void |
92 | | CopyableCanvasRenderer::ClearCachedResources() |
93 | 0 | { |
94 | 0 | SetDirty(); |
95 | 0 |
|
96 | 0 | if (mBufferProvider) { |
97 | 0 | mBufferProvider->ClearCachedResources(); |
98 | 0 | } |
99 | 0 |
|
100 | 0 | mCachedTempSurface = nullptr; |
101 | 0 | } |
102 | | |
103 | | void |
104 | | CopyableCanvasRenderer::Destroy() |
105 | 0 | { |
106 | 0 | if (mBufferProvider) { |
107 | 0 | mBufferProvider->ClearCachedResources(); |
108 | 0 | } |
109 | 0 |
|
110 | 0 | mBufferProvider = nullptr; |
111 | 0 | mCachedTempSurface = nullptr; |
112 | 0 | } |
113 | | |
114 | | already_AddRefed<SourceSurface> |
115 | | CopyableCanvasRenderer::ReadbackSurface() |
116 | 0 | { |
117 | 0 | struct ScopedFireTransactionCallback { |
118 | 0 | explicit ScopedFireTransactionCallback(CopyableCanvasRenderer* aRenderer) |
119 | 0 | : mRenderer(aRenderer) |
120 | 0 | { |
121 | 0 | mRenderer->FirePreTransactionCallback(); |
122 | 0 | } |
123 | 0 |
|
124 | 0 | ~ScopedFireTransactionCallback() |
125 | 0 | { |
126 | 0 | mRenderer->FireDidTransactionCallback(); |
127 | 0 | } |
128 | 0 |
|
129 | 0 | CopyableCanvasRenderer* mRenderer; |
130 | 0 | }; |
131 | 0 |
|
132 | 0 | ScopedFireTransactionCallback callback(this); |
133 | 0 | if (mAsyncRenderer) { |
134 | 0 | MOZ_ASSERT(!mBufferProvider); |
135 | 0 | MOZ_ASSERT(!mGLContext); |
136 | 0 | return mAsyncRenderer->GetSurface(); |
137 | 0 | } |
138 | 0 |
|
139 | 0 | if (!mGLContext) { |
140 | 0 | return nullptr; |
141 | 0 | } |
142 | 0 | |
143 | 0 | SharedSurface* frontbuffer = nullptr; |
144 | 0 | if (mGLFrontbuffer) { |
145 | 0 | frontbuffer = mGLFrontbuffer.get(); |
146 | 0 | } else if (mGLContext->Screen()) { |
147 | 0 | const auto& front = mGLContext->Screen()->Front(); |
148 | 0 | if (front) { |
149 | 0 | frontbuffer = front->Surf(); |
150 | 0 | } |
151 | 0 | } |
152 | 0 |
|
153 | 0 | if (!frontbuffer) { |
154 | 0 | NS_WARNING("Null frame received."); |
155 | 0 | return nullptr; |
156 | 0 | } |
157 | 0 |
|
158 | 0 | IntSize readSize(frontbuffer->mSize); |
159 | 0 | SurfaceFormat format = frontbuffer->mHasAlpha ? SurfaceFormat::B8G8R8A8 |
160 | 0 | : SurfaceFormat::B8G8R8X8; |
161 | 0 | bool needsPremult = frontbuffer->mHasAlpha && !mIsAlphaPremultiplied; |
162 | 0 |
|
163 | 0 | RefPtr<DataSourceSurface> resultSurf = GetTempSurface(readSize, format); |
164 | 0 | // There will already be a warning from inside of GetTempSurface, but |
165 | 0 | // it doesn't hurt to complain: |
166 | 0 | if (NS_WARN_IF(!resultSurf)) { |
167 | 0 | return nullptr; |
168 | 0 | } |
169 | 0 | |
170 | 0 | // Readback handles Flush/MarkDirty. |
171 | 0 | if (!mGLContext->Readback(frontbuffer, resultSurf)) { |
172 | 0 | NS_WARNING("Failed to read back canvas surface."); |
173 | 0 | return nullptr; |
174 | 0 | } |
175 | 0 | if (needsPremult) { |
176 | 0 | gfxUtils::PremultiplyDataSurface(resultSurf, resultSurf); |
177 | 0 | } |
178 | 0 | MOZ_ASSERT(resultSurf); |
179 | 0 |
|
180 | 0 | return resultSurf.forget(); |
181 | 0 | } |
182 | | |
183 | | DataSourceSurface* |
184 | | CopyableCanvasRenderer::GetTempSurface(const IntSize& aSize, |
185 | | const SurfaceFormat aFormat) |
186 | 0 | { |
187 | 0 | if (!mCachedTempSurface || |
188 | 0 | aSize != mCachedTempSurface->GetSize() || |
189 | 0 | aFormat != mCachedTempSurface->GetFormat()) |
190 | 0 | { |
191 | 0 | // Create a surface aligned to 8 bytes since that's the highest alignment WebGL can handle. |
192 | 0 | uint32_t stride = GetAlignedStride<8>(aSize.width, BytesPerPixel(aFormat)); |
193 | 0 | mCachedTempSurface = Factory::CreateDataSourceSurfaceWithStride(aSize, aFormat, stride); |
194 | 0 | } |
195 | 0 |
|
196 | 0 | return mCachedTempSurface; |
197 | 0 | } |
198 | | |
199 | | } // namespace layers |
200 | | } // namespace mozilla |