Caffe2 - C++ API
A deep learning, cross platform ML framework
IOSGLImageAllocator.cc
1 
2 #include "IOSGLImageAllocator.h"
3 
4 #include "../core/GLImage.h"
5 #include "../core/GLImageAllocator.h"
6 #include "../core/GLPlainTexture.h"
7 
8 #include "IOSGLContext.h"
9 #include "IOSGLTexture.h"
10 
11 #include "../core/arm_neon_support.h"
12 
13 template <class T>
15  int width,
16  int height,
17  int channels,
18  int tile_x,
19  int tile_y,
20  bool useCVPixelBuffer) {
21  GLImageVector<T>* output_images =
22  new GLImageVector<T>(num_images, width, height, channels, tile_x, tile_y);
23  if (useCVPixelBuffer) {
24  IOSGLContext* gl_context = (IOSGLContext*)GLContext::getGLContext();
25  for (int i = 0; i < num_images; i++) {
26  GLImage<T>* output_image = new GLImage<T>(
27  width, height, channels, tile_x, tile_y, [&](int slice) -> const GLTexture* {
28  gl_log(GL_VERBOSE,
29  "%s pixelbuffers.size(): %ld\n",
30  __PRETTY_FUNCTION__,
31  pixelbuffers.size());
32 
33  CVPixelBufferRef buffer = NULL;
34  int slices = (channels + 3) / 4;
35  int slice_index = i * slices + slice;
36  if (pixelbuffers.size() < slice_index + 1) {
37  const int texture_width = width * tile_x;
38  const int texture_height = height * tile_y;
39  buffer =
40  IOSGLTexture::createCVPixelBuffer(pixelFormat, texture_width, texture_height);
41  gl_log(GL_VERBOSE,
42  "created a new buffer %p for image %d slice %d of dimensions %dx%d\n",
43  buffer,
44  i,
45  slice,
46  texture_width,
47  texture_height);
48  pixelbuffers.push_back(buffer);
49  } else {
50  buffer = pixelbuffers[slice_index];
51 
52  gl_log(GL_VERBOSE, "reused buffer %p for image %d slice %d\n", buffer, i, slice);
53  }
54 
55  return gl_context->createNewTexture(buffer, GLImageAllocator<T>::type);
56  });
57  output_images->push_back(output_image);
58  }
59  } else {
60  for (int i = 0; i < num_images; i++) {
61  GLImage<T>* image = new GLImage<T>(
62  width, height, channels, tile_x, tile_y, [&](int slice) -> const GLTexture* {
63  return new GLPlainTexture(
64  GLImageAllocator<T>::type, nullptr, width * tile_x, height * tile_y);
65  });
66  output_images->push_back(image);
67  }
68  }
69  return output_images;
70 }
71 
72 template <>
73 const FourCharCode IOSGLImageAllocator<float16_t>::pixelFormat = kCVPixelFormatType_64RGBAHalf;
74 template <>
75 const FourCharCode IOSGLImageAllocator<uint8_t>::pixelFormat = kCVPixelFormatType_32BGRA;
76 
77 template class IOSGLImageAllocator<float16_t>;
78 template class IOSGLImageAllocator<uint8_t>;