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