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
23class GrAccelData;
24class GrTextureMaker;
25class GrTextureProducer;
26struct GrCachedLayer;
27
28class SkSpecialImage;
29class SkSurface;
30class SkVertices;
31
32/**
33 * Subclass of SkBaseDevice, which directs all drawing to the GrGpu owned by the
34 * canvas.
35 */
36class SkGpuDevice : public SkClipStackDevice {
37public:
38 enum InitContents {
39 kClear_InitContents,
40 kUninit_InitContents
41 };
42
43 /**
44 * Creates an SkGpuDevice from a GrRenderTargetContext whose backing width/height is
45 * different than its actual width/height (e.g., approx-match scratch texture).
46 */
47 static sk_sp<SkGpuDevice> Make(
48 GrContext*, std::unique_ptr<GrRenderTargetContext>, InitContents);
49
50 /**
51 * New device that will create an offscreen renderTarget based on the ImageInfo and
52 * sampleCount. The mipMapped flag tells the gpu to create the underlying render target with
53 * mips. The Budgeted param controls whether the device's backing store counts against the
54 * resource cache budget. On failure, returns nullptr.
55 * This entry point creates a kExact backing store. It is used when creating SkGpuDevices
56 * for SkSurfaces.
57 */
58 static sk_sp<SkGpuDevice> Make(GrContext*, SkBudgeted, const SkImageInfo&,
59 int sampleCount, GrSurfaceOrigin, const SkSurfaceProps*,
60 GrMipMapped mipMapped, InitContents);
61
62 ~SkGpuDevice() override {}
63
64 GrContext* context() 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& paint,
105 SkImage*, const SkMatrix&) override;
106
107 void drawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], SkCanvas::QuadAAFlags aaFlags,
108 const SkColor4f& color, SkBlendMode mode) override;
109 void drawEdgeAAImageSet(const SkCanvas::ImageSetEntry[], int count, const SkPoint dstClips[],
110 const SkMatrix[], const SkPaint&, SkCanvas::SrcRectConstraint) override;
111
112 sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
113 sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
114 sk_sp<SkSpecialImage> snapSpecial(const SkIRect&, bool = false) override;
115
116 void flush() override;
117 GrSemaphoresSubmitted flush(SkSurface::BackendSurfaceAccess access, const GrFlushInfo&);
118 bool wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores);
119
120 bool onAccessPixels(SkPixmap*) override;
121
122 bool android_utils_clipWithStencil() override;
123
124protected:
125 bool onReadPixels(const SkPixmap&, int, int) override;
126 bool onWritePixels(const SkPixmap&, int, int) override;
127
128private:
129 // We want these unreffed in RenderTargetContext, GrContext order.
130 sk_sp<GrContext> fContext;
131 std::unique_ptr<GrRenderTargetContext> fRenderTargetContext;
132
133 enum Flags {
134 kNeedClear_Flag = 1 << 0, //!< Surface requires an initial clear
135 kIsOpaque_Flag = 1 << 1, //!< Hint from client that rendering to this device will be
136 // opaque even if the config supports alpha.
137 };
138 static bool CheckAlphaTypeAndGetFlags(const SkImageInfo* info, InitContents init,
139 unsigned* flags);
140
141 SkGpuDevice(GrContext*, std::unique_ptr<GrRenderTargetContext>, unsigned flags);
142
143 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
144
145 sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
146
147 SkImageFilterCache* getImageFilterCache() override;
148
149 bool forceConservativeRasterClip() const override { return true; }
150
151 GrClipStackClip clip() const { return GrClipStackClip(&this->cs()); }
152
153 sk_sp<SkSpecialImage> filterTexture(SkSpecialImage*,
154 int left, int top,
155 SkIPoint* offset,
156 const SkImageFilter* filter);
157
158 // If not null, dstClip must be contained inside dst and will also respect the edge AA flags.
159 // If 'preViewMatrix' is not null, final CTM will be this->ctm() * preViewMatrix.
160 void drawImageQuad(const SkImage*, const SkRect* src, const SkRect* dst,
161 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
162 const SkMatrix* preViewMatrix, const SkPaint&, SkCanvas::SrcRectConstraint);
163
164 // FIXME(michaelludwig) - Should be removed in favor of using drawImageQuad with edge flags to
165 // for every element in the SkLatticeIter.
166 void drawProducerLattice(GrTextureProducer*, std::unique_ptr<SkLatticeIter>, const SkRect& dst,
167 const SkPaint&);
168
169 void drawStrokedLine(const SkPoint pts[2], const SkPaint&);
170
171 static std::unique_ptr<GrRenderTargetContext> MakeRenderTargetContext(GrContext*,
172 SkBudgeted,
173 const SkImageInfo&,
174 int sampleCount,
175 GrSurfaceOrigin,
176 const SkSurfaceProps*,
177 GrMipMapped);
178
179 friend class GrAtlasTextContext;
180 friend class SkSurface_Gpu; // for access to surfaceProps
181 typedef SkClipStackDevice INHERITED;
182};
183
184#endif
185