• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
3 
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *   * Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *   * Redistributions in binary form must reproduce the above
10  *     copyright notice, this list of conditions and the following
11  *     disclaimer in the documentation and/or other materials provided
12  *     with the distribution.
13  *   * Neither the name of The Linux Foundation nor the names of its
14  *     contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef __GR_BUF_DESCRIPTOR_H__
31 #define __GR_BUF_DESCRIPTOR_H__
32 
33 #include <android/hardware/graphics/mapper/2.1/IMapper.h>
34 #include <atomic>
35 
36 namespace gralloc {
37 using android::hardware::graphics::mapper::V2_0::Error;
38 using android::hardware::graphics::mapper::V2_1::IMapper;
39 using android::hardware::hidl_vec;
40 
41 const uint32_t kBufferDescriptorSize = 7;
42 const uint32_t kMagicVersion = 0x76312E30;  // v1.0
43 
44 class BufferDescriptor {
45  public:
BufferDescriptor()46   BufferDescriptor() {}
BufferDescriptor(uint64_t id)47   explicit BufferDescriptor(uint64_t id) : id_(id) {}
48 
Encode(const IMapper::BufferDescriptorInfo & bd_info)49   static hidl_vec<uint32_t> Encode(const IMapper::BufferDescriptorInfo &bd_info) {
50     hidl_vec<uint32_t> out;
51     out.resize(kBufferDescriptorSize);
52     out[0] = kMagicVersion;
53     out[1] = bd_info.width;
54     out[2] = bd_info.height;
55     out[3] = bd_info.layerCount;
56     out[4] = static_cast<uint32_t>(bd_info.format);
57     out[5] = static_cast<uint32_t>(bd_info.usage);
58     out[6] = static_cast<uint32_t>(bd_info.usage >> 32);
59     return out;
60   }
61 
Decode(const hidl_vec<uint32_t> & in)62   Error Decode(const hidl_vec<uint32_t> &in) {
63     if (in.size() != kBufferDescriptorSize || in[0] != kMagicVersion) {
64       return Error::BAD_DESCRIPTOR;
65     }
66     width_ = static_cast<int32_t>(in[1]);
67     height_ = static_cast<int32_t>(in[2]);
68     layer_count_ = in[3];
69     format_ = static_cast<int32_t>(in[4]);
70     usage_ = static_cast<uint64_t>(in[6]) << 32 | in[5];
71     return Error::NONE;
72   }
73 
SetUsage(uint64_t usage)74   void SetUsage(uint64_t usage) { usage_ |= usage; }
75 
SetDimensions(int w,int h)76   void SetDimensions(int w, int h) {
77     width_ = w;
78     height_ = h;
79   }
80 
SetColorFormat(int format)81   void SetColorFormat(int format) { format_ = format; }
82 
SetLayerCount(uint32_t layer_count)83   void SetLayerCount(uint32_t layer_count) { layer_count_ = layer_count; }
84 
GetUsage()85   uint64_t GetUsage() const { return usage_; }
86 
GetWidth()87   int GetWidth() const { return width_; }
88 
GetHeight()89   int GetHeight() const { return height_; }
90 
GetFormat()91   int GetFormat() const { return format_; }
92 
GetLayerCount()93   uint32_t GetLayerCount() const { return layer_count_; }
94 
GetId()95   uint64_t GetId() const { return id_; }
96 
97  private:
98   int width_ = -1;
99   int height_ = -1;
100   int format_ = -1;
101   uint32_t layer_count_ = 1;
102   uint64_t usage_ = 0;
103   const uint64_t id_ = 0;
104 };
105 };      // namespace gralloc
106 #endif  // __GR_BUF_DESCRIPTOR_H__
107