1/*
2 * Copyright 2011, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _ANDROID_MEDIA_UTILS_H_
18#define _ANDROID_MEDIA_UTILS_H_
19
20#include "src/piex_types.h"
21#include "src/piex.h"
22
23#include <android_runtime/AndroidRuntime.h>
24#include <camera3.h>
25#include <gui/CpuConsumer.h>
26#include <jni.h>
27#include <JNIHelp.h>
28#include <utils/KeyedVector.h>
29#include <utils/String8.h>
30#include <SkStream.h>
31
32namespace android {
33
34class AssetStream : public piex::StreamInterface {
35private:
36    SkStream *mStream;
37    size_t mPosition;
38
39public:
40    explicit AssetStream(SkStream* stream);
41    ~AssetStream();
42
43    // Reads 'length' amount of bytes from 'offset' to 'data'. The 'data' buffer
44    // provided by the caller, guaranteed to be at least "length" bytes long.
45    // On 'kOk' the 'data' pointer contains 'length' valid bytes beginning at
46    // 'offset' bytes from the start of the stream.
47    // Returns 'kFail' if 'offset' + 'length' exceeds the stream and does not
48    // change the contents of 'data'.
49    piex::Error GetData(
50            const size_t offset, const size_t length, std::uint8_t* data) override;
51};
52
53class BufferedStream : public piex::StreamInterface {
54private:
55    SkStream *mStream;
56    // Growable memory stream
57    SkDynamicMemoryWStream mStreamBuffer;
58
59    // Minimum size to read on filling the buffer.
60    const size_t kMinSizeToRead = 8192;
61
62public:
63    explicit BufferedStream(SkStream* stream);
64    ~BufferedStream();
65
66    // Reads 'length' amount of bytes from 'offset' to 'data'. The 'data' buffer
67    // provided by the caller, guaranteed to be at least "length" bytes long.
68    // On 'kOk' the 'data' pointer contains 'length' valid bytes beginning at
69    // 'offset' bytes from the start of the stream.
70    // Returns 'kFail' if 'offset' + 'length' exceeds the stream and does not
71    // change the contents of 'data'.
72    piex::Error GetData(
73            const size_t offset, const size_t length, std::uint8_t* data) override;
74};
75
76class FileStream : public piex::StreamInterface {
77private:
78    FILE *mFile;
79    size_t mPosition;
80
81public:
82    explicit FileStream(const int fd);
83    explicit FileStream(const String8 filename);
84    ~FileStream();
85
86    // Reads 'length' amount of bytes from 'offset' to 'data'. The 'data' buffer
87    // provided by the caller, guaranteed to be at least "length" bytes long.
88    // On 'kOk' the 'data' pointer contains 'length' valid bytes beginning at
89    // 'offset' bytes from the start of the stream.
90    // Returns 'kFail' if 'offset' + 'length' exceeds the stream and does not
91    // change the contents of 'data'.
92    piex::Error GetData(
93            const size_t offset, const size_t length, std::uint8_t* data) override;
94    bool exists() const;
95};
96
97// Reads EXIF metadata from a given raw image via piex.
98// And returns true if the operation is successful; otherwise, false.
99bool GetExifFromRawImage(
100        piex::StreamInterface* stream, const String8& filename, piex::PreviewImageData& image_data);
101
102// Returns true if the conversion is successful; otherwise, false.
103bool ConvertKeyValueArraysToKeyedVector(
104        JNIEnv *env, jobjectArray keys, jobjectArray values,
105        KeyedVector<String8, String8>* vector);
106
107struct AMessage;
108status_t ConvertMessageToMap(
109        JNIEnv *env, const sp<AMessage> &msg, jobject *map);
110
111status_t ConvertKeyValueArraysToMessage(
112        JNIEnv *env, jobjectArray keys, jobjectArray values,
113        sp<AMessage> *msg);
114
115// -----------Utility functions used by ImageReader/Writer JNI-----------------
116
117typedef CpuConsumer::LockedBuffer LockedImage;
118
119bool usingRGBAToJpegOverride(int32_t imageFormat, int32_t containerFormat);
120
121int32_t applyFormatOverrides(int32_t imageFormat, int32_t containerFormat);
122
123uint32_t Image_getJpegSize(LockedImage* buffer, bool usingRGBAOverride);
124
125bool isFormatOpaque(int format);
126
127bool isPossiblyYUV(PixelFormat format);
128
129status_t getLockedImageInfo(LockedImage* buffer, int idx, int32_t containerFormat,
130        uint8_t **base, uint32_t *size, int *pixelStride, int *rowStride);
131
132status_t lockImageFromBuffer(sp<GraphicBuffer> buffer, uint32_t inUsage,
133        const Rect& rect, int fenceFd, LockedImage* outputImage);
134
135status_t lockImageFromBuffer(BufferItem* bufferItem, uint32_t inUsage,
136        int fenceFd, LockedImage* outputImage);
137
138int getBufferWidth(BufferItem *buffer);
139
140int getBufferHeight(BufferItem *buffer);
141
142};  // namespace android
143
144#endif //  _ANDROID_MEDIA_UTILS_H_
145