• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2010 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 SkGpuDevice_DEFINED
9 #define SkGpuDevice_DEFINED
10 
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkPicture.h"
13 #include "include/core/SkRegion.h"
14 #include "include/core/SkSurface.h"
15 #include "include/gpu/GrContext.h"
16 #include "include/gpu/GrTypes.h"
17 #include "src/core/SkClipStackDevice.h"
18 #include "src/gpu/GrClipStackClip.h"
19 #include "src/gpu/GrContextPriv.h"
20 #include "src/gpu/GrRenderTargetContext.h"
21 #include "src/gpu/SkGr.h"
22 
23 class GrAccelData;
24 class GrTextureMaker;
25 class GrTextureProducer;
26 struct GrCachedLayer;
27 
28 class SkSpecialImage;
29 
30 /**
31  *  Subclass of SkBaseDevice, which directs all drawing to the GrGpu owned by the
32  *  canvas.
33  */
34 class SkGpuDevice : public SkClipStackDevice {
35 public:
36     enum InitContents {
37         kClear_InitContents,
38         kUninit_InitContents
39     };
40 
41     /**
42      * Creates an SkGpuDevice from a GrRenderTargetContext whose backing width/height is
43      * different than its actual width/height (e.g., approx-match scratch texture).
44      */
45     static sk_sp<SkGpuDevice> Make(GrContext*, sk_sp<GrRenderTargetContext> renderTargetContext,
46                                    int width, int height, InitContents);
47 
48     /**
49      * New device that will create an offscreen renderTarget based on the ImageInfo and
50      * sampleCount. The mipMapped flag tells the gpu to create the underlying render target with
51      * mips. The Budgeted param controls whether the device's backing store counts against the
52      * resource cache budget. On failure, returns nullptr.
53      * This entry point creates a kExact backing store. It is used when creating SkGpuDevices
54      * for SkSurfaces.
55      */
56     static sk_sp<SkGpuDevice> Make(GrContext*, SkBudgeted, const SkImageInfo&,
57                                    int sampleCount, GrSurfaceOrigin, const SkSurfaceProps*,
58                                    GrMipMapped mipMapped, InitContents);
59 
~SkGpuDevice()60     ~SkGpuDevice() override {}
61 
context()62     GrContext* context() const override { return fContext.get(); }
63 
64     // set all pixels to 0
65     void clearAll();
66 
67     void replaceRenderTargetContext(bool shouldRetainContent);
68     void replaceRenderTargetContext(sk_sp<GrRenderTargetContext>, bool shouldRetainContent);
69 
70     GrRenderTargetContext* accessRenderTargetContext() override;
71 
72     void drawPaint(const SkPaint& paint) override;
73     void drawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint[],
74                     const SkPaint& paint) override;
75     void drawRect(const SkRect& r, const SkPaint& paint) override;
76     void drawRRect(const SkRRect& r, const SkPaint& paint) override;
77     void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) override;
78     void drawRegion(const SkRegion& r, const SkPaint& paint) override;
79     void drawOval(const SkRect& oval, const SkPaint& paint) override;
80     void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
81                  bool useCenter, const SkPaint& paint) override;
82     void drawPath(const SkPath& path, const SkPaint& paint, bool pathIsMutable) override;
83     void drawBitmapRect(const SkBitmap&, const SkRect* srcOrNull, const SkRect& dst,
84                         const SkPaint& paint, SkCanvas::SrcRectConstraint) override;
85     void drawSprite(const SkBitmap& bitmap, int x, int y,
86                     const SkPaint& paint) override;
87     void drawGlyphRunList(const SkGlyphRunList& glyphRunList) override;
88     void drawVertices(const SkVertices*, const SkVertices::Bone bones[], int boneCount, SkBlendMode,
89                       const SkPaint&) override;
90     void drawShadow(const SkPath&, const SkDrawShadowRec&) override;
91     void drawAtlas(const SkImage* atlas, const SkRSXform[], const SkRect[],
92                    const SkColor[], int count, SkBlendMode, const SkPaint&) override;
93     void drawDevice(SkBaseDevice*, int x, int y, const SkPaint&) override;
94 
95     void drawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
96                        const SkPaint&, SkCanvas::SrcRectConstraint) override;
97 
98     void drawImageNine(const SkImage* image, const SkIRect& center,
99                        const SkRect& dst, const SkPaint& paint) override;
100     void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
101                         const SkRect& dst, const SkPaint& paint) override;
102 
103     void drawImageLattice(const SkImage*, const SkCanvas::Lattice&,
104                           const SkRect& dst, const SkPaint&) override;
105     void drawBitmapLattice(const SkBitmap&, const SkCanvas::Lattice&,
106                            const SkRect& dst, const SkPaint&) override;
107 
108     void drawDrawable(SkDrawable*, const SkMatrix*, SkCanvas* canvas) override;
109 
110     void drawSpecial(SkSpecialImage*, int left, int top, const SkPaint& paint,
111                      SkImage*, const SkMatrix&) override;
112 
113     void drawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
114                         SkCanvas::QuadAAFlags aaFlags, SkColor color, SkBlendMode mode) override;
115     void drawEdgeAAImageSet(const SkCanvas::ImageSetEntry[], int count, const SkPoint dstClips[],
116                             const SkMatrix[], const SkPaint&, SkCanvas::SrcRectConstraint) override;
117 
118     sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
119     sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
120     sk_sp<SkSpecialImage> snapSpecial() override;
121     sk_sp<SkSpecialImage> snapBackImage(const SkIRect&) override;
122 
123     void flush() override;
124     GrSemaphoresSubmitted flush(SkSurface::BackendSurfaceAccess access, const GrFlushInfo&);
125     bool wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores);
126 
127     bool onAccessPixels(SkPixmap*) override;
128 
129 protected:
130     bool onReadPixels(const SkPixmap&, int, int) override;
131     bool onWritePixels(const SkPixmap&, int, int) override;
132 
133 private:
134     // We want these unreffed in RenderTargetContext, GrContext order.
135     sk_sp<GrContext>             fContext;
136     sk_sp<GrRenderTargetContext> fRenderTargetContext;
137 
138     SkISize                      fSize;
139 
140     enum Flags {
141         kNeedClear_Flag = 1 << 0,  //!< Surface requires an initial clear
142         kIsOpaque_Flag  = 1 << 1,  //!< Hint from client that rendering to this device will be
143                                    //   opaque even if the config supports alpha.
144     };
145     static bool CheckAlphaTypeAndGetFlags(const SkImageInfo* info, InitContents init,
146                                           unsigned* flags);
147 
148     SkGpuDevice(GrContext*, sk_sp<GrRenderTargetContext>, int width, int height, unsigned flags);
149 
150     SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
151 
152     sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
153 
154     SkImageFilterCache* getImageFilterCache() override;
155 
forceConservativeRasterClip()156     bool forceConservativeRasterClip() const override { return true; }
157 
clip()158     GrClipStackClip clip() const { return GrClipStackClip(&this->cs()); }
159 
160     const GrCaps* caps() const;
161 
162     /**
163      * Helper functions called by drawBitmapCommon. By the time these are called the SkDraw's
164      * matrix, clip, and the device's render target has already been set on GrContext.
165      */
166 
167     // The tileSize and clippedSrcRect will be valid only if true is returned.
168     bool shouldTileImageID(uint32_t imageID,
169                            const SkIRect& imageRect,
170                            const SkMatrix& viewMatrix,
171                            const SkMatrix& srcToDstRectMatrix,
172                            const GrSamplerState& params,
173                            const SkRect* srcRectPtr,
174                            int maxTileSize,
175                            int* tileSize,
176                            SkIRect* clippedSubset) const;
177     // Just returns the predicate, not the out-tileSize or out-clippedSubset, as they are not
178     // needed at the moment.
179     bool shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
180                          SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
181                          const SkMatrix& viewMatrix, const SkMatrix& srcToDstRect) const;
182 
183     sk_sp<SkSpecialImage> filterTexture(SkSpecialImage*,
184                                         int left, int top,
185                                         SkIPoint* offset,
186                                         const SkImageFilter* filter);
187 
188     // Splits bitmap into tiles of tileSize and draws them using separate textures for each tile.
189     void drawTiledBitmap(const SkBitmap& bitmap,
190                          const SkMatrix& viewMatrix,
191                          const SkMatrix& srcToDstMatrix,
192                          const SkRect& srcRect,
193                          const SkIRect& clippedSrcRect,
194                          const GrSamplerState& params,
195                          const SkPaint& paint,
196                          SkCanvas::SrcRectConstraint,
197                          int tileSize,
198                          bool bicubic);
199 
200     // Used by drawTiledBitmap to draw each tile.
201     void drawBitmapTile(const SkBitmap&,
202                         const SkMatrix& viewMatrix,
203                         const SkRect& dstRect,
204                         const SkRect& srcRect,
205                         const GrSamplerState& samplerState,
206                         const SkPaint& paint,
207                         SkCanvas::SrcRectConstraint,
208                         bool bicubic,
209                         bool needsTextureDomain);
210 
211     // If not null, dstClip must be contained inside dst and will also respect the edge AA flags.
212     // If 'preViewMatrix' is not null, final CTM will be this->ctm() * preViewMatrix.
213     void drawImageQuad(const SkImage*, const SkRect* src, const SkRect* dst,
214                        const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
215                        const SkMatrix* preViewMatrix, const SkPaint&, SkCanvas::SrcRectConstraint);
216 
217     // TODO(michaelludwig): This can be removed once drawBitmapRect is removed from SkDevice
218     // so that drawImageQuad is the sole entry point into the draw-single-image op
219     void drawTextureProducer(GrTextureProducer*,
220                              const SkRect* srcRect,
221                              const SkRect* dstRect,
222                              SkCanvas::SrcRectConstraint,
223                              const SkMatrix& viewMatrix,
224                              const SkPaint&,
225                              bool attemptDrawTexture);
226 
227     void drawProducerLattice(GrTextureProducer*, std::unique_ptr<SkLatticeIter>, const SkRect& dst,
228                              const SkPaint&);
229 
230     void drawStrokedLine(const SkPoint pts[2], const SkPaint&);
231 
232     void wireframeVertices(SkVertices::VertexMode, int vertexCount, const SkPoint verts[],
233                            const SkVertices::Bone bones[], int boneCount, SkBlendMode,
234                            const uint16_t indices[], int indexCount, const SkPaint&);
235 
236     static sk_sp<GrRenderTargetContext> MakeRenderTargetContext(GrContext*,
237                                                                 SkBudgeted,
238                                                                 const SkImageInfo&,
239                                                                 int sampleCount,
240                                                                 GrSurfaceOrigin,
241                                                                 const SkSurfaceProps*,
242                                                                 GrMipMapped);
243 
244     friend class GrAtlasTextContext;
245     friend class SkSurface_Gpu;      // for access to surfaceProps
246     typedef SkClipStackDevice INHERITED;
247 };
248 
249 #endif
250