summaryrefslogtreecommitdiffstats
path: root/widget/gonk/nativewindow/FakeSurfaceComposer.cpp
blob: 7e4a2a9d865970e660d9febda6d2a7652a8d9429 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/*
 * Copyright (C) 2007 The Android Open Source Project
 * Copyright (C) 2013 Mozilla Foundation
 *
 * 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.
 */

#define LOG_TAG "FakeSurfaceComposer"
//#define LOG_NDEBUG 0

#include <stdint.h>
#include <sys/types.h>
#include <errno.h>
#include <cutils/atomic.h>
#include <cutils/log.h>
#include <cutils/properties.h>
#include <private/android_filesystem_config.h>

#include <gui/IDisplayEventConnection.h>
#include <gui/GraphicBufferAlloc.h>
#include <gui/Surface.h>
#include <ui/DisplayInfo.h>

#if ANDROID_VERSION >= 21
#include <ui/Rect.h>
#endif

#include "../libdisplay/GonkDisplay.h"
#include "../nsScreenManagerGonk.h"
#include "FakeSurfaceComposer.h"
#include "gfxPrefs.h"
#include "MainThreadUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/layers/CompositorBridgeParent.h"
#include "mozilla/layers/CompositorThread.h"
#include "nsProxyRelease.h"
#include "nsThreadUtils.h"

using namespace mozilla;

