1/*
2// Copyright (c) 2014 Intel Corporation 
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#include <common/utils/HwcTrace.h>
17#include <Hwcomposer.h>
18#include <DisplayQuery.h>
19#include <common/observers/SoftVsyncObserver.h>
20#include <DummyDevice.h>
21
22namespace android {
23namespace intel {
24DummyDevice::DummyDevice(uint32_t disp, Hwcomposer& hwc)
25    : mInitialized(false),
26      mConnected(false),
27      mBlank(false),
28      mDisp(disp),
29      mHwc(hwc),
30      mVsyncObserver(NULL),
31      mName("Dummy")
32{
33    CTRACE();
34}
35
36DummyDevice::~DummyDevice()
37{
38    WARN_IF_NOT_DEINIT();
39}
40
41bool DummyDevice::prePrepare(hwc_display_contents_1_t *display)
42{
43    RETURN_FALSE_IF_NOT_INIT();
44
45    if (!display) {
46        return true;
47    }
48
49    // nothing need to do for dummy display
50    return true;
51}
52
53bool DummyDevice::prepare(hwc_display_contents_1_t *display)
54{
55    RETURN_FALSE_IF_NOT_INIT();
56
57    if (!display || mDisp >= DEVICE_VIRTUAL) {
58        return true;
59    }
60
61    // skip all layers composition on dummy display
62    if (display->flags & HWC_GEOMETRY_CHANGED) {
63        for (size_t i=0; i < display->numHwLayers-1; i++) {
64            hwc_layer_1 * player = &display->hwLayers[i];
65            player->compositionType = HWC_OVERLAY;
66            player->flags &= ~HWC_SKIP_LAYER;
67        }
68    }
69
70    return true;
71}
72
73bool DummyDevice::commit(hwc_display_contents_1_t *display, IDisplayContext *context)
74{
75    RETURN_FALSE_IF_NOT_INIT();
76
77    if (!display || !context)
78        return true;
79
80    // nothing need to do for dummy display
81    return true;
82}
83
84bool DummyDevice::vsyncControl(bool enabled)
85{
86    RETURN_FALSE_IF_NOT_INIT();
87    return mVsyncObserver->control(enabled);
88}
89
90bool DummyDevice::blank(bool blank)
91{
92    RETURN_FALSE_IF_NOT_INIT();
93
94    mBlank = blank;
95
96    return true;
97}
98
99bool DummyDevice::getDisplaySize(int *width, int *height)
100{
101    RETURN_FALSE_IF_NOT_INIT();
102    if (!width || !height) {
103        ELOGTRACE("invalid parameters");
104        return false;
105    }
106
107    // TODO: make this platform specifc
108    *width = 1280;//720;
109    *height = 720;//1280;
110    return true;
111}
112
113bool DummyDevice::getDisplayConfigs(uint32_t *configs,
114                                         size_t *numConfigs)
115{
116    RETURN_FALSE_IF_NOT_INIT();
117    if (!configs || !numConfigs) {
118        ELOGTRACE("invalid parameters");
119        return false;
120    }
121
122    if (!mConnected) {
123        ILOGTRACE("dummy device is not connected");
124        return false;
125    }
126
127    *configs = 0;
128    *numConfigs = 1;
129
130    return true;
131}
132
133bool DummyDevice::getDisplayAttributes(uint32_t configs,
134                                            const uint32_t *attributes,
135                                            int32_t *values)
136{
137    RETURN_FALSE_IF_NOT_INIT();
138
139    if ((configs > 0) || !attributes || !values) {
140        ELOGTRACE("invalid parameters");
141        return false;
142    }
143
144    if (!mConnected) {
145        ILOGTRACE("dummy device is not connected");
146        return false;
147    }
148
149    int i = 0;
150    while (attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE) {
151        switch (attributes[i]) {
152        case HWC_DISPLAY_VSYNC_PERIOD:
153            values[i] = 1e9 / 60;
154            break;
155        case HWC_DISPLAY_WIDTH:
156            values[i] = 1280;
157            break;
158        case HWC_DISPLAY_HEIGHT:
159            values[i] = 720;
160            break;
161        case HWC_DISPLAY_DPI_X:
162            values[i] = 0;
163            break;
164        case HWC_DISPLAY_DPI_Y:
165            values[i] = 0;
166            break;
167        default:
168            ELOGTRACE("unknown attribute %d", attributes[i]);
169            break;
170        }
171        i++;
172    }
173
174    return true;
175}
176
177bool DummyDevice::compositionComplete()
178{
179    RETURN_FALSE_IF_NOT_INIT();
180    return true;
181}
182
183bool DummyDevice::initialize()
184{
185    mInitialized = true;
186
187    mVsyncObserver = new SoftVsyncObserver(*this);
188    if (!mVsyncObserver || !mVsyncObserver->initialize()) {
189        DEINIT_AND_RETURN_FALSE("Failed to create Soft Vsync Observer");
190        mInitialized = false;
191    }
192
193    return mInitialized;
194}
195
196bool DummyDevice::isConnected() const
197{
198    return mConnected;
199}
200
201const char* DummyDevice::getName() const
202{
203    return "Dummy";
204}
205
206int DummyDevice::getType() const
207{
208    return mDisp;
209}
210
211void DummyDevice::onVsync(int64_t timestamp)
212{
213    if (!mConnected)
214        return;
215
216    mHwc.vsync(mDisp, timestamp);
217}
218
219void DummyDevice::dump(Dump& d)
220{
221    d.append("-------------------------------------------------------------\n");
222    d.append("Device Name: %s (%s)\n", mName,
223            mConnected ? "connected" : "disconnected");
224}
225
226void DummyDevice::deinitialize()
227{
228    DEINIT_AND_DELETE_OBJ(mVsyncObserver);
229    mInitialized = false;
230}
231
232bool DummyDevice::setPowerMode(int /*mode*/)
233{
234    return true;
235}
236
237int DummyDevice::getActiveConfig()
238{
239    return 0;
240}
241
242bool DummyDevice::setActiveConfig(int /*index*/)
243{
244    return false;
245}
246
247} // namespace intel
248} // namespace android
249