Add helper class for dealing with gralloc.

This reverts commit a485a7e60db95039df5de82119e47878372550ac
(which reverts the original version of this CL).

Fixed build errors - put reinterpret_cast around delete [] in
V4L2Gralloc destructor, and fixed missing parens around another
reinterpret cast.

BUG: 29335262
Change-Id: Iaa5a71afef8850ba499cb1a7159a831da4f7014a
diff --git a/modules/camera/3_4/V4L2Gralloc.h b/modules/camera/3_4/V4L2Gralloc.h
new file mode 100644
index 0000000..6e37adb
--- /dev/null
+++ b/modules/camera/3_4/V4L2Gralloc.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef V4L2_GRALLOC_H
+#define V4L2_GRALLOC_H
+
+#include <linux/videodev2.h>
+
+#include <unordered_map>
+
+#include <hardware/camera3.h>
+#include <hardware/gralloc.h>
+#include <system/graphics.h>
+
+namespace v4l2_camera_hal {
+
+// Generously allow up to 3MB.
+static constexpr size_t V4L2_MAX_JPEG_SIZE = 3000000;
+
+// V4L2Gralloc is a wrapper around relevant parts of a gralloc module,
+// with some assistive transformations.
+class V4L2Gralloc {
+public:
+  V4L2Gralloc(const gralloc_module_t* module = nullptr);
+  ~V4L2Gralloc();
+
+  // Lock a camera buffer. Sets device buffer user pointer and length.
+  int lock(const camera3_stream_buffer_t* camera_buffer,
+           uint32_t bytes_per_line,
+           v4l2_buffer* device_buffer);
+  // Unlock a buffer that was locked by this helper (equality determined
+  // based on buffer user pointer, not the specific object).
+  int unlock(const v4l2_buffer* device_buffer);
+
+  // Check that the module passed in to the constructor is supported.
+  bool isValid();
+
+private:
+  const gralloc_module_t* mModule;
+
+  struct BufferData {
+    const camera3_stream_buffer_t* camera_buffer;
+    // Below fields only used when a ycbcr format transform is necessary.
+    std::unique_ptr<android_ycbcr> transform_dest;  // nullptr if no transform.
+    uint32_t v4l2_bytes_per_line;
+  };
+  // Map data pointer : BufferData about that buffer.
+  std::unordered_map<void*, const BufferData*> mBufferMap;
+};
+} // namespace default_camera_hal
+
+#endif // CAMERA_GRALLOC_H_