COMPMID-1004 GLES: Add memory manager to GLES functions

Change-Id: I80fc9c0dd02afd79b501abde751036f9599b7bf2
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/125103
Tested-by: Jenkins <bsgcomp@arm.com>
Reviewed-by: Georgios Pinitas <georgios.pinitas@arm.com>
diff --git a/arm_compute/runtime/GLES_COMPUTE/GCBufferAllocator.h b/arm_compute/runtime/GLES_COMPUTE/GCBufferAllocator.h
new file mode 100644
index 0000000..103ac37
--- /dev/null
+++ b/arm_compute/runtime/GLES_COMPUTE/GCBufferAllocator.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *gc
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#ifndef __ARM_COMPUTE_GCBUFFERALLOCATOR_H__
+#define __ARM_COMPUTE_GCBUFFERALLOCATOR_H__
+
+#include "arm_compute/runtime/IAllocator.h"
+
+#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h"
+#include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h"
+
+#include <cstddef>
+
+namespace arm_compute
+{
+/** Default gles buffer allocator implementation */
+class GCBufferAllocator : public IAllocator
+{
+public:
+    /** Default constructor */
+    GCBufferAllocator() = default;
+
+    // Inherited methods overridden:
+    void *allocate(size_t size, size_t alignment) override;
+    void free(void *ptr) override;
+};
+} // arm_compute
+#endif /*__ARM_COMPUTE_GCBUFFERALLOCATOR_H__ */
diff --git a/arm_compute/runtime/GLES_COMPUTE/GCMemoryGroup.h b/arm_compute/runtime/GLES_COMPUTE/GCMemoryGroup.h
new file mode 100644
index 0000000..485aa0e
--- /dev/null
+++ b/arm_compute/runtime/GLES_COMPUTE/GCMemoryGroup.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#ifndef __ARM_COMPUTE_GCMEMORYGROUP_H__
+#define __ARM_COMPUTE_GCMEMORYGROUP_H__
+
+#include "arm_compute/runtime/MemoryGroupBase.h"
+
+#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h"
+#include "arm_compute/core/utils/misc/Cast.h"
+#include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
+
+namespace arm_compute
+{
+using GCMemoryGroup = MemoryGroupBase<GCTensor>;
+
+template <>
+inline void MemoryGroupBase<GCTensor>::associate_memory_group(GCTensor *obj)
+{
+    ARM_COMPUTE_ERROR_ON(obj == nullptr);
+    ARM_COMPUTE_ERROR_ON(dynamic_cast<GCTensorAllocator *>(obj->allocator()) == nullptr);
+
+    auto allocator = arm_compute::utils::cast::polymorphic_downcast<GCTensorAllocator *>(obj->allocator());
+    ARM_COMPUTE_ERROR_ON(allocator == nullptr);
+    allocator->set_associated_memory_group(this);
+}
+} // arm_compute
+#endif /*__ARM_COMPUTE_GCMEMORYGROUP_H__ */
diff --git a/arm_compute/runtime/GLES_COMPUTE/GCTensor.h b/arm_compute/runtime/GLES_COMPUTE/GCTensor.h
index 3e51f99..0f5f194 100644
--- a/arm_compute/runtime/GLES_COMPUTE/GCTensor.h
+++ b/arm_compute/runtime/GLES_COMPUTE/GCTensor.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -91,7 +91,7 @@
     void do_unmap() override;
 
 private:
-    mutable GCTensorAllocator _allocator;
+    mutable GCTensorAllocator _allocator; /**< Instance of the OpenGL ES tensor allocator */
 };
 
 using GCImage = GCTensor;
diff --git a/arm_compute/runtime/GLES_COMPUTE/GCTensorAllocator.h b/arm_compute/runtime/GLES_COMPUTE/GCTensorAllocator.h
index ce52cbb..fc14f04 100644
--- a/arm_compute/runtime/GLES_COMPUTE/GCTensorAllocator.h
+++ b/arm_compute/runtime/GLES_COMPUTE/GCTensorAllocator.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -27,17 +27,37 @@
 
 #include "arm_compute/core/GLES_COMPUTE/OpenGLES.h"
 #include "arm_compute/runtime/ITensorAllocator.h"
