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/GrTypes.h"
16#include "src/core/SkClipStackDevice.h"
17#include "src/gpu/GrClipStackClip.h"
18#include "src/gpu/GrRenderTargetContext.h"
19#include "src/gpu/SkGr.h"
20
21class GrAccelData;
22class GrTextureMaker;
23class GrTextureProducer;
24struct GrCachedLayer;
25
26class SkSpecialImage;
27class SkSurface;
28class SkVertices;
29
30/**
31 * Subclass of SkBaseDevice, which directs all drawing to the GrGpu owned by the
32 * canvas.
33 */
34class SkGpuDevice : public SkClipStackDevice {
35public:
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(GrRecordingContext*,
46 std::unique_ptr<GrRenderTargetContext>,
47 InitContents);
48
49 /**
50 * New device that will create an offscreen renderTarget based on the ImageInfo and
51 * sampleCount. The mipMapped flag tells the gpu to create the underlying render target with
52 * mips. The Budgeted param controls whether the device's backing store counts against the
53 * resource cache budget. On failure, returns nullptr.
54 * This entry point creates a kExact backing store. It is used when creating SkGpuDevices
55 * for SkSurfaces.
56 */
57 static sk_sp<SkGpuDevice> Make(GrRecordingContext*, SkBudgeted, const SkImageInfo&,
58 int sampleCount, GrSurfaceOrigin, const SkSurfaceProps*,
59 GrMipmapped mipMapped, InitContents);
60
61 ~SkGpuDevice() override {}
62
63 GrContext* context() const override;
64 GrRecordingContext* recordingContext() const override { return fContext.get(); }
65
66 // set all pixels to 0
67 void clearAll();
68
69 void replaceRenderTargetContext(SkSurface::ContentChangeMode mode);
70 void replaceRenderTargetContext(std::unique_ptr<GrRenderTargetContext>,
71 SkSurface::ContentChangeMode mode);
72
73 GrRenderTargetContext* accessRenderTargetContext() override;
74
75 void drawPaint(const SkPaint& paint) override;
76 void drawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint[],
77 const SkPaint& paint) override;
78 void drawRect(const SkRect& r, const SkPaint& paint) override;
79 void drawRRect(const SkRRect& r, const SkPaint& paint) override;
80 void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) override;
81 void drawRegion(const SkRegion& r, const SkPaint& paint) override;
82 void drawOval(const SkRect& oval, const SkPaint& paint) override;
83 void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
84 bool useCenter, const SkPaint& paint) override;
85 void drawPath(const SkPath& path, const SkPaint& paint, bool pathIsMutable) override;
86
87 void drawGlyphRunList(const SkGlyphRunList& glyphRunList) override;
88 void drawVertices(const SkVertices*, SkBlendMode, const SkPaint&) override;
89 void drawShadow(const SkPath&, const SkDrawShadowRec&) override;
90 void drawAtlas(const SkImage* atlas, const SkRSXform[], const SkRect[],
91 const SkColor[], int count, SkBlendMode, const SkPaint&) override;
92
93 void drawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
94 const SkPaint&, SkCanvas::SrcRectConstraint) override;
95 void drawImageNine(const SkImage* image, const SkIRect& center,
96 const SkRect& dst, const SkPaint& paint) override;
97 void drawImageLattice(const SkImage*, const SkCanvas::Lattice&,
98 const SkRect& dst, const SkPaint&) override;
99
100 void drawDrawable(SkDrawable*, const SkMatrix*, SkCanvas* canvas) override;
101
102 void drawDevice(SkBaseDevice*, int x, int y, const SkPaint&) override;
103
104 void drawSpecial(SkSpecialImage*, int left, int top, const SkPaint&) override;
105
106 void drawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], SkCanvas::QuadAAFlags aaFlags,
107 const SkColor4f& color, SkBlendMode mode) override;
108 void drawEdgeAAImageSet(const SkCanvas::ImageSetEntry[], int count, const SkPoint dstClips[],
109 const SkMatrix[], const SkPaint&, SkCanvas::SrcRectConstraint) override;
110
111 sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
112 sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
113 sk_sp<SkSpecialImage> snapSpecial(const SkIRect&, bool = false) override;
114
115 void flush() override;
116 GrSemaphoresSubmitted flush(SkSurface::BackendSurfaceAccess access, const GrFlushInfo&,
117 const GrBackendSurfaceMutableState*);
118 bool wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores,
119 bool deleteSemaphoresAfterWait);
120
121 bool onAccessPixels(SkPixmap*) override;
122
123 bool android_utils_clipWithStencil() override;
124
125protected:
126 bool onReadPixels(const SkPixmap&, int, int) override;
127 bool onWritePixels(const SkPixmap&, int, int) override;
128
129private:
130 // We want these unreffed in RenderTargetContext, GrContext order.
131 sk_sp<GrRecordingContext> fContext;
132 std::unique_ptr<GrRenderTargetContext> fRenderTargetContext;
133 GrClipStackClip fClip;
134
135 enum Flags {
136 kNeedClear_Flag = 1 << 0, //!< Surface requires an initial clear
137 kIsOpaque_Flag = 1 << 1, //!< Hint from client that rendering to this device will be
138 // opaque even if the config supports alpha.
139 };
140 static bool CheckAlphaTypeAndGetFlags(const SkImageInfo* info, InitContents init,
141 unsigned* flags);
142
143 SkGpuDevice(GrRecordingContext*, std::unique_ptr<GrRenderTargetContext>, unsigned flags);
144
145 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
146
147 sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
148
149 SkImageFilterCache* getImageFilterCache() override;
150
151 bool forceConservativeRasterClip() const override { return true; }
152
153 const GrClip* clip() const { return &fClip; }
154
155 sk_sp<SkSpecialImage> filterTexture(SkSpecialImage*,
156 int left, int top,
157 SkIPoint* offset,
158 const SkImageFilter* filter);
159
160 // If not null, dstClip must be contained inside dst and will also respect the edge AA flags.
161 // If 'preViewMatrix' is not null, final CTM will be this->ctm() * preViewMatrix.
162 void drawImageQuad(const SkImage*, const SkRect* src, const SkRect* dst,
163 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
164 const SkMatrix* preViewMatrix, const SkPaint&, SkCanvas::SrcRectConstraint);
165
166 // FIXME(michaelludwig) - Should be removed in favor of using drawImageQuad with edge flags to
167 // for every element in the SkLatticeIter.
168 void drawProducerLattice(GrTextureProducer*, std::unique_ptr<SkLatticeIter>, const SkRect& dst,
169 const SkPaint&);
170
171 void drawStrokedLine(const SkPoint pts[2], const SkPaint&);
172
173 static std::unique_ptr<GrRenderTargetContext> MakeRenderTargetContext(GrRecordingContext*,
174 SkBudgeted,
175 const SkImageInfo&,
176 int sampleCount,
177 GrSurfaceOrigin,
178 const SkSurfaceProps*,
179 GrMipmapped);
180
181 friend class GrAtlasTextContext;
182 friend class SkSurface_Gpu; // for access to surfaceProps
183 typedef SkClipStackDevice INHERITED;
184};
185
186#endif
187