1 /* 2 * Copyright 2017 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 #pragma once 18 19 #include <memory> 20 21 #include <log/log.h> 22 #include <mapper-hal/2.1/Mapper.h> 23 #include <mapper-hal/2.1/MapperHal.h> 24 #include <mapper-passthrough/2.0/GrallocLoader.h> 25 #include <mapper-passthrough/2.1/Gralloc0Hal.h> 26 #include <mapper-passthrough/2.1/Gralloc1Hal.h> 27 28 namespace android { 29 namespace hardware { 30 namespace graphics { 31 namespace mapper { 32 namespace V2_1 { 33 namespace passthrough { 34 35 class GrallocLoader : public V2_0::passthrough::GrallocLoader { 36 public: load()37 static IMapper* load() { 38 const hw_module_t* module = loadModule(); 39 if (!module) { 40 return nullptr; 41 } 42 auto hal = createHal(module); 43 if (!hal) { 44 return nullptr; 45 } 46 return createMapper(std::move(hal)); 47 } 48 49 // create a MapperHal instance createHal(const hw_module_t * module)50 static std::unique_ptr<hal::MapperHal> createHal(const hw_module_t* module) { 51 int major = getModuleMajorApiVersion(module); 52 switch (major) { 53 case 1: { 54 auto hal = std::make_unique<Gralloc1Hal>(); 55 return hal->initWithModule(module) ? std::move(hal) : nullptr; 56 } 57 case 0: { 58 auto hal = std::make_unique<Gralloc0Hal>(); 59 return hal->initWithModule(module) ? std::move(hal) : nullptr; 60 } 61 default: 62 ALOGE("unknown gralloc module major version %d", major); 63 return nullptr; 64 } 65 } 66 67 // create an IAllocator instance createMapper(std::unique_ptr<hal::MapperHal> hal)68 static IMapper* createMapper(std::unique_ptr<hal::MapperHal> hal) { 69 auto mapper = std::make_unique<V2_0::passthrough::GrallocMapper<hal::Mapper>>(); 70 return mapper->init(std::move(hal)) ? mapper.release() : nullptr; 71 } 72 }; 73 74 } // namespace passthrough 75 } // namespace V2_1 76 } // namespace mapper 77 } // namespace graphics 78 } // namespace hardware 79 } // namespace android 80