Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / core / SkBitmapDevice.h
1
2 /*
3  * Copyright 2013 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8
9 #ifndef SkBitmapDevice_DEFINED
10 #define SkBitmapDevice_DEFINED
11
12 #include "SkDevice.h"
13
14 ///////////////////////////////////////////////////////////////////////////////
15 class SK_API SkBitmapDevice : public SkBaseDevice {
16 public:
17     SK_DECLARE_INST_COUNT(SkBitmapDevice)
18
19     /**
20      *  Construct a new device with the specified bitmap as its backend. It is
21      *  valid for the bitmap to have no pixels associated with it. In that case,
22      *  any drawing to this device will have no effect.
23     */
24     SkBitmapDevice(const SkBitmap& bitmap);
25
26     /**
27      *  Construct a new device with the specified bitmap as its backend. It is
28      *  valid for the bitmap to have no pixels associated with it. In that case,
29      *  any drawing to this device will have no effect.
30     */
31     SkBitmapDevice(const SkBitmap& bitmap, const SkDeviceProperties& deviceProperties);
32
33     static SkBitmapDevice* Create(const SkImageInfo&,
34                                   const SkDeviceProperties* = NULL);
35
36     /** Return the width of the device (in pixels).
37     */
38     virtual int width() const SK_OVERRIDE { return fBitmap.width(); }
39     /** Return the height of the device (in pixels).
40     */
41     virtual int height() const SK_OVERRIDE { return fBitmap.height(); }
42
43     /** Returns true if the device's bitmap's config treats every pixels as
44         implicitly opaque.
45     */
46     virtual bool isOpaque() const SK_OVERRIDE { return fBitmap.isOpaque(); }
47
48     /** Return the bitmap config of the device's pixels
49     */
50     virtual SkBitmap::Config config() const SK_OVERRIDE { return fBitmap.config(); }
51
52     virtual SkImageInfo imageInfo() const SK_OVERRIDE;
53
54     /**
55      * Return the device's associated gpu render target, or NULL.
56      */
57     virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; }
58
59 protected:
60     /**
61      *  Device may filter the text flags for drawing text here. If it wants to
62      *  make a change to the specified values, it should write them into the
63      *  textflags parameter (output) and return true. If the paint is fine as
64      *  is, then ignore the textflags parameter and return false.
65      *
66      *  The baseclass SkDevice filters based on its depth and blitters.
67      */
68     virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE;
69
70     /** Clears the entire device to the specified color (including alpha).
71      *  Ignores the clip.
72      */
73     virtual void clear(SkColor color) SK_OVERRIDE;
74
75     /** These are called inside the per-device-layer loop for each draw call.
76      When these are called, we have already applied any saveLayer operations,
77      and are handling any looping from the paint, and any effects from the
78      DrawFilter.
79      */
80     virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
81     virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
82                             const SkPoint[], const SkPaint& paint) SK_OVERRIDE;
83     virtual void drawRect(const SkDraw&, const SkRect& r,
84                           const SkPaint& paint) SK_OVERRIDE;
85     virtual void drawOval(const SkDraw&, const SkRect& oval,
86                           const SkPaint& paint) SK_OVERRIDE;
87     virtual void drawRRect(const SkDraw&, const SkRRect& rr,
88                            const SkPaint& paint) SK_OVERRIDE;
89
90     /**
91      *  If pathIsMutable, then the implementation is allowed to cast path to a
92      *  non-const pointer and modify it in place (as an optimization). Canvas
93      *  may do this to implement helpers such as drawOval, by placing a temp
94      *  path on the stack to hold the representation of the oval.
95      *
96      *  If prePathMatrix is not null, it should logically be applied before any
97      *  stroking or other effects. If there are no effects on the paint that
98      *  affect the geometry/rasterization, then the pre matrix can just be
99      *  pre-concated with the current matrix.
100      */
101     virtual void drawPath(const SkDraw&, const SkPath& path,
102                           const SkPaint& paint,
103                           const SkMatrix* prePathMatrix = NULL,
104                           bool pathIsMutable = false) SK_OVERRIDE;
105     virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
106                             const SkMatrix& matrix, const SkPaint& paint) SK_OVERRIDE;
107     virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
108                             int x, int y, const SkPaint& paint) SK_OVERRIDE;
109
110     /**
111      *  The default impl. will create a bitmap-shader from the bitmap,
112      *  and call drawRect with it.
113      */
114     virtual void drawBitmapRect(const SkDraw&, const SkBitmap&,
115                                 const SkRect* srcOrNull, const SkRect& dst,
116                                 const SkPaint& paint,
117                                 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE;
118
119     /**
120      *  Does not handle text decoration.
121      *  Decorations (underline and stike-thru) will be handled by SkCanvas.
122      */
123     virtual void drawText(const SkDraw&, const void* text, size_t len,
124                           SkScalar x, SkScalar y, const SkPaint& paint) SK_OVERRIDE;
125     virtual void drawPosText(const SkDraw&, const void* text, size_t len,
126                              const SkScalar pos[], SkScalar constY,
127                              int scalarsPerPos, const SkPaint& paint) SK_OVERRIDE;
128     virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
129                                 const SkPath& path, const SkMatrix* matrix,
130                                 const SkPaint& paint) SK_OVERRIDE;
131     virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
132                               const SkPoint verts[], const SkPoint texs[],
133                               const SkColor colors[], SkXfermode* xmode,
134                               const uint16_t indices[], int indexCount,
135                               const SkPaint& paint) SK_OVERRIDE;
136     /** The SkBaseDevice passed will be an SkBaseDevice which was returned by a call to
137         onCreateDevice on this device with kSaveLayer_Usage.
138      */
139     virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
140                             const SkPaint&) SK_OVERRIDE;
141
142     ///////////////////////////////////////////////////////////////////////////
143
144     /** Update as needed the pixel value in the bitmap, so that the caller can
145         access the pixels directly. Note: only the pixels field should be
146         altered. The config/width/height/rowbytes must remain unchanged.
147         @return the device contents as a bitmap
148     */
149     virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE;
150
151     SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); }
152     // just for subclasses, to assign a custom pixelref
153     SkPixelRef* setPixelRef(SkPixelRef* pr) {
154         fBitmap.setPixelRef(pr);
155         return pr;
156     }
157
158     virtual bool onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) SK_OVERRIDE;
159     virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) SK_OVERRIDE;
160     virtual void* onAccessPixels(SkImageInfo* info, size_t* rowBytes) SK_OVERRIDE;
161
162     /** Called when this device is installed into a Canvas. Balanced by a call
163         to unlockPixels() when the device is removed from a Canvas.
164     */
165     virtual void lockPixels() SK_OVERRIDE;
166     virtual void unlockPixels() SK_OVERRIDE;
167
168     /**
169      *  Returns true if the device allows processing of this imagefilter. If
170      *  false is returned, then the filter is ignored. This may happen for
171      *  some subclasses that do not support pixel manipulations after drawing
172      *  has occurred (e.g. printing). The default implementation returns true.
173      */
174     virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE;
175
176     /**
177      *  Override and return true for filters that the device can handle
178      *  intrinsically. Doing so means that SkCanvas will pass-through this
179      *  filter to drawSprite and drawDevice (and potentially filterImage).
180      *  Returning false means the SkCanvas will have apply the filter itself,
181      *  and just pass the resulting image to the device.
182      */
183     virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE;
184
185     /**
186      *  Related (but not required) to canHandleImageFilter, this method returns
187      *  true if the device could apply the filter to the src bitmap and return
188      *  the result (and updates offset as needed).
189      *  If the device does not recognize or support this filter,
190      *  it just returns false and leaves result and offset unchanged.
191      */
192     virtual bool filterImage(const SkImageFilter*, const SkBitmap&, const SkImageFilter::Context&,
193                              SkBitmap* result, SkIPoint* offset) SK_OVERRIDE;
194
195 private:
196     friend class SkCanvas;
197     friend struct DeviceCM; //for setMatrixClip
198     friend class SkDraw;
199     friend class SkDrawIter;
200     friend class SkDeviceFilteredPaint;
201     friend class SkDeviceImageFilterProxy;
202
203     friend class SkSurface_Raster;
204
205     // used to change the backend's pixels (and possibly config/rowbytes)
206     // but cannot change the width/height, so there should be no change to
207     // any clip information.
208     virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE;
209
210     virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) SK_OVERRIDE;
211
212     /** Causes any deferred drawing to the device to be completed.
213      */
214     virtual void flush() SK_OVERRIDE {}
215
216     virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE;
217     virtual const void* peekPixels(SkImageInfo*, size_t* rowBytes) SK_OVERRIDE;
218
219     SkBitmap    fBitmap;
220
221     typedef SkBaseDevice INHERITED;
222 };
223
224 #endif // SkBitmapDevice_DEFINED