Bug 1781129 - Part 2: Use BigBuffer for ShmemImage, r=edgar

The ShmemImage type was previously implemented using a Shmem, however due to
the usage patterns, `BigBuffer` is probably a better fit, and allows unifying
more code in nsContentUtils.

Differential Revision: https://phabricator.services.mozilla.com/D151853
This commit is contained in:
Nika Layzell 2022-08-26 16:08:05 +00:00
parent 91a521bf3a
commit ff386ec755
6 changed files with 25 additions and 28 deletions

View File

@ -7942,10 +7942,10 @@ nsresult nsContentUtils::CalculateBufferSizeForImage(
return NS_OK;
}
static already_AddRefed<DataSourceSurface> ShmemToDataSurface(
Shmem& aData, uint32_t aStride, const IntSize& aImageSize,
static already_AddRefed<DataSourceSurface> BigBufferToDataSurface(
BigBuffer& aData, uint32_t aStride, const IntSize& aImageSize,
SurfaceFormat aFormat) {
if (!aData.IsReadable() || !aImageSize.width || !aImageSize.height) {
if (!aData.Size() || !aImageSize.width || !aImageSize.height) {
return nullptr;
}
@ -7956,11 +7956,11 @@ static already_AddRefed<DataSourceSurface> ShmemToDataSurface(
aStride, aImageSize, aFormat, &maxBufLen, &imageBufLen))) {
return nullptr;
}
if (imageBufLen > aData.Size<uint8_t>()) {
if (imageBufLen > aData.Size()) {
return nullptr;
}
return CreateDataSourceSurfaceFromData(aImageSize, aFormat,
aData.get<uint8_t>(), aStride);
return CreateDataSourceSurfaceFromData(aImageSize, aFormat, aData.Data(),
aStride);
}
nsresult nsContentUtils::DeserializeDataTransferImageContainer(
@ -8307,24 +8307,21 @@ Maybe<Shmem> nsContentUtils::GetSurfaceData(DataSourceSurface& aSurface,
GetSurfaceDataShmem(aAllocator));
}
Maybe<ShmemImage> nsContentUtils::SurfaceToIPCImage(
DataSourceSurface& aSurface, IShmemAllocator* aAllocator) {
Maybe<IPCImage> nsContentUtils::SurfaceToIPCImage(DataSourceSurface& aSurface) {
size_t len = 0;
int32_t stride = 0;
auto mem = GetSurfaceData(aSurface, &len, &stride, aAllocator);
auto mem = GetSurfaceData(aSurface, &len, &stride);
if (!mem) {
return Nothing();
}
return Some(ShmemImage{*mem, uint32_t(stride), aSurface.GetFormat(),
ImageIntSize::FromUnknownSize(aSurface.GetSize())});
return Some(IPCImage{std::move(*mem), uint32_t(stride), aSurface.GetFormat(),
ImageIntSize::FromUnknownSize(aSurface.GetSize())});
}
already_AddRefed<DataSourceSurface> nsContentUtils::IPCImageToSurface(
ShmemImage&& aImage, IShmemAllocator* aAllocator) {
auto release =
MakeScopeExit([&] { aAllocator->DeallocShmem(aImage.data()); });
return ShmemToDataSurface(aImage.data(), aImage.stride(),
aImage.size().ToUnknownSize(), aImage.format());
IPCImage&& aImage) {
return BigBufferToDataSurface(aImage.data(), aImage.stride(),
aImage.size().ToUnknownSize(), aImage.format());
}
Modifiers nsContentUtils::GetWidgetModifiers(int32_t aModifiers) {

View File

@ -162,7 +162,7 @@ template <class T>
class StaticRefPtr;
namespace dom {
class ShmemImage;
class IPCImage;
struct AutocompleteInfo;
class BrowserChild;
class BrowserParent;
@ -2928,10 +2928,10 @@ class nsContentUtils {
mozilla::gfx::DataSourceSurface& aSurface, size_t* aLength,
int32_t* aStride, mozilla::ipc::IShmemAllocator* aAlloc);
static mozilla::Maybe<mozilla::dom::ShmemImage> SurfaceToIPCImage(
mozilla::gfx::DataSourceSurface&, mozilla::ipc::IShmemAllocator*);
static mozilla::Maybe<mozilla::dom::IPCImage> SurfaceToIPCImage(
mozilla::gfx::DataSourceSurface&);
static already_AddRefed<mozilla::gfx::DataSourceSurface> IPCImageToSurface(
mozilla::dom::ShmemImage&&, mozilla::ipc::IShmemAllocator*);
mozilla::dom::IPCImage&&);
// Helpers shared by the implementations of nsContentUtils methods and
// nsIDOMWindowUtils methods.

View File

@ -4993,10 +4993,10 @@ mozilla::ipc::IPCResult ContentParent::RecvCopyFavicon(
}
mozilla::ipc::IPCResult ContentParent::RecvFindImageText(
ShmemImage&& aImage, nsTArray<nsCString>&& aLanguages,
IPCImage&& aImage, nsTArray<nsCString>&& aLanguages,
FindImageTextResolver&& aResolver) {
RefPtr<DataSourceSurface> surf =
nsContentUtils::IPCImageToSurface(std::move(aImage), this);
nsContentUtils::IPCImageToSurface(std::move(aImage));
if (!surf) {
aResolver(TextRecognitionResultOrError("Failed to read image"_ns));
return IPC_OK();

View File

@ -1114,7 +1114,7 @@ class ContentParent final : public PContentParent,
mozilla::ipc::IPCResult RecvCopyFavicon(nsIURI* aOldURI, nsIURI* aNewURI,
const bool& aInPrivateBrowsing);
mozilla::ipc::IPCResult RecvFindImageText(ShmemImage&&, nsTArray<nsCString>&&,
mozilla::ipc::IPCResult RecvFindImageText(IPCImage&&, nsTArray<nsCString>&&,
FindImageTextResolver&&);
virtual void ProcessingError(Result aCode, const char* aMsgName) override;

View File

@ -443,8 +443,8 @@ union TextRecognitionResultOrError {
nsCString;
};
struct ShmemImage {
Shmem data;
struct IPCImage {
BigBuffer data;
uint32_t stride;
SurfaceFormat format;
ImageIntSize size;
@ -1243,7 +1243,7 @@ parent:
async CopyFavicon(nsIURI oldURI, nsIURI newURI, bool isPrivate);
async FindImageText(ShmemImage image, nsCString[] languages)
async FindImageText(IPCImage image, nsCString[] languages)
returns (TextRecognitionResultOrError result);
/**

View File

@ -41,13 +41,13 @@ auto TextRecognition::FindText(gfx::DataSourceSurface& aSurface,
-> RefPtr<NativePromise> {
if (XRE_IsContentProcess()) {
auto* contentChild = ContentChild::GetSingleton();
auto image = nsContentUtils::SurfaceToIPCImage(aSurface, contentChild);
auto image = nsContentUtils::SurfaceToIPCImage(aSurface);
if (!image) {
return NativePromise::CreateAndReject("Failed to share data surface"_ns,
__func__);
}
auto promise = MakeRefPtr<NativePromise::Private>(__func__);
contentChild->SendFindImageText(*image, aLanguages)
contentChild->SendFindImageText(std::move(*image), aLanguages)
->Then(
GetCurrentSerialEventTarget(), __func__,
[promise](TextRecognitionResultOrError&& aResultOrError) {