1/*
2 * Copyright 2018 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#define LOG_TAG "GraphicBufferTest"
18
19#include <ui/DetachedBufferHandle.h>
20#include <ui/GraphicBuffer.h>
21
22#include <gtest/gtest.h>
23
24namespace android {
25
26namespace {
27
28constexpr uint32_t kTestWidth = 1024;
29constexpr uint32_t kTestHeight = 1;
30constexpr uint32_t kTestFormat = HAL_PIXEL_FORMAT_BLOB;
31constexpr uint32_t kTestLayerCount = 1;
32constexpr uint64_t kTestUsage = GraphicBuffer::USAGE_SW_WRITE_OFTEN;
33
34} // namespace
35
36class GraphicBufferTest : public testing::Test {};
37
38TEST_F(GraphicBufferTest, DetachedBuffer) {
39    sp<GraphicBuffer> buffer(
40            new GraphicBuffer(kTestWidth, kTestHeight, kTestFormat, kTestLayerCount, kTestUsage));
41
42    // Currently a newly allocated GraphicBuffer is in legacy mode, i.e. not associated with
43    // BufferHub. But this may change in the future.
44    EXPECT_FALSE(buffer->isDetachedBuffer());
45
46    pdx::LocalChannelHandle channel{nullptr, 1234};
47    EXPECT_TRUE(channel.valid());
48
49    std::unique_ptr<DetachedBufferHandle> handle = DetachedBufferHandle::Create(std::move(channel));
50    EXPECT_FALSE(channel.valid());
51    EXPECT_TRUE(handle->isValid());
52    EXPECT_TRUE(handle->handle().valid());
53
54    buffer->setDetachedBufferHandle(std::move(handle));
55    EXPECT_TRUE(handle == nullptr);
56    EXPECT_TRUE(buffer->isDetachedBuffer());
57
58    handle = buffer->takeDetachedBufferHandle();
59    EXPECT_TRUE(handle != nullptr);
60    EXPECT_TRUE(handle->isValid());
61    EXPECT_FALSE(buffer->isDetachedBuffer());
62}
63
64} // namespace android
65