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 
32 namespace android {
33 
34 class AssetStream : public piex::StreamInterface {
35 private:
36     SkStream *mStream;
37     size_t mPosition;
38 
39 public:
40     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 
53 class BufferedStream : public piex::StreamInterface {
54 private:
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 
62 public:
63     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 
76 class FileStream : public piex::StreamInterface {
77 private:
78     FILE *mFile;
79     size_t mPosition;
80 
81 public:
82     FileStream(const int fd);
83     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.
99 bool GetExifFromRawImage(
100         piex::StreamInterface* stream, const String8& filename, piex::PreviewImageData& image_data);
101 
102 // Returns true if the conversion is successful; otherwise, false.
103 bool ConvertKeyValueArraysToKeyedVector(
104         JNIEnv *env, jobjectArray keys, jobjectArray values,
105         KeyedVector<String8, String8>* vector);
106 
107 struct AMessage;
108 status_t ConvertMessageToMap(
109         JNIEnv *env, const sp<AMessage> &msg, jobject *map);
110 
111 status_t ConvertKeyValueArraysToMessage(
112         JNIEnv *env, jobjectArray keys, jobjectArray values,
113         sp<AMessage> *msg);
114 
115 // -----------Utility functions used by ImageReader/Writer JNI-----------------
116 
117 typedef CpuConsumer::LockedBuffer LockedImage;
118 
119 bool usingRGBAToJpegOverride(int32_t imageFormat, int32_t containerFormat);
120 
121 int32_t applyFormatOverrides(int32_t imageFormat, int32_t containerFormat);
122 
123 uint32_t Image_getJpegSize(LockedImage* buffer, bool usingRGBAOverride);
124 
125 bool isFormatOpaque(int format);
126 
127 bool isPossiblyYUV(PixelFormat format);
128 
129 status_t getLockedImageInfo(LockedImage* buffer, int idx, int32_t containerFormat,
130         uint8_t **base, uint32_t *size, int *pixelStride, int *rowStride);
131 
132 status_t lockImageFromBuffer(sp<GraphicBuffer> buffer, uint32_t inUsage,
133         const Rect& rect, int fenceFd, LockedImage* outputImage);
134 
135 status_t lockImageFromBuffer(BufferItem* bufferItem, uint32_t inUsage,
136         int fenceFd, LockedImage* outputImage);
137 
138 int getBufferWidth(BufferItem *buffer);
139 
140 int getBufferHeight(BufferItem *buffer);
141 
142 };  // namespace android
143 
144 #endif //  _ANDROID_MEDIA_UTILS_H_
145