createImageBitmap from ImageData makes two copies when premultiplyAlpha = false |
||||||
Issue descriptionAt this moment, when premultiplyAlpha = false, and resize is required, createImageBitmap(ImageData) works as follows: 1. create a SkImage from the ImageData, which makes a copy of the ImageData. The reason for this is that we cannot use SkSurface to put the IMageData in (SkSurface is premultiplied), and the SkImage cannot take ownership of the ImageData. 2. Scale the SkImage, which creates a new copy. Proposed solution: implement a scale function in ImageData class which scales the data directly and produce a copy. Then create a SkImage from that copy directly, and this time SkImage can just take the ownership because the data is a copy.
,
Nov 29 2016
,
Nov 29 2017
This issue has been Available for over a year. If it's no longer important or seems unlikely to be fixed, please consider closing it out. If it is important, please re-triage the issue. Sorry for the inconvenience if the bug really should have been left as Available. If you change it back, also remove the "Hotlist-Recharge-Cold" label. For more details visit https://www.chromium.org/issue-tracking/autotriage - Your friendly Sheriffbot
,
Dec 4 2017
,
Dec 6 2017
,
Dec 6 2017
The real situation is a little more complicated, because we have cropping and resizing together. The current logic uses the following order: - crop (and color convert) over ImageData - create SkImage - down scale the image if needed - flip the image if needed - up scale the image is needed This indeed suffers from a duplicate copy, but I assume the memory will get released as soon as we exit the function and the primary SkImage goes out of scope. I don't think this worths to implement another version of scaling that will be a headache to keep in sync with Skia scale algorithms.
,
Dec 6 2017
There are two other things that we probably can do to improve the situation though. First, SkImage is immutable, so if we create an SkImage from the user buffer, the buffer won't get altered down the way. However, I'm not sure if we can do that without transferring the sole ownership to SkImage. I'll investigate this. A probably better solution is to rewrite the scaling code to use SkPixmap instead of SkImage. I'll investigate this after the first solution since it requires more change in the code.
,
Feb 15 2018
Discussed this with mtklein@ form Skia team. According to mtklein@, "there's no way to make an image that does not own its pixels". We of course can set the release proc to no-op, but in this case we need to guarantee that the buffer outlives the SkImage object. This seems to be not straightforward since, according to mtklein@, "the image will sometimes be ref'd by Skia itself for arbitrary amounts of time, particularly in Ganesh", which will extend the image life span in different scenarios. Flagging this bug as a won't fix.
,
Feb 15 2018
I still think you were on to something in Comment 7, about using SkPixmap routines. SkPixmaps are essentially a thin ownership-agnostic wrapper over a pointer to bytes... you should probably be able to take an input buffer, scale it to the right size (new allocation, obviously), do any other manipulation you need in place, and then only when done pass those pixels off to an SkImage, using SkImage::MakeFromRaster() to transfer ownership to the SkImage. (Or did I misunderstand what you're doing?)
,
Feb 15 2018
This is what I do now. The thing is that I'm creating a copy of the buffer passed by user in https://cs.chromium.org/chromium/src/third_party/WebKit/Source/core/imagebitmap/ImageBitmap.cpp?q=imagebitmap&sq=package:chromium&dr=CSs&l=629. If any of color conversion, scaling, alpha multiplying or flipping is asked by the user for ImageBitmap, then it is mandatory to create a copy. The question is, if none of the above is needed, can we use the passed buffer to create the SkImage that serves as the pixel storage for image bitmap? Looking at the code a little bit more, I think even if Skia allows that, this is still wrong. AFAIK ImageBitmap object is not supposed to change after creation, but it will if we back it by the user buffer and the user decides to change the content of the buffer after creating the ImageBitmap.
,
Feb 15 2018
Ah, ok. Yes, unless you can guarantee the pixels won't change, we have to copy them. |
||||||
►
Sign in to add a comment |
||||||
Comment 1 by bugdroid1@chromium.org
, Aug 5 2016