• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2   * Copyright (C) 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 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  
26  #ifndef ShareableBitmap_h
27  #define ShareableBitmap_h
28  
29  #include "SharedMemory.h"
30  #include <WebCore/IntRect.h>
31  #include <wtf/PassOwnPtr.h>
32  #include <wtf/PassRefPtr.h>
33  #include <wtf/RefCounted.h>
34  #include <wtf/RefPtr.h>
35  
36  #if USE(CG)
37  #include <wtf/RetainPtr.h>
38  #endif
39  
40  namespace WebCore {
41      class GraphicsContext;
42  }
43  
44  namespace WebKit {
45  
46  class ShareableBitmap : public RefCounted<ShareableBitmap> {
47  public:
48      enum Flag {
49          SupportsAlpha = 1 << 0,
50      };
51      typedef unsigned Flags;
52  
53      class Handle {
54          WTF_MAKE_NONCOPYABLE(Handle);
55      public:
56          Handle();
57  
isNull()58          bool isNull() const { return m_handle.isNull(); }
59  
60          void encode(CoreIPC::ArgumentEncoder*) const;
61          static bool decode(CoreIPC::ArgumentDecoder*, Handle&);
62  
63      private:
64          friend class ShareableBitmap;
65  
66          mutable SharedMemory::Handle m_handle;
67          WebCore::IntSize m_size;
68          Flags m_flags;
69      };
70  
71      // Create a shareable bitmap that uses malloced memory.
72      static PassRefPtr<ShareableBitmap> create(const WebCore::IntSize&, Flags);
73  
74      // Create a shareable bitmap whose backing memory can be shared with another process.
75      static PassRefPtr<ShareableBitmap> createShareable(const WebCore::IntSize&, Flags);
76  
77      // Create a shareable bitmap from an already existing shared memory block.
78      static PassRefPtr<ShareableBitmap> create(const WebCore::IntSize&, Flags, PassRefPtr<SharedMemory>);
79  
80      // Create a shareable bitmap from a handle.
81      static PassRefPtr<ShareableBitmap> create(const Handle&);
82  
83      // Create a handle.
84      bool createHandle(Handle&);
85  
86      ~ShareableBitmap();
87  
size()88      const WebCore::IntSize& size() const { return m_size; }
bounds()89      WebCore::IntRect bounds() const { return WebCore::IntRect(WebCore::IntPoint(), size()); }
90  
91      bool resize(const WebCore::IntSize& size);
92  
93      // Create a graphics context that can be used to paint into the backing store.
94      PassOwnPtr<WebCore::GraphicsContext> createGraphicsContext();
95  
96      // Paint the backing store into the given context.
97      void paint(WebCore::GraphicsContext&, const WebCore::IntPoint& dstPoint, const WebCore::IntRect& srcRect);
98  
isBackedBySharedMemory()99      bool isBackedBySharedMemory() const { return m_sharedMemory; }
100  
101  #if USE(CG)
102      // This creates a copied CGImageRef (most likely a copy-on-write) of the shareable bitmap.
103      RetainPtr<CGImageRef> makeCGImageCopy();
104  
105      // This creates a CGImageRef that directly references the shared bitmap data.
106      // This is only safe to use when we know that the contents of the shareable bitmap won't change.
107      RetainPtr<CGImageRef> makeCGImage();
108  #endif
109  
110  private:
111      ShareableBitmap(const WebCore::IntSize&, Flags, void*);
112      ShareableBitmap(const WebCore::IntSize&, Flags, PassRefPtr<SharedMemory>);
113  
numBytesForSize(const WebCore::IntSize & size)114      static size_t numBytesForSize(const WebCore::IntSize& size) { return size.width() * size.height() * 4; }
115  
116  #if USE(CG)
117      static void releaseBitmapContextData(void* typelessBitmap, void* typelessData);
118      static void releaseDataProviderData(void* typelessBitmap, const void* typelessData, size_t);
119  #endif
120  
121      void* data() const;
sizeInBytes()122      size_t sizeInBytes() const { return numBytesForSize(m_size); }
123  
124      WebCore::IntSize m_size;
125      Flags m_flags;
126  
127      // If the shareable bitmap is backed by shared memory, this points to the shared memory object.
128      RefPtr<SharedMemory> m_sharedMemory;
129  
130      // If the shareable bitmap is backed by fastMalloced memory, this points to the data.
131      void* m_data;
132  };
133  
134  } // namespace WebKit
135  
136  #endif // ShareableBitmap_h
137