diff options
author | msarett <msarett@google.com> | 2016-03-09 14:20:58 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-03-09 14:20:58 -0800 |
commit | 1897631ebe417631ea7a046a2eb0995ab9d04539 (patch) | |
tree | 766fb1f25096283de72e39d0754f434525b5683e /src/ports | |
parent | beba90864ae4645b9f4f7a47065f567862ada12c (diff) |
Add an SkImageGeneratorCG
This will serve as a replacement for SkImageDecoder_CG.
BUG=skia:4914
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1718273004
Review URL: https://codereview.chromium.org/1718273004
Diffstat (limited to 'src/ports')
-rw-r--r-- | src/ports/SkImageGeneratorCG.cpp | 123 | ||||
-rw-r--r-- | src/ports/SkImageGeneratorCG.h | 43 |
2 files changed, 166 insertions, 0 deletions
diff --git a/src/ports/SkImageGeneratorCG.cpp b/src/ports/SkImageGeneratorCG.cpp new file mode 100644 index 0000000000..cf00ea703d --- /dev/null +++ b/src/ports/SkImageGeneratorCG.cpp @@ -0,0 +1,123 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkImageGeneratorCG.h" +#include "SkUnpremultiply.h" + +#ifdef SK_BUILD_FOR_MAC +#include <ApplicationServices/ApplicationServices.h> +#endif + +#ifdef SK_BUILD_FOR_IOS +#include <CoreGraphics/CoreGraphics.h> +#include <ImageIO/ImageIO.h> +#include <MobileCoreServices/MobileCoreServices.h> +#endif + +static CGImageSourceRef data_to_CGImageSrc(SkData* data) { + CGDataProviderRef cgData = CGDataProviderCreateWithData(data, data->data(), data->size(), + nullptr); + if (!cgData) { + return nullptr; + } + CGImageSourceRef imageSrc = CGImageSourceCreateWithDataProvider(cgData, 0); + CGDataProviderRelease(cgData); + return imageSrc; +} + +SkImageGenerator* SkImageGeneratorCG::NewFromEncodedCG(SkData* data) { + CGImageSourceRef imageSrc = data_to_CGImageSrc(data); + if (!imageSrc) { + return nullptr; + } + + // Make sure we call CFRelease to free the imageSrc. Since CFRelease actually takes + // a const void*, we must cast the imageSrc to a const void*. + SkAutoTCallVProc<const void, CFRelease> autoImageSrc(imageSrc); + + CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSrc, 0, nullptr); + if (!properties) { + return nullptr; + } + + CFNumberRef widthRef = (CFNumberRef) (CFDictionaryGetValue(properties, + kCGImagePropertyPixelWidth)); + CFNumberRef heightRef = (CFNumberRef) (CFDictionaryGetValue(properties, + kCGImagePropertyPixelHeight)); + if (nullptr == widthRef || nullptr == heightRef) { + return nullptr; + } + bool hasAlpha = (bool) (CFDictionaryGetValue(properties, + kCGImagePropertyHasAlpha)); + + int width, height; + if (!CFNumberGetValue(widthRef, kCFNumberIntType, &width) || + !CFNumberGetValue(heightRef, kCFNumberIntType, &height)) { + return nullptr; + } + + SkAlphaType alphaType = hasAlpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType; + SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, alphaType); + + // FIXME: We have the opportunity to extract color space information here, + // though I think it makes sense to wait until we understand how + // we want to communicate it to the generator. + + return new SkImageGeneratorCG(info, autoImageSrc.detach(), data); +} + +SkImageGeneratorCG::SkImageGeneratorCG(const SkImageInfo& info, const void* imageSrc, SkData* data) + : INHERITED(info) + , fImageSrc(imageSrc) + , fData(SkRef(data)) +{} + +SkData* SkImageGeneratorCG::onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) { + return SkRef(fData.get()); +} + +bool SkImageGeneratorCG::onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, + SkPMColor ctable[], int* ctableCount) { + if (kN32_SkColorType != info.colorType()) { + // FIXME: Support other colorTypes. + return false; + } + + switch (info.alphaType()) { + case kOpaque_SkAlphaType: + if (kOpaque_SkAlphaType != this->getInfo().alphaType()) { + return false; + } + break; + case kPremul_SkAlphaType: + break; + default: + return false; + } + + CGImageRef image = CGImageSourceCreateImageAtIndex((CGImageSourceRef) fImageSrc.get(), 0, + nullptr); + if (!image) { + return false; + } + SkAutoTCallVProc<CGImage, CGImageRelease> autoImage(image); + + // FIXME: Using this function (as opposed to swizzling ourselves) greatly + // restricts the color and alpha types that we support. If we + // swizzle ourselves, we can add support for: + // kUnpremul_SkAlphaType + // 16-bit per component RGBA + // kGray_8_SkColorType + // kIndex_8_SkColorType + // Additionally, it would be interesting to compare the performance + // of SkSwizzler with CG's built in swizzler. + if (!SkCopyPixelsFromCGImage(info, rowBytes, pixels, image)) { + return false; + } + + return true; +} diff --git a/src/ports/SkImageGeneratorCG.h b/src/ports/SkImageGeneratorCG.h new file mode 100644 index 0000000000..cdfeae02b6 --- /dev/null +++ b/src/ports/SkImageGeneratorCG.h @@ -0,0 +1,43 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkTypes.h" +#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) + +#include "SkCGUtils.h" +#include "SkData.h" +#include "SkImageGenerator.h" +#include "SkTemplates.h" + +class SkImageGeneratorCG : public SkImageGenerator { +public: + /* + * Refs the data if an image generator can be returned. Otherwise does + * not affect the data. + */ + static SkImageGenerator* NewFromEncodedCG(SkData* data); + +protected: + SkData* onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) override; + + bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, SkPMColor ctable[], + int* ctableCount) override; + +private: + /* + * Takes ownership of the imageSrc + * Refs the data + */ + SkImageGeneratorCG(const SkImageInfo& info, const void* imageSrc, SkData* data); + + SkAutoTCallVProc<const void, CFRelease> fImageSrc; + SkAutoTUnref<SkData> fData; + + typedef SkImageGenerator INHERITED; +}; + +#endif //defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |