SkImageGeneratorCG.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include "include/codec/SkEncodedOrigin.h"
  8. #include "include/ports/SkImageGeneratorCG.h"
  9. #include "include/private/SkTemplates.h"
  10. #include "include/utils/mac/SkCGUtils.h"
  11. #include "src/core/SkPixmapPriv.h"
  12. #include "src/utils/mac/SkUniqueCFRef.h"
  13. #ifdef SK_BUILD_FOR_MAC
  14. #include <ApplicationServices/ApplicationServices.h>
  15. #endif
  16. #ifdef SK_BUILD_FOR_IOS
  17. #include <CoreGraphics/CoreGraphics.h>
  18. #include <ImageIO/ImageIO.h>
  19. #include <MobileCoreServices/MobileCoreServices.h>
  20. #endif
  21. namespace {
  22. class ImageGeneratorCG : public SkImageGenerator {
  23. public:
  24. ImageGeneratorCG(const SkImageInfo&, SkUniqueCFRef<CGImageSourceRef> imageSrc,
  25. sk_sp<SkData> data, SkEncodedOrigin);
  26. protected:
  27. sk_sp<SkData> onRefEncodedData() override;
  28. bool onGetPixels(const SkImageInfo&, void* pixels, size_t rowBytes, const Options&) override;
  29. private:
  30. const SkUniqueCFRef<CGImageSourceRef> fImageSrc;
  31. const sk_sp<SkData> fData;
  32. const SkEncodedOrigin fOrigin;
  33. typedef SkImageGenerator INHERITED;
  34. };
  35. static SkUniqueCFRef<CGImageSourceRef> data_to_CGImageSrc(SkData* data) {
  36. SkUniqueCFRef<CGDataProviderRef> cgData(
  37. CGDataProviderCreateWithData(data, data->data(), data->size(), nullptr));
  38. if (!cgData) {
  39. return nullptr;
  40. }
  41. return SkUniqueCFRef<CGImageSourceRef>(
  42. CGImageSourceCreateWithDataProvider(cgData.get(), nullptr));
  43. }
  44. } // namespace
  45. std::unique_ptr<SkImageGenerator> SkImageGeneratorCG::MakeFromEncodedCG(sk_sp<SkData> data) {
  46. SkUniqueCFRef<CGImageSourceRef> imageSrc = data_to_CGImageSrc(data.get());
  47. if (!imageSrc) {
  48. return nullptr;
  49. }
  50. SkUniqueCFRef<CFDictionaryRef> properties(
  51. CGImageSourceCopyPropertiesAtIndex(imageSrc.get(), 0, nullptr));
  52. if (!properties) {
  53. return nullptr;
  54. }
  55. CFNumberRef widthRef = static_cast<CFNumberRef>(
  56. CFDictionaryGetValue(properties.get(), kCGImagePropertyPixelWidth));
  57. CFNumberRef heightRef = static_cast<CFNumberRef>(
  58. CFDictionaryGetValue(properties.get(), kCGImagePropertyPixelHeight));
  59. if (nullptr == widthRef || nullptr == heightRef) {
  60. return nullptr;
  61. }
  62. int width, height;
  63. if (!CFNumberGetValue(widthRef , kCFNumberIntType, &width ) ||
  64. !CFNumberGetValue(heightRef, kCFNumberIntType, &height))
  65. {
  66. return nullptr;
  67. }
  68. bool hasAlpha = bool(CFDictionaryGetValue(properties.get(), kCGImagePropertyHasAlpha));
  69. SkAlphaType alphaType = hasAlpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType;
  70. SkImageInfo info = SkImageInfo::MakeS32(width, height, alphaType);
  71. SkEncodedOrigin origin = kDefault_SkEncodedOrigin;
  72. CFNumberRef orientationRef = static_cast<CFNumberRef>(
  73. CFDictionaryGetValue(properties.get(), kCGImagePropertyOrientation));
  74. int originInt;
  75. if (orientationRef && CFNumberGetValue(orientationRef, kCFNumberIntType, &originInt)) {
  76. origin = (SkEncodedOrigin) originInt;
  77. }
  78. if (SkPixmapPriv::ShouldSwapWidthHeight(origin)) {
  79. info = SkPixmapPriv::SwapWidthHeight(info);
  80. }
  81. // FIXME: We have the opportunity to extract color space information here,
  82. // though I think it makes sense to wait until we understand how
  83. // we want to communicate it to the generator.
  84. return std::unique_ptr<SkImageGenerator>(new ImageGeneratorCG(info, std::move(imageSrc),
  85. std::move(data), origin));
  86. }
  87. ImageGeneratorCG::ImageGeneratorCG(const SkImageInfo& info, SkUniqueCFRef<CGImageSourceRef> src,
  88. sk_sp<SkData> data, SkEncodedOrigin origin)
  89. : INHERITED(info)
  90. , fImageSrc(std::move(src))
  91. , fData(std::move(data))
  92. , fOrigin(origin)
  93. {}
  94. sk_sp<SkData> ImageGeneratorCG::onRefEncodedData() {
  95. return fData;
  96. }
  97. bool ImageGeneratorCG::onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
  98. const Options&)
  99. {
  100. if (kN32_SkColorType != info.colorType()) {
  101. // FIXME: Support other colorTypes.
  102. return false;
  103. }
  104. switch (info.alphaType()) {
  105. case kOpaque_SkAlphaType:
  106. if (kOpaque_SkAlphaType != this->getInfo().alphaType()) {
  107. return false;
  108. }
  109. break;
  110. case kPremul_SkAlphaType:
  111. break;
  112. default:
  113. return false;
  114. }
  115. SkUniqueCFRef<CGImageRef> image(CGImageSourceCreateImageAtIndex(fImageSrc.get(), 0, nullptr));
  116. if (!image) {
  117. return false;
  118. }
  119. SkPixmap dst(info, pixels, rowBytes);
  120. auto decode = [&image](const SkPixmap& pm) {
  121. // FIXME: Using SkCopyPixelsFromCGImage (as opposed to swizzling
  122. // ourselves) greatly restricts the color and alpha types that we
  123. // support. If we swizzle ourselves, we can add support for:
  124. // kUnpremul_SkAlphaType
  125. // 16-bit per component RGBA
  126. // kGray_8_SkColorType
  127. // Additionally, it would be interesting to compare the performance
  128. // of SkSwizzler with CG's built in swizzler.
  129. return SkCopyPixelsFromCGImage(pm, image.get());
  130. };
  131. return SkPixmapPriv::Orient(dst, fOrigin, decode);
  132. }