core_interface.cpp revision 4a1efd0680d2e9b61739e1eaeffd89174d6d2605
1/*
2* Copyright (c) 2014 - 2015, The Linux Foundation. All rights reserved.
3*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7*     * Redistributions of source code must retain the above copyright
8*       notice, this list of conditions and the following disclaimer.
9*     * Redistributions in binary form must reproduce the above
10*       copyright notice, this list of conditions and the following
11*       disclaimer in the documentation and/or other materials provided
12*       with the distribution.
13*     * Neither the name of The Linux Foundation nor the names of its
14*       contributors may be used to endorse or promote products derived
15*       from this software without specific prior written permission.
16*
17* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
30#include <utils/locker.h>
31#include <utils/constants.h>
32#include <utils/debug.h>
33#include <core/buffer_sync_handler.h>
34
35#include "core_impl.h"
36
37#define __CLASS__ "CoreInterface"
38
39#define GET_REVISION(version) (version >> 16)
40#define GET_DATA_ALIGNMENT(version) ((version >> 8) & 0xFF)
41#define GET_INSTRUCTION_SET(version) (version & 0xFF)
42
43namespace sdm {
44
45// Currently, we support only one client and one session for display core. So, create a global
46// singleton core object.
47struct CoreSingleton {
48  CoreSingleton() : core_impl(NULL) { }
49
50  CoreImpl *core_impl;
51  Locker locker;
52} g_core;
53
54// TODO(user): Have a single structure handle carries all the interface pointers.
55DisplayError CoreInterface::CreateCore(DebugHandler *debug_handler,
56                                       BufferAllocator *buffer_allocator,
57                                       BufferSyncHandler *buffer_sync_handler,
58                                       CoreInterface **interface, uint32_t client_version) {
59  return CreateCore(debug_handler, buffer_allocator, buffer_sync_handler, NULL,
60                    interface, client_version);
61}
62
63DisplayError CoreInterface::CreateCore(DebugHandler *debug_handler,
64                                       BufferAllocator *buffer_allocator,
65                                       BufferSyncHandler *buffer_sync_handler,
66                                       SocketHandler *socket_handler,
67                                       CoreInterface **interface, uint32_t client_version) {
68  SCOPE_LOCK(g_core.locker);
69
70  if (!debug_handler || !buffer_allocator || !buffer_sync_handler || !interface) {
71    return kErrorParameters;
72  }
73
74  // Check compatibility of client and core.
75  uint32_t lib_version = SDM_VERSION_TAG;
76  if (GET_REVISION(client_version) > GET_REVISION(lib_version)) {
77    return kErrorVersion;
78  } else if (GET_DATA_ALIGNMENT(client_version) != GET_DATA_ALIGNMENT(lib_version)) {
79    return kErrorDataAlignment;
80  } else if (GET_INSTRUCTION_SET(client_version) != GET_INSTRUCTION_SET(lib_version)) {
81    return kErrorInstructionSet;
82  }
83
84  CoreImpl *&core_impl = g_core.core_impl;
85  if (core_impl) {
86    return kErrorUndefined;
87  }
88
89  Debug::SetDebugHandler(debug_handler);
90
91  // Create appropriate CoreImpl object based on client version.
92  if (GET_REVISION(client_version) == CoreImpl::kRevision) {
93    core_impl = new CoreImpl(buffer_allocator, buffer_sync_handler, socket_handler);
94  } else {
95    return kErrorNotSupported;
96  }
97
98  if (!core_impl) {
99    return kErrorMemory;
100  }
101
102  DisplayError error = core_impl->Init();
103  if (error != kErrorNone) {
104    delete core_impl;
105    core_impl = NULL;
106    return error;
107  }
108
109  *interface = core_impl;
110  DLOGI("Open interface handle = %p", *interface);
111
112  return kErrorNone;
113}
114
115DisplayError CoreInterface::DestroyCore() {
116  SCOPE_LOCK(g_core.locker);
117
118  DLOGI("Close handle");
119
120  CoreImpl *&core_impl = g_core.core_impl;
121  if (!core_impl) {
122    return kErrorUndefined;
123  }
124
125  core_impl->Deinit();
126  delete core_impl;
127  core_impl = NULL;
128
129  return kErrorNone;
130}
131
132}  // namespace sdm
133
134