• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2021 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // CLImage.cpp: Implements the cl::Image class.
7 
8 #include "libANGLE/CLImage.h"
9 
10 #include "libANGLE/cl_utils.h"
11 
12 #include <cstring>
13 
14 namespace cl
15 {
16 
IsTypeValid(MemObjectType imageType)17 bool Image::IsTypeValid(MemObjectType imageType)
18 {
19     switch (imageType)
20     {
21         case MemObjectType::Image1D:
22         case MemObjectType::Image2D:
23         case MemObjectType::Image3D:
24         case MemObjectType::Image1D_Array:
25         case MemObjectType::Image2D_Array:
26         case MemObjectType::Image1D_Buffer:
27             break;
28         default:
29             return false;
30     }
31     return true;
32 }
33 
getInfo(ImageInfo name,size_t valueSize,void * value,size_t * valueSizeRet) const34 cl_int Image::getInfo(ImageInfo name, size_t valueSize, void *value, size_t *valueSizeRet) const
35 {
36     size_t valSizeT       = 0u;
37     void *valPointer      = nullptr;
38     const void *copyValue = nullptr;
39     size_t copySize       = 0u;
40 
41     switch (name)
42     {
43         case ImageInfo::Format:
44             copyValue = &mFormat;
45             copySize  = sizeof(mFormat);
46             break;
47         case ImageInfo::ElementSize:
48             valSizeT  = GetElementSize(mFormat);
49             copyValue = &valSizeT;
50             copySize  = sizeof(valSizeT);
51             break;
52         case ImageInfo::RowPitch:
53             copyValue = &mDesc.rowPitch;
54             copySize  = sizeof(mDesc.rowPitch);
55             break;
56         case ImageInfo::SlicePitch:
57             copyValue = &mDesc.slicePitch;
58             copySize  = sizeof(mDesc.slicePitch);
59             break;
60         case ImageInfo::Width:
61             copyValue = &mDesc.width;
62             copySize  = sizeof(mDesc.width);
63             break;
64         case ImageInfo::Height:
65             copyValue = &mDesc.height;
66             copySize  = sizeof(mDesc.height);
67             break;
68         case ImageInfo::Depth:
69             copyValue = &mDesc.depth;
70             copySize  = sizeof(mDesc.depth);
71             break;
72         case ImageInfo::ArraySize:
73             copyValue = &mDesc.arraySize;
74             copySize  = sizeof(mDesc.arraySize);
75             break;
76         case ImageInfo::Buffer:
77             valPointer = Memory::CastNative(mParent.get());
78             copyValue  = &valPointer;
79             copySize   = sizeof(valPointer);
80             break;
81         case ImageInfo::NumMipLevels:
82             copyValue = &mDesc.numMipLevels;
83             copySize  = sizeof(mDesc.numMipLevels);
84             break;
85         case ImageInfo::NumSamples:
86             copyValue = &mDesc.numSamples;
87             copySize  = sizeof(mDesc.numSamples);
88             break;
89         default:
90             return CL_INVALID_VALUE;
91     }
92 
93     if (value != nullptr)
94     {
95         // CL_INVALID_VALUE if size in bytes specified by param_value_size is < size of return type
96         // as described in the Image Object Queries table and param_value is not NULL.
97         if (valueSize < copySize)
98         {
99             return CL_INVALID_VALUE;
100         }
101         if (copyValue != nullptr)
102         {
103             std::memcpy(value, copyValue, copySize);
104         }
105     }
106     if (valueSizeRet != nullptr)
107     {
108         *valueSizeRet = copySize;
109     }
110     return CL_SUCCESS;
111 }
112 
113 Image::~Image() = default;
114 
isRegionValid(const size_t origin[3],const size_t region[3]) const115 bool Image::isRegionValid(const size_t origin[3], const size_t region[3]) const
116 {
117     switch (getType())
118     {
119         case MemObjectType::Image1D:
120         case MemObjectType::Image1D_Buffer:
121             return origin[0] + region[0] <= mDesc.width;
122         case MemObjectType::Image2D:
123             return origin[0] + region[0] <= mDesc.width && origin[1] + region[1] <= mDesc.height;
124         case MemObjectType::Image3D:
125             return origin[0] + region[0] <= mDesc.width && origin[1] + region[1] <= mDesc.height &&
126                    origin[2] + region[2] <= mDesc.depth;
127         case MemObjectType::Image1D_Array:
128             return origin[0] + region[0] <= mDesc.width && origin[1] + region[1] <= mDesc.arraySize;
129         case MemObjectType::Image2D_Array:
130             return origin[0] + region[0] <= mDesc.width && origin[1] + region[1] <= mDesc.height &&
131                    origin[2] + region[2] <= mDesc.arraySize;
132         default:
133             ASSERT(false);
134             break;
135     }
136     return false;
137 }
138 
Image(Context & context,PropArray && properties,MemFlags flags,const cl_image_format & format,const ImageDescriptor & desc,Memory * parent,void * hostPtr,cl_int & errorCode)139 Image::Image(Context &context,
140              PropArray &&properties,
141              MemFlags flags,
142              const cl_image_format &format,
143              const ImageDescriptor &desc,
144              Memory *parent,
145              void *hostPtr,
146              cl_int &errorCode)
147     : Memory(*this,
148              context,
149              std::move(properties),
150              flags,
151              format,
152              desc,
153              parent,
154              hostPtr,
155              errorCode),
156       mFormat(format),
157       mDesc(desc)
158 {}
159 
160 }  // namespace cl
161