Bug 1568876 - Make copyRule in the ChangesView fission compatible. r=rcaliman
[gecko.git] / gfx / layers / CanvasTranslator.h
blob5b00735311cfb3fbf4017c4546ceb005f09fa8a0
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 https://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_layers_CanvasTranslator_h
8 #define mozilla_layers_CanvasTranslator_h
10 #include <unordered_map>
11 #include <vector>
13 #include "mozilla/gfx/InlineTranslator.h"
14 #include "mozilla/layers/CanvasDrawEventRecorder.h"
15 #include "mozilla/layers/LayersSurfaces.h"
16 #include "mozilla/ipc/CrossProcessSemaphore.h"
17 #include "mozilla/Monitor.h"
18 #include "mozilla/UniquePtr.h"
20 namespace mozilla {
21 namespace layers {
23 class TextureData;
25 class CanvasTranslator final : public gfx::InlineTranslator {
26 public:
27 /**
28 * Create an uninitialized CanvasTranslator. This must be initialized via the
29 * Init function before any translation can occur. We need this to be able
30 * to create the CanvasTranslator on a different thread where we will need
31 * to call WaitForSurfaceDescriptor. Otherwise we have a race between the
32 * CanvasTranslator being created on the canvas thread and the first texture
33 * being required on the Compositor thread.
35 * @returns the new CanvasTranslator
37 static UniquePtr<CanvasTranslator> Create();
39 ~CanvasTranslator();
41 /**
42 * Initializes a canvas translator for a particular TextureType, which
43 * translates events from a CanvasEventRingBuffer.
45 * @param aTextureType the TextureType the translator will create
46 * @param aReadHandle handle to the shared memory for the
47 * CanvasEventRingBuffer
48 * @param aReaderSem reading blocked semaphore for the CanvasEventRingBuffer
49 * @param aWriterSem writing blocked semaphore for the CanvasEventRingBuffer
50 * @param aReaderServices provides functions required by the reader
51 * @returns true if the initialization works, false otherwise
53 bool Init(const TextureType& aTextureType,
54 const ipc::SharedMemoryBasic::Handle& aReadHandle,
55 const CrossProcessSemaphoreHandle& aReaderSem,
56 const CrossProcessSemaphoreHandle& aWriterSem,
57 UniquePtr<CanvasEventRingBuffer::ReaderServices> aReaderServices);
59 bool IsValid() { return mIsValid; }
61 /**
62 * Translates events until no more are available or the end of a transaction
63 * If this returns false the caller of this is responsible for re-calling
64 * this function.
66 * @returns true if all events are processed and false otherwise.
68 bool TranslateRecording();
70 /**
71 * Marks the beginning of rendering for a transaction. While in a transaction
72 * the translator will wait for a short time for events before returning.
73 * When not in a transaction the translator will only translate one event at a
74 * time.
76 void BeginTransaction();
78 /**
79 * Marks the end of a transaction.
81 void EndTransaction();
83 /**
84 * Flushes canvas drawing, for example to a device.
86 void Flush();
88 /**
89 * Used to send data back to the writer. This is done through the same shared
90 * memory so the writer must wait and read the response after it has submitted
91 * the event that uses this.
93 * @param aData the data to be written back to the writer
94 * @param aSize the number of chars to write
96 void ReturnWrite(const char* aData, size_t aSize) {
97 mStream.ReturnWrite(aData, aSize);
101 * Used during playback of events to create DrawTargets. For the
102 * CanvasTranslator this means creating TextureDatas and getting the
103 * DrawTargets from those.
105 * @param aRefPtr the key to store the created DrawTarget against
106 * @param aSize the size of the DrawTarget
107 * @param aFormat the surface format for the DrawTarget
108 * @returns the new DrawTarget
110 already_AddRefed<gfx::DrawTarget> CreateDrawTarget(
111 gfx::ReferencePtr aRefPtr, const gfx::IntSize& aSize,
112 gfx::SurfaceFormat aFormat) final;
115 * Get the TextureData associated with a DrawTarget from another process.
117 * @param aDrawTarget the key used to find the TextureData
118 * @returns the TextureData found
120 TextureData* LookupTextureData(gfx::ReferencePtr aDrawTarget);
123 * Waits for the SurfaceDescriptor associated with a DrawTarget from another
124 * process to be created and then returns it.
126 * @param aDrawTarget the key used to find the TextureData
127 * @returns the SurfaceDescriptor found
129 UniquePtr<SurfaceDescriptor> WaitForSurfaceDescriptor(
130 gfx::ReferencePtr aDrawTarget);
133 * Removes the DrawTarget and other objects associated with a DrawTarget from
134 * another process.
136 * @param aDrawTarget the key to the objects to remove
138 void RemoveDrawTarget(gfx::ReferencePtr aDrawTarget) final;
141 * Removes the SourceSurface and other objects associated with a SourceSurface
142 * from another process.
144 * @param aRefPtr the key to the objects to remove
146 void RemoveSourceSurface(gfx::ReferencePtr aRefPtr) final {
147 RemoveDataSurface(aRefPtr);
148 InlineTranslator::RemoveSourceSurface(aRefPtr);
152 * Gets the cached DataSourceSurface, if it exists, associated with a
153 * SourceSurface from another process.
155 * @param aRefPtr the key used to find the DataSourceSurface
156 * @returns the DataSourceSurface or nullptr if not found
158 gfx::DataSourceSurface* LookupDataSurface(gfx::ReferencePtr aRefPtr);
161 * Used to cache the DataSourceSurface from a SourceSurface associated with a
162 * SourceSurface from another process. This is to improve performance if we
163 * require the data for that SourceSurface.
165 * @param aRefPtr the key used to store the DataSourceSurface
166 * @param aSurface the DataSourceSurface to store
168 void AddDataSurface(gfx::ReferencePtr aRefPtr,
169 RefPtr<gfx::DataSourceSurface>&& aSurface);
172 * Gets the cached DataSourceSurface, if it exists, associated with a
173 * SourceSurface from another process.
175 * @param aRefPtr the key used to find the DataSourceSurface
176 * @returns the DataSourceSurface or nullptr if not found
178 void RemoveDataSurface(gfx::ReferencePtr aRefPtr);
181 * Sets a ScopedMap, to be used in a later event.
183 * @param aSurface the associated surface in the other process
184 * @param aMap the ScopedMap to store
186 void SetPreparedMap(gfx::ReferencePtr aSurface,
187 UniquePtr<gfx::DataSourceSurface::ScopedMap> aMap);
190 * Gets the ScopedMap stored using SetPreparedMap.
192 * @param aSurface must match the surface from the SetPreparedMap call
193 * @returns the ScopedMap if aSurface matches otherwise nullptr
195 UniquePtr<gfx::DataSourceSurface::ScopedMap> GetPreparedMap(
196 gfx::ReferencePtr aSurface);
198 private:
199 CanvasTranslator();
201 void AddSurfaceDescriptor(gfx::ReferencePtr aRefPtr,
202 TextureData* atextureData);
204 bool HandleExtensionEvent(int32_t aType);
206 CanvasEventRingBuffer mStream;
207 TextureType mTextureType = TextureType::Unknown;
208 UniquePtr<TextureData> mReferenceTextureData;
209 typedef std::unordered_map<void*, UniquePtr<TextureData>> TextureMap;
210 TextureMap mTextureDatas;
211 nsRefPtrHashtable<nsPtrHashKey<void>, gfx::DataSourceSurface> mDataSurfaces;
212 gfx::ReferencePtr mMappedSurface;
213 UniquePtr<gfx::DataSourceSurface::ScopedMap> mPreparedMap;
214 typedef std::unordered_map<void*, UniquePtr<SurfaceDescriptor>> DescriptorMap;
215 DescriptorMap mSurfaceDescriptors;
216 Monitor mSurfaceDescriptorsMonitor{
217 "CanvasTranslator::mSurfaceDescriptorsMonitor"};
218 bool mIsValid = true;
219 bool mIsInTransaction = false;
222 } // namespace layers
223 } // namespace mozilla
225 #endif // mozilla_layers_CanvasTranslator_h