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