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///////////////////////////////////////////////////////////////////////////////
15class SK_API SkBitmapDevice : public SkBaseDevice {
16public:
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);
25private:
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    static SkBitmapDevice* Create(const SkImageInfo&, const SkDeviceProperties*);
33public:
34    static SkBitmapDevice* Create(const SkImageInfo& info) {
35        return Create(info, NULL);
36    }
37
38    virtual SkImageInfo imageInfo() const SK_OVERRIDE;
39
40protected:
41    virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE;
42
43    /** Clears the entire device to the specified color (including alpha).
44     *  Ignores the clip.
45     */
46    virtual void clear(SkColor color) SK_OVERRIDE;
47
48    /** These are called inside the per-device-layer loop for each draw call.
49     When these are called, we have already applied any saveLayer operations,
50     and are handling any looping from the paint, and any effects from the
51     DrawFilter.
52     */
53    virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
54    virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
55                            const SkPoint[], const SkPaint& paint) SK_OVERRIDE;
56    virtual void drawRect(const SkDraw&, const SkRect& r,
57                          const SkPaint& paint) SK_OVERRIDE;
58    virtual void drawOval(const SkDraw&, const SkRect& oval,
59                          const SkPaint& paint) SK_OVERRIDE;
60    virtual void drawRRect(const SkDraw&, const SkRRect& rr,
61                           const SkPaint& paint) SK_OVERRIDE;
62
63    /**
64     *  If pathIsMutable, then the implementation is allowed to cast path to a
65     *  non-const pointer and modify it in place (as an optimization). Canvas
66     *  may do this to implement helpers such as drawOval, by placing a temp
67     *  path on the stack to hold the representation of the oval.
68     *
69     *  If prePathMatrix is not null, it should logically be applied before any
70     *  stroking or other effects. If there are no effects on the paint that
71     *  affect the geometry/rasterization, then the pre matrix can just be
72     *  pre-concated with the current matrix.
73     */
74    virtual void drawPath(const SkDraw&, const SkPath& path,
75                          const SkPaint& paint,
76                          const SkMatrix* prePathMatrix = NULL,
77                          bool pathIsMutable = false) SK_OVERRIDE;
78    virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
79                            const SkMatrix& matrix, const SkPaint& paint) SK_OVERRIDE;
80    virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
81                            int x, int y, const SkPaint& paint) SK_OVERRIDE;
82
83    /**
84     *  The default impl. will create a bitmap-shader from the bitmap,
85     *  and call drawRect with it.
86     */
87    virtual void drawBitmapRect(const SkDraw&, const SkBitmap&,
88                                const SkRect* srcOrNull, const SkRect& dst,
89                                const SkPaint& paint,
90                                SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE;
91
92    /**
93     *  Does not handle text decoration.
94     *  Decorations (underline and stike-thru) will be handled by SkCanvas.
95     */
96    virtual void drawText(const SkDraw&, const void* text, size_t len,
97                          SkScalar x, SkScalar y, const SkPaint& paint) SK_OVERRIDE;
98    virtual void drawPosText(const SkDraw&, const void* text, size_t len,
99                             const SkScalar pos[], SkScalar constY,
100                             int scalarsPerPos, const SkPaint& paint) SK_OVERRIDE;
101    virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
102                                const SkPath& path, const SkMatrix* matrix,
103                                const SkPaint& paint) SK_OVERRIDE;
104    virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
105                              const SkPoint verts[], const SkPoint texs[],
106                              const SkColor colors[], SkXfermode* xmode,
107                              const uint16_t indices[], int indexCount,
108                              const SkPaint& paint) SK_OVERRIDE;
109    /** The SkBaseDevice passed will be an SkBaseDevice which was returned by a call to
110        onCreateDevice on this device with kSaveLayer_Usage.
111     */
112    virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
113                            const SkPaint&) SK_OVERRIDE;
114
115    ///////////////////////////////////////////////////////////////////////////
116
117    /** Update as needed the pixel value in the bitmap, so that the caller can
118        access the pixels directly. Note: only the pixels field should be
119        altered. The config/width/height/rowbytes must remain unchanged.
120        @return the device contents as a bitmap
121    */
122    virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE;
123
124    SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); }
125    // just for subclasses, to assign a custom pixelref
126    SkPixelRef* setPixelRef(SkPixelRef* pr) {
127        fBitmap.setPixelRef(pr);
128        return pr;
129    }
130
131    virtual bool onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) SK_OVERRIDE;
132    virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) SK_OVERRIDE;
133    virtual void* onAccessPixels(SkImageInfo* info, size_t* rowBytes) SK_OVERRIDE;
134
135    /** Called when this device is installed into a Canvas. Balanced by a call
136        to unlockPixels() when the device is removed from a Canvas.
137    */
138    virtual void lockPixels() SK_OVERRIDE;
139    virtual void unlockPixels() SK_OVERRIDE;
140
141private:
142    friend class SkCanvas;
143    friend struct DeviceCM; //for setMatrixClip
144    friend class SkDraw;
145    friend class SkDrawIter;
146    friend class SkDeviceFilteredPaint;
147    friend class SkDeviceImageFilterProxy;
148
149    friend class SkSurface_Raster;
150
151    // used to change the backend's pixels (and possibly config/rowbytes)
152    // but cannot change the width/height, so there should be no change to
153    // any clip information.
154    virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE;
155
156    virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) SK_OVERRIDE;
157
158    virtual SkSurface* newSurface(const SkImageInfo&, const SkSurfaceProps&) SK_OVERRIDE;
159    virtual const void* peekPixels(SkImageInfo*, size_t* rowBytes) SK_OVERRIDE;
160
161    virtual SkImageFilter::Cache* getImageFilterCache() SK_OVERRIDE;
162
163    SkBitmap    fBitmap;
164
165    typedef SkBaseDevice INHERITED;
166};
167
168#endif // SkBitmapDevice_DEFINED
169