• 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 "SkBitmap.h"
13 #include "SkCanvas.h"
14 #include "SkColor.h"
15 #include "SkDevice.h"
16 #include "SkImageFilter.h"
17 #include "SkImageInfo.h"
18 #include "SkRect.h"
19 #include "SkScalar.h"
20 #include "SkSize.h"
21 #include "SkSurfaceProps.h"
22 #include "SkTypes.h"
23 
24 class SkDraw;
25 class SkMatrix;
26 class SkPaint;
27 class SkPath;
28 class SkPixelRef;
29 class SkPixmap;
30 class SkRRect;
31 class SkSurface;
32 class SkXfermode;
33 struct SkPoint;
34 
35 ///////////////////////////////////////////////////////////////////////////////
36 class SK_API SkBitmapDevice : public SkBaseDevice {
37 public:
38     /**
39      *  Construct a new device with the specified bitmap as its backend. It is
40      *  valid for the bitmap to have no pixels associated with it. In that case,
41      *  any drawing to this device will have no effect.
42      */
43     SkBitmapDevice(const SkBitmap& bitmap);
44 
45     /**
46      * Create a new device along with its requisite pixel memory using
47      * default SkSurfaceProps (i.e., kLegacyFontHost_InitType-style).
48      * Note: this entry point is slated for removal - no one should call it.
49      */
50     static SkBitmapDevice* Create(const SkImageInfo& info);
51 
52     /**
53      *  Construct a new device with the specified bitmap as its backend. It is
54      *  valid for the bitmap to have no pixels associated with it. In that case,
55      *  any drawing to this device will have no effect.
56      */
57     SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps);
58 
59     static SkBitmapDevice* Create(const SkImageInfo&, const SkSurfaceProps&);
60 
61     SkImageInfo imageInfo() const override;
62 
63 protected:
64     bool onShouldDisableLCD(const SkPaint&) const override;
65 
66     /** These are called inside the per-device-layer loop for each draw call.
67      When these are called, we have already applied any saveLayer operations,
68      and are handling any looping from the paint, and any effects from the
69      DrawFilter.
70      */
71     void drawPaint(const SkDraw&, const SkPaint& paint) override;
72     virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
73                             const SkPoint[], const SkPaint& paint) override;
74     virtual void drawRect(const SkDraw&, const SkRect& r,
75                           const SkPaint& paint) override;
76     virtual void drawOval(const SkDraw&, const SkRect& oval,
77                           const SkPaint& paint) override;
78     virtual void drawRRect(const SkDraw&, const SkRRect& rr,
79                            const SkPaint& paint) override;
80 
81     /**
82      *  If pathIsMutable, then the implementation is allowed to cast path to a
83      *  non-const pointer and modify it in place (as an optimization). Canvas
84      *  may do this to implement helpers such as drawOval, by placing a temp
85      *  path on the stack to hold the representation of the oval.
86      *
87      *  If prePathMatrix is not null, it should logically be applied before any
88      *  stroking or other effects. If there are no effects on the paint that
89      *  affect the geometry/rasterization, then the pre matrix can just be
90      *  pre-concated with the current matrix.
91      */
92     virtual void drawPath(const SkDraw&, const SkPath& path,
93                           const SkPaint& paint,
94                           const SkMatrix* prePathMatrix = NULL,
95                           bool pathIsMutable = false) override;
96     virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
97                             const SkMatrix& matrix, const SkPaint& paint) override;
98     virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
99                             int x, int y, const SkPaint& paint) override;
100 
101     /**
102      *  The default impl. will create a bitmap-shader from the bitmap,
103      *  and call drawRect with it.
104      */
105     void drawBitmapRect(const SkDraw&, const SkBitmap&, const SkRect*, const SkRect&,
106                         const SkPaint&, SkCanvas::SrcRectConstraint) override;
107 
108     /**
109      *  Does not handle text decoration.
110      *  Decorations (underline and stike-thru) will be handled by SkCanvas.
111      */
112     virtual void drawText(const SkDraw&, const void* text, size_t len,
113                           SkScalar x, SkScalar y, const SkPaint& paint) override;
114     virtual void drawPosText(const SkDraw&, const void* text, size_t len,
115                              const SkScalar pos[], int scalarsPerPos,
116                              const SkPoint& offset, const SkPaint& paint) override;
117     virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
118                               const SkPoint verts[], const SkPoint texs[],
119                               const SkColor colors[], SkXfermode* xmode,
120                               const uint16_t indices[], int indexCount,
121                               const SkPaint& paint) override;
122     virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, const SkPaint&) override;
123 
124     ///////////////////////////////////////////////////////////////////////////
125 
126     /** Update as needed the pixel value in the bitmap, so that the caller can
127         access the pixels directly. Note: only the pixels field should be
128         altered. The config/width/height/rowbytes must remain unchanged.
129         @return the device contents as a bitmap
130     */
131     const SkBitmap& onAccessBitmap() override;
132 
getPixelRef()133     SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); }
134     // just for subclasses, to assign a custom pixelref
setPixelRef(SkPixelRef * pr)135     SkPixelRef* setPixelRef(SkPixelRef* pr) {
136         fBitmap.setPixelRef(pr);
137         return pr;
138     }
139 
140     bool onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) override;
141     bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) override;
142     bool onPeekPixels(SkPixmap*) override;
143     bool onAccessPixels(SkPixmap*) override;
144     void onAttachToCanvas(SkCanvas*) override;
145     void onDetachFromCanvas() override;
146 
147 private:
148     friend class SkCanvas;
149     friend struct DeviceCM; //for setMatrixClip
150     friend class SkDraw;
151     friend class SkDrawIter;
152     friend class SkDeviceFilteredPaint;
153 
154     friend class SkSurface_Raster;
155 
156     // used to change the backend's pixels (and possibly config/rowbytes)
157     // but cannot change the width/height, so there should be no change to
158     // any clip information.
159     void replaceBitmapBackendForRasterSurface(const SkBitmap&) override;
160 
161     SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
162 
163     SkSurface* newSurface(const SkImageInfo&, const SkSurfaceProps&) override;
164 
165     SkImageFilter::Cache* getImageFilterCache() override;
166 
167     SkBitmap    fBitmap;
168 
169     void setNewSize(const SkISize&);  // Used by SkCanvas for resetForNextPicture().
170 
171     typedef SkBaseDevice INHERITED;
172 };
173 
174 #endif // SkBitmapDevice_DEFINED
175