+#include "arm_compute/runtime/MemoryGroupBase.h"
 
 #include <memory>
 
 namespace arm_compute
 {
+class GCTensor;
+template <typename>
+class MemoryGroupBase;
+using GCMemoryGroup = MemoryGroupBase<GCTensor>;
+
+class GLBufferWrapper
+{
+public:
+    GLBufferWrapper()
+        : _ssbo_name(0)
+    {
+        ARM_COMPUTE_GL_CHECK(glGenBuffers(1, &_ssbo_name));
+    }
+    ~GLBufferWrapper()
+    {
+        ARM_COMPUTE_GL_CHECK(glDeleteBuffers(1, &_ssbo_name));
+    }
+    GLuint _ssbo_name;
+};
 /** Basic implementation of a GLES memory tensor allocator. */
 class GCTensorAllocator : public ITensorAllocator
 {
 public:
     /** Default constructor. */
-    GCTensorAllocator();
+    GCTensorAllocator(GCTensor *owner = nullptr);
 
     /** Prevent instances of this class from being copied (As this class contains pointers). */
     GCTensorAllocator(const GCTensorAllocator &) = delete;
@@ -52,7 +72,7 @@
     GCTensorAllocator &operator=(GCTensorAllocator &&) = default;
 
     /** Default destructor */
-    ~GCTensorAllocator() = default;
+    ~GCTensorAllocator();
 
     /** Interface to be implemented by the child class to return the pointer to the mapped data. */
     uint8_t *data();
@@ -95,6 +115,12 @@
      */
     void free() override;
 
+    /** Associates the tensor with a memory group
+     *
+     * @param[in] associated_memory_group Memory group to associate the tensor with
+     */
+    void set_associated_memory_group(GCMemoryGroup *associated_memory_group);
+
 protected:
     /** Call map() on the SSBO.
      *
@@ -106,22 +132,10 @@
     void unlock() override;
 
 private:
-    class GLBufferWrapper
-    {
-    public:
-        GLBufferWrapper()
-            : _ssbo_name(0)
-        {
-            ARM_COMPUTE_GL_CHECK(glGenBuffers(1, &_ssbo_name));
-        }
-        ~GLBufferWrapper()
-        {
-            ARM_COMPUTE_GL_CHECK(glDeleteBuffers(1, &_ssbo_name));
-        }
-        GLuint _ssbo_name;
-    };
-    std::unique_ptr<GLBufferWrapper> _gl_buffer;
-    uint8_t                         *_mapping;
+    GCMemoryGroup                   *_associated_memory_group; /**< Registered memory group */
+    std::unique_ptr<GLBufferWrapper> _gl_buffer;               /**< OpenGL ES object containing the tensor data. */
+    uint8_t                         *_mapping;                 /**< Pointer to the CPU mapping of the OpenGL ES buffer. */
+    GCTensor                        *_owner;                   /**< Owner of the allocator */
 };
 }
 
diff --git a/arm_compute/runtime/GLES_COMPUTE/functions/GCConvolutionLayer.h b/arm_compute/runtime/GLES_COMPUTE/functions/GCConvolutionLayer.h
index e3fa98e..2bac982 100644
--- a/arm_compute/runtime/GLES_COMPUTE/functions/GCConvolutionLayer.h
+++ b/arm_compute/runtime/GLES_COMPUTE/functions/GCConvolutionLayer.h
@@ -33,6 +33,7 @@
 #include "arm_compute/core/GLES_COMPUTE/kernels/GCIm2ColKernel.h"
 #include "arm_compute/core/GLES_COMPUTE/kernels/GCWeightsReshapeKernel.h"
 #include "arm_compute/core/Types.h"
+#include "arm_compute/runtime/GLES_COMPUTE/GCMemoryGroup.h"
 #include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
 #include "arm_compute/runtime/IFunction.h"
 
