1c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh/* 2c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * Copyright (C) 2016 The Android Open Source Project 3c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * 4c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * Licensed under the Apache License, Version 2.0 (the "License"); 5c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * you may not use this file except in compliance with the License. 6c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * You may obtain a copy of the License at 7c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * 8c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * http://www.apache.org/licenses/LICENSE-2.0 9c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * 10c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * Unless required by applicable law or agreed to in writing, software 11c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * distributed under the License is distributed on an "AS IS" BASIS, 12c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * See the License for the specific language governing permissions and 14c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * limitations under the License. 15c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh */ 16c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 17c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#ifndef _NDK_IMAGE_PRIV_H 18c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#define _NDK_IMAGE_PRIV_H 19c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 20c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#include <inttypes.h> 21c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#include <utils/Log.h> 22c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#include <utils/StrongPointer.h> 23c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 242f1a4737c997d9589447a51974e3aaecafee2937Jiwen 'Steve' Cai#include <gui/BufferItem.h> 25c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#include <gui/CpuConsumer.h> 26c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 27c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#include "NdkImageReaderPriv.h" 28c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#include "NdkImage.h" 29c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 30c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 31c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yehusing namespace android; 32c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 33c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh// TODO: this only supports ImageReader 34c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yehstruct AImage { 35e31bc87213619054f18fcc8462641767e82a7eafJiwen 'Steve' Cai AImage(AImageReader* reader, int32_t format, uint64_t usage, BufferItem* buffer, 36e31bc87213619054f18fcc8462641767e82a7eafJiwen 'Steve' Cai int64_t timestamp, int32_t width, int32_t height, int32_t numPlanes); 37c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 38c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh // free all resources while keeping object alive. Caller must obtain reader lock 39e168996c51cbbc78790c2b3282c9455a5c7b667cJiwen 'Steve' Cai void close() { close(-1); } 40e168996c51cbbc78790c2b3282c9455a5c7b667cJiwen 'Steve' Cai void close(int releaseFenceFd); 41c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 42c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh // Remove from object memory. Must be called after close 43c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh void free(); 44c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 45c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh bool isClosed() const ; 46c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 47c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh // only For AImage to grab reader lock 48c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh // Always grab reader lock before grabbing image lock 49c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh void lockReader() const; 50c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh void unlockReader() const; 51c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 52c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh media_status_t getWidth(/*out*/int32_t* width) const; 53c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh media_status_t getHeight(/*out*/int32_t* height) const; 54c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh media_status_t getFormat(/*out*/int32_t* format) const; 55c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh media_status_t getNumPlanes(/*out*/int32_t* numPlanes) const; 56c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh media_status_t getTimestamp(/*out*/int64_t* timestamp) const; 57c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 582f1a4737c997d9589447a51974e3aaecafee2937Jiwen 'Steve' Cai media_status_t lockImage(); 592f1a4737c997d9589447a51974e3aaecafee2937Jiwen 'Steve' Cai media_status_t unlockImageIfLocked(/*out*/int* fenceFd); 602f1a4737c997d9589447a51974e3aaecafee2937Jiwen 'Steve' Cai 61c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh media_status_t getPlanePixelStride(int planeIdx, /*out*/int32_t* pixelStride) const; 62c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh media_status_t getPlaneRowStride(int planeIdx, /*out*/int32_t* rowStride) const; 63c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh media_status_t getPlaneData(int planeIdx,/*out*/uint8_t** data, /*out*/int* dataLength) const; 64e168996c51cbbc78790c2b3282c9455a5c7b667cJiwen 'Steve' Cai media_status_t getHardwareBuffer(/*out*/AHardwareBuffer** buffer) const; 65c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 66c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh private: 67c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh // AImage should be deleted through free() API. 68c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh ~AImage(); 69c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 70c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh friend struct AImageReader; // for reader to access mBuffer 71c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 72c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh uint32_t getJpegSize() const; 73c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 74c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh // When reader is close, AImage will only accept close API call 75c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh wp<AImageReader> mReader; 76c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh const int32_t mFormat; 77e31bc87213619054f18fcc8462641767e82a7eafJiwen 'Steve' Cai const uint64_t mUsage; // AHARDWAREBUFFER_USAGE_* flags. 782f1a4737c997d9589447a51974e3aaecafee2937Jiwen 'Steve' Cai BufferItem* mBuffer; 792f1a4737c997d9589447a51974e3aaecafee2937Jiwen 'Steve' Cai std::unique_ptr<CpuConsumer::LockedBuffer> mLockedBuffer; 80c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh const int64_t mTimestamp; 81c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh const int32_t mWidth; 82c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh const int32_t mHeight; 83c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh const int32_t mNumPlanes; 84c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh bool mIsClosed = false; 85c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh mutable Mutex mLock; 86c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh}; 87c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh 88c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#endif // _NDK_IMAGE_PRIV_H 89