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