Create the AutoGoldfishDmaContext class

This class is a RAII wrapper over goldfish_dma_context.
This class will be used next to FixedBuffer to provide a
DMA buffer for OpenGL calls.

Bug: 116046430
Test: make -j
Change-Id: Ic461b7482490769ee3ca733787fb462e295520ec
Signed-off-by: Roman Kiryanov <rkir@google.com>
diff --git a/shared/OpenglCodecCommon/Android.mk b/shared/OpenglCodecCommon/Android.mk
index 6eddf85..2a27bbc 100644
--- a/shared/OpenglCodecCommon/Android.mk
+++ b/shared/OpenglCodecCommon/Android.mk
@@ -12,6 +12,7 @@
         IndexRangeCache.cpp \
         SocketStream.cpp \
         TcpStream.cpp \
+        auto_goldfish_dma_context.cpp \
 
 ifeq (true,$(GOLDFISH_OPENGL_BUILD_FOR_HOST))
 
diff --git a/shared/OpenglCodecCommon/GLSharedGroup.h b/shared/OpenglCodecCommon/GLSharedGroup.h
index 0bb3bfa..aabb2cc 100755
--- a/shared/OpenglCodecCommon/GLSharedGroup.h
+++ b/shared/OpenglCodecCommon/GLSharedGroup.h
@@ -58,6 +58,10 @@
     // Internal bookkeeping
     FixedBuffer m_fixedBuffer; // actual buffer is shadowed here
     IndexRangeCache m_indexRangeCache;
+
+    // DMA support
+
+
 };
 
 class ProgramData {
diff --git a/shared/OpenglCodecCommon/auto_goldfish_dma_context.cpp b/shared/OpenglCodecCommon/auto_goldfish_dma_context.cpp
new file mode 100644
index 0000000..7b83892
--- /dev/null
+++ b/shared/OpenglCodecCommon/auto_goldfish_dma_context.cpp
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2018 Google, Inc.
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include "auto_goldfish_dma_context.h"
+
+namespace {
+goldfish_dma_context empty() {
+    goldfish_dma_context ctx = {};
+    return ctx;
+}
+
+void destroy(goldfish_dma_context *ctx) {
+    goldfish_dma_unmap(ctx);
+    goldfish_dma_free(ctx);
+}
+}  // namespace
+
+AutoGoldfishDmaContext::AutoGoldfishDmaContext() : m_ctx(empty()) {}
+
+AutoGoldfishDmaContext::AutoGoldfishDmaContext(goldfish_dma_context *ctx)
+    : m_ctx(*ctx) {
+    *ctx = empty();
+}
+
+AutoGoldfishDmaContext::~AutoGoldfishDmaContext() {
+    destroy(&m_ctx);
+}
+
+void AutoGoldfishDmaContext::reset(goldfish_dma_context *ctx) {
+    destroy(&m_ctx);
+    m_ctx = *ctx;
+    *ctx = empty();
+}
+
+goldfish_dma_context AutoGoldfishDmaContext::release() {
+    goldfish_dma_context copy = m_ctx;
+    m_ctx = empty();
+    return copy;
+}
+
diff --git a/shared/OpenglCodecCommon/auto_goldfish_dma_context.h b/shared/OpenglCodecCommon/auto_goldfish_dma_context.h
new file mode 100644
index 0000000..7d97fc2
--- /dev/null
+++ b/shared/OpenglCodecCommon/auto_goldfish_dma_context.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2018 Google, Inc.
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#ifndef ANDROID_INCLUDE_HARDWARE_AUTO_GOLDFISH_DMA_CONTEXT_H
+#define ANDROID_INCLUDE_HARDWARE_AUTO_GOLDFISH_DMA_CONTEXT_H
+
+#include <inttypes.h>
+#include "goldfish_dma.h"
+
+// A C++ wrapper for goldfish_dma_context that releases resources in dctor.
+class AutoGoldfishDmaContext {
+public:
+    AutoGoldfishDmaContext();
+    explicit AutoGoldfishDmaContext(goldfish_dma_context *ctx);
+    ~AutoGoldfishDmaContext();
+
+    const goldfish_dma_context &get() const { return m_ctx; }
+    void reset(goldfish_dma_context *ctx);
+    goldfish_dma_context release();
+
+private:
+    AutoGoldfishDmaContext(const AutoGoldfishDmaContext &rhs);
+    AutoGoldfishDmaContext& operator=(const AutoGoldfishDmaContext &rhs);
+
+    goldfish_dma_context m_ctx;
+};
+
+#endif  // ANDROID_INCLUDE_HARDWARE_AUTO_GOLDFISH_DMA_CONTEXT_H