| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkImageGeneratorCG.h" | 8 #include "SkImageGeneratorCG.h" |
| 9 | 9 |
| 10 #ifdef SK_BUILD_FOR_MAC | 10 #ifdef SK_BUILD_FOR_MAC |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 CGImageSourceRef imageSrc = CGImageSourceCreateWithDataProvider(cgData, 0); | 26 CGImageSourceRef imageSrc = CGImageSourceCreateWithDataProvider(cgData, 0); |
| 27 CGDataProviderRelease(cgData); | 27 CGDataProviderRelease(cgData); |
| 28 return imageSrc; | 28 return imageSrc; |
| 29 } | 29 } |
| 30 | 30 |
| 31 SkImageGenerator* SkImageGeneratorCG::NewFromEncodedCG(SkData* data) { | 31 SkImageGenerator* SkImageGeneratorCG::NewFromEncodedCG(SkData* data) { |
| 32 CGImageSourceRef imageSrc = data_to_CGImageSrc(data); | 32 CGImageSourceRef imageSrc = data_to_CGImageSrc(data); |
| 33 if (!imageSrc) { | 33 if (!imageSrc) { |
| 34 return nullptr; | 34 return nullptr; |
| 35 } | 35 } |
| 36 | 36 |
| 37 // Make sure we call CFRelease to free the imageSrc. Since CFRelease actual
ly takes | 37 // Make sure we call CFRelease to free the imageSrc. Since CFRelease actual
ly takes |
| 38 // a const void*, we must cast the imageSrc to a const void*. | 38 // a const void*, we must cast the imageSrc to a const void*. |
| 39 SkAutoTCallVProc<const void, CFRelease> autoImageSrc(imageSrc); | 39 SkAutoTCallVProc<const void, CFRelease> autoImageSrc(imageSrc); |
| 40 | 40 |
| 41 CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSrc, 0,
nullptr); | 41 CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSrc, 0,
nullptr); |
| 42 if (!properties) { | 42 if (!properties) { |
| 43 return nullptr; | 43 return nullptr; |
| 44 } | 44 } |
| 45 | 45 |
| 46 CFNumberRef widthRef = (CFNumberRef) (CFDictionaryGetValue(properties, | 46 CFNumberRef widthRef = (CFNumberRef) (CFDictionaryGetValue(properties, |
| 47 kCGImagePropertyPixelWidth)); | 47 kCGImagePropertyPixelWidth)); |
| 48 CFNumberRef heightRef = (CFNumberRef) (CFDictionaryGetValue(properties, | 48 CFNumberRef heightRef = (CFNumberRef) (CFDictionaryGetValue(properties, |
| 49 kCGImagePropertyPixelHeight)); | 49 kCGImagePropertyPixelHeight)); |
| 50 if (nullptr == widthRef || nullptr == heightRef) { | 50 if (nullptr == widthRef || nullptr == heightRef) { |
| 51 return nullptr; | 51 return nullptr; |
| 52 } | 52 } |
| 53 bool hasAlpha = (bool) (CFDictionaryGetValue(properties, | 53 bool hasAlpha = (bool) (CFDictionaryGetValue(properties, |
| 54 kCGImagePropertyHasAlpha)); | 54 kCGImagePropertyHasAlpha)); |
| 55 | 55 |
| 56 int width, height; | 56 int width, height; |
| 57 if (!CFNumberGetValue(widthRef, kCFNumberIntType, &width) || | 57 if (!CFNumberGetValue(widthRef, kCFNumberIntType, &width) || |
| 58 !CFNumberGetValue(heightRef, kCFNumberIntType, &height)) { | 58 !CFNumberGetValue(heightRef, kCFNumberIntType, &height)) { |
| 59 return nullptr; | 59 return nullptr; |
| 60 } | 60 } |
| 61 | 61 |
| 62 SkAlphaType alphaType = hasAlpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType
; | 62 SkAlphaType alphaType = hasAlpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType
; |
| 63 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, alphaT
ype); | 63 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, alphaT
ype); |
| 64 | 64 |
| 65 // FIXME: We have the opportunity to extract color space information here, | 65 // FIXME: We have the opportunity to extract color space information here, |
| 66 // though I think it makes sense to wait until we understand how | 66 // though I think it makes sense to wait until we understand how |
| 67 // we want to communicate it to the generator. | 67 // we want to communicate it to the generator. |
| 68 | 68 |
| 69 return new SkImageGeneratorCG(info, autoImageSrc.release(), data); | 69 return new SkImageGeneratorCG(info, autoImageSrc.release(), data); |
| 70 } | 70 } |
| 71 | 71 |
| 72 SkImageGeneratorCG::SkImageGeneratorCG(const SkImageInfo& info, const void* imag
eSrc, SkData* data) | 72 SkImageGeneratorCG::SkImageGeneratorCG(const SkImageInfo& info, const void* imag
eSrc, SkData* data) |
| 73 : INHERITED(info) | 73 : INHERITED(info) |
| 74 , fImageSrc(imageSrc) | 74 , fImageSrc(imageSrc) |
| 75 , fData(SkRef(data)) | 75 , fData(SkRef(data)) |
| 76 {} | 76 {} |
| 77 | 77 |
| 78 SkData* SkImageGeneratorCG::onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) { | 78 SkData* SkImageGeneratorCG::onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) { |
| 79 return SkRef(fData.get()); | 79 return SkRef(fData.get()); |
| 80 } | 80 } |
| 81 | 81 |
| 82 bool SkImageGeneratorCG::onGetPixels(const SkImageInfo& info, void* pixels, size
_t rowBytes, | 82 bool SkImageGeneratorCG::onGetPixels(const SkImageInfo& info, void* pixels, size
_t rowBytes, |
| 83 SkPMColor ctable[], int* ctableCount) { | 83 SkPMColor ctable[], int* ctableCount) { |
| 84 if (kN32_SkColorType != info.colorType()) { | 84 if (kN32_SkColorType != info.colorType()) { |
| 85 // FIXME: Support other colorTypes. | 85 // FIXME: Support other colorTypes. |
| 86 return false; | 86 return false; |
| 87 } | 87 } |
| 88 | 88 |
| 89 switch (info.alphaType()) { | 89 switch (info.alphaType()) { |
| 90 case kOpaque_SkAlphaType: | 90 case kOpaque_SkAlphaType: |
| 91 if (kOpaque_SkAlphaType != this->getInfo().alphaType()) { | 91 if (kOpaque_SkAlphaType != this->getInfo().alphaType()) { |
| 92 return false; | 92 return false; |
| 93 } | 93 } |
| 94 break; | 94 break; |
| 95 case kPremul_SkAlphaType: | 95 case kPremul_SkAlphaType: |
| 96 break; | 96 break; |
| 97 default: | 97 default: |
| 98 return false; | 98 return false; |
| 99 } | 99 } |
| 100 | 100 |
| 101 CGImageRef image = CGImageSourceCreateImageAtIndex((CGImageSourceRef) fImage
Src.get(), 0, | 101 CGImageRef image = CGImageSourceCreateImageAtIndex((CGImageSourceRef) fImage
Src.get(), 0, |
| 102 nullptr); | 102 nullptr); |
| 103 if (!image) { | 103 if (!image) { |
| 104 return false; | 104 return false; |
| 105 } | 105 } |
| 106 SkAutoTCallVProc<CGImage, CGImageRelease> autoImage(image); | 106 SkAutoTCallVProc<CGImage, CGImageRelease> autoImage(image); |
| 107 | 107 |
| 108 // FIXME: Using this function (as opposed to swizzling ourselves) greatly | 108 // FIXME: Using this function (as opposed to swizzling ourselves) greatly |
| 109 // restricts the color and alpha types that we support. If we | 109 // restricts the color and alpha types that we support. If we |
| 110 // swizzle ourselves, we can add support for: | 110 // swizzle ourselves, we can add support for: |
| 111 // kUnpremul_SkAlphaType | 111 // kUnpremul_SkAlphaType |
| 112 // 16-bit per component RGBA | 112 // 16-bit per component RGBA |
| 113 // kGray_8_SkColorType | 113 // kGray_8_SkColorType |
| 114 // kIndex_8_SkColorType | 114 // kIndex_8_SkColorType |
| 115 // Additionally, it would be interesting to compare the performance | 115 // Additionally, it would be interesting to compare the performance |
| 116 // of SkSwizzler with CG's built in swizzler. | 116 // of SkSwizzler with CG's built in swizzler. |
| 117 if (!SkCopyPixelsFromCGImage(info, rowBytes, pixels, image)) { | 117 if (!SkCopyPixelsFromCGImage(info, rowBytes, pixels, image)) { |
| 118 return false; | 118 return false; |
| 119 } | 119 } |
| 120 | 120 |
| 121 return true; | 121 return true; |
| 122 } | 122 } |
| OLD | NEW |