Forum Topic: bitmapData.draw() without lag?

(239 views • 19 replies)

This topic is 1 page long.

<< < > >>
Questioning

cool901

Reply To Post Reply & Quote

Posted at: 9/19/09 01:48 PM

cool901 EVIL LEVEL 01

Sign-Up: 02/27/06

Posts: 116

Ok, I believe I posted this here before and had no luck, but I'm trying again. And it's not just simply drawing to bitmap data once and call it quits. I'm talking about drawing to bitmap data over and over again at atleast 12 fps with pretty much no lag.

I know for a fact that the slow down has nothing to do with my computer, I think flash is doing too much and can't handle it.

Is there anyway to not pressure flash and still get the same result. I looked into copyPixels() but it appears I already need a bitmapData drawn into to use that other function so its a no go.


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 9/19/09 02:11 PM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,418

The function uses the normal Flash renderer to create the image. If it lags at 12 fps, you're obviously drawing too much.

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

cool901

Reply To Post Reply & Quote

Posted at: 9/19/09 02:21 PM

cool901 EVIL LEVEL 01

Sign-Up: 02/27/06

Posts: 116

At 9/19/09 02:11 PM, GustTheASGuy wrote: The function uses the normal Flash renderer to create the image. If it lags at 12 fps, you're obviously drawing too much.

Too put it simply, I wanna draw images at the same rate as a video.


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 9/19/09 02:38 PM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,418

That's great.
You're drawing too much.

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

cool901

Reply To Post Reply & Quote

Posted at: 9/19/09 06:36 PM

cool901 EVIL LEVEL 01

Sign-Up: 02/27/06

Posts: 116

At 9/19/09 02:38 PM, GustTheASGuy wrote: That's great.
You're drawing too much.

And what I'm hinting at is that I am asking how to "draw too much" and not lag?


Sleeping

hdxmike

Reply To Post Reply & Quote

Posted at: 9/19/09 06:47 PM

hdxmike LIGHT LEVEL 09

Sign-Up: 09/11/09

Posts: 1,806

what do you think the term
"too much"
is supposed to mean


None

Paranoia

Reply To Post Reply & Quote

Posted at: 9/19/09 07:12 PM

Paranoia DARK LEVEL 34

Sign-Up: 04/22/05

Posts: 9,697

At 9/19/09 06:47 PM, hdxmike wrote: what do you think the term
"too much"
is supposed to mean

It's fairly self-explainatory.

Too - excessively: to a degree exceeding normal or proper limits; "too big"

Much - frequently or in great quantities: "I don't drink much"; "I don't travel much"

You're doing more than Flash can handle. Either draw a smaller area or eat the lag.

BBS Signature

Sleeping

hdxmike

Reply To Post Reply & Quote

Posted at: 9/19/09 07:35 PM

hdxmike LIGHT LEVEL 09

Sign-Up: 09/11/09

Posts: 1,806

maybe you didnt read that right...


None

knugen

Reply To Post Reply & Quote

Posted at: 9/19/09 07:43 PM

knugen LIGHT LEVEL 35

Sign-Up: 02/07/05

Posts: 4,690

I don't know if AS3 is faster when it comes to bitmaps, but it is in general, so if you're using AS2 you know what to do.

Your code might of course be inefficient, and I believe that is what you want help with to prevent, so show relevant snippets for us and you've got a bigger chance of getting help.


None

cool901

Reply To Post Reply & Quote

Posted at: 9/20/09 09:26 AM

cool901 EVIL LEVEL 01

Sign-Up: 02/27/06

Posts: 116

At 9/19/09 07:43 PM, knugen wrote: Your code might of course be inefficient, and I believe that is what you want help with to prevent, so show relevant snippets for us and you've got a bigger chance of getting help.

Ok, for one... I am using AS3.

And for two, here is all the code.

on sending swf...

import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.media.Camera;
import flash.media.Video;
import flash.utils.ByteArray;
import flash.net.XMLSocket;

import com.dynamicflash.util.Base64;

