From 69c98c5f712b06ba5656c53db810c44f99e724f5 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Tue, 11 Jul 2023 17:45:40 +0800 Subject: [PATCH] Added test case `test28ThatNotTriggerCACopyImage` and `test28ThatDoTriggerCACopyImage` --- Tests/Tests/SDImageCoderTests.m | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/Tests/Tests/SDImageCoderTests.m b/Tests/Tests/SDImageCoderTests.m index d62273d3..4ec29e7d 100644 --- a/Tests/Tests/SDImageCoderTests.m +++ b/Tests/Tests/SDImageCoderTests.m @@ -448,6 +448,62 @@ expect(data2).notTo.beNil(); } +- (void)test28ThatNotTriggerCACopyImage { + // 10 * 8 pixels, RGBA8888 + size_t width = 10; + size_t height = 8; + size_t bitsPerComponent = 8; + size_t components = 4; + size_t bitsPerPixel = bitsPerComponent * components; + size_t bytesPerRow = SDByteAlign(bitsPerPixel / 8 * width, SDImageCoderHelper.preferredByteAlignment); + size_t size = bytesPerRow * height; + uint8_t bitmap[size]; + for (size_t i = 0; i < size; i++) { + bitmap[i] = 255; + } + CGColorSpaceRef colorspace = [SDImageCoderHelper colorSpaceGetDeviceRGB]; + CGBitmapInfo bitmapInfo = [SDImageCoderHelper preferredBitmapInfo:YES]; + CFDataRef data = CFDataCreateWithBytesNoCopy(NULL, bitmap, size, NULL); + CGDataProviderRef provider = CGDataProviderCreateWithCFData(data); + BOOL shouldInterpolate = YES; + CGColorRenderingIntent intent = kCGRenderingIntentDefault; + CGImageRef cgImage = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorspace, bitmapInfo, provider, NULL, shouldInterpolate, intent); + XCTAssert(cgImage); + BOOL result = [SDImageCoderHelper CGImageIsHardwareSupported:cgImage]; + // Since it's 32 bytes aligned, return false + XCTAssertTrue(result); +} + +- (void)test28ThatDoTriggerCACopyImage { + // 10 * 8 pixels, RGBA8888 + size_t width = 10; + size_t height = 8; + size_t bitsPerComponent = 8; + size_t components = 4; + size_t bitsPerPixel = bitsPerComponent * components; + size_t bytesPerRow = bitsPerPixel / 8 * width; + size_t size = bytesPerRow * height; + uint8_t bitmap[size]; + for (size_t i = 0; i < size; i++) { + bitmap[i] = 255; + } + CGColorSpaceRef colorspace = [SDImageCoderHelper colorSpaceGetDeviceRGB]; + CGBitmapInfo bitmapInfo = [SDImageCoderHelper preferredBitmapInfo:YES]; + CFDataRef data = CFDataCreateWithBytesNoCopy(NULL, bitmap, size, NULL); + CGDataProviderRef provider = CGDataProviderCreateWithCFData(data); + BOOL shouldInterpolate = YES; + CGColorRenderingIntent intent = kCGRenderingIntentDefault; + CGImageRef cgImage = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorspace, bitmapInfo, provider, NULL, shouldInterpolate, intent); + XCTAssert(cgImage); + BOOL result = [SDImageCoderHelper CGImageIsHardwareSupported:cgImage]; + // Since it's not 32 bytes aligned, return false + XCTAssertFalse(result); + // Let's force-decode to check again + CGImageRef newCGImage = [SDImageCoderHelper CGImageCreateDecoded:cgImage]; + BOOL newResult = [SDImageCoderHelper CGImageIsHardwareSupported:newCGImage]; + XCTAssertTrue(newResult); +} + #pragma mark - Utils - (void)verifyCoder:(id)coder