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