1 #import "SkSampleUIView.h"
3 #define SKGL_CONFIG kEAGLColorFormatRGB565
4 //#define SKGL_CONFIG kEAGLColorFormatRGBA8
10 #include "SkSurface.h"
11 #include "SampleApp.h"
17 #include "gl/GrGLInterface.h"
18 #include "GrContext.h"
19 #include "SkGpuDevice.h"
22 class SkiOSDeviceManager : public SampleWindow::DeviceManager {
24 SkiOSDeviceManager(GLint layerFBO) {
28 fCurRenderTarget = NULL;
32 fBackend = SkOSWindow::kNone_BackEndType;
35 virtual ~SkiOSDeviceManager() {
37 SkSafeUnref(fCurContext);
38 SkSafeUnref(fCurIntf);
39 SkSafeUnref(fCurRenderTarget);
43 void setUpBackend(SampleWindow* win, int msaaSampleCount) SK_OVERRIDE {
44 SkASSERT(SkOSWindow::kNone_BackEndType == fBackend);
46 fBackend = SkOSWindow::kNone_BackEndType;
49 switch (win->getDeviceType()) {
50 // these two don't use GL
51 case SampleWindow::kRaster_DeviceType:
52 case SampleWindow::kPicture_DeviceType:
54 // these guys use the native backend
55 case SampleWindow::kGPU_DeviceType:
56 fBackend = SkOSWindow::kNativeGL_BackEndType;
62 SkOSWindow::AttachmentInfo info;
63 bool result = win->attach(fBackend, msaaSampleCount, &info);
65 SkDebugf("Failed to initialize GL");
68 fMSAASampleCount = msaaSampleCount;
70 SkASSERT(NULL == fCurIntf);
71 switch (win->getDeviceType()) {
72 // these two don't use GL
73 case SampleWindow::kRaster_DeviceType:
74 case SampleWindow::kPicture_DeviceType:
77 case SampleWindow::kGPU_DeviceType:
78 fCurIntf = GrGLCreateNativeInterface();
85 SkASSERT(NULL == fCurContext);
86 if (SkOSWindow::kNone_BackEndType != fBackend) {
87 fCurContext = GrContext::Create(kOpenGL_GrBackend,
88 (GrBackendContext) fCurIntf);
91 if ((NULL == fCurContext || NULL == fCurIntf) &&
92 SkOSWindow::kNone_BackEndType != fBackend) {
93 // We need some context and interface to see results if we're using a GL backend
94 SkSafeUnref(fCurContext);
95 SkSafeUnref(fCurIntf);
96 SkDebugf("Failed to setup 3D");
99 #endif // SK_SUPPORT_GPU
100 // call windowSizeChanged to create the render target
101 this->windowSizeChanged(win);
104 void tearDownBackend(SampleWindow *win) SK_OVERRIDE {
106 SkSafeUnref(fCurContext);
109 SkSafeUnref(fCurIntf);
112 SkSafeUnref(fCurRenderTarget);
113 fCurRenderTarget = NULL;
116 fBackend = SampleWindow::kNone_BackEndType;
119 SkSurface* createSurface(SampleWindow::DeviceType dType, SampleWindow* win) SK_OVERRIDE{
121 if (SampleWindow::IsGpuDeviceType(dType) && fCurContext) {
122 SkSurfaceProps props(win->getSurfaceProps());
123 return SkSurface::NewRenderTargetDirect(fCurRenderTarget, &props);
129 virtual void publishCanvas(SampleWindow::DeviceType dType,
131 SampleWindow* win) SK_OVERRIDE {
133 if (NULL != fCurContext) {
134 fCurContext->flush();
140 void windowSizeChanged(SampleWindow* win) SK_OVERRIDE {
142 if (NULL != fCurContext) {
143 SkOSWindow::AttachmentInfo info;
145 win->attach(fBackend, fMSAASampleCount, &info);
147 glBindFramebuffer(GL_FRAMEBUFFER, fLayerFBO);
148 GrBackendRenderTargetDesc desc;
149 desc.fWidth = SkScalarRoundToInt(win->width());
150 desc.fHeight = SkScalarRoundToInt(win->height());
151 desc.fConfig = kSkia8888_GrPixelConfig;
152 desc.fRenderTargetHandle = fLayerFBO;
153 desc.fSampleCnt = info.fSampleCount;
154 desc.fStencilBits = info.fStencilBits;
156 SkSafeUnref(fCurRenderTarget);
157 fCurRenderTarget = fCurContext->wrapBackendRenderTarget(desc);
162 GrContext* getGrContext() SK_OVERRIDE {
170 GrRenderTarget* getGrRenderTarget() SK_OVERRIDE {
172 return fCurRenderTarget;
178 bool isUsingGL() const { return SkOSWindow::kNone_BackEndType != fBackend; }
183 GrContext* fCurContext;
184 const GrGLInterface* fCurIntf;
185 GrRenderTarget* fCurRenderTarget;
186 int fMSAASampleCount;
190 SkOSWindow::SkBackEndTypes fBackend;
192 typedef SampleWindow::DeviceManager INHERITED;
195 ////////////////////////////////////////////////////////////////////////////////
196 @implementation SkSampleUIView
198 @synthesize fTitle, fRasterLayer, fGLLayer;
200 #include "SkApplication.h"
202 #include "SkWindow.h"
205 static const int FRAME_COUNT = 60;
207 CFTimeInterval fNow0, fNow1;
208 CFTimeInterval fTime0, fTime1, fTotalTime;
212 fTime0 = fTime1 = fTotalTime = 0;
217 fNow0 = CACurrentMediaTime();
221 fNow1 = CACurrentMediaTime();
224 void flush(SkOSWindow* hwnd) {
225 CFTimeInterval now2 = CACurrentMediaTime();
227 fTime0 += fNow1 - fNow0;
228 fTime1 += now2 - fNow1;
230 if (++fFrameCounter == FRAME_COUNT) {
231 CFTimeInterval totalNow = CACurrentMediaTime();
232 fTotalTime = totalNow - fTotalTime;
234 //SkMSec ms0 = (int)(1000 * fTime0 / FRAME_COUNT);
235 //SkMSec msTotal = (int)(1000 * fTotalTime / FRAME_COUNT);
236 //str.printf(" ms: %d [%d], fps: %3.1f", msTotal, ms0,
237 // FRAME_COUNT / fTotalTime);
238 str.printf(" fps:%3.1f", FRAME_COUNT / fTotalTime);
239 hwnd->setTitle(NULL);
240 fTotalTime = totalNow;
247 static FPSState gFPS;
249 #define FPS_StartDraw() gFPS.startDraw()
250 #define FPS_EndDraw() gFPS.endDraw()
251 #define FPS_Flush(wind) gFPS.flush(wind)
253 ///////////////////////////////////////////////////////////////////////////////
255 - (id)initWithDefaults {
256 if (self = [super initWithDefaults]) {
257 fRedrawRequestPending = false;
258 fFPSState = new FPSState;
261 fGL.fContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
263 fGL.fContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
266 if (!fGL.fContext || ![EAGLContext setCurrentContext:fGL.fContext])
272 // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
273 glGenFramebuffers(1, &fGL.fFramebuffer);
274 glBindFramebuffer(GL_FRAMEBUFFER, fGL.fFramebuffer);
276 glGenRenderbuffers(1, &fGL.fRenderbuffer);
277 glGenRenderbuffers(1, &fGL.fStencilbuffer);
279 glBindRenderbuffer(GL_RENDERBUFFER, fGL.fRenderbuffer);
280 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, fGL.fRenderbuffer);
282 glBindRenderbuffer(GL_RENDERBUFFER, fGL.fStencilbuffer);
283 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fGL.fStencilbuffer);
285 self.fGLLayer = [CAEAGLLayer layer];
286 fGLLayer.bounds = self.bounds;
287 fGLLayer.anchorPoint = CGPointMake(0, 0);
288 fGLLayer.opaque = TRUE;
289 [self.layer addSublayer:fGLLayer];
290 fGLLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
291 [NSNumber numberWithBool:NO],
292 kEAGLDrawablePropertyRetainedBacking,
294 kEAGLDrawablePropertyColorFormat,
297 self.fRasterLayer = [CALayer layer];
298 fRasterLayer.anchorPoint = CGPointMake(0, 0);
299 fRasterLayer.opaque = TRUE;
300 [self.layer addSublayer:fRasterLayer];
302 NSMutableDictionary *newActions = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSNull null], @"onOrderIn",
303 [NSNull null], @"onOrderOut",
304 [NSNull null], @"sublayers",
305 [NSNull null], @"contents",
306 [NSNull null], @"bounds",
308 fGLLayer.actions = newActions;
309 fRasterLayer.actions = newActions;
310 [newActions release];
312 fDevManager = new SkiOSDeviceManager(fGL.fFramebuffer);
313 static char* kDummyArgv = const_cast<char*>("dummyExecutableName");
314 fWind = new SampleWindow(self, 1, &kDummyArgv, fDevManager);
316 fWind->resize(self.frame.size.width, self.frame.size.height,
325 self.fRasterLayer = nil;
327 [fGL.fContext release];
331 - (void)layoutSubviews {
334 // Allocate color buffer backing based on the current layer size
335 glBindRenderbuffer(GL_RENDERBUFFER, fGL.fRenderbuffer);
336 [fGL.fContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:fGLLayer];
338 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &fGL.fWidth);
339 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &fGL.fHeight);
341 glBindRenderbuffer(GL_RENDERBUFFER, fGL.fStencilbuffer);
342 glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, fGL.fWidth, fGL.fHeight);
344 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
345 NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
348 if (fDevManager->isUsingGL()) {
351 CGRect rect = CGRectMake(0, 0, W, H);
352 fGLLayer.bounds = rect;
355 CGRect rect = self.bounds;
356 W = (int)CGRectGetWidth(rect);
357 H = (int)CGRectGetHeight(rect);
358 fRasterLayer.bounds = rect;
361 printf("---- layoutSubviews %d %d\n", W, H);
366 ///////////////////////////////////////////////////////////////////////////////
368 - (void)drawWithCanvas:(SkCanvas*)canvas {
369 fRedrawRequestPending = false;
370 fFPSState->startDraw();
372 fFPSState->endDraw();
376 fFPSState->flush(fWind);
380 // This application only creates a single context which is already set current at this point.
381 // This call is redundant, but needed if dealing with multiple contexts.
382 [EAGLContext setCurrentContext:fGL.fContext];
384 // This application only creates a single default framebuffer which is already bound at this point.
385 // This call is redundant, but needed if dealing with multiple framebuffers.
386 glBindFramebuffer(GL_FRAMEBUFFER, fGL.fFramebuffer);
389 glGetIntegerv(GL_SCISSOR_TEST, &scissorEnable);
390 glDisable(GL_SCISSOR_TEST);
391 glClearColor(0,0,0,0);
392 glClear(GL_COLOR_BUFFER_BIT);
394 glEnable(GL_SCISSOR_TEST);
396 glViewport(0, 0, fGL.fWidth, fGL.fHeight);
399 SkAutoTUnref<SkSurface> surface(fWind->createSurface());
400 SkCanvas* canvas = surface->getCanvas();
402 // if we're not "retained", then we have to always redraw everything.
403 // This call forces us to ignore the fDirtyRgn, and draw everywhere.
404 // If we are "retained", we can skip this call (as the raster case does)
405 fWind->forceInvalAll();
407 [self drawWithCanvas:canvas];
409 // This application only creates a single color renderbuffer which is already bound at this point.
410 // This call is redundant, but needed if dealing with multiple renderbuffers.
411 glBindRenderbuffer(GL_RENDERBUFFER, fGL.fRenderbuffer);
412 [fGL.fContext presentRenderbuffer:GL_RENDERBUFFER];
415 - (void)drawInRaster {
416 SkAutoTUnref<SkSurface> surface(fWind->createSurface());
417 SkCanvas* canvas = surface->getCanvas();
418 [self drawWithCanvas:canvas];
419 CGImageRef cgimage = SkCreateCGImageRef(fWind->getBitmap());
420 fRasterLayer.contents = (id)cgimage;
421 CGImageRelease(cgimage);
424 - (void)forceRedraw {
425 if (fDevManager->isUsingGL())
431 ///////////////////////////////////////////////////////////////////////////////
433 - (void)setSkTitle:(const char *)title {
434 NSString* text = [NSString stringWithUTF8String:title];
435 if ([text length] > 0)
438 if (fTitleItem && fTitle) {
439 fTitleItem.title = [NSString stringWithFormat:@"%@%@", fTitle,
440 [NSString stringWithUTF8String:fFPSState->str.c_str()]];
444 - (void)postInvalWithRect:(const SkIRect*)r {
445 if (!fRedrawRequestPending) {
446 fRedrawRequestPending = true;
447 bool gl = fDevManager->isUsingGL();
448 [CATransaction begin];
449 [CATransaction setAnimationDuration:0];
450 fRasterLayer.hidden = gl;
451 fGLLayer.hidden = !gl;
452 [CATransaction commit];
454 [self performSelector:@selector(drawInGL) withObject:nil afterDelay:0];
457 [self performSelector:@selector(drawInRaster) withObject:nil afterDelay:0];
458 [self setNeedsDisplay];
463 - (void)getAttachmentInfo:(SkOSWindow::AttachmentInfo*)info {
464 glBindRenderbuffer(GL_RENDERBUFFER, fGL.fRenderbuffer);
465 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
466 GL_RENDERBUFFER_STENCIL_SIZE,
467 &info->fStencilBits);
468 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
469 GL_RENDERBUFFER_SAMPLES_APPLE,
470 &info->fSampleCount);