Line data Source code
1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #include "WebRenderTextureHost.h"
7 :
8 : #include "mozilla/layers/ImageDataSerializer.h"
9 : #include "mozilla/layers/LayersSurfaces.h"
10 : #include "mozilla/webrender/RenderThread.h"
11 :
12 : namespace mozilla {
13 : namespace layers {
14 :
15 0 : WebRenderTextureHost::WebRenderTextureHost(const SurfaceDescriptor& aDesc,
16 : TextureFlags aFlags,
17 : TextureHost* aTexture,
18 0 : wr::ExternalImageId& aExternalImageId)
19 : : TextureHost(aFlags)
20 : , mExternalImageId(aExternalImageId)
21 0 : , mIsWrappingNativeHandle(false)
22 : {
23 : // The wrapped textureHost will be used in WebRender, and the WebRender could
24 : // run at another thread. It's hard to control the life-time when gecko
25 : // receives PTextureParent destroy message. It's possible that textureHost is
26 : // still used by WebRender. So, we only accept the textureHost without
27 : // DEALLOCATE_CLIENT flag here. If the buffer deallocation is controlled by
28 : // parent, we could do something to make sure the wrapped textureHost is not
29 : // used by WebRender and then release it.
30 0 : MOZ_ASSERT(!(aFlags & TextureFlags::DEALLOCATE_CLIENT));
31 :
32 0 : MOZ_COUNT_CTOR(WebRenderTextureHost);
33 0 : mWrappedTextureHost = aTexture;
34 :
35 0 : CreateRenderTextureHost(aDesc, aTexture);
36 0 : }
37 :
38 0 : WebRenderTextureHost::~WebRenderTextureHost()
39 : {
40 0 : MOZ_COUNT_DTOR(WebRenderTextureHost);
41 0 : wr::RenderThread::Get()->UnregisterExternalImage(wr::AsUint64(mExternalImageId));
42 0 : }
43 :
44 : void
45 0 : WebRenderTextureHost::CreateRenderTextureHost(const layers::SurfaceDescriptor& aDesc,
46 : TextureHost* aTexture)
47 : {
48 0 : MOZ_ASSERT(aTexture);
49 :
50 0 : switch (aDesc.type()) {
51 : case SurfaceDescriptor::TSurfaceDescriptorBuffer: {
52 0 : mIsWrappingNativeHandle = false;
53 0 : break;
54 : }
55 : #ifdef XP_MACOSX
56 : case SurfaceDescriptor::TSurfaceDescriptorMacIOSurface: {
57 : mIsWrappingNativeHandle = true;
58 : break;
59 : }
60 : #endif
61 : case SurfaceDescriptor::TSurfaceDescriptorGPUVideo: {
62 0 : mIsWrappingNativeHandle = !aTexture->HasIntermediateBuffer();
63 0 : break;
64 : }
65 : default:
66 0 : gfxCriticalError() << "No WR implement for texture type:" << aDesc.type();
67 : }
68 :
69 0 : aTexture->CreateRenderTexture(mExternalImageId);
70 0 : }
71 :
72 : bool
73 0 : WebRenderTextureHost::Lock()
74 : {
75 0 : MOZ_ASSERT_UNREACHABLE("unexpected to be called");
76 : return false;
77 : }
78 :
79 : void
80 0 : WebRenderTextureHost::Unlock()
81 : {
82 0 : MOZ_ASSERT_UNREACHABLE("unexpected to be called");
83 : return;
84 : }
85 :
86 : bool
87 0 : WebRenderTextureHost::BindTextureSource(CompositableTextureSourceRef& aTexture)
88 : {
89 0 : MOZ_ASSERT_UNREACHABLE("unexpected to be called");
90 : return false;
91 : }
92 :
93 : already_AddRefed<gfx::DataSourceSurface>
94 0 : WebRenderTextureHost::GetAsSurface()
95 : {
96 0 : if (!mWrappedTextureHost) {
97 0 : return nullptr;
98 : }
99 0 : return mWrappedTextureHost->GetAsSurface();
100 : }
101 :
102 : void
103 0 : WebRenderTextureHost::SetTextureSourceProvider(TextureSourceProvider* aProvider)
104 : {
105 0 : }
106 :
107 : YUVColorSpace
108 0 : WebRenderTextureHost::GetYUVColorSpace() const
109 : {
110 0 : if (mWrappedTextureHost) {
111 0 : return mWrappedTextureHost->GetYUVColorSpace();
112 : }
113 0 : return YUVColorSpace::UNKNOWN;
114 : }
115 :
116 : gfx::IntSize
117 0 : WebRenderTextureHost::GetSize() const
118 : {
119 0 : if (!mWrappedTextureHost) {
120 0 : return gfx::IntSize();
121 : }
122 0 : return mWrappedTextureHost->GetSize();
123 : }
124 :
125 : gfx::SurfaceFormat
126 0 : WebRenderTextureHost::GetFormat() const
127 : {
128 0 : if (!mWrappedTextureHost) {
129 0 : return gfx::SurfaceFormat::UNKNOWN;
130 : }
131 0 : return mWrappedTextureHost->GetFormat();
132 : }
133 :
134 : gfx::SurfaceFormat
135 0 : WebRenderTextureHost::GetReadFormat() const
136 : {
137 0 : if (!mWrappedTextureHost) {
138 0 : return gfx::SurfaceFormat::UNKNOWN;
139 : }
140 0 : return mWrappedTextureHost->GetReadFormat();
141 : }
142 :
143 : int32_t
144 0 : WebRenderTextureHost::GetRGBStride()
145 : {
146 0 : if (!mWrappedTextureHost) {
147 0 : return 0;
148 : }
149 0 : gfx::SurfaceFormat format = GetFormat();
150 0 : if (GetFormat() == gfx::SurfaceFormat::YUV) {
151 : // XXX this stride is used until yuv image rendering by webrender is used.
152 : // Software converted RGB buffers strides are aliened to 16
153 0 : return gfx::GetAlignedStride<16>(GetSize().width, BytesPerPixel(gfx::SurfaceFormat::B8G8R8A8));
154 : }
155 0 : return ImageDataSerializer::ComputeRGBStride(format, GetSize().width);
156 : }
157 :
158 : void
159 0 : WebRenderTextureHost::GetWRImageKeys(nsTArray<wr::ImageKey>& aImageKeys,
160 : const std::function<wr::ImageKey()>& aImageKeyAllocator)
161 : {
162 0 : MOZ_ASSERT(mWrappedTextureHost);
163 0 : MOZ_ASSERT(aImageKeys.IsEmpty());
164 :
165 0 : mWrappedTextureHost->GetWRImageKeys(aImageKeys, aImageKeyAllocator);
166 0 : }
167 :
168 : void
169 0 : WebRenderTextureHost::AddWRImage(wr::WebRenderAPI* aAPI,
170 : Range<const wr::ImageKey>& aImageKeys,
171 : const wr::ExternalImageId& aExtID)
172 : {
173 0 : MOZ_ASSERT(mWrappedTextureHost);
174 0 : MOZ_ASSERT(mExternalImageId == aExtID);
175 :
176 0 : mWrappedTextureHost->AddWRImage(aAPI, aImageKeys, aExtID);
177 0 : }
178 :
179 : void
180 0 : WebRenderTextureHost::PushExternalImage(wr::DisplayListBuilder& aBuilder,
181 : const WrRect& aBounds,
182 : const WrRect& aClip,
183 : wr::ImageRendering aFilter,
184 : Range<const wr::ImageKey>& aImageKeys)
185 : {
186 0 : MOZ_ASSERT(mWrappedTextureHost);
187 0 : MOZ_ASSERT(aImageKeys.length() > 0);
188 :
189 0 : mWrappedTextureHost->PushExternalImage(aBuilder,
190 : aBounds,
191 : aClip,
192 : aFilter,
193 0 : aImageKeys);
194 0 : }
195 :
196 : } // namespace layers
197 : } // namespace mozilla
|