• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef LOG_TAG
20 #warning "GrallocLoader.h included without LOG_TAG"
21 #endif
22 
23 #include <memory>
24 
25 #include <allocator-hal/2.0/Allocator.h>
26 #include <allocator-passthrough/2.0/Gralloc0Hal.h>
27 #include <allocator-passthrough/2.0/Gralloc1Hal.h>
28 #include <hardware/gralloc.h>
29 #include <hardware/hardware.h>
30 #include <log/log.h>
31 
32 namespace android {
33 namespace hardware {
34 namespace graphics {
35 namespace allocator {
36 namespace V2_0 {
37 namespace passthrough {
38 
39 class GrallocLoader {
40    public:
load()41     static IAllocator* load() {
42         const hw_module_t* module = loadModule();
43         if (!module) {
44             return nullptr;
45         }
46         auto hal = createHal(module);
47         if (!hal) {
48             return nullptr;
49         }
50         return createAllocator(std::move(hal));
51     }
52 
53     // load the gralloc module
loadModule()54     static const hw_module_t* loadModule() {
55         const hw_module_t* module;
56         int error = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
57         if (error) {
58             ALOGE("failed to get gralloc module");
59             return nullptr;
60         }
61 
62         return module;
63     }
64 
65     // return the major api version of the module
getModuleMajorApiVersion(const hw_module_t * module)66     static int getModuleMajorApiVersion(const hw_module_t* module) {
67         return (module->module_api_version >> 8) & 0xff;
68     }
69 
70     // create an AllocatorHal instance
createHal(const hw_module_t * module)71     static std::unique_ptr<hal::AllocatorHal> createHal(const hw_module_t* module) {
72         int major = getModuleMajorApiVersion(module);
73         switch (major) {
74             case 1: {
75                 auto hal = std::make_unique<Gralloc1Hal>();
76                 return hal->initWithModule(module) ? std::move(hal) : nullptr;
77             }
78             case 0: {
79                 auto hal = std::make_unique<Gralloc0Hal>();
80                 return hal->initWithModule(module) ? std::move(hal) : nullptr;
81             }
82             default:
83                 ALOGE("unknown gralloc module major version %d", major);
84                 return nullptr;
85         }
86     }
87 
88     // create an IAllocator instance
createAllocator(std::unique_ptr<hal::AllocatorHal> hal)89     static IAllocator* createAllocator(std::unique_ptr<hal::AllocatorHal> hal) {
90         auto allocator = std::make_unique<hal::Allocator>();
91         return allocator->init(std::move(hal)) ? allocator.release() : nullptr;
92     }
93 };
94 
95 }  // namespace passthrough
96 }  // namespace V2_0
97 }  // namespace allocator
98 }  // namespace graphics
99 }  // namespace hardware
100 }  // namespace android
101