• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_GL_OBJECT_MANAGER_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_GL_OBJECT_MANAGER_H_
18 
19 #include <cstdint>
20 #include <functional>
21 #include <memory>
22 #include <utility>
23 #include <vector>
24 
25 #include "tensorflow/lite/delegates/gpu/common/model.h"
26 #include "tensorflow/lite/delegates/gpu/common/status.h"
27 #include "tensorflow/lite/delegates/gpu/gl/gl_buffer.h"
28 #include "tensorflow/lite/delegates/gpu/gl/gl_texture.h"
29 #include "tensorflow/lite/delegates/gpu/gl/stats.h"
30 
31 namespace tflite {
32 namespace gpu {
33 namespace gl {
34 
35 // ObjectManager is a registry that owns corresponding objects and provides
36 // discovery functionality. All objects are kept until manager is destroyed.
37 //
38 // All buffers and textures share the same id space, therefore, it is an error
39 // to register two objects with the same id.
40 // TODO(akulik): make ObjectManager templated by object type.
41 class ObjectManager {
42  public:
43   // Moves ownership over the given buffer to the manager.
44   absl::Status RegisterBuffer(uint32_t id, GlBuffer buffer);
45 
46   void RemoveBuffer(uint32_t id);
47 
48   // Return a permanent pointer to a buffer for the given id or nullptr.
49   GlBuffer* FindBuffer(uint32_t id) const;
50 
51   // Moves ownership over the given texture to the manager.
52   absl::Status RegisterTexture(uint32_t id, GlTexture texture);
53 
54   void RemoveTexture(uint32_t id);
55 
56   // Return a permanent pointer to a texture for the given id or nullptr.
57   GlTexture* FindTexture(uint32_t id) const;
58 
59   ObjectsStats stats() const;
60 
61  private:
62   std::vector<std::unique_ptr<GlBuffer>> buffers_;
63   std::vector<std::unique_ptr<GlTexture>> textures_;
64 };
65 
66 // TODO(akulik): find better place for functions below.
67 
68 // Creates read-only buffer from the given tensor. Tensor data is converted to
69 // PHWC4 layout.
70 absl::Status CreatePHWC4BufferFromTensor(const TensorFloat32& tensor,
71                                          GlBuffer* gl_buffer);
72 
73 // Creates read-write buffer for the given tensor shape, where data layout is
74 // supposed to be PHWC4.
75 absl::Status CreatePHWC4BufferFromTensorRef(const TensorRef<BHWC>& tensor_ref,
76                                             GlBuffer* gl_buffer);
77 
78 // Copies data from a buffer that holds data in PHWC4 layout to the given
79 // tensor.
80 absl::Status CopyFromPHWC4Buffer(const GlBuffer& buffer, TensorFloat32* tensor);
81 
82 }  // namespace gl
83 }  // namespace gpu
84 }  // namespace tflite
85 
86 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_GL_OBJECT_MANAGER_H_
87