namespace android {

/* static */
void FakeSurfaceComposer::instantiate() {
    defaultServiceManager()->addService(
            String16("SurfaceFlinger"), new FakeSurfaceComposer());
}

FakeSurfaceComposer::FakeSurfaceComposer()
    : BnSurfaceComposer()
{
}

FakeSurfaceComposer::~FakeSurfaceComposer()
{
}

status_t FakeSurfaceComposer::onTransact(uint32_t code, const Parcel& data,
                                         Parcel* reply, uint32_t flags)
{
    switch (code) {
        case CREATE_CONNECTION:
        case CREATE_DISPLAY:
        case SET_TRANSACTION_STATE:
        case CAPTURE_SCREEN:
        {
            // codes that require permission check
            IPCThreadState* ipc = IPCThreadState::self();
            const int pid = ipc->getCallingPid();
            const int uid = ipc->getCallingUid();
            // Accept request only when uid is root.
            if (uid != AID_ROOT) {
                ALOGE("Permission Denial: "
                        "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
                return PERMISSION_DENIED;
            }
            break;
        }
    }

    return BnSurfaceComposer::onTransact(code, data, reply, flags);
}

sp<ISurfaceComposerClient> FakeSurfaceComposer::createConnection()
{
    return nullptr;
}

sp<IGraphicBufferAlloc> FakeSurfaceComposer::createGraphicBufferAlloc()
{
    sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
    return gba;
}

class DestroyDisplayRunnable : public Runnable {
public:
    DestroyDisplayRunnable(FakeSurfaceComposer* aComposer, ssize_t aIndex)
        : mComposer(aComposer), mIndex(aIndex) { }
    NS_IMETHOD Run() override {
        MOZ_ASSERT(NS_IsMainThread(), "Must be on main thread.");
        Mutex::Autolock _l(mComposer->mStateLock);
        RefPtr<nsScreenManagerGonk> screenManager =
            nsScreenManagerGonk::GetInstance();
        screenManager->RemoveScreen(GonkDisplay::DISPLAY_VIRTUAL);
        mComposer->mDisplays.removeItemsAt(mIndex);
        return NS_OK;
    }
    sp<FakeSurfaceComposer> mComposer;
    ssize_t mIndex;
};

sp<IBinder> FakeSurfaceComposer::createDisplay(const String8& displayName,
        bool secure)
{
#if ANDROID_VERSION >= 19
    class DisplayToken : public BBinder {
        sp<FakeSurfaceComposer> composer;
        virtual ~DisplayToken() {
            MOZ_ASSERT(NS_IsMainThread(), "Must be on main thread.");
            // no more references, this display must be terminated
            Mutex::Autolock _l(composer->mStateLock);
            ssize_t idx = composer->mDisplays.indexOfKey(this);
            if (idx >= 0) {
                nsCOMPtr<nsIRunnable> task(new DestroyDisplayRunnable(composer.get(), idx));
                NS_DispatchToMainThread(task);
            }
        }
    public:
        DisplayToken(const sp<FakeSurfaceComposer>& composer)
            : composer(composer) {
        }
    };

    sp<BBinder> token = new DisplayToken(this);

    Mutex::Autolock _l(mStateLock);
    DisplayDeviceState info(HWC_DISPLAY_VIRTUAL);
    info.displayName = displayName;
    info.displayId = GonkDisplay::DISPLAY_VIRTUAL;
    info.isSecure = secure;
    mDisplays.add(token, info);
    return token;
#else
    return nullptr;
#endif
}

#if ANDROID_VERSION >= 19
void FakeSurfaceComposer::destroyDisplay(const sp<IBinder>& display)
{
    Mutex::Autolock _l(mStateLock);

    ssize_t idx = mDisplays.indexOfKey(display);
    if (idx < 0) {
        ALOGW("destroyDisplay: invalid display token");
        return;
    }

    nsCOMPtr<nsIRunnable> task(new DestroyDisplayRunnable(this, idx));
    NS_DispatchToMainThread(task);
}
#endif

sp<IBinder> FakeSurfaceComposer::getBuiltInDisplay(int32_t id)
{
    // support only primary display
    if (uint32_t(id) != HWC_DISPLAY_PRIMARY) {
        return NULL;
    }

    if (!mPrimaryDisplay.get()) {
        mPrimaryDisplay = new BBinder();
    }
    return mPrimaryDisplay;
}

void FakeSurfaceComposer::setTransactionState(
        const Vector<ComposerState>& state,
        const Vector<DisplayState>& displays,
        uint32_t flags)
{
    Mutex::Autolock _l(mStateLock);
    size_t count = displays.size();
    for (size_t i=0 ; i<count ; i++) {
        const DisplayState& s(displays[i]);
        setDisplayStateLocked(s);
    }
}

uint32_t FakeSurfaceComposer::setDisplayStateLocked(const DisplayState& s)
{
    ssize_t dpyIdx = mDisplays.indexOfKey(s.token);
    if (dpyIdx < 0) {
        return 0;
    }

    uint32_t flags = 0;
    DisplayDeviceState& disp(mDisplays.editValueAt(dpyIdx));

    if (!disp.isValid()) {
        return 0;
    }

    const uint32_t what = s.what;
    if (what & DisplayState::eSurfaceChanged) {
        if (disp.surface->asBinder() != s.surface->asBinder()) {
            disp.surface = s.surface;
            flags |= eDisplayTransactionNeeded;
        }
    }
    if (what & DisplayState::eLayerStackChanged) {
        if (disp.layerStack != s.layerStack) {
            disp.layerStack = s.layerStack;
            flags |= eDisplayTransactionNeeded;
        }
    }
    if (what & DisplayState::eDisplayProjectionChanged) {
        if (disp.orientation != s.orientation) {
            disp.orientation = s.orientation;
            flags |= eDisplayTransactionNeeded;
        }
        if (disp.frame != s.frame) {
            disp.frame = s.frame;
            flags |= eDisplayTransactionNeeded;
        }
        if (disp.viewport != s.viewport) {
            disp.viewport = s.viewport;
            flags |= eDisplayTransactionNeeded;
        }
    }
#if ANDROID_VERSION >= 21
    if (what & DisplayState::eDisplaySizeChanged) {
        if (disp.width != s.width) {
            disp.width = s.width;
            flags |= eDisplayTransactionNeeded;
        }
        if (disp.height != s.height) {
            disp.height = s.height;
            flags |= eDisplayTransactionNeeded;
        }
    }
#endif

    if (what & DisplayState::eSurfaceChanged) {
        nsCOMPtr<nsIRunnable> runnable =
            NS_NewRunnableFunction([&]() {
                MOZ_ASSERT(NS_IsMainThread(), "Must be on main thread.");
                RefPtr<nsScreenManagerGonk> screenManager = nsScreenManagerGonk::GetInstance();
                screenManager->AddScreen(GonkDisplay::DISPLAY_VIRTUAL, disp.surface.get());
            });
        NS_DispatchToMainThread(runnable, NS_DISPATCH_SYNC);
    }

    return flags;
}

void FakeSurfaceComposer::bootFinished()
{
}

bool FakeSurfaceComposer::authenticateSurfaceTexture(
        const sp<IGraphicBufferProducer>& bufferProducer) const {
    return false;
}

sp<IDisplayEventConnection> FakeSurfaceComposer::createDisplayEventConnection() {
    return nullptr;
}

// ---------------------------------------------------------------------------
// Capture screen into an IGraphiBufferProducer
// ---------------------------------------------------------------------------

class Barrier {
public:
    inline Barrier() : state(CLOSED) { }
    inline ~Barrier() { }

    // Release any threads waiting at the Barrier.
    // Provides release semantics: preceding loads and stores will be visible
    // to other threads before they wake up.
    void open() {
        Mutex::Autolock _l(lock);
        state = OPENED;
        cv.broadcast();
    }

    // Reset the Barrier, so wait() will block until open() has been called.
    void close() {
        Mutex::Autolock _l(lock);
        state = CLOSED;
    }

    // Wait until the Barrier is OPEN.
    // Provides acquire semantics: no subsequent loads or stores will occur
    // until wait() returns.
    void wait() const {
        Mutex::Autolock _l(lock);
        while (state == CLOSED) {
            cv.wait(lock);
        }
    }
private:
    enum { OPENED, CLOSED };
    mutable     Mutex       lock;
    mutable     Condition   cv;
    volatile    int         state;
};

/* The code below is here to handle b/8734824
 *
 * We create a IGraphicBufferProducer wrapper that forwards all calls
 * to the calling binder thread, where they are executed. This allows
 * the calling thread to be reused (on the other side) and not
 * depend on having "enough" binder threads to handle the requests.
 *
 */

class GraphicProducerWrapper : public BBinder, public MessageHandler {
    sp<IGraphicBufferProducer> impl;
    sp<Looper> looper;
    status_t result;
    bool exitPending;
    bool exitRequested;
    mutable Barrier barrier;
    volatile int32_t memoryBarrier;
    uint32_t code;
    Parcel const* data;
    Parcel* reply;

    enum {
        MSG_API_CALL,
        MSG_EXIT
    };

    /*
     * this is called by our "fake" BpGraphicBufferProducer. We package the
     * data and reply Parcel and forward them to the calling thread.
     */
    virtual status_t transact(uint32_t code,
            const Parcel& data, Parcel* reply, uint32_t flags) {
        this->code = code;
        this->data = &data;
        this->reply = reply;
        android_atomic_acquire_store(0, &memoryBarrier);
        if (exitPending) {
            // if we've exited, we run the message synchronously right here
            handleMessage(Message(MSG_API_CALL));
        } else {
            barrier.close();
            looper->sendMessage(this, Message(MSG_API_CALL));
            barrier.wait();
        }
        return NO_ERROR;
    }

    /*
     * here we run on the binder calling thread. All we've got to do is
     * call the real BpGraphicBufferProducer.
     */
    virtual void handleMessage(const Message& message) {
        android_atomic_release_load(&memoryBarrier);
        if (message.what == MSG_API_CALL) {
            impl->asBinder()->transact(code, data[0], reply);
            barrier.open();
        } else if (message.what == MSG_EXIT) {
            exitRequested = true;
        }
    }

public:
    GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
        impl(impl), looper(new Looper(true)), result(NO_ERROR),
        exitPending(false), exitRequested(false) {
    }

    status_t waitForResponse() {
        do {
            looper->pollOnce(-1);
        } while (!exitRequested);
        return result;
    }

    void exit(status_t result) {
        this->result = result;
        exitPending = true;
        looper->sendMessage(this, Message(MSG_EXIT));
    }
};

status_t
FakeSurfaceComposer::captureScreen(const sp<IBinder>& display
                                 , const sp<IGraphicBufferProducer>& producer
#if ANDROID_VERSION >= 21
                                 , Rect sourceCrop
#endif
                                 , uint32_t reqWidth
                                 , uint32_t reqHeight
                                 , uint32_t minLayerZ
                                 , uint32_t maxLayerZ
#if ANDROID_VERSION >= 21
                                 , bool useIdentityTransform
                                 , Rotation rotation
#elif ANDROID_VERSION < 19
                                 , bool isCpuConsumer
#endif
                                  )
{
    if (display == 0 || producer == 0) {
        return BAD_VALUE;
    }

    // Limit only to primary display
    if (display != mPrimaryDisplay) {
        return BAD_VALUE;
    }

    // this creates a "fake" BBinder which will serve as a "fake" remote
    // binder to receive the marshaled calls and forward them to the
    // real remote (a BpGraphicBufferProducer)
    sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
    // the asInterface() call below creates our "fake" BpGraphicBufferProducer
    // which does the marshaling work forwards to our "fake remote" above.
    sp<IGraphicBufferProducer> fakeProducer = IGraphicBufferProducer::asInterface(wrapper);

    nsCOMPtr<nsIRunnable> runnable =
        NS_NewRunnableFunction([&]() {
            captureScreenImp(fakeProducer, reqWidth, reqHeight, wrapper.get());
        });
    NS_DispatchToMainThread(runnable);

    status_t result = wrapper->waitForResponse();

    return result;
}

class RunnableCallTask final : public Runnable
{
public:
    explicit RunnableCallTask(nsIRunnable* aRunnable)
        : mRunnable(aRunnable) {}

    NS_IMETHOD Run() override
    {
        return mRunnable->Run();
    }
protected:
    nsCOMPtr<nsIRunnable> mRunnable;
};

void
FakeSurfaceComposer::captureScreenImp(const sp<IGraphicBufferProducer>& producer,
                                      uint32_t reqWidth,
                                      uint32_t reqHeight,
                                      const sp<GraphicProducerWrapper>& wrapper)
{
    MOZ_ASSERT(NS_IsMainThread());
    MOZ_ASSERT(wrapper.get());

    RefPtr<nsScreenGonk> screen = nsScreenManagerGonk::GetPrimaryScreen();

    // get screen geometry
    nsIntRect screenBounds = screen->GetNaturalBounds().ToUnknownRect();
    const uint32_t hw_w = screenBounds.width;
    const uint32_t hw_h = screenBounds.height;

    if (reqWidth > hw_w || reqHeight > hw_h) {
        ALOGE("size mismatch (%d, %d) > (%d, %d)",
                reqWidth, reqHeight, hw_w, hw_h);
        static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(BAD_VALUE);
        return;
    }

    reqWidth  = (!reqWidth)  ? hw_w : reqWidth;
    reqHeight = (!reqHeight) ? hw_h : reqHeight;

    nsCOMPtr<nsIRunnable> runnable =
        NS_NewRunnableFunction([screen, reqWidth, reqHeight, producer, wrapper]() {
            // create a surface (because we're a producer, and we need to
            // dequeue/queue a buffer)
            sp<Surface> sur = new Surface(producer);
            ANativeWindow* window = sur.get();
            // The closure makes screen const and we can't call forget() on it.
            RefPtr<nsScreenGonk> screenAlias = screen;

            if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) != NO_ERROR) {
                static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(BAD_VALUE);
                NS_ReleaseOnMainThread(screenAlias.forget());
                return;
            }
            uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
                             GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;

            int err = 0;
            err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
            err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
            err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
            err |= native_window_set_usage(window, usage);

            status_t result = NO_ERROR;
            if (err == NO_ERROR) {
                ANativeWindowBuffer* buffer;
                result = native_window_dequeue_buffer_and_wait(window,  &buffer);
                if (result == NO_ERROR) {
                    nsresult rv = screen->MakeSnapshot(buffer);
                    if (rv != NS_OK) {
                        result = INVALID_OPERATION;
                    }
                    window->queueBuffer(window, buffer, -1);
                }
            } else {
                result = BAD_VALUE;
            }
            native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
            static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
            NS_ReleaseOnMainThread(screenAlias.forget());
        });

