Welcome to mirror list, hosted at ThFree Co, Russian Federation.

hw_texture_ios.mm « drape - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 538d24af05a56c10daa93958b4e91e620d0219b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "hw_texture_ios.hpp"

#include "base/logging.hpp"

#include "drape/glfunctions.hpp"

#import <QuartzCore/CAEAGLLayer.h>

#import <Foundation/NSDictionary.h>
#import <Foundation/NSValue.h>

#include <boost/integer_traits.hpp>
#include <boost/gil/algorithm.hpp>
#include <boost/gil/typedefs.hpp>

using boost::gil::gray8c_pixel_t;
using boost::gil::gray8_pixel_t;
using boost::gil::gray8c_view_t;
using boost::gil::gray8_view_t;
using boost::gil::rgba8c_pixel_t;
using boost::gil::rgba8_pixel_t;
using boost::gil::rgba8c_view_t;
using boost::gil::rgba8_view_t;
using boost::gil::interleaved_view;
using boost::gil::subimage_view;
using boost::gil::copy_pixels;

namespace dp
{

HWTextureAllocatorApple::HWTextureAllocatorApple()
{
  CVReturn cvRetval = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, nullptr,
                                                   [EAGLContext currentContext],
                                                   nullptr, &m_textureCache);

  CHECK_EQUAL(cvRetval, kCVReturnSuccess, ());
}

HWTextureAllocatorApple::~HWTextureAllocatorApple()
{
  CFRelease(m_textureCache);
}

CVPixelBufferRef HWTextureAllocatorApple::CVCreatePixelBuffer(uint32_t width, uint32_t height, int format)
{
  NSDictionary * attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                            [NSDictionary dictionary], kCVPixelBufferIOSurfacePropertiesKey,
                            [NSNumber numberWithInt:16], kCVPixelBufferBytesPerRowAlignmentKey,
                            [NSNumber numberWithBool:YES], kCVPixelBufferOpenGLESCompatibilityKey,
                            nil];

  CFDictionaryRef attrsRef = (__bridge CFDictionaryRef)attrs;

  CVPixelBufferRef result;
  CVReturn cvRetval;
  switch (format)
  {
  case dp::RGBA8:
    cvRetval = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, attrsRef, &result);
    break;
  case dp::ALPHA:
    cvRetval = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_OneComponent8,
                                   attrsRef, &result);
    break;
  default:
    ASSERT(false, ());
    break;
  }

  CHECK_EQUAL(cvRetval, kCVReturnSuccess, ());

  return result;
}

void HWTextureAllocatorApple::CVDestroyPixelBuffer(CVPixelBufferRef buffer)
{
  CFRelease(buffer);
}

CVOpenGLESTextureRef HWTextureAllocatorApple::CVCreateTexture(CVPixelBufferRef buffer, uint32_t width, uint32_t height,
                                                              glConst layout, glConst pixelType)
{
  CVOpenGLESTextureRef texture;
  CVReturn cvRetval = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault, m_textureCache, buffer,
                                                                   nullptr, gl_const::GLTexture2D, layout,
                                                                   width, height, layout, pixelType, 0, &texture);

  CHECK_EQUAL(cvRetval, kCVReturnSuccess, ());

  return texture;
}

void HWTextureAllocatorApple::CVDestroyTexture(CVOpenGLESTextureRef texture)
{
  CFRelease(texture);
}

void HWTextureAllocatorApple::RiseFlushFlag()
{
  m_needFlush = true;
}

drape_ptr<HWTexture> HWTextureAllocatorApple::CreateTexture()
{
  return make_unique_dp<HWTextureApple>(make_ref<HWTextureAllocatorApple>(this));
}

void HWTextureAllocatorApple::Flush()
{
  if (m_needFlush)
  {
    CVOpenGLESTextureCacheFlush(m_textureCache, 0);
    m_needFlush = false;
  }
}

HWTextureApple::HWTextureApple(ref_ptr<HWTextureAllocatorApple> allocator)
  : m_directBuffer(nullptr)
  , m_texture(nullptr)
  , m_allocator(nullptr)
  , m_directPointer(nullptr)
{
}

HWTextureApple::~HWTextureApple()
{
  if (m_allocator != nullptr)
  {
    m_allocator->CVDestroyTexture(m_texture);
    m_allocator->CVDestroyPixelBuffer(m_directBuffer);
  }
}

void HWTextureApple::Create(Params const & params, ref_ptr<void> data)
{
  TBase::Create(params, data);

  m_allocator = params.m_allocator.downcast<HWTextureAllocatorApple>();
  m_directBuffer = m_allocator->CVCreatePixelBuffer(params.m_width, params.m_height, params.m_format);

  glConst layout, pixelType;
  UnpackFormat(params.m_format, layout, pixelType);
  m_texture = m_allocator->CVCreateTexture(m_directBuffer, params.m_width, params.m_height,
                                         layout, pixelType);

  m_textureID = CVOpenGLESTextureGetName(m_texture);
  GLFunctions::glBindTexture(m_textureID);
  GLFunctions::glTexParameter(gl_const::GLMinFilter, params.m_filter);
  GLFunctions::glTexParameter(gl_const::GLMagFilter, params.m_filter);
  GLFunctions::glTexParameter(gl_const::GLWrapS, params.m_wrapSMode);
  GLFunctions::glTexParameter(gl_const::GLWrapT, params.m_wrapTMode);

  if (data == nullptr)
    return;

  Lock();
  memcpy(m_directPointer, data.get(), m_width * m_height * GetBytesPerPixel(m_format));
  Unlock();
}

void HWTextureApple::UploadData(uint32_t x, uint32_t y, uint32_t width, uint32_t height, ref_ptr<void> data)
{
  uint8_t bytesPerPixel = GetBytesPerPixel(GetFormat());
  Lock();
  if (bytesPerPixel == 1)
  {
    gray8c_view_t srcView = interleaved_view(width, height, (gray8c_pixel_t *)data.get(), width);
    gray8_view_t dstView = interleaved_view(m_width, m_height, (gray8_pixel_t *)m_directPointer, m_width);
    gray8_view_t subDstView = subimage_view(dstView, x, y, width, height);
    copy_pixels(srcView, subDstView);
  }
  else if (bytesPerPixel == 4)
  {
    rgba8c_view_t srcView = interleaved_view(width, height,
                                             (rgba8c_pixel_t *)data.get(),
                                             width * bytesPerPixel);
    rgba8_view_t dstView = interleaved_view(m_width, m_height,
                                            (rgba8_pixel_t *)m_directPointer,
                                            m_width * bytesPerPixel);
    rgba8_view_t subDstView = subimage_view(dstView, x, y, width, height);
    copy_pixels(srcView, subDstView);
  }
  Unlock();
}

void HWTextureApple::Lock()
{
  ASSERT(m_directPointer == nullptr, ());

  CHECK_EQUAL(CVPixelBufferLockBaseAddress(m_directBuffer, 0), kCVReturnSuccess, ());

  ASSERT_EQUAL(CVPixelBufferGetBytesPerRow(m_directBuffer), m_width * GetBytesPerPixel(m_format), ());
  m_directPointer = CVPixelBufferGetBaseAddress(m_directBuffer);
}

void HWTextureApple::Unlock()
{
  ASSERT(m_directPointer != nullptr, ());
  m_directPointer = nullptr;
  CHECK_EQUAL(CVPixelBufferUnlockBaseAddress(m_directBuffer, 0), kCVReturnSuccess, ());
  m_allocator->RiseFlushFlag();
}

}