1/*
2 *  Copyright (C) 2007-2009 Torch Mobile, Inc. All rights reserved.
3 *  Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com>
4 *
5 *  This library is free software; you can redistribute it and/or
6 *  modify it under the terms of the GNU Library General Public
7 *  License as published by the Free Software Foundation; either
8 *  version 2 of the License, or (at your option) any later version.
9 *
10 *  This library is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 *  Library General Public License for more details.
14 *
15 *  You should have received a copy of the GNU Library General Public License
16 *  along with this library; see the file COPYING.LIB.  If not, write to
17 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 *  Boston, MA 02110-1301, USA.
19 */
20
21#ifndef SharedBitmap_h
22#define SharedBitmap_h
23
24#include "AffineTransform.h"
25#include "BitmapInfo.h"
26#include "ColorSpace.h"
27#include "GraphicsTypes.h"
28#include <memory>
29#include <wtf/PassOwnPtr.h>
30#include <wtf/PassRefPtr.h>
31#include <wtf/RefCounted.h>
32#include <wtf/Vector.h>
33#include <wtf/win/GDIObject.h>
34
35namespace WebCore {
36
37class FloatPoint;
38class FloatRect;
39class GraphicsContext;
40class IntRect;
41class IntSize;
42class TransformationMatrix;
43
44class SharedBitmap: public RefCounted<SharedBitmap> {
45public:
46    ~SharedBitmap();
47    static PassRefPtr<SharedBitmap> create(const IntSize&, BitmapInfo::BitCount = BitmapInfo::BitCount32, bool initPixels = true);
48    static PassRefPtr<SharedBitmap> create(const Vector<unsigned>&, const IntSize&, bool hasAlpha = true);
49
50    const BitmapInfo& bitmapInfo() const { return m_bmpInfo; }
51    void* bytes() { return m_pixels; }
52    const void* bytes() const { return m_pixels; }
53    unsigned width() const { return m_bmpInfo.width(); }
54    unsigned height() const { return m_bmpInfo.height(); }
55    unsigned validHeight() const { return m_validHeight; }
56    void setValidHeight(unsigned validHeight) { m_validHeight = validHeight; }
57    void resetPixels(bool black = false);
58    void clearPixels(const IntRect& r);
59    bool locked() const { return m_locked; }
60    void lock() { m_locked = true; }
61    void unlock() { m_locked = false; }
62    bool freeMemory();
63    bool is16bit() const { return m_bmpInfo.is16bit(); }
64    bool is32bit() const { return m_bmpInfo.is32bit(); }
65    bool to16bit();
66    bool hasAlpha() const { return m_hasAlpha; }
67    void setHasAlpha(bool alpha) { m_hasAlpha = alpha; }
68    bool ensureHandle();
69    HBITMAP getHandle() { return m_hbitmap.get(); }
70    GDIObject<HBITMAP> createHandle(void** pixels, BitmapInfo* bmpInfo, int h = -1, bool use16bit = true) const;
71    bool usesTransparentColor() const { return m_usesTransparentColor; }
72    COLORREF transparentColor() const { return m_transparentColor; }
73    void setTransparentColor(COLORREF c)
74    {
75        m_usesTransparentColor = true;
76        m_transparentColor = c;
77    }
78    bool canUseDIBits() const { return !hasAlpha() && !usesTransparentColor(); }
79
80    GDIObject<HBITMAP> clipBitmap(const IntRect&, bool useAlpha, BitmapInfo&, void*& pixels);
81
82    PassRefPtr<SharedBitmap> clipBitmap(const IntRect& rect, bool useAlpha);
83
84    void draw(GraphicsContext* ctxt, const IntRect& dstRect, const IntRect& srcRect, ColorSpace styleColorSpace, CompositeOperator compositeOp, BlendMode blendMode);
85    void drawPattern(GraphicsContext* ctxt, const FloatRect& tileRectIn, const AffineTransform& patternTransform,
86                    const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator op, const FloatRect& destRect, const IntSize& origSourceSize);
87    void draw(HDC, const IntRect& dstRect, const IntRect& srcRect, CompositeOperator compositeOp, BlendMode blendMode);
88    void drawPattern(HDC, const AffineTransform&, const FloatRect& tileRectIn, const AffineTransform& patternTransform,
89                    const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator op, const FloatRect& destRect, const IntSize& origSourceSize);
90
91    class DCProvider {
92    public:
93        virtual HDC getDC(SharedBitmap*, unsigned*);
94        virtual void releaseDC(SharedBitmap*, HDC, unsigned);
95    };
96
97    static DCProvider* s_dcProvider;
98
99    HDC getDC(unsigned* key1) { return s_dcProvider->getDC(this, key1); }
100    void releaseDC(HDC hdc, unsigned key1) { s_dcProvider->releaseDC(this, hdc, key1); }
101
102    class DCHolder {
103    public:
104        DCHolder(SharedBitmap* bmp = 0) { setInternal(bmp); }
105        ~DCHolder() { clearInternal(); }
106        void set(SharedBitmap* bmp = 0)
107        {
108            clearInternal();
109            setInternal(bmp);
110        }
111        HDC get() const { return m_hdc; }
112    private:
113        DCHolder& operator=(const DCHolder&);
114        DCHolder(const DCHolder&);
115        void clearInternal()
116        {
117            if (m_hdc)
118                m_bitmap->releaseDC(m_hdc, m_key);
119        }
120        void setInternal(SharedBitmap* bmp)
121        {
122            m_bitmap = bmp;
123            m_hdc = bmp ? bmp->getDC(&m_key) : 0;
124        }
125        SharedBitmap* m_bitmap;
126        HDC m_hdc;
127        unsigned m_key;
128    };
129
130private:
131    SharedBitmap(const IntSize&, BitmapInfo::BitCount, bool initPixels);
132    BitmapInfo m_bmpInfo;
133    GDIObject<HBITMAP> m_hbitmap;
134    void* m_pixels;
135    std::unique_ptr<unsigned[]> m_pixelData;
136    COLORREF m_transparentColor;
137    int m_validHeight;
138    bool m_locked;
139    bool m_usesTransparentColor;
140    bool m_hasAlpha;
141};
142
143} // namespace WebCore
144
145#endif // SharedBitmap_h
146