var snapshot:BitmapData;
var output_vid:Video;
var ba:ByteArray;
var testImgSocket:XMLSocket=new XMLSocket();
testImgSocket.connect("127.0.0.1",8090);
var activeCamera:Camera=Camera.getCamera();
activeCamera.setMode(640,480,12);
output_vid=new Video(160,120);
output_vid.attachCamera(activeCamera);
addChild(output_vid);

var rectangle:Rectangle=new Rectangle(0,0,160,120);

snapshot=new BitmapData(output_vid.width,output_vid.height);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(evt:Event) {
	snapshot.draw(output_vid, new Matrix());
	ba=snapshot.getPixels(rectangle);
    ba.compress();
	var encoded:String=Base64.encodeByteArray(ba);
	testImgSocket.send(";"+encoded);
}

and on receiving swf.

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.utils.ByteArray;
import flash.net.XMLSocket;

import com.dynamicflash.util.Base64;

var dispshotDisplay:Bitmap;
var dispshot:BitmapData;
var testImgSocket:XMLSocket=new XMLSocket();
testImgSocket.connect("127.0.0.1",8090);

dispshot=new BitmapData(160,120);
dispshotDisplay=new Bitmap(dispshot);
dispshotDisplay.x=200;
dispshotDisplay.y=175;
addChild(dispshotDisplay);

var rectangle:Rectangle=new Rectangle(0,0,160,120);

testImgSocket.addEventListener(DataEvent.DATA, onIncomingData);

function onIncomingData(event:DataEvent):void {
	var decoded:ByteArray=Base64.decodeToByteArray(event.data);
	decoded.uncompress();
	dispshot.setPixels(rectangle,decoded);
}

None

henke37

Reply To Post Reply & Quote

Posted at: 9/20/09 10:53 AM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,647

Uhm, why the custom, crap protocol? Why are you not using the built in fms protocol? It got serious codecs that are not incredibly naive about the data. They know how to compress video properly. Without the need to encode the bits using base 64...

And most importantly, they are not bogged down by running as actionscript, but they are run as compiled native code. And that's the worst case scenario, if you are lucky, it's done with hand optimized assembly instead.

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


None

LeechmasterB

Reply To Post Reply & Quote

Posted at: 9/20/09 11:25 AM

LeechmasterB EVIL LEVEL 16

Sign-Up: 04/01/05

Posts: 938

function onEnterFrame(evt:Event) {
	snapshot.draw(output_vid, new Matrix());
	<strong>ba=snapshot.getPixels(rectangle);</strong>
       <strong>ba.compress();</strong>
	<strong>var encoded:String=Base64.encodeByteArray(ba);</strong>
	testImgSocket.send(";"+encoded);
}

That part is surely rather the slowdown then the simple draw abobe... there s your performance leak ^^


None

cool901

Reply To Post Reply & Quote

Posted at: 9/20/09 03:24 PM

cool901 EVIL LEVEL 01

Sign-Up: 02/27/06

Posts: 116

At 9/20/09 10:53 AM, henke37 wrote: Uhm, why the custom, crap protocol? Why are you not using the built in fms protocol? It got serious codecs that are not incredibly naive about the data. They know how to compress video properly. Without the need to encode the bits using base 64...

And most importantly, they are not bogged down by running as actionscript, but they are run as compiled native code. And that's the worst case scenario, if you are lucky, it's done with hand optimized assembly instead.

Sorry to say this, but I don't understand what your saying really. And also, I don't really want to use FMS at all. And why should I have to? I wanna use my own way that I will understand completely all every time I use it because I made it.


None

cool901

Reply To Post Reply & Quote

Posted at: 9/20/09 03:28 PM

cool901 EVIL LEVEL 01

Sign-Up: 02/27/06

Posts: 116

At 9/20/09 11:25 AM, LeechmasterB wrote: function onEnterFrame(evt:Event) {
snapshot.draw(output_vid, new Matrix());
ba=snapshot.getPixels(rectangle);
ba.compress();
var encoded:String=Base64.encodeByteArray(ba );
testImgSocket.send(";"+encoded);
}

That part is surely rather the slowdown then the simple draw abobe... there s your performance leak ^^

