ZoulouX

AIR RenderMode GPU Stress Test

May 9th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package fr.babos.test.perfs
  2. {
  3.     import com.greensock.easing.Back;
  4.     import com.greensock.TweenMax;
  5.     import flash.display.Bitmap;
  6.     import flash.display.BitmapData;
  7.     import flash.display.DisplayObject;
  8.     import flash.display.Loader;
  9.     import flash.display.LoaderInfo;
  10.     import flash.display.PixelSnapping;
  11.     import flash.display.Shape;
  12.     import flash.display.Sprite;
  13.     import flash.display.StageAlign;
  14.     import flash.display.StageQuality;
  15.     import flash.display.StageScaleMode;
  16.     import flash.events.Event;
  17.     import flash.events.IOErrorEvent;
  18.     import flash.geom.Matrix;
  19.     import flash.geom.Point;
  20.     import flash.net.URLLoader;
  21.     import flash.net.URLRequest;
  22.     import flash.system.ImageDecodingPolicy;
  23.     import flash.system.LoaderContext;
  24.     import flash.ui.Multitouch;
  25.     import flash.ui.MultitouchInputMode;
  26.     import flash.utils.Dictionary;
  27.     import fr.babos.touch.emulator.MouseToTouchEmulator;
  28.     import net.hires.debug.Stats;
  29.    
  30.     /**
  31.      * ...
  32.      * @author ZoulouX
  33.      */
  34.     public class PerfsTest extends Sprite
  35.     {
  36.         protected var _twitterData:Object;
  37.        
  38.         protected var _imagesURLs       :Vector.<String>            = new Vector.<String>;
  39.         protected var _imagesData       :Vector.<BitmapData>        = new Vector.<BitmapData>;
  40.         protected var _images           :Vector.<DisplayObject>     = new Vector.<DisplayObject>;
  41.        
  42.         protected var _directions       :Dictionary                 = new Dictionary();
  43.        
  44.         public function PerfsTest()
  45.         {
  46.             if (stage == null)
  47.                 addEventListener(Event.ADDED_TO_STAGE, init);
  48.             else
  49.                 init();
  50.         }
  51.        
  52.         private function init (e:Event = null):void
  53.         {
  54.             removeEventListener(Event.ADDED_TO_STAGE, init);
  55.            
  56.             stage.align = StageAlign.TOP_LEFT;
  57.             stage.scaleMode = StageScaleMode.NO_SCALE;
  58.            
  59.             stage.frameRate = 60;
  60.            
  61.             stage.quality = StageQuality.LOW;
  62.            
  63.             addChild(new Stats());
  64.            
  65.             if (Multitouch.supportsTouchEvents)
  66.             {
  67.                 Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
  68.             }
  69.             else
  70.             {
  71.                 MouseToTouchEmulator.emulate(stage, true, true, false);
  72.             }
  73.            
  74.            
  75.             //var serviceURL:String = "http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=zouloux&count=100";
  76.             var serviceURL:String = "http://search.twitter.com/search.json?q=flash&rpp=300&include_entities=true&result_type=mixed";
  77.            
  78.             var urlLoader:URLLoader = new URLLoader(new URLRequest(serviceURL));
  79.             urlLoader.addEventListener(Event.COMPLETE, dataLoadedHandler);
  80.             trace("loading");
  81.         }
  82.        
  83.         public function dataLoadedHandler (event:Event):void
  84.         {
  85.             (event.currentTarget as URLLoader).removeEventListener(Event.COMPLETE, dataLoadedHandler);
  86.            
  87.             trace("loaded");
  88.            
  89.             _twitterData = JSON.parse((event.currentTarget as URLLoader).data);
  90.            
  91.             if (_twitterData["results"] != null)
  92.                 _twitterData = _twitterData.results;
  93.            
  94.             const total:uint = _twitterData.length;
  95.            
  96.             for (var i:uint = 0; i < total; i ++)
  97.             {
  98.                 var imageURL:String;
  99.                
  100.                 if (_twitterData[i].profile_image_url != null)
  101.                     imageURL = _twitterData[i].profile_image_url;
  102.                 else if (_twitterData[i].user != null)
  103.                     imageURL = _twitterData[i].user.profile_image_url;
  104.                
  105.                 //trace(imageURL);
  106.                
  107.                 _imagesURLs.push(imageURL);
  108.             }
  109.            
  110.             // Commencer par charger la première image
  111.             loadImage();
  112.             loadImage();
  113.             loadImage();
  114.            
  115.             addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  116.         }
  117.        
  118.         protected function loadImage ():void
  119.         {
  120.             if (_imagesURLs.length > 0)
  121.             {
  122.                 var loader:Loader = new Loader();
  123.                 var context:LoaderContext = new LoaderContext();
  124.                 context.allowCodeImport = false;
  125.                 context.imageDecodingPolicy = ImageDecodingPolicy.ON_LOAD;
  126.                
  127.                 loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoadedHandler);
  128.                 loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
  129.                 loader.contentLoaderInfo.addEventListener(IOErrorEvent.DISK_ERROR, errorHandler);
  130.                 loader.contentLoaderInfo.addEventListener(IOErrorEvent.NETWORK_ERROR, errorHandler);
  131.                 loader.contentLoaderInfo.addEventListener(IOErrorEvent.VERIFY_ERROR, errorHandler);
  132.                
  133.                 loader.load(new URLRequest(_imagesURLs.shift()), context);
  134.             }
  135.         }
  136.        
  137.         protected function errorHandler (event:IOErrorEvent):void
  138.         {
  139.             //trace(event.text);
  140.            
  141.             loadImage();
  142.         }
  143.        
  144.         protected function imageLoadedHandler (event:Event):void
  145.         {
  146.             //trace("image loaded");
  147.            
  148.             var bitmapData:BitmapData = ((event.currentTarget as LoaderInfo).content as Bitmap).bitmapData;
  149.            
  150.             _imagesData.push(bitmapData);
  151.            
  152.             createImage(bitmapData);
  153.            
  154.             loadImage();
  155.         }
  156.        
  157.         /*
  158.         // 30 FPS
  159.         private function createImage (bitmapData:BitmapData):void
  160.         {
  161.             var shape:Shape = new Shape();
  162.            
  163.             var point1:Point = new Point(0, 0);
  164.             var point2:Point = new Point(bitmapData.width, 0);
  165.             var point3:Point = new Point(0, bitmapData.height);
  166.             var point4:Point = new Point(bitmapData.width, bitmapData.height);
  167.            
  168.             var uvPoint1:Point = new Point(0, 0);
  169.             var uvPoint2:Point = new Point(1, 0);
  170.             var uvPoint3:Point = new Point(0, 1);
  171.             var uvPoint4:Point = new Point(1, 1);
  172.            
  173.             var verticies:Vector.<Number> = Vector.<Number>([point1.x, point1.y, point2.x, point2.y, point3.x, point3.y, point4.x, point4.y]);
  174.             var indices:Vector.<int> = Vector.<int>([0, 1, 2, 1, 3, 2]);
  175.             var uvtData:Vector.<Number> = Vector.<Number>([uvPoint1.x, uvPoint1.y, uvPoint2.x, uvPoint2.y, uvPoint3.x, uvPoint3.y, uvPoint4.x, uvPoint4.y]);
  176.            
  177.             shape.graphics.beginBitmapFill(bitmapData, new Matrix(), false, true);
  178.             shape.graphics.drawTriangles(verticies, indices, uvtData);
  179.             shape.graphics.endFill();
  180.            
  181.             //shape.cacheAsBitmap = true;
  182.             //shape.cacheAsBitmapMatrix = new Matrix();
  183.            
  184.             place(shape);
  185.         }
  186.         */
  187.        
  188.         /*
  189.         // 28 FPS
  190.         private function createImage (bitmapData:BitmapData):void
  191.         {
  192.             var shape:Shape = new Shape();
  193.            
  194.             shape.graphics.beginBitmapFill(bitmapData, new Matrix(), false, true);
  195.             shape.graphics.drawRect(0, 0, bitmapData.width, bitmapData.height);
  196.             shape.graphics.endFill();
  197.            
  198.             //shape.cacheAsBitmap = true;
  199.             //shape.cacheAsBitmapMatrix = new Matrix();
  200.            
  201.             place(shape);
  202.         }
  203.         */
  204.        
  205.        
  206.        
  207.         // 35 FPS
  208.         private function createImage (bitmapData:BitmapData):void
  209.         {
  210.             //var bitmap:Bitmap = new Bitmap(bitmapData, PixelSnapping.NEVER, true);
  211.            
  212.             // + 1 FPS (no smooth)
  213.             var bitmap:Bitmap = new Bitmap(bitmapData, PixelSnapping.NEVER, false);
  214.            
  215.             // -10 FPS :-/
  216.             //bitmap.opaqueBackground = true;
  217.            
  218.             // -2 FPS
  219.             //bitmap.cacheAsBitmap = true;
  220.             //bitmap.cacheAsBitmapMatrix = new Matrix();
  221.            
  222.             // -20 FPS !
  223.             //bitmap.z = 0;
  224.            
  225.             place(bitmap);
  226.         }
  227.        
  228.         private function place (pDisplayObject:DisplayObject):void
  229.         {
  230.             _images.push(pDisplayObject);
  231.            
  232.             _directions[pDisplayObject] = {
  233.                 x: Math.random() * 2 - 1,
  234.                 y: Math.random() * 2 - 1,
  235.                 rotation: Math.random() * 6 - 3
  236.             };
  237.            
  238.             pDisplayObject.scaleX = pDisplayObject.scaleY = 1 + Math.random();
  239.            
  240.             pDisplayObject.x = (stage.stageWidth - pDisplayObject.width) * Math.random();
  241.             pDisplayObject.y = (stage.stageHeight - pDisplayObject.height) * Math.random();
  242.            
  243.             TweenMax.from(pDisplayObject, .5, {
  244.                 alpha: 0
  245.             });
  246.             TweenMax.from(pDisplayObject, .7, {
  247.                 scaleX: 0,
  248.                 scaleY: 0,
  249.                 ease: Back.easeOut
  250.             });
  251.            
  252.             addChildAt(pDisplayObject, 0);
  253.         }
  254.        
  255.         protected function enterFrameHandler (event:Event):void
  256.         {
  257.             var total:uint = _images.length;
  258.             var image:DisplayObject;
  259.            
  260.             for (var i:int = 0; i < total; i++)
  261.             {
  262.                 image = _images[i];
  263.                
  264.                 if (image.x < 0)
  265.                     _directions[image].x = Math.abs(_directions[image].x);
  266.                 else if (image.x > stage.stageWidth - image.width)
  267.                     _directions[image].x = - Math.abs(_directions[image].x);
  268.                    
  269.                 if (image.y < 0)
  270.                     _directions[image].y = Math.abs(_directions[image].y);
  271.                 else if (image.y > stage.stageHeight - image.height)
  272.                     _directions[image].y = - Math.abs(_directions[image].y);
  273.                
  274.                 _images[i].x += _directions[image].x;
  275.                 _images[i].y += _directions[image].y;
  276.                 //_images[i].rotation += _directions[image].rotation;
  277.             }
  278.         }
  279.     }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment