1 /*
2  * Copyright 2010 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkDevice_DEFINED
9 #define SkDevice_DEFINED
10 
11 #include "SkRefCnt.h"
12 #include "SkCanvas.h"
13 #include "SkColor.h"
14 #include "SkImageFilter.h"
15 #include "SkSurfaceProps.h"
16 
17 class SkBitmap;
18 class SkClipStack;
19 class SkDraw;
20 class SkDrawFilter;
21 struct SkIRect;
22 class SkMatrix;
23 class SkMetaData;
24 class SkRegion;
25 class GrRenderTarget;
26 
27 class SK_API SkBaseDevice : public SkRefCnt {
28 public:
29     /**
30      *  Construct a new device.
31     */
32     explicit SkBaseDevice(const SkSurfaceProps&);
33     virtual ~SkBaseDevice();
34 
35     SkMetaData& getMetaData();
36 
37     /**
38      *  Return ImageInfo for this device. If the canvas is not backed by pixels
39      *  (cpu or gpu), then the info's ColorType will be kUnknown_SkColorType.
40      */
41     virtual SkImageInfo imageInfo() const;
42 
43     /**
44      *  Return the bounds of the device in the coordinate space of the root
45      *  canvas. The root device will have its top-left at 0,0, but other devices
46      *  such as those associated with saveLayer may have a non-zero origin.
47      */
getGlobalBounds(SkIRect * bounds)48     void getGlobalBounds(SkIRect* bounds) const {
49         SkASSERT(bounds);
50         const SkIPoint& origin = this->getOrigin();
51         bounds->setXYWH(origin.x(), origin.y(), this->width(), this->height());
52     }
53 
getGlobalBounds()54     SkIRect getGlobalBounds() const {
55         SkIRect bounds;
56         this->getGlobalBounds(&bounds);
57         return bounds;
58     }
59 
width()60     int width() const {
61         return this->imageInfo().width();
62     }
63 
height()64     int height() const {
65         return this->imageInfo().height();
66     }
67 
isOpaque()68     bool isOpaque() const {
69         return this->imageInfo().isOpaque();
70     }
71 
72     /** Return the bitmap associated with this device. Call this each time you need
73         to access the bitmap, as it notifies the subclass to perform any flushing
74         etc. before you examine the pixels.
75         @param changePixels set to true if the caller plans to change the pixels
76         @return the device's bitmap
77     */
78     const SkBitmap& accessBitmap(bool changePixels);
79 
80     bool writePixels(const SkImageInfo&, const void*, size_t rowBytes, int x, int y);
81 
82     /**
83      *  Try to get write-access to the pixels behind the device. If successful, this returns true
84      *  and fills-out the pixmap parameter. On success it also bumps the genID of the underlying
85      *  bitmap.
86      *
87      *  On failure, returns false and ignores the pixmap parameter.
88      */
89     bool accessPixels(SkPixmap* pmap);
90 
91     /**
92      *  Try to get read-only-access to the pixels behind the device. If successful, this returns
93      *  true and fills-out the pixmap parameter.
94      *
95      *  On failure, returns false and ignores the pixmap parameter.
96      */
97     bool peekPixels(SkPixmap*);
98 
99     /**
100      * Return the device's associated gpu render target, or NULL.
101      */
accessRenderTarget()102     virtual GrRenderTarget* accessRenderTarget() { return NULL; }
103 
104 
105     /**
106      *  Return the device's origin: its offset in device coordinates from
107      *  the default origin in its canvas' matrix/clip
108      */
getOrigin()109     const SkIPoint& getOrigin() const { return fOrigin; }
110 
111     /**
112      * onAttachToCanvas is invoked whenever a device is installed in a canvas
113      * (i.e., setDevice, saveLayer (for the new device created by the save),
114      * and SkCanvas' SkBaseDevice & SkBitmap -taking ctors). It allows the
115      * devices to prepare for drawing (e.g., locking their pixels, etc.)
116      */
onAttachToCanvas(SkCanvas *)117     virtual void onAttachToCanvas(SkCanvas*) {
118         SkASSERT(!fAttachedToCanvas);
119 #ifdef SK_DEBUG
120         fAttachedToCanvas = true;
121 #endif
122     };
123 
124     /**
125      * onDetachFromCanvas notifies a device that it will no longer be drawn to.
126      * It gives the device a chance to clean up (e.g., unlock its pixels). It
127      * is invoked from setDevice (for the displaced device), restore and
128      * possibly from SkCanvas' dtor.
129      */
onDetachFromCanvas()130     virtual void onDetachFromCanvas() {
131         SkASSERT(fAttachedToCanvas);
132 #ifdef SK_DEBUG
133         fAttachedToCanvas = false;
134 #endif
135     };
136 
137 protected:
138     enum TileUsage {
139         kPossible_TileUsage,    //!< the created device may be drawn tiled
140         kNever_TileUsage,       //!< the created device will never be drawn tiled
141     };
142 
143     struct TextFlags {
144         uint32_t    fFlags;     // SkPaint::getFlags()
145     };
146 
147     /**
148      * Returns the text-related flags, possibly modified based on the state of the
149      * device (e.g. support for LCD).
150      */
151     uint32_t filterTextFlags(const SkPaint&) const;
152 
onShouldDisableLCD(const SkPaint &)153     virtual bool onShouldDisableLCD(const SkPaint&) const { return false; }
154 
155     /**
156      *
157      *  DEPRECATED: This will be removed in a future change. Device subclasses
158      *  should use the matrix and clip from the SkDraw passed to draw functions.
159      *
160      *  Called with the correct matrix and clip before this device is drawn
161      *  to using those settings. If your subclass overrides this, be sure to
162      *  call through to the base class as well.
163      *
164      *  The clipstack is another view of the clip. It records the actual
165      *  geometry that went into building the region. It is present for devices
166      *  that want to parse it, but is not required: the region is a complete
167      *  picture of the current clip. (i.e. if you regionize all of the geometry
168      *  in the clipstack, you will arrive at an equivalent region to the one
169      *  passed in).
170      */
setMatrixClip(const SkMatrix &,const SkRegion &,const SkClipStack &)171      virtual void setMatrixClip(const SkMatrix&, const SkRegion&,
172                                 const SkClipStack&) {};
173 
174     /** These are called inside the per-device-layer loop for each draw call.
175      When these are called, we have already applied any saveLayer operations,
176      and are handling any looping from the paint, and any effects from the
177      DrawFilter.
178      */
179     virtual void drawPaint(const SkDraw&, const SkPaint& paint) = 0;
180     virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
181                             const SkPoint[], const SkPaint& paint) = 0;
182     virtual void drawRect(const SkDraw&, const SkRect& r,
183                           const SkPaint& paint) = 0;
184     virtual void drawOval(const SkDraw&, const SkRect& oval,
185                           const SkPaint& paint) = 0;
186     virtual void drawRRect(const SkDraw&, const SkRRect& rr,
187                            const SkPaint& paint) = 0;
188 
189     // Default impl calls drawPath()
190     virtual void drawDRRect(const SkDraw&, const SkRRect& outer,
191                             const SkRRect& inner, const SkPaint&);
192 
193     /**
194      *  If pathIsMutable, then the implementation is allowed to cast path to a
195      *  non-const pointer and modify it in place (as an optimization). Canvas
196      *  may do this to implement helpers such as drawOval, by placing a temp
197      *  path on the stack to hold the representation of the oval.
198      *
199      *  If prePathMatrix is not null, it should logically be applied before any
200      *  stroking or other effects. If there are no effects on the paint that
201      *  affect the geometry/rasterization, then the pre matrix can just be
202      *  pre-concated with the current matrix.
203      */
204     virtual void drawPath(const SkDraw&, const SkPath& path,
205                           const SkPaint& paint,
206                           const SkMatrix* prePathMatrix = NULL,
207                           bool pathIsMutable = false) = 0;
208     virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
209                             const SkMatrix& matrix, const SkPaint& paint) = 0;
210     virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
211                             int x, int y, const SkPaint& paint) = 0;
212 
213     /**
214      *  The default impl. will create a bitmap-shader from the bitmap,
215      *  and call drawRect with it.
216      */
217     virtual void drawBitmapRect(const SkDraw&, const SkBitmap&,
218                                 const SkRect* srcOrNull, const SkRect& dst,
219                                 const SkPaint& paint,
220                                 SkCanvas::SrcRectConstraint) = 0;
221     virtual void drawBitmapNine(const SkDraw&, const SkBitmap&, const SkIRect& center,
222                                const SkRect& dst, const SkPaint&);
223 
224     virtual void drawImage(const SkDraw&, const SkImage*, SkScalar x, SkScalar y, const SkPaint&);
225     virtual void drawImageRect(const SkDraw&, const SkImage*, const SkRect* src, const SkRect& dst,
226                                const SkPaint&, SkCanvas::SrcRectConstraint);
227     virtual void drawImageNine(const SkDraw&, const SkImage*, const SkIRect& center,
228                                const SkRect& dst, const SkPaint&);
229 
230     /**
231      *  Does not handle text decoration.
232      *  Decorations (underline and stike-thru) will be handled by SkCanvas.
233      */
234     virtual void drawText(const SkDraw&, const void* text, size_t len,
235                           SkScalar x, SkScalar y, const SkPaint& paint) = 0;
236     virtual void drawPosText(const SkDraw&, const void* text, size_t len,
237                              const SkScalar pos[], int scalarsPerPos,
238                              const SkPoint& offset, const SkPaint& paint) = 0;
239     virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
240                               const SkPoint verts[], const SkPoint texs[],
241                               const SkColor colors[], SkXfermode* xmode,
242                               const uint16_t indices[], int indexCount,
243                               const SkPaint& paint) = 0;
244     // default implementation unrolls the blob runs.
245     virtual void drawTextBlob(const SkDraw&, const SkTextBlob*, SkScalar x, SkScalar y,
246                               const SkPaint& paint, SkDrawFilter* drawFilter);
247     // default implementation calls drawVertices
248     virtual void drawPatch(const SkDraw&, const SkPoint cubics[12], const SkColor colors[4],
249                            const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint);
250 
251     // default implementation calls drawPath
252     virtual void drawAtlas(const SkDraw&, const SkImage* atlas, const SkRSXform[], const SkRect[],
253                            const SkColor[], int count, SkXfermode::Mode, const SkPaint&);
254 
255     /** The SkDevice passed will be an SkDevice which was returned by a call to
256         onCreateDevice on this device with kNeverTile_TileExpectation.
257      */
258     virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
259                             const SkPaint&) = 0;
260 
261     virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, const SkPath&,
262                                 const SkMatrix*, const SkPaint&);
263 
264     bool readPixels(const SkImageInfo&, void* dst, size_t rowBytes, int x, int y);
265 
266     ///////////////////////////////////////////////////////////////////////////
267 
268     /** Update as needed the pixel value in the bitmap, so that the caller can
269         access the pixels directly.
270         @return The device contents as a bitmap
271     */
272     virtual const SkBitmap& onAccessBitmap() = 0;
273 
274     /**
275      *  Override and return true for filters that the device can handle
276      *  intrinsically. Doing so means that SkCanvas will pass-through this
277      *  filter to drawSprite and drawDevice (and potentially filterImage).
278      *  Returning false means the SkCanvas will have apply the filter itself,
279      *  and just pass the resulting image to the device.
280      */
canHandleImageFilter(const SkImageFilter *)281     virtual bool canHandleImageFilter(const SkImageFilter*) { return false; }
282 
283     /**
284      *  Related (but not required) to canHandleImageFilter, this method returns
285      *  true if the device could apply the filter to the src bitmap and return
286      *  the result (and updates offset as needed).
287      *  If the device does not recognize or support this filter,
288      *  it just returns false and leaves result and offset unchanged.
289      */
filterImage(const SkImageFilter *,const SkBitmap &,const SkImageFilter::Context &,SkBitmap *,SkIPoint *)290     virtual bool filterImage(const SkImageFilter*, const SkBitmap&,
291                              const SkImageFilter::Context&,
292                              SkBitmap* /*result*/, SkIPoint* /*offset*/) {
293         return false;
294     }
295 
296 protected:
newSurface(const SkImageInfo &,const SkSurfaceProps &)297     virtual SkSurface* newSurface(const SkImageInfo&, const SkSurfaceProps&) { return NULL; }
onPeekPixels(SkPixmap *)298     virtual bool onPeekPixels(SkPixmap*) { return false; }
299 
300     /**
301      *  The caller is responsible for "pre-clipping" the dst. The impl can assume that the dst
302      *  image at the specified x,y offset will fit within the device's bounds.
303      *
304      *  This is explicitly asserted in readPixels(), the public way to call this.
305      */
306     virtual bool onReadPixels(const SkImageInfo&, void*, size_t, int x, int y);
307 
308     /**
309      *  The caller is responsible for "pre-clipping" the src. The impl can assume that the src
310      *  image at the specified x,y offset will fit within the device's bounds.
311      *
312      *  This is explicitly asserted in writePixelsDirect(), the public way to call this.
313      */
314     virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int x, int y);
315 
onAccessPixels(SkPixmap *)316     virtual bool onAccessPixels(SkPixmap*) { return false; }
317 
surfaceProps()318     const SkSurfaceProps& surfaceProps() const {
319         return fSurfaceProps;
320     }
321 
322     /**
323      *  PRIVATE / EXPERIMENTAL -- do not call
324      *  This entry point gives the backend an opportunity to take over the rendering
325      *  of 'picture'. If optimization data is available (due to an earlier
326      *  'optimize' call) this entry point should make use of it and return true
327      *  if all rendering has been done. If false is returned, SkCanvas will
328      *  perform its own rendering pass. It is acceptable for the backend
329      *  to perform some device-specific warm up tasks and then let SkCanvas
330      *  perform the main rendering loop (by return false from here).
331      */
332     virtual bool EXPERIMENTAL_drawPicture(SkCanvas*, const SkPicture*, const SkMatrix*,
333                                           const SkPaint*);
334 
335     struct CreateInfo {
336         static SkPixelGeometry AdjustGeometry(const SkImageInfo&, TileUsage, SkPixelGeometry,
337                                               bool preserveLCDText);
338 
339         // The constructor may change the pixel geometry based on other parameters.
CreateInfoCreateInfo340         CreateInfo(const SkImageInfo& info,
341                    TileUsage tileUsage,
342                    SkPixelGeometry geo)
343             : fInfo(info)
344             , fTileUsage(tileUsage)
345             , fPixelGeometry(AdjustGeometry(info, tileUsage, geo, false))
346             , fForImageFilter(false) {}
347 
CreateInfoCreateInfo348         CreateInfo(const SkImageInfo& info,
349                    TileUsage tileUsage,
350                    SkPixelGeometry geo,
351                    bool preserveLCDText,
352                    bool forImageFilter)
353             : fInfo(info)
354             , fTileUsage(tileUsage)
355             , fPixelGeometry(AdjustGeometry(info, tileUsage, geo, preserveLCDText))
356             , fForImageFilter(forImageFilter) {}
357 
358         const SkImageInfo       fInfo;
359         const TileUsage         fTileUsage;
360         const SkPixelGeometry   fPixelGeometry;
361         const bool              fForImageFilter;
362     };
363 
364     /**
365      *  Create a new device based on CreateInfo. If the paint is not null, then it represents a
366      *  preview of how the new device will be composed with its creator device (this).
367      *
368      *  The subclass may be handed this device in drawDevice(), so it must always return
369      *  a device that it knows how to draw, and that it knows how to identify if it is not of the
370      *  same subclass (since drawDevice is passed a SkBaseDevice*). If the subclass cannot fulfill
371      *  that contract (e.g. PDF cannot support some settings on the paint) it should return NULL,
372      *  and the caller may then decide to explicitly create a bitmapdevice, knowing that later
373      *  it could not call drawDevice with it (but it could call drawSprite or drawBitmap).
374      */
onCreateDevice(const CreateInfo &,const SkPaint *)375     virtual SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) {
376         return NULL;
377     }
378 
379 private:
380     friend class SkCanvas;
381     friend struct DeviceCM; //for setMatrixClip
382     friend class SkDraw;
383     friend class SkDrawIter;
384     friend class SkDeviceFilteredPaint;
385     friend class SkImageFilter::DeviceProxy;
386     friend class SkNoPixelsBitmapDevice;
387     friend class SkSurface_Raster;
388 
389     /**
390      *  Calls through to drawSprite, processing imagefilter as needed.
391      */
392     void drawBitmapAsSprite(const SkDraw&, const SkBitmap&, int x, int y, const SkPaint&);
393 
394     // used to change the backend's pixels (and possibly config/rowbytes)
395     // but cannot change the width/height, so there should be no change to
396     // any clip information.
397     // TODO: move to SkBitmapDevice
replaceBitmapBackendForRasterSurface(const SkBitmap &)398     virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) {}
399 
forceConservativeRasterClip()400     virtual bool forceConservativeRasterClip() const { return false; }
401 
402     // just called by SkCanvas when built as a layer
setOrigin(int x,int y)403     void setOrigin(int x, int y) { fOrigin.set(x, y); }
404 
405     /** Causes any deferred drawing to the device to be completed.
406      */
flush()407     virtual void flush() {}
408 
getImageFilterCache()409     virtual SkImageFilter::Cache* getImageFilterCache() { return NULL; }
410 
411     SkIPoint    fOrigin;
412     SkMetaData* fMetaData;
413     SkSurfaceProps fSurfaceProps;
414 
415 #ifdef SK_DEBUG
416     bool        fAttachedToCanvas;
417 #endif
418 
419     typedef SkRefCnt INHERITED;
420 };
421 
422 #endif
423