Well I wouldn't be too sure of that because even when I just do the webcam and draw it without using anything else it still lags.

Is there a better drawing method that doesn't use a lot of the flash renderer?

And I am writing it to a byteArray and encoding it that way I can send it over an xmlSocket Server. The idea is to make a webcam chat system. And I absolutely do not intend to use FMS.


None

LeechmasterB

Reply To Post Reply & Quote

Posted at: 9/20/09 03:43 PM

LeechmasterB EVIL LEVEL 16

Sign-Up: 04/01/05

Posts: 938

I can draw an 800x600 area using filters and still get 50 fps or so, so there s no way its caused by the drawing alone. Getting all pixels of an image and processing each invididually is really slow and overkill in flash though.

And no flash does not have hardware acceleration, so you are stuck with the flash renderer.

Last but not least for sending video over the net there are flash.net.NetStream and stuff already there to use, which surely is faster then anything you custom made for that purpose.

If its too slow you have not much of a choice...


None

cool901

Reply To Post Reply & Quote

Posted at: 9/20/09 03:48 PM

cool901 EVIL LEVEL 01

Sign-Up: 02/27/06

Posts: 116

At 9/20/09 03:43 PM, LeechmasterB wrote: I can draw an 800x600 area using filters and still get 50 fps or so, so there s no way its caused by the drawing alone. Getting all pixels of an image and processing each invididually is really slow and overkill in flash though.

And no flash does not have hardware acceleration, so you are stuck with the flash renderer.

Last but not least for sending video over the net there are flash.net.NetStream and stuff already there to use, which surely is faster then anything you custom made for that purpose.

If its too slow you have not much of a choice...

Hmm... Do you need anything on your computer to use the Flash.net.NetStream class? I have heard of that numerous times but do I need a certain thing other than flash to make use of it?

And I wasn't asking for something that stops it from using the flash renderer. I was asking if there was a better drawing method than the one I am using.

Also... You said it was still going fast, were you running the webcam object at the same time, and were you continuously drawing over and over?


None

henke37

Reply To Post Reply & Quote

Posted at: 9/21/09 01:15 AM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,647

At 9/20/09 03:28 PM, cool901 wrote: And I am writing it to a byteArray and encoding it that way I can send it over an xmlSocket Server. The idea is to make a webcam chat system. And I absolutely do not intend to use FMS.

Uhm, xmlSocket is a horrible protocol for the job. It is text based and provides little to none bandwidth optimization of the video.
The FMS protocol simply is the only serious solution. Anything that you can think up simply will not be adequate. For example, you are using base64 encoding for no good reason, wasting both bandwidth and time.

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 9/21/09 02:43 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,418

To put it simply, the source of lag is indeed not 'draw' but the way you're communicating the data stream. It doesn't matter if you understand it this way, it doesn't work. And you're still using components that you don't understand, such as the compression.

The completely needless base 64 encoding is probably most of it, the compression is also needless.

But you should see if you can connect NetStream through localhost, like was said.

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

cool901

Reply To Post Reply & Quote

Posted at: 9/21/09 06:12 PM

cool901 EVIL LEVEL 01

Sign-Up: 02/27/06

Posts: 116

At 9/21/09 02:43 AM, GustTheASGuy wrote:
The completely needless base 64 encoding is probably most of it, the compression is also needless.

But you should see if you can connect NetStream through localhost, like was said.

Well I wasn't even thinking about compression when I started using base64. If I remember correctly I couldn't send over objects and other things directly or w/e and I needed to have it as a string before sending it.

basically

xmlSocket.send(object/bitmapData/byteArr ay); caused an error

and

xmlSocket.send(string); = well it sent it just fine.


None

henke37

Reply To Post Reply & Quote

Posted at: 9/22/09 12:19 PM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,647

Use the Socket class instead, it has this neat writeBytes method. Or rather, don't and use the FMS protocol instead. It's the only serious solution for webcamera chats. Well, it's the best that we have.

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


All times are Eastern Standard Time (GMT -5) | Current Time: 09:24 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!