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

EAGLView.mm « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 842ec24bef23f00d21059b4e3cfccd9eef96d217 (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
206
207
208
209
210
211
212
213
214
215
216
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGLDrawable.h>

#import "EAGLView.h"
#import "WindowHandle.h"

#include "../../yg/screen.hpp"
#include "../../yg/texture.hpp"
#include "../../yg/resource_manager.hpp"
#include "../../yg/internal/opengl.hpp"
#include "../../yg/skin.hpp"
#include "../../platform/platform.hpp"
#include "RenderBuffer.hpp"
#include "RenderContext.hpp"

bool _doRepaint = true;
bool _inRepaint = false;

@implementation EAGLView

@synthesize framework;
@synthesize windowHandle;
@synthesize drawer;
@synthesize renderContext;
@synthesize renderBuffer;
@synthesize resourceManager;
@synthesize displayLink;

// You must implement this method
+ (Class)layerClass
{
  return [CAEAGLLayer class];
}

// The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
- (id)initWithCoder:(NSCoder*)coder
{
  if ((self = [super initWithCoder:coder]))
  {
    // Setup Layer Properties
    CAEAGLLayer * eaglLayer = (CAEAGLLayer *)self.layer;

    eaglLayer.opaque = YES;

    renderContext = shared_ptr<iphone::RenderContext>(new iphone::RenderContext());
    
    if (!renderContext.get())
    {
      [self release];
      return nil;
    }
    
    renderContext->makeCurrent();

    // to avoid grid bug on 3G device
    yg::RtFormat fmt = yg::Rt4Bpp;
    if ([[NSString stringWithFormat:@"%s", glGetString(GL_RENDERER)] hasPrefix:@"PowerVR MBX"])
      fmt = yg::Rt8Bpp;
    
    /// ColorFormat : RGB565
    /// Backbuffer : YES, (to prevent from loosing content when mixing with ordinary layers).
    eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithBool:NO],
                                    kEAGLDrawablePropertyRetainedBacking,
                                    kEAGLColorFormatRGB565,
                                    kEAGLDrawablePropertyColorFormat,
                                    nil];

    int etalonW = 320;
    int scrW = etalonW;

    UIDevice * device = [UIDevice currentDevice];

    float ver = [device.systemVersion floatValue];
    NSLog(@"%@", device.systemVersion);
    /// rounding problems
    if (ver >= 3.199)
    {
      UIScreen * mainScr = [UIScreen mainScreen];
      scrW = mainScr.currentMode.size.width;
      if (scrW == 640)
        self.contentScaleFactor = 2.0;
    }

    frameBuffer = shared_ptr<yg::gl::FrameBuffer>(new yg::gl::FrameBuffer());

    int bigVBSize = pow(2, ceil(log2(15000 * sizeof(yg::gl::Vertex))));
    int bigIBSize = pow(2, ceil(log2(30000 * sizeof(unsigned short))));

    int smallVBSize = pow(2, ceil(log2(1500 * sizeof(yg::gl::Vertex))));
    int smallIBSize = pow(2, ceil(log2(3000 * sizeof(unsigned short))));
    
    int blitVBSize = pow(2, ceil(log2(10 * sizeof(yg::gl::AuxVertex))));
    int blitIBSize = pow(2, ceil(log2(10 * sizeof(unsigned short))));
    
    int multiBlitVBSize = pow(2, ceil(log2(300 * sizeof(yg::gl::AuxVertex))));
    int multiBlitIBSize = pow(2, ceil(log2(300 * sizeof(unsigned short))));

    int tinyVBSize = pow(2, ceil(log2(300 * sizeof(yg::gl::AuxVertex))));
    int tinyIBSize = pow(2, ceil(log2(300 * sizeof(unsigned short))));
    
    NSLog(@"Vendor: %s, Renderer: %s", glGetString(GL_VENDOR), glGetString(GL_RENDERER));
    
    Platform & pl = GetPlatform();
    
    size_t dynTexWidth = 512;
    size_t dynTexHeight = 256;
    size_t dynTexCount = 10;
    
    size_t fontTexWidth = 512;
    size_t fontTexHeight = 256;
    size_t fontTexCount = 5;
    
    resourceManager = shared_ptr<yg::ResourceManager>(new yg::ResourceManager(
          bigVBSize, bigIBSize, 6 * GetPlatform().CpuCores(),
          smallVBSize, smallIBSize, 15 * GetPlatform().CpuCores(),
          blitVBSize, blitIBSize, 15 * GetPlatform().CpuCores(),
          dynTexWidth, dynTexHeight, dynTexCount * GetPlatform().CpuCores(),
          fontTexWidth, fontTexHeight, fontTexCount * GetPlatform().CpuCores(),
					"unicode_blocks.txt",
					"fonts_whitelist.txt",
 					"fonts_blacklist.txt",
          2 * 1024 * 1024,
          GetPlatform().CpuCores() + 2,
          fmt,
          !yg::gl::g_isBufferObjectsSupported));
    
    resourceManager->initMultiBlitStorage(multiBlitVBSize, multiBlitIBSize, 10);
    resourceManager->initTinyStorage(tinyVBSize, tinyIBSize, 10);
    
    Platform::FilesList fonts;
    pl.GetFontNames(fonts);
		resourceManager->addFonts(fonts);

		DrawerYG::params_t p;
		p.m_resourceManager = resourceManager;
    p.m_frameBuffer = frameBuffer;
    p.m_glyphCacheID = resourceManager->guiThreadGlyphCacheID();
    p.m_skinName = pl.SkinName();
    p.m_visualScale = pl.VisualScale();
    p.m_isSynchronized = false;
    p.m_useTinyStorage = false; //< use tiny buffers to minimize CPU->GPU data transfer overhead. 

		drawer = shared_ptr<DrawerYG>(new DrawerYG(p));

    windowHandle = shared_ptr<iphone::WindowHandle>(new iphone::WindowHandle(_doRepaint));
    
		windowHandle->setRenderContext(renderContext);
    
    displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawView)];
    displayLink.frameInterval = 1;
    [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
  }

  return self;
}

- (void)onSize:(int)width withHeight:(int)height
{
  framework->OnSize(width, height);
	/// free old video memory
	frameBuffer->resetRenderTarget();
	renderBuffer.reset();

	/// allocate the new one
	renderBuffer = shared_ptr<iphone::RenderBuffer>(new iphone::RenderBuffer(renderContext, (CAEAGLLayer*)self.layer));
	frameBuffer->setRenderTarget(renderBuffer);
  frameBuffer->setDepthBuffer(make_shared_ptr(new yg::gl::RenderBuffer(width, height, true)));
	frameBuffer->onSize(width, height);
	drawer->onSize(width, height);

  drawer->screen()->beginFrame();
  drawer->screen()->clear();
  drawer->screen()->endFrame();
}

- (void)drawView
{
	shared_ptr<PaintEvent> pe(new PaintEvent(drawer.get()));
  if (windowHandle->needRedraw())
  {
    windowHandle->setNeedRedraw(false);
    framework->BeginPaint(pe);
    framework->DoPaint(pe);
    renderBuffer->present();
    framework->EndPaint(pe);
  }
}

- (void)drawViewThunk:(id)obj
{
	[self drawView];
}

- (void)drawViewOnMainThread
{
	[self performSelectorOnMainThread:@selector(drawViewThunk:) withObject:nil waitUntilDone:NO];
}

- (void)layoutSubviews
{
  CGFloat const scale = self.contentScaleFactor;
  CGSize const s = self.bounds.size;
	[self onSize:s.width * scale withHeight:s.height * scale];
}

- (void)dealloc
{
  [displayLink invalidate];
  [displayLink release];
  [EAGLContext setCurrentContext:nil];
  [super dealloc];
}


@end