    layers::CompositorThreadHolder::Loop()->PostTask(
        MakeAndAddRef<RunnableCallTask>(runnable));
}

#if ANDROID_VERSION >= 21
void
FakeSurfaceComposer::setPowerMode(const sp<IBinder>& display, int mode)
{
}

status_t
FakeSurfaceComposer::getDisplayConfigs(const sp<IBinder>& display, Vector<DisplayInfo>* configs)
{
    if (configs == NULL) {
        return BAD_VALUE;
    }

    // Limit DisplayConfigs only to primary display
    if (!display.get() || display != mPrimaryDisplay) {
        return NAME_NOT_FOUND;
    }

    configs->clear();
    DisplayInfo info = DisplayInfo();

    nsCOMPtr<nsIRunnable> runnable =
        NS_NewRunnableFunction([&]() {
            MOZ_ASSERT(NS_IsMainThread());
            getPrimaryDisplayInfo(&info);
        });
    NS_DispatchToMainThread(runnable, NS_DISPATCH_SYNC);

    configs->push_back(info);
    return NO_ERROR;
}

status_t
FakeSurfaceComposer::getDisplayStats(const sp<IBinder>& display, DisplayStatInfo* stats)
{
    return INVALID_OPERATION;
}

int
FakeSurfaceComposer::getActiveConfig(const sp<IBinder>& display)
{
   // Only support primary display.
   if (display.get() && (display == mPrimaryDisplay)) {
       return 0;
   }
   return INVALID_OPERATION;
}

