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 "base/BufferQueue.h" 17 #include "RenderChannel.h" 18 #include "RendererImpl.h" 19 20 namespace emugl { 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 ~RenderChannelImpl(); 33 34 ///////////////////////////////////////////////////////////////// 35 // RenderChannel overriden methods. These are called from the guest 36 // client thread. 37 38 // Set the event |callback| to be notified when the host changes the 39 // state of the channel, according to the event mask provided by 40 // setWantedEvents(). Call this function right after creating the 41 // instance. 42 virtual void setEventCallback(EventCallback&& callback) override final; 43 44 // Set the mask of events the guest wants to be notified of from the 45 // host thread. 46 virtual void setWantedEvents(State state) override final; 47 48 // Return the current channel state relative to the guest. 49 virtual State state() const override final; 50 51 // Try to send a buffer from the guest to the host render thread. 52 virtual IoResult tryWrite(Buffer&& buffer) override final; 53 54 // Try to read a buffer from the host render thread into the guest. 55 virtual IoResult tryRead(Buffer* buffer) override final; 56 57 // Read a buffer from the host render thread into the guest. 58 virtual IoResult readBefore(Buffer* buffer, Duration waitUntilUs) override final; 59 60 // Close the channel from the guest. 61 virtual void stop() override final; 62 63 // Callback function when snapshotting the virtual machine. 64 virtual void onSave(android::base::Stream* stream) override; 65 66 ///////////////////////////////////////////////////////////////// 67 // These functions are called from the host render thread or renderer. 68 69 // Send a buffer to the guest, this call is blocking. On success, 70 // move |buffer| into the channel and return true. On failure, return 71 // false (meaning that the channel was closed). 72 bool writeToGuest(Buffer&& buffer); 73 74 // Read data from the guest. If |blocking| is true, the call will be 75 // blocking. On success, move item into |*buffer| and return true. On 76 // failure, return IoResult::Error to indicate the channel was closed, 77 // or IoResult::TryAgain to indicate it was empty (this can happen only 78 // if |blocking| is false). 79 IoResult readFromGuest(Buffer* buffer, bool blocking); 80 81 // Close the channel from the host. 82 void stopFromHost(); 83 84 // Check if either guest or host stopped the channel. 85 bool isStopped() const; 86 87 // Return the underlying render thread object. 88 RenderThread* renderThread() const; 89 90 // Pause normal operations and enter the snapshot mode. In snapshot mode 91 // RenderChannel is supposed to allow everyone to write data into the 92 // channel, but it should not return any data back. This way we can make 93 // sure all data at the snapshot time is here and is saved, and we won't 94 // miss some important rendering call. 95 void pausePreSnapshot(); 96 97 // Resume the normal operation after saving or loading a snapshot. 98 void resume(); 99 100 private: 101 void updateStateLocked(); 102 void notifyStateChangeLocked(); 103 104 EventCallback mEventCallback; 105 std::unique_ptr<RenderThread> mRenderThread; 106 107 // A single lock to protect the state and the two buffer queues at the 108 // same time. NOTE: This needs to appear before the BufferQueue instances. 109 mutable android::base::Lock mLock; 110 State mState = State::Empty; 111 State mWantedEvents = State::Empty; 112 BufferQueue<RenderChannel::Buffer> mFromGuest; 113 BufferQueue<RenderChannel::Buffer> mToGuest; 114 }; 115 116 } // namespace emugl 117