Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2016 Google Inc. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license that can be | |
| 5 * found in the LICENSE file. | |
| 6 */ | |
| 7 | |
| 8 #include "SkImageGeneratorCG.h" | |
| 9 #include "SkUnpremultiply.h" | |
| 10 | |
| 11 #ifdef SK_BUILD_FOR_MAC | |
| 12 #include <ApplicationServices/ApplicationServices.h> | |
| 13 #endif | |
| 14 | |
| 15 #ifdef SK_BUILD_FOR_IOS | |
| 16 #include <CoreGraphics/CoreGraphics.h> | |
| 17 #include <ImageIO/ImageIO.h> | |
| 18 #include <MobileCoreServices/MobileCoreServices.h> | |
| 19 #endif | |
| 20 | |
| 21 static CGImageSourceRef data_to_CGImageSrc(SkData* data) { | |
| 22 CGDataProviderRef cgData = CGDataProviderCreateWithData(data, data->data(), data->size(), | |
| 23 nullptr); | |
| 24 if (!cgData) { | |
| 25 return nullptr; | |
| 26 } | |
| 27 CGImageSourceRef imageSrc = CGImageSourceCreateWithDataProvider(cgData, 0); | |
| 28 CGDataProviderRelease(cgData); | |
| 29 return imageSrc; | |
| 30 } | |
| 31 | |
| 32 SkImageGenerator* SkImageGeneratorCG::NewFromEncodedCG(SkData* data) { | |
| 33 CGImageSourceRef imageSrc = data_to_CGImageSrc(data); | |
| 34 if (!imageSrc) { | |
| 35 return nullptr; | |
| 36 } | |
| 37 SkAutoTCallVProc<const void, CFRelease> autoImageSrc(imageSrc); | |
|
scroggo
2016/03/07 13:04:30
Why do you treat this as a const void instead of a
msarett
2016/03/08 23:20:18
CFRelease must be used to free the CGImageSrcRef,
| |
| 38 | |
| 39 CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSrc, 0, nullptr); | |
| 40 if (!properties) { | |
| 41 return nullptr; | |
| 42 } | |
| 43 | |
| 44 CFNumberRef widthRef = (CFNumberRef) (CFDictionaryGetValue(properties, | |
| 45 kCGImagePropertyPixelWidth)); | |
| 46 CFNumberRef heightRef = (CFNumberRef) (CFDictionaryGetValue(properties, | |
| 47 kCGImagePropertyPixelHeight)); | |
| 48 bool hasAlpha = (bool) (CFDictionaryGetValue(properties, | |
| 49 kCGImagePropertyHasAlpha)); | |
| 50 | |
| 51 int width, height; | |
| 52 CFNumberGetValue(widthRef, kCFNumberIntType, &width); | |
|
scroggo
2016/03/07 13:04:30
It looks like these return whether or not they suc
msarett
2016/03/08 23:20:17
Done.
| |
| 53 CFNumberGetValue(heightRef, kCFNumberIntType, &height); | |
| 54 | |
| 55 SkAlphaType alphaType = hasAlpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType ; | |
| 56 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, alphaT ype); | |
| 57 | |
| 58 // FIXME: We have the opportunity to extract color space information here, | |
| 59 // though I think it makes sense to wait until we understand how | |
| 60 // we want to communicate it to the generator. | |
| 61 | |
| 62 return new SkImageGeneratorCG(info, autoImageSrc.detach(), data); | |
| 63 } | |
| 64 | |
| 65 SkImageGeneratorCG::SkImageGeneratorCG(const SkImageInfo& info, const void* imag eSrc, SkData* data) | |
| 66 : INHERITED(info) | |
| 67 , fImageSrc(imageSrc) | |
| 68 , fData(SkRef(data)) | |
| 69 {} | |
| 70 | |
| 71 SkData* SkImageGeneratorCG::onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) { | |
| 72 return SkRef(fData.get()); | |
| 73 } | |
| 74 | |
| 75 bool SkImageGeneratorCG::onGetPixels(const SkImageInfo& info, void* pixels, size _t rowBytes, | |
| 76 SkPMColor ctable[], int* ctableCount) { | |
| 77 if (kN32_SkColorType != info.colorType()) { | |
|
scroggo
2016/03/07 13:04:30
FIXME: Support other types?
msarett
2016/03/08 23:20:17
Done.
| |
| 78 return false; | |
| 79 } | |
| 80 | |
| 81 switch (info.alphaType()) { | |
| 82 case kOpaque_SkAlphaType: | |
| 83 if (kOpaque_SkAlphaType != this->getInfo().alphaType()) { | |
| 84 return false; | |
| 85 } | |
| 86 break; | |
| 87 case kPremul_SkAlphaType: | |
| 88 break; | |
| 89 default: | |
| 90 return false; | |
| 91 } | |
| 92 | |
| 93 CGImageRef image = CGImageSourceCreateImageAtIndex((CGImageSourceRef) fImage Src.get(), 0, | |
| 94 nullptr); | |
| 95 if (!image) { | |
| 96 return false; | |
| 97 } | |
| 98 SkAutoTCallVProc<CGImage, CGImageRelease> autoImage(image); | |
| 99 | |
| 100 // FIXME: Using this function (as opposed to swizzling ourselves) greatly | |
| 101 // restricts the color and alpha types that we support. If we | |
|
scroggo
2016/03/07 13:04:30
I'd be curious about the performance difference, t
msarett
2016/03/08 23:20:18
Me too :). Adding to the FIXME.
| |
| 102 // swizzle ourselves, we can add support for: | |
| 103 // kUnpremul_SkAlphaType | |
| 104 // 16-bit per component RGBA | |
| 105 // kGray_8_SkColorType | |
| 106 // kIndex_8_SkColorType | |
| 107 if (!SkCopyPixelsFromCGImage(info, rowBytes, pixels, image)) { | |
| 108 return false; | |
| 109 } | |
| 110 | |
| 111 return true; | |
| 112 } | |
| OLD | NEW |