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/gpu/BaseDevice.h" 17 #include "src/gpu/SkGr.h" 18 #include "src/gpu/v1/ClipStack.h" 19 #include "src/gpu/v1/SurfaceDrawContext_v1.h" 20 21 class SkSpecialImage; 22 class SkSurface; 23 class SkSurface_Gpu; 24 class SkVertices; 25 26 namespace skgpu::v1 { 27 28 /** 29 * Subclass of BaseDevice, which directs all drawing to the GrGpu owned by the canvas. 30 */ 31 class Device final : public BaseDevice { 32 public: 33 bool wait(int numSemaphores, 34 const GrBackendSemaphore* waitSemaphores, 35 bool deleteSemaphoresAfterWait) override; 36 discard()37 void discard() override { 38 fSurfaceDrawContext->discard(); 39 } 40 resolveMSAA()41 void resolveMSAA() override { 42 fSurfaceDrawContext->resolveMSAA(); 43 } 44 45 bool replaceBackingProxy(SkSurface::ContentChangeMode, 46 sk_sp<GrRenderTargetProxy>, 47 GrColorType, 48 sk_sp<SkColorSpace>, 49 GrSurfaceOrigin, 50 const SkSurfaceProps&) override; 51 using BaseDevice::replaceBackingProxy; 52 53 void asyncRescaleAndReadPixels(const SkImageInfo& info, 54 const SkIRect& srcRect, 55 RescaleGamma rescaleGamma, 56 RescaleMode rescaleMode, 57 ReadPixelsCallback callback, 58 ReadPixelsContext context) override; 59 60 void asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace, 61 sk_sp<SkColorSpace> dstColorSpace, 62 const SkIRect& srcRect, 63 SkISize dstSize, 64 RescaleGamma rescaleGamma, 65 RescaleMode, 66 ReadPixelsCallback callback, 67 ReadPixelsContext context) override; 68 69 /** 70 * This factory uses the color space, origin, surface properties, and initialization 71 * method along with the provided proxy to create the gpu device. 72 */ 73 static sk_sp<BaseDevice> Make(GrRecordingContext*, 74 GrColorType, 75 sk_sp<GrSurfaceProxy>, 76 sk_sp<SkColorSpace>, 77 GrSurfaceOrigin, 78 const SkSurfaceProps&, 79 InitContents); 80 81 /** 82 * This factory uses the budgeted, imageInfo, fit, sampleCount, mipmapped, and isProtected 83 * parameters to create a proxy to back the gpu device. The color space (from the image info), 84 * origin, surface properties, and initialization method are then used (with the created proxy) 85 * to create the device. 86 */ 87 static sk_sp<BaseDevice> Make(GrRecordingContext*, 88 SkBudgeted, 89 const SkImageInfo&, 90 SkBackingFit, 91 int sampleCount, 92 GrMipmapped, 93 GrProtected, 94 GrSurfaceOrigin, 95 const SkSurfaceProps&, 96 InitContents); 97 ~Device()98 ~Device() override {} 99 100 SurfaceDrawContext* surfaceDrawContext() override; 101 const SurfaceDrawContext* surfaceDrawContext() const; 102 skgpu::SurfaceFillContext* surfaceFillContext() override; 103 104 // set all pixels to 0 105 void clearAll(); 106 107 void drawPaint(const SkPaint& paint) override; 108 void drawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint[], 109 const SkPaint& paint) override; 110 void drawRect(const SkRect& r, const SkPaint& paint) override; 111 void drawRRect(const SkRRect& r, const SkPaint& paint) override; 112 void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) override; 113 void drawRegion(const SkRegion& r, const SkPaint& paint) override; 114 void drawOval(const SkRect& oval, const SkPaint& paint) override; 115 void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, 116 bool useCenter, const SkPaint& paint) override; 117 void drawPath(const SkPath& path, const SkPaint& paint, bool pathIsMutable) override; 118 119 void drawVertices(const SkVertices*, sk_sp<SkBlender>, const SkPaint&) override; 120 void drawCustomMesh(SkCustomMesh, sk_sp<SkBlender>, const SkPaint&) override; 121 void drawShadow(const SkPath&, const SkDrawShadowRec&) override; 122 void drawAtlas(const SkRSXform[], const SkRect[], const SkColor[], int count, sk_sp<SkBlender>, 123 const SkPaint&) override; 124 125 void drawImageRect(const SkImage*, const SkRect* src, const SkRect& dst, 126 const SkSamplingOptions&, const SkPaint&, 127 SkCanvas::SrcRectConstraint) override; 128 void drawImageLattice(const SkImage*, const SkCanvas::Lattice&, 129 const SkRect& dst, SkFilterMode, const SkPaint&) override; 130 131 void drawDrawable(SkCanvas*, SkDrawable*, const SkMatrix*) override; 132 133 void drawDevice(SkBaseDevice*, const SkSamplingOptions&, const SkPaint&) override; 134 void drawSpecial(SkSpecialImage*, const SkMatrix& localToDevice, const SkSamplingOptions&, 135 const SkPaint&) override; 136 137 void drawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], SkCanvas::QuadAAFlags aaFlags, 138 const SkColor4f& color, SkBlendMode mode) override; 139 void drawEdgeAAImageSet(const SkCanvas::ImageSetEntry[], int count, const SkPoint dstClips[], 140 const SkMatrix[], const SkSamplingOptions&, const SkPaint&, 141 SkCanvas::SrcRectConstraint) override; 142 143 sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override; 144 sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override; 145 sk_sp<SkSpecialImage> snapSpecial(const SkIRect& subset, bool forceCopy = false) override; 146 147 bool onAccessPixels(SkPixmap*) override; 148 149 bool android_utils_clipWithStencil() override; 150 151 protected: 152 bool onReadPixels(const SkPixmap&, int, int) override; 153 bool onWritePixels(const SkPixmap&, int, int) override; 154 onSave()155 void onSave() override { fClip.save(); } onRestore()156 void onRestore() override { fClip.restore(); } 157 158 void onDrawGlyphRunList(SkCanvas*, const SkGlyphRunList&, const SkPaint&) override; 159 160 sk_sp<GrSlug> convertGlyphRunListToSlug( 161 const SkGlyphRunList& glyphRunList, 162 const SkPaint& paint) override; 163 164 void drawSlug(SkCanvas*, GrSlug* slug) override; 165 onClipRect(const SkRect & rect,SkClipOp op,bool aa)166 void onClipRect(const SkRect& rect, SkClipOp op, bool aa) override { 167 SkASSERT(op == SkClipOp::kIntersect || op == SkClipOp::kDifference); 168 fClip.clipRect(this->localToDevice(), rect, GrAA(aa), op); 169 } onClipRRect(const SkRRect & rrect,SkClipOp op,bool aa)170 void onClipRRect(const SkRRect& rrect, SkClipOp op, bool aa) override { 171 SkASSERT(op == SkClipOp::kIntersect || op == SkClipOp::kDifference); 172 fClip.clipRRect(this->localToDevice(), rrect, GrAA(aa), op); 173 } 174 void onClipPath(const SkPath& path, SkClipOp op, bool aa) override; onClipShader(sk_sp<SkShader> shader)175 void onClipShader(sk_sp<SkShader> shader) override { 176 fClip.clipShader(std::move(shader)); 177 } onReplaceClip(const SkIRect & rect)178 void onReplaceClip(const SkIRect& rect) override { 179 // Transform from "global/canvas" coordinates to relative to this device 180 SkRect deviceRect = SkMatrixPriv::MapRect(this->globalToDevice(), SkRect::Make(rect)); 181 fClip.replaceClip(deviceRect.round()); 182 } 183 void onClipRegion(const SkRegion& globalRgn, SkClipOp op) override; 184 void onAsRgnClip(SkRegion*) const override; 185 ClipType onGetClipType() const override; 186 bool onClipIsAA() const override; 187 onClipIsWideOpen()188 bool onClipIsWideOpen() const override { 189 return fClip.clipState() == ClipStack::ClipState::kWideOpen; 190 } onDevClipBounds()191 SkIRect onDevClipBounds() const override { return fClip.getConservativeBounds(); } 192 193 private: 194 std::unique_ptr<SurfaceDrawContext> fSurfaceDrawContext; 195 196 ClipStack fClip; 197 198 static sk_sp<BaseDevice> Make(std::unique_ptr<SurfaceDrawContext>, 199 SkAlphaType, 200 InitContents); 201 202 Device(std::unique_ptr<SurfaceDrawContext>, DeviceFlags); 203 204 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override; 205 206 sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override; 207 208 SkImageFilterCache* getImageFilterCache() override; 209 forceConservativeRasterClip()210 bool forceConservativeRasterClip() const override { return true; } 211 clip()212 const GrClip* clip() const { return &fClip; } 213 214 // If not null, dstClip must be contained inside dst and will also respect the edge AA flags. 215 // If 'preViewMatrix' is not null, final CTM will be this->ctm() * preViewMatrix. 216 void drawImageQuad(const SkImage*, const SkRect* src, const SkRect* dst, 217 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags, 218 const SkMatrix* preViewMatrix, const SkSamplingOptions&, 219 const SkPaint&, SkCanvas::SrcRectConstraint); 220 221 // FIXME(michaelludwig) - Should be removed in favor of using drawImageQuad with edge flags to 222 // for every element in the SkLatticeIter. 223 void drawViewLattice(GrSurfaceProxyView, 224 const GrColorInfo& colorInfo, 225 std::unique_ptr<SkLatticeIter>, 226 const SkRect& dst, 227 SkFilterMode, 228 const SkPaint&); 229 230 friend class ::SkSurface_Gpu; // for access to surfaceProps 231 using INHERITED = BaseDevice; 232 }; 233 234 } // namespace skgpu::v1 235 236 #undef GR_CLIP_STACK 237 238 #endif 239