|
1 /* -*- Mode: C++; tab-width: 2; 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 "ClientCanvasLayer.h" |
|
7 #include "GLContext.h" // for GLContext |
|
8 #include "GLScreenBuffer.h" // for GLScreenBuffer |
|
9 #include "GeckoProfiler.h" // for PROFILER_LABEL |
|
10 #include "SharedSurfaceEGL.h" // for SurfaceFactory_EGLImage |
|
11 #include "SharedSurfaceGL.h" // for SurfaceFactory_GLTexture, etc |
|
12 #include "SurfaceStream.h" // for SurfaceStream, etc |
|
13 #include "SurfaceTypes.h" // for SurfaceStreamType |
|
14 #include "ClientLayerManager.h" // for ClientLayerManager, etc |
|
15 #include "mozilla/gfx/Point.h" // for IntSize |
|
16 #include "mozilla/layers/CompositorTypes.h" |
|
17 #include "mozilla/layers/LayersTypes.h" |
|
18 #include "nsCOMPtr.h" // for already_AddRefed |
|
19 #include "nsISupportsImpl.h" // for Layer::AddRef, etc |
|
20 #include "nsRect.h" // for nsIntRect |
|
21 #include "nsXULAppAPI.h" // for XRE_GetProcessType, etc |
|
22 #ifdef MOZ_WIDGET_GONK |
|
23 #include "SharedSurfaceGralloc.h" |
|
24 #endif |
|
25 #ifdef XP_MACOSX |
|
26 #include "SharedSurfaceIO.h" |
|
27 #endif |
|
28 #include "gfxPrefs.h" // for WebGLForceLayersReadback |
|
29 |
|
30 using namespace mozilla::gfx; |
|
31 using namespace mozilla::gl; |
|
32 |
|
33 namespace mozilla { |
|
34 namespace layers { |
|
35 |
|
36 ClientCanvasLayer::~ClientCanvasLayer() |
|
37 { |
|
38 MOZ_COUNT_DTOR(ClientCanvasLayer); |
|
39 if (mCanvasClient) { |
|
40 mCanvasClient->OnDetach(); |
|
41 mCanvasClient = nullptr; |
|
42 } |
|
43 if (mTextureSurface) { |
|
44 delete mTextureSurface; |
|
45 } |
|
46 } |
|
47 |
|
48 void |
|
49 ClientCanvasLayer::Initialize(const Data& aData) |
|
50 { |
|
51 CopyableCanvasLayer::Initialize(aData); |
|
52 |
|
53 mCanvasClient = nullptr; |
|
54 |
|
55 if (mGLContext) { |
|
56 GLScreenBuffer* screen = mGLContext->Screen(); |
|
57 |
|
58 SurfaceCaps caps = screen->Caps(); |
|
59 if (mStream) { |
|
60 // The screen caps are irrelevant if we're using a separate stream |
|
61 caps = GetContentFlags() & CONTENT_OPAQUE ? SurfaceCaps::ForRGB() : SurfaceCaps::ForRGBA(); |
|
62 } |
|
63 |
|
64 SurfaceStreamType streamType = |
|
65 SurfaceStream::ChooseGLStreamType(SurfaceStream::OffMainThread, |
|
66 screen->PreserveBuffer()); |
|
67 SurfaceFactory_GL* factory = nullptr; |
|
68 if (!gfxPrefs::WebGLForceLayersReadback()) { |
|
69 if (ClientManager()->AsShadowForwarder()->GetCompositorBackendType() == mozilla::layers::LayersBackend::LAYERS_OPENGL) { |
|
70 if (mGLContext->GetContextType() == GLContextType::EGL) { |
|
71 bool isCrossProcess = !(XRE_GetProcessType() == GeckoProcessType_Default); |
|
72 |
|
73 if (!isCrossProcess) { |
|
74 // [Basic/OGL Layers, OMTC] WebGL layer init. |
|
75 factory = SurfaceFactory_EGLImage::Create(mGLContext, caps); |
|
76 } else { |
|
77 // [Basic/OGL Layers, OOPC] WebGL layer init. (Out Of Process Compositing) |
|
78 #ifdef MOZ_WIDGET_GONK |
|
79 factory = new SurfaceFactory_Gralloc(mGLContext, caps, ClientManager()->AsShadowForwarder()); |
|
80 #else |
|
81 // we could do readback here maybe |
|
82 NS_NOTREACHED("isCrossProcess but not on native B2G!"); |
|
83 #endif |
|
84 } |
|
85 } else { |
|
86 // [Basic Layers, OMTC] WebGL layer init. |
|
87 // Well, this *should* work... |
|
88 #ifdef XP_MACOSX |
|
89 factory = new SurfaceFactory_IOSurface(mGLContext, caps); |
|
90 #else |
|
91 factory = new SurfaceFactory_GLTexture(mGLContext, nullptr, caps); |
|
92 #endif |
|
93 } |
|
94 } |
|
95 } |
|
96 |
|
97 if (mStream) { |
|
98 // We're using a stream other than the one in the default screen |
|
99 mFactory = factory; |
|
100 if (!mFactory) { |
|
101 // Absolutely must have a factory here, so create a basic one |
|
102 mFactory = new SurfaceFactory_Basic(mGLContext, caps); |
|
103 } |
|
104 |
|
105 gfx::IntSize size = gfx::IntSize(aData.mSize.width, aData.mSize.height); |
|
106 mTextureSurface = SharedSurface_GLTexture::Create(mGLContext, mGLContext, |
|
107 mGLContext->GetGLFormats(), |
|
108 size, caps.alpha, aData.mTexID); |
|
109 SharedSurface* producer = mStream->SwapProducer(mFactory, size); |
|
110 if (!producer) { |
|
111 // Fallback to basic factory |
|
112 delete mFactory; |
|
113 mFactory = new SurfaceFactory_Basic(mGLContext, caps); |
|
114 producer = mStream->SwapProducer(mFactory, size); |
|
115 MOZ_ASSERT(producer, "Failed to create initial canvas surface with basic factory"); |
|
116 } |
|
117 } else if (factory) { |
|
118 screen->Morph(factory, streamType); |
|
119 } |
|
120 } |
|
121 } |
|
122 |
|
123 void |
|
124 ClientCanvasLayer::RenderLayer() |
|
125 { |
|
126 PROFILER_LABEL("ClientCanvasLayer", "Paint"); |
|
127 if (!IsDirty()) { |
|
128 return; |
|
129 } |
|
130 |
|
131 if (GetMaskLayer()) { |
|
132 ToClientLayer(GetMaskLayer())->RenderLayer(); |
|
133 } |
|
134 |
|
135 if (!mCanvasClient) { |
|
136 TextureFlags flags = TEXTURE_IMMEDIATE_UPLOAD; |
|
137 if (mNeedsYFlip) { |
|
138 flags |= TEXTURE_NEEDS_Y_FLIP; |
|
139 } |
|
140 |
|
141 if (!mGLContext) { |
|
142 // We don't support locking for buffer surfaces currently |
|
143 flags |= TEXTURE_IMMEDIATE_UPLOAD; |
|
144 } else { |
|
145 // GLContext's SurfaceStream handles ownership itself, |
|
146 // and doesn't require layers to do any deallocation. |
|
147 flags |= TEXTURE_DEALLOCATE_CLIENT; |
|
148 } |
|
149 mCanvasClient = CanvasClient::CreateCanvasClient(GetCanvasClientType(), |
|
150 ClientManager()->AsShadowForwarder(), flags); |
|
151 if (!mCanvasClient) { |
|
152 return; |
|
153 } |
|
154 if (HasShadow()) { |
|
155 mCanvasClient->Connect(); |
|
156 ClientManager()->AsShadowForwarder()->Attach(mCanvasClient, this); |
|
157 } |
|
158 } |
|
159 |
|
160 FirePreTransactionCallback(); |
|
161 mCanvasClient->Update(gfx::IntSize(mBounds.width, mBounds.height), this); |
|
162 |
|
163 FireDidTransactionCallback(); |
|
164 |
|
165 ClientManager()->Hold(this); |
|
166 mCanvasClient->Updated(); |
|
167 mCanvasClient->OnTransaction(); |
|
168 } |
|
169 |
|
170 already_AddRefed<CanvasLayer> |
|
171 ClientLayerManager::CreateCanvasLayer() |
|
172 { |
|
173 NS_ASSERTION(InConstruction(), "Only allowed in construction phase"); |
|
174 nsRefPtr<ClientCanvasLayer> layer = |
|
175 new ClientCanvasLayer(this); |
|
176 CREATE_SHADOW(Canvas); |
|
177 return layer.forget(); |
|
178 } |
|
179 |
|
180 } |
|
181 } |