Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 89e319f7 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "android.hardware.media.bufferpool@2.0 tests"

parents dc1cbab5 9ec33560
Loading
Loading
Loading
Loading
+51 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

cc_test {
    name: "VtsVndkHidlBufferpoolV2_0TargetSingleTest",
    defaults: ["VtsHalTargetTestDefaults"],
    srcs: [
        "allocator.cpp",
        "single.cpp",
    ],
    static_libs: [
        "android.hardware.media.bufferpool@2.0",
        "libcutils",
        "libstagefright_bufferpool@2.0",
    ],
    shared_libs: [
        "libfmq",
    ],
    compile_multilib: "both",
}

cc_test {
    name: "VtsVndkHidlBufferpoolV2_0TargetMultiTest",
    defaults: ["VtsHalTargetTestDefaults"],
    srcs: [
        "allocator.cpp",
        "multi.cpp",
    ],
    static_libs: [
        "android.hardware.media.bufferpool@2.0",
        "libcutils",
        "libstagefright_bufferpool@2.0",
    ],
    shared_libs: [
        "libfmq",
    ],
    compile_multilib: "both",
}
+9 −0
Original line number Diff line number Diff line
# Media team
lajos@google.com
pawin@google.com
taklee@google.com
wonsik@google.com

# VTS team
yim@google.com
zhuoyao@google.com
+158 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cutils/ashmem.h>
#include "allocator.h"

union Params {
  struct {
    uint32_t capacity;
  } data;
  uint8_t array[0];
  Params() : data{0} {}
  Params(uint32_t size)
      : data{size} {}
};


namespace {

struct HandleAshmem : public native_handle_t {
  HandleAshmem(int ashmemFd, size_t size)
    : native_handle_t(cHeader),
    mFds{ ashmemFd },
    mInts{ int (size & 0xFFFFFFFF), int((uint64_t(size) >> 32) & 0xFFFFFFFF), kMagic } {}

  int ashmemFd() const { return mFds.mAshmem; }
  size_t size() const {
    return size_t(unsigned(mInts.mSizeLo))
        | size_t(uint64_t(unsigned(mInts.mSizeHi)) << 32);
  }

protected:
  struct {
    int mAshmem;
  } mFds;
  struct {
    int mSizeLo;
    int mSizeHi;
    int mMagic;
  } mInts;

private:
  enum {
    kMagic = 'ahm\x00',
    numFds = sizeof(mFds) / sizeof(int),
    numInts = sizeof(mInts) / sizeof(int),
    version = sizeof(native_handle_t)
  };
  const static native_handle_t cHeader;
};

const native_handle_t HandleAshmem::cHeader = {
  HandleAshmem::version,
  HandleAshmem::numFds,
  HandleAshmem::numInts,
  {}
};

class AllocationAshmem {
private:
  AllocationAshmem(int ashmemFd, size_t capacity, bool res)
    : mHandle(ashmemFd, capacity),
      mInit(res) {}

public:
  static AllocationAshmem *Alloc(size_t size) {
    constexpr static const char *kAllocationTag = "bufferpool_test";
    int ashmemFd = ashmem_create_region(kAllocationTag, size);
    return new AllocationAshmem(ashmemFd, size, ashmemFd >= 0);
  }

  ~AllocationAshmem() {
    if (mInit) {
      native_handle_close(&mHandle);
    }
  }

  const HandleAshmem *handle() {
    return &mHandle;
  }

private:
  HandleAshmem mHandle;
  bool mInit;
  // TODO: mapping and map fd
};

struct AllocationDtor {
  AllocationDtor(const std::shared_ptr<AllocationAshmem> &alloc)
      : mAlloc(alloc) {}

  void operator()(BufferPoolAllocation *poolAlloc) { delete poolAlloc; }

  const std::shared_ptr<AllocationAshmem> mAlloc;
};

}


ResultStatus TestBufferPoolAllocator::allocate(
    const std::vector<uint8_t> &params,
    std::shared_ptr<BufferPoolAllocation> *alloc,
    size_t *allocSize) {
  Params ashmemParams;
  memcpy(&ashmemParams, params.data(), std::min(sizeof(Params), params.size()));

  std::shared_ptr<AllocationAshmem> ashmemAlloc =
      std::shared_ptr<AllocationAshmem>(
          AllocationAshmem::Alloc(ashmemParams.data.capacity));
  if (ashmemAlloc) {
    BufferPoolAllocation *ptr = new BufferPoolAllocation(ashmemAlloc->handle());
    if (ptr) {
      *alloc = std::shared_ptr<BufferPoolAllocation>(ptr, AllocationDtor(ashmemAlloc));
      if (*alloc) {
          *allocSize = ashmemParams.data.capacity;
          return ResultStatus::OK;
      }
      delete ptr;
      return ResultStatus::NO_MEMORY;
    }
  }
  return ResultStatus::CRITICAL_ERROR;
}

