blob: a37d75245bdda2fb6e354ea82aaa44c3a6159dba [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#ifndef SkGpuDevice_DEFINED
9#define SkGpuDevice_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#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"
reed@google.comac10a2d2010-12-22 21:39:39 +000022
robertphillips64bf7672014-08-21 13:07:35 -070023class GrAccelData;
Brian Salomon34169692017-08-28 15:32:01 -040024class GrTextureMaker;
bsalomonb1b01992015-11-18 10:56:08 -080025class GrTextureProducer;
robertphillips64bf7672014-08-21 13:07:35 -070026struct GrCachedLayer;
commit-bot@chromium.orgddf2bfb2014-01-30 16:41:23 +000027
robertphillips970587b2016-07-14 14:12:55 -070028class SkSpecialImage;
29
reed@google.comac10a2d2010-12-22 21:39:39 +000030/**
reed89443ab2014-06-27 11:34:19 -070031 * Subclass of SkBaseDevice, which directs all drawing to the GrGpu owned by the
reed@google.comac10a2d2010-12-22 21:39:39 +000032 * canvas.
33 */
Mike Reed57c2b8b2017-12-31 15:23:54 -050034class SkGpuDevice : public SkClipStackDevice {
reed@google.comac10a2d2010-12-22 21:39:39 +000035public:
bsalomon74f681d2015-06-23 14:38:48 -070036 enum InitContents {
37 kClear_InitContents,
38 kUninit_InitContents
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +000039 };
bsalomon@google.com123ac1d2013-03-28 19:18:12 +000040
41 /**
Brian Osman11052242016-10-27 14:47:55 -040042 * Creates an SkGpuDevice from a GrRenderTargetContext whose backing width/height is
senorblancod0d37ca2015-04-02 04:54:56 -070043 * different than its actual width/height (e.g., approx-match scratch texture).
44 */
Brian Salomonbf6b9792019-08-21 09:38:10 -040045 static sk_sp<SkGpuDevice> Make(
46 GrContext*, std::unique_ptr<GrRenderTargetContext>, InitContents);
senorblancod0d37ca2015-04-02 04:54:56 -070047
48 /**
bsalomonafe30052015-01-16 07:32:33 -080049 * New device that will create an offscreen renderTarget based on the ImageInfo and
Greg Daniele252f082017-10-23 16:05:23 -040050 * sampleCount. The mipMapped flag tells the gpu to create the underlying render target with
51 * mips. The Budgeted param controls whether the device's backing store counts against the
52 * resource cache budget. On failure, returns nullptr.
Robert Phillips0ae6faa2017-03-21 16:22:00 -040053 * This entry point creates a kExact backing store. It is used when creating SkGpuDevices
54 * for SkSurfaces.
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000055 */
robertphillips24e91282016-04-29 06:46:36 -070056 static sk_sp<SkGpuDevice> Make(GrContext*, SkBudgeted, const SkImageInfo&,
Greg Daniele252f082017-10-23 16:05:23 -040057 int sampleCount, GrSurfaceOrigin, const SkSurfaceProps*,
58 GrMipMapped mipMapped, InitContents);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000059
joshualitt06dd0a82016-01-08 06:32:31 -080060 ~SkGpuDevice() override {}
reed@google.com7b201d22011-01-11 18:59:23 +000061
Hal Canary144caf52016-11-07 17:57:18 -050062 GrContext* context() const override { return fContext.get(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000063
reed8eddfb52014-12-04 07:50:14 -080064 // set all pixels to 0
65 void clearAll();
66
Brian Osman11052242016-10-27 14:47:55 -040067 void replaceRenderTargetContext(bool shouldRetainContent);
Brian Salomonbf6b9792019-08-21 09:38:10 -040068 void replaceRenderTargetContext(std::unique_ptr<GrRenderTargetContext>,
69 bool shouldRetainContent);
kkinnunenabcfab42015-02-22 22:53:44 -080070
Brian Osman11052242016-10-27 14:47:55 -040071 GrRenderTargetContext* accessRenderTargetContext() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000072
Mike Reeda1361362017-03-07 09:37:29 -050073 void drawPaint(const SkPaint& paint) override;
74 void drawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint[],
bsalomondcb406c2016-08-22 14:48:36 -070075 const SkPaint& paint) override;
Mike Reeda1361362017-03-07 09:37:29 -050076 void drawRect(const SkRect& r, const SkPaint& paint) override;
77 void drawRRect(const SkRRect& r, const SkPaint& paint) override;
Robert Phillips137ca522018-08-15 10:14:33 -040078 void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) override;
Mike Reeda1361362017-03-07 09:37:29 -050079 void drawRegion(const SkRegion& r, const SkPaint& paint) override;
80 void drawOval(const SkRect& oval, const SkPaint& paint) override;
81 void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
bsalomondcb406c2016-08-22 14:48:36 -070082 bool useCenter, const SkPaint& paint) override;
Robert Phillips137ca522018-08-15 10:14:33 -040083 void drawPath(const SkPath& path, const SkPaint& paint, bool pathIsMutable) override;
Mike Reeda1361362017-03-07 09:37:29 -050084 void drawBitmapRect(const SkBitmap&, const SkRect* srcOrNull, const SkRect& dst,
bsalomondcb406c2016-08-22 14:48:36 -070085 const SkPaint& paint, SkCanvas::SrcRectConstraint) override;
Mike Reeda1361362017-03-07 09:37:29 -050086 void drawSprite(const SkBitmap& bitmap, int x, int y,
bsalomondcb406c2016-08-22 14:48:36 -070087 const SkPaint& paint) override;
Herb Derbyb935cf82018-07-26 16:54:18 -040088 void drawGlyphRunList(const SkGlyphRunList& glyphRunList) override;
Ruiqi Maoc97a3392018-08-15 10:44:19 -040089 void drawVertices(const SkVertices*, const SkVertices::Bone bones[], int boneCount, SkBlendMode,
Ruiqi Maof5101492018-06-29 14:32:21 -040090 const SkPaint&) override;
Mike Reed4204da22017-05-17 08:53:36 -040091 void drawShadow(const SkPath&, const SkDrawShadowRec&) override;
Brian Osman4d92b892019-03-24 00:53:23 +000092 void drawAtlas(const SkImage* atlas, const SkRSXform[], const SkRect[],
93 const SkColor[], int count, SkBlendMode, const SkPaint&) override;
Mike Reeda1361362017-03-07 09:37:29 -050094 void drawDevice(SkBaseDevice*, int x, int y, const SkPaint&) override;
robertphillips970587b2016-07-14 14:12:55 -070095
Mike Reeda1361362017-03-07 09:37:29 -050096 void drawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
reeda5517e22015-07-14 10:54:12 -070097 const SkPaint&, SkCanvas::SrcRectConstraint) override;
reed@google.com7b201d22011-01-11 18:59:23 +000098
Mike Reeda1361362017-03-07 09:37:29 -050099 void drawImageNine(const SkImage* image, const SkIRect& center,
joshualitt33a5fce2015-11-18 13:28:51 -0800100 const SkRect& dst, const SkPaint& paint) override;
Mike Reeda1361362017-03-07 09:37:29 -0500101 void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
joshualitt33a5fce2015-11-18 13:28:51 -0800102 const SkRect& dst, const SkPaint& paint) override;
103
Mike Reeda1361362017-03-07 09:37:29 -0500104 void drawImageLattice(const SkImage*, const SkCanvas::Lattice&,
msarett10e3d9b2016-08-18 15:46:03 -0700105 const SkRect& dst, const SkPaint&) override;
Mike Reeda1361362017-03-07 09:37:29 -0500106 void drawBitmapLattice(const SkBitmap&, const SkCanvas::Lattice&,
msarett10e3d9b2016-08-18 15:46:03 -0700107 const SkRect& dst, const SkPaint&) override;
108
Greg Daniel9ed1a2c2018-10-18 12:43:25 -0400109 void drawDrawable(SkDrawable*, const SkMatrix*, SkCanvas* canvas) override;
110
Florin Malita53f77bd2017-04-28 13:48:37 -0400111 void drawSpecial(SkSpecialImage*, int left, int top, const SkPaint& paint,
112 SkImage*, const SkMatrix&) override;
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400113
Michael Ludwiga595f862019-08-27 15:25:49 -0400114 void drawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], SkCanvas::QuadAAFlags aaFlags,
115 const SkColor4f& color, SkBlendMode mode) override;
Michael Ludwig390f0cc2019-03-19 09:16:38 -0400116 void drawEdgeAAImageSet(const SkCanvas::ImageSetEntry[], int count, const SkPoint dstClips[],
117 const SkMatrix[], const SkPaint&, SkCanvas::SrcRectConstraint) override;
118
robertphillips6451a0c2016-07-18 08:31:31 -0700119 sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
reede51c3562016-07-19 14:33:20 -0700120 sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
Michael Ludwigac352122019-08-28 21:03:05 +0000121 sk_sp<SkSpecialImage> snapSpecial(const SkIRect&, bool = false) override;
robertphillips970587b2016-07-14 14:12:55 -0700122
mtklein36352bf2015-03-25 18:17:31 -0700123 void flush() override;
Greg Daniele6bfb7d2019-04-17 15:26:11 -0400124 GrSemaphoresSubmitted flush(SkSurface::BackendSurfaceAccess access, const GrFlushInfo&);
Greg Danielc64ee462017-06-15 16:59:49 -0400125 bool wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores);
reed@google.com7b201d22011-01-11 18:59:23 +0000126
reed41e010c2015-06-09 12:16:53 -0700127 bool onAccessPixels(SkPixmap*) override;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000128
reed@google.comac10a2d2010-12-22 21:39:39 +0000129protected:
Mike Reed353196f2017-07-21 11:01:18 -0400130 bool onReadPixels(const SkPixmap&, int, int) override;
131 bool onWritePixels(const SkPixmap&, int, int) override;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000132
reed@google.comac10a2d2010-12-22 21:39:39 +0000133private:
Brian Osman11052242016-10-27 14:47:55 -0400134 // We want these unreffed in RenderTargetContext, GrContext order.
Brian Salomonbf6b9792019-08-21 09:38:10 -0400135 sk_sp<GrContext> fContext;
136 std::unique_ptr<GrRenderTargetContext> fRenderTargetContext;
bsalomon74f681d2015-06-23 14:38:48 -0700137
138 enum Flags {
139 kNeedClear_Flag = 1 << 0, //!< Surface requires an initial clear
140 kIsOpaque_Flag = 1 << 1, //!< Hint from client that rendering to this device will be
141 // opaque even if the config supports alpha.
142 };
143 static bool CheckAlphaTypeAndGetFlags(const SkImageInfo* info, InitContents init,
144 unsigned* flags);
reed89443ab2014-06-27 11:34:19 -0700145
Brian Salomonbf6b9792019-08-21 09:38:10 -0400146 SkGpuDevice(GrContext*, std::unique_ptr<GrRenderTargetContext>, unsigned flags);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000147
mtklein36352bf2015-03-25 18:17:31 -0700148 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
bsalomon@google.come97f0852011-06-17 13:10:25 +0000149
reede8f30622016-03-23 18:59:25 -0700150 sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
reed@google.com76f10a32014-02-05 15:32:21 +0000151
senorblanco900c3672016-04-27 11:31:23 -0700152 SkImageFilterCache* getImageFilterCache() override;
reed841010f2014-09-10 07:23:32 -0700153
mtklein36352bf2015-03-25 18:17:31 -0700154 bool forceConservativeRasterClip() const override { return true; }
senorblanco55b6d8b2014-07-30 11:26:46 -0700155
Brian Salomon0166cfd2017-03-13 17:58:25 -0400156 GrClipStackClip clip() const { return GrClipStackClip(&this->cs()); }
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000157
Robert Phillips920d4882019-03-04 15:16:44 -0500158 const GrCaps* caps() const;
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400159
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000160 /**
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000161 * Helper functions called by drawBitmapCommon. By the time these are called the SkDraw's
commit-bot@chromium.org7edad872013-10-25 14:58:12 +0000162 * matrix, clip, and the device's render target has already been set on GrContext.
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000163 */
commit-bot@chromium.org7edad872013-10-25 14:58:12 +0000164
165 // The tileSize and clippedSrcRect will be valid only if true is returned.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400166 bool shouldTileImageID(uint32_t imageID,
167 const SkIRect& imageRect,
reed85d91782015-09-10 14:33:38 -0700168 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700169 const SkMatrix& srcToDstRectMatrix,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400170 const GrSamplerState& params,
reed85d91782015-09-10 14:33:38 -0700171 const SkRect* srcRectPtr,
172 int maxTileSize,
173 int* tileSize,
174 SkIRect* clippedSubset) const;
reed85d91782015-09-10 14:33:38 -0700175 // Just returns the predicate, not the out-tileSize or out-clippedSubset, as they are not
176 // needed at the moment.
177 bool shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
178 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700179 const SkMatrix& viewMatrix, const SkMatrix& srcToDstRect) const;
bsalomonc55271f2015-11-09 11:55:57 -0800180
Mike Reeda1361362017-03-07 09:37:29 -0500181 sk_sp<SkSpecialImage> filterTexture(SkSpecialImage*,
robertphillips970587b2016-07-14 14:12:55 -0700182 int left, int top,
183 SkIPoint* offset,
184 const SkImageFilter* filter);
185
bsalomone553b642016-08-17 09:02:09 -0700186 // Splits bitmap into tiles of tileSize and draws them using separate textures for each tile.
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000187 void drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800188 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700189 const SkMatrix& srcToDstMatrix,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000190 const SkRect& srcRect,
commit-bot@chromium.org7edad872013-10-25 14:58:12 +0000191 const SkIRect& clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400192 const GrSamplerState& params,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000193 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -0700194 SkCanvas::SrcRectConstraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000195 int tileSize,
196 bool bicubic);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000197
bsalomone553b642016-08-17 09:02:09 -0700198 // Used by drawTiledBitmap to draw each tile.
199 void drawBitmapTile(const SkBitmap&,
200 const SkMatrix& viewMatrix,
201 const SkRect& dstRect,
202 const SkRect& srcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400203 const GrSamplerState& samplerState,
bsalomone553b642016-08-17 09:02:09 -0700204 const SkPaint& paint,
205 SkCanvas::SrcRectConstraint,
206 bool bicubic,
207 bool needsTextureDomain);
208
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500209 // If not null, dstClip must be contained inside dst and will also respect the edge AA flags.
210 // If 'preViewMatrix' is not null, final CTM will be this->ctm() * preViewMatrix.
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500211 void drawImageQuad(const SkImage*, const SkRect* src, const SkRect* dst,
212 const SkPoint dstClip[4], GrAA aa, GrQuadAAFlags aaFlags,
Michael Ludwig7ae2ab52019-03-05 16:00:20 -0500213 const SkMatrix* preViewMatrix, const SkPaint&, SkCanvas::SrcRectConstraint);
Brian Salomon34169692017-08-28 15:32:01 -0400214
Michael Ludwig1433cfd2019-02-27 17:12:30 -0500215 // TODO(michaelludwig): This can be removed once drawBitmapRect is removed from SkDevice
216 // so that drawImageQuad is the sole entry point into the draw-single-image op
bsalomonb1b01992015-11-18 10:56:08 -0800217 void drawTextureProducer(GrTextureProducer*,
bsalomonc55271f2015-11-09 11:55:57 -0800218 const SkRect* srcRect,
219 const SkRect* dstRect,
bsalomonb1b01992015-11-18 10:56:08 -0800220 SkCanvas::SrcRectConstraint,
bsalomonc55271f2015-11-09 11:55:57 -0800221 const SkMatrix& viewMatrix,
Jim Van Verth30e0d7f2018-11-02 13:36:42 -0400222 const SkPaint&,
223 bool attemptDrawTexture);
bsalomonc55271f2015-11-09 11:55:57 -0800224
Brian Salomon2a943df2018-05-04 13:43:19 -0400225 void drawProducerLattice(GrTextureProducer*, std::unique_ptr<SkLatticeIter>, const SkRect& dst,
226 const SkPaint&);
robertphillipsd7706102016-02-25 09:28:08 -0800227
Mike Reeda1361362017-03-07 09:37:29 -0500228 void drawStrokedLine(const SkPoint pts[2], const SkPaint&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000229
Mike Reed887cdf12017-04-03 11:11:09 -0400230 void wireframeVertices(SkVertices::VertexMode, int vertexCount, const SkPoint verts[],
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400231 const SkVertices::Bone bones[], int boneCount, SkBlendMode,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400232 const uint16_t indices[], int indexCount, const SkPaint&);
Mike Reed2f6b5a42017-03-19 15:04:17 -0400233
Brian Salomonbf6b9792019-08-21 09:38:10 -0400234 static std::unique_ptr<GrRenderTargetContext> MakeRenderTargetContext(GrContext*,
235 SkBudgeted,
236 const SkImageInfo&,
237 int sampleCount,
238 GrSurfaceOrigin,
239 const SkSurfaceProps*,
240 GrMipMapped);
kkinnunenabcfab42015-02-22 22:53:44 -0800241
joshualittdbd35932015-04-02 09:19:04 -0700242 friend class GrAtlasTextContext;
robertphillips7b05ff12015-06-19 14:14:54 -0700243 friend class SkSurface_Gpu; // for access to surfaceProps
Mike Reedc5e641c2017-02-17 14:38:11 -0500244 typedef SkClipStackDevice INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000245};
246
247#endif