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