xref: /third_party/skia/src/core/SkBitmapDevice.h (revision cb93a386)
1/*
2 * Copyright 2013 Google Inc.
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 SkBitmapDevice_DEFINED
9#define SkBitmapDevice_DEFINED
10
11#include "include/core/SkBitmap.h"
12#include "include/core/SkBlurTypes.h"
13#include "include/core/SkCanvas.h"
14#include "include/core/SkColor.h"
15#include "include/core/SkImageInfo.h"
16#include "include/core/SkRect.h"
17#include "include/core/SkScalar.h"
18#include "include/core/SkSize.h"
19#include "include/core/SkSurfaceProps.h"
20#include "src/core/SkDevice.h"
21#include "src/core/SkGlyphRunPainter.h"
22#include "src/core/SkRasterClip.h"
23#include "src/core/SkRasterClipStack.h"
24
25class SkImageFilterCache;
26class SkMatrix;
27class SkPaint;
28class SkPath;
29class SkPixmap;
30class SkRasterHandleAllocator;
31class SkRRect;
32class SkSurface;
33struct SkPoint;
34
35///////////////////////////////////////////////////////////////////////////////
36class SkBitmapDevice : public SkBaseDevice {
37public:
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                   void* externalHandle, const SkBitmap* coverage);
59
60    static SkBitmapDevice* Create(const SkImageInfo&, const SkSurfaceProps&,
61                                  bool trackCoverage,
62                                  SkRasterHandleAllocator*);
63
64    static SkBitmapDevice* Create(const SkImageInfo& info, const SkSurfaceProps& props) {
65        return Create(info, props, false, nullptr);
66    }
67
68    const SkPixmap* accessCoverage() const {
69        return fCoverage ? &fCoverage->pixmap() : nullptr;
70    }
71
72protected:
73    void* getRasterHandle() const override { return fRasterHandle; }
74
75    /** These are called inside the per-device-layer loop for each draw call.
76     When these are called, we have already applied any saveLayer operations,
77     and are handling any looping from the paint.
78     */
79    void drawPaint(const SkPaint& paint) override;
80    void drawPoints(SkCanvas::PointMode mode, size_t count,
81                            const SkPoint[], const SkPaint& paint) override;
82    void drawRect(const SkRect& r, const SkPaint& paint) override;
83    void drawOval(const SkRect& oval, const SkPaint& paint) override;
84    void drawRRect(const SkRRect& rr, const SkPaint& paint) override;
85
86    /**
87     *  If pathIsMutable, then the implementation is allowed to cast path to a
88     *  non-const pointer and modify it in place (as an optimization). Canvas
89     *  may do this to implement helpers such as drawOval, by placing a temp
90     *  path on the stack to hold the representation of the oval.
91     */
92    void drawPath(const SkPath&, const SkPaint&, bool pathIsMutable) override;
93
94    void drawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
95                       const SkSamplingOptions&, const SkPaint&,
96                       SkCanvas::SrcRectConstraint) override;
97
98    void drawVertices(const SkVertices*, SkBlendMode, const SkPaint&) override;
99    void drawAtlas(const SkRSXform[], const SkRect[], const SkColor[], int count, SkBlendMode,
100                   const SkPaint&) override;
101
102    ///////////////////////////////////////////////////////////////////////////
103
104    void drawDevice(SkBaseDevice*, const SkSamplingOptions&, const SkPaint&) override;
105    void drawSpecial(SkSpecialImage*, const SkMatrix&, const SkSamplingOptions&,
106                     const SkPaint&) override;
107
108    sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
109    sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
110    sk_sp<SkSpecialImage> snapSpecial(const SkIRect&, bool = false) override;
111    void setImmutable() override { fBitmap.setImmutable(); }
112
113    ///////////////////////////////////////////////////////////////////////////
114
115    void onDrawGlyphRunList(const SkGlyphRunList& glyphRunList, const SkPaint& paint) override;
116    bool onReadPixels(const SkPixmap&, int x, int y) override;
117    bool onWritePixels(const SkPixmap&, int, int) override;
118    bool onPeekPixels(SkPixmap*) override;
119    bool onAccessPixels(SkPixmap*) override;
120
121    void onSave() override;
122    void onRestore() override;
123    void onClipRect(const SkRect& rect, SkClipOp, bool aa) override;
124    void onClipRRect(const SkRRect& rrect, SkClipOp, bool aa) override;
125    void onClipPath(const SkPath& path, SkClipOp, bool aa) override;
126    void onClipShader(sk_sp<SkShader>) override;
127    void onClipRegion(const SkRegion& deviceRgn, SkClipOp) override;
128    void onReplaceClip(const SkIRect& rect) override;
129    bool onClipIsAA() const override;
130    bool onClipIsWideOpen() const override;
131    void onAsRgnClip(SkRegion*) const override;
132    void validateDevBounds(const SkIRect& r) override;
133    ClipType onGetClipType() const override;
134    SkIRect onDevClipBounds() const override;
135
136    void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull,
137                    const SkSamplingOptions&, const SkPaint&);
138
139    bool drawBlurImage(const SkImage* image, const SkBlurArg& blurArg) override { return false; }
140
141private:
142    friend class SkCanvas;
143    friend class SkDraw;
144    friend class SkDrawTiler;
145    friend class SkSurface_Raster;
146
147    class BDDraw;
148
149    // used to change the backend's pixels (and possibly config/rowbytes)
150    // but cannot change the width/height, so there should be no change to
151    // any clip information.
152    void replaceBitmapBackendForRasterSurface(const SkBitmap&) override;
153
154    SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
155
156    sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
157
158    SkImageFilterCache* getImageFilterCache() override;
159
160    SkBitmap    fBitmap;
161    void*       fRasterHandle = nullptr;
162    SkRasterClipStack  fRCStack;
163    std::unique_ptr<SkBitmap> fCoverage;    // if non-null, will have the same dimensions as fBitmap
164    SkGlyphRunListPainter fGlyphPainter;
165
166
167    using INHERITED = SkBaseDevice;
168};
169
170class SkBitmapDeviceFilteredSurfaceProps {
171public:
172    SkBitmapDeviceFilteredSurfaceProps(const SkBitmap& bitmap, const SkPaint& paint,
173                                       const SkSurfaceProps& surfaceProps)
174        : fSurfaceProps((kN32_SkColorType != bitmap.colorType() || !paint.isSrcOver())
175                        ? fLazy.init(surfaceProps.flags(), kUnknown_SkPixelGeometry)
176                        : &surfaceProps)
177    { }
178
179    SkBitmapDeviceFilteredSurfaceProps(const SkBitmapDeviceFilteredSurfaceProps&) = delete;
180    SkBitmapDeviceFilteredSurfaceProps& operator=(const SkBitmapDeviceFilteredSurfaceProps&) = delete;
181    SkBitmapDeviceFilteredSurfaceProps(SkBitmapDeviceFilteredSurfaceProps&&) = delete;
182    SkBitmapDeviceFilteredSurfaceProps& operator=(SkBitmapDeviceFilteredSurfaceProps&&) = delete;
183
184    const SkSurfaceProps& operator()() const { return *fSurfaceProps; }
185
186private:
187    SkTLazy<SkSurfaceProps> fLazy;
188    SkSurfaceProps const * const fSurfaceProps;
189};
190
191#endif // SkBitmapDevice_DEFINED
192