1/* 2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 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 26#include "config.h" 27#include "GraphicsContext.h" 28 29#if USE(CG) 30#include "GraphicsContextPlatformPrivateCG.h" 31#elif USE(CAIRO) 32#include "GraphicsContextPlatformPrivateCairo.h" 33#endif 34 35#include "AffineTransform.h" 36#include "BitmapInfo.h" 37#include "TransformationMatrix.h" 38#include "NotImplemented.h" 39#include "Path.h" 40#include <wtf/MathExtras.h> 41 42using namespace std; 43 44namespace WebCore { 45 46static void fillWithClearColor(HBITMAP bitmap) 47{ 48 BITMAP bmpInfo; 49 GetObject(bitmap, sizeof(bmpInfo), &bmpInfo); 50 int bufferSize = bmpInfo.bmWidthBytes * bmpInfo.bmHeight; 51 memset(bmpInfo.bmBits, 0, bufferSize); 52} 53 54#if PLATFORM(WIN) 55void GraphicsContext::setShouldIncludeChildWindows(bool include) 56{ 57 m_data->m_shouldIncludeChildWindows = include; 58} 59 60bool GraphicsContext::shouldIncludeChildWindows() const 61{ 62 return m_data->m_shouldIncludeChildWindows; 63} 64 65GraphicsContext::WindowsBitmap::WindowsBitmap(HDC hdc, const IntSize& size) 66 : m_hdc(0) 67{ 68 BitmapInfo bitmapInfo = BitmapInfo::create(size); 69 70 void* storage = 0; 71 m_bitmap = CreateDIBSection(0, &bitmapInfo, DIB_RGB_COLORS, &storage, 0, 0); 72 if (!m_bitmap) 73 return; 74 75 m_hdc = CreateCompatibleDC(hdc); 76 SelectObject(m_hdc, m_bitmap); 77 78 m_pixelData.initialize(m_bitmap); 79 80 ASSERT(storage == m_pixelData.buffer()); 81 82 SetGraphicsMode(m_hdc, GM_ADVANCED); 83} 84 85GraphicsContext::WindowsBitmap::~WindowsBitmap() 86{ 87 if (!m_bitmap) 88 return; 89 90 DeleteDC(m_hdc); 91 DeleteObject(m_bitmap); 92} 93 94PassOwnPtr<GraphicsContext::WindowsBitmap> GraphicsContext::createWindowsBitmap(const IntSize& size) 95{ 96 return adoptPtr(new WindowsBitmap(m_data->m_hdc, size)); 97} 98#endif 99 100HDC GraphicsContext::getWindowsContext(const IntRect& dstRect, bool supportAlphaBlend, bool mayCreateBitmap) 101{ 102 // FIXME: Should a bitmap be created also when a shadow is set? 103 if (mayCreateBitmap && (!m_data->m_hdc || isInTransparencyLayer())) { 104 if (dstRect.isEmpty()) 105 return 0; 106 107 // Create a bitmap DC in which to draw. 108 BitmapInfo bitmapInfo = BitmapInfo::create(dstRect.size()); 109 110 void* pixels = 0; 111 HBITMAP bitmap = ::CreateDIBSection(NULL, &bitmapInfo, DIB_RGB_COLORS, &pixels, 0, 0); 112 if (!bitmap) 113 return 0; 114 115 HDC bitmapDC = ::CreateCompatibleDC(m_data->m_hdc); 116 ::SelectObject(bitmapDC, bitmap); 117 118 // Fill our buffer with clear if we're going to alpha blend. 119 if (supportAlphaBlend) 120 fillWithClearColor(bitmap); 121 122 // Make sure we can do world transforms. 123 SetGraphicsMode(bitmapDC, GM_ADVANCED); 124 125 // Apply a translation to our context so that the drawing done will be at (0,0) of the bitmap. 126 XFORM xform = TransformationMatrix().translate(-dstRect.x(), -dstRect.y()); 127 128 ::SetWorldTransform(bitmapDC, &xform); 129 130 return bitmapDC; 131 } 132 133 m_data->flush(); 134 m_data->save(); 135 return m_data->m_hdc; 136} 137 138#if PLATFORM(WIN) 139void GraphicsContextPlatformPrivate::save() 140{ 141 if (!m_hdc) 142 return; 143 SaveDC(m_hdc); 144} 145 146void GraphicsContextPlatformPrivate::restore() 147{ 148 if (!m_hdc) 149 return; 150 RestoreDC(m_hdc, -1); 151} 152 153void GraphicsContextPlatformPrivate::clip(const FloatRect& clipRect) 154{ 155 if (!m_hdc) 156 return; 157 IntersectClipRect(m_hdc, clipRect.x(), clipRect.y(), clipRect.maxX(), clipRect.maxY()); 158} 159 160void GraphicsContextPlatformPrivate::clip(const Path&) 161{ 162 notImplemented(); 163} 164 165void GraphicsContextPlatformPrivate::scale(const FloatSize& size) 166{ 167 if (!m_hdc) 168 return; 169 170 XFORM xform = TransformationMatrix().scaleNonUniform(size.width(), size.height()); 171 ModifyWorldTransform(m_hdc, &xform, MWT_LEFTMULTIPLY); 172} 173 174static const double deg2rad = 0.017453292519943295769; // pi/180 175 176void GraphicsContextPlatformPrivate::rotate(float degreesAngle) 177{ 178 XFORM xform = TransformationMatrix().rotate(degreesAngle); 179 ModifyWorldTransform(m_hdc, &xform, MWT_LEFTMULTIPLY); 180} 181 182void GraphicsContextPlatformPrivate::translate(float x , float y) 183{ 184 if (!m_hdc) 185 return; 186 187 XFORM xform = TransformationMatrix().translate(x, y); 188 ModifyWorldTransform(m_hdc, &xform, MWT_LEFTMULTIPLY); 189} 190 191void GraphicsContextPlatformPrivate::concatCTM(const AffineTransform& transform) 192{ 193 if (!m_hdc) 194 return; 195 196 XFORM xform = transform.toTransformationMatrix(); 197 ModifyWorldTransform(m_hdc, &xform, MWT_LEFTMULTIPLY); 198} 199 200void GraphicsContextPlatformPrivate::setCTM(const AffineTransform& transform) 201{ 202 if (!m_hdc) 203 return; 204 205 XFORM xform = transform.toTransformationMatrix(); 206 SetWorldTransform(m_hdc, &xform); 207} 208#endif 209 210} 211