Re: Getting immutable UIImage data pointer without copy?

2017-11-14 Thread Rick Mann


> On Nov 14, 2017, at 20:18 , Jens Alfke  wrote:
> 
> 
> 
>> On Nov 14, 2017, at 8:11 PM, Rick Mann  wrote:
>> 
>> Maybe, at least for the bonus question. The bigger question is around the 
>> copy.
> 
> Where pixmaps live is a pretty complex issue — ideally they live in the GPU’s 
> address space as textures so they can be blitted efficiently, and that’s 
> often (depending on your hardware) not accessible to the CPU at all. But 
> there’s a lot of logic in CoreGraphics and the underlying graphics engines to 
> shuffle the pixmaps back and forth so software can read and write them.
> 
> Another factor is that the pixmap may be rescaled or translated into 
> different color spaces to optimize rendering it. So there may not exist a 
> default rendering of it at the time you request one, meaning CG will generate 
> a new pixmap for you. (Also, I dunno about UIImage, but NSImage supports 
> image formats like PDF that don’t have a native pixmap representation at all.)
> 
> I’m not super into graphics, but my gut feeling is that, if you care about 
> optimization details like whether pixmaps are being copied, you should really 
> be using a lower-level graphics API than UIImage, which is mostly a 
> cheap-and-cheerful convenience layer for apps that just need to splat a few 
> PNGs into their GUI.

Well, then let me rephrase it as "unnecessary copies." In this case, I 
(currently) need to manipulate the pixels to generate a new image. I will be 
moving this to a Metal 2 filter soon enough, but for pre-iOS 11 users, I'd 
still like to use as little memory as possible (we use a lot of memory in our 
app).


-- 
Rick Mann
rm...@latencyzero.com


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Getting immutable UIImage data pointer without copy?

2017-11-14 Thread Quincey Morris
On Nov 14, 2017, at 20:11 , Rick Mann  wrote:
> 
> The bigger question is around the copy.

In general, the answer must be that a copy ought to be expected. That’s because 
a data provider is not required to have a data buffer, so there’s not 
necessarily any underlying data to access directly. (The actual data could be 
streamed, or generated procedurally.) If you can limit the kinds of image 
sources, you might get a more specific answer.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Getting immutable UIImage data pointer without copy?

2017-11-14 Thread Jens Alfke


> On Nov 14, 2017, at 8:11 PM, Rick Mann  wrote:
> 
> Maybe, at least for the bonus question. The bigger question is around the 
> copy.

Where pixmaps live is a pretty complex issue — ideally they live in the GPU’s 
address space as textures so they can be blitted efficiently, and that’s often 
(depending on your hardware) not accessible to the CPU at all. But there’s a 
lot of logic in CoreGraphics and the underlying graphics engines to shuffle the 
pixmaps back and forth so software can read and write them.

Another factor is that the pixmap may be rescaled or translated into different 
color spaces to optimize rendering it. So there may not exist a default 
rendering of it at the time you request one, meaning CG will generate a new 
pixmap for you. (Also, I dunno about UIImage, but NSImage supports image 
formats like PDF that don’t have a native pixmap representation at all.)

I’m not super into graphics, but my gut feeling is that, if you care about 
optimization details like whether pixmaps are being copied, you should really 
be using a lower-level graphics API than UIImage, which is mostly a 
cheap-and-cheerful convenience layer for apps that just need to splat a few 
PNGs into their GUI.

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Getting immutable UIImage data pointer without copy?

2017-11-14 Thread Rick Mann


