• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 Google 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  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef PlatformCanvas_h
27 #define PlatformCanvas_h
28 
29 #include "IntSize.h"
30 #include <stdint.h>
31 #include <wtf/Noncopyable.h>
32 #include <wtf/OwnPtr.h>
33 
34 #if USE(CG)
35 #include <CoreGraphics/CGColorSpace.h>
36 #include <CoreGraphics/CGContext.h>
37 #include <wtf/OwnArrayPtr.h>
38 #include <wtf/RetainPtr.h>
39 #endif
40 
41 #if USE(SKIA)
42 class SkBitmap;
43 class SkCanvas;
44 #endif
45 
46 namespace WebCore {
47 
48 class GraphicsContext;
49 
50 #if USE(SKIA)
51 class PlatformContextSkia;
52 #endif
53 
54 // A 2D buffer of pixels with an associated GraphicsContext.
55 class PlatformCanvas {
56     WTF_MAKE_NONCOPYABLE(PlatformCanvas);
57 public:
58     PlatformCanvas();
59     ~PlatformCanvas();
60 
61     // Scoped lock class to get temporary access to this canvas's pixels.
62     class AutoLocker {
63         WTF_MAKE_NONCOPYABLE(AutoLocker);
64     public:
65         explicit AutoLocker(PlatformCanvas*);
66         ~AutoLocker();
67 
pixels()68         const uint8_t* pixels() const { return m_pixels; }
69     private:
70         PlatformCanvas* m_canvas;
71 #if USE(SKIA)
72         const SkBitmap* m_bitmap;
73 #endif
74         uint8_t* m_pixels;
75     };
76 
77     // Scoped lock class to get temporary access to paint into this canvas.
78     class Painter {
79         WTF_MAKE_NONCOPYABLE(Painter);
80     public:
81         enum TextOption { GrayscaleText, SubpixelText };
82 
83         Painter(PlatformCanvas*, TextOption);
84         ~Painter();
85 
context()86         GraphicsContext* context() const { return m_context.get(); }
87     private:
88         OwnPtr<GraphicsContext> m_context;
89 #if USE(SKIA)
90         OwnPtr<PlatformContextSkia> m_skiaContext;
91 #elif USE(CG)
92         RetainPtr<CGColorSpaceRef> m_colorSpace;
93         RetainPtr<CGContextRef> m_contextCG;
94 #endif
95     };
96 
97     void resize(const IntSize&);
size()98     IntSize size() const { return m_size; }
99 
100 private:
101 #if USE(SKIA)
102     OwnPtr<SkCanvas> m_skiaCanvas;
103 #elif USE(CG)
104     OwnArrayPtr<uint8_t> m_pixelData;
105 #endif
106     IntSize m_size;
107 };
108 
109 } // namespace WebCore
110 
111 #endif
112