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