status_t
FakeSurfaceComposer::setActiveConfig(const sp<IBinder>& display, int id)
{
    return INVALID_OPERATION;
}

status_t
FakeSurfaceComposer::clearAnimationFrameStats()
{
    return INVALID_OPERATION;
}

status_t
FakeSurfaceComposer::getAnimationFrameStats(FrameStats* outStats) const
{
    return INVALID_OPERATION;
}
#else
void
FakeSurfaceComposer::blank(const sp<IBinder>& display)
{
}

void
FakeSurfaceComposer::unblank(const sp<IBinder>& display)
{
}

status_t
FakeSurfaceComposer::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info)
{
    if (info == NULL) {
        return BAD_VALUE;
    }

    // Limit DisplayConfigs only to primary display
    if (!display.get() || display != mPrimaryDisplay) {
        return NAME_NOT_FOUND;
    }

    nsCOMPtr<nsIRunnable> runnable =
        NS_NewRunnableFunction([&]() {
            MOZ_ASSERT(NS_IsMainThread());
            getPrimaryDisplayInfo(info);
        });
    NS_DispatchToMainThread(runnable, NS_DISPATCH_SYNC);

    return NO_ERROR;
}
#endif

#define VSYNC_EVENT_PHASE_OFFSET_NS 0
#define SF_VSYNC_EVENT_PHASE_OFFSET_NS 0

void
FakeSurfaceComposer::getPrimaryDisplayInfo(DisplayInfo* info)
{
    MOZ_ASSERT(NS_IsMainThread(), "Must be on main thread.");

    // Implementation mimic android SurfaceFlinger::getDisplayConfigs().

    class Density {
        static int getDensityFromProperty(char const* propName) {
            char property[PROPERTY_VALUE_MAX];
            int density = 0;
            if (property_get(propName, property, NULL) > 0) {
                density = atoi(property);
            }
            return density;
        }
    public:
        static int getEmuDensity() {
            return getDensityFromProperty("qemu.sf.lcd_density"); }
        static int getBuildDensity()  {
            return getDensityFromProperty("ro.sf.lcd_density"); }
    };

    RefPtr<nsScreenGonk> screen = nsScreenManagerGonk::GetPrimaryScreen();

    float xdpi = screen->GetDpi();
    float ydpi = screen->GetDpi();
    int fps = 60; // XXX set a value from hwc hal
    nsIntRect screenBounds = screen->GetNaturalBounds().ToUnknownRect();

    // The density of the device is provided by a build property
    float density = Density::getBuildDensity() / 160.0f;
    if (density == 0) {
        // the build doesn't provide a density -- this is wrong!
        // use xdpi instead
        ALOGE("ro.sf.lcd_density must be defined as a build property");
        density = xdpi / 160.0f;
    }
    info->density = density;
    info->orientation = screen->EffectiveScreenRotation();

    info->w = screenBounds.width;
    info->h = screenBounds.height;
    info->xdpi = xdpi;
    info->ydpi = ydpi;
    info->fps = fps;
#if ANDROID_VERSION >= 21
    info->appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;

    // This is how far in advance a buffer must be queued for
    // presentation at a given time.  If you want a buffer to appear
    // on the screen at time N, you must submit the buffer before
    // (N - presentationDeadline).
    //
    // Normally it's one full refresh period (to give SF a chance to
    // latch the buffer), but this can be reduced by configuring a
    // DispSync offset.  Any additional delays introduced by the hardware
    // composer or panel must be accounted for here.
    //
    // We add an additional 1ms to allow for processing time and
    // differences between the ideal and actual refresh rate.
    info->presentationDeadline =
            (1e9 / fps) - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
#endif
    // All non-virtual displays are currently considered secure.
    info->secure = true;
}

}; // namespace android