/src/3rdparty/webkit/Source/WebCore/platform/graphics/win/GraphicsContextCGWin.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 257 lines · 149 code · 51 blank · 57 comment · 19 complexity · f7804c0bf505d852aac83209546ba111 MD5 · raw file

  1. /*
  2. * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2010 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 COMPUTER, INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "GraphicsContextCG.h"
  27. #include "AffineTransform.h"
  28. #include "Path.h"
  29. #include <CoreGraphics/CGBitmapContext.h>
  30. #include <WebKitSystemInterface/WebKitSystemInterface.h>
  31. #include "GraphicsContextPlatformPrivateCG.h"
  32. using namespace std;
  33. namespace WebCore {
  34. static CGContextRef CGContextWithHDC(HDC hdc, bool hasAlpha)
  35. {
  36. HBITMAP bitmap = static_cast<HBITMAP>(GetCurrentObject(hdc, OBJ_BITMAP));
  37. DIBPixelData pixelData(bitmap);
  38. // FIXME: We can get here because we asked for a bitmap that is too big
  39. // when we have a tiled layer and we're compositing. In that case
  40. // bmBitsPixel will be 0. This seems to be benign, so for now we will
  41. // exit gracefully and look at it later:
  42. // https://bugs.webkit.org/show_bug.cgi?id=52041
  43. // ASSERT(bitmapBits.bitsPerPixel() == 32);
  44. if (pixelData.bitsPerPixel() != 32)
  45. return 0;
  46. CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Little | (hasAlpha ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst);
  47. CGContextRef context = CGBitmapContextCreate(pixelData.buffer(), pixelData.size().width(), pixelData.size().height(), 8,
  48. pixelData.bytesPerRow(), deviceRGBColorSpaceRef(), bitmapInfo);
  49. // Flip coords
  50. CGContextTranslateCTM(context, 0, pixelData.size().height());
  51. CGContextScaleCTM(context, 1, -1);
  52. // Put the HDC In advanced mode so it will honor affine transforms.
  53. SetGraphicsMode(hdc, GM_ADVANCED);
  54. return context;
  55. }
  56. GraphicsContext::GraphicsContext(HDC hdc, bool hasAlpha)
  57. : m_updatingControlTints(false)
  58. {
  59. platformInit(hdc, hasAlpha);
  60. }
  61. void GraphicsContext::platformInit(HDC hdc, bool hasAlpha)
  62. {
  63. m_data = new GraphicsContextPlatformPrivate(CGContextWithHDC(hdc, hasAlpha));
  64. CGContextRelease(m_data->m_cgContext.get());
  65. m_data->m_hdc = hdc;
  66. setPaintingDisabled(!m_data->m_cgContext);
  67. if (m_data->m_cgContext) {
  68. // Make sure the context starts in sync with our state.
  69. setPlatformFillColor(fillColor(), ColorSpaceDeviceRGB);
  70. setPlatformStrokeColor(strokeColor(), ColorSpaceDeviceRGB);
  71. }
  72. }
  73. // FIXME: Is it possible to merge getWindowsContext and createWindowsBitmap into a single API
  74. // suitable for all clients?
  75. void GraphicsContext::releaseWindowsContext(HDC hdc, const IntRect& dstRect, bool supportAlphaBlend, bool mayCreateBitmap)
  76. {
  77. bool createdBitmap = mayCreateBitmap && (!m_data->m_hdc || inTransparencyLayer());
  78. if (!createdBitmap) {
  79. m_data->restore();
  80. return;
  81. }
  82. if (dstRect.isEmpty())
  83. return;
  84. OwnPtr<HBITMAP> bitmap = adoptPtr(static_cast<HBITMAP>(GetCurrentObject(hdc, OBJ_BITMAP)));
  85. DIBPixelData pixelData(bitmap.get());
  86. ASSERT(pixelData.bitsPerPixel() == 32);
  87. CGContextRef bitmapContext = CGBitmapContextCreate(pixelData.buffer(), pixelData.size().width(), pixelData.size().height(), 8,
  88. pixelData.bytesPerRow(), deviceRGBColorSpaceRef(), kCGBitmapByteOrder32Little |
  89. (supportAlphaBlend ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst));
  90. CGImageRef image = CGBitmapContextCreateImage(bitmapContext);
  91. CGContextDrawImage(m_data->m_cgContext.get(), dstRect, image);
  92. // Delete all our junk.
  93. CGImageRelease(image);
  94. CGContextRelease(bitmapContext);
  95. ::DeleteDC(hdc);
  96. }
  97. void GraphicsContext::drawWindowsBitmap(WindowsBitmap* image, const IntPoint& point)
  98. {
  99. // FIXME: Creating CFData is non-optimal, but needed to avoid crashing when printing. Ideally we should
  100. // make a custom CGDataProvider that controls the WindowsBitmap lifetime. see <rdar://6394455>
  101. RetainPtr<CFDataRef> imageData(AdoptCF, CFDataCreate(kCFAllocatorDefault, image->buffer(), image->bufferLength()));
  102. RetainPtr<CGDataProviderRef> dataProvider(AdoptCF, CGDataProviderCreateWithCFData(imageData.get()));
  103. RetainPtr<CGImageRef> cgImage(AdoptCF, CGImageCreate(image->size().width(), image->size().height(), 8, 32, image->bytesPerRow(), deviceRGBColorSpaceRef(),
  104. kCGBitmapByteOrder32Little | kCGImageAlphaFirst, dataProvider.get(), 0, true, kCGRenderingIntentDefault));
  105. CGContextDrawImage(m_data->m_cgContext.get(), CGRectMake(point.x(), point.y(), image->size().width(), image->size().height()), cgImage.get());
  106. }
  107. void GraphicsContext::drawFocusRing(const Path& path, int width, int offset, const Color& color)
  108. {
  109. // FIXME: implement
  110. }
  111. // FIXME: This is nearly identical to the GraphicsContext::drawFocusRing function in GraphicsContextMac.mm.
  112. // The code could move to GraphicsContextCG.cpp and be shared.
  113. void GraphicsContext::drawFocusRing(const Vector<IntRect>& rects, int width, int offset, const Color& color)
  114. {
  115. if (paintingDisabled())
  116. return;
  117. float radius = (width - 1) / 2.0f;
  118. offset += radius;
  119. CGColorRef colorRef = color.isValid() ? cachedCGColor(color, ColorSpaceDeviceRGB) : 0;
  120. CGMutablePathRef focusRingPath = CGPathCreateMutable();
  121. unsigned rectCount = rects.size();
  122. for (unsigned i = 0; i < rectCount; i++)
  123. CGPathAddRect(focusRingPath, 0, CGRectInset(rects[i], -offset, -offset));
  124. CGContextRef context = platformContext();
  125. CGContextSaveGState(context);
  126. CGContextBeginPath(context);
  127. CGContextAddPath(context, focusRingPath);
  128. wkDrawFocusRing(context, colorRef, radius);
  129. CGPathRelease(focusRingPath);
  130. CGContextRestoreGState(context);
  131. }
  132. // Pulled from GraphicsContextCG
  133. static void setCGStrokeColor(CGContextRef context, const Color& color)
  134. {
  135. CGFloat red, green, blue, alpha;
  136. color.getRGBA(red, green, blue, alpha);
  137. CGContextSetRGBStrokeColor(context, red, green, blue, alpha);
  138. }
  139. static const Color& spellingPatternColor() {
  140. static const Color spellingColor(255, 0, 0);
  141. return spellingColor;
  142. }
  143. static const Color& grammarPatternColor() {
  144. static const Color grammarColor(0, 128, 0);
  145. return grammarColor;
  146. }
  147. void GraphicsContext::drawLineForTextChecking(const FloatPoint& point, float width, TextCheckingLineStyle style)
  148. {
  149. if (paintingDisabled())
  150. return;
  151. if (style != TextCheckingSpellingLineStyle && style != TextCheckingGrammarLineStyle)
  152. return;
  153. // These are the same for misspelling or bad grammar
  154. const int patternHeight = 3; // 3 rows
  155. ASSERT(cMisspellingLineThickness == patternHeight);
  156. const int patternWidth = 4; // 4 pixels
  157. ASSERT(patternWidth == cMisspellingLinePatternWidth);
  158. // Make sure to draw only complete dots.
  159. // NOTE: Code here used to shift the underline to the left and increase the width
  160. // to make sure everything gets underlined, but that results in drawing out of
  161. // bounds (e.g. when at the edge of a view) and could make it appear that the
  162. // space between adjacent misspelled words was underlined.
  163. // allow slightly more considering that the pattern ends with a transparent pixel
  164. float widthMod = fmodf(width, patternWidth);
  165. if (patternWidth - widthMod > cMisspellingLinePatternGapWidth)
  166. width -= widthMod;
  167. // Draw the underline
  168. CGContextRef context = platformContext();
  169. CGContextSaveGState(context);
  170. const Color& patternColor = style == TextCheckingGrammarLineStyle ? grammarPatternColor() : spellingPatternColor();
  171. setCGStrokeColor(context, patternColor);
  172. wkSetPatternPhaseInUserSpace(context, point);
  173. CGContextSetBlendMode(context, kCGBlendModeNormal);
  174. // 3 rows, each offset by half a pixel for blending purposes
  175. const CGPoint upperPoints [] = {{point.x(), point.y() + patternHeight - 2.5 }, {point.x() + width, point.y() + patternHeight - 2.5}};
  176. const CGPoint middlePoints [] = {{point.x(), point.y() + patternHeight - 1.5 }, {point.x() + width, point.y() + patternHeight - 1.5}};
  177. const CGPoint lowerPoints [] = {{point.x(), point.y() + patternHeight - 0.5 }, {point.x() + width, point.y() + patternHeight - 0.5 }};
  178. // Dash lengths for the top and bottom of the error underline are the same.
  179. // These are magic.
  180. static const float edge_dash_lengths[] = {2.0f, 2.0f};
  181. static const float middle_dash_lengths[] = {2.76f, 1.24f};
  182. static const float edge_offset = -(edge_dash_lengths[1] - 1.0f) / 2.0f;
  183. static const float middle_offset = -(middle_dash_lengths[1] - 1.0f) / 2.0f;
  184. // Line opacities. Once again, these are magic.
  185. const float upperOpacity = 0.33f;
  186. const float middleOpacity = 0.75f;
  187. const float lowerOpacity = 0.88f;
  188. //Top line
  189. CGContextSetLineDash(context, edge_offset, edge_dash_lengths, WTF_ARRAY_LENGTH(edge_dash_lengths));
  190. CGContextSetAlpha(context, upperOpacity);
  191. CGContextStrokeLineSegments(context, upperPoints, 2);
  192. // Middle line
  193. CGContextSetLineDash(context, middle_offset, middle_dash_lengths, WTF_ARRAY_LENGTH(middle_dash_lengths));
  194. CGContextSetAlpha(context, middleOpacity);
  195. CGContextStrokeLineSegments(context, middlePoints, 2);
  196. // Bottom line
  197. CGContextSetLineDash(context, edge_offset, edge_dash_lengths, WTF_ARRAY_LENGTH(edge_dash_lengths));
  198. CGContextSetAlpha(context, lowerOpacity);
  199. CGContextStrokeLineSegments(context, lowerPoints, 2);
  200. CGContextRestoreGState(context);
  201. }
  202. void GraphicsContextPlatformPrivate::flush()
  203. {
  204. CGContextFlush(m_cgContext.get());
  205. }
  206. }