> On Nov 14, 2017, at 20:07 , Quincey Morris 
>  wrote:
> 
> On Nov 14, 2017, at 18:36 , Rick Mann  wrote:
>> 
>> Is there a way to get at the underlying raw image data for a given UIImage 
>> (in an immutable buffer) in Swift?
>> 
>> Does this end up making copies? (For bonus points, what's the array magic?)
>> 
>> let img: UIImage = UIImage(named: "MyImage")
>> let data: CFData? = img.CGImage.dataProvider.data
>> let dataArray: [UInt8] = 
> 
> There are some simple answers, but the correct answer is “it depends”. For 
> example, you can do this:
> 
>> var data = Data ([1,2,3,4])
>> print (data [3])
> 
> In other words, seeing a Data instance as an array of bytes is simple. Or, if 
> you want to do something more like the old days in Obj-C, you can do this:
> 
>> data.withUnsafeBytes {
>> (bytes: UnsafePointer) in
>> print (bytes [3])
>> }
> 
> which (in some sense) gives you a raw-ish pointer to the underlying data, 
> inside the closure. (The latter, which a different generic specialization 
> type, is also what you’d use if you wanted to access pairs of bytes as UInt16 
> values, etc.)
> 
> Back to original problem, the following code in a playground works:
> 
>> let img = UIImage(named: "Image”)! // I used a PNG image so the data is 
>> simple
>> let data = img.cgImage!.dataProvider!.data! as Data
>> print (data [0], data [1], data [2], data [3])
> 
> The last part of this is (a) whether you can always get the raw data as 
> bytes, (b) what those bytes represent, and (c) does this kind of approach 
> make a copy? The answer is “I don’t know”, because it’s going to depend on 
> the format of the image and the particular data provider. AFAIK, both the 
> array treatment and the UnsafePointer treatment require a continuous 
> underlying buffer, so if the data provide build the data using multiple 
> partial buffers, I suppose there has to be a copy to meet the API semantics.
> 
> You could also iterate through a Data object using a for loop (and general 
> collection/sequence methods as required). Since that would access only 1 byte 
> at a time, I’d expect there’s no copy involved, but who knows what the 
> performance might be in general.
> 
> Does any of that help?

Maybe, at least for the bonus question. The bigger question is around the copy.


-- 
Rick Mann
rm...@latencyzero.com


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Getting immutable UIImage data pointer without copy?

2017-11-14 Thread Quincey Morris
(resent to the list)

On Nov 14, 2017, at 18:36 , Rick Mann > wrote:
> 
> Is there a way to get at the underlying raw image data for a given UIImage 
> (in an immutable buffer) in Swift?
> 
> Does this end up making copies? (For bonus points, what's the array magic?)
> 
> let img: UIImage = UIImage(named: "MyImage")
> let data: CFData? = img.CGImage.dataProvider.data
> let dataArray: [UInt8] = 

There are some simple answers, but the correct answer is “it depends”. For 
example, you can do this:

> var data = Data ([1,2,3,4])
> print (data [3])

In other words, seeing a Data instance as an array of bytes is simple. Or, if 
you want to do something more like the old days in Obj-C, you can do this:

> data.withUnsafeBytes {
> (bytes: UnsafePointer) in
> print (bytes [3])
> }

which (in some sense) gives you a raw-ish pointer to the underlying data, 
inside the closure. (The latter, which a different generic specialization type, 
is also what you’d use if you wanted to access pairs of bytes as UInt16 values, 
etc.)

Back to original problem, the following code in a playground works:

> let img = UIImage(named: "Image”)! // I used a PNG image so the data is simple
> let data = img.cgImage!.dataProvider!.data! as Data
> print (data [0], data [1], data [2], data [3])

The last part of this is (a) whether you can always get the raw data as bytes, 
(b) what those bytes represent, and (c) does this kind of approach make a copy? 
The answer is “I don’t know”, because it’s going to depend on the format of the 
image and the particular data provider. AFAIK, both the array treatment and the 
UnsafePointer treatment require a continuous underlying buffer, so if the data 
provide build the data using multiple partial buffers, I suppose there has to 
be a copy to meet the API semantics.

You could also iterate through a Data object using a for loop (and general 
collection/sequence methods as required). Since that would access only 1 byte 
at a time, I’d expect there’s no copy involved, but who knows what the 
performance might be in general.

Does any of that help?
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Getting immutable UIImage data pointer without copy?

2017-11-14 Thread Rick Mann
Is there a way to get at the underlying raw image data for a given UIImage (in 
an immutable buffer) in Swift?

Does this end up making copies? (For bonus points, what's the array magic?)

let img: UIImage = UIImage(named: "MyImage")
let data: CFData? = img.CGImage.dataProvider.data
let dataArray: [UInt8] = 

Thanks,

-- 
Rick Mann
rm...@latencyzero.com


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSTimer not firing when computer is inactive for a while.

2017-11-14 Thread Jens Alfke


> On Nov 14, 2017, at 4:43 PM, Nathan Day  wrote:
> 
> The background app is kept alive by launchd as a User Agent and looks for new 
> resources to be downloaded periodically

If this is a launchd agent, you can configure its plist so it gets launched 
periodically. That way it won’t have to keep running all the time; it can just 
poll the network, then quit if there’s nothing to download.

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSTimer not firing when computer is inactive for a while.

2017-11-14 Thread Quincey Morris
On Nov 14, 2017, at 16:57 , Quincey Morris 
 wrote:
> 
> I would suggest you try setting the QoS to at least “utility”, perhaps even 
> “user initiated”.

To clarify: *what* you set to a different QoS depends on the nature of the 
thing you described as a “background process”. For example, if you’re using 
XPC, I think you can change the QoS of the XPC service as a whole. Changing the 
QoS of a particular thread may not be the correct solution.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSTimer not firing when computer is inactive for a while.

2017-11-14 Thread Quincey Morris
On Nov 14, 2017, at 16:43 , Nathan Day  wrote:
> 
> I have a background process that uses an NSTimer that is set to fire every 
> minute, but when the computer is inactive for a while it will stop firing

My guess is that it’s not a problem with the timer, but with the quality of 
service (QoS) of the thread that’s servicing the timer. If this is a background 
process, it may be that the default QoS is “background”, which allows the 
system to defer execution indefinitely. I would suggest you try setting the QoS 
to at least “utility”, perhaps even “user initiated”.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSTimer not firing when computer is inactive for a while.

2017-11-14 Thread Nathan Day

I should make it clear, this is a Mac OS X application.

Nathan Day

On 14 Nov, 2017, at 01:43 PM, Nathan Day  wrote:

I have a background process that uses an NSTimer that is set to fire every 
minute, but when the computer is inactive for a while it will stop firing, its 
not until someone interacts with the computer the timer will start firing 
again. The timer is added to the main NSRunLoop, maybe the NSRunLoop needs to 
be kept active by user interaction, the background app has a status menu bar 
item to provide feedback when its active and provide a simple menu for basic 
user control.

I have checked "Prevent computer from sleeping automatically when the display is off" in 
Energy Saver, I have also tried setting "Turn display off after" to never but it doesn't 
make a difference, these are things we can control on the users machines because it runs on company 
computers.

The background app is kept alive by launchd as a User Agent and looks for new 
resources to be downloaded periodically, it needs to start downloading these 
resources not to long after they are available, they can take hours to download 
in some case, and they need to be available when the user returns to their 
computer. There is also a foreground app that talks to the background app to 
display the downloaded resources, a bit like iTunes and iTunes Helper.

Nathan Day
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/nathan_day%40mac.com

This email sent to nathan_...@mac.com
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


NSTimer not firing when computer is inactive for a while.

2017-11-14 Thread Nathan Day

I have a background process that uses an NSTimer that is set to fire every 
minute, but when the computer is inactive for a while it will stop firing, its 
not until someone interacts with the computer the timer will start firing 
again. The timer is added to the main NSRunLoop, maybe the NSRunLoop needs to 
be kept active by user interaction, the background app has a status menu bar 
item to provide feedback when its active and provide a simple menu for basic 
user control.

I have checked "Prevent computer from sleeping automatically when the display is off" in 
Energy Saver, I have also tried setting "Turn display off after" to never but it doesn't 
make a difference, these are things we can control on the users machines because it runs on company 
computers.

The background app is kept alive by launchd as a User Agent and looks for new 
resources to be downloaded periodically, it needs to start downloading these 
resources not to long after they are available, they can take hours to download 
in some case, and they need to be available when the user returns to their 
computer. There is also a foreground app that talks to the background app to 
display the downloaded resources, a bit like iTunes and iTunes Helper.

Nathan Day
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com