1 // Copyright 2021 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 #ifndef VIRTIO_GPU_TIMELINES_H 15 #define VIRTIO_GPU_TIMELINES_H 16 17 #include <atomic> 18 #include <list> 19 #include <memory> 20 #include <unordered_map> 21 #include <variant> 22 23 #include "base/Lock.h" 24 #include "virtio-gpu-gfxstream-renderer.h" 25 #include "virtio_gpu_ops.h" 26 27 class VirtioGpuTimelines { 28 public: 29 using FenceId = uint64_t; 30 using CtxId = VirtioGpuCtxId; 31 using TaskId = uint64_t; 32 VirtioGpuTimelines(); 33 34 TaskId enqueueTask(CtxId); 35 void enqueueFence(CtxId, FenceId, FenceCompletionCallback); 36 void notifyTaskCompletion(TaskId); 37 38 private: 39 struct Fence { 40 std::unique_ptr<FenceCompletionCallback> mCompletionCallback; FenceFence41 Fence(FenceCompletionCallback completionCallback) 42 : mCompletionCallback(std::make_unique<FenceCompletionCallback>( 43 completionCallback)) {} 44 }; 45 struct Task { 46 TaskId mId; 47 CtxId mCtxId; 48 std::atomic_bool mHasCompleted; TaskTask49 Task(TaskId id, CtxId ctxId) 50 : mId(id), mCtxId(ctxId), mHasCompleted(false) {} 51 }; 52 using TimelineItem = 53 std::variant<std::unique_ptr<Fence>, std::shared_ptr<Task>>; 54 android::base::Lock mLock; 55 std::atomic<TaskId> mNextId; 56 // The mTaskIdToTask cache must be destroyed after the actual owner of Task, 57 // mTimelineQueues, is destroyed, because the deleter of Task will 58 // automatically remove the entry in mTaskIdToTask. 59 std::unordered_map<TaskId, std::weak_ptr<Task>> mTaskIdToTask; 60 std::unordered_map<CtxId, std::list<TimelineItem>> mTimelineQueues; 61 // Go over the timeline, signal any fences without pending tasks, and remove 62 // timeline items that are no longer needed. 63 void poll_locked(CtxId); 64 }; 65 66 #endif // VIRTIO_GPU_TIMELINES_H 67