Skip to content

Commit

Permalink
GraphicsContext::drawNativeImage has redundant imageSize parameter
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=266279
rdar://119551804

Reviewed by Matt Woodrow.

NativeImage knows its size.
The GraphicsContext::drawNativeImage has FloatRect sourceRect parameter
to define the portion of the full size to use.

The IntRect imageSize is probably a legacy argument used for partial
images before sourceRect was introduced.

All callsites but one use the full NativeImage size as the imageSize.
The only non-full image draw in BitmapImage uses the same rect as
the source rect.

The function can be further simplified, but to make this patch easier
to review make minimal compatible changes to
GraphicsContextCG::drawNativeImageInternal.

Reland:
Harden against callers that call with sourceRect bigger than the
actual source is. The previously used sourceRect.intersect(imageRect)
was incorrect, as that would not adjust the respective destinationRect.

* Source/WebCore/Modules/WebGPU/Implementation/WebGPUImpl.cpp:
(WebCore::WebGPU::GPUImpl::paintToCanvas):
* Source/WebCore/html/canvas/CanvasRenderingContext2DBase.cpp:
(WebCore::CanvasRenderingContext2DBase::drawImage):
* Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp:
(WebCore::WebGLRenderingContextBase::videoFrameToImage):
* Source/WebCore/platform/graphics/BifurcatedGraphicsContext.cpp:
(WebCore::BifurcatedGraphicsContext::drawNativeImageInternal):
* Source/WebCore/platform/graphics/BifurcatedGraphicsContext.h:
* Source/WebCore/platform/graphics/BitmapImage.cpp:
(WebCore::BitmapImage::preTransformedNativeImageForCurrentFrame):
(WebCore::drawNativeImage):
* Source/WebCore/platform/graphics/GraphicsContext.cpp:
(WebCore::GraphicsContext::drawNativeImage):
(WebCore::GraphicsContext::drawImageBuffer):
(WebCore::GraphicsContext::drawConsumingImageBuffer):
* Source/WebCore/platform/graphics/GraphicsContext.h:
(WebCore::GraphicsContext::drawNativeImage):
(WebCore::GraphicsContext::drawNativeImageInternal):
* Source/WebCore/platform/graphics/GraphicsContextGL.cpp:
(WebCore::GraphicsContextGL::paintToCanvas):
* Source/WebCore/platform/graphics/NativeImage.h:
* Source/WebCore/platform/graphics/NullGraphicsContext.h:
* Source/WebCore/platform/graphics/SourceImage.cpp:
(WebCore::SourceImage::imageBuffer const):
* Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm:
(WebCore::MediaPlayerPrivateAVFoundationObjC::paintWithVideoOutput):
* Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.mm:
(WebCore::MediaPlayerPrivateMediaSourceAVFObjC::paintCurrentFrameInContext):
* Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaStreamAVFObjC.mm:
(WebCore::MediaPlayerPrivateMediaStreamAVFObjC::paintCurrentFrameInContext):
* Source/WebCore/platform/graphics/cairo/GraphicsContextCairo.cpp:
(WebCore::GraphicsContextCairo::drawNativeImageInternal):
* Source/WebCore/platform/graphics/cairo/GraphicsContextCairo.h:
* Source/WebCore/platform/graphics/cairo/NativeImageCairo.cpp:
(WebCore::NativeImage::draw):
* Source/WebCore/platform/graphics/cg/GraphicsContextCG.cpp:
(WebCore::GraphicsContextCG::drawNativeImageInternal):
* Source/WebCore/platform/graphics/cg/GraphicsContextCG.h:
* Source/WebCore/platform/graphics/cg/NativeImageCG.cpp:
(WebCore::NativeImage::draw):
* Source/WebCore/platform/graphics/cocoa/IconCocoa.mm:
(WebCore::Icon::paint):
* Source/WebCore/platform/graphics/cocoa/MediaPlayerPrivateWebM.mm:
(WebCore::MediaPlayerPrivateWebM::paintCurrentFrameInContext):
* Source/WebCore/platform/graphics/coretext/DrawGlyphsRecorderCoreText.cpp:
(WebCore::DrawGlyphsRecorder::recordDrawImage):
* Source/WebCore/platform/graphics/cv/VideoFrameCV.mm:
(WebCore::VideoFrame::paintInContext):
* Source/WebCore/platform/graphics/displaylists/DisplayListItems.cpp:
(WebCore::DisplayList::DrawNativeImage::apply const):
* Source/WebCore/platform/graphics/displaylists/DisplayListItems.h:
(WebCore::DisplayList::DrawNativeImage::DrawNativeImage):
(WebCore::DisplayList::DrawNativeImage::imageIdentifier const):
(WebCore::DisplayList::DrawNativeImage::imageSize const): Deleted.
* Source/WebCore/platform/graphics/displaylists/DisplayListRecorder.cpp:
(WebCore::DisplayList::Recorder::drawNativeImageInternal):
* Source/WebCore/platform/graphics/displaylists/DisplayListRecorder.h:
* Source/WebCore/platform/graphics/displaylists/DisplayListRecorderImpl.cpp:
(WebCore::DisplayList::RecorderImpl::recordDrawNativeImage):
* Source/WebCore/platform/graphics/displaylists/DisplayListRecorderImpl.h:
* Source/WebCore/platform/graphics/filters/software/FEImageSoftwareApplier.cpp:
(WebCore::FEImageSoftwareApplier::apply const):
* Source/WebCore/platform/graphics/nicosia/cairo/NicosiaCairoOperationRecorder.cpp:
(Nicosia::CairoOperationRecorder::drawNativeImageInternal):
* Source/WebCore/platform/graphics/nicosia/cairo/NicosiaCairoOperationRecorder.h:
* Source/WebCore/platform/graphics/win/MediaPlayerPrivateMediaFoundation.cpp:
(WebCore::MediaPlayerPrivateMediaFoundation::Direct3DPresenter::paintCurrentFrame):
* Source/WebKit/GPUProcess/graphics/RemoteDisplayListRecorder.cpp:
(WebKit::RemoteDisplayListRecorder::drawNativeImage):
* Source/WebKit/GPUProcess/graphics/RemoteDisplayListRecorder.h:
* Source/WebKit/GPUProcess/graphics/RemoteDisplayListRecorder.messages.in:
* Source/WebKit/GPUProcess/graphics/RemoteImageBuffer.cpp:
(WebKit::RemoteImageBuffer::filteredNativeImage):
* Source/WebKit/GPUProcess/media/RemoteImageDecoderAVFProxy.cpp:
(WebKit::RemoteImageDecoderAVFProxy::createFrameImageAtIndex):
* Source/WebKit/GPUProcess/media/RemoteMediaPlayerManagerProxy.cpp:
(WebKit::RemoteMediaPlayerManagerProxy::bitmapImageForCurrentTime):
* Source/WebKit/Shared/API/c/cg/WKImageCG.cpp:
(WKImageCreateFromCGImage):
* Source/WebKit/Shared/DisplayListArgumentCoders.serialization.in:
* Source/WebKit/Shared/ShareableBitmap.cpp:
(WebKit::ShareableBitmap::createFromImageDraw):
* Source/WebKit/WebProcess/GPU/graphics/RemoteDisplayListRecorderProxy.cpp:
(WebKit::RemoteDisplayListRecorderProxy::recordDrawNativeImage):
* Source/WebKit/WebProcess/GPU/graphics/RemoteDisplayListRecorderProxy.h:
* Source/WebKit/WebProcess/GPU/media/MediaPlayerPrivateRemote.cpp:
(WebKit::MediaPlayerPrivateRemote::paintCurrentFrameInContext):
* Source/WebKit/WebProcess/WebCoreSupport/ShareableBitmapUtilities.cpp:
(WebKit::createShareableBitmap):
* Source/WebKit/WebProcess/cocoa/TextTrackRepresentationCocoa.mm:
(WebKit::WebTextTrackRepresentationCocoa::update):
* Tools/TestWebKitAPI/Tests/WebCore/ImageBufferTests.cpp:
(TestWebKitAPI::TEST_P):
* Tools/TestWebKitAPI/Tests/WebCore/cg/GraphicsContextCGTests.mm:
(TestWebKitAPI::TEST):