@@ -83,7 +84,7 @@
 {
 public:
     /** Default constructor */
-    GCConvolutionLayer();
+    GCConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
 
     /** Set the input and output tensors.
      *
@@ -115,6 +116,7 @@
     void configure_mm(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output, bool is_interleaved_transposed = true);
 
 private:
+    GCMemoryGroup                    _memory_group;
     GCConvolutionLayerReshapeWeights _reshape_weights;
     GCIm2ColKernel                   _input_im2col_kernel;
     GCGEMMInterleave4x4Kernel        _input_interleave_kernel;
diff --git a/arm_compute/runtime/GLES_COMPUTE/functions/GCFullyConnectedLayer.h b/arm_compute/runtime/GLES_COMPUTE/functions/GCFullyConnectedLayer.h
index 3ba44f5..81be1de 100644
--- a/arm_compute/runtime/GLES_COMPUTE/functions/GCFullyConnectedLayer.h
+++ b/arm_compute/runtime/GLES_COMPUTE/functions/GCFullyConnectedLayer.h
@@ -28,6 +28,7 @@
 #include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMMatrixMultiplyKernel.h"
 #include "arm_compute/core/GLES_COMPUTE/kernels/GCIm2ColKernel.h"
 #include "arm_compute/core/GLES_COMPUTE/kernels/GCTransposeKernel.h"
+#include "arm_compute/runtime/GLES_COMPUTE/GCMemoryGroup.h"
 #include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
 #include "arm_compute/runtime/GLES_COMPUTE/IGCSimpleFunction.h"
 
@@ -63,7 +64,7 @@
 {
 public:
     /** Constructor */
-    GCFullyConnectedLayer();
+    GCFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
     /** Set the input and output tensors.
      *
      * @param[in]  input                Source tensor. Data type supported: F16/F32.
@@ -82,6 +83,7 @@
     void configure_fc_fc(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output);
     void configure_conv_fc(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output);
 
+    GCMemoryGroup                       _memory_group;
     GCIm2ColKernel                      _im2col_kernel;
     GCFullyConnectedLayerReshapeWeights _reshape_weights_kernel;
     GCGEMMMatrixMultiplyKernel          _mm_kernel;
diff --git a/arm_compute/runtime/GLES_COMPUTE/functions/GCGEMM.h b/arm_compute/runtime/GLES_COMPUTE/functions/GCGEMM.h
index 8ddfae1..31ad0ab 100644
--- a/arm_compute/runtime/GLES_COMPUTE/functions/GCGEMM.h
+++ b/arm_compute/runtime/GLES_COMPUTE/functions/GCGEMM.h
@@ -29,6 +29,7 @@
 #include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMMatrixAdditionKernel.h"
 #include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMMatrixMultiplyKernel.h"
 #include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMTranspose1xWKernel.h"
+#include "arm_compute/runtime/GLES_COMPUTE/GCMemoryGroup.h"
 #include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
 #include "arm_compute/runtime/IFunction.h"
 
@@ -48,7 +49,7 @@
 {
 public:
     /** Default constructor. */
-    GCGEMM();
+    GCGEMM(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
 
     /** Initialise the kernel's inputs and output
      *
@@ -73,6 +74,7 @@
     void run() override;
 
 private:
+    GCMemoryGroup              _memory_group;
     GCGEMMInterleave4x4Kernel  _interleave_kernel;
     GCGEMMTranspose1xWKernel   _transpose_kernel;
     GCGEMMMatrixMultiplyKernel _mm_kernel;
diff --git a/arm_compute/runtime/GLES_COMPUTE/functions/GCNormalizationLayer.h b/arm_compute/runtime/GLES_COMPUTE/functions/GCNormalizationLayer.h
index d080a2f..adc8157 100644
--- a/arm_compute/runtime/GLES_COMPUTE/functions/GCNormalizationLayer.h
+++ b/arm_compute/runtime/GLES_COMPUTE/functions/GCNormalizationLayer.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -29,6 +29,7 @@
 #include "arm_compute/core/GLES_COMPUTE/kernels/GCFillBorderKernel.h"
 #include "arm_compute/core/GLES_COMPUTE/kernels/GCNormalizationLayerKernel.h"
 #include "arm_compute/core/GLES_COMPUTE/kernels/GCPixelWiseMultiplicationKernel.h"
+#include "arm_compute/runtime/GLES_COMPUTE/GCMemoryGroup.h"
 #include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
 
 #include "arm_compute/core/Types.h"
@@ -48,7 +49,7 @@
 {
 public:
     /** Default constructor */
-    GCNormalizationLayer();
+    GCNormalizationLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
     /** Set the input and output tensors.
      *
      * @param[in]  input     Source tensor. 3 lower dims represent a single input with dimensions [width, height, IFM],
@@ -62,6 +63,7 @@
     void run() override;
 
 private:
+    GCMemoryGroup                   _memory_group;
     GCTensor                        _squared_input;   /**< The intermediate buffer which stores results of squaring input*/
     GCNormalizationLayerKernel      _norm_kernel;     /**< Normalization layer kernel to run */
     GCPixelWiseMultiplicationKernel _multiply_kernel; /**< Pixel multiplication kernel to run */
diff --git a/arm_compute/runtime/GLES_COMPUTE/functions/GCSoftmaxLayer.h b/arm_compute/runtime/GLES_COMPUTE/functions/GCSoftmaxLayer.h
index e7f8d50..1011c9a 100644
--- a/arm_compute/runtime/GLES_COMPUTE/functions/GCSoftmaxLayer.h
+++ b/arm_compute/runtime/GLES_COMPUTE/functions/GCSoftmaxLayer.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -25,6 +25,7 @@
 #define __ARM_COMPUTE_GCSOFTMAXLAYER_H__
 
 #include "arm_compute/core/GLES_COMPUTE/kernels/GCSoftmaxLayerKernel.h"
+#include "arm_compute/runtime/GLES_COMPUTE/GCMemoryGroup.h"
 #include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
 #include "arm_compute/runtime/IFunction.h"
 
@@ -46,7 +47,7 @@
 {
 public:
     /** Constructor */
-    GCSoftmaxLayer();
+    GCSoftmaxLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
     /** Set the input and output tensors.
      *
      * @param[in]  input  Source tensor. Data types supported: F16/F32
@@ -59,6 +60,7 @@
     void run() override;
 
 private:
+    GCMemoryGroup               _memory_group;
     GCLogits1DMaxKernel         _max_kernel;
     GCLogits1DShiftExpSumKernel _shift_exp_sum_kernel;
     GCLogits1DNormKernel        _norm_kernel;