• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #include "aemu/base/containers/BufferQueue.h"
17 #include "render-utils/RenderChannel.h"
18 #include "RendererImpl.h"
19 
20 namespace gfxstream {
21 
22 class RenderThread;
23 
24 using android::base::BufferQueue;
25 
26 // Implementation of the RenderChannel interface that connects a guest
27 // client thread (really an AndroidPipe implementation) to a host
28 // RenderThread instance.
29 class RenderChannelImpl final : public RenderChannel {
30 public:
31     explicit RenderChannelImpl(android::base::Stream* loadStream = nullptr,
32                                uint32_t contextId = -1);
33     ~RenderChannelImpl();
34 
35     /////////////////////////////////////////////////////////////////
36     // RenderChannel overriden methods. These are called from the guest
37     // client thread.
38 
39     // Set the event |callback| to be notified when the host changes the
40     // state of the channel, according to the event mask provided by
41     // setWantedEvents(). Call this function right after creating the
42     // instance.
43     virtual void setEventCallback(EventCallback&& callback) override final;
44 
45     // Set the mask of events the guest wants to be notified of from the
46     // host thread.
47     virtual void setWantedEvents(State state) override final;
48 
49     // Return the current channel state relative to the guest.
50     virtual State state() const override final;
51 
52     // Try to send a buffer from the guest to the host render thread.
53     virtual IoResult tryWrite(Buffer&& buffer) override final;
54 
55     // Blocking call that waits until a buffer is ready to be sent from guest to host.
56     virtual void waitUntilWritable() override final;
57 
58     // Try to read a buffer from the host render thread into the guest.
59     virtual IoResult tryRead(Buffer* buffer) override final;
60 
61     // Read a buffer from the host render thread into the guest.
62     virtual IoResult readBefore(Buffer* buffer, Duration waitUntilUs) override final;
63 
64     // Blocking call that waits until a buffer is ready to be read from host to guest.
65     virtual void waitUntilReadable() override final;
66 
67     // Close the channel from the guest.
68     virtual void stop() override final;
69 
70     // Callback function when snapshotting the virtual machine.
71     virtual void onSave(android::base::Stream* stream) override;
72 
73     /////////////////////////////////////////////////////////////////
74     // These functions are called from the host render thread or renderer.
75 
76     // Send a buffer to the guest, this call is blocking. On success,
77     // move |buffer| into the channel and return true. On failure, return
78     // false (meaning that the channel was closed).
79     bool writeToGuest(Buffer&& buffer);
80 
81     // Read data from the guest. If |blocking| is true, the call will be
82     // blocking. On success, move item into |*buffer| and return true. On
83     // failure, return IoResult::Error to indicate the channel was closed,
84     // or IoResult::TryAgain to indicate it was empty (this can happen only
85     // if |blocking| is false).
86     IoResult readFromGuest(Buffer* buffer, bool blocking);
87 
88     // Close the channel from the host.
89     void stopFromHost();
90 
91     // Check if either guest or host stopped the channel.
92     bool isStopped() const;
93 
94     // Return the underlying render thread object.
95     RenderThread* renderThread() const;
96 
97     // Pause normal operations and enter the snapshot mode. In snapshot mode
98     // RenderChannel is supposed to allow everyone to write data into the
99     // channel, but it should not return any data back. This way we can make
100     // sure all data at the snapshot time is here and is saved, and we won't
101     // miss some important rendering call.
102     void pausePreSnapshot();
103 
104     // Resume the normal operation after saving or loading a snapshot.
105     void resume();
106 
107 private:
108     void updateStateLocked();
109     void notifyStateChangeLocked();
110 
111     EventCallback mEventCallback;
112     std::unique_ptr<RenderThread> mRenderThread;
113 
114     // A single lock to protect the state and the two buffer queues at the
115     // same time. NOTE: This needs to appear before the BufferQueue instances.
116     mutable android::base::Lock mLock;
117     State mState = State::Empty;
118     State mWantedEvents = State::Empty;
119     BufferQueue<RenderChannel::Buffer> mFromGuest;
120     BufferQueue<RenderChannel::Buffer> mToGuest;
121 };
122 
123 }  // namespace gfxstream
124