1/*
2 * Copyright (C) 2016 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#include <array>
18#include <unordered_set>
19#include <unordered_map>
20#include <gtest/gtest.h>
21#include <dlfcn.h>
22#include <android-base/unique_fd.h>
23#include <hardware/hardware.h>
24#include <sync/sync.h>
25#include <ui/GraphicTypes.h>
26
27#define HWC2_INCLUDE_STRINGIFICATION
28#define HWC2_USE_CPP11
29#include <hardware/hwcomposer2.h>
30#undef HWC2_INCLUDE_STRINGIFICATION
31#undef HWC2_USE_CPP11
32
33#include "Hwc2TestLayer.h"
34#include "Hwc2TestLayers.h"
35#include "Hwc2TestClientTarget.h"
36#include "Hwc2TestVirtualDisplay.h"
37
38using android::ui::ColorMode;
39using android::ui::Dataspace;
40
41void hwc2TestHotplugCallback(hwc2_callback_data_t callbackData,
42        hwc2_display_t display, int32_t connected);
43void hwc2TestVsyncCallback(hwc2_callback_data_t callbackData,
44        hwc2_display_t display, int64_t timestamp);
45
46class Hwc2Test : public testing::Test {
47public:
48
49    virtual void SetUp()
50    {
51        hw_module_t const* hwc2Module;
52
53        int err = hw_get_module(HWC_HARDWARE_MODULE_ID, &hwc2Module);
54        ASSERT_GE(err, 0) << "failed to get hwc hardware module: "
55                << strerror(-err);
56
57        /* The following method will fail if you have not run
58         * "adb shell stop" */
59        err = hwc2_open(hwc2Module, &mHwc2Device);
60        ASSERT_GE(err, 0) << "failed to open hwc hardware module: "
61                << strerror(-err);
62
63        populateDisplays();
64    }
65
66    virtual void TearDown()
67    {
68
69        for (auto itr = mLayers.begin(); itr != mLayers.end();) {
70            hwc2_display_t display = itr->first;
71            hwc2_layer_t layer = itr->second;
72            itr++;
73            /* Destroys and removes the layer from mLayers */
74            destroyLayer(display, layer);
75        }
76
77        for (auto itr = mActiveDisplays.begin(); itr != mActiveDisplays.end();) {
78            hwc2_display_t display = *itr;
79            itr++;
80            /* Sets power mode to off and removes the display from
81             * mActiveDisplays */
82            setPowerMode(display, HWC2_POWER_MODE_OFF);
83        }
84
85        for (auto itr = mVirtualDisplays.begin(); itr != mVirtualDisplays.end();) {
86            hwc2_display_t display = *itr;
87            itr++;
88            /* Destroys virtual displays */
89            destroyVirtualDisplay(display);
90        }
91
92        if (mHwc2Device)
93            hwc2_close(mHwc2Device);
94    }
95
96    void registerCallback(hwc2_callback_descriptor_t descriptor,
97            hwc2_callback_data_t callbackData, hwc2_function_pointer_t pointer,
98            hwc2_error_t* outErr = nullptr)
99    {
100        auto pfn = reinterpret_cast<HWC2_PFN_REGISTER_CALLBACK>(
101                getFunction(HWC2_FUNCTION_REGISTER_CALLBACK));
102        ASSERT_TRUE(pfn) << "failed to get function";
103
104        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, descriptor,
105                callbackData, pointer));
106        if (outErr) {
107            *outErr = err;
108        } else {
109            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to register callback";
110        }
111    }
112
113    void getDisplayType(hwc2_display_t display, hwc2_display_type_t* outType,
114            hwc2_error_t* outErr = nullptr)
115    {
116        auto pfn = reinterpret_cast<HWC2_PFN_GET_DISPLAY_TYPE>(
117                getFunction(HWC2_FUNCTION_GET_DISPLAY_TYPE));
118        ASSERT_TRUE(pfn) << "failed to get function";
119
120        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
121                    reinterpret_cast<int32_t*>(outType)));
122        if (outErr) {
123            *outErr = err;
124        } else {
125            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to get display type";
126        }
127    }
128
129    /* If the populateDisplays function is still receiving displays and the
130     * display is connected, the display handle is stored in mDisplays. */
131    void hotplugCallback(hwc2_display_t display, int32_t connected)
132    {
133        std::lock_guard<std::mutex> lock(mHotplugMutex);
134
135        if (mHotplugStatus != Hwc2TestHotplugStatus::Receiving)
136            return;
137
138        if (connected == HWC2_CONNECTION_CONNECTED)
139            mDisplays.insert(display);
140
141        mHotplugCv.notify_all();
142    }
143
144    void createLayer(hwc2_display_t display, hwc2_layer_t* outLayer,
145            hwc2_error_t* outErr = nullptr)
146    {
147        auto pfn = reinterpret_cast<HWC2_PFN_CREATE_LAYER>(
148                getFunction(HWC2_FUNCTION_CREATE_LAYER));
149        ASSERT_TRUE(pfn) << "failed to get function";
150
151        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
152                outLayer));
153
154        if (err == HWC2_ERROR_NONE)
155            mLayers.insert(std::make_pair(display, *outLayer));
156
157        if (outErr) {
158            *outErr = err;
159        } else {
160            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to create layer";
161        }
162    }
163
164    void destroyLayer(hwc2_display_t display, hwc2_layer_t layer,
165            hwc2_error_t* outErr = nullptr)
166    {
167        auto pfn = reinterpret_cast<HWC2_PFN_DESTROY_LAYER>(
168                getFunction(HWC2_FUNCTION_DESTROY_LAYER));
169        ASSERT_TRUE(pfn) << "failed to get function";
170
171        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, layer));
172
173        if (err == HWC2_ERROR_NONE)
174            mLayers.erase(std::make_pair(display, layer));
175
176        if (outErr) {
177            *outErr = err;
178        } else {
179            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to destroy layer "
180                    << layer;
181        }
182    }
183
184    void getDisplayAttribute(hwc2_display_t display, hwc2_config_t config,
185            hwc2_attribute_t attribute, int32_t* outValue,
186            hwc2_error_t* outErr = nullptr)
187    {
188        auto pfn = reinterpret_cast<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
189                getFunction(HWC2_FUNCTION_GET_DISPLAY_ATTRIBUTE));
190        ASSERT_TRUE(pfn) << "failed to get function";
191
192        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, config,
193                attribute, outValue));
194
195        if (outErr) {
196            *outErr = err;
197        } else {
198            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to get display attribute "
199                    << getAttributeName(attribute) << " for config " << config;
200        }
201    }
202
203    void getDisplayConfigs(hwc2_display_t display,
204            std::vector<hwc2_config_t>* outConfigs,
205            hwc2_error_t* outErr = nullptr)
206    {
207        auto pfn = reinterpret_cast<HWC2_PFN_GET_DISPLAY_CONFIGS>(
208                getFunction(HWC2_FUNCTION_GET_DISPLAY_CONFIGS));
209        ASSERT_TRUE(pfn) << "failed to get function";
210
211        uint32_t numConfigs = 0;
212
213        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
214                &numConfigs, nullptr));
215
216        if (err == HWC2_ERROR_NONE) {
217            outConfigs->resize(numConfigs);
218
219            err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
220                    &numConfigs, outConfigs->data()));
221        }
222
223        if (outErr) {
224            *outErr = err;
225        } else {
226            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to get configs for"
227                    " display " << display;
228        }
229    }
230
231    void getActiveConfig(hwc2_display_t display, hwc2_config_t* outConfig,
232            hwc2_error_t* outErr = nullptr)
233    {
234        auto pfn = reinterpret_cast<HWC2_PFN_GET_ACTIVE_CONFIG>(
235                getFunction(HWC2_FUNCTION_GET_ACTIVE_CONFIG));
236        ASSERT_TRUE(pfn) << "failed to get function";
237
238        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
239                outConfig));
240        if (outErr) {
241            *outErr = err;
242        } else {
243            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to get active config on"
244                    " display " << display;
245        }
246    }
247
248    void setActiveConfig(hwc2_display_t display, hwc2_config_t config,
249            hwc2_error_t* outErr = nullptr)
250    {
251        auto pfn = reinterpret_cast<HWC2_PFN_SET_ACTIVE_CONFIG>(
252                getFunction(HWC2_FUNCTION_SET_ACTIVE_CONFIG));
253        ASSERT_TRUE(pfn) << "failed to get function";
254
255        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, config));
256        if (outErr) {
257            *outErr = err;
258        } else {
259            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set active config "
260                    << config;
261        }
262    }
263
264    void getDozeSupport(hwc2_display_t display, int32_t* outSupport,
265            hwc2_error_t* outErr = nullptr)
266    {
267        auto pfn = reinterpret_cast<HWC2_PFN_GET_DOZE_SUPPORT>(
268                getFunction(HWC2_FUNCTION_GET_DOZE_SUPPORT));
269        ASSERT_TRUE(pfn) << "failed to get function";
270
271        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
272                outSupport));
273        if (outErr) {
274            *outErr = err;
275        } else {
276            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to get doze support on"
277                    " display " << display;
278        }
279    }
280
281    void setPowerMode(hwc2_display_t display, hwc2_power_mode_t mode,
282            hwc2_error_t* outErr = nullptr)
283    {
284        auto pfn = reinterpret_cast<HWC2_PFN_SET_POWER_MODE>(
285                getFunction(HWC2_FUNCTION_SET_POWER_MODE));
286        ASSERT_TRUE(pfn) << "failed to get function";
287
288        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
289                mode));
290        if (outErr) {
291            *outErr = err;
292            if (err != HWC2_ERROR_NONE)
293                return;
294        } else {
295            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set power mode "
296                    << getPowerModeName(mode) << " on display " << display;
297        }
298
299        if (mode == HWC2_POWER_MODE_OFF) {
300            mActiveDisplays.erase(display);
301        } else {
302            mActiveDisplays.insert(display);
303        }
304    }
305
306    void setVsyncEnabled(hwc2_display_t display, hwc2_vsync_t enabled,
307            hwc2_error_t* outErr = nullptr)
308    {
309        auto pfn = reinterpret_cast<HWC2_PFN_SET_VSYNC_ENABLED>(
310                getFunction(HWC2_FUNCTION_SET_VSYNC_ENABLED));
311        ASSERT_TRUE(pfn) << "failed to get function";
312
313        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
314                enabled));
315        if (outErr) {
316            *outErr = err;
317        } else {
318            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set vsync enabled "
319                    << getVsyncName(enabled);
320        }
321    }
322
323    void vsyncCallback(hwc2_display_t display, int64_t timestamp)
324    {
325        std::lock_guard<std::mutex> lock(mVsyncMutex);
326        mVsyncDisplay = display;
327        mVsyncTimestamp = timestamp;
328        mVsyncCv.notify_all();
329    }
330
331    void getDisplayName(hwc2_display_t display, std::string* outName,
332                hwc2_error_t* outErr = nullptr)
333    {
334        auto pfn = reinterpret_cast<HWC2_PFN_GET_DISPLAY_NAME>(
335                getFunction(HWC2_FUNCTION_GET_DISPLAY_NAME));
336        ASSERT_TRUE(pfn) << "failed to get function";
337
338        uint32_t size = 0;
339
340        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, &size,
341                nullptr));
342
343        if (err == HWC2_ERROR_NONE) {
344            std::vector<char> name(size);
345
346            err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, &size,
347                    name.data()));
348
349            outName->assign(name.data());
350        }
351
352        if (outErr) {
353            *outErr = err;
354        } else {
355            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to get display name for "
356                    << display;
357        }
358    }
359
360    void setLayerCompositionType(hwc2_display_t display, hwc2_layer_t layer,
361            hwc2_composition_t composition, hwc2_error_t* outErr = nullptr)
362    {
363        auto pfn = reinterpret_cast<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
364                getFunction(HWC2_FUNCTION_SET_LAYER_COMPOSITION_TYPE));
365        ASSERT_TRUE(pfn) << "failed to get function";
366
367        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, layer,
368                composition));
369        if (outErr) {
370            *outErr = err;
371        } else {
372            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set layer composition"
373                    " type " << getCompositionName(composition);
374        }
375    }
376
377    void setCursorPosition(hwc2_display_t display, hwc2_layer_t layer,
378            int32_t x, int32_t y, hwc2_error_t* outErr = nullptr)
379    {
380        auto pfn = reinterpret_cast<HWC2_PFN_SET_CURSOR_POSITION>(
381                getFunction(HWC2_FUNCTION_SET_CURSOR_POSITION));
382        ASSERT_TRUE(pfn) << "failed to get function";
383
384        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, layer, x,
385                y));
386        if (outErr) {
387            *outErr = err;
388        } else {
389            ASSERT_TRUE((err == HWC2_ERROR_NONE) ||
390                (err == HWC2_ERROR_BAD_LAYER)) <<
391                "failed to set cursor position";
392        }
393    }
394
395    void setLayerBlendMode(hwc2_display_t display, hwc2_layer_t layer,
396            hwc2_blend_mode_t mode, hwc2_error_t* outErr = nullptr)
397    {
398        auto pfn = reinterpret_cast<HWC2_PFN_SET_LAYER_BLEND_MODE>(
399                getFunction(HWC2_FUNCTION_SET_LAYER_BLEND_MODE));
400        ASSERT_TRUE(pfn) << "failed to get function";
401
402        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, layer,
403                mode));
404        if (outErr) {
405            *outErr = err;
406        } else {
407            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set layer blend mode "
408                    << getBlendModeName(mode);
409        }
410    }
411
412    void setLayerBuffer(hwc2_display_t display, hwc2_layer_t layer,
413            buffer_handle_t buffer, int32_t acquireFence,
414            hwc2_error_t* outErr = nullptr)
415    {
416        auto pfn = reinterpret_cast<HWC2_PFN_SET_LAYER_BUFFER>(
417                getFunction(HWC2_FUNCTION_SET_LAYER_BUFFER));
418        ASSERT_TRUE(pfn) << "failed to get function";
419
420        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, layer,
421                buffer, acquireFence));
422        if (outErr) {
423            *outErr = err;
424        } else {
425            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set layer buffer";
426        }
427    }
428
429    void setLayerColor(hwc2_display_t display, hwc2_layer_t layer,
430            hwc_color_t color, hwc2_error_t* outErr = nullptr)
431    {
432        auto pfn = reinterpret_cast<HWC2_PFN_SET_LAYER_COLOR>(
433                getFunction(HWC2_FUNCTION_SET_LAYER_COLOR));
434        ASSERT_TRUE(pfn) << "failed to get function";
435
436        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, layer,
437                color));
438        if (outErr) {
439            *outErr = err;
440        } else {
441            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set layer color";
442        }
443    }
444
445    void setLayerDataspace(hwc2_display_t display, hwc2_layer_t layer,
446            Dataspace dataspace, hwc2_error_t* outErr = nullptr)
447    {
448        auto pfn = reinterpret_cast<HWC2_PFN_SET_LAYER_DATASPACE>(
449                getFunction(HWC2_FUNCTION_SET_LAYER_DATASPACE));
450        ASSERT_TRUE(pfn) << "failed to get function";
451
452        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
453                layer, static_cast<int>(dataspace)));
454        if (outErr) {
455            *outErr = err;
456        } else {
457            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set layer dataspace";
458        }
459    }
460
461    void setLayerDisplayFrame(hwc2_display_t display, hwc2_layer_t layer,
462            const hwc_rect_t& displayFrame, hwc2_error_t* outErr = nullptr)
463    {
464        auto pfn = reinterpret_cast<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
465                getFunction(HWC2_FUNCTION_SET_LAYER_DISPLAY_FRAME));
466        ASSERT_TRUE(pfn) << "failed to get function";
467
468        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, layer,
469                displayFrame));
470        if (outErr) {
471            *outErr = err;
472        } else {
473            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set layer display"
474                    " frame";
475        }
476    }
477
478    void setLayerPlaneAlpha(hwc2_display_t display, hwc2_layer_t layer,
479            float alpha, hwc2_error_t* outErr = nullptr)
480    {
481        auto pfn = reinterpret_cast<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
482                getFunction(HWC2_FUNCTION_SET_LAYER_PLANE_ALPHA));
483        ASSERT_TRUE(pfn) << "failed to get function";
484
485        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, layer,
486                alpha));
487        if (outErr) {
488            *outErr = err;
489        } else {
490            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set layer plane alpha "
491                    << alpha;
492        }
493    }
494
495    void setLayerSourceCrop(hwc2_display_t display, hwc2_layer_t layer,
496            const hwc_frect_t& sourceCrop, hwc2_error_t* outErr = nullptr)
497    {
498        auto pfn = reinterpret_cast<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
499                getFunction(HWC2_FUNCTION_SET_LAYER_SOURCE_CROP));
500        ASSERT_TRUE(pfn) << "failed to get function";
501
502        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, layer,
503                sourceCrop));
504        if (outErr) {
505            *outErr = err;
506        } else {
507            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set layer source crop";
508        }
509    }
510
511    void setLayerSurfaceDamage(hwc2_display_t display, hwc2_layer_t layer,
512            const hwc_region_t& surfaceDamage, hwc2_error_t* outErr = nullptr)
513    {
514        auto pfn = reinterpret_cast<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
515                getFunction(HWC2_FUNCTION_SET_LAYER_SURFACE_DAMAGE));
516        ASSERT_TRUE(pfn) << "failed to get function";
517
518        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, layer,
519                surfaceDamage));
520        if (outErr) {
521            *outErr = err;
522        } else {
523            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set layer surface"
524                    " damage";
525        }
526    }
527
528    void setLayerTransform(hwc2_display_t display, hwc2_layer_t layer,
529            hwc_transform_t transform, hwc2_error_t* outErr = nullptr)
530    {
531        auto pfn = reinterpret_cast<HWC2_PFN_SET_LAYER_TRANSFORM>(
532                getFunction(HWC2_FUNCTION_SET_LAYER_TRANSFORM));
533        ASSERT_TRUE(pfn) << "failed to get function";
534
535        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, layer,
536                transform));
537        if (outErr) {
538            *outErr = err;
539        } else {
540            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set layer transform "
541                    << getTransformName(transform);
542        }
543    }
544
545    void setLayerVisibleRegion(hwc2_display_t display, hwc2_layer_t layer,
546            const hwc_region_t& visibleRegion, hwc2_error_t* outErr = nullptr)
547    {
548        auto pfn = reinterpret_cast<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
549                getFunction(HWC2_FUNCTION_SET_LAYER_VISIBLE_REGION));
550        ASSERT_TRUE(pfn) << "failed to get function";
551
552        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, layer,
553                visibleRegion));
554        if (outErr) {
555            *outErr = err;
556        } else {
557            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set layer visible"
558                    " region";
559        }
560    }
561
562    void setLayerZOrder(hwc2_display_t display, hwc2_layer_t layer,
563            uint32_t zOrder, hwc2_error_t* outErr = nullptr)
564    {
565        auto pfn = reinterpret_cast<HWC2_PFN_SET_LAYER_Z_ORDER>(
566                getFunction(HWC2_FUNCTION_SET_LAYER_Z_ORDER));
567        ASSERT_TRUE(pfn) << "failed to get function";
568
569        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, layer,
570                zOrder));
571        if (outErr) {
572            *outErr = err;
573        } else {
574            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set layer z order "
575                    << zOrder;
576        }
577    }
578
579    void validateDisplay(hwc2_display_t display, uint32_t* outNumTypes,
580            uint32_t* outNumRequests, hwc2_error_t* outErr)
581    {
582        auto pfn = reinterpret_cast<HWC2_PFN_VALIDATE_DISPLAY>(
583                getFunction(HWC2_FUNCTION_VALIDATE_DISPLAY));
584        ASSERT_TRUE(pfn) << "failed to get function";
585
586        *outErr = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
587                outNumTypes, outNumRequests));
588    }
589
590    void validateDisplay(hwc2_display_t display, uint32_t* outNumTypes,
591            uint32_t* outNumRequests, bool* outHasChanges)
592    {
593        hwc2_error_t err = HWC2_ERROR_NONE;
594
595        EXPECT_NO_FATAL_FAILURE(validateDisplay(display, outNumTypes,
596                outNumRequests, &err));
597
598        if (err != HWC2_ERROR_HAS_CHANGES) {
599            *outHasChanges = false;
600            EXPECT_EQ(err, HWC2_ERROR_NONE) << "failed to validate display";
601        } else {
602            *outHasChanges = true;
603        }
604    }
605
606    void getDisplayRequests(hwc2_display_t display,
607            hwc2_display_request_t* outDisplayRequests,
608            std::vector<hwc2_layer_t>* outLayers,
609            std::vector<hwc2_layer_request_t>* outLayerRequests,
610            hwc2_error_t* outErr = nullptr)
611    {
612        auto pfn = reinterpret_cast<HWC2_PFN_GET_DISPLAY_REQUESTS>(
613                getFunction(HWC2_FUNCTION_GET_DISPLAY_REQUESTS));
614        ASSERT_TRUE(pfn) << "failed to get function";
615
616        uint32_t numElements = 0;
617
618        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
619                reinterpret_cast<int32_t*>(outDisplayRequests), &numElements,
620                nullptr, nullptr));
621
622        if (err == HWC2_ERROR_NONE && numElements > 0) {
623            outLayers->resize(numElements);
624            outLayerRequests->resize(numElements);
625
626            err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
627                    reinterpret_cast<int32_t*>(outDisplayRequests), &numElements,
628                    reinterpret_cast<uint64_t*>(outLayers->data()),
629                    reinterpret_cast<int32_t*>(outLayerRequests->data())));
630        }
631
632        if (outErr) {
633            *outErr = err;
634        } else {
635            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to get display requests";
636        }
637    }
638
639    void handleRequests(hwc2_display_t display,
640            const std::vector<hwc2_layer_t>& layers, uint32_t numRequests,
641            std::set<hwc2_layer_t>* outClearLayers = nullptr,
642            bool* outFlipClientTarget = nullptr)
643    {
644        hwc2_display_request_t displayRequest =
645                static_cast<hwc2_display_request_t>(0);
646        std::vector<hwc2_layer_t> requestedLayers;
647        std::vector<hwc2_layer_request_t> requests;
648
649        ASSERT_NO_FATAL_FAILURE(getDisplayRequests(display, &displayRequest,
650                &requestedLayers, &requests));
651
652        EXPECT_EQ(numRequests, requests.size()) << "validate returned "
653                << numRequests << " requests and get display requests returned "
654                << requests.size() << " requests";
655
656        for (size_t i = 0; i < requests.size(); i++) {
657            hwc2_layer_t requestedLayer = requestedLayers.at(i);
658            hwc2_layer_request_t request = requests.at(i);
659
660            EXPECT_EQ(std::count(layers.begin(), layers.end(), requestedLayer),
661                    1) << "get display requests returned an unknown layer";
662            EXPECT_NE(request, 0) << "returned empty request for layer "
663                    << requestedLayer;
664
665            if (outClearLayers && request
666                    == HWC2_LAYER_REQUEST_CLEAR_CLIENT_TARGET)
667                outClearLayers->insert(requestedLayer);
668        }
669
670        if (outFlipClientTarget)
671            *outFlipClientTarget = displayRequest
672                    & HWC2_DISPLAY_REQUEST_FLIP_CLIENT_TARGET;
673    }
674
675    void getChangedCompositionTypes(hwc2_display_t display,
676            std::vector<hwc2_layer_t>* outLayers,
677            std::vector<hwc2_composition_t>* outTypes,
678            hwc2_error_t* outErr = nullptr)
679    {
680        auto pfn = reinterpret_cast<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
681                getFunction(HWC2_FUNCTION_GET_CHANGED_COMPOSITION_TYPES));
682        ASSERT_TRUE(pfn) << "failed to get function";
683
684        uint32_t numElements = 0;
685
686        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
687                &numElements, nullptr, nullptr));
688
689        if (err == HWC2_ERROR_NONE && numElements > 0) {
690            outLayers->resize(numElements);
691            outTypes->resize(numElements);
692
693            err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
694                    &numElements, reinterpret_cast<uint64_t*>(outLayers->data()),
695                    reinterpret_cast<int32_t*>(outTypes->data())));
696        }
697
698        if (outErr) {
699            *outErr = err;
700        } else {
701            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to get changed"
702                    " composition types";
703        }
704    }
705
706    void handleCompositionChanges(hwc2_display_t display,
707            const Hwc2TestLayers& testLayers,
708            const std::vector<hwc2_layer_t>& layers, uint32_t numTypes,
709            std::set<hwc2_layer_t>* outClientLayers = nullptr)
710    {
711        std::vector<hwc2_layer_t> changedLayers;
712        std::vector<hwc2_composition_t> types;
713
714        ASSERT_NO_FATAL_FAILURE(getChangedCompositionTypes(display,
715                &changedLayers, &types));
716
717        EXPECT_EQ(numTypes, types.size()) << "validate returned "
718                << numTypes << " types and get changed composition types"
719                " returned " << types.size() << " types";
720
721        for (size_t i = 0; i < types.size(); i++) {
722
723            auto layer = std::find(layers.begin(), layers.end(),
724                    changedLayers.at(i));
725
726            EXPECT_TRUE(layer != layers.end() || !testLayers.contains(*layer))
727                    << "get changed composition types returned an unknown layer";
728
729            hwc2_composition_t requestedType = testLayers.getComposition(*layer);
730            hwc2_composition_t returnedType = types.at(i);
731
732            EXPECT_NE(returnedType, HWC2_COMPOSITION_INVALID) << "get changed"
733                    " composition types returned invalid composition";
734
735            switch (requestedType) {
736            case HWC2_COMPOSITION_CLIENT:
737                EXPECT_TRUE(false) << getCompositionName(returnedType)
738                        << " cannot be changed";
739                break;
740            case HWC2_COMPOSITION_DEVICE:
741            case HWC2_COMPOSITION_SOLID_COLOR:
742                EXPECT_EQ(returnedType, HWC2_COMPOSITION_CLIENT)
743                        << "composition of type "
744                        << getCompositionName(requestedType)
745                        << " can only be changed to "
746                        << getCompositionName(HWC2_COMPOSITION_CLIENT);
747                break;
748            case HWC2_COMPOSITION_CURSOR:
749            case HWC2_COMPOSITION_SIDEBAND:
750                EXPECT_TRUE(returnedType == HWC2_COMPOSITION_CLIENT
751                        || returnedType == HWC2_COMPOSITION_DEVICE)
752                        << "composition of type "
753                        << getCompositionName(requestedType)
754                        << " can only be changed to "
755                        << getCompositionName(HWC2_COMPOSITION_CLIENT) << " or "
756                        << getCompositionName(HWC2_COMPOSITION_DEVICE);
757                break;
758            default:
759                EXPECT_TRUE(false) << "unknown type "
760                        << getCompositionName(requestedType);
761                break;
762            }
763
764            if (outClientLayers)
765                if (returnedType == HWC2_COMPOSITION_CLIENT)
766                    outClientLayers->insert(*layer);
767        }
768
769        if (outClientLayers) {
770            for (auto layer : layers) {
771                if (testLayers.getComposition(layer) == HWC2_COMPOSITION_CLIENT)
772                    outClientLayers->insert(layer);
773            }
774        }
775    }
776
777    void acceptDisplayChanges(hwc2_display_t display,
778            hwc2_error_t* outErr = nullptr)
779    {
780        auto pfn = reinterpret_cast<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
781                getFunction(HWC2_FUNCTION_ACCEPT_DISPLAY_CHANGES));
782        ASSERT_TRUE(pfn) << "failed to get function";
783
784        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display));
785        if (outErr) {
786            *outErr = err;
787        } else {
788            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to accept display changes";
789        }
790    }
791
792    void getClientTargetSupport(hwc2_display_t display, int32_t width,
793            int32_t height, android_pixel_format_t format,
794            Dataspace dataspace, hwc2_error_t* outErr = nullptr)
795    {
796        auto pfn = reinterpret_cast<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
797                getFunction(HWC2_FUNCTION_GET_CLIENT_TARGET_SUPPORT));
798        ASSERT_TRUE(pfn) << "failed to get function";
799
800        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, width,
801                height, format, static_cast<int>(dataspace)));
802        if (outErr) {
803            *outErr = err;
804        } else {
805            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to get client target"
806                    " support";
807        }
808    }
809
810    void setClientTarget(hwc2_display_t display, buffer_handle_t handle,
811            int32_t acquireFence, Dataspace dataspace,
812            hwc_region_t damage, hwc2_error_t* outErr = nullptr)
813    {
814        auto pfn = reinterpret_cast<HWC2_PFN_SET_CLIENT_TARGET>(
815                getFunction(HWC2_FUNCTION_SET_CLIENT_TARGET));
816        ASSERT_TRUE(pfn) << "failed to get function";
817
818        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, handle,
819                acquireFence, static_cast<int>(dataspace), damage));
820        if (outErr) {
821            *outErr = err;
822        } else {
823            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set client target";
824        }
825    }
826
827    void presentDisplay(hwc2_display_t display, int32_t* outPresentFence,
828            hwc2_error_t* outErr = nullptr)
829    {
830        auto pfn = reinterpret_cast<HWC2_PFN_PRESENT_DISPLAY>(
831                getFunction(HWC2_FUNCTION_PRESENT_DISPLAY));
832        ASSERT_TRUE(pfn) << "failed to get function";
833
834        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
835                outPresentFence));
836        if (outErr) {
837            *outErr = err;
838        } else {
839            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to present display";
840        }
841    }
842
843    void getReleaseFences(hwc2_display_t display,
844            std::vector<hwc2_layer_t>* outLayers,
845            std::vector<int32_t>* outFences, hwc2_error_t* outErr = nullptr)
846    {
847        auto pfn = reinterpret_cast<HWC2_PFN_GET_RELEASE_FENCES>(
848                getFunction(HWC2_FUNCTION_GET_RELEASE_FENCES));
849        ASSERT_TRUE(pfn) << "failed to get function";
850
851        uint32_t numElements = 0;
852
853        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
854                &numElements, nullptr, nullptr));
855
856        if (err == HWC2_ERROR_NONE) {
857            outLayers->resize(numElements);
858            outFences->resize(numElements);
859
860            err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
861                    &numElements, outLayers->data(), outFences->data()));
862        }
863
864        if (outErr) {
865            *outErr = err;
866        } else {
867            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to get release fences";
868        }
869    }
870
871    void getColorModes(hwc2_display_t display,
872            std::vector<ColorMode>* outColorModes,
873            hwc2_error_t* outErr = nullptr)
874    {
875        auto pfn = reinterpret_cast<HWC2_PFN_GET_COLOR_MODES>(
876                getFunction(HWC2_FUNCTION_GET_COLOR_MODES));
877        ASSERT_TRUE(pfn) << "failed to get function";
878
879        uint32_t numColorModes = 0;
880
881        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
882                &numColorModes, nullptr));
883        if (err == HWC2_ERROR_NONE) {
884            outColorModes->resize(numColorModes);
885
886            err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
887                    &numColorModes,
888                    reinterpret_cast<int32_t*>(outColorModes->data())));
889        }
890
891        if (outErr) {
892            *outErr = err;
893        } else {
894            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to get color modes for"
895                    " display " << display;
896        }
897    }
898
899    void setColorMode(hwc2_display_t display, ColorMode colorMode,
900            hwc2_error_t* outErr = nullptr)
901    {
902        auto pfn = reinterpret_cast<HWC2_PFN_SET_COLOR_MODE>(
903                getFunction(HWC2_FUNCTION_SET_COLOR_MODE));
904        ASSERT_TRUE(pfn) << "failed to get function";
905
906        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
907                static_cast<int32_t>(colorMode)));
908        if (outErr) {
909            *outErr = err;
910        } else {
911            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set color mode "
912                    << static_cast<int>(colorMode);
913        }
914    }
915
916    void getHdrCapabilities(hwc2_display_t display,
917            std::vector<android_hdr_t>* outTypes, float* outMaxLuminance,
918            float* outMaxAverageLuminance, float* outMinLuminance,
919            hwc2_error_t* outErr = nullptr)
920    {
921        auto pfn = reinterpret_cast<HWC2_PFN_GET_HDR_CAPABILITIES>(
922                getFunction(HWC2_FUNCTION_GET_HDR_CAPABILITIES));
923        ASSERT_TRUE(pfn) << "failed to get function";
924
925        uint32_t numTypes = 0;
926
927        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
928                &numTypes, nullptr, outMaxLuminance, outMaxAverageLuminance,
929                outMinLuminance));
930
931        if (err == HWC2_ERROR_NONE) {
932            outTypes->resize(numTypes);
933
934            err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, &numTypes,
935                    reinterpret_cast<int32_t*>(outTypes->data()), outMaxLuminance,
936                    outMaxAverageLuminance, outMinLuminance));
937        }
938
939        if (outErr) {
940            *outErr = err;
941        } else {
942            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to get hdr capabilities"
943                    " for display " << display;
944        }
945    }
946
947    void setColorTransform(hwc2_display_t display,
948            const std::array<float, 16>& matrix, android_color_transform_t hint,
949            hwc2_error_t* outErr = nullptr)
950    {
951        auto pfn = reinterpret_cast<HWC2_PFN_SET_COLOR_TRANSFORM>(
952                getFunction(HWC2_FUNCTION_SET_COLOR_TRANSFORM));
953        ASSERT_TRUE(pfn) << "failed to get function";
954
955        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display,
956                matrix.data(), hint));
957
958        if (outErr) {
959            *outErr = err;
960        } else {
961            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set color transform "
962                    << hint;
963        }
964    }
965
966    void createVirtualDisplay(uint32_t width, uint32_t height,
967            android_pixel_format_t* outFormat, hwc2_display_t* outDisplay,
968            hwc2_error_t* outErr = nullptr)
969    {
970        auto pfn = reinterpret_cast<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
971                getFunction(HWC2_FUNCTION_CREATE_VIRTUAL_DISPLAY));
972        ASSERT_TRUE(pfn) << "failed to get function";
973
974        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, width, height,
975                reinterpret_cast<int32_t*>(outFormat), outDisplay));
976
977        if (err == HWC2_ERROR_NONE)
978            mVirtualDisplays.insert(*outDisplay);
979
980        if (outErr) {
981            *outErr = err;
982        } else {
983            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to create virtual display";
984        }
985    }
986
987    void destroyVirtualDisplay(hwc2_display_t display,
988            hwc2_error_t* outErr = nullptr)
989    {
990        auto pfn = reinterpret_cast<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
991                getFunction(HWC2_FUNCTION_DESTROY_VIRTUAL_DISPLAY));
992        ASSERT_TRUE(pfn) << "failed to get function";
993
994        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display));
995
996        if (err == HWC2_ERROR_NONE)
997            mVirtualDisplays.erase(display);
998
999        if (outErr) {
1000            *outErr = err;
1001        } else {
1002            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to destroy virtual display";
1003        }
1004    }
1005
1006    void getMaxVirtualDisplayCount(uint32_t* outMaxCnt)
1007    {
1008        auto pfn = reinterpret_cast<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1009                getFunction(HWC2_FUNCTION_GET_MAX_VIRTUAL_DISPLAY_COUNT));
1010        ASSERT_TRUE(pfn) << "failed to get function";
1011
1012        *outMaxCnt = pfn(mHwc2Device);
1013    }
1014
1015    void setOutputBuffer(hwc2_display_t display, buffer_handle_t buffer,
1016            int32_t releaseFence, hwc2_error_t* outErr = nullptr)
1017    {
1018        auto pfn = reinterpret_cast<HWC2_PFN_SET_OUTPUT_BUFFER>(
1019                getFunction(HWC2_FUNCTION_SET_OUTPUT_BUFFER));
1020        ASSERT_TRUE(pfn) << "failed to get function";
1021
1022        auto err = static_cast<hwc2_error_t>(pfn(mHwc2Device, display, buffer,
1023                releaseFence));
1024        if (outErr) {
1025            *outErr = err;
1026        } else {
1027            ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to set output buffer";
1028        }
1029    }
1030
1031    void dump(std::string* outBuffer)
1032    {
1033        auto pfn = reinterpret_cast<HWC2_PFN_DUMP>(
1034                getFunction(HWC2_FUNCTION_DUMP));
1035        ASSERT_TRUE(pfn) << "failed to get function";
1036
1037        uint32_t size = 0;
1038
1039        pfn(mHwc2Device, &size, nullptr);
1040
1041        std::vector<char> buffer(size);
1042
1043        pfn(mHwc2Device, &size, buffer.data());
1044
1045        outBuffer->assign(buffer.data());
1046    }
1047
1048    void getBadDisplay(hwc2_display_t* outDisplay)
1049    {
1050        for (hwc2_display_t display = 0; display < UINT64_MAX; display++) {
1051            if (mDisplays.count(display) == 0) {
1052                *outDisplay = display;
1053                return;
1054            }
1055        }
1056        ASSERT_TRUE(false) << "Unable to find bad display. UINT64_MAX displays"
1057                " are registered. This should never happen.";
1058    }
1059
1060    void waitForVsync(hwc2_display_t* outDisplay = nullptr,
1061            int64_t* outTimestamp = nullptr)
1062    {
1063        std::unique_lock<std::mutex> lock(mVsyncMutex);
1064        ASSERT_EQ(mVsyncCv.wait_for(lock, std::chrono::seconds(3)),
1065                std::cv_status::no_timeout) << "timed out attempting to get"
1066                " vsync callback";
1067        if (outDisplay)
1068            *outDisplay = mVsyncDisplay;
1069        if (outTimestamp)
1070            *outTimestamp = mVsyncTimestamp;
1071    }
1072
1073    void enableVsync(hwc2_display_t display)
1074    {
1075        ASSERT_NO_FATAL_FAILURE(registerCallback(HWC2_CALLBACK_VSYNC, this,
1076                reinterpret_cast<hwc2_function_pointer_t>(
1077                hwc2TestVsyncCallback)));
1078        ASSERT_NO_FATAL_FAILURE(setVsyncEnabled(display, HWC2_VSYNC_ENABLE));
1079    }
1080
1081    void disableVsync(hwc2_display_t display)
1082    {
1083        ASSERT_NO_FATAL_FAILURE(setVsyncEnabled(display, HWC2_VSYNC_DISABLE));
1084    }
1085
1086protected:
1087    hwc2_function_pointer_t getFunction(hwc2_function_descriptor_t descriptor)
1088    {
1089        return mHwc2Device->getFunction(mHwc2Device, descriptor);
1090    }
1091
1092    void getCapabilities(std::vector<hwc2_capability_t>* outCapabilities)
1093    {
1094        uint32_t num = 0;
1095
1096        mHwc2Device->getCapabilities(mHwc2Device, &num, nullptr);
1097
1098        outCapabilities->resize(num);
1099
1100        mHwc2Device->getCapabilities(mHwc2Device, &num,
1101                reinterpret_cast<int32_t*>(outCapabilities->data()));
1102    }
1103
1104    /* Registers a hotplug callback and waits for hotplug callbacks. This
1105     * function will have no effect if called more than once. */
1106    void populateDisplays()
1107    {
1108        /* Sets the hotplug status to receiving */
1109        {
1110            std::lock_guard<std::mutex> lock(mHotplugMutex);
1111
1112            if (mHotplugStatus != Hwc2TestHotplugStatus::Init)
1113                return;
1114            mHotplugStatus = Hwc2TestHotplugStatus::Receiving;
1115        }
1116
1117        /* Registers the callback. This function call cannot be locked because
1118         * a callback could happen on the same thread */
1119        ASSERT_NO_FATAL_FAILURE(registerCallback(HWC2_CALLBACK_HOTPLUG, this,
1120                reinterpret_cast<hwc2_function_pointer_t>(
1121                hwc2TestHotplugCallback)));
1122
1123        /* Waits for hotplug events. If a hotplug event has not come within 1
1124         * second, stop waiting. */
1125        std::unique_lock<std::mutex> lock(mHotplugMutex);
1126
1127        while (mHotplugCv.wait_for(lock, std::chrono::seconds(1)) !=
1128                std::cv_status::timeout) { }
1129
1130        /* Sets the hotplug status to done. Future calls will have no effect */
1131        mHotplugStatus = Hwc2TestHotplugStatus::Done;
1132    }
1133
1134    /* NOTE: will create min(newlayerCnt, max supported layers) layers */
1135    void createLayers(hwc2_display_t display,
1136            std::vector<hwc2_layer_t>* outLayers, size_t newLayerCnt)
1137    {
1138        std::vector<hwc2_layer_t> newLayers;
1139        hwc2_layer_t layer;
1140        hwc2_error_t err = HWC2_ERROR_NONE;
1141
1142        for (size_t i = 0; i < newLayerCnt; i++) {
1143
1144            EXPECT_NO_FATAL_FAILURE(createLayer(display, &layer, &err));
1145            if (err == HWC2_ERROR_NO_RESOURCES)
1146                break;
1147            if (err != HWC2_ERROR_NONE) {
1148                newLayers.clear();
1149                ASSERT_EQ(err, HWC2_ERROR_NONE) << "failed to create layer";
1150            }
1151            newLayers.push_back(layer);
1152        }
1153
1154        *outLayers = std::move(newLayers);
1155    }
1156
1157    void destroyLayers(hwc2_display_t display,
1158            std::vector<hwc2_layer_t>&& layers)
1159    {
1160        for (hwc2_layer_t layer : layers) {
1161            EXPECT_NO_FATAL_FAILURE(destroyLayer(display, layer));
1162        }
1163    }
1164
1165    void getInvalidConfig(hwc2_display_t display, hwc2_config_t* outConfig)
1166    {
1167        std::vector<hwc2_config_t> configs;
1168
1169        ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
1170
1171        hwc2_config_t CONFIG_MAX = UINT32_MAX;
1172
1173        ASSERT_LE(configs.size() - 1, CONFIG_MAX) << "every config value"
1174                " (2^32 values) has been taken which shouldn't happen";
1175
1176        hwc2_config_t config;
1177        for (config = 0; config < CONFIG_MAX; config++) {
1178            if (std::count(configs.begin(), configs.end(), config) == 0)
1179                break;
1180        }
1181
1182        *outConfig = config;
1183    }
1184
1185    /* Calls a set property function from Hwc2Test to set a property value from
1186     * Hwc2TestLayer to hwc2_layer_t on hwc2_display_t */
1187    using TestLayerPropertyFunction = void (*)(Hwc2Test* test,
1188            hwc2_display_t display, hwc2_layer_t layer,
1189            Hwc2TestLayer* testLayer, hwc2_error_t* outErr);
1190
1191    /* Calls a set property function from Hwc2Test to set property values from
1192     * Hwc2TestLayers to hwc2_layer_t on hwc2_display_t */
1193    using TestLayerPropertiesFunction = void (*)(Hwc2Test* test,
1194            hwc2_display_t display, hwc2_layer_t layer,
1195            Hwc2TestLayers* testLayers);
1196
1197    /* Calls a set property function from Hwc2Test to set a bad property value
1198     * on hwc2_layer_t on hwc2_display_t */
1199    using TestLayerPropertyBadLayerFunction = void (*)(Hwc2Test* test,
1200            hwc2_display_t display, hwc2_layer_t layer,
1201            Hwc2TestLayer* testLayer, hwc2_error_t* outErr);
1202
1203    /* Calls a set property function from Hwc2Test to set a bad property value
1204     * on hwc2_layer_t on hwc2_display_t */
1205    using TestLayerPropertyBadParameterFunction = void (*)(Hwc2Test* test,
1206            hwc2_display_t display, hwc2_layer_t layer, hwc2_error_t* outErr);
1207
1208    /* Is called after a display is powered on and all layer properties have
1209     * been set. It should be used to test functions such as validate, accepting
1210     * changes, present, etc. */
1211    using TestDisplayLayersFunction = void (*)(Hwc2Test* test,
1212            hwc2_display_t display, const std::vector<hwc2_layer_t>& layers,
1213            Hwc2TestLayers* testLayers);
1214
1215    /* It is called on an non validated display */
1216    using TestDisplayNonValidatedLayersFunction = void (*)(Hwc2Test* test,
1217            hwc2_display_t display, std::vector<hwc2_layer_t>* layers);
1218
1219    /* Tests client target support on a particular display and config */
1220    using TestClientTargetSupportFunction = void (*)(Hwc2Test* test,
1221            hwc2_display_t display,
1222            const Hwc2TestClientTargetSupport& testClientTargetSupport);
1223
1224    /* Tests a particular active display config */
1225    using TestActiveDisplayConfigFunction = void (*)(Hwc2Test* test,
1226            hwc2_display_t display);
1227
1228    /* Tests a newly created virtual display */
1229    using TestCreateVirtualDisplayFunction = void (*)(Hwc2Test* test,
1230            hwc2_display_t display, Hwc2TestVirtualDisplay* testVirtualDisplay);
1231
1232    /* Advances a property of Hwc2TestLayer */
1233    using AdvanceProperty = bool (*)(Hwc2TestLayer* testLayer);
1234
1235    /* Advances properties of Hwc2TestLayers */
1236    using AdvanceProperties = bool (*)(Hwc2TestLayers* testLayer);
1237
1238    /* Advances properties of Hwc2TestClientTargetSupport */
1239    using AdvanceClientTargetSupport = bool (*)(
1240            Hwc2TestClientTargetSupport* testClientTargetSupport);
1241
1242    /* For each active display it cycles through each display config and tests
1243     * each property value. It creates a layer, sets the property and then
1244     * destroys the layer */
1245    void setLayerProperty(Hwc2TestCoverage coverage,
1246            TestLayerPropertyFunction function, AdvanceProperty advance)
1247    {
1248        for (auto display : mDisplays) {
1249            std::vector<hwc2_config_t> configs;
1250
1251            ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
1252
1253            for (auto config : configs) {
1254                hwc2_layer_t layer;
1255                Area displayArea;
1256
1257                ASSERT_NO_FATAL_FAILURE(setActiveConfig(display, config));
1258                ASSERT_NO_FATAL_FAILURE(getActiveDisplayArea(display,
1259                        &displayArea));
1260                Hwc2TestLayer testLayer(coverage, displayArea);
1261
1262                do {
1263                    ASSERT_NO_FATAL_FAILURE(createLayer(display, &layer));
1264
1265                    ASSERT_NO_FATAL_FAILURE(function(this, display, layer,
1266                            &testLayer, nullptr));
1267
1268                    ASSERT_NO_FATAL_FAILURE(destroyLayer(display, layer));
1269                } while (advance(&testLayer));
1270            }
1271        }
1272    }
1273
1274    /* For each active display it cycles through each display config and tests
1275     * each property value. It creates a layer, cycles through each property
1276     * value and updates the layer property value and then destroys the layer */
1277    void setLayerPropertyUpdate(Hwc2TestCoverage coverage,
1278            TestLayerPropertyFunction function, AdvanceProperty advance)
1279    {
1280        for (auto display : mDisplays) {
1281            std::vector<hwc2_config_t> configs;
1282
1283            ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
1284
1285            for (auto config : configs) {
1286                hwc2_layer_t layer;
1287                Area displayArea;
1288
1289                ASSERT_NO_FATAL_FAILURE(setActiveConfig(display, config));
1290                ASSERT_NO_FATAL_FAILURE(getActiveDisplayArea(display,
1291                        &displayArea));
1292                Hwc2TestLayer testLayer(coverage, displayArea);
1293
1294                ASSERT_NO_FATAL_FAILURE(createLayer(display, &layer));
1295
1296                do {
1297                    ASSERT_NO_FATAL_FAILURE(function(this, display, layer,
1298                            &testLayer, nullptr));
1299                } while (advance(&testLayer));
1300
1301                ASSERT_NO_FATAL_FAILURE(destroyLayer(display, layer));
1302            }
1303        }
1304    }
1305
1306    /* For each active display it cycles through each display config and tests
1307     * each property value. It creates multiple layers, calls the
1308     * TestLayerPropertiesFunction to set property values and then
1309     * destroys the layers */
1310    void setLayerProperties(Hwc2TestCoverage coverage, size_t layerCnt,
1311            TestLayerPropertiesFunction function, AdvanceProperties advance)
1312    {
1313        for (auto display : mDisplays) {
1314            std::vector<hwc2_config_t> configs;
1315
1316            ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
1317
1318            for (auto config : configs) {
1319                std::vector<hwc2_layer_t> layers;
1320                Area displayArea;
1321
1322                ASSERT_NO_FATAL_FAILURE(setActiveConfig(display, config));
1323                ASSERT_NO_FATAL_FAILURE(getActiveDisplayArea(display,
1324                        &displayArea));
1325
1326                ASSERT_NO_FATAL_FAILURE(createLayers(display, &layers, layerCnt));
1327                Hwc2TestLayers testLayers(layers, coverage, displayArea);
1328
1329                do {
1330                    for (auto layer : layers) {
1331                        EXPECT_NO_FATAL_FAILURE(function(this, display, layer,
1332                                &testLayers));
1333                    }
1334                } while (advance(&testLayers));
1335
1336                ASSERT_NO_FATAL_FAILURE(destroyLayers(display, std::move(layers)));
1337            }
1338        }
1339    }
1340
1341    /* For each active display it cycles through each display config.
1342     * 1) It attempts to set a valid property value to bad layer handle.
1343     * 2) It creates a layer x and attempts to set a valid property value to
1344     *    layer x + 1
1345     * 3) It destroys the layer x and attempts to set a valid property value to
1346     *    the destroyed layer x.
1347     */
1348    void setLayerPropertyBadLayer(Hwc2TestCoverage coverage,
1349            TestLayerPropertyBadLayerFunction function)
1350    {
1351        for (auto display : mDisplays) {
1352            std::vector<hwc2_config_t> configs;
1353
1354            ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
1355
1356            for (auto config : configs) {
1357                hwc2_layer_t layer = 0;
1358                Area displayArea;
1359                hwc2_error_t err = HWC2_ERROR_NONE;
1360
1361                ASSERT_NO_FATAL_FAILURE(setActiveConfig(display, config));
1362                ASSERT_NO_FATAL_FAILURE(getActiveDisplayArea(display,
1363                        &displayArea));
1364                Hwc2TestLayer testLayer(coverage, displayArea);
1365
1366                ASSERT_NO_FATAL_FAILURE(function(this, display, layer,
1367                        &testLayer, &err));
1368                EXPECT_EQ(err, HWC2_ERROR_BAD_LAYER) << "returned wrong error code";
1369
1370                ASSERT_NO_FATAL_FAILURE(createLayer(display, &layer));
1371
1372                ASSERT_NO_FATAL_FAILURE(function(this, display, layer + 1,
1373                        &testLayer, &err));
1374                EXPECT_EQ(err, HWC2_ERROR_BAD_LAYER) << "returned wrong error code";
1375
1376                ASSERT_NO_FATAL_FAILURE(destroyLayer(display, layer));
1377
1378                ASSERT_NO_FATAL_FAILURE(function(this, display, layer,
1379                        &testLayer, &err));
1380                EXPECT_EQ(err, HWC2_ERROR_BAD_LAYER) << "returned wrong error code";
1381            }
1382        }
1383    }
1384
1385    /* For each active display it cycles through each display config and tests
1386     * each property value. It creates a layer, sets a bad property value and
1387     * then destroys the layer */
1388    void setLayerPropertyBadParameter(TestLayerPropertyBadParameterFunction function)
1389    {
1390        for (auto display : mDisplays) {
1391            std::vector<hwc2_config_t> configs;
1392
1393            ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
1394
1395            for (auto config : configs) {
1396                hwc2_layer_t layer;
1397                hwc2_error_t err = HWC2_ERROR_NONE;
1398
1399                ASSERT_NO_FATAL_FAILURE(setActiveConfig(display, config));
1400
1401                ASSERT_NO_FATAL_FAILURE(createLayer(display, &layer));
1402
1403                ASSERT_NO_FATAL_FAILURE(function(this, display, layer, &err));
1404                EXPECT_EQ(err, HWC2_ERROR_BAD_PARAMETER) << "returned wrong"
1405                        " error code";
1406
1407                ASSERT_NO_FATAL_FAILURE(destroyLayer(display, layer));
1408            }
1409        }
1410    }
1411
1412    /* For each active display it powers on the display, cycles through each
1413     * config and creates a set of layers with a certain amount of coverage.
1414     * For each active display, for each config and for each set of layers,
1415     * it calls the TestDisplayLayersFunction */
1416    void displayLayers(Hwc2TestCoverage coverage, size_t layerCnt,
1417            TestDisplayLayersFunction function)
1418    {
1419        for (auto display : mDisplays) {
1420            std::vector<hwc2_config_t> configs;
1421
1422            ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_ON));
1423
1424            ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
1425
1426            for (auto config : configs) {
1427                Area displayArea;
1428                std::vector<hwc2_layer_t> layers;
1429
1430                ASSERT_NO_FATAL_FAILURE(setActiveConfig(display, config));
1431                ASSERT_NO_FATAL_FAILURE(getActiveDisplayArea(display, &displayArea));
1432
1433                ASSERT_NO_FATAL_FAILURE(createLayers(display, &layers, layerCnt));
1434                Hwc2TestLayers testLayers(layers, coverage, displayArea);
1435
1436                do {
1437                    bool skip;
1438
1439                    ASSERT_NO_FATAL_FAILURE(setLayerProperties(display, layers,
1440                            &testLayers, &skip));
1441                    if (!skip)
1442                        EXPECT_NO_FATAL_FAILURE(function(this, display, layers,
1443                                &testLayers));
1444
1445                } while (testLayers.advance());
1446
1447                ASSERT_NO_FATAL_FAILURE(destroyLayers(display,
1448                        std::move(layers)));
1449            }
1450
1451            ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF));
1452        }
1453    }
1454
1455    /* For each active display, it calls the
1456     * TestDisplayNonValidatedLayersFunction on a variety on non-validated
1457     * layer combinations */
1458    void displayNonValidatedLayers(size_t layerCnt,
1459            TestDisplayNonValidatedLayersFunction function)
1460    {
1461        for (auto display : mDisplays) {
1462            uint32_t numTypes, numRequests;
1463            std::vector<hwc2_layer_t> layers;
1464            bool hasChanges;
1465
1466            ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_ON));
1467
1468            EXPECT_NO_FATAL_FAILURE(function(this, display, &layers));
1469
1470            ASSERT_NO_FATAL_FAILURE(createLayers(display, &layers, layerCnt));
1471
1472            EXPECT_NO_FATAL_FAILURE(function(this, display, &layers));
1473
1474            for (auto layer : layers) {
1475                ASSERT_NO_FATAL_FAILURE(setLayerCompositionType(display, layer,
1476                        HWC2_COMPOSITION_CLIENT));
1477            }
1478
1479            EXPECT_NO_FATAL_FAILURE(function(this, display, &layers));
1480
1481            ASSERT_NO_FATAL_FAILURE(validateDisplay(display, &numTypes,
1482                    &numRequests, &hasChanges));
1483
1484            for (auto layer : layers) {
1485                ASSERT_NO_FATAL_FAILURE(setLayerCompositionType(display, layer,
1486                        HWC2_COMPOSITION_DEVICE));
1487            }
1488
1489            EXPECT_NO_FATAL_FAILURE(function(this, display, &layers));
1490
1491            ASSERT_NO_FATAL_FAILURE(destroyLayers(display, std::move(layers)));
1492
1493            EXPECT_NO_FATAL_FAILURE(function(this, display, &layers));
1494
1495            ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF));
1496        }
1497    }
1498
1499    /* Test client target support on each config on each active display */
1500    void setClientTargetSupport(Hwc2TestCoverage coverage,
1501            TestClientTargetSupportFunction function,
1502            AdvanceClientTargetSupport advance)
1503    {
1504        for (auto display : mDisplays) {
1505            std::vector<hwc2_config_t> configs;
1506
1507            ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
1508
1509            for (auto config : configs) {
1510                Area displayArea;
1511
1512                ASSERT_NO_FATAL_FAILURE(setActiveConfig(display, config));
1513                ASSERT_NO_FATAL_FAILURE(getActiveDisplayArea(display,
1514                        &displayArea));
1515                Hwc2TestClientTargetSupport testClientTargetSupport(coverage,
1516                        displayArea);
1517
1518                do {
1519                    EXPECT_NO_FATAL_FAILURE(function(this, display,
1520                            testClientTargetSupport));
1521
1522                } while (advance(&testClientTargetSupport));
1523            }
1524        }
1525    }
1526
1527    /* Cycles through each config on each active display and calls
1528     * a TestActiveDisplayConfigFunction */
1529    void setActiveDisplayConfig(TestActiveDisplayConfigFunction function)
1530    {
1531        for (auto display : mDisplays) {
1532            std::vector<hwc2_config_t> configs;
1533
1534            ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
1535
1536            for (auto config : configs) {
1537                ASSERT_NO_FATAL_FAILURE(setActiveConfig(display, config));
1538
1539                EXPECT_NO_FATAL_FAILURE(function(this, display));
1540            }
1541        }
1542    }
1543
1544    /* Creates a virtual display for testing */
1545    void createVirtualDisplay(Hwc2TestCoverage coverage,
1546            TestCreateVirtualDisplayFunction function)
1547    {
1548        Hwc2TestVirtualDisplay testVirtualDisplay(coverage);
1549
1550        do {
1551            hwc2_display_t display;
1552            hwc2_error_t err = HWC2_ERROR_NONE;
1553
1554            const UnsignedArea& dimension =
1555                    testVirtualDisplay.getDisplayDimension();
1556            android_pixel_format_t desiredFormat = HAL_PIXEL_FORMAT_RGBA_8888;
1557
1558            ASSERT_NO_FATAL_FAILURE(createVirtualDisplay(dimension.width,
1559                    dimension.height, &desiredFormat, &display, &err));
1560
1561            EXPECT_TRUE(err == HWC2_ERROR_NONE || err == HWC2_ERROR_NO_RESOURCES
1562                    || err == HWC2_ERROR_UNSUPPORTED)
1563                    << "returned wrong error code";
1564            EXPECT_GE(desiredFormat, 0) << "invalid format";
1565
1566            if (err != HWC2_ERROR_NONE)
1567                continue;
1568
1569            EXPECT_NO_FATAL_FAILURE(function(this, display,
1570                    &testVirtualDisplay));
1571
1572            ASSERT_NO_FATAL_FAILURE(destroyVirtualDisplay(display));
1573
1574        } while (testVirtualDisplay.advance());
1575    }
1576
1577
1578    void getActiveConfigAttribute(hwc2_display_t display,
1579            hwc2_attribute_t attribute, int32_t* outValue)
1580    {
1581        hwc2_config_t config;
1582        ASSERT_NO_FATAL_FAILURE(getActiveConfig(display, &config));
1583        ASSERT_NO_FATAL_FAILURE(getDisplayAttribute(display, config,
1584                attribute, outValue));
1585        ASSERT_GE(*outValue, 0) << "failed to get valid "
1586                << getAttributeName(attribute);
1587    }
1588
1589    void getActiveDisplayArea(hwc2_display_t display, Area* displayArea)
1590    {
1591        ASSERT_NO_FATAL_FAILURE(getActiveConfigAttribute(display,
1592                HWC2_ATTRIBUTE_WIDTH, &displayArea->width));
1593        ASSERT_NO_FATAL_FAILURE(getActiveConfigAttribute(display,
1594                HWC2_ATTRIBUTE_HEIGHT, &displayArea->height));
1595    }
1596
1597    void closeFences(hwc2_display_t display, int32_t presentFence)
1598    {
1599        std::vector<hwc2_layer_t> layers;
1600        std::vector<int32_t> fences;
1601        const int msWait = 3000;
1602
1603        if (presentFence >= 0) {
1604            ASSERT_GE(sync_wait(presentFence, msWait), 0);
1605            close(presentFence);
1606        }
1607
1608        ASSERT_NO_FATAL_FAILURE(getReleaseFences(display, &layers, &fences));
1609        EXPECT_EQ(layers.size(), fences.size());
1610
1611        for (int32_t fence : fences) {
1612            if (fence >= 0) {
1613                EXPECT_GE(sync_wait(fence, msWait), 0);
1614                close(fence);
1615            }
1616        }
1617    }
1618
1619    void setLayerProperties(hwc2_display_t display, hwc2_layer_t layer,
1620            Hwc2TestLayers* testLayers, bool* outSkip)
1621    {
1622        hwc2_composition_t composition;
1623        buffer_handle_t handle = nullptr;
1624        int32_t acquireFence;
1625        hwc2_error_t err = HWC2_ERROR_NONE;
1626        *outSkip = true;
1627
1628        if (!testLayers->contains(layer))
1629            return;
1630
1631        composition = testLayers->getComposition(layer);
1632
1633        /* If the device cannot support a buffer format, then do not continue */
1634        if ((composition == HWC2_COMPOSITION_DEVICE
1635                || composition == HWC2_COMPOSITION_CURSOR)
1636                && testLayers->getBuffer(layer, &handle, &acquireFence) < 0)
1637            return;
1638
1639        EXPECT_NO_FATAL_FAILURE(setLayerCompositionType(display, layer,
1640                composition, &err));
1641        if (err == HWC2_ERROR_UNSUPPORTED)
1642            EXPECT_TRUE(composition != HWC2_COMPOSITION_CLIENT
1643                    && composition != HWC2_COMPOSITION_DEVICE);
1644
1645        const hwc_rect_t cursor = testLayers->getCursorPosition(layer);
1646
1647        EXPECT_NO_FATAL_FAILURE(setLayerBuffer(display, layer, handle,
1648                acquireFence));
1649        EXPECT_NO_FATAL_FAILURE(setLayerBlendMode(display, layer,
1650                testLayers->getBlendMode(layer)));
1651        EXPECT_NO_FATAL_FAILURE(setLayerColor(display, layer,
1652                testLayers->getColor(layer)));
1653        if (composition == HWC2_COMPOSITION_CURSOR)
1654            EXPECT_NO_FATAL_FAILURE(setCursorPosition(display, layer,
1655            cursor.left, cursor.top));
1656        EXPECT_NO_FATAL_FAILURE(setLayerDataspace(display, layer,
1657                testLayers->getDataspace(layer)));
1658        EXPECT_NO_FATAL_FAILURE(setLayerDisplayFrame(display, layer,
1659                testLayers->getDisplayFrame(layer)));
1660        EXPECT_NO_FATAL_FAILURE(setLayerPlaneAlpha(display, layer,
1661                testLayers->getPlaneAlpha(layer)));
1662        EXPECT_NO_FATAL_FAILURE(setLayerSourceCrop(display, layer,
1663                testLayers->getSourceCrop(layer)));
1664        EXPECT_NO_FATAL_FAILURE(setLayerSurfaceDamage(display, layer,
1665                testLayers->getSurfaceDamage(layer)));
1666        EXPECT_NO_FATAL_FAILURE(setLayerTransform(display, layer,
1667                testLayers->getTransform(layer)));
1668        EXPECT_NO_FATAL_FAILURE(setLayerVisibleRegion(display, layer,
1669                testLayers->getVisibleRegion(layer)));
1670        EXPECT_NO_FATAL_FAILURE(setLayerZOrder(display, layer,
1671                testLayers->getZOrder(layer)));
1672
1673        *outSkip = false;
1674    }
1675
1676    void setLayerProperties(hwc2_display_t display,
1677            const std::vector<hwc2_layer_t>& layers,
1678            Hwc2TestLayers* testLayers, bool* outSkip)
1679    {
1680        for (auto layer : layers) {
1681            EXPECT_NO_FATAL_FAILURE(setLayerProperties(display, layer,
1682                    testLayers, outSkip));
1683            if (*outSkip)
1684                return;
1685        }
1686    }
1687
1688    void setClientTarget(hwc2_display_t display,
1689            Hwc2TestClientTarget* testClientTarget,
1690            const Hwc2TestLayers& testLayers,
1691            const std::set<hwc2_layer_t>& clientLayers,
1692            const std::set<hwc2_layer_t>& clearLayers, bool flipClientTarget,
1693            const Area& displayArea)
1694    {
1695        Dataspace dataspace = Dataspace::UNKNOWN;
1696        hwc_region_t damage = { };
1697        buffer_handle_t handle;
1698        int32_t acquireFence;
1699
1700        ASSERT_EQ(testClientTarget->getBuffer(testLayers, clientLayers,
1701                clearLayers, flipClientTarget, displayArea, &handle,
1702                &acquireFence), 0);
1703        EXPECT_NO_FATAL_FAILURE(setClientTarget(display, handle, acquireFence,
1704                dataspace, damage));
1705    }
1706
1707    void presentDisplays(size_t layerCnt, Hwc2TestCoverage coverage,
1708            const std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage>&
1709            coverageExceptions, bool optimize)
1710    {
1711        for (auto display : mDisplays) {
1712            std::vector<hwc2_config_t> configs;
1713
1714            ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_ON));
1715            ASSERT_NO_FATAL_FAILURE(enableVsync(display));
1716
1717            ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
1718
1719            for (auto config : configs) {
1720                Area displayArea;
1721                std::vector<hwc2_layer_t> layers;
1722
1723                ASSERT_NO_FATAL_FAILURE(setActiveConfig(display, config));
1724                ASSERT_NO_FATAL_FAILURE(getActiveDisplayArea(display,
1725                        &displayArea));
1726
1727                ASSERT_NO_FATAL_FAILURE(createLayers(display, &layers, layerCnt));
1728                Hwc2TestLayers testLayers(layers, coverage, displayArea,
1729                        coverageExceptions);
1730
1731                if (optimize && !testLayers.optimizeLayouts())
1732                    continue;
1733
1734                std::set<hwc2_layer_t> clientLayers;
1735                std::set<hwc2_layer_t> clearLayers;
1736                Hwc2TestClientTarget testClientTarget;
1737
1738                do {
1739                    uint32_t numTypes, numRequests;
1740                    bool hasChanges, skip;
1741                    bool flipClientTarget;
1742                    int32_t presentFence;
1743
1744                    ASSERT_NO_FATAL_FAILURE(setLayerProperties(display, layers,
1745                            &testLayers, &skip));
1746                    if (skip)
1747                        continue;
1748
1749                    ASSERT_NO_FATAL_FAILURE(validateDisplay(display, &numTypes,
1750                            &numRequests, &hasChanges));
1751                    if (hasChanges)
1752                        EXPECT_LE(numTypes, static_cast<uint32_t>(layers.size()))
1753                                << "wrong number of requests";
1754
1755                    ASSERT_NO_FATAL_FAILURE(handleCompositionChanges(display,
1756                            testLayers, layers, numTypes, &clientLayers));
1757                    ASSERT_NO_FATAL_FAILURE(handleRequests(display, layers,
1758                            numRequests, &clearLayers, &flipClientTarget));
1759                    ASSERT_NO_FATAL_FAILURE(setClientTarget(display,
1760                            &testClientTarget, testLayers, clientLayers,
1761                            clearLayers, flipClientTarget, displayArea));
1762                    ASSERT_NO_FATAL_FAILURE(acceptDisplayChanges(display));
1763
1764                    ASSERT_NO_FATAL_FAILURE(waitForVsync());
1765
1766                    EXPECT_NO_FATAL_FAILURE(presentDisplay(display,
1767                            &presentFence));
1768
1769                    ASSERT_NO_FATAL_FAILURE(closeFences(display, presentFence));
1770
1771                } while (testLayers.advance());
1772
1773                ASSERT_NO_FATAL_FAILURE(destroyLayers(display,
1774                        std::move(layers)));
1775            }
1776
1777            ASSERT_NO_FATAL_FAILURE(disableVsync(display));
1778            ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF));
1779        }
1780    }
1781
1782    void createAndPresentVirtualDisplay(size_t layerCnt,
1783            Hwc2TestCoverage coverage,
1784            const std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage>&
1785            coverageExceptions)
1786    {
1787        Hwc2TestVirtualDisplay testVirtualDisplay(coverage);
1788        hwc2_display_t display;
1789        android_pixel_format_t desiredFormat = HAL_PIXEL_FORMAT_RGBA_8888;
1790
1791        do {
1792            // Items dependent on the display dimensions
1793            hwc2_error_t err = HWC2_ERROR_NONE;
1794            const UnsignedArea& dimension =
1795                    testVirtualDisplay.getDisplayDimension();
1796            ASSERT_NO_FATAL_FAILURE(createVirtualDisplay(dimension.width,
1797                    dimension.height, &desiredFormat, &display, &err));
1798            ASSERT_TRUE(err == HWC2_ERROR_NONE)
1799                    << "Cannot allocate virtual display";
1800
1801            ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_ON));
1802            ASSERT_NO_FATAL_FAILURE(enableVsync(display));
1803
1804            std::vector<hwc2_config_t> configs;
1805            ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
1806
1807            for (auto config : configs) {
1808                ASSERT_NO_FATAL_FAILURE(setActiveConfig(display, config));
1809
1810                Area displayArea;
1811                ASSERT_NO_FATAL_FAILURE(getActiveDisplayArea(display,
1812                        &displayArea));
1813
1814                std::vector<hwc2_layer_t> layers;
1815                ASSERT_NO_FATAL_FAILURE(createLayers(display, &layers,
1816                        layerCnt));
1817                Hwc2TestLayers testLayers(layers, coverage, displayArea,
1818                        coverageExceptions);
1819
1820                /*
1821                 * Layouts that do not cover an entire virtual display will
1822                 * cause undefined behavior.
1823                 * Enable optimizeLayouts to avoid this.
1824                 */
1825                testLayers.optimizeLayouts();
1826                do {
1827                    // Items dependent on the testLayers properties
1828                    std::set<hwc2_layer_t> clientLayers;
1829                    std::set<hwc2_layer_t> clearLayers;
1830                    uint32_t numTypes, numRequests;
1831                    bool hasChanges, skip;
1832                    bool flipClientTarget;
1833                    int32_t presentFence;
1834                    Hwc2TestClientTarget testClientTarget;
1835                    buffer_handle_t outputBufferHandle;
1836                    android::base::unique_fd outputBufferReleaseFence;
1837
1838                    ASSERT_NO_FATAL_FAILURE(setLayerProperties(display, layers,
1839                            &testLayers, &skip));
1840
1841                    if (skip)
1842                        continue;
1843
1844                    ASSERT_NO_FATAL_FAILURE(validateDisplay(display, &numTypes,
1845                            &numRequests, &hasChanges));
1846
1847                    if (hasChanges)
1848                        EXPECT_LE(numTypes, static_cast<uint32_t>(layers.size()))
1849                                << "wrong number of requests";
1850
1851                    ASSERT_NO_FATAL_FAILURE(handleCompositionChanges(display,
1852                            testLayers, layers, numTypes, &clientLayers));
1853
1854                    ASSERT_NO_FATAL_FAILURE(handleRequests(display, layers,
1855                            numRequests, &clearLayers, &flipClientTarget));
1856                    ASSERT_NO_FATAL_FAILURE(setClientTarget(display,
1857                            &testClientTarget, testLayers, clientLayers,
1858                            clearLayers, flipClientTarget, displayArea));
1859                    ASSERT_NO_FATAL_FAILURE(acceptDisplayChanges(display));
1860
1861                    ASSERT_EQ(testVirtualDisplay.getOutputBuffer(
1862                            &outputBufferHandle, &outputBufferReleaseFence), 0);
1863                    ASSERT_NO_FATAL_FAILURE(setOutputBuffer(display,
1864                            outputBufferHandle, outputBufferReleaseFence));
1865
1866                    EXPECT_NO_FATAL_FAILURE(presentDisplay(display,
1867                            &presentFence));
1868                    ASSERT_NO_FATAL_FAILURE(closeFences(display, presentFence));
1869
1870                    ASSERT_EQ(testVirtualDisplay.verifyOutputBuffer(&testLayers,
1871                            &layers, &clearLayers), 0);
1872
1873                    /*
1874                     * Upscaling the image causes minor pixel differences.
1875                     * Work around this by using some threshold.
1876                     *
1877                     * Fail test if we are off by more than 1% of our
1878                     * pixels.
1879                     */
1880                    ComparatorResult& comparatorResult = ComparatorResult::get();
1881                    int threshold = (dimension.width * dimension.height) / 100;
1882                    double diffPercent = (comparatorResult.getDifferentPixelCount() * 100.0) /
1883                            (dimension.width * dimension.height);
1884
1885                    if (comparatorResult.getDifferentPixelCount() != 0)
1886                        EXPECT_TRUE(false)
1887                                << comparatorResult.getDifferentPixelCount() << " pixels ("
1888                                << diffPercent << "%) are different.";
1889
1890                    if (comparatorResult.getDifferentPixelCount() > threshold) {
1891                        EXPECT_TRUE(false)
1892                                << "Mismatched pixel count exceeds threshold. "
1893                                << "Writing buffers to file.";
1894
1895                        const ::testing::TestInfo* const test_info =
1896                                ::testing::UnitTest::GetInstance()
1897                                ->current_test_info();
1898
1899                        EXPECT_EQ(testVirtualDisplay.writeBuffersToFile(
1900                                test_info->name()), 0)
1901                                << "Failed to write buffers.";
1902                    }
1903
1904                    ASSERT_LE(comparatorResult.getDifferentPixelCount(), threshold)
1905                            << comparatorResult.getDifferentPixelCount() << " pixels ("
1906                            << diffPercent << "%) are different. "
1907                            << "Exceeds 1% threshold, terminating test. "
1908                            << "Test case: " << testLayers.dump();
1909
1910                } while (testLayers.advance());
1911
1912                ASSERT_NO_FATAL_FAILURE(destroyLayers(display,
1913                        std::move(layers)));
1914            }
1915            ASSERT_NO_FATAL_FAILURE(disableVsync(display));
1916            ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF));
1917            ASSERT_NO_FATAL_FAILURE(destroyVirtualDisplay(display));
1918        } while (testVirtualDisplay.advance());
1919    }
1920
1921    hwc2_device_t* mHwc2Device = nullptr;
1922
1923    enum class Hwc2TestHotplugStatus {
1924        Init = 1,
1925        Receiving,
1926        Done,
1927    };
1928
1929    std::mutex mHotplugMutex;
1930    std::condition_variable mHotplugCv;
1931    Hwc2TestHotplugStatus mHotplugStatus = Hwc2TestHotplugStatus::Init;
1932    std::unordered_set<hwc2_display_t> mDisplays;
1933
1934    /* Store all created layers that have not been destroyed. If an ASSERT_*
1935     * fails, then destroy the layers on exit */
1936    std::set<std::pair<hwc2_display_t, hwc2_layer_t>> mLayers;
1937
1938    /* Store the power mode state. If it is not HWC2_POWER_MODE_OFF when
1939     * tearing down the test cases, change it to HWC2_POWER_MODE_OFF */
1940    std::set<hwc2_display_t> mActiveDisplays;
1941
1942    /* Store all created virtual displays that have not been destroyed. If an
1943     * ASSERT_* fails, then destroy the virtual displays on exit */
1944    std::set<hwc2_display_t> mVirtualDisplays;
1945
1946    std::mutex mVsyncMutex;
1947    std::condition_variable mVsyncCv;
1948    hwc2_display_t mVsyncDisplay;
1949    int64_t mVsyncTimestamp = -1;
1950};
1951
1952void hwc2TestHotplugCallback(hwc2_callback_data_t callbackData,
1953        hwc2_display_t display, int32_t connection)
1954{
1955    if (callbackData)
1956        static_cast<Hwc2Test*>(callbackData)->hotplugCallback(display,
1957                connection);
1958}
1959
1960void hwc2TestVsyncCallback(hwc2_callback_data_t callbackData,
1961        hwc2_display_t display, int64_t timestamp)
1962{
1963    if (callbackData)
1964        static_cast<Hwc2Test*>(callbackData)->vsyncCallback(display,
1965                timestamp);
1966}
1967
1968void setBlendMode(Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
1969        Hwc2TestLayer* testLayer, hwc2_error_t* outErr)
1970{
1971    EXPECT_NO_FATAL_FAILURE(test->setLayerBlendMode(display, layer,
1972            testLayer->getBlendMode(), outErr));
1973}
1974
1975void setBuffer(Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
1976        Hwc2TestLayer* testLayer, hwc2_error_t* outErr)
1977{
1978    buffer_handle_t handle;
1979    android::base::unique_fd acquireFence;
1980    hwc2_composition_t composition = testLayer->getComposition();
1981
1982    if (composition == HWC2_COMPOSITION_CLIENT
1983            || composition == HWC2_COMPOSITION_SOLID_COLOR
1984            || composition == HWC2_COMPOSITION_SIDEBAND)
1985        return;
1986
1987    if (testLayer->getBuffer(&handle, &acquireFence) < 0)
1988        return;
1989
1990    ASSERT_NO_FATAL_FAILURE(test->setLayerCompositionType(display, layer,
1991            composition));
1992    EXPECT_NO_FATAL_FAILURE(test->setLayerBuffer(display, layer,
1993            handle, acquireFence, outErr));
1994}
1995
1996void setColor(Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
1997        Hwc2TestLayer* testLayer, hwc2_error_t* outErr)
1998{
1999    ASSERT_NO_FATAL_FAILURE(test->setLayerCompositionType(display,
2000            layer, HWC2_COMPOSITION_SOLID_COLOR));
2001    ASSERT_NO_FATAL_FAILURE(test->setLayerPlaneAlpha(display,
2002            layer, testLayer->getPlaneAlpha()));
2003    ASSERT_NO_FATAL_FAILURE(test->setLayerBlendMode(display,
2004            layer, testLayer->getBlendMode()));
2005    EXPECT_NO_FATAL_FAILURE(test->setLayerColor(display, layer,
2006            testLayer->getColor(), outErr));
2007}
2008
2009void setComposition(Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
2010        Hwc2TestLayer* testLayer, hwc2_error_t* outErr)
2011{
2012    hwc2_composition_t composition = testLayer->getComposition();
2013    hwc2_error_t err = HWC2_ERROR_NONE;
2014
2015    ASSERT_NO_FATAL_FAILURE(test->setLayerCompositionType(display, layer,
2016            composition, &err));
2017    if (outErr) {
2018        *outErr = err;
2019        return;
2020    }
2021
2022    if (composition != HWC2_COMPOSITION_SIDEBAND) {
2023        EXPECT_EQ(err, HWC2_ERROR_NONE) << "returned wrong error code";
2024    } else {
2025        EXPECT_TRUE(err == HWC2_ERROR_NONE || err == HWC2_ERROR_UNSUPPORTED)
2026                 << "returned wrong error code";
2027    }
2028}
2029
2030void setCursorPosition(Hwc2Test* test, hwc2_display_t display,
2031        hwc2_layer_t layer, Hwc2TestLayer* testLayer, hwc2_error_t* outErr)
2032{
2033    ASSERT_NO_FATAL_FAILURE(test->setLayerCompositionType(display,
2034            layer, HWC2_COMPOSITION_CURSOR));
2035
2036    const hwc_rect_t cursorPosition = testLayer->getCursorPosition();
2037    EXPECT_NO_FATAL_FAILURE(test->setCursorPosition(display, layer,
2038            cursorPosition.left, cursorPosition.top, outErr));
2039}
2040
2041void setDataspace(Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
2042        Hwc2TestLayer* testLayer, hwc2_error_t* outErr)
2043{
2044    EXPECT_NO_FATAL_FAILURE(test->setLayerDataspace(display, layer,
2045            testLayer->getDataspace(), outErr));
2046}
2047
2048void setDisplayFrame(Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
2049        Hwc2TestLayer* testLayer, hwc2_error_t* outErr)
2050{
2051    EXPECT_NO_FATAL_FAILURE(test->setLayerDisplayFrame(display, layer,
2052            testLayer->getDisplayFrame(), outErr));
2053}
2054
2055void setPlaneAlpha(Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
2056        Hwc2TestLayer* testLayer, hwc2_error_t *outErr)
2057{
2058    ASSERT_NO_FATAL_FAILURE(test->setLayerBlendMode(display, layer,
2059            testLayer->getBlendMode()));
2060    EXPECT_NO_FATAL_FAILURE(test->setLayerPlaneAlpha(display, layer,
2061            testLayer->getPlaneAlpha(), outErr));
2062}
2063
2064void setSourceCrop(Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
2065        Hwc2TestLayer* testLayer, hwc2_error_t* outErr)
2066{
2067    EXPECT_NO_FATAL_FAILURE(test->setLayerSourceCrop(display, layer,
2068            testLayer->getSourceCrop(), outErr));
2069}
2070
2071void setSurfaceDamage(Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
2072        Hwc2TestLayer* testLayer, hwc2_error_t* outErr)
2073{
2074    EXPECT_NO_FATAL_FAILURE(test->setLayerSurfaceDamage(display, layer,
2075            testLayer->getSurfaceDamage(), outErr));
2076}
2077
2078void setTransform(Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
2079        Hwc2TestLayer* testLayer, hwc2_error_t* outErr)
2080{
2081    EXPECT_NO_FATAL_FAILURE(test->setLayerTransform(display, layer,
2082            testLayer->getTransform(), outErr));
2083}
2084
2085void setVisibleRegion(Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
2086        Hwc2TestLayer* testLayer, hwc2_error_t* outErr)
2087{
2088    EXPECT_NO_FATAL_FAILURE(test->setLayerVisibleRegion(display, layer,
2089            testLayer->getVisibleRegion(), outErr));
2090}
2091
2092void setZOrder(Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
2093        Hwc2TestLayer* testLayer, hwc2_error_t* outErr)
2094{
2095    EXPECT_NO_FATAL_FAILURE(test->setLayerZOrder(display, layer,
2096            testLayer->getZOrder(), outErr));
2097}
2098
2099bool advanceBlendMode(Hwc2TestLayer* testLayer)
2100{
2101    return testLayer->advanceBlendMode();
2102}
2103
2104bool advanceBuffer(Hwc2TestLayer* testLayer)
2105{
2106    if (testLayer->advanceComposition())
2107        return true;
2108    return testLayer->advanceBufferArea();
2109}
2110
2111bool advanceColor(Hwc2TestLayer* testLayer)
2112{
2113    /* Color depends on blend mode so advance blend mode last so color is not
2114     * force to update as often */
2115    if (testLayer->advancePlaneAlpha())
2116        return true;
2117    if (testLayer->advanceColor())
2118        return true;
2119    return testLayer->advanceBlendMode();
2120}
2121
2122bool advanceComposition(Hwc2TestLayer* testLayer)
2123{
2124    return testLayer->advanceComposition();
2125}
2126
2127bool advanceCursorPosition(Hwc2TestLayer* testLayer)
2128{
2129    return testLayer->advanceCursorPosition();
2130}
2131
2132bool advanceDataspace(Hwc2TestLayer* testLayer)
2133{
2134    return testLayer->advanceDataspace();
2135}
2136
2137bool advanceDisplayFrame(Hwc2TestLayer* testLayer)
2138{
2139    return testLayer->advanceDisplayFrame();
2140}
2141
2142bool advancePlaneAlpha(Hwc2TestLayer* testLayer)
2143{
2144    return testLayer->advancePlaneAlpha();
2145}
2146
2147bool advanceSourceCrop(Hwc2TestLayer* testLayer)
2148{
2149    if (testLayer->advanceSourceCrop())
2150        return true;
2151    return testLayer->advanceBufferArea();
2152}
2153
2154bool advanceSurfaceDamage(Hwc2TestLayer* testLayer)
2155{
2156    if (testLayer->advanceSurfaceDamage())
2157        return true;
2158    return testLayer->advanceBufferArea();
2159}
2160
2161bool advanceTransform(Hwc2TestLayer* testLayer)
2162{
2163    return testLayer->advanceTransform();
2164}
2165
2166bool advanceVisibleRegions(Hwc2TestLayers* testLayers)
2167{
2168    return testLayers->advanceVisibleRegions();
2169}
2170
2171bool advanceClientTargetSupport(
2172        Hwc2TestClientTargetSupport* testClientTargetSupport)
2173{
2174    return testClientTargetSupport->advance();
2175}
2176
2177static const std::array<hwc2_function_descriptor_t, 42> requiredFunctions = {{
2178    HWC2_FUNCTION_ACCEPT_DISPLAY_CHANGES,
2179    HWC2_FUNCTION_CREATE_LAYER,
2180    HWC2_FUNCTION_CREATE_VIRTUAL_DISPLAY,
2181    HWC2_FUNCTION_DESTROY_LAYER,
2182    HWC2_FUNCTION_DESTROY_VIRTUAL_DISPLAY,
2183    HWC2_FUNCTION_DUMP,
2184    HWC2_FUNCTION_GET_ACTIVE_CONFIG,
2185    HWC2_FUNCTION_GET_CHANGED_COMPOSITION_TYPES,
2186    HWC2_FUNCTION_GET_CLIENT_TARGET_SUPPORT,
2187    HWC2_FUNCTION_GET_COLOR_MODES,
2188    HWC2_FUNCTION_GET_DISPLAY_ATTRIBUTE,
2189    HWC2_FUNCTION_GET_DISPLAY_CONFIGS,
2190    HWC2_FUNCTION_GET_DISPLAY_NAME,
2191    HWC2_FUNCTION_GET_DISPLAY_REQUESTS,
2192    HWC2_FUNCTION_GET_DISPLAY_TYPE,
2193    HWC2_FUNCTION_GET_DOZE_SUPPORT,
2194    HWC2_FUNCTION_GET_HDR_CAPABILITIES,
2195    HWC2_FUNCTION_GET_MAX_VIRTUAL_DISPLAY_COUNT,
2196    HWC2_FUNCTION_GET_RELEASE_FENCES,
2197    HWC2_FUNCTION_PRESENT_DISPLAY,
2198    HWC2_FUNCTION_REGISTER_CALLBACK,
2199    HWC2_FUNCTION_SET_ACTIVE_CONFIG,
2200    HWC2_FUNCTION_SET_CLIENT_TARGET,
2201    HWC2_FUNCTION_SET_COLOR_MODE,
2202    HWC2_FUNCTION_SET_COLOR_TRANSFORM,
2203    HWC2_FUNCTION_SET_CURSOR_POSITION,
2204    HWC2_FUNCTION_SET_LAYER_BLEND_MODE,
2205    HWC2_FUNCTION_SET_LAYER_BUFFER,
2206    HWC2_FUNCTION_SET_LAYER_COLOR,
2207    HWC2_FUNCTION_SET_LAYER_COMPOSITION_TYPE,
2208    HWC2_FUNCTION_SET_LAYER_DATASPACE,
2209    HWC2_FUNCTION_SET_LAYER_DISPLAY_FRAME,
2210    HWC2_FUNCTION_SET_LAYER_PLANE_ALPHA,
2211    HWC2_FUNCTION_SET_LAYER_SOURCE_CROP,
2212    HWC2_FUNCTION_SET_LAYER_SURFACE_DAMAGE,
2213    HWC2_FUNCTION_SET_LAYER_TRANSFORM,
2214    HWC2_FUNCTION_SET_LAYER_VISIBLE_REGION,
2215    HWC2_FUNCTION_SET_LAYER_Z_ORDER,
2216    HWC2_FUNCTION_SET_OUTPUT_BUFFER,
2217    HWC2_FUNCTION_SET_POWER_MODE,
2218    HWC2_FUNCTION_SET_VSYNC_ENABLED,
2219    HWC2_FUNCTION_VALIDATE_DISPLAY,
2220}};
2221
2222/* TESTCASE: Tests that the HWC2 supports all required functions. */
2223TEST_F(Hwc2Test, GET_FUNCTION)
2224{
2225    for (hwc2_function_descriptor_t descriptor : requiredFunctions) {
2226        hwc2_function_pointer_t pfn = getFunction(descriptor);
2227        EXPECT_TRUE(pfn) << "failed to get function "
2228                << getFunctionDescriptorName(descriptor);
2229    }
2230}
2231
2232/* TESTCASE: Tests that the HWC2 fails to retrieve and invalid function. */
2233TEST_F(Hwc2Test, GET_FUNCTION_invalid_function)
2234{
2235    hwc2_function_pointer_t pfn = getFunction(HWC2_FUNCTION_INVALID);
2236    EXPECT_FALSE(pfn) << "failed to get invalid function";
2237}
2238
2239/* TESTCASE: Tests that the HWC2 does not return an invalid capability. */
2240TEST_F(Hwc2Test, GET_CAPABILITIES)
2241{
2242    std::vector<hwc2_capability_t> capabilities;
2243
2244    getCapabilities(&capabilities);
2245
2246    EXPECT_EQ(std::count(capabilities.begin(), capabilities.end(),
2247            HWC2_CAPABILITY_INVALID), 0);
2248}
2249
2250static const std::array<hwc2_callback_descriptor_t, 3> callbackDescriptors = {{
2251    HWC2_CALLBACK_HOTPLUG,
2252    HWC2_CALLBACK_REFRESH,
2253    HWC2_CALLBACK_VSYNC,
2254}};
2255
2256/* TESTCASE: Tests that the HWC2 can successfully register all required
2257 * callback functions. */
2258TEST_F(Hwc2Test, REGISTER_CALLBACK)
2259{
2260    hwc2_callback_data_t data = reinterpret_cast<hwc2_callback_data_t>(
2261            const_cast<char*>("data"));
2262
2263    for (auto descriptor : callbackDescriptors) {
2264        ASSERT_NO_FATAL_FAILURE(registerCallback(descriptor, data,
2265                []() { return; }));
2266    }
2267}
2268
2269/* TESTCASE: Test that the HWC2 fails to register invalid callbacks. */
2270TEST_F(Hwc2Test, REGISTER_CALLBACK_bad_parameter)
2271{
2272    hwc2_callback_data_t data = reinterpret_cast<hwc2_callback_data_t>(
2273            const_cast<char*>("data"));
2274    hwc2_error_t err = HWC2_ERROR_NONE;
2275
2276    ASSERT_NO_FATAL_FAILURE(registerCallback(HWC2_CALLBACK_INVALID, data,
2277            []() { return; }, &err));
2278    EXPECT_EQ(err, HWC2_ERROR_BAD_PARAMETER) << "returned wrong error code";
2279}
2280
2281/* TESTCASE: Tests that the HWC2 can register a callback with null data. */
2282TEST_F(Hwc2Test, REGISTER_CALLBACK_null_data)
2283{
2284    hwc2_callback_data_t data = nullptr;
2285
2286    for (auto descriptor : callbackDescriptors) {
2287        ASSERT_NO_FATAL_FAILURE(registerCallback(descriptor, data,
2288                []() { return; }));
2289    }
2290}
2291
2292/* TESTCASE: Tests that the HWC2 returns the correct display type for each
2293 * physical display. */
2294TEST_F(Hwc2Test, GET_DISPLAY_TYPE)
2295{
2296    for (auto display : mDisplays) {
2297        hwc2_display_type_t type;
2298
2299        ASSERT_NO_FATAL_FAILURE(getDisplayType(display, &type));
2300        EXPECT_EQ(type, HWC2_DISPLAY_TYPE_PHYSICAL) << "failed to return"
2301                " correct display type";
2302    }
2303}
2304
2305/* TESTCASE: Tests that the HWC2 returns an error when the display type of a bad
2306 * display is requested. */
2307TEST_F(Hwc2Test, GET_DISPLAY_TYPE_bad_display)
2308{
2309    hwc2_display_t display;
2310    hwc2_display_type_t type;
2311    hwc2_error_t err = HWC2_ERROR_NONE;
2312
2313    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
2314
2315    ASSERT_NO_FATAL_FAILURE(getDisplayType(display, &type, &err));
2316    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
2317}
2318
2319/* TESTCASE: Tests that the HWC2 can create and destroy layers. */
2320TEST_F(Hwc2Test, CREATE_DESTROY_LAYER)
2321{
2322    for (auto display : mDisplays) {
2323        hwc2_layer_t layer;
2324
2325        ASSERT_NO_FATAL_FAILURE(createLayer(display, &layer));
2326
2327        ASSERT_NO_FATAL_FAILURE(destroyLayer(display, layer));
2328    }
2329}
2330
2331/* TESTCASE: Tests that the HWC2 cannot create a layer for a bad display */
2332TEST_F(Hwc2Test, CREATE_LAYER_bad_display)
2333{
2334    hwc2_display_t display;
2335    hwc2_layer_t layer;
2336    hwc2_error_t err = HWC2_ERROR_NONE;
2337
2338    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
2339
2340    ASSERT_NO_FATAL_FAILURE(createLayer(display, &layer, &err));
2341    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
2342}
2343
2344/* TESTCASE: Tests that the HWC2 will either support a large number of resources
2345 * or will return no resources. */
2346TEST_F(Hwc2Test, CREATE_LAYER_no_resources)
2347{
2348    const size_t layerCnt = 1000;
2349
2350    for (auto display : mDisplays) {
2351        std::vector<hwc2_layer_t> layers;
2352
2353        ASSERT_NO_FATAL_FAILURE(createLayers(display, &layers, layerCnt));
2354
2355        ASSERT_NO_FATAL_FAILURE(destroyLayers(display, std::move(layers)));
2356    }
2357}
2358
2359/* TESTCASE: Tests that the HWC2 cannot destroy a layer for a bad display */
2360TEST_F(Hwc2Test, DESTROY_LAYER_bad_display)
2361{
2362    hwc2_display_t badDisplay;
2363
2364    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&badDisplay));
2365
2366    for (auto display : mDisplays) {
2367        hwc2_layer_t layer = 0;
2368        hwc2_error_t err = HWC2_ERROR_NONE;
2369
2370        ASSERT_NO_FATAL_FAILURE(destroyLayer(badDisplay, layer, &err));
2371        EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
2372
2373        ASSERT_NO_FATAL_FAILURE(createLayer(display, &layer));
2374
2375        ASSERT_NO_FATAL_FAILURE(destroyLayer(badDisplay, layer, &err));
2376        EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
2377
2378        ASSERT_NO_FATAL_FAILURE(destroyLayer(display, layer));
2379    }
2380}
2381
2382/* TESTCASE: Tests that the HWC2 cannot destory a bad layer */
2383TEST_F(Hwc2Test, DESTROY_LAYER_bad_layer)
2384{
2385    for (auto display : mDisplays) {
2386        hwc2_layer_t layer;
2387        hwc2_error_t err = HWC2_ERROR_NONE;
2388
2389        ASSERT_NO_FATAL_FAILURE(destroyLayer(display, UINT64_MAX / 2, &err));
2390        EXPECT_EQ(err, HWC2_ERROR_BAD_LAYER) << "returned wrong error code";
2391
2392        ASSERT_NO_FATAL_FAILURE(destroyLayer(display, 0, &err));
2393        EXPECT_EQ(err, HWC2_ERROR_BAD_LAYER) << "returned wrong error code";
2394
2395        ASSERT_NO_FATAL_FAILURE(destroyLayer(display, UINT64_MAX - 1, &err));
2396        EXPECT_EQ(err, HWC2_ERROR_BAD_LAYER) << "returned wrong error code";
2397
2398        ASSERT_NO_FATAL_FAILURE(destroyLayer(display, 1, &err));
2399        EXPECT_EQ(err, HWC2_ERROR_BAD_LAYER) << "returned wrong error code";
2400
2401        ASSERT_NO_FATAL_FAILURE(destroyLayer(display, UINT64_MAX, &err));
2402        EXPECT_EQ(err, HWC2_ERROR_BAD_LAYER) << "returned wrong error code";
2403
2404        ASSERT_NO_FATAL_FAILURE(createLayer(display, &layer));
2405
2406        ASSERT_NO_FATAL_FAILURE(destroyLayer(display, layer + 1, &err));
2407        EXPECT_EQ(err, HWC2_ERROR_BAD_LAYER) << "returned wrong error code";
2408
2409        ASSERT_NO_FATAL_FAILURE(destroyLayer(display, layer));
2410
2411        ASSERT_NO_FATAL_FAILURE(destroyLayer(display, layer, &err));
2412        EXPECT_EQ(err, HWC2_ERROR_BAD_LAYER) << "returned wrong error code";
2413    }
2414}
2415
2416static const std::array<hwc2_attribute_t, 2> requiredAttributes = {{
2417    HWC2_ATTRIBUTE_WIDTH,
2418    HWC2_ATTRIBUTE_HEIGHT,
2419}};
2420
2421static const std::array<hwc2_attribute_t, 3> optionalAttributes = {{
2422    HWC2_ATTRIBUTE_VSYNC_PERIOD,
2423    HWC2_ATTRIBUTE_DPI_X,
2424    HWC2_ATTRIBUTE_DPI_Y,
2425}};
2426
2427/* TESTCASE: Tests that the HWC2 can return display attributes for a valid
2428 * config. */
2429TEST_F(Hwc2Test, GET_DISPLAY_ATTRIBUTE)
2430{
2431    for (auto display : mDisplays) {
2432        std::vector<hwc2_config_t> configs;
2433
2434        ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
2435
2436        for (auto config : configs) {
2437            int32_t value;
2438
2439            for (auto attribute : requiredAttributes) {
2440                ASSERT_NO_FATAL_FAILURE(getDisplayAttribute(display, config,
2441                        attribute, &value));
2442                EXPECT_GE(value, 0) << "missing required attribute "
2443                        << getAttributeName(attribute) << " for config "
2444                        << config;
2445            }
2446            for (auto attribute : optionalAttributes) {
2447                ASSERT_NO_FATAL_FAILURE(getDisplayAttribute(display, config,
2448                        attribute, &value));
2449            }
2450        }
2451    }
2452}
2453
2454/* TESTCASE: Tests that the HWC2 will return a value of -1 for an invalid
2455 * attribute */
2456TEST_F(Hwc2Test, GET_DISPLAY_ATTRIBUTE_invalid_attribute)
2457{
2458    const hwc2_attribute_t attribute = HWC2_ATTRIBUTE_INVALID;
2459
2460    for (auto display : mDisplays) {
2461        std::vector<hwc2_config_t> configs;
2462
2463        ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
2464
2465        for (auto config : configs) {
2466            int32_t value;
2467            hwc2_error_t err = HWC2_ERROR_NONE;
2468
2469            ASSERT_NO_FATAL_FAILURE(getDisplayAttribute(display, config,
2470                    attribute, &value, &err));
2471            EXPECT_EQ(value, -1) << "failed to return -1 for an invalid"
2472                    " attribute for config " << config;
2473        }
2474    }
2475}
2476
2477/* TESTCASE: Tests that the HWC2 will fail to get attributes for a bad display */
2478TEST_F(Hwc2Test, GET_DISPLAY_ATTRIBUTE_bad_display)
2479{
2480    hwc2_display_t display;
2481    const hwc2_config_t config = 0;
2482    int32_t value;
2483    hwc2_error_t err = HWC2_ERROR_NONE;
2484
2485    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
2486
2487    for (auto attribute : requiredAttributes) {
2488        ASSERT_NO_FATAL_FAILURE(getDisplayAttribute(display, config, attribute,
2489                &value, &err));
2490        EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
2491    }
2492
2493    for (auto attribute : optionalAttributes) {
2494        ASSERT_NO_FATAL_FAILURE(getDisplayAttribute(display, config, attribute,
2495                &value, &err));
2496        EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
2497    }
2498}
2499
2500/* TESTCASE: Tests that the HWC2 will fail to get attributes for a bad config */
2501TEST_F(Hwc2Test, GET_DISPLAY_ATTRIBUTE_bad_config)
2502{
2503    for (auto display : mDisplays) {
2504        hwc2_config_t config;
2505        int32_t value;
2506        hwc2_error_t err = HWC2_ERROR_NONE;
2507
2508        ASSERT_NO_FATAL_FAILURE(getInvalidConfig(display, &config));
2509
2510        for (auto attribute : requiredAttributes) {
2511            ASSERT_NO_FATAL_FAILURE(getDisplayAttribute(display, config,
2512                    attribute, &value, &err));
2513            EXPECT_EQ(err, HWC2_ERROR_BAD_CONFIG) << "returned wrong error code";
2514        }
2515
2516        for (auto attribute : optionalAttributes) {
2517            ASSERT_NO_FATAL_FAILURE(getDisplayAttribute(display, config,
2518                    attribute, &value, &err));
2519            EXPECT_EQ(err, HWC2_ERROR_BAD_CONFIG) << "returned wrong error code";
2520        }
2521    }
2522}
2523
2524/* TESTCASE: Tests that the HWC2 will get display configs for active displays */
2525TEST_F(Hwc2Test, GET_DISPLAY_CONFIGS)
2526{
2527    for (auto display : mDisplays) {
2528        std::vector<hwc2_config_t> configs;
2529
2530        ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
2531    }
2532}
2533
2534/* TESTCASE: Tests that the HWC2 will not get display configs for bad displays */
2535TEST_F(Hwc2Test, GET_DISPLAY_CONFIGS_bad_display)
2536{
2537    hwc2_display_t display;
2538    std::vector<hwc2_config_t> configs;
2539    hwc2_error_t err = HWC2_ERROR_NONE;
2540
2541    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
2542
2543    ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs, &err));
2544
2545    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
2546    EXPECT_TRUE(configs.empty()) << "returned configs for bad display";
2547}
2548
2549/* TESTCASE: Tests that the HWC2 will return the same config list multiple
2550 * times in a row. */
2551TEST_F(Hwc2Test, GET_DISPLAY_CONFIGS_same)
2552{
2553    for (auto display : mDisplays) {
2554        std::vector<hwc2_config_t> configs1, configs2;
2555
2556        ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs1));
2557        ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs2));
2558
2559        EXPECT_TRUE(std::is_permutation(configs1.begin(), configs1.end(),
2560                configs2.begin())) << "returned two different config sets";
2561    }
2562}
2563
2564/* TESTCASE: Tests that the HWC2 does not return duplicate display configs */
2565TEST_F(Hwc2Test, GET_DISPLAY_CONFIGS_duplicate)
2566{
2567    for (auto display : mDisplays) {
2568        std::vector<hwc2_config_t> configs;
2569
2570        ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
2571
2572        std::unordered_set<hwc2_config_t> configsSet(configs.begin(),
2573                configs.end());
2574        EXPECT_EQ(configs.size(), configsSet.size()) << "returned duplicate"
2575                " configs";
2576    }
2577}
2578
2579/* TESTCASE: Tests that the HWC2 returns the active config for a display */
2580TEST_F(Hwc2Test, GET_ACTIVE_CONFIG)
2581{
2582    for (auto display : mDisplays) {
2583        std::vector<hwc2_config_t> configs;
2584
2585        ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
2586
2587        for (auto config : configs) {
2588            hwc2_config_t activeConfig;
2589
2590            ASSERT_NO_FATAL_FAILURE(setActiveConfig(display, config));
2591            ASSERT_NO_FATAL_FAILURE(getActiveConfig(display, &activeConfig));
2592
2593            EXPECT_EQ(activeConfig, config) << "failed to get active config";
2594        }
2595    }
2596}
2597
2598/* TESTCASE: Tests that the HWC2 does not return an active config for a bad
2599 * display. */
2600TEST_F(Hwc2Test, GET_ACTIVE_CONFIG_bad_display)
2601{
2602    hwc2_display_t display;
2603    hwc2_config_t activeConfig;
2604    hwc2_error_t err = HWC2_ERROR_NONE;
2605
2606    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
2607
2608    ASSERT_NO_FATAL_FAILURE(getActiveConfig(display, &activeConfig, &err));
2609
2610    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
2611}
2612
2613/* TESTCASE: Tests that the HWC2 either begins with a valid active config
2614 * or returns an error when getActiveConfig is called. */
2615TEST_F(Hwc2Test, GET_ACTIVE_CONFIG_bad_config)
2616{
2617    for (auto display : mDisplays) {
2618        std::vector<hwc2_config_t> configs;
2619        hwc2_config_t activeConfig;
2620        hwc2_error_t err = HWC2_ERROR_NONE;
2621
2622        ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
2623
2624        if (configs.empty())
2625            return;
2626
2627        ASSERT_NO_FATAL_FAILURE(getActiveConfig(display, &activeConfig, &err));
2628        if (err == HWC2_ERROR_NONE) {
2629            EXPECT_NE(std::count(configs.begin(), configs.end(),
2630                    activeConfig), 0) << "active config is not found in "
2631                    " configs for display";
2632        } else {
2633            EXPECT_EQ(err, HWC2_ERROR_BAD_CONFIG) << "returned wrong error code";
2634        }
2635    }
2636}
2637
2638/* TESTCASE: Tests that the HWC2 can set every display config as an active
2639 * config */
2640TEST_F(Hwc2Test, SET_ACTIVE_CONFIG)
2641{
2642    for (auto display : mDisplays) {
2643        std::vector<hwc2_config_t> configs;
2644
2645        ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
2646
2647        for (auto config : configs) {
2648            EXPECT_NO_FATAL_FAILURE(setActiveConfig(display, config));
2649        }
2650    }
2651}
2652
2653/* TESTCASE: Tests that the HWC2 cannot set an active config for a bad display */
2654TEST_F(Hwc2Test, SET_ACTIVE_CONFIG_bad_display)
2655{
2656    hwc2_display_t display;
2657    const hwc2_config_t config = 0;
2658    hwc2_error_t err = HWC2_ERROR_NONE;
2659
2660    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
2661
2662    ASSERT_NO_FATAL_FAILURE(setActiveConfig(display, config, &err));
2663    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
2664}
2665
2666/* TESTCASE: Tests that the HWC2 cannot set an invalid active config */
2667TEST_F(Hwc2Test, SET_ACTIVE_CONFIG_bad_config)
2668{
2669    for (auto display : mDisplays) {
2670        hwc2_config_t config;
2671        hwc2_error_t err = HWC2_ERROR_NONE;
2672
2673        ASSERT_NO_FATAL_FAILURE(getInvalidConfig(display, &config));
2674
2675        ASSERT_NO_FATAL_FAILURE(setActiveConfig(display, config, &err));
2676        EXPECT_EQ(err, HWC2_ERROR_BAD_CONFIG) << "returned wrong error code";
2677    }
2678}
2679
2680/* TESTCASE: Tests that the HWC2 returns a valid value for getDozeSupport. */
2681TEST_F(Hwc2Test, GET_DOZE_SUPPORT)
2682{
2683    for (auto display : mDisplays) {
2684        int32_t support = -1;
2685
2686        ASSERT_NO_FATAL_FAILURE(getDozeSupport(display, &support));
2687
2688        EXPECT_TRUE(support == 0 || support == 1) << "invalid doze support value";
2689    }
2690}
2691
2692/* TESTCASE: Tests that the HWC2 cannot get doze support for a bad display. */
2693TEST_F(Hwc2Test, GET_DOZE_SUPPORT_bad_display)
2694{
2695    hwc2_display_t display;
2696    int32_t support = -1;
2697    hwc2_error_t err = HWC2_ERROR_NONE;
2698
2699    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
2700
2701    ASSERT_NO_FATAL_FAILURE(getDozeSupport(display, &support, &err));
2702
2703    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
2704}
2705
2706/* TESTCASE: Tests that the HWC2 can set all supported power modes */
2707TEST_F(Hwc2Test, SET_POWER_MODE)
2708{
2709    for (auto display : mDisplays) {
2710        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_ON));
2711        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF));
2712
2713        int32_t support = -1;
2714        ASSERT_NO_FATAL_FAILURE(getDozeSupport(display, &support));
2715        if (support != 1)
2716            return;
2717
2718        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_DOZE));
2719        ASSERT_NO_FATAL_FAILURE(setPowerMode(display,
2720                HWC2_POWER_MODE_DOZE_SUSPEND));
2721
2722        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF));
2723    }
2724}
2725
2726/* TESTCASE: Tests that the HWC2 cannot set a power mode for a bad display. */
2727TEST_F(Hwc2Test, SET_POWER_MODE_bad_display)
2728{
2729    hwc2_display_t display;
2730    hwc2_error_t err = HWC2_ERROR_NONE;
2731
2732    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
2733
2734    ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_ON, &err));
2735    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
2736
2737    ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF, &err));
2738    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
2739
2740    int32_t support = -1;
2741    ASSERT_NO_FATAL_FAILURE(getDozeSupport(display, &support, &err));
2742    if (support != 1)
2743        return;
2744
2745    ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_DOZE, &err));
2746    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
2747
2748    ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_DOZE_SUSPEND,
2749            &err));
2750    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
2751}
2752
2753/* TESTCASE: Tests that the HWC2 cannot set an invalid power mode value. */
2754TEST_F(Hwc2Test, SET_POWER_MODE_bad_parameter)
2755{
2756    for (auto display : mDisplays) {
2757        hwc2_power_mode_t mode = static_cast<hwc2_power_mode_t>(
2758                HWC2_POWER_MODE_DOZE_SUSPEND + 1);
2759        hwc2_error_t err = HWC2_ERROR_NONE;
2760
2761        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, mode, &err));
2762        EXPECT_EQ(err, HWC2_ERROR_BAD_PARAMETER) << "returned wrong error code "
2763                << mode;
2764    }
2765}
2766
2767/* TESTCASE: Tests that the HWC2 will return unsupported if it does not support
2768 * an optional power mode. */
2769TEST_F(Hwc2Test, SET_POWER_MODE_unsupported)
2770{
2771    for (auto display : mDisplays) {
2772        int32_t support = -1;
2773        hwc2_error_t err = HWC2_ERROR_NONE;
2774
2775        ASSERT_NO_FATAL_FAILURE(getDozeSupport(display, &support, &err));
2776        if (support == 1)
2777            return;
2778
2779        ASSERT_EQ(support, 0) << "invalid doze support value";
2780
2781        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_DOZE,
2782                &err));
2783        EXPECT_EQ(err, HWC2_ERROR_UNSUPPORTED) << "returned wrong error code";
2784
2785        ASSERT_NO_FATAL_FAILURE(setPowerMode(display,
2786                HWC2_POWER_MODE_DOZE_SUSPEND, &err));
2787        EXPECT_EQ(err, HWC2_ERROR_UNSUPPORTED) <<  "returned wrong error code";
2788    }
2789}
2790
2791/* TESTCASE: Tests that the HWC2 can set the same power mode multiple times. */
2792TEST_F(Hwc2Test, SET_POWER_MODE_stress)
2793{
2794    for (auto display : mDisplays) {
2795        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF));
2796        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF));
2797
2798        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_ON));
2799        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_ON));
2800
2801        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF));
2802        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF));
2803
2804        int32_t support = -1;
2805        ASSERT_NO_FATAL_FAILURE(getDozeSupport(display, &support));
2806        if (support != 1)
2807            return;
2808
2809        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_DOZE));
2810        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_DOZE));
2811
2812        ASSERT_NO_FATAL_FAILURE(setPowerMode(display,
2813                HWC2_POWER_MODE_DOZE_SUSPEND));
2814        ASSERT_NO_FATAL_FAILURE(setPowerMode(display,
2815                HWC2_POWER_MODE_DOZE_SUSPEND));
2816
2817        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF));
2818    }
2819}
2820
2821/* TESTCASE: Tests that the HWC2 can enable and disable vsync on active
2822 * displays */
2823TEST_F(Hwc2Test, SET_VSYNC_ENABLED)
2824{
2825    for (auto display : mDisplays) {
2826        hwc2_callback_data_t data = reinterpret_cast<hwc2_callback_data_t>(
2827                const_cast<char*>("data"));
2828
2829        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_ON));
2830
2831        ASSERT_NO_FATAL_FAILURE(registerCallback(HWC2_CALLBACK_VSYNC, data,
2832                []() { return; }));
2833
2834        ASSERT_NO_FATAL_FAILURE(setVsyncEnabled(display, HWC2_VSYNC_ENABLE));
2835
2836        ASSERT_NO_FATAL_FAILURE(setVsyncEnabled(display, HWC2_VSYNC_DISABLE));
2837
2838        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF));
2839    }
2840}
2841
2842/* TESTCASE: Tests that the HWC2 issues a valid vsync callback. */
2843TEST_F(Hwc2Test, SET_VSYNC_ENABLED_callback)
2844{
2845    for (auto display : mDisplays) {
2846        hwc2_display_t receivedDisplay;
2847        int64_t receivedTimestamp;
2848
2849        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_ON));
2850
2851        ASSERT_NO_FATAL_FAILURE(enableVsync(display));
2852
2853        ASSERT_NO_FATAL_FAILURE(waitForVsync(&receivedDisplay,
2854                &receivedTimestamp));
2855
2856        EXPECT_EQ(receivedDisplay, display) << "failed to get correct display";
2857        EXPECT_GE(receivedTimestamp, 0) << "failed to get valid timestamp";
2858
2859        ASSERT_NO_FATAL_FAILURE(disableVsync(display));
2860
2861        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF));
2862    }
2863}
2864
2865/* TESTCASE: Tests that the HWC2 cannot enable a vsync for a bad display */
2866TEST_F(Hwc2Test, SET_VSYNC_ENABLED_bad_display)
2867{
2868    hwc2_display_t display;
2869    hwc2_callback_data_t data = reinterpret_cast<hwc2_callback_data_t>(
2870            const_cast<char*>("data"));
2871    hwc2_error_t err = HWC2_ERROR_NONE;
2872
2873    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
2874
2875    ASSERT_NO_FATAL_FAILURE(registerCallback(HWC2_CALLBACK_VSYNC, data,
2876            []() { return; }));
2877
2878    ASSERT_NO_FATAL_FAILURE(setVsyncEnabled(display, HWC2_VSYNC_ENABLE, &err));
2879    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
2880
2881    ASSERT_NO_FATAL_FAILURE(setVsyncEnabled(display, HWC2_VSYNC_DISABLE, &err));
2882    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
2883}
2884
2885/* TESTCASE: Tests that the HWC2 cannot enable an invalid vsync value */
2886TEST_F(Hwc2Test, SET_VSYNC_ENABLED_bad_parameter)
2887{
2888    for (auto display : mDisplays) {
2889        hwc2_callback_data_t data = reinterpret_cast<hwc2_callback_data_t>(
2890                const_cast<char*>("data"));
2891        hwc2_error_t err = HWC2_ERROR_NONE;
2892
2893        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_ON));
2894
2895        ASSERT_NO_FATAL_FAILURE(registerCallback(HWC2_CALLBACK_VSYNC, data,
2896                []() { return; }));
2897
2898        ASSERT_NO_FATAL_FAILURE(setVsyncEnabled(display, HWC2_VSYNC_INVALID,
2899                &err));
2900        EXPECT_EQ(err, HWC2_ERROR_BAD_PARAMETER) << "returned wrong error code";
2901
2902        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF));
2903    }
2904}
2905
2906/* TESTCASE: Tests that the HWC2 can enable and disable a vsync value multiple
2907 * times. */
2908TEST_F(Hwc2Test, SET_VSYNC_ENABLED_stress)
2909{
2910    for (auto display : mDisplays) {
2911        hwc2_callback_data_t data = reinterpret_cast<hwc2_callback_data_t>(
2912                const_cast<char*>("data"));
2913
2914        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_ON));
2915
2916        ASSERT_NO_FATAL_FAILURE(registerCallback(HWC2_CALLBACK_VSYNC, data,
2917                []() { return; }));
2918
2919        ASSERT_NO_FATAL_FAILURE(setVsyncEnabled(display, HWC2_VSYNC_DISABLE));
2920
2921        ASSERT_NO_FATAL_FAILURE(setVsyncEnabled(display, HWC2_VSYNC_ENABLE));
2922        ASSERT_NO_FATAL_FAILURE(setVsyncEnabled(display, HWC2_VSYNC_ENABLE));
2923
2924        ASSERT_NO_FATAL_FAILURE(setVsyncEnabled(display, HWC2_VSYNC_DISABLE));
2925        ASSERT_NO_FATAL_FAILURE(setVsyncEnabled(display, HWC2_VSYNC_DISABLE));
2926
2927        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF));
2928    }
2929}
2930
2931/* TESTCASE: Tests that the HWC2 can set a vsync enable value when the display
2932 * is off and no callback is registered. */
2933TEST_F(Hwc2Test, SET_VSYNC_ENABLED_no_callback_no_power)
2934{
2935    const uint secs = 1;
2936
2937    for (auto display : mDisplays) {
2938        ASSERT_NO_FATAL_FAILURE(setVsyncEnabled(display, HWC2_VSYNC_ENABLE));
2939
2940        sleep(secs);
2941
2942        ASSERT_NO_FATAL_FAILURE(setVsyncEnabled(display, HWC2_VSYNC_DISABLE));
2943    }
2944}
2945
2946/* TESTCASE: Tests that the HWC2 can set a vsync enable value when no callback
2947 * is registered. */
2948TEST_F(Hwc2Test, SET_VSYNC_ENABLED_no_callback)
2949{
2950    const uint secs = 1;
2951
2952    for (auto display : mDisplays) {
2953        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_ON));
2954
2955        ASSERT_NO_FATAL_FAILURE(setVsyncEnabled(display, HWC2_VSYNC_ENABLE));
2956
2957        sleep(secs);
2958
2959        ASSERT_NO_FATAL_FAILURE(setVsyncEnabled(display, HWC2_VSYNC_DISABLE));
2960
2961        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF));
2962    }
2963}
2964
2965/* TESTCASE: Tests that the HWC2 returns a display name for each display */
2966TEST_F(Hwc2Test, GET_DISPLAY_NAME)
2967{
2968    for (auto display : mDisplays) {
2969        std::string name;
2970
2971        ASSERT_NO_FATAL_FAILURE(getDisplayName(display, &name));
2972    }
2973}
2974
2975/* TESTCASE: Tests that the HWC2 does not return a display name for a bad
2976 * display */
2977TEST_F(Hwc2Test, GET_DISPLAY_NAME_bad_display)
2978{
2979    hwc2_display_t display;
2980    std::string name;
2981    hwc2_error_t err = HWC2_ERROR_NONE;
2982
2983    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
2984
2985    ASSERT_NO_FATAL_FAILURE(getDisplayName(display, &name, &err));
2986    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
2987}
2988
2989/* TESTCASE: Tests that the HWC2 can set basic composition types. */
2990TEST_F(Hwc2Test, SET_LAYER_COMPOSITION_TYPE)
2991{
2992    ASSERT_NO_FATAL_FAILURE(setLayerProperty(Hwc2TestCoverage::Complete,
2993            setComposition, advanceComposition));
2994}
2995
2996/* TESTCASE: Tests that the HWC2 can update a basic composition type on a
2997 * layer. */
2998TEST_F(Hwc2Test, SET_LAYER_COMPOSITION_TYPE_update)
2999{
3000    ASSERT_NO_FATAL_FAILURE(setLayerPropertyUpdate(Hwc2TestCoverage::Complete,
3001            setComposition, advanceComposition));
3002}
3003
3004/* TESTCASE: Tests that the HWC2 cannot set a composition type for a bad layer */
3005TEST_F(Hwc2Test, SET_LAYER_COMPOSITION_TYPE_bad_layer)
3006{
3007    ASSERT_NO_FATAL_FAILURE(setLayerPropertyBadLayer(Hwc2TestCoverage::Default,
3008            setComposition));
3009}
3010
3011/* TESTCASE: Tests that the HWC2 cannot set a bad composition type */
3012TEST_F(Hwc2Test, SET_LAYER_COMPOSITION_TYPE_bad_parameter)
3013{
3014    ASSERT_NO_FATAL_FAILURE(setLayerPropertyBadParameter(
3015            [] (Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
3016                    hwc2_error_t* outErr) {
3017
3018                ASSERT_NO_FATAL_FAILURE(test->setLayerCompositionType(display,
3019                        layer, HWC2_COMPOSITION_INVALID, outErr));
3020            }
3021    ));
3022}
3023
3024/* TESTCASE: Tests that the HWC2 can set the cursor position of a layer. */
3025TEST_F(Hwc2Test, SET_CURSOR_POSITION)
3026{
3027    ASSERT_NO_FATAL_FAILURE(setLayerProperty(Hwc2TestCoverage::Complete,
3028            ::setCursorPosition, advanceCursorPosition));
3029}
3030
3031/* TESTCASE: Tests that the HWC2 can update the cursor position of a layer. */
3032TEST_F(Hwc2Test, SET_CURSOR_POSITION_update)
3033{
3034    ASSERT_NO_FATAL_FAILURE(setLayerPropertyUpdate(Hwc2TestCoverage::Complete,
3035            ::setCursorPosition, advanceCursorPosition));
3036}
3037
3038/* TESTCASE: Tests that the HWC2 can set the cursor position of a layer when the
3039 * composition type has not been set to HWC2_COMPOSITION_CURSOR. */
3040TEST_F(Hwc2Test, SET_CURSOR_POSITION_composition_type_unset)
3041{
3042    ASSERT_NO_FATAL_FAILURE(setLayerProperty(Hwc2TestCoverage::Complete,
3043            [] (Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
3044                    Hwc2TestLayer* testLayer, hwc2_error_t* outErr) {
3045                const hwc_rect_t cursorPosition = testLayer->getCursorPosition();
3046                EXPECT_NO_FATAL_FAILURE(test->setCursorPosition(display, layer,
3047                        cursorPosition.left, cursorPosition.top, outErr));
3048            },
3049
3050            advanceCursorPosition));
3051}
3052
3053/* TESTCASE: Tests that the HWC2 cannot set the cursor position of a bad
3054 * display. */
3055TEST_F(Hwc2Test, SET_CURSOR_POSITION_bad_display)
3056{
3057    hwc2_display_t display;
3058    hwc2_layer_t layer = 0;
3059    int32_t x = 0, y = 0;
3060    hwc2_error_t err = HWC2_ERROR_NONE;
3061
3062    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
3063
3064    ASSERT_NO_FATAL_FAILURE(setCursorPosition(display, layer, x, y, &err));
3065    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
3066}
3067
3068/* TESTCASE: Tests that the HWC2 cannot set the cursor position of a bad layer. */
3069TEST_F(Hwc2Test, SET_CURSOR_POSITION_bad_layer)
3070{
3071    ASSERT_NO_FATAL_FAILURE(setLayerPropertyBadLayer(Hwc2TestCoverage::Default,
3072            [] (Hwc2Test* test, hwc2_display_t display, hwc2_layer_t badLayer,
3073                    Hwc2TestLayer* testLayer, hwc2_error_t* outErr) {
3074
3075                const hwc_rect_t cursorPosition = testLayer->getCursorPosition();
3076                EXPECT_NO_FATAL_FAILURE(test->setCursorPosition(display,
3077                        badLayer, cursorPosition.left, cursorPosition.top,
3078                        outErr));
3079            }
3080   ));
3081}
3082
3083/* TESTCASE: Tests that the HWC2 can set a blend mode value of a layer. */
3084TEST_F(Hwc2Test, SET_LAYER_BLEND_MODE)
3085{
3086    ASSERT_NO_FATAL_FAILURE(setLayerProperty(Hwc2TestCoverage::Complete,
3087            setBlendMode, advanceBlendMode));
3088}
3089
3090/* TESTCASE: Tests that the HWC2 can update a blend mode value of a layer. */
3091TEST_F(Hwc2Test, SET_LAYER_BLEND_MODE_update)
3092{
3093    ASSERT_NO_FATAL_FAILURE(setLayerPropertyUpdate(Hwc2TestCoverage::Complete,
3094            setBlendMode, advanceBlendMode));
3095}
3096
3097/* TESTCASE: Tests that the HWC2 cannot set a blend mode for a bad layer. */
3098TEST_F(Hwc2Test, SET_LAYER_BLEND_MODE_bad_layer)
3099{
3100    ASSERT_NO_FATAL_FAILURE(setLayerPropertyBadLayer(Hwc2TestCoverage::Default,
3101            setBlendMode));
3102}
3103
3104/* TESTCASE: Tests that the HWC2 cannot set an invalid blend mode. */
3105TEST_F(Hwc2Test, SET_LAYER_BLEND_MODE_bad_parameter)
3106{
3107    ASSERT_NO_FATAL_FAILURE(setLayerPropertyBadParameter(
3108            [] (Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
3109                    hwc2_error_t* outErr) {
3110
3111                ASSERT_NO_FATAL_FAILURE(test->setLayerBlendMode(display,
3112                        layer, HWC2_BLEND_MODE_INVALID, outErr));
3113            }
3114    ));
3115}
3116
3117/* TESTCASE: Tests that the HWC2 can set the buffer of a layer. */
3118TEST_F(Hwc2Test, SET_LAYER_BUFFER)
3119{
3120    ASSERT_NO_FATAL_FAILURE(setLayerProperty(Hwc2TestCoverage::Complete,
3121            setBuffer, advanceBuffer));
3122}
3123
3124/* TESTCASE: Tests that the HWC2 can update the buffer of a layer. */
3125TEST_F(Hwc2Test, SET_LAYER_BUFFER_update)
3126{
3127    ASSERT_NO_FATAL_FAILURE(setLayerPropertyUpdate(Hwc2TestCoverage::Complete,
3128            setBuffer, advanceBuffer));
3129}
3130
3131/* TESTCASE: Tests that the HWC2 cannot set the buffer of a bad layer. */
3132TEST_F(Hwc2Test, SET_LAYER_BUFFER_bad_layer)
3133{
3134    ASSERT_NO_FATAL_FAILURE(setLayerPropertyBadLayer(Hwc2TestCoverage::Default,
3135            [] (Hwc2Test* test, hwc2_display_t display, hwc2_layer_t badLayer,
3136                    Hwc2TestLayer* testLayer, hwc2_error_t* outErr) {
3137
3138                buffer_handle_t handle = nullptr;
3139                android::base::unique_fd acquireFence;
3140
3141                /* If there is not available buffer for the given buffer
3142                 * properties, it should not fail this test case */
3143                if (testLayer->getBuffer(&handle, &acquireFence) == 0) {
3144                    *outErr = HWC2_ERROR_BAD_LAYER;
3145                    return;
3146                }
3147
3148                ASSERT_NO_FATAL_FAILURE(test->setLayerBuffer(display, badLayer,
3149                        handle, acquireFence, outErr));
3150            }
3151    ));
3152}
3153
3154/* TESTCASE: Tests that the HWC2 can set an invalid buffer for a layer. */
3155TEST_F(Hwc2Test, SET_LAYER_BUFFER_bad_parameter)
3156{
3157    ASSERT_NO_FATAL_FAILURE(setLayerPropertyBadParameter(
3158            [] (Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
3159                    hwc2_error_t* outErr) {
3160
3161                buffer_handle_t handle = nullptr;
3162                int32_t acquireFence = -1;
3163
3164                ASSERT_NO_FATAL_FAILURE(test->setLayerBuffer(display, layer,
3165                        handle, acquireFence, outErr));
3166            }
3167    ));
3168}
3169
3170/* TESTCASE: Tests that the HWC2 can set the color of a layer. */
3171TEST_F(Hwc2Test, SET_LAYER_COLOR)
3172{
3173    ASSERT_NO_FATAL_FAILURE(setLayerProperty(Hwc2TestCoverage::Complete,
3174            setColor, advanceColor));
3175}
3176
3177/* TESTCASE: Tests that the HWC2 can update the color of a layer. */
3178TEST_F(Hwc2Test, SET_LAYER_COLOR_update)
3179{
3180    ASSERT_NO_FATAL_FAILURE(setLayerPropertyUpdate(Hwc2TestCoverage::Complete,
3181            setColor, advanceColor));
3182}
3183
3184/* TESTCASE: Tests that the HWC2 can set the color of a layer when the
3185 * composition type has not been set to HWC2_COMPOSITION_SOLID_COLOR. */
3186TEST_F(Hwc2Test, SET_LAYER_COLOR_composition_type_unset)
3187{
3188    ASSERT_NO_FATAL_FAILURE(setLayerProperty(Hwc2TestCoverage::Basic,
3189            [] (Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
3190                    Hwc2TestLayer* testLayer, hwc2_error_t* outErr) {
3191
3192                EXPECT_NO_FATAL_FAILURE(test->setLayerColor(display, layer,
3193                        testLayer->getColor(), outErr));
3194            },
3195
3196            advanceColor));
3197}
3198
3199/* TESTCASE: Tests that the HWC2 cannot set the color of a bad layer. */
3200TEST_F(Hwc2Test, SET_LAYER_COLOR_bad_layer)
3201{
3202    ASSERT_NO_FATAL_FAILURE(setLayerPropertyBadLayer(Hwc2TestCoverage::Default,
3203            [] (Hwc2Test* test, hwc2_display_t display, hwc2_layer_t badLayer,
3204                    Hwc2TestLayer* testLayer, hwc2_error_t* outErr) {
3205
3206                EXPECT_NO_FATAL_FAILURE(test->setLayerColor(display, badLayer,
3207                        testLayer->getColor(), outErr));
3208            }
3209    ));
3210}
3211
3212/* TESTCASE: Tests that the HWC2 can set the dataspace of a layer. */
3213TEST_F(Hwc2Test, SET_LAYER_DATASPACE)
3214{
3215    ASSERT_NO_FATAL_FAILURE(setLayerProperty(Hwc2TestCoverage::Complete,
3216            setDataspace, advanceDataspace));
3217}
3218
3219/* TESTCASE: Tests that the HWC2 can update the dataspace of a layer. */
3220TEST_F(Hwc2Test, SET_LAYER_DATASPACE_update)
3221{
3222    ASSERT_NO_FATAL_FAILURE(setLayerPropertyUpdate(Hwc2TestCoverage::Complete,
3223            setDataspace, advanceDataspace));
3224}
3225
3226/* TESTCASE: Tests that the HWC2 cannot set a dataspace for a bad layer. */
3227TEST_F(Hwc2Test, SET_LAYER_DATASPACE_bad_layer)
3228{
3229    ASSERT_NO_FATAL_FAILURE(setLayerPropertyBadLayer(Hwc2TestCoverage::Default,
3230            setDataspace));
3231}
3232
3233/* TESTCASE: Tests that the HWC2 can set the display frame of a layer. */
3234TEST_F(Hwc2Test, SET_LAYER_DISPLAY_FRAME)
3235{
3236    ASSERT_NO_FATAL_FAILURE(setLayerProperty(Hwc2TestCoverage::Complete,
3237            setDisplayFrame, advanceDisplayFrame));
3238}
3239
3240/* TESTCASE: Tests that the HWC2 can update the display frame of a layer. */
3241TEST_F(Hwc2Test, SET_LAYER_DISPLAY_FRAME_update)
3242{
3243    ASSERT_NO_FATAL_FAILURE(setLayerPropertyUpdate(Hwc2TestCoverage::Complete,
3244            setDisplayFrame, advanceDisplayFrame));
3245}
3246
3247/* TESTCASE: Tests that the HWC2 cannot set the display frame of a bad layer. */
3248TEST_F(Hwc2Test, SET_LAYER_DISPLAY_FRAME_bad_layer)
3249{
3250    ASSERT_NO_FATAL_FAILURE(setLayerPropertyBadLayer(Hwc2TestCoverage::Default,
3251            setDisplayFrame));
3252}
3253
3254/* TESTCASE: Tests that the HWC2 can set the plane alpha of a layer. */
3255TEST_F(Hwc2Test, SET_LAYER_PLANE_ALPHA)
3256{
3257    ASSERT_NO_FATAL_FAILURE(setLayerProperty(Hwc2TestCoverage::Complete,
3258            setPlaneAlpha, advancePlaneAlpha));
3259}
3260
3261/* TESTCASE: Tests that the HWC2 can update the plane alpha of a layer. */
3262TEST_F(Hwc2Test, SET_LAYER_PLANE_ALPHA_update)
3263{
3264    ASSERT_NO_FATAL_FAILURE(setLayerPropertyUpdate(Hwc2TestCoverage::Complete,
3265            setPlaneAlpha, advancePlaneAlpha));
3266}
3267
3268/* TESTCASE: Tests that the HWC2 cannot set a plane alpha for a bad layer. */
3269TEST_F(Hwc2Test, SET_LAYER_PLANE_ALPHA_bad_layer)
3270{
3271    ASSERT_NO_FATAL_FAILURE(setLayerPropertyBadLayer(Hwc2TestCoverage::Default,
3272            [] (Hwc2Test* test, hwc2_display_t display, hwc2_layer_t badLayer,
3273                    Hwc2TestLayer* testLayer, hwc2_error_t *outErr) {
3274
3275                    EXPECT_NO_FATAL_FAILURE(test->setLayerPlaneAlpha(display,
3276                            badLayer, testLayer->getPlaneAlpha(), outErr));
3277            }
3278    ));
3279}
3280
3281/* TESTCASE: Tests that the HWC2 can set the source crop of a layer. */
3282TEST_F(Hwc2Test, SET_LAYER_SOURCE_CROP)
3283{
3284    ASSERT_NO_FATAL_FAILURE(setLayerProperty(Hwc2TestCoverage::Complete,
3285            setSourceCrop, advanceSourceCrop));
3286}
3287
3288/* TESTCASE: Tests that the HWC2 can update the source crop of a layer. */
3289TEST_F(Hwc2Test, SET_LAYER_SOURCE_CROP_update)
3290{
3291    ASSERT_NO_FATAL_FAILURE(setLayerPropertyUpdate(Hwc2TestCoverage::Complete,
3292            setSourceCrop, advanceSourceCrop));
3293}
3294
3295/* TESTCASE: Tests that the HWC2 cannot set the source crop of a bad layer. */
3296TEST_F(Hwc2Test, SET_LAYER_SOURCE_CROP_bad_layer)
3297{
3298    ASSERT_NO_FATAL_FAILURE(setLayerPropertyBadLayer(Hwc2TestCoverage::Default,
3299            setSourceCrop));
3300}
3301
3302/* TESTCASE: Tests that the HWC2 can set the surface damage of a layer. */
3303TEST_F(Hwc2Test, SET_LAYER_SURFACE_DAMAGE)
3304{
3305    ASSERT_NO_FATAL_FAILURE(setLayerProperty(Hwc2TestCoverage::Complete,
3306            setSurfaceDamage, advanceSurfaceDamage));
3307}
3308
3309/* TESTCASE: Tests that the HWC2 can update the surface damage of a layer. */
3310TEST_F(Hwc2Test, SET_LAYER_SURFACE_DAMAGE_update)
3311{
3312    ASSERT_NO_FATAL_FAILURE(setLayerPropertyUpdate(Hwc2TestCoverage::Complete,
3313            setSurfaceDamage, advanceSurfaceDamage));
3314}
3315
3316/* TESTCASE: Tests that the HWC2 cannot set the surface damage of a bad layer. */
3317TEST_F(Hwc2Test, SET_LAYER_SURFACE_DAMAGE_bad_layer)
3318{
3319    ASSERT_NO_FATAL_FAILURE(setLayerPropertyBadLayer(Hwc2TestCoverage::Default,
3320            setSurfaceDamage));
3321}
3322
3323/* TESTCASE: Tests that the HWC2 can set the transform value of a layer. */
3324TEST_F(Hwc2Test, SET_LAYER_TRANSFORM)
3325{
3326    ASSERT_NO_FATAL_FAILURE(setLayerProperty(Hwc2TestCoverage::Complete,
3327            setTransform, advanceTransform));
3328}
3329
3330/* TESTCASE: Tests that the HWC2 can update the transform value of a layer. */
3331TEST_F(Hwc2Test, SET_LAYER_TRANSFORM_update)
3332{
3333    ASSERT_NO_FATAL_FAILURE(setLayerPropertyUpdate(Hwc2TestCoverage::Complete,
3334            setTransform, advanceTransform));
3335}
3336
3337/* TESTCASE: Tests that the HWC2 cannot set the transform for a bad layer. */
3338TEST_F(Hwc2Test, SET_LAYER_TRANSFORM_bad_layer)
3339{
3340    ASSERT_NO_FATAL_FAILURE(setLayerPropertyBadLayer(Hwc2TestCoverage::Default,
3341            setTransform));
3342}
3343
3344/* TESTCASE: Tests that the HWC2 can set the visible region of a layer. */
3345TEST_F(Hwc2Test, SET_LAYER_VISIBLE_REGION)
3346{
3347    ASSERT_NO_FATAL_FAILURE(setLayerProperties(Hwc2TestCoverage::Basic, 5,
3348            [] (Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
3349                    Hwc2TestLayers* testLayers) {
3350
3351                EXPECT_NO_FATAL_FAILURE(test->setLayerVisibleRegion(display,
3352                        layer, testLayers->getVisibleRegion(layer)));
3353            },
3354
3355            advanceVisibleRegions));
3356}
3357
3358/* TESTCASE: Tests that the HWC2 cannot set the visible region of a bad layer. */
3359TEST_F(Hwc2Test, SET_LAYER_VISIBLE_REGION_bad_layer)
3360{
3361    ASSERT_NO_FATAL_FAILURE(setLayerPropertyBadLayer(Hwc2TestCoverage::Default,
3362            setVisibleRegion));
3363}
3364
3365/* TESTCASE: Tests that the HWC2 can set the z order of a layer. */
3366TEST_F(Hwc2Test, SET_LAYER_Z_ORDER)
3367{
3368    ASSERT_NO_FATAL_FAILURE(setLayerProperties(Hwc2TestCoverage::Complete, 10,
3369            [] (Hwc2Test* test, hwc2_display_t display, hwc2_layer_t layer,
3370                    Hwc2TestLayers* testLayers) {
3371
3372                EXPECT_NO_FATAL_FAILURE(test->setLayerZOrder(display, layer,
3373                        testLayers->getZOrder(layer)));
3374            },
3375
3376            /* TestLayer z orders are set during the construction of TestLayers
3377             * and cannot be updated. There is no need (or ability) to cycle
3378             * through additional z order configurations. */
3379            [] (Hwc2TestLayers* /*testLayers*/) {
3380                return false;
3381            }
3382    ));
3383}
3384
3385/* TESTCASE: Tests that the HWC2 can update the z order of a layer. */
3386TEST_F(Hwc2Test, SET_LAYER_Z_ORDER_update)
3387{
3388    const std::vector<uint32_t> zOrders = { static_cast<uint32_t>(0),
3389            static_cast<uint32_t>(1), static_cast<uint32_t>(UINT32_MAX / 4),
3390            static_cast<uint32_t>(UINT32_MAX / 2),
3391            static_cast<uint32_t>(UINT32_MAX) };
3392
3393    for (auto display : mDisplays) {
3394        std::vector<hwc2_config_t> configs;
3395
3396        ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
3397
3398        for (auto config : configs) {
3399            hwc2_layer_t layer;
3400
3401            ASSERT_NO_FATAL_FAILURE(setActiveConfig(display, config));
3402
3403            ASSERT_NO_FATAL_FAILURE(createLayer(display, &layer));
3404
3405            for (uint32_t zOrder : zOrders) {
3406                EXPECT_NO_FATAL_FAILURE(setLayerZOrder(display, layer, zOrder));
3407            }
3408
3409            ASSERT_NO_FATAL_FAILURE(destroyLayer(display, layer));
3410        }
3411    }
3412}
3413
3414/* TESTCASE: Tests that the HWC2 cannot set the z order of a bad layer. */
3415TEST_F(Hwc2Test, SET_LAYER_Z_ORDER_bad_layer)
3416{
3417    ASSERT_NO_FATAL_FAILURE(setLayerPropertyBadLayer(Hwc2TestCoverage::Default,
3418            setZOrder));
3419}
3420
3421/* TESTCASE: Tests that the HWC2 can display a layer with basic property
3422 * coverage */
3423TEST_F(Hwc2Test, VALIDATE_DISPLAY_basic)
3424{
3425    ASSERT_NO_FATAL_FAILURE(displayLayers(Hwc2TestCoverage::Basic, 1,
3426            [] (Hwc2Test* test, hwc2_display_t display,
3427                    const std::vector<hwc2_layer_t>& layers,
3428                    Hwc2TestLayers* /*testLayers*/) {
3429
3430                uint32_t numTypes, numRequests;
3431                bool hasChanges = false;
3432
3433                EXPECT_NO_FATAL_FAILURE(test->validateDisplay(display, &numTypes,
3434                        &numRequests, &hasChanges));
3435                if (hasChanges)
3436                    EXPECT_LE(numTypes, static_cast<uint32_t>(layers.size()))
3437                            << "wrong number of requests";
3438            }
3439    ));
3440}
3441
3442/* TESTCASE: Tests that the HWC2 can display 5 layers with default coverage. */
3443TEST_F(Hwc2Test, VALIDATE_DISPLAY_default_5)
3444{
3445    ASSERT_NO_FATAL_FAILURE(displayLayers(Hwc2TestCoverage::Default, 5,
3446            [] (Hwc2Test* test, hwc2_display_t display,
3447                    const std::vector<hwc2_layer_t>& layers,
3448                    Hwc2TestLayers* /*testLayers*/) {
3449
3450                uint32_t numTypes, numRequests;
3451                bool hasChanges = false;
3452
3453                EXPECT_NO_FATAL_FAILURE(test->validateDisplay(display, &numTypes,
3454                        &numRequests, &hasChanges));
3455                if (hasChanges)
3456                    EXPECT_LE(numTypes, static_cast<uint32_t>(layers.size()))
3457                            << "wrong number of requests";
3458            }
3459    ));
3460}
3461
3462/* TESTCASE: Tests that the HWC2 cannot validate a bad display */
3463TEST_F(Hwc2Test, VALIDATE_DISPLAY_bad_display)
3464{
3465    hwc2_display_t display;
3466    uint32_t numTypes, numRequests;
3467    hwc2_error_t err = HWC2_ERROR_NONE;
3468
3469    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
3470
3471    ASSERT_NO_FATAL_FAILURE(validateDisplay(display, &numTypes, &numRequests,
3472            &err));
3473    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
3474}
3475
3476/* TESTCASE: Tests that the HWC2 can get display requests after validating a
3477 * basic layer. */
3478TEST_F(Hwc2Test, GET_DISPLAY_REQUESTS_basic)
3479{
3480    ASSERT_NO_FATAL_FAILURE(displayLayers(Hwc2TestCoverage::Basic, 1,
3481            [] (Hwc2Test* test, hwc2_display_t display,
3482                    const std::vector<hwc2_layer_t>& layers,
3483                    Hwc2TestLayers* /*testLayers*/) {
3484
3485                uint32_t numTypes, numRequests;
3486                bool hasChanges = false;
3487
3488                ASSERT_NO_FATAL_FAILURE(test->validateDisplay(display, &numTypes,
3489                        &numRequests, &hasChanges));
3490                if (hasChanges)
3491                    EXPECT_LE(numTypes, layers.size())
3492                            << "wrong number of requests";
3493
3494                EXPECT_NO_FATAL_FAILURE(test->handleRequests(display, layers,
3495                        numRequests));
3496            }
3497    ));
3498}
3499
3500/* TESTCASE: Tests that the HWC2 cannot get display requests from a bad display */
3501TEST_F(Hwc2Test, GET_DISPLAY_REQUESTS_bad_display)
3502{
3503    hwc2_display_t display;
3504    hwc2_display_request_t displayRequests;
3505    std::vector<hwc2_layer_t> layers;
3506    std::vector<hwc2_layer_request_t> layerRequests;
3507    hwc2_error_t err = HWC2_ERROR_NONE;
3508
3509    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
3510
3511    EXPECT_NO_FATAL_FAILURE(getDisplayRequests(display, &displayRequests,
3512            &layers, &layerRequests, &err));
3513    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
3514}
3515
3516/* TESTCASE: Tests that the HWC2 cannot get display requests from an non
3517 * validated display. */
3518TEST_F(Hwc2Test, GET_DISPLAY_REQUESTS_not_validated)
3519{
3520    ASSERT_NO_FATAL_FAILURE(displayNonValidatedLayers(5,
3521            [] (Hwc2Test* test, hwc2_display_t display,
3522                    std::vector<hwc2_layer_t>* layers) {
3523
3524                hwc2_display_request_t displayRequests;
3525                std::vector<hwc2_layer_request_t> layerRequests;
3526                hwc2_error_t err = HWC2_ERROR_NONE;
3527
3528                ASSERT_NO_FATAL_FAILURE(test->getDisplayRequests(display,
3529                        &displayRequests, layers, &layerRequests, &err));
3530                EXPECT_EQ(err, HWC2_ERROR_NOT_VALIDATED)
3531                        << "returned wrong error code";
3532            }
3533    ));
3534}
3535
3536/* TESTCASE: Tests that the HWC2 can get changed composition types after
3537 * validating a basic layer. */
3538TEST_F(Hwc2Test, GET_CHANGED_COMPOSITION_TYPES_basic)
3539{
3540    ASSERT_NO_FATAL_FAILURE(displayLayers(Hwc2TestCoverage::Basic, 1,
3541            [] (Hwc2Test* test, hwc2_display_t display,
3542                    const std::vector<hwc2_layer_t>& layers,
3543                    Hwc2TestLayers* testLayers) {
3544
3545                uint32_t numTypes, numRequests;
3546                bool hasChanges = false;
3547
3548                ASSERT_NO_FATAL_FAILURE(test->validateDisplay(display, &numTypes,
3549                        &numRequests, &hasChanges));
3550                if (hasChanges)
3551                    EXPECT_LE(numTypes, layers.size())
3552                            << "wrong number of requests";
3553
3554                EXPECT_NO_FATAL_FAILURE(test->handleCompositionChanges(display,
3555                        *testLayers, layers, numTypes));
3556            }
3557    ));
3558}
3559
3560/* TESTCASE: Tests that the HWC2 cannot get changed composition types from a bad
3561 * display */
3562TEST_F(Hwc2Test, GET_CHANGED_COMPOSITION_TYPES_bad_display)
3563{
3564    hwc2_display_t display;
3565    std::vector<hwc2_layer_t> layers;
3566    std::vector<hwc2_composition_t> types;
3567    hwc2_error_t err = HWC2_ERROR_NONE;
3568
3569    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
3570
3571    EXPECT_NO_FATAL_FAILURE(getChangedCompositionTypes(display, &layers,
3572            &types, &err));
3573    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
3574}
3575
3576/* TESTCASE: Tests that the HWC2 cannot get changed composition types from an non
3577 * validated display. */
3578TEST_F(Hwc2Test, GET_CHANGED_COMPOSITION_TYPES_not_validated)
3579{
3580    ASSERT_NO_FATAL_FAILURE(displayNonValidatedLayers(5,
3581            [] (Hwc2Test* test, hwc2_display_t display,
3582                    std::vector<hwc2_layer_t>* layers) {
3583
3584                std::vector<hwc2_composition_t> types;
3585                hwc2_error_t err = HWC2_ERROR_NONE;
3586
3587                ASSERT_NO_FATAL_FAILURE(test->getChangedCompositionTypes(
3588                        display, layers, &types, &err));
3589                EXPECT_EQ(err, HWC2_ERROR_NOT_VALIDATED)
3590                        << "returned wrong error code";
3591            }
3592    ));
3593}
3594
3595/* TESTCASE: Tests that the HWC2 can accept display changes after validating a
3596 * basic layer. */
3597TEST_F(Hwc2Test, ACCEPT_DISPLAY_CHANGES_basic)
3598{
3599    ASSERT_NO_FATAL_FAILURE(displayLayers(Hwc2TestCoverage::Basic, 1,
3600            [] (Hwc2Test* test, hwc2_display_t display,
3601                    const std::vector<hwc2_layer_t>& layers,
3602                    Hwc2TestLayers* testLayers) {
3603
3604                uint32_t numTypes, numRequests;
3605                bool hasChanges = false;
3606
3607                ASSERT_NO_FATAL_FAILURE(test->validateDisplay(display, &numTypes,
3608                        &numRequests, &hasChanges));
3609                if (hasChanges)
3610                    EXPECT_LE(numTypes, layers.size())
3611                            << "wrong number of requests";
3612
3613                ASSERT_NO_FATAL_FAILURE(test->handleCompositionChanges(display,
3614                        *testLayers, layers, numTypes));
3615
3616                EXPECT_NO_FATAL_FAILURE(test->acceptDisplayChanges(display));
3617            }
3618    ));
3619}
3620
3621/* TESTCASE: Tests that the HWC2 cannot accept display changes from a bad
3622 * display */
3623TEST_F(Hwc2Test, ACCEPT_DISPLAY_CHANGES_bad_display)
3624{
3625    hwc2_display_t display;
3626    hwc2_error_t err = HWC2_ERROR_NONE;
3627
3628    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
3629
3630    EXPECT_NO_FATAL_FAILURE(acceptDisplayChanges(display, &err));
3631    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
3632}
3633
3634/* TESTCASE: Tests that the HWC2 cannot accept display changes from an non
3635 * validated display. */
3636TEST_F(Hwc2Test, ACCEPT_DISPLAY_CHANGES_not_validated)
3637{
3638    ASSERT_NO_FATAL_FAILURE(displayNonValidatedLayers(5,
3639            [] (Hwc2Test* test, hwc2_display_t display,
3640                    std::vector<hwc2_layer_t>* /*layers*/) {
3641
3642                hwc2_error_t err = HWC2_ERROR_NONE;
3643
3644                ASSERT_NO_FATAL_FAILURE(test->acceptDisplayChanges(display, &err));
3645                EXPECT_EQ(err, HWC2_ERROR_NOT_VALIDATED)
3646                        << "returned wrong error code";
3647            }
3648    ));
3649}
3650
3651/* TESTCASE: Tests that the HWC2 supports client target with required values */
3652TEST_F(Hwc2Test, GET_CLIENT_TARGET_SUPPORT)
3653{
3654    ASSERT_NO_FATAL_FAILURE(setClientTargetSupport(Hwc2TestCoverage::Default,
3655            [] (Hwc2Test* test, hwc2_display_t display,
3656                    const Hwc2TestClientTargetSupport& testClientTargetSupport) {
3657
3658                const Area bufferArea = testClientTargetSupport.getBufferArea();
3659                const android_pixel_format_t format = HAL_PIXEL_FORMAT_RGBA_8888;
3660
3661                ASSERT_NO_FATAL_FAILURE(test->getClientTargetSupport(display,
3662                        bufferArea.width, bufferArea.height, format,
3663                        testClientTargetSupport.getDataspace()));
3664            },
3665
3666            advanceClientTargetSupport));
3667}
3668
3669/* TESTCASE: Tests that the HWC2 cannot get client target support for a bad
3670 * display. */
3671TEST_F(Hwc2Test, GET_CLIENT_TARGET_SUPPORT_bad_display)
3672{
3673    ASSERT_NO_FATAL_FAILURE(setClientTargetSupport(Hwc2TestCoverage::Default,
3674            [] (Hwc2Test* test, hwc2_display_t /*display*/,
3675                    const Hwc2TestClientTargetSupport& testClientTargetSupport) {
3676
3677                const Area bufferArea = testClientTargetSupport.getBufferArea();
3678                const android_pixel_format_t format = HAL_PIXEL_FORMAT_RGBA_8888;
3679                hwc2_display_t badDisplay;
3680                hwc2_error_t err = HWC2_ERROR_NONE;
3681
3682                ASSERT_NO_FATAL_FAILURE(test->getBadDisplay(&badDisplay));
3683
3684                ASSERT_NO_FATAL_FAILURE(test->getClientTargetSupport(badDisplay,
3685                        bufferArea.width, bufferArea.height, format,
3686                        testClientTargetSupport.getDataspace(), &err));
3687                EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
3688            },
3689
3690            advanceClientTargetSupport));
3691}
3692
3693/* TESTCASE: Tests that the HWC2 either supports or returns error unsupported
3694 * for a variety of client target values. */
3695TEST_F(Hwc2Test, GET_CLIENT_TARGET_SUPPORT_unsupported)
3696{
3697    ASSERT_NO_FATAL_FAILURE(setClientTargetSupport(Hwc2TestCoverage::Complete,
3698            [] (Hwc2Test* test, hwc2_display_t display,
3699                    const Hwc2TestClientTargetSupport& testClientTargetSupport) {
3700
3701                const Area bufferArea = testClientTargetSupport.getBufferArea();
3702                const android_pixel_format_t format = HAL_PIXEL_FORMAT_RGBA_8888;
3703                hwc2_error_t err = HWC2_ERROR_NONE;
3704
3705                ASSERT_NO_FATAL_FAILURE(test->getClientTargetSupport(display,
3706                        bufferArea.width, bufferArea.height, format,
3707                        testClientTargetSupport.getDataspace(), &err));
3708                EXPECT_TRUE(err == HWC2_ERROR_NONE
3709                        || err == HWC2_ERROR_UNSUPPORTED)
3710                        << "returned wrong error code";
3711            },
3712
3713            advanceClientTargetSupport));
3714}
3715
3716/* TESTCASE: Tests that the HWC2 can set a client target buffer for a basic
3717 * layer. */
3718TEST_F(Hwc2Test, SET_CLIENT_TARGET_basic)
3719{
3720    const Dataspace dataspace = Dataspace::UNKNOWN;
3721    const hwc_region_t damage = { };
3722    const size_t layerCnt = 1;
3723
3724    for (auto display : mDisplays) {
3725        std::vector<hwc2_config_t> configs;
3726
3727        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_ON));
3728
3729        ASSERT_NO_FATAL_FAILURE(getDisplayConfigs(display, &configs));
3730
3731        for (auto config : configs) {
3732            Area displayArea;
3733            std::vector<hwc2_layer_t> layers;
3734
3735            ASSERT_NO_FATAL_FAILURE(setActiveConfig(display, config));
3736            ASSERT_NO_FATAL_FAILURE(getActiveDisplayArea(display, &displayArea));
3737
3738            ASSERT_NO_FATAL_FAILURE(createLayers(display, &layers, layerCnt));
3739            Hwc2TestLayers testLayers(layers, Hwc2TestCoverage::Basic,
3740                    displayArea);
3741
3742            if (!testLayers.optimizeLayouts())
3743                continue;
3744
3745            Hwc2TestClientTarget testClientTarget;
3746
3747            do {
3748                std::set<hwc2_layer_t> clientLayers;
3749                std::set<hwc2_layer_t> clearLayers;
3750                uint32_t numTypes, numRequests;
3751                bool hasChanges, skip;
3752                bool flipClientTarget;
3753                buffer_handle_t handle;
3754                int32_t acquireFence;
3755
3756                ASSERT_NO_FATAL_FAILURE(setLayerProperties(display, layers,
3757                        &testLayers, &skip));
3758                if (skip)
3759                    continue;
3760
3761                ASSERT_NO_FATAL_FAILURE(validateDisplay(display, &numTypes,
3762                        &numRequests, &hasChanges));
3763                if (hasChanges)
3764                    EXPECT_LE(numTypes, layers.size())
3765                            << "wrong number of requests";
3766
3767                ASSERT_NO_FATAL_FAILURE(handleCompositionChanges(display,
3768                        testLayers, layers, numTypes, &clientLayers));
3769                ASSERT_NO_FATAL_FAILURE(handleRequests(display, layers,
3770                        numRequests, &clearLayers, &flipClientTarget));
3771                ASSERT_EQ(testClientTarget.getBuffer(testLayers, clientLayers,
3772                        clearLayers, flipClientTarget, displayArea, &handle,
3773                        &acquireFence), 0);
3774                EXPECT_NO_FATAL_FAILURE(setClientTarget(display, handle,
3775                        acquireFence, dataspace, damage));
3776
3777                if (acquireFence >= 0)
3778                    close(acquireFence);
3779
3780            } while (testLayers.advance());
3781
3782            ASSERT_NO_FATAL_FAILURE(destroyLayers(display, std::move(layers)));
3783        }
3784
3785        ASSERT_NO_FATAL_FAILURE(setPowerMode(display, HWC2_POWER_MODE_OFF));
3786    }
3787}
3788
3789/* TESTCASE: Tests that the HWC2 cannot set a client target for a bad display. */
3790TEST_F(Hwc2Test, SET_CLIENT_TARGET_bad_display)
3791{
3792    hwc2_display_t display;
3793    std::vector<hwc2_layer_t> layers;
3794    const Area displayArea = {0, 0};
3795    Hwc2TestLayers testLayers(layers, Hwc2TestCoverage::Default, displayArea);
3796    std::set<hwc2_layer_t> clientLayers;
3797    std::set<hwc2_layer_t> flipClientTargetLayers;
3798    bool flipClientTarget = true;
3799    const Dataspace dataspace = Dataspace::UNKNOWN;
3800    const hwc_region_t damage = { };
3801    buffer_handle_t handle;
3802    int32_t acquireFence;
3803    hwc2_error_t err = HWC2_ERROR_NONE;
3804
3805    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
3806
3807    Hwc2TestClientTarget testClientTarget;
3808
3809    ASSERT_EQ(testClientTarget.getBuffer(testLayers, clientLayers,
3810            flipClientTargetLayers, flipClientTarget, displayArea, &handle,
3811            &acquireFence), 0);
3812
3813    EXPECT_NO_FATAL_FAILURE(setClientTarget(display, handle, acquireFence,
3814            dataspace, damage, &err));
3815
3816    if (acquireFence >= 0)
3817        close(acquireFence);
3818
3819    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
3820}
3821
3822/* TESTCASE: Tests that the HWC2 can present 1 default layer. */
3823TEST_F(Hwc2Test, PRESENT_DISPLAY_default_1)
3824{
3825    const size_t layerCnt = 1;
3826    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
3827    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions;
3828    bool optimize = false;
3829
3830    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
3831            optimize));
3832}
3833
3834/* TESTCASE: Tests that the HWC2 can present 2 default layers. */
3835TEST_F(Hwc2Test, PRESENT_DISPLAY_default_2)
3836{
3837    const size_t layerCnt = 2;
3838    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
3839    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions;
3840    bool optimize = false;
3841
3842    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
3843            optimize));
3844}
3845
3846/* TESTCASE: Tests that the HWC2 can present 3 default layers. */
3847TEST_F(Hwc2Test, PRESENT_DISPLAY_default_3)
3848{
3849    const size_t layerCnt = 3;
3850    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
3851    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions;
3852    bool optimize = false;
3853
3854    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
3855            optimize));
3856}
3857
3858/* TESTCASE: Tests that the HWC2 can present 4 default layers. */
3859TEST_F(Hwc2Test, PRESENT_DISPLAY_default_4)
3860{
3861    const size_t layerCnt = 4;
3862    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
3863    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions;
3864    bool optimize = false;
3865
3866    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
3867            optimize));
3868}
3869
3870/* TESTCASE: Tests that the HWC2 can present 5 default layers. */
3871TEST_F(Hwc2Test, PRESENT_DISPLAY_default_5)
3872{
3873    const size_t layerCnt = 5;
3874    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
3875    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions;
3876    bool optimize = false;
3877
3878    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
3879            optimize));
3880}
3881
3882/* TESTCASE: Tests that the HWC2 can present 6 default layers. */
3883TEST_F(Hwc2Test, PRESENT_DISPLAY_default_6)
3884{
3885    const size_t layerCnt = 6;
3886    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
3887    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions;
3888    bool optimize = false;
3889
3890    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
3891            optimize));
3892}
3893
3894/* TESTCASE: Tests that the HWC2 can present 1 layer with complete coverage of
3895 * blend mode. */
3896TEST_F(Hwc2Test, PRESENT_DISPLAY_blend_mode_1)
3897{
3898    const size_t layerCnt = 1;
3899    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
3900    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
3901            {{Hwc2TestPropertyName::BlendMode, Hwc2TestCoverage::Complete},
3902            {Hwc2TestPropertyName::Transform, Hwc2TestCoverage::Basic},
3903            {Hwc2TestPropertyName::PlaneAlpha, Hwc2TestCoverage::Basic}};
3904    bool optimize = false;
3905
3906    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
3907            optimize));
3908}
3909
3910/* TESTCASE: Tests that the HWC2 can present 2 layers with complete coverage of
3911 * blend mode. */
3912TEST_F(Hwc2Test, PRESENT_DISPLAY_blend_mode_2)
3913{
3914    const size_t layerCnt = 2;
3915    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
3916    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
3917            {{Hwc2TestPropertyName::BlendMode, Hwc2TestCoverage::Complete},
3918            {Hwc2TestPropertyName::PlaneAlpha, Hwc2TestCoverage::Basic}};
3919    bool optimize = false;
3920
3921    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
3922            optimize));
3923}
3924
3925/* TESTCASE: Tests that the HWC2 can present 1 layer with complete coverage of
3926 * buffer. */
3927TEST_F(Hwc2Test, PRESENT_DISPLAY_buffer_1)
3928{
3929    const size_t layerCnt = 1;
3930    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
3931    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
3932            {{Hwc2TestPropertyName::BufferArea, Hwc2TestCoverage::Complete}};
3933    bool optimize = true;
3934
3935    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
3936            optimize));
3937}
3938
3939/* TESTCASE: Tests that the HWC2 can present 1 layer with complete coverage of
3940 * color. */
3941TEST_F(Hwc2Test, PRESENT_DISPLAY_color_1)
3942{
3943    const size_t layerCnt = 1;
3944    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
3945    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
3946            {{Hwc2TestPropertyName::Composition, Hwc2TestCoverage::Complete},
3947            {Hwc2TestPropertyName::Color, Hwc2TestCoverage::Complete}};
3948    bool optimize = true;
3949
3950    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
3951            optimize));
3952}
3953
3954/* TESTCASE: Tests that the HWC2 can present 2 layers with complete coverage of
3955 * color. */
3956TEST_F(Hwc2Test, PRESENT_DISPLAY_color_2)
3957{
3958    const size_t layerCnt = 2;
3959    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
3960    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
3961            {{Hwc2TestPropertyName::Composition, Hwc2TestCoverage::Complete},
3962            {Hwc2TestPropertyName::BlendMode, Hwc2TestCoverage::Basic},
3963            {Hwc2TestPropertyName::PlaneAlpha, Hwc2TestCoverage::Basic},
3964            {Hwc2TestPropertyName::Color, Hwc2TestCoverage::Basic}};
3965    bool optimize = true;
3966
3967    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
3968            optimize));
3969}
3970
3971/* TESTCASE: Tests that the HWC2 can present 1 layer with complete coverage of
3972 * composition. */
3973TEST_F(Hwc2Test, PRESENT_DISPLAY_composition_1)
3974{
3975    const size_t layerCnt = 1;
3976    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
3977    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
3978            {{Hwc2TestPropertyName::Composition, Hwc2TestCoverage::Complete}};
3979    bool optimize = true;
3980
3981    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
3982            optimize));
3983}
3984
3985/* TESTCASE: Tests that the HWC2 can present 1 layer with complete coverage of
3986 * cursor. */
3987TEST_F(Hwc2Test, PRESENT_DISPLAY_cursor_1)
3988{
3989    const size_t layerCnt = 1;
3990    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
3991    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
3992            {{Hwc2TestPropertyName::Composition, Hwc2TestCoverage::Complete},
3993            {Hwc2TestPropertyName::CursorPosition, Hwc2TestCoverage::Complete}};
3994    bool optimize = true;
3995
3996    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
3997            optimize));
3998}
3999
4000/* TESTCASE: Tests that the HWC2 can present 2 layers with complete coverage of
4001 * cursor. */
4002TEST_F(Hwc2Test, PRESENT_DISPLAY_cursor_2)
4003{
4004    const size_t layerCnt = 2;
4005    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4006    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
4007            {{Hwc2TestPropertyName::Composition, Hwc2TestCoverage::Complete},
4008            {Hwc2TestPropertyName::CursorPosition, Hwc2TestCoverage::Complete},
4009            {Hwc2TestPropertyName::DisplayFrame, Hwc2TestCoverage::Basic}};
4010    bool optimize = true;
4011
4012    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
4013            optimize));
4014}
4015
4016/* TESTCASE: Tests that the HWC2 can present 1 layer with complete coverage of
4017 * dataspace. */
4018TEST_F(Hwc2Test, PRESENT_DISPLAY_dataspace_1)
4019{
4020    const size_t layerCnt = 1;
4021    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4022    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
4023            {{Hwc2TestPropertyName::Dataspace, Hwc2TestCoverage::Complete}};
4024    bool optimize = true;
4025
4026    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
4027            optimize));
4028}
4029
4030/* TESTCASE: Tests that the HWC2 can present 1 layer with complete coverage of
4031 * display frame. */
4032TEST_F(Hwc2Test, PRESENT_DISPLAY_display_frame_1)
4033{
4034    const size_t layerCnt = 1;
4035    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4036    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
4037            {{Hwc2TestPropertyName::DisplayFrame, Hwc2TestCoverage::Complete}};
4038    bool optimize = true;
4039
4040    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
4041            optimize));
4042}
4043
4044/* TESTCASE: Tests that the HWC2 can present 2 layers with complete coverage of
4045 * display frame. */
4046TEST_F(Hwc2Test, PRESENT_DISPLAY_display_frame_2)
4047{
4048    const size_t layerCnt = 2;
4049    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4050    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
4051            {{Hwc2TestPropertyName::DisplayFrame, Hwc2TestCoverage::Complete}};
4052    bool optimize = true;
4053
4054    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
4055            optimize));
4056}
4057
4058/* TESTCASE: Tests that the HWC2 can present 3 layers with complete coverage of
4059 * display frame. */
4060TEST_F(Hwc2Test, PRESENT_DISPLAY_display_frame_3)
4061{
4062    const size_t layerCnt = 3;
4063    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4064    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
4065            {{Hwc2TestPropertyName::DisplayFrame, Hwc2TestCoverage::Complete}};
4066    bool optimize = true;
4067
4068    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
4069            optimize));
4070}
4071
4072/* TESTCASE: Tests that the HWC2 can present 4 layers with complete coverage of
4073 * display frame. */
4074TEST_F(Hwc2Test, PRESENT_DISPLAY_display_frame_4)
4075{
4076    const size_t layerCnt = 4;
4077    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4078    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
4079            {{Hwc2TestPropertyName::DisplayFrame, Hwc2TestCoverage::Complete}};
4080    bool optimize = true;
4081
4082    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
4083            optimize));
4084}
4085
4086/* TESTCASE: Tests that the HWC2 can present 1 layer with complete coverage of
4087 * plane alpha. */
4088TEST_F(Hwc2Test, PRESENT_DISPLAY_plane_alpha_1)
4089{
4090    const size_t layerCnt = 1;
4091    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4092    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
4093            {{Hwc2TestPropertyName::BlendMode, Hwc2TestCoverage::Basic},
4094            {Hwc2TestPropertyName::PlaneAlpha, Hwc2TestCoverage::Complete}};
4095    bool optimize = false;
4096
4097    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
4098            optimize));
4099}
4100
4101/* TESTCASE: Tests that the HWC2 can present 2 layers with complete coverage of
4102 * plane alpha. */
4103TEST_F(Hwc2Test, PRESENT_DISPLAY_plane_alpha_2)
4104{
4105    const size_t layerCnt = 2;
4106    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4107    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
4108            {{Hwc2TestPropertyName::BlendMode, Hwc2TestCoverage::Basic},
4109            {Hwc2TestPropertyName::PlaneAlpha, Hwc2TestCoverage::Complete}};
4110    bool optimize = false;
4111
4112    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
4113            optimize));
4114}
4115
4116/* TESTCASE: Tests that the HWC2 can present 1 layer with complete coverage of
4117 * source crop. */
4118TEST_F(Hwc2Test, PRESENT_DISPLAY_source_crop_1)
4119{
4120    const size_t layerCnt = 1;
4121    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4122    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
4123            {{Hwc2TestPropertyName::DisplayFrame, Hwc2TestCoverage::Complete},
4124            {Hwc2TestPropertyName::SourceCrop, Hwc2TestCoverage::Complete}};
4125    bool optimize = true;
4126
4127    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
4128            optimize));
4129}
4130
4131/* TESTCASE: Tests that the HWC2 can present 2 layers with complete coverage of
4132 * source crop. */
4133TEST_F(Hwc2Test, PRESENT_DISPLAY_source_crop_2)
4134{
4135    const size_t layerCnt = 2;
4136    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4137    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
4138            {{Hwc2TestPropertyName::DisplayFrame, Hwc2TestCoverage::Complete},
4139            {Hwc2TestPropertyName::SourceCrop, Hwc2TestCoverage::Complete}};
4140    bool optimize = true;
4141
4142    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
4143            optimize));
4144}
4145
4146
4147/* TESTCASE: Tests that the HWC2 can present 1 layer with complete coverage of
4148 * surface damage. */
4149TEST_F(Hwc2Test, PRESENT_DISPLAY_surface_damage_1)
4150{
4151    const size_t layerCnt = 1;
4152    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4153    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
4154            {{Hwc2TestPropertyName::SurfaceDamage, Hwc2TestCoverage::Complete}};
4155    bool optimize = true;
4156
4157    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
4158            optimize));
4159}
4160
4161/* TESTCASE: Tests that the HWC2 can present 1 layer with complete coverage of
4162 * transform. */
4163TEST_F(Hwc2Test, PRESENT_DISPLAY_transform_1)
4164{
4165    const size_t layerCnt = 1;
4166    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4167    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
4168            {{Hwc2TestPropertyName::Transform, Hwc2TestCoverage::Complete}};
4169    bool optimize = true;
4170
4171    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
4172            optimize));
4173}
4174
4175/* TESTCASE: Tests that the HWC2 can present 2 layers with complete coverage of
4176 * transform. */
4177TEST_F(Hwc2Test, PRESENT_DISPLAY_transform_2)
4178{
4179    const size_t layerCnt = 2;
4180    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4181    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions =
4182            {{Hwc2TestPropertyName::Transform, Hwc2TestCoverage::Complete},
4183            {Hwc2TestPropertyName::DisplayFrame, Hwc2TestCoverage::Basic}};
4184    bool optimize = true;
4185
4186    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
4187            optimize));
4188}
4189
4190/* TESTCASE: Tests that the HWC2 can present 1 layer with complete coverage of
4191 * basic. */
4192TEST_F(Hwc2Test, PRESENT_DISPLAY_basic_1)
4193{
4194    const size_t layerCnt = 1;
4195    Hwc2TestCoverage coverage = Hwc2TestCoverage::Basic;
4196    std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage> exceptions;
4197    bool optimize = true;
4198
4199    ASSERT_NO_FATAL_FAILURE(presentDisplays(layerCnt, coverage, exceptions,
4200            optimize));
4201}
4202
4203/* TESTCASE: Tests that the HWC2 cannot present a bad display.  */
4204TEST_F(Hwc2Test, PRESENT_DISPLAY_bad_display)
4205{
4206    hwc2_display_t display;
4207    int32_t presentFence;
4208    hwc2_error_t err = HWC2_ERROR_NONE;
4209
4210    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
4211
4212    ASSERT_NO_FATAL_FAILURE(presentDisplay(display, &presentFence, &err));
4213    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
4214}
4215
4216/* TESTCASE: Tests that the HWC2 cannot present an unvalidated display. */
4217TEST_F(Hwc2Test, PRESENT_DISPLAY_not_validated)
4218{
4219    ASSERT_NO_FATAL_FAILURE(displayLayers(Hwc2TestCoverage::Default, 1,
4220            [] (Hwc2Test* test, hwc2_display_t display,
4221                    const std::vector<hwc2_layer_t>& /*layers*/,
4222                    Hwc2TestLayers* /*testLayers*/) {
4223
4224                int32_t presentFence;
4225                hwc2_error_t err = HWC2_ERROR_NONE;
4226
4227                ASSERT_NO_FATAL_FAILURE(test->setPowerMode(display,
4228                        HWC2_POWER_MODE_ON));
4229                ASSERT_NO_FATAL_FAILURE(test->enableVsync(display));
4230
4231                ASSERT_NO_FATAL_FAILURE(test->waitForVsync());
4232
4233                ASSERT_NO_FATAL_FAILURE(test->presentDisplay(display,
4234                        &presentFence, &err));
4235                EXPECT_EQ(err, HWC2_ERROR_NOT_VALIDATED)
4236                        << "returned wrong error code";
4237
4238                ASSERT_NO_FATAL_FAILURE(test->disableVsync(display));
4239                ASSERT_NO_FATAL_FAILURE(test->setPowerMode(display,
4240                        HWC2_POWER_MODE_OFF));
4241            }
4242    ));
4243}
4244
4245/* TESTCASE: Tests that the HWC2 cannot get release fences from a bad display. */
4246TEST_F(Hwc2Test, GET_RELEASE_FENCES_bad_display)
4247{
4248    hwc2_display_t display;
4249    std::vector<hwc2_layer_t> layers;
4250    std::vector<int32_t> fences;
4251    hwc2_error_t err = HWC2_ERROR_NONE;
4252
4253    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
4254
4255    ASSERT_NO_FATAL_FAILURE(getReleaseFences(display, &layers, &fences, &err));
4256    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
4257}
4258
4259static const std::array<ColorMode, 9> androidColorModes = {{
4260    ColorMode::NATIVE,
4261    ColorMode::STANDARD_BT601_625,
4262    ColorMode::STANDARD_BT601_625_UNADJUSTED,
4263    ColorMode::STANDARD_BT601_525,
4264    ColorMode::STANDARD_BT601_525_UNADJUSTED,
4265    ColorMode::STANDARD_BT709,
4266    ColorMode::DCI_P3,
4267    ColorMode::SRGB,
4268    ColorMode::ADOBE_RGB,
4269}};
4270
4271/* TESTCASE: Tests that the HWC2 can get the color modes for a display. The
4272 * display must support ColorMode::NATIVE */
4273TEST_F(Hwc2Test, GET_COLOR_MODES)
4274{
4275    ASSERT_NO_FATAL_FAILURE(setActiveDisplayConfig(
4276            [] (Hwc2Test* test, hwc2_display_t display) {
4277
4278                std::vector<ColorMode> colorModes;
4279
4280                ASSERT_NO_FATAL_FAILURE(test->getColorModes(display,
4281                        &colorModes));
4282
4283                EXPECT_NE(std::count(colorModes.begin(), colorModes.end(),
4284                        ColorMode::NATIVE), 0) << "all displays"
4285                        " must support ColorMode::NATIVE";
4286            }
4287    ));
4288}
4289
4290/* TESTCASE: Tests that the HWC2 cannot get color modes from a bad display. */
4291TEST_F(Hwc2Test, GET_COLOR_MODES_bad_display)
4292{
4293    hwc2_display_t display;
4294    std::vector<ColorMode> colorModes;
4295    hwc2_error_t err = HWC2_ERROR_NONE;
4296
4297    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
4298
4299    ASSERT_NO_FATAL_FAILURE(getColorModes(display, &colorModes, &err));
4300    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
4301}
4302
4303/* TESTCASE: Tests that the HWC2 can set the required color mode on a display. */
4304TEST_F(Hwc2Test, SET_COLOR_MODES)
4305{
4306    ASSERT_NO_FATAL_FAILURE(setActiveDisplayConfig(
4307            [] (Hwc2Test* test, hwc2_display_t display) {
4308
4309                const ColorMode colorMode = ColorMode::NATIVE;
4310
4311                EXPECT_NO_FATAL_FAILURE(test->setColorMode(display, colorMode));
4312            }
4313    ));
4314}
4315
4316/* TESTCASE: Tests that the HWC2 cannot set a color mode on a bad display. */
4317TEST_F(Hwc2Test, SET_COLOR_MODES_bad_display)
4318{
4319    hwc2_display_t display;
4320    const ColorMode colorMode = ColorMode::NATIVE;
4321    hwc2_error_t err = HWC2_ERROR_NONE;
4322
4323    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
4324
4325    ASSERT_NO_FATAL_FAILURE(setColorMode(display, colorMode, &err));
4326    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
4327}
4328
4329/* TESTCASE: Tests that the HWC2 cannot set an invalid color mode. */
4330TEST_F(Hwc2Test, SET_COLOR_MODES_bad_parameter)
4331{
4332    ASSERT_NO_FATAL_FAILURE(setActiveDisplayConfig(
4333            [] (Hwc2Test* test, hwc2_display_t display) {
4334
4335                const ColorMode colorMode = static_cast<ColorMode>(-1);
4336                hwc2_error_t err = HWC2_ERROR_NONE;
4337
4338                ASSERT_NO_FATAL_FAILURE(test->setColorMode(display, colorMode,
4339                        &err));
4340                EXPECT_EQ(err, HWC2_ERROR_BAD_PARAMETER)
4341                        << "returned wrong error code";
4342            }
4343    ));
4344}
4345
4346/* TESTCASE: Tests that the HWC2 either supports or returns error unsupported
4347 * for all valid color modes. */
4348TEST_F(Hwc2Test, SET_COLOR_MODES_unsupported)
4349{
4350    ASSERT_NO_FATAL_FAILURE(setActiveDisplayConfig(
4351            [] (Hwc2Test* test, hwc2_display_t display) {
4352
4353                for (auto colorMode : androidColorModes) {
4354                    hwc2_error_t err = HWC2_ERROR_NONE;
4355
4356                    ASSERT_NO_FATAL_FAILURE(test->setColorMode(display,
4357                            colorMode, &err));
4358
4359                    EXPECT_TRUE(err == HWC2_ERROR_NONE
4360                            || err == HWC2_ERROR_UNSUPPORTED)
4361                            << "returned wrong error code";
4362                }
4363            }
4364    ));
4365}
4366
4367/* TESTCASE: Tests that the HWC2 gets the HDR capabilities for a display and
4368 * test if they are valid. */
4369TEST_F(Hwc2Test, GET_HDR_CAPABILITIES)
4370{
4371    ASSERT_NO_FATAL_FAILURE(setActiveDisplayConfig(
4372            [] (Hwc2Test* test, hwc2_display_t display) {
4373
4374                std::vector<android_hdr_t> hdrCapabilities;
4375                float maxLuminance, maxAverageLuminance, minLuminance;
4376
4377                EXPECT_NO_FATAL_FAILURE(test->getHdrCapabilities(display,
4378                        &hdrCapabilities, &maxLuminance, &maxAverageLuminance,
4379                        &minLuminance));
4380
4381                if (hdrCapabilities.empty())
4382                    return;
4383
4384                EXPECT_GE(maxLuminance, maxAverageLuminance);
4385                EXPECT_GE(maxAverageLuminance, minLuminance);
4386            }
4387    ));
4388}
4389
4390/* TESTCASE: Tests that the HWC2 cannot get hdr capabilities from a bad display */
4391TEST_F(Hwc2Test, GET_HDR_CAPABILITIES_bad_display)
4392{
4393    hwc2_display_t display;
4394    std::vector<android_hdr_t> hdrCapabilities;
4395    float maxLuminance, maxAverageLuminance, minLuminance;
4396    hwc2_error_t err = HWC2_ERROR_NONE;
4397
4398    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
4399
4400    ASSERT_NO_FATAL_FAILURE(getHdrCapabilities(display, &hdrCapabilities,
4401            &maxLuminance, &maxAverageLuminance, &minLuminance, &err));
4402    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
4403}
4404
4405static const std::array<float, 16> identityMatrix = {{
4406    1.0,  0.0,  0.0,  0.0,
4407    0.0,  1.0,  0.0,  0.0,
4408    0.0,  0.0,  1.0,  0.0,
4409    0.0,  0.0,  0.0,  1.0,
4410}};
4411
4412/* Values for the color transform matrices were precomputed using the source code
4413 * in surfaceflinger/Effects/Daltonizer.cpp. */
4414
4415static const std::array<const std::array<float, 16>, 5> exampleMatrices = {{
4416    identityMatrix,
4417    /* Converts RGB color to the XYZ space */
4418    {{ 0.4124, 0.2126, 0.0193, 0,
4419       0.3576, 0.7152, 0.1192, 0,
4420       0.1805, 0.0722, 0.9505, 0,
4421       0     , 0     , 0     , 1 }},
4422    /* Protanomaly */
4423    {{ 0.068493,  0.931506,  0,  0,
4424       0.068493,  0.931507,  0,  0,
4425       0.013626, -0.013626,  1,  0,
4426       0,         0,         0,  1 }},
4427    /* Deuteranomaly */
4428    {{ 0.288299, 0.711701,  0,  0,
4429       0.052709, 0.947291,  0,  0,
4430      -0.257912, 0.257912,  1,  0,
4431       0,        0,         0,  1 }},
4432    /* Tritanomaly */
4433    {{ 1, -0.805712, 0.805712,  0,
4434       0,  0.378838, 0.621162,  0,
4435       0,  0.104823, 0.895177,  0,
4436       0,  0,        0,         1 }},
4437}};
4438
4439/* TESTCASE: Tests that the HWC2 can set the identity color transform */
4440TEST_F(Hwc2Test, SET_COLOR_TRANSFORM)
4441{
4442    ASSERT_NO_FATAL_FAILURE(setActiveDisplayConfig(
4443            [] (Hwc2Test* test, hwc2_display_t display) {
4444
4445                EXPECT_NO_FATAL_FAILURE(test->setColorTransform(display,
4446                        identityMatrix, HAL_COLOR_TRANSFORM_IDENTITY));
4447            }
4448    ));
4449}
4450
4451/* TESTCASE: Tests that the HWC2 cannot set the color transform for a bad
4452 * display. */
4453TEST_F(Hwc2Test, SET_COLOR_TRANSFORM_bad_display)
4454{
4455    hwc2_display_t display;
4456    hwc2_error_t err = HWC2_ERROR_NONE;
4457
4458    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
4459
4460    ASSERT_NO_FATAL_FAILURE(setColorTransform(display, identityMatrix,
4461            HAL_COLOR_TRANSFORM_IDENTITY, &err));
4462    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
4463}
4464
4465/* TESTCASE: Tests that the HWC2 cannot set an invalid color transform. */
4466TEST_F(Hwc2Test, SET_COLOR_TRANSFORM_bad_parameter)
4467{
4468    ASSERT_NO_FATAL_FAILURE(setActiveDisplayConfig(
4469            [] (Hwc2Test* test, hwc2_display_t display) {
4470
4471                const android_color_transform_t hint =
4472                        static_cast<android_color_transform_t>(-1);
4473                hwc2_error_t err = HWC2_ERROR_NONE;
4474
4475                ASSERT_NO_FATAL_FAILURE(test->setColorTransform(display,
4476                        identityMatrix, hint, &err));
4477                EXPECT_EQ(err, HWC2_ERROR_BAD_PARAMETER)
4478                        << "returned wrong error code";
4479            }
4480    ));
4481}
4482
4483/* TESTCASE: Tests that the HWC2 can set an arbitrary color matrix. */
4484TEST_F(Hwc2Test, SET_COLOR_TRANSFORM_arbitrary_matrix)
4485{
4486    ASSERT_NO_FATAL_FAILURE(setActiveDisplayConfig(
4487            [] (Hwc2Test* test, hwc2_display_t display) {
4488
4489                const android_color_transform_t hint =
4490                        HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX;
4491
4492                for (const std::array<float, 16>& matrix : exampleMatrices) {
4493                    EXPECT_NO_FATAL_FAILURE(test->setColorTransform(display,
4494                            matrix, hint));
4495                }
4496            }
4497    ));
4498}
4499
4500/* TESTCASE: Tests that the HWC2 create an destory virtual displays. */
4501TEST_F(Hwc2Test, CREATE_DESTROY_VIRTUAL_DISPLAY)
4502{
4503    ASSERT_NO_FATAL_FAILURE(createVirtualDisplay(Hwc2TestCoverage::Complete,
4504            [] (Hwc2Test* /*test*/, hwc2_display_t /*display*/,
4505                    Hwc2TestVirtualDisplay* /*testVirtualDisplay*/) { }));
4506}
4507
4508/* TESTCASE: Tests that the HWC2 can create and destroy multiple virtual
4509 * displays. */
4510TEST_F(Hwc2Test, CREATE_DESTROY_VIRTUAL_DISPLAY_multiple)
4511{
4512    Hwc2TestVirtualDisplay testVirtualDisplay(Hwc2TestCoverage::Complete);
4513    std::vector<hwc2_display_t> displays;
4514
4515    do {
4516        const UnsignedArea& dimension =
4517                testVirtualDisplay.getDisplayDimension();
4518        android_pixel_format_t desiredFormat = HAL_PIXEL_FORMAT_RGBA_8888;
4519        hwc2_display_t display;
4520        hwc2_error_t err = HWC2_ERROR_NONE;
4521
4522        ASSERT_NO_FATAL_FAILURE(createVirtualDisplay(dimension.width,
4523                dimension.height, &desiredFormat, &display, &err));
4524
4525        EXPECT_TRUE(err == HWC2_ERROR_NONE || err == HWC2_ERROR_NO_RESOURCES
4526                || err == HWC2_ERROR_UNSUPPORTED) << "returned wrong error code";
4527        EXPECT_GE(desiredFormat, 0) << "invalid format";
4528
4529        if (err == HWC2_ERROR_NONE)
4530            displays.push_back(display);
4531
4532    } while (testVirtualDisplay.advance());
4533
4534    for (hwc2_display_t display : displays) {
4535        EXPECT_NO_FATAL_FAILURE(destroyVirtualDisplay(display));
4536    }
4537}
4538
4539/* TESTCASE: Tests that the HWC2 cannot destroy a bad virtual displays.  */
4540TEST_F(Hwc2Test, DESTROY_VIRTUAL_DISPLAY_bad_display)
4541{
4542    hwc2_display_t display;
4543    hwc2_error_t err = HWC2_ERROR_NONE;
4544
4545    ASSERT_NO_FATAL_FAILURE(getBadDisplay(&display));
4546
4547    ASSERT_NO_FATAL_FAILURE(destroyVirtualDisplay(display, &err));
4548    EXPECT_EQ(err, HWC2_ERROR_BAD_DISPLAY) << "returned wrong error code";
4549}
4550
4551/* TESTCASE: Tests that the HWC2 cannot destroy a physical display. */
4552TEST_F(Hwc2Test, DESTROY_VIRTUAL_DISPLAY_bad_parameter)
4553{
4554    hwc2_error_t err = HWC2_ERROR_NONE;
4555    for (auto display : mDisplays) {
4556        ASSERT_NO_FATAL_FAILURE(destroyVirtualDisplay(display, &err));
4557        EXPECT_EQ(err, HWC2_ERROR_BAD_PARAMETER) << "returned wrong error code";
4558    }
4559}
4560
4561/* TESTCASE: Tests that the HWC2 can get the max virtual display count. */
4562TEST_F(Hwc2Test, GET_MAX_VIRTUAL_DISPLAY_COUNT)
4563{
4564    uint32_t maxCnt;
4565
4566    ASSERT_NO_FATAL_FAILURE(getMaxVirtualDisplayCount(&maxCnt));
4567}
4568
4569/* TESTCASE: Tests that the HWC2 returns the same max virtual display count for
4570 * each call. */
4571TEST_F(Hwc2Test, GET_MAX_VIRTUAL_DISPLAY_COUNT_duplicate)
4572{
4573    uint32_t maxCnt1, maxCnt2;
4574
4575    ASSERT_NO_FATAL_FAILURE(getMaxVirtualDisplayCount(&maxCnt1));
4576    ASSERT_NO_FATAL_FAILURE(getMaxVirtualDisplayCount(&maxCnt2));
4577
4578    EXPECT_EQ(maxCnt1, maxCnt2) << "returned two different max virtual display"
4579            " counts";
4580}
4581
4582/* TESTCASE: Tests that the HWC2 can create the max number of virtual displays
4583 * that it reports. */
4584TEST_F(Hwc2Test, GET_MAX_VIRTUAL_DISPLAY_COUNT_create_max)
4585{
4586    std::vector<hwc2_display_t> displays;
4587    uint32_t maxCnt;
4588
4589    ASSERT_NO_FATAL_FAILURE(getMaxVirtualDisplayCount(&maxCnt));
4590
4591    while (displays.size() < maxCnt) {
4592        uint32_t width = 1920, height = 1080;
4593        android_pixel_format_t desiredFormat = HAL_PIXEL_FORMAT_RGBA_8888;
4594        hwc2_display_t display;
4595        hwc2_error_t err = HWC2_ERROR_NONE;
4596
4597        ASSERT_NO_FATAL_FAILURE(createVirtualDisplay(width, height,
4598                    &desiredFormat, &display, &err));
4599
4600        EXPECT_TRUE(err == HWC2_ERROR_NONE || err == HWC2_ERROR_UNSUPPORTED)
4601                << "returned wrong error code";
4602        if (err != HWC2_ERROR_NONE)
4603            break;
4604
4605        displays.push_back(display);
4606    }
4607
4608    for (hwc2_display_t display : displays) {
4609        EXPECT_NO_FATAL_FAILURE(destroyVirtualDisplay(display));
4610    }
4611}
4612
4613/* TESTCASE: Tests that the HWC2 can set an output buffer for a virtual
4614 * display. */
4615TEST_F(Hwc2Test, SET_OUTPUT_BUFFER)
4616{
4617    ASSERT_NO_FATAL_FAILURE(createVirtualDisplay(Hwc2TestCoverage::Complete,
4618            [] (Hwc2Test* test, hwc2_display_t display,
4619                    Hwc2TestVirtualDisplay* testVirtualDisplay) {
4620
4621                buffer_handle_t handle;
4622                android::base::unique_fd acquireFence;
4623
4624                if (testVirtualDisplay->getOutputBuffer(&handle, &acquireFence) >= 0)
4625                    EXPECT_NO_FATAL_FAILURE(test->setOutputBuffer(display,
4626                            handle, acquireFence));
4627            }));
4628}
4629
4630/* TESTCASE: Tests that the HWC2 cannot set an output buffer for a bad display */
4631TEST_F(Hwc2Test, SET_OUTPUT_BUFFER_bad_display)
4632{
4633    ASSERT_NO_FATAL_FAILURE(createVirtualDisplay(Hwc2TestCoverage::Default,
4634            [] (Hwc2Test* test, hwc2_display_t /*display*/,
4635                    Hwc2TestVirtualDisplay* testVirtualDisplay) {
4636
4637                hwc2_display_t badDisplay;
4638                buffer_handle_t handle;
4639                android::base::unique_fd acquireFence;
4640                hwc2_error_t err = HWC2_ERROR_NONE;
4641
4642                ASSERT_NO_FATAL_FAILURE(test->getBadDisplay(&badDisplay));
4643
4644                if (testVirtualDisplay->getOutputBuffer(&handle, &acquireFence) < 0)
4645                    return;
4646
4647                ASSERT_NO_FATAL_FAILURE(test->setOutputBuffer(badDisplay,
4648                        handle, acquireFence, &err));
4649                EXPECT_TRUE(err == HWC2_ERROR_BAD_DISPLAY)
4650                        << "returned wrong error code";
4651            }));
4652}
4653
4654/* TESTCASE: Tests that the HWC2 cannot set an invalid output buffer. */
4655TEST_F(Hwc2Test, SET_OUTPUT_BUFFER_bad_parameter)
4656{
4657    ASSERT_NO_FATAL_FAILURE(createVirtualDisplay(Hwc2TestCoverage::Default,
4658            [] (Hwc2Test* test, hwc2_display_t display,
4659                    Hwc2TestVirtualDisplay* /*testVirtualDisplay*/) {
4660
4661                const buffer_handle_t handle = nullptr;
4662                uint32_t releaseFence = -1;
4663                hwc2_error_t err = HWC2_ERROR_NONE;
4664
4665                ASSERT_NO_FATAL_FAILURE(test->setOutputBuffer(display, handle,
4666                        releaseFence, &err));
4667                EXPECT_EQ(err, HWC2_ERROR_BAD_PARAMETER)
4668                        << "returned wrong error code";
4669            }));
4670}
4671
4672/* TESTCASE: Tests that the HWC2 cannot set an output buffer for non virtual
4673 * display */
4674TEST_F(Hwc2Test, SET_OUTPUT_BUFFER_unsupported)
4675{
4676    for (auto display : mDisplays) {
4677        Hwc2TestVirtualDisplay testVirtualDisplay(Hwc2TestCoverage::Complete);
4678
4679        do {
4680            buffer_handle_t handle;
4681            android::base::unique_fd acquireFence;
4682            hwc2_error_t err = HWC2_ERROR_NONE;
4683
4684            if (testVirtualDisplay.getOutputBuffer(&handle, &acquireFence) < 0)
4685                continue;
4686
4687            ASSERT_NO_FATAL_FAILURE(setOutputBuffer(display, handle,
4688                    acquireFence, &err));
4689            EXPECT_EQ(err, HWC2_ERROR_UNSUPPORTED) << "returned wrong error code";
4690
4691        } while (testVirtualDisplay.advance());
4692    }
4693}
4694
4695/* TESTCASE: Tests that the HWC2 can dump debug information. */
4696TEST_F(Hwc2Test, DUMP)
4697{
4698    std::string buffer;
4699
4700    ASSERT_NO_FATAL_FAILURE(dump(&buffer));
4701}
4702
4703/*
4704 * TODO(b/64724708): Hwc2TestPropertyName::BufferArea MUST be default for all
4705 * virtual display tests as we don't handle this case correctly.
4706 *
4707 * Only default dataspace is supported in our drawing code.
4708 */
4709const std::unordered_map<Hwc2TestPropertyName, Hwc2TestCoverage>
4710        virtualDisplayExceptions =
4711        {{Hwc2TestPropertyName::BufferArea, Hwc2TestCoverage::Default},
4712        {Hwc2TestPropertyName::Dataspace, Hwc2TestCoverage::Default}};
4713
4714/* TESTCASE: Tests that the HWC2 can present 1 layer with default coverage on a
4715 * virtual display. */
4716TEST_F(Hwc2Test, PRESENT_VIRTUAL_DISPLAY_default_1)
4717{
4718    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4719    const size_t layerCnt = 1;
4720    ASSERT_NO_FATAL_FAILURE(createAndPresentVirtualDisplay(layerCnt, coverage,
4721            virtualDisplayExceptions));
4722}
4723
4724/* TESTCASE: Tests that the HWC2 can present 1 layer with basic coverage on a
4725 * virtual display. */
4726TEST_F(Hwc2Test, PRESENT_VIRTUAL_DISPLAY_basic_1)
4727{
4728    Hwc2TestCoverage coverage = Hwc2TestCoverage::Basic;
4729    const size_t layerCnt = 1;
4730    ASSERT_NO_FATAL_FAILURE(createAndPresentVirtualDisplay(layerCnt, coverage,
4731            virtualDisplayExceptions));
4732}
4733
4734/* TESTCASE: Tests that the HWC2 can present 2 layers with default coverage on a
4735 * virtual display. */
4736TEST_F(Hwc2Test, PRESENT_VIRTUAL_DISPLAY_default_2)
4737{
4738    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4739    const size_t layerCnt = 2;
4740    ASSERT_NO_FATAL_FAILURE(createAndPresentVirtualDisplay(layerCnt, coverage,
4741            virtualDisplayExceptions));
4742}
4743
4744/* TESTCASE: Tests that the HWC2 can present 3 layers with default coverage on a
4745 * virtual display. */
4746TEST_F(Hwc2Test, PRESENT_VIRTUAL_DISPLAY_default_3)
4747{
4748    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4749    const size_t layerCnt = 3;
4750    ASSERT_NO_FATAL_FAILURE(createAndPresentVirtualDisplay(layerCnt, coverage,
4751            virtualDisplayExceptions));
4752}
4753
4754/* TESTCASE: Tests that the HWC2 can present 4 layers with default coverage on a
4755 * virtual display. */
4756TEST_F(Hwc2Test, PRESENT_VIRTUAL_DISPLAY_default_4)
4757{
4758    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4759    const size_t layerCnt = 4;
4760    ASSERT_NO_FATAL_FAILURE(createAndPresentVirtualDisplay(layerCnt, coverage,
4761            virtualDisplayExceptions));
4762}
4763
4764/* TESTCASE: Tests that the HWC2 can present 5 layers with default coverage on a
4765 * virtual display. */
4766TEST_F(Hwc2Test, PRESENT_VIRTUAL_DISPLAY_default_5)
4767{
4768    Hwc2TestCoverage coverage = Hwc2TestCoverage::Default;
4769    const size_t layerCnt = 5;
4770    ASSERT_NO_FATAL_FAILURE(createAndPresentVirtualDisplay(layerCnt, coverage,
4771            virtualDisplayExceptions));
4772}
4773