Guest User

Untitled

a guest
May 27th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #import <AppKit/AppKit.h>
  3.  
  4. #include "Mac/PluginWindowMac.h"
  5. #include "Mac/PluginWindowMacCA.h"
  6. #include "Mac/PluginWindowMacICA.h"
  7.  
  8. #include "SPlayerPluginMac.h"
  9.  
  10.  GLuint LoadTexture( const char * filename, int width, int height );
  11.  
  12. @interface SPCALayer : CALayer {
  13.     GLfloat m_angle;
  14.     GLuint texture;
  15.     int loaded;
  16. }
  17. @end
  18.  
  19. @implementation SPCALayer
  20.  
  21. - (id) init {
  22.     if ([super init]) {
  23.         m_angle = 0;
  24.         loaded = 0;
  25.     }
  26.     return self;
  27. }
  28.  
  29. - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {
  30.    
  31.     CGColorRef bgColor = CGColorCreateGenericRGB(0.952, 0.95, 0.91, 0.9);
  32.     CGContextSetFillColorWithColor(context, bgColor);
  33.     CGContextFillRect(context, layer.bounds);
  34.    
  35.     static const size_t kComponentsPerPixel = 4;
  36.     static const size_t kBitsPerComponent = sizeof(unsigned char) * 8;
  37.    
  38.     NSInteger layerHeight = layer.bounds.size.height;
  39.     NSInteger layerWidth = layer.bounds.size.width;
  40.    
  41.     NSLog(@"\n\nw: %d \t h: %d\n\n", (int)layerWidth, (int)layerHeight);
  42.    
  43.     CGContextSaveGState(context);
  44.    
  45.     CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
  46.    
  47.     size_t bufferLength = layerWidth * layerHeight * kComponentsPerPixel;
  48.    
  49.     unsigned char *buffer = (unsigned char *)malloc(bufferLength);
  50.    
  51.     NSInteger r = random() % 255;
  52.     for (NSInteger i = 0; i < bufferLength; i += 4)
  53.     {
  54.         NSInteger x =  (i / 4) % (NSInteger)layer.bounds.size.width;
  55.         NSInteger y =  (NSInteger)(i / 4) / (NSInteger)layer.bounds.size.width;
  56.         buffer[i]   =  (x % 255) ^ (y % 255) + random() % 16;
  57.         buffer[i+1] =  r;
  58.         buffer[i+2] =  255-r;
  59.         buffer[i+3] =  255;
  60.     }
  61.    
  62.     CGDataProviderRef provider =
  63.     CGDataProviderCreateWithData(NULL, buffer, bufferLength, NULL);
  64.    
  65.     CGImageRef imageRef =
  66.     CGImageCreate(layerWidth, layerHeight, kBitsPerComponent,
  67.                   kBitsPerComponent * kComponentsPerPixel,
  68.                   kComponentsPerPixel * layerWidth,
  69.                   rgb,
  70.                   kCGBitmapByteOrderDefault | kCGImageAlphaLast,
  71.                   provider, NULL, false, kCGRenderingIntentDefault);
  72.    
  73.     CGContextDrawImage(context, layer.bounds, imageRef);
  74.    
  75.     CGImageRelease(imageRef);
  76.    
  77.     [super setNeedsDisplay];
  78.    
  79. }
  80.  
  81. @end
  82.  
  83.  
  84. SPlayerPluginMac::SPlayerPluginMac() : m_layer(NULL) {}
  85.  
  86. SPlayerPluginMac::~SPlayerPluginMac()
  87. {
  88.     if (m_layer) {
  89.         [(CALayer*)m_layer removeFromSuperlayer];
  90.         [(CALayer*)m_layer release];
  91.         m_layer = NULL;
  92.     }
  93. }
  94.  
  95. bool SPlayerPluginMac::onWindowAttached(FB::AttachedEvent* evt, FB::PluginWindowMac* wnd)
  96. {
  97.     if (FB::PluginWindowMac::DrawingModelCoreAnimation == wnd->getDrawingModel() || FB::PluginWindowMac::DrawingModelInvalidatingCoreAnimation == wnd->getDrawingModel()) {
  98.         SPCALayer* layer = [SPCALayer new];
  99.        
  100.         layer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
  101.         layer.needsDisplayOnBoundsChange = YES;
  102.        
  103.         m_layer = layer;
  104.  
  105.         layer.backgroundColor=CGColorCreateGenericRGB(0.5, 0.5, 0.3, 1);
  106.        
  107.         if (FB::PluginWindowMac::DrawingModelInvalidatingCoreAnimation == wnd->getDrawingModel())
  108.             wnd->StartAutoInvalidate(1.0/30.0);
  109.  
  110.         [(CALayer*) wnd->getDrawingPrimitive() addSublayer:layer];
  111.        
  112.         // Draw Label
  113.         CATextLayer* txtlayer = [CATextLayer layer];
  114.         txtlayer.string = (FB::PluginWindowMac::DrawingModelInvalidatingCoreAnimation == wnd->getDrawingModel()) ? @"STREAMER (CA Invalidating) " : [NSString stringWithFormat: @"STREAMER (CA)"  ];
  115.         txtlayer.fontSize = 24;
  116.         txtlayer.foregroundColor = CGColorCreateGenericRGB(0,0,0,1.0);
  117.         txtlayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
  118.         txtlayer.needsDisplayOnBoundsChange = YES;
  119.         [(CALayer*) wnd->getDrawingPrimitive() addSublayer:txtlayer];
  120.         CALayer *customDrawn = [CALayer layer];
  121.  
  122.         customDrawn.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
  123.         customDrawn.needsDisplayOnBoundsChange = YES;
  124.  
  125.         customDrawn.delegate = (id)layer;
  126.  
  127.         customDrawn.masksToBounds = YES;
  128.         [layer addSublayer:customDrawn];
  129.        
  130.         [customDrawn setNeedsDisplay];        
  131.     }
  132.     return SPlayerPlugin::onWindowAttached(evt,wnd);
  133. }
  134.  
  135. bool SPlayerPluginMac::onWindowDetached(FB::DetachedEvent* evt, FB::PluginWindowMac* wnd)
  136. {
  137.     return SPlayerPlugin::onWindowDetached(evt,wnd);
  138. }
Add Comment
Please, Sign In to add comment