Canonical link: https://commits.webkit.org/272333@main
  • Loading branch information
kkinnunen-apple committed Dec 20, 2023
1 parent 34d1661 commit f0cddec
Show file tree
Hide file tree
Showing 51 changed files with 76 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void GPUImpl::paintToCanvas(WebCore::NativeImage& image, const WebCore::IntSize&
FloatRect canvasRect(FloatPoint(), canvasSize);
GraphicsContextStateSaver stateSaver(context);
context.setImageInterpolationQuality(InterpolationQuality::DoNotInterpolate);
context.drawNativeImage(image, imageSize, canvasRect, FloatRect(FloatPoint(), imageSize), { CompositeOperator::Copy });
context.drawNativeImage(image, canvasRect, FloatRect(FloatPoint(), imageSize), { CompositeOperator::Copy });
}

} // namespace WebCore::WebGPU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1774,7 +1774,7 @@ ExceptionOr<void> CanvasRenderingContext2DBase::drawImage(HTMLVideoElement& vide
#if USE(CG)
if (c->hasPlatformContext() && video.shouldGetNativeImageForCanvasDrawing()) {
if (auto image = video.nativeImageForCurrentTime()) {
c->drawNativeImage(*image, FloatSize(video.videoWidth(), video.videoHeight()), normalizedDstRect, normalizedSrcRect);
c->drawNativeImage(*image, normalizedDstRect, normalizedSrcRect);

didDraw(repaintEntireCanvas, normalizedDstRect, defaultDidDrawOptionsWithoutPostProcessing());

Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/html/canvas/WebGLRenderingContextBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4221,13 +4221,13 @@ RefPtr<Image> WebGLRenderingContextBase::videoFrameToImage(HTMLVideoElement& vid
synthesizeGLError(GraphicsContextGL::INVALID_VALUE, functionName, "video visible size is empty");
return nullptr;
}
FloatRect imageRect { { }, imageSize };
imageBuffer = m_generatedImageCache.imageBuffer(imageSize, nativeImage->colorSpace(), CompositeOperator::Copy);
if (!imageBuffer) {
synthesizeGLError(GraphicsContextGL::OUT_OF_MEMORY, functionName, "out of memory");
return nullptr;
}
imageBuffer->context().drawNativeImage(*nativeImage, imageRect.size(), imageRect, imageRect, { CompositeOperator::Copy });
FloatRect imageRect { { }, imageSize };
imageBuffer->context().drawNativeImage(*nativeImage, imageRect, imageRect, { CompositeOperator::Copy });
}
#endif
if (!imageBuffer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,10 @@ void BifurcatedGraphicsContext::setMiterLimit(float miterLimit)
VERIFY_STATE_SYNCHRONIZATION();
}

void BifurcatedGraphicsContext::drawNativeImageInternal(NativeImage& nativeImage, const FloatSize& selfSize, const FloatRect& destRect, const FloatRect& srcRect, ImagePaintingOptions options)
void BifurcatedGraphicsContext::drawNativeImageInternal(NativeImage& nativeImage, const FloatRect& destRect, const FloatRect& srcRect, ImagePaintingOptions options)
{
m_primaryContext.drawNativeImageInternal(nativeImage, selfSize, destRect, srcRect, options);
m_secondaryContext.drawNativeImageInternal(nativeImage, selfSize, destRect, srcRect, options);
m_primaryContext.drawNativeImageInternal(nativeImage, destRect, srcRect, options);
m_secondaryContext.drawNativeImageInternal(nativeImage, destRect, srcRect, options);

VERIFY_STATE_SYNCHRONIZATION();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class WEBCORE_EXPORT BifurcatedGraphicsContext : public GraphicsContext {
void setLineJoin(LineJoin) final;
void setMiterLimit(float) final;

void drawNativeImageInternal(NativeImage&, const FloatSize& selfSize, const FloatRect& destRect, const FloatRect& srcRect, ImagePaintingOptions = { }) final;
void drawNativeImageInternal(NativeImage&, const FloatRect& destRect, const FloatRect& srcRect, ImagePaintingOptions = { }) final;
void drawSystemImage(SystemImage&, const FloatRect&) final;
void drawPattern(NativeImage&, const FloatRect& destRect, const FloatRect& tileRect, const AffineTransform& patternTransform, const FloatPoint& phase, const FloatSize& spacing, ImagePaintingOptions = { }) final;
ImageDrawResult drawImage(Image&, const FloatRect& destination, const FloatRect& source, ImagePaintingOptions = { ImageOrientation::Orientation::FromImage }) final;
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/platform/graphics/BitmapImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ RefPtr<NativeImage> BitmapImage::preTransformedNativeImageForCurrentFrame(bool r
FloatRect destRect(FloatPoint(), correctedSizeFloat);
FloatRect sourceRect(FloatPoint(), sourceSize);

buffer->context().drawNativeImage(*image, sourceSize, destRect, sourceRect, { orientation });
buffer->context().drawNativeImage(*image, destRect, sourceRect, { orientation });
return ImageBuffer::sinkIntoNativeImage(WTFMove(buffer));
}

Expand Down Expand Up @@ -228,7 +228,7 @@ static inline void drawNativeImage(NativeImage& image, GraphicsContext& context,
if (subsampledImageSize != srcSize)
adjustedSrcRect = mapRect(srcRect, FloatRect({ }, srcSize), FloatRect({ }, subsampledImageSize));

context.drawNativeImage(image, subsampledImageSize, destRect, adjustedSrcRect, options);
context.drawNativeImage(image, destRect, adjustedSrcRect, options);
}

ImageDrawResult BitmapImage::draw(GraphicsContext& context, const FloatRect& destRect, const FloatRect& requestedSrcRect, ImagePaintingOptions options)
Expand Down
9 changes: 4 additions & 5 deletions Source/WebCore/platform/graphics/GraphicsContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ RefPtr<ImageBuffer> GraphicsContext::createAlignedImageBuffer(const FloatRect& r
return createScaledImageBuffer(rect, scaleFactor(), colorSpace, renderingMode(), renderingMethod);
}

void GraphicsContext::drawNativeImage(NativeImage& image, const FloatSize& imageSize, const FloatRect& destination, const FloatRect& source, ImagePaintingOptions options)
void GraphicsContext::drawNativeImage(NativeImage& image, const FloatRect& destination, const FloatRect& source, ImagePaintingOptions options)
{
image.draw(*this, imageSize, destination, source, options);
image.draw(*this, destination, source, options);
}

void GraphicsContext::drawSystemImage(SystemImage& systemImage, const FloatRect& destinationRect)
Expand Down Expand Up @@ -360,7 +360,7 @@ void GraphicsContext::drawImageBuffer(ImageBuffer& image, const FloatRect& desti
FloatRect sourceScaled = source;
sourceScaled.scale(image.resolutionScale());
if (auto nativeImage = nativeImageForDrawing(image))
drawNativeImageInternal(*nativeImage, image.backendSize(), destination, sourceScaled, options);
drawNativeImageInternal(*nativeImage, destination, sourceScaled, options);
}

void GraphicsContext::drawConsumingImageBuffer(RefPtr<ImageBuffer> image, const FloatPoint& destination, ImagePaintingOptions imagePaintingOptions)
Expand All @@ -387,9 +387,8 @@ void GraphicsContext::drawConsumingImageBuffer(RefPtr<ImageBuffer> image, const
InterpolationQualityMaintainer interpolationQualityForThisScope(*this, options.interpolationQuality());
FloatRect scaledSource = source;
scaledSource.scale(image->resolutionScale());
auto backendSize = image->backendSize();
if (auto nativeImage = ImageBuffer::sinkIntoNativeImage(WTFMove(image)))
drawNativeImageInternal(*nativeImage, backendSize, destination, scaledSource, options);
drawNativeImageInternal(*nativeImage, destination, scaledSource, options);
}

void GraphicsContext::drawFilteredImageBuffer(ImageBuffer* sourceImage, const FloatRect& sourceImageRect, Filter& filter, FilterResults& results)
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/platform/graphics/GraphicsContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class GraphicsContext {
WEBCORE_EXPORT virtual RefPtr<ImageBuffer> createAlignedImageBuffer(const FloatSize&, const DestinationColorSpace& = DestinationColorSpace::SRGB(), std::optional<RenderingMethod> = std::nullopt) const;
WEBCORE_EXPORT virtual RefPtr<ImageBuffer> createAlignedImageBuffer(const FloatRect&, const DestinationColorSpace& = DestinationColorSpace::SRGB(), std::optional<RenderingMethod> = std::nullopt) const;

WEBCORE_EXPORT void drawNativeImage(NativeImage&, const FloatSize& selfSize, const FloatRect& destRect, const FloatRect& srcRect, ImagePaintingOptions = { });
WEBCORE_EXPORT void drawNativeImage(NativeImage&, const FloatRect& destRect, const FloatRect& srcRect, ImagePaintingOptions = { });

WEBCORE_EXPORT virtual void drawSystemImage(SystemImage&, const FloatRect&);

Expand Down Expand Up @@ -370,7 +370,7 @@ class GraphicsContext {

IsDeferred deferred() const { return m_isDeferred; }
private:
virtual void drawNativeImageInternal(NativeImage&, const FloatSize& selfSize, const FloatRect& destRect, const FloatRect& srcRect, ImagePaintingOptions = { }) = 0;
virtual void drawNativeImageInternal(NativeImage&, const FloatRect& destRect, const FloatRect& srcRect, ImagePaintingOptions = { }) = 0;

protected:
WEBCORE_EXPORT RefPtr<NativeImage> nativeImageForDrawing(ImageBuffer&);
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/platform/graphics/GraphicsContextGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ void GraphicsContextGL::paintToCanvas(NativeImage& image, const IntSize& canvasS
context.scale(FloatSize(1, -1));
context.translate(0, -imageSize.height());
context.setImageInterpolationQuality(InterpolationQuality::DoNotInterpolate);
context.drawNativeImage(image, imageSize, canvasRect, FloatRect(FloatPoint(), imageSize), { CompositeOperator::Copy });
context.drawNativeImage(image, canvasRect, FloatRect { { }, imageSize }, { CompositeOperator::Copy });
}

void GraphicsContextGL::paintToCanvas(const GraphicsContextGLAttributes& sourceContextAttributes, Ref<PixelBuffer>&& pixelBuffer, const IntSize& canvasSize, GraphicsContext& context)
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/platform/graphics/NativeImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class NativeImage final : public RenderingResource {
Color singlePixelSolidColor() const;
WEBCORE_EXPORT DestinationColorSpace colorSpace() const;

void draw(GraphicsContext&, const FloatSize& imageSize, const FloatRect& destRect, const FloatRect& srcRect, ImagePaintingOptions);
void draw(GraphicsContext&, const FloatRect& destRect, const FloatRect& srcRect, ImagePaintingOptions);
void clearSubimages();

WEBCORE_EXPORT void replaceContents(PlatformImagePtr);
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/platform/graphics/NullGraphicsContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class NullGraphicsContext : public GraphicsContext {

void didUpdateState(GraphicsContextState&) final { }

void drawNativeImageInternal(NativeImage&, const FloatSize&, const FloatRect&, const FloatRect&, ImagePaintingOptions) final { }
void drawNativeImageInternal(NativeImage&, const FloatRect&, const FloatRect&, ImagePaintingOptions) final { }

void drawSystemImage(SystemImage&, const FloatRect&) final { };

Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/platform/graphics/SourceImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ ImageBuffer* SourceImage::imageBuffer() const
if (!imageBuffer)
return nullptr;

imageBuffer->context().drawNativeImage(nativeImage, rect.size(), rect, rect);
imageBuffer->context().drawNativeImage(nativeImage, rect, rect);
m_transformedImageVariant = { imageBuffer.releaseNonNull() };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2773,7 +2773,7 @@ void determineChangedTracksFromNewTracksAndOldItems(MediaSelectionGroupAVFObjC*
INFO_LOG(logIdentifier);

FloatRect imageRect { FloatPoint::zero(), m_lastImage->size() };
context.drawNativeImage(*m_lastImage, imageRect.size(), outputRect, imageRect);
context.drawNativeImage(*m_lastImage, outputRect, imageRect);

// If we have created an AVAssetImageGenerator in the past due to m_videoOutput not having an available
// video frame, destroy it now that it is no longer needed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ void getSupportedTypes(HashSet<String>& types) const final

GraphicsContextStateSaver stateSaver(context);
FloatRect imageRect { FloatPoint::zero(), image->size() };
context.drawNativeImage(*image, imageRect.size(), outputRect, imageRect);
context.drawNativeImage(*image, outputRect, imageRect);
}

#if !HAVE(AVSAMPLEBUFFERDISPLAYLAYER_COPYDISPLAYEDPIXELBUFFER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ static inline CGAffineTransform videoTransformationMatrix(VideoFrame& videoFrame
AffineTransform videoTransform = videoTransformationMatrix(*m_imagePainter.videoFrame);
FloatRect transformedDestRect = valueOrDefault(videoTransform.inverse()).mapRect(destRect);
context.concatCTM(videoTransform);
context.drawNativeImage(*image, imageRect.size(), transformedDestRect, imageRect);
context.drawNativeImage(*image, transformedDestRect, imageRect);
}

RefPtr<VideoFrame> MediaPlayerPrivateMediaStreamAVFObjC::videoFrameForCurrentTime()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void GraphicsContextCairo::drawRect(const FloatRect& rect, float borderThickness
Cairo::drawRect(*this, rect, borderThickness, fillColor(), strokeStyle(), strokeColor());
}

void GraphicsContextCairo::drawNativeImageInternal(NativeImage& nativeImage, const FloatSize&, const FloatRect& destRect, const FloatRect& srcRect, ImagePaintingOptions options)
void GraphicsContextCairo::drawNativeImageInternal(NativeImage& nativeImage, const FloatRect& destRect, const FloatRect& srcRect, ImagePaintingOptions options)
{
auto& state = this->state();
Cairo::drawPlatformImage(*this, nativeImage.platformImage().get(), destRect, srcRect, { options, state.imageInterpolationQuality() }, state.alpha(), Cairo::ShadowState(state));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class WEBCORE_EXPORT GraphicsContextCairo final : public GraphicsContext {
void strokePath(const Path&) final;
void clearRect(const FloatRect&) final;

void drawNativeImageInternal(NativeImage&, const FloatSize&, const FloatRect&, const FloatRect&, ImagePaintingOptions) final;
void drawNativeImageInternal(NativeImage&, const FloatRect&, const FloatRect&, ImagePaintingOptions) final;
void drawPattern(NativeImage&, const FloatRect& destRect, const FloatRect& srcRect, const AffineTransform&, const FloatPoint& phase, const FloatSize& spacing, ImagePaintingOptions) final;

void drawRect(const FloatRect&, float) final;
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/platform/graphics/cairo/NativeImageCairo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ Color NativeImage::singlePixelSolidColor() const
return unpremultiplied(asSRGBA(PackedColor::ARGB { *pixel }));
}

void NativeImage::draw(GraphicsContext& context, const FloatSize& imageSize, const FloatRect& destinationRect, const FloatRect& sourceRect, ImagePaintingOptions options)
void NativeImage::draw(GraphicsContext& context, const FloatRect& destinationRect, const FloatRect& sourceRect, ImagePaintingOptions options)
{
context.drawNativeImageInternal(*this, imageSize, destinationRect, sourceRect, options);
context.drawNativeImageInternal(*this, destinationRect, sourceRect, options);
}

void NativeImage::clearSubimages()
Expand Down
27 changes: 7 additions & 20 deletions Source/WebCore/platform/graphics/cg/GraphicsContextCG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,18 @@ void GraphicsContextCG::restore(GraphicsContextState::Purpose purpose)
m_userToDeviceTransformKnownToBeIdentity = false;
}

void GraphicsContextCG::drawNativeImageInternal(NativeImage& nativeImage, const FloatSize& imageSize, const FloatRect& destRect, const FloatRect& srcRect, ImagePaintingOptions options)
void GraphicsContextCG::drawNativeImageInternal(NativeImage& nativeImage, const FloatRect& destRect, const FloatRect& srcRect, ImagePaintingOptions options)
{
auto image = nativeImage.platformImage();
if (!image)
return;
auto imageSize = nativeImage.size();
if (options.orientation().usesWidthAsHeight())
imageSize = imageSize.transposedSize();
auto imageRect = FloatRect { { }, imageSize };
auto normalizedSrcRect = normalizeRect(srcRect);
auto normalizedDestRect = normalizeRect(destRect);

if (!image || !imageRect.intersects(normalizedSrcRect))
if (!imageRect.intersects(normalizedSrcRect))
return;

#if !LOG_DISABLED
Expand Down Expand Up @@ -317,19 +321,13 @@ void GraphicsContextCG::drawNativeImageInternal(NativeImage& nativeImage, const
return adoptCF(CGImageCreateWithImageInRect(image, physicalSubimageRect));
};

auto imageLogicalSize = [](CGImageRef image, ImagePaintingOptions options) -> FloatSize {
FloatSize size = FloatSize(CGImageGetWidth(image), CGImageGetHeight(image));
return options.orientation().usesWidthAsHeight() ? size.transposedSize() : size;
};

auto context = platformContext();
CGContextStateSaver stateSaver(context, false);
auto transform = CGContextGetCTM(context);

convertToDestinationColorSpaceIfNeeded(image);

auto subImage = image;
auto currentImageSize = imageLogicalSize(image.get(), options);

auto adjustedDestRect = normalizedDestRect;

Expand All @@ -348,13 +346,6 @@ void GraphicsContextCG::drawNativeImageInternal(NativeImage& nativeImage, const

auto subPixelPadding = normalizedSrcRect.location() - subimageRect.location();
adjustedDestRect = { adjustedDestRect.location() - subPixelPadding * scale, subimageRect.size() * scale };

// If the image is only partially loaded, then shrink the destination rect that we're drawing
// into accordingly.
if (currentImageSize.height() < normalizedSrcRect.maxY()) {
auto currentSubimageSize = imageLogicalSize(subImage.get(), options);
adjustedDestRect.setHeight(currentSubimageSize.height() * scale.height());
}
} else {
// If the source rect is a subportion of the image, then we compute an inflated destination rect
// that will hold the entire image and then set a clip to the portion that we want to display.
Expand All @@ -367,10 +358,6 @@ void GraphicsContextCG::drawNativeImageInternal(NativeImage& nativeImage, const
}
}

// If the image is only partially loaded, then shrink the destination rect that we're drawing into accordingly.
if (subImage == image && currentImageSize.height() < imageSize.height())
adjustedDestRect.setHeight(adjustedDestRect.height() * currentImageSize.height() / imageSize.height());

#if PLATFORM(IOS_FAMILY)
bool wasAntialiased = CGContextGetShouldAntialias(context);
// Anti-aliasing is on by default on the iPhone. Need to turn it off when drawing images.
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/platform/graphics/cg/GraphicsContextCG.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class WEBCORE_EXPORT GraphicsContextCG : public GraphicsContext {

private:
void convertToDestinationColorSpaceIfNeeded(RetainPtr<CGImageRef>&);
void drawNativeImageInternal(NativeImage&, const FloatSize& selfSize, const FloatRect& destRect, const FloatRect& srcRect, ImagePaintingOptions = { }) final;
void drawNativeImageInternal(NativeImage&, const FloatRect& destRect, const FloatRect& srcRect, ImagePaintingOptions = { }) final;

void clearCGShadow();
// Returns the platform context for purposes of context state change, not draws.
Expand Down
10 changes: 5 additions & 5 deletions Source/WebCore/platform/graphics/cg/NativeImageCG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Color NativeImage::singlePixelSolidColor() const
return makeFromComponentsClampingExceptAlpha<SRGBA<uint8_t>>(pixel[0] * 255 / pixel[3], pixel[1] * 255 / pixel[3], pixel[2] * 255 / pixel[3], pixel[3]);
}

void NativeImage::draw(GraphicsContext& context, const FloatSize& imageSize, const FloatRect& destinationRect, const FloatRect& sourceRect, ImagePaintingOptions options)
void NativeImage::draw(GraphicsContext& context, const FloatRect& destinationRect, const FloatRect& sourceRect, ImagePaintingOptions options)
{
auto isHDRColorSpace = [](CGColorSpaceRef colorSpace) -> bool {
ALLOW_DEPRECATED_DECLARATIONS_BEGIN
Expand Down Expand Up @@ -125,7 +125,7 @@ ALLOW_DEPRECATED_DECLARATIONS_END
#endif
};

auto drawHDRNativeImage = [&](GraphicsContext& context, const FloatSize& imageSize, const FloatRect& destinationRect, const FloatRect& sourceRect, ImagePaintingOptions options) -> bool {
auto drawHDRNativeImage = [&](GraphicsContext& context, const FloatRect& destinationRect, const FloatRect& sourceRect, ImagePaintingOptions options) -> bool {
if (sourceRect.isEmpty() || !isHDRNativeImage(*this))
return false;

Expand All @@ -140,7 +140,7 @@ ALLOW_DEPRECATED_DECLARATIONS_END
return false;

// Draw sourceRect from the image into the temporary ImageBuffer.
imageBuffer->context().drawNativeImageInternal(*this, imageSize, destinationRect, sourceRect, options);
imageBuffer->context().drawNativeImageInternal(*this, destinationRect, sourceRect, options);

auto sourceRectScaled = FloatRect { { }, sourceRect.size() };
auto scaleFactor = destinationRect.size() / sourceRect.size();
Expand All @@ -151,10 +151,10 @@ ALLOW_DEPRECATED_DECLARATIONS_END
};

// FIXME: rdar://105525195 -- Remove this HDR workaround once the system libraries can render images without clipping HDR data.
if (drawHDRNativeImage(context, imageSize, destinationRect, sourceRect, options))
if (drawHDRNativeImage(context, destinationRect, sourceRect, options))
return;

context.drawNativeImageInternal(*this, imageSize, destinationRect, sourceRect, options);
context.drawNativeImageInternal(*this, destinationRect, sourceRect, options);
}

void NativeImage::clearSubimages()
Expand Down
Loading

0 comments on commit f0cddec

Please sign in to comment.