1
2/*
3 * Copyright 2010 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 SkGpuDevice_DEFINED
10#define SkGpuDevice_DEFINED
11
12#include "SkGr.h"
13#include "SkBitmap.h"
14#include "SkDevice.h"
15#include "SkPicture.h"
16#include "SkRegion.h"
17#include "SkSurface.h"
18#include "GrDrawContext.h"
19#include "GrContext.h"
20#include "GrSurfacePriv.h"
21#include "GrTypes.h"
22
23class GrAccelData;
24class GrTextureProducer;
25struct GrCachedLayer;
26
27/**
28 *  Subclass of SkBaseDevice, which directs all drawing to the GrGpu owned by the
29 *  canvas.
30 */
31class SK_API SkGpuDevice : public SkBaseDevice {
32public:
33    enum InitContents {
34        kClear_InitContents,
35        kUninit_InitContents
36    };
37
38    /**
39     * Creates an SkGpuDevice from a GrRenderTarget.
40     */
41    static SkGpuDevice* Create(GrRenderTarget* target, const SkSurfaceProps*, InitContents);
42
43    /**
44     * Creates an SkGpuDevice from a GrRenderTarget whose texture width/height is
45     * different than its actual width/height (e.g., approx-match scratch texture).
46     */
47    static SkGpuDevice* Create(GrRenderTarget* target, int width, int height,
48                               const SkSurfaceProps*, InitContents);
49
50    /**
51     * New device that will create an offscreen renderTarget based on the ImageInfo and
52     * sampleCount. The Budgeted param controls whether the device's backing store counts against
53     * the resource cache budget. On failure, returns nullptr.
54     */
55    static SkGpuDevice* Create(GrContext*, SkBudgeted, const SkImageInfo&,
56                               int sampleCount, const SkSurfaceProps*,
57                               InitContents, GrTextureStorageAllocator = GrTextureStorageAllocator());
58
59    ~SkGpuDevice() override {}
60
61    SkGpuDevice* cloneDevice(const SkSurfaceProps& props) {
62        SkBaseDevice* dev = this->onCreateDevice(CreateInfo(this->imageInfo(), kPossible_TileUsage,
63                                                            props.pixelGeometry()),
64                                                 nullptr);
65        return static_cast<SkGpuDevice*>(dev);
66    }
67
68    GrContext* context() const { return fContext; }
69
70    // set all pixels to 0
71    void clearAll();
72
73    void replaceRenderTarget(bool shouldRetainContent);
74
75    GrRenderTarget* accessRenderTarget() override;
76
77    SkImageInfo imageInfo() const override {
78        return fLegacyBitmap.info();
79    }
80
81    void drawPaint(const SkDraw&, const SkPaint& paint) override;
82    virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
83                            const SkPoint[], const SkPaint& paint) override;
84    virtual void drawRect(const SkDraw&, const SkRect& r,
85                          const SkPaint& paint) override;
86    virtual void drawRRect(const SkDraw&, const SkRRect& r,
87                           const SkPaint& paint) override;
88    virtual void drawDRRect(const SkDraw& draw, const SkRRect& outer,
89                            const SkRRect& inner, const SkPaint& paint) override;
90    virtual void drawOval(const SkDraw&, const SkRect& oval,
91                          const SkPaint& paint) override;
92    virtual void drawPath(const SkDraw&, const SkPath& path,
93                          const SkPaint& paint, const SkMatrix* prePathMatrix,
94                          bool pathIsMutable) override;
95    virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
96                            const SkMatrix&, const SkPaint&) override;
97    virtual void drawBitmapRect(const SkDraw&, const SkBitmap&,
98                                const SkRect* srcOrNull, const SkRect& dst,
99                                const SkPaint& paint, SkCanvas::SrcRectConstraint) override;
100    virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
101                            int x, int y, const SkPaint& paint) override;
102    virtual void drawText(const SkDraw&, const void* text, size_t len,
103                          SkScalar x, SkScalar y, const SkPaint&) override;
104    virtual void drawPosText(const SkDraw&, const void* text, size_t len,
105                             const SkScalar pos[], int scalarsPerPos,
106                             const SkPoint& offset, const SkPaint&) override;
107    virtual void drawTextBlob(const SkDraw&, const SkTextBlob*, SkScalar x, SkScalar y,
108                              const SkPaint& paint, SkDrawFilter* drawFilter) override;
109    virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
110                              const SkPoint verts[], const SkPoint texs[],
111                              const SkColor colors[], SkXfermode* xmode,
112                              const uint16_t indices[], int indexCount,
113                              const SkPaint&) override;
114    void drawAtlas(const SkDraw&, const SkImage* atlas, const SkRSXform[], const SkRect[],
115                       const SkColor[], int count, SkXfermode::Mode, const SkPaint&) override;
116    virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
117                            const SkPaint&) override;
118    void drawImage(const SkDraw&, const SkImage*, SkScalar x, SkScalar y, const SkPaint&) override;
119    void drawImageRect(const SkDraw&, const SkImage*, const SkRect* src, const SkRect& dst,
120                       const SkPaint&, SkCanvas::SrcRectConstraint) override;
121
122    void drawImageNine(const SkDraw& draw, const SkImage* image, const SkIRect& center,
123                       const SkRect& dst, const SkPaint& paint) override;
124    void drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
125                        const SkRect& dst, const SkPaint& paint) override;
126
127    void flush() override;
128
129    void onAttachToCanvas(SkCanvas* canvas) override;
130    void onDetachFromCanvas() override;
131
132    const SkBitmap& onAccessBitmap() override;
133    bool onAccessPixels(SkPixmap*) override;
134
135    bool canHandleImageFilter(const SkImageFilter*) override;
136    virtual bool filterImage(const SkImageFilter*, const SkBitmap&,
137                             const SkImageFilter::Context&,
138                             SkBitmap*, SkIPoint*) override;
139
140    bool filterTexture(GrContext*, GrTexture*, int width, int height, const SkImageFilter*,
141                       const SkImageFilter::Context&,
142                       SkBitmap* result, SkIPoint* offset);
143
144    static SkImageFilter::Cache* NewImageFilterCache();
145
146    // for debugging purposes only
147    void drawTexture(GrTexture*, const SkRect& dst, const SkPaint&);
148
149protected:
150    bool onReadPixels(const SkImageInfo&, void*, size_t, int, int) override;
151    bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) override;
152    bool onShouldDisableLCD(const SkPaint&) const final;
153
154    /**  PRIVATE / EXPERIMENTAL -- do not call */
155    virtual bool EXPERIMENTAL_drawPicture(SkCanvas* canvas, const SkPicture* picture,
156                                          const SkMatrix*, const SkPaint*) override;
157
158private:
159    // We want these unreffed in DrawContext, RenderTarget, GrContext order.
160    SkAutoTUnref<GrContext>         fContext;
161    SkAutoTUnref<GrRenderTarget>    fRenderTarget;
162    SkAutoTUnref<GrDrawContext>     fDrawContext;
163
164    SkAutoTUnref<const SkClipStack> fClipStack;
165    SkIPoint                        fClipOrigin;
166    GrClip                          fClip;;
167    // remove when our clients don't rely on accessBitmap()
168    SkBitmap                        fLegacyBitmap;
169    bool                            fOpaque;
170
171    enum Flags {
172        kNeedClear_Flag = 1 << 0,  //!< Surface requires an initial clear
173        kIsOpaque_Flag  = 1 << 1,  //!< Hint from client that rendering to this device will be
174                                   //   opaque even if the config supports alpha.
175    };
176    static bool CheckAlphaTypeAndGetFlags(const SkImageInfo* info, InitContents init,
177                                          unsigned* flags);
178
179    SkGpuDevice(GrRenderTarget*, int width, int height, const SkSurfaceProps*, unsigned flags);
180
181    SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
182
183    SkSurface* newSurface(const SkImageInfo&, const SkSurfaceProps&) override;
184
185    SkImageFilter::Cache* getImageFilterCache() override;
186
187    bool forceConservativeRasterClip() const override { return true; }
188
189    // sets the render target and clip on context
190    void prepareDraw(const SkDraw&);
191
192    /**
193     * Helper functions called by drawBitmapCommon. By the time these are called the SkDraw's
194     * matrix, clip, and the device's render target has already been set on GrContext.
195     */
196
197    // The tileSize and clippedSrcRect will be valid only if true is returned.
198    bool shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
199                           const SkMatrix& viewMatrix,
200                           const GrTextureParams& params,
201                           const SkRect* srcRectPtr,
202                           int maxTileSize,
203                           int* tileSize,
204                           SkIRect* clippedSubset) const;
205    bool shouldTileBitmap(const SkBitmap& bitmap,
206                          const SkMatrix& viewMatrix,
207                          const GrTextureParams& sampler,
208                          const SkRect* srcRectPtr,
209                          int maxTileSize,
210                          int* tileSize,
211                          SkIRect* clippedSrcRect) const;
212    // Just returns the predicate, not the out-tileSize or out-clippedSubset, as they are not
213    // needed at the moment.
214    bool shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
215                         SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
216                         const SkMatrix& viewMatrix) const;
217
218    void internalDrawBitmap(const SkBitmap&,
219                            const SkMatrix& viewMatrix,
220                            const SkRect&,
221                            const GrTextureParams& params,
222                            const SkPaint& paint,
223                            SkCanvas::SrcRectConstraint,
224                            bool bicubic,
225                            bool needsTextureDomain);
226
227    void drawTiledBitmap(const SkBitmap& bitmap,
228                         const SkMatrix& viewMatrix,
229                         const SkRect& srcRect,
230                         const SkIRect& clippedSrcRect,
231                         const GrTextureParams& params,
232                         const SkPaint& paint,
233                         SkCanvas::SrcRectConstraint,
234                         int tileSize,
235                         bool bicubic);
236
237    void drawTextureProducer(GrTextureProducer*,
238                             const SkRect* srcRect,
239                             const SkRect* dstRect,
240                             SkCanvas::SrcRectConstraint,
241                             const SkMatrix& viewMatrix,
242                             const GrClip&,
243                             const SkPaint&);
244
245    void drawTextureProducerImpl(GrTextureProducer*,
246                                 const SkRect& clippedSrcRect,
247                                 const SkRect& clippedDstRect,
248                                 SkCanvas::SrcRectConstraint,
249                                 const SkMatrix& viewMatrix,
250                                 const SkMatrix& srcToDstMatrix,
251                                 const GrClip&,
252                                 const SkPaint&);
253
254    bool drawFilledDRRect(const SkMatrix& viewMatrix, const SkRRect& outer,
255                          const SkRRect& inner, const SkPaint& paint);
256
257    void drawProducerNine(const SkDraw&, GrTextureProducer*, const SkIRect& center,
258                          const SkRect& dst, const SkPaint&);
259
260    bool drawDashLine(const SkPoint pts[2], const SkPaint& paint);
261
262    static GrRenderTarget* CreateRenderTarget(GrContext*, SkBudgeted, const SkImageInfo&,
263                                              int sampleCount, GrTextureStorageAllocator);
264
265    friend class GrAtlasTextContext;
266    friend class SkSurface_Gpu;      // for access to surfaceProps
267    typedef SkBaseDevice INHERITED;
268};
269
270#endif
271