bool TestBufferPoolAllocator::compatible(const std::vector<uint8_t> &newParams,
                                        const std::vector<uint8_t> &oldParams) {
  size_t newSize = newParams.size();
  size_t oldSize = oldParams.size();
  if (newSize == oldSize) {
    for (size_t i = 0; i < newSize; ++i) {
      if (newParams[i] != oldParams[i]) {
        return false;
      }
    }
    return true;
  }
  return false;
}

void getTestAllocatorParams(std::vector<uint8_t> *params) {
  constexpr static int kAllocationSize = 1024 * 10;
  Params ashmemParams(kAllocationSize);

  params->assign(ashmemParams.array, ashmemParams.array + sizeof(ashmemParams));
}
+47 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef VNDK_HIDL_BUFFERPOOL_V2_0_ALLOCATOR_H
#define VNDK_HIDL_BUFFERPOOL_V2_0_ALLOCATOR_H

#include <bufferpool/BufferPoolTypes.h>

using android::hardware::media::bufferpool::V2_0::ResultStatus;
using android::hardware::media::bufferpool::V2_0::implementation::
    BufferPoolAllocation;
using android::hardware::media::bufferpool::V2_0::implementation::
    BufferPoolAllocator;

// buffer allocator for the tests
class TestBufferPoolAllocator : public BufferPoolAllocator {
 public:
  TestBufferPoolAllocator() {}

  ~TestBufferPoolAllocator() override {}

  ResultStatus allocate(const std::vector<uint8_t> &params,
                        std::shared_ptr<BufferPoolAllocation> *alloc,
                        size_t *allocSize) override;

  bool compatible(const std::vector<uint8_t> &newParams,
                  const std::vector<uint8_t> &oldParams) override;

};

// retrieve buffer allocator paramters
void getTestAllocatorParams(std::vector<uint8_t> *params);

#endif  // VNDK_HIDL_BUFFERPOOL_V2_0_ALLOCATOR_H
+219 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "buffferpool_unit_test"

#include <gtest/gtest.h>

#include <android-base/logging.h>
#include <binder/ProcessState.h>
#include <bufferpool/ClientManager.h>
#include <hidl/HidlSupport.h>
#include <hidl/HidlTransportSupport.h>
#include <hidl/LegacySupport.h>
#include <hidl/Status.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <iostream>
#include <memory>
#include <vector>
#include "allocator.h"

using android::hardware::configureRpcThreadpool;
using android::hardware::hidl_handle;
using android::hardware::media::bufferpool::V2_0::IClientManager;
using android::hardware::media::bufferpool::V2_0::ResultStatus;
using android::hardware::media::bufferpool::V2_0::implementation::BufferId;
using android::hardware::media::bufferpool::V2_0::implementation::ClientManager;
using android::hardware::media::bufferpool::V2_0::implementation::ConnectionId;
using android::hardware::media::bufferpool::V2_0::implementation::TransactionId;
using android::hardware::media::bufferpool::BufferPoolData;

namespace {

// communication message types between processes.
enum PipeCommand : int32_t {
    INIT_OK = 0,
    INIT_ERROR,
    SEND,
    RECEIVE_OK,
    RECEIVE_ERROR,
};

// communication message between processes.
union PipeMessage {
    struct  {
        int32_t command;
        BufferId bufferId;
        ConnectionId connectionId;
        TransactionId transactionId;
        int64_t  timestampUs;
    } data;
    char array[0];
};

// media.bufferpool test setup
class BufferpoolMultiTest : public ::testing::Test {
 public:
  virtual void SetUp() override {
    ResultStatus status;
    mReceiverPid = -1;
    mConnectionValid = false;

    ASSERT_TRUE(pipe(mCommandPipeFds) == 0);
    ASSERT_TRUE(pipe(mResultPipeFds) == 0);

    mReceiverPid = fork();
    ASSERT_TRUE(mReceiverPid >= 0);

    if (mReceiverPid == 0) {
      doReceiver();
      // In order to ignore gtest behaviour, wait for being killed from
      // tearDown
      pause();
    }

    mManager = ClientManager::getInstance();
    ASSERT_NE(mManager, nullptr);

    mAllocator = std::make_shared<TestBufferPoolAllocator>();
    ASSERT_TRUE((bool)mAllocator);

    status = mManager->create(mAllocator, &mConnectionId);
    ASSERT_TRUE(status == ResultStatus::OK);
    mConnectionValid = true;
  }

  virtual void TearDown() override {
    if (mReceiverPid > 0) {
      kill(mReceiverPid, SIGKILL);
      int wstatus;
      wait(&wstatus);
    }

    if (mConnectionValid) {
      mManager->close(mConnectionId);
    }
  }

 protected:
  static void description(const std::string& description) {
    RecordProperty("description", description);
  }

  android::sp<ClientManager> mManager;
  std::shared_ptr<BufferPoolAllocator> mAllocator;
  bool mConnectionValid;
  ConnectionId mConnectionId;
  pid_t mReceiverPid;
  int mCommandPipeFds[2];
  int mResultPipeFds[2];

  bool sendMessage(int *pipes, const PipeMessage &message) {
    int ret = write(pipes[1], message.array, sizeof(PipeMessage));
    return ret == sizeof(PipeMessage);
  }

  bool receiveMessage(int *pipes, PipeMessage *message) {
    int ret = read(pipes[0], message->array, sizeof(PipeMessage));
    return ret == sizeof(PipeMessage);
  }

  void doReceiver() {
    configureRpcThreadpool(1, false);
    PipeMessage message;
    mManager = ClientManager::getInstance();
    if (!mManager) {
      message.data.command = PipeCommand::INIT_ERROR;
      sendMessage(mResultPipeFds, message);
      return;
    }
    android::status_t status = mManager->registerAsService();
    if (status != android::OK) {
      message.data.command = PipeCommand::INIT_ERROR;
      sendMessage(mResultPipeFds, message);
      return;
    }
    message.data.command = PipeCommand::INIT_OK;
    sendMessage(mResultPipeFds, message);

    receiveMessage(mCommandPipeFds, &message);
    {
      native_handle_t *rhandle = nullptr;
      std::shared_ptr<BufferPoolData> rbuffer;
      ResultStatus status = mManager->receive(
          message.data.connectionId, message.data.transactionId,
          message.data.bufferId, message.data.timestampUs, &rhandle, &rbuffer);
      mManager->close(message.data.connectionId);
      if (status != ResultStatus::OK) {
        message.data.command = PipeCommand::RECEIVE_ERROR;
        sendMessage(mResultPipeFds, message);
        return;
      }
    }
    message.data.command = PipeCommand::RECEIVE_OK;
    sendMessage(mResultPipeFds, message);
  }
};

// Buffer transfer test between processes.
TEST_F(BufferpoolMultiTest, TransferBuffer) {
  ResultStatus status;
  PipeMessage message;

  ASSERT_TRUE(receiveMessage(mResultPipeFds, &message));

  android::sp<IClientManager> receiver = IClientManager::getService();
  ConnectionId receiverId;
  ASSERT_TRUE((bool)receiver);

  status = mManager->registerSender(receiver, mConnectionId, &receiverId);
  ASSERT_TRUE(status == ResultStatus::OK);
  {
    native_handle_t *shandle = nullptr;
    std::shared_ptr<BufferPoolData> sbuffer;
    TransactionId transactionId;
    int64_t postUs;
    std::vector<uint8_t> vecParams;

    getTestAllocatorParams(&vecParams);
    status = mManager->allocate(mConnectionId, vecParams, &shandle, &sbuffer);
    ASSERT_TRUE(status == ResultStatus::OK);

    status = mManager->postSend(receiverId, sbuffer, &transactionId, &postUs);
    ASSERT_TRUE(status == ResultStatus::OK);

    message.data.command = PipeCommand::SEND;
    message.data.bufferId = sbuffer->mId;
    message.data.connectionId = receiverId;
    message.data.transactionId = transactionId;
    message.data.timestampUs = postUs;
    sendMessage(mCommandPipeFds, message);
  }
  EXPECT_TRUE(receiveMessage(mResultPipeFds, &message));
}

}  // anonymous namespace

int main(int argc, char** argv) {
  setenv("TREBLE_TESTING_OVERRIDE", "true", true);
  ::testing::InitGoogleTest(&argc, argv);
  int status = RUN_ALL_TESTS();
  LOG(INFO) << "Test result = " << status;
  return status;
}
Loading