1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 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 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_LITE_DELEGATES_GPU_CL_EGL_SYNC_H_ 17 #define TENSORFLOW_LITE_DELEGATES_GPU_CL_EGL_SYNC_H_ 18 19 #include <EGL/egl.h> 20 #include <EGL/eglext.h> 21 22 #include "tensorflow/lite/delegates/gpu/common/status.h" 23 24 namespace tflite { 25 namespace gpu { 26 namespace cl { 27 28 // RAII wrapper for EGL sync object. 29 // EglSync is moveable but not copyable. 30 class EglSync { 31 public: 32 // Creates a fence in OpenGL command stream. This sync is enqueued and *not* 33 // flushed. 34 // 35 // Depends on EGL_KHR_fence_sync extension. 36 static absl::Status NewFence(EGLDisplay display, EglSync* sync); 37 38 // Creates invalid object. EglSync()39 EglSync() : EglSync(EGL_NO_DISPLAY, EGL_NO_SYNC_KHR) {} 40 EglSync(EGLDisplay display,EGLSyncKHR sync)41 EglSync(EGLDisplay display, EGLSyncKHR sync) 42 : display_(display), sync_(sync) {} 43 44 // Move-only 45 EglSync(EglSync&& sync); 46 EglSync& operator=(EglSync&& sync); 47 EglSync(const EglSync&) = delete; 48 EglSync& operator=(const EglSync&) = delete; 49 ~EglSync()50 ~EglSync() { Invalidate(); } 51 52 // Causes GPU to block and wait until this sync has been signaled. 53 // This call does not block and returns immediately. 54 absl::Status ServerWait(); 55 56 // Causes CPU to block and wait until this sync has been signaled. 57 absl::Status ClientWait(); 58 59 // Returns the EGLDisplay on which this instance was created. display()60 EGLDisplay display() const { return display_; } 61 62 // Returns the EGLSyncKHR wrapped by this instance. sync()63 EGLSyncKHR sync() const { return sync_; } 64 65 // Returns true if this instance wraps a valid EGLSync object. is_valid()66 bool is_valid() const { return sync_ != nullptr; } 67 68 private: 69 void Invalidate(); 70 71 EGLDisplay display_; 72 EGLSyncKHR sync_; 73 }; 74 75 } // namespace cl 76 } // namespace gpu 77 } // namespace tflite 78 79 #endif // TENSORFLOW_LITE_DELEGATES_GPU_CL_EGL_SYNC_H_ 80