ShareableBitmapCG.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2010, 2011 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "ShareableBitmap.h"
  27. #include <WebCore/BitmapImage.h>
  28. #include <WebCore/GraphicsContext.h>
  29. #include <wtf/RetainPtr.h>
  30. #include "CGUtilities.h"
  31. using namespace WebCore;
  32. namespace WebKit {
  33. static CGBitmapInfo bitmapInfo(ShareableBitmap::Flags flags)
  34. {
  35. CGBitmapInfo info = kCGBitmapByteOrder32Host;
  36. if (flags & ShareableBitmap::SupportsAlpha)
  37. info |= kCGImageAlphaPremultipliedFirst;
  38. else
  39. info |= kCGImageAlphaNoneSkipFirst;
  40. return info;
  41. }
  42. PassOwnPtr<GraphicsContext> ShareableBitmap::createGraphicsContext()
  43. {
  44. RetainPtr<CGColorSpaceRef> colorSpace = adoptCF(CGColorSpaceCreateDeviceRGB());
  45. ref(); // Balanced by deref in releaseBitmapContextData.
  46. RetainPtr<CGContextRef> bitmapContext = adoptCF(CGBitmapContextCreateWithData(data(),
  47. m_size.width(), m_size.height(), 8, m_size.width() * 4, colorSpace.get(),
  48. bitmapInfo(m_flags), releaseBitmapContextData, this));
  49. // We want the origin to be in the top left corner so we flip the backing store context.
  50. CGContextTranslateCTM(bitmapContext.get(), 0, m_size.height());
  51. CGContextScaleCTM(bitmapContext.get(), 1, -1);
  52. return adoptPtr(new GraphicsContext(bitmapContext.get()));
  53. }
  54. void ShareableBitmap::paint(WebCore::GraphicsContext& context, const IntPoint& destination, const IntRect& source)
  55. {
  56. paintImage(context.platformContext(), makeCGImageCopy().get(), 1, destination, source);
  57. }
  58. void ShareableBitmap::paint(WebCore::GraphicsContext& context, float scaleFactor, const IntPoint& destination, const IntRect& source)
  59. {
  60. paintImage(context.platformContext(), makeCGImageCopy().get(), scaleFactor, destination, source);
  61. }
  62. RetainPtr<CGImageRef> ShareableBitmap::makeCGImageCopy()
  63. {
  64. OwnPtr<GraphicsContext> graphicsContext = createGraphicsContext();
  65. RetainPtr<CGImageRef> image = adoptCF(CGBitmapContextCreateImage(graphicsContext->platformContext()));
  66. return image;
  67. }
  68. RetainPtr<CGImageRef> ShareableBitmap::makeCGImage()
  69. {
  70. ref(); // Balanced by deref in releaseDataProviderData.
  71. RetainPtr<CGDataProvider> dataProvider = adoptCF(CGDataProviderCreateWithData(this, data(), sizeInBytes(), releaseDataProviderData));
  72. return createCGImage(dataProvider.get());
  73. }
  74. RetainPtr<CGImageRef> ShareableBitmap::createCGImage(CGDataProviderRef dataProvider) const
  75. {
  76. ASSERT_ARG(dataProvider, dataProvider);
  77. RetainPtr<CGColorSpaceRef> colorSpace = adoptCF(CGColorSpaceCreateDeviceRGB());
  78. RetainPtr<CGImageRef> image = adoptCF(CGImageCreate(m_size.width(), m_size.height(), 8, 32, m_size.width() * 4, colorSpace.get(), bitmapInfo(m_flags), dataProvider, 0, false, kCGRenderingIntentDefault));
  79. return image;
  80. }
  81. void ShareableBitmap::releaseBitmapContextData(void* typelessBitmap, void* typelessData)
  82. {
  83. ShareableBitmap* bitmap = static_cast<ShareableBitmap*>(typelessBitmap);
  84. ASSERT_UNUSED(typelessData, bitmap->data() == typelessData);
  85. bitmap->deref(); // Balanced by ref in createGraphicsContext.
  86. }
  87. void ShareableBitmap::releaseDataProviderData(void* typelessBitmap, const void* typelessData, size_t)
  88. {
  89. ShareableBitmap* bitmap = static_cast<ShareableBitmap*>(typelessBitmap);
  90. ASSERT_UNUSED(typelessData, bitmap->data() == typelessData);
  91. bitmap->deref(); // Balanced by ref in createCGImage.
  92. }
  93. PassRefPtr<Image> ShareableBitmap::createImage()
  94. {
  95. RetainPtr<CGImageRef> platformImage = makeCGImage();
  96. if (!platformImage)
  97. return 0;
  98. // BitmapImage::create adopts the CGImageRef that's passed in, which is why we need to leakRef here.
  99. return BitmapImage::create(platformImage.leakRef());
  100. }
  101. } // namespace WebKit