/* * Copyright 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 ANDROID_UI_GRALLOC1_H #define ANDROID_UI_GRALLOC1_H #define GRALLOC1_LOG_TAG "Gralloc1" #include #include namespace std { template <> struct hash { size_t operator()(gralloc1_capability_t capability) const { return std::hash()(static_cast(capability)); } }; } namespace android { class Fence; class GraphicBuffer; namespace Gralloc1 { class Device; class Descriptor { public: Descriptor(Device& device, gralloc1_buffer_descriptor_t deviceId) : mShimDevice(device), mDeviceId(deviceId), mWidth(0), mHeight(0), mFormat(static_cast(0)), mProducerUsage(GRALLOC1_PRODUCER_USAGE_NONE), mConsumerUsage(GRALLOC1_CONSUMER_USAGE_NONE) {} ~Descriptor(); gralloc1_buffer_descriptor_t getDeviceId() const { return mDeviceId; } gralloc1_error_t setDimensions(uint32_t width, uint32_t height); gralloc1_error_t setFormat(android_pixel_format_t format); gralloc1_error_t setProducerUsage(gralloc1_producer_usage_t usage); gralloc1_error_t setConsumerUsage(gralloc1_consumer_usage_t usage); private: Device& mShimDevice; const gralloc1_buffer_descriptor_t mDeviceId; uint32_t mWidth; uint32_t mHeight; android_pixel_format_t mFormat; gralloc1_producer_usage_t mProducerUsage; gralloc1_consumer_usage_t mConsumerUsage; }; // Descriptor class Device { friend class Gralloc1::Descriptor; public: Device(gralloc1_device_t* device); bool hasCapability(gralloc1_capability_t capability) const; std::string dump(); std::shared_ptr createDescriptor(); gralloc1_error_t getStride(buffer_handle_t buffer, uint32_t* outStride); gralloc1_error_t allocate( const std::vector>& descriptors, std::vector* outBuffers); gralloc1_error_t allocate( const std::shared_ptr& descriptor, gralloc1_backing_store_t id, buffer_handle_t* outBuffer); gralloc1_error_t retain(buffer_handle_t buffer); gralloc1_error_t retain(const GraphicBuffer* buffer); gralloc1_error_t release(buffer_handle_t buffer); gralloc1_error_t getNumFlexPlanes(buffer_handle_t buffer, uint32_t* outNumPlanes); gralloc1_error_t lock(buffer_handle_t buffer, gralloc1_producer_usage_t producerUsage, gralloc1_consumer_usage_t consumerUsage, const gralloc1_rect_t* accessRegion, void** outData, const sp& acquireFence); gralloc1_error_t lockFlex(buffer_handle_t buffer, gralloc1_producer_usage_t producerUsage, gralloc1_consumer_usage_t consumerUsage, const gralloc1_rect_t* accessRegion, struct android_flex_layout* outData, const sp& acquireFence); gralloc1_error_t lockYCbCr(buffer_handle_t buffer, gralloc1_producer_usage_t producerUsage, gralloc1_consumer_usage_t consumerUsage, const gralloc1_rect_t* accessRegion, struct android_ycbcr* outData, const sp& acquireFence); gralloc1_error_t unlock(buffer_handle_t buffer, sp* outFence); private: std::unordered_set loadCapabilities(); bool loadFunctions(); template gralloc1_error_t lockHelper(LockType pfn, buffer_handle_t buffer, gralloc1_producer_usage_t producerUsage, gralloc1_consumer_usage_t consumerUsage, const gralloc1_rect_t* accessRegion, OutType* outData, const sp& acquireFence) { int32_t intError = pfn(mDevice, buffer, static_cast(producerUsage), static_cast(consumerUsage), accessRegion, outData, acquireFence->dup()); return static_cast(intError); } gralloc1_device_t* const mDevice; const std::unordered_set mCapabilities; template struct FunctionLoader { FunctionLoader() : pfn(nullptr) {} bool load(gralloc1_device_t* device, bool errorIfNull) { gralloc1_function_pointer_t rawPointer = device->getFunction(device, descriptor); pfn = reinterpret_cast(rawPointer); if (errorIfNull && !rawPointer) { ALOG(LOG_ERROR, GRALLOC1_LOG_TAG, "Failed to load function pointer %d", descriptor); } return rawPointer != nullptr; } template typename std::result_of::type operator()(Args... args) { return pfn(args...); } PFN pfn; }; // Function pointers struct Functions { FunctionLoader dump; FunctionLoader createDescriptor; FunctionLoader destroyDescriptor; FunctionLoader setConsumerUsage; FunctionLoader setDimensions; FunctionLoader setFormat; FunctionLoader setProducerUsage; FunctionLoader getBackingStore; FunctionLoader getConsumerUsage; FunctionLoader getDimensions; FunctionLoader getFormat; FunctionLoader getProducerUsage; FunctionLoader getStride; FunctionLoader allocate; FunctionLoader retain; FunctionLoader release; FunctionLoader getNumFlexPlanes; FunctionLoader lock; FunctionLoader lockFlex; FunctionLoader lockYCbCr; FunctionLoader unlock; // Adapter-only functions FunctionLoader retainGraphicBuffer; FunctionLoader allocateWithId; } mFunctions; }; // class android::Gralloc1::Device class Loader { public: Loader(); ~Loader(); std::unique_ptr getDevice(); private: static std::unique_ptr mAdapter; std::unique_ptr mDevice; }; } // namespace android::Gralloc1 } // namespace android #endif