blob: a1a6606c60f49f466c23c04a99dd875e3c9c9657 [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
Brian Salomonc7fe0f72018-05-11 10:14:21 -040011#include "GrClipStackClip.h"
12#include "GrContext.h"
13#include "GrContextPriv.h"
14#include "GrRenderTargetContext.h"
15#include "GrTypes.h"
reed@google.comaf951c92011-06-16 19:10:39 +000016#include "SkBitmap.h"
Mike Reedc5e641c2017-02-17 14:38:11 -050017#include "SkClipStackDevice.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040018#include "SkGr.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000019#include "SkPicture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020#include "SkRegion.h"
bsalomonafe30052015-01-16 07:32:33 -080021#include "SkSurface.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 */
Robert Phillips9fab7e92016-11-17 12:45:04 -050045 static sk_sp<SkGpuDevice> Make(GrContext*, sk_sp<GrRenderTargetContext> renderTargetContext,
46 int width, int height, 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);
kkinnunenabcfab42015-02-22 22:53:44 -080068
Brian Osman11052242016-10-27 14:47:55 -040069 GrRenderTargetContext* accessRenderTargetContext() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000070
Mike Reeda1361362017-03-07 09:37:29 -050071 void drawPaint(const SkPaint& paint) override;
72 void drawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint[],
bsalomondcb406c2016-08-22 14:48:36 -070073 const SkPaint& paint) override;
Mike Reeda1361362017-03-07 09:37:29 -050074 void drawRect(const SkRect& r, const SkPaint& paint) override;
75 void drawRRect(const SkRRect& r, const SkPaint& paint) override;
Robert Phillips137ca522018-08-15 10:14:33 -040076 void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) override;
Mike Reeda1361362017-03-07 09:37:29 -050077 void drawRegion(const SkRegion& r, const SkPaint& paint) override;
78 void drawOval(const SkRect& oval, const SkPaint& paint) override;
79 void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
bsalomondcb406c2016-08-22 14:48:36 -070080 bool useCenter, const SkPaint& paint) override;
Robert Phillips137ca522018-08-15 10:14:33 -040081 void drawPath(const SkPath& path, const SkPaint& paint, bool pathIsMutable) override;
82 void drawBitmap(const SkBitmap&, SkScalar x, SkScalar y, const SkPaint&) override;
Mike Reeda1361362017-03-07 09:37:29 -050083 void drawBitmapRect(const SkBitmap&, const SkRect* srcOrNull, const SkRect& dst,
bsalomondcb406c2016-08-22 14:48:36 -070084 const SkPaint& paint, SkCanvas::SrcRectConstraint) override;
Mike Reeda1361362017-03-07 09:37:29 -050085 void drawSprite(const SkBitmap& bitmap, int x, int y,
bsalomondcb406c2016-08-22 14:48:36 -070086 const SkPaint& paint) override;
Herb Derbyb935cf82018-07-26 16:54:18 -040087 void drawGlyphRunList(const SkGlyphRunList& glyphRunList) override;
Ruiqi Maoc97a3392018-08-15 10:44:19 -040088 void drawVertices(const SkVertices*, const SkVertices::Bone bones[], int boneCount, SkBlendMode,
Ruiqi Maof5101492018-06-29 14:32:21 -040089 const SkPaint&) override;
Mike Reed4204da22017-05-17 08:53:36 -040090 void drawShadow(const SkPath&, const SkDrawShadowRec&) override;
Mike Reeda1361362017-03-07 09:37:29 -050091 void drawAtlas(const SkImage* atlas, const SkRSXform[], const SkRect[],
Mike Reedfaba3712016-11-03 14:45:31 -040092 const SkColor[], int count, SkBlendMode, const SkPaint&) override;
Mike Reeda1361362017-03-07 09:37:29 -050093 void drawDevice(SkBaseDevice*, int x, int y, const SkPaint&) override;
robertphillips970587b2016-07-14 14:12:55 -070094
Mike Reeda1361362017-03-07 09:37:29 -050095 void drawImage(const SkImage*, SkScalar x, SkScalar y, const SkPaint&) override;
96 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
Florin Malita53f77bd2017-04-28 13:48:37 -0400109 void drawSpecial(SkSpecialImage*, int left, int top, const SkPaint& paint,
110 SkImage*, const SkMatrix&) override;
robertphillips6451a0c2016-07-18 08:31:31 -0700111 sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
reede51c3562016-07-19 14:33:20 -0700112 sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
robertphillips6451a0c2016-07-18 08:31:31 -0700113 sk_sp<SkSpecialImage> snapSpecial() override;
robertphillips970587b2016-07-14 14:12:55 -0700114
mtklein36352bf2015-03-25 18:17:31 -0700115 void flush() override;
Greg Daniel51316782017-08-02 15:10:09 +0000116 GrSemaphoresSubmitted flushAndSignalSemaphores(int numSemaphores,
117 GrBackendSemaphore signalSemaphores[]);
Greg Danielc64ee462017-06-15 16:59:49 -0400118 bool wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores);
reed@google.com7b201d22011-01-11 18:59:23 +0000119
reed41e010c2015-06-09 12:16:53 -0700120 bool onAccessPixels(SkPixmap*) override;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000121
reed@google.comac10a2d2010-12-22 21:39:39 +0000122protected:
Mike Reed353196f2017-07-21 11:01:18 -0400123 bool onReadPixels(const SkPixmap&, int, int) override;
124 bool onWritePixels(const SkPixmap&, int, int) override;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000125
reed@google.comac10a2d2010-12-22 21:39:39 +0000126private:
Brian Osman11052242016-10-27 14:47:55 -0400127 // We want these unreffed in RenderTargetContext, GrContext order.
Hal Canary144caf52016-11-07 17:57:18 -0500128 sk_sp<GrContext> fContext;
129 sk_sp<GrRenderTargetContext> fRenderTargetContext;
joshualitt06dd0a82016-01-08 06:32:31 -0800130
Hal Canary144caf52016-11-07 17:57:18 -0500131 SkISize fSize;
132 bool fOpaque;
bsalomon74f681d2015-06-23 14:38:48 -0700133
134 enum Flags {
135 kNeedClear_Flag = 1 << 0, //!< Surface requires an initial clear
136 kIsOpaque_Flag = 1 << 1, //!< Hint from client that rendering to this device will be
137 // opaque even if the config supports alpha.
138 };
139 static bool CheckAlphaTypeAndGetFlags(const SkImageInfo* info, InitContents init,
140 unsigned* flags);
reed89443ab2014-06-27 11:34:19 -0700141
Robert Phillips9fab7e92016-11-17 12:45:04 -0500142 SkGpuDevice(GrContext*, sk_sp<GrRenderTargetContext>, int width, int height, unsigned flags);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000143
mtklein36352bf2015-03-25 18:17:31 -0700144 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
bsalomon@google.come97f0852011-06-17 13:10:25 +0000145
reede8f30622016-03-23 18:59:25 -0700146 sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
reed@google.com76f10a32014-02-05 15:32:21 +0000147
senorblanco900c3672016-04-27 11:31:23 -0700148 SkImageFilterCache* getImageFilterCache() override;
reed841010f2014-09-10 07:23:32 -0700149
mtklein36352bf2015-03-25 18:17:31 -0700150 bool forceConservativeRasterClip() const override { return true; }
senorblanco55b6d8b2014-07-30 11:26:46 -0700151
Brian Salomon0166cfd2017-03-13 17:58:25 -0400152 GrClipStackClip clip() const { return GrClipStackClip(&this->cs()); }
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000153
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400154 const GrCaps* caps() const { return fContext->contextPriv().caps(); }
155
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000156 /**
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000157 * Helper functions called by drawBitmapCommon. By the time these are called the SkDraw's
commit-bot@chromium.org7edad872013-10-25 14:58:12 +0000158 * matrix, clip, and the device's render target has already been set on GrContext.
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000159 */
commit-bot@chromium.org7edad872013-10-25 14:58:12 +0000160
161 // The tileSize and clippedSrcRect will be valid only if true is returned.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400162 bool shouldTileImageID(uint32_t imageID,
163 const SkIRect& imageRect,
reed85d91782015-09-10 14:33:38 -0700164 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700165 const SkMatrix& srcToDstRectMatrix,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400166 const GrSamplerState& params,
reed85d91782015-09-10 14:33:38 -0700167 const SkRect* srcRectPtr,
168 int maxTileSize,
169 int* tileSize,
170 SkIRect* clippedSubset) const;
reed85d91782015-09-10 14:33:38 -0700171 // Just returns the predicate, not the out-tileSize or out-clippedSubset, as they are not
172 // needed at the moment.
173 bool shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
174 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700175 const SkMatrix& viewMatrix, const SkMatrix& srcToDstRect) const;
bsalomonc55271f2015-11-09 11:55:57 -0800176
Mike Reeda1361362017-03-07 09:37:29 -0500177 sk_sp<SkSpecialImage> filterTexture(SkSpecialImage*,
robertphillips970587b2016-07-14 14:12:55 -0700178 int left, int top,
179 SkIPoint* offset,
180 const SkImageFilter* filter);
181
bsalomone553b642016-08-17 09:02:09 -0700182 // Splits bitmap into tiles of tileSize and draws them using separate textures for each tile.
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000183 void drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800184 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700185 const SkMatrix& srcToDstMatrix,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000186 const SkRect& srcRect,
commit-bot@chromium.org7edad872013-10-25 14:58:12 +0000187 const SkIRect& clippedSrcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400188 const GrSamplerState& params,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000189 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -0700190 SkCanvas::SrcRectConstraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000191 int tileSize,
192 bool bicubic);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000193
bsalomone553b642016-08-17 09:02:09 -0700194 // Used by drawTiledBitmap to draw each tile.
195 void drawBitmapTile(const SkBitmap&,
196 const SkMatrix& viewMatrix,
197 const SkRect& dstRect,
198 const SkRect& srcRect,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400199 const GrSamplerState& samplerState,
bsalomone553b642016-08-17 09:02:09 -0700200 const SkPaint& paint,
201 SkCanvas::SrcRectConstraint,
202 bool bicubic,
203 bool needsTextureDomain);
204
Brian Salomon34169692017-08-28 15:32:01 -0400205 void drawPinnedTextureProxy(sk_sp<GrTextureProxy>,
206 uint32_t pinnedUniqueID,
207 SkColorSpace*,
208 SkAlphaType alphaType,
209 const SkRect* srcRect,
210 const SkRect* dstRect,
211 SkCanvas::SrcRectConstraint,
212 const SkMatrix& viewMatrix,
213 const SkPaint&);
214
215 void drawTextureMaker(GrTextureMaker* maker,
216 int imageW,
217 int imageH,
218 const SkRect* srcRect,
219 const SkRect* dstRect,
220 SkCanvas::SrcRectConstraint,
221 const SkMatrix& viewMatrix,
222 const SkPaint&);
223
bsalomonb1b01992015-11-18 10:56:08 -0800224 void drawTextureProducer(GrTextureProducer*,
bsalomonc55271f2015-11-09 11:55:57 -0800225 const SkRect* srcRect,
226 const SkRect* dstRect,
bsalomonb1b01992015-11-18 10:56:08 -0800227 SkCanvas::SrcRectConstraint,
bsalomonc55271f2015-11-09 11:55:57 -0800228 const SkMatrix& viewMatrix,
bsalomonb1b01992015-11-18 10:56:08 -0800229 const SkPaint&);
bsalomonc55271f2015-11-09 11:55:57 -0800230
bsalomonb1b01992015-11-18 10:56:08 -0800231 void drawTextureProducerImpl(GrTextureProducer*,
bsalomonc55271f2015-11-09 11:55:57 -0800232 const SkRect& clippedSrcRect,
233 const SkRect& clippedDstRect,
bsalomonb1b01992015-11-18 10:56:08 -0800234 SkCanvas::SrcRectConstraint,
bsalomonc55271f2015-11-09 11:55:57 -0800235 const SkMatrix& viewMatrix,
236 const SkMatrix& srcToDstMatrix,
bsalomonb1b01992015-11-18 10:56:08 -0800237 const SkPaint&);
bsalomonc55271f2015-11-09 11:55:57 -0800238
Brian Salomon2a943df2018-05-04 13:43:19 -0400239 void drawProducerLattice(GrTextureProducer*, std::unique_ptr<SkLatticeIter>, const SkRect& dst,
240 const SkPaint&);
robertphillipsd7706102016-02-25 09:28:08 -0800241
Mike Reeda1361362017-03-07 09:37:29 -0500242 void drawStrokedLine(const SkPoint pts[2], const SkPaint&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000243
Mike Reed887cdf12017-04-03 11:11:09 -0400244 void wireframeVertices(SkVertices::VertexMode, int vertexCount, const SkPoint verts[],
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400245 const SkVertices::Bone bones[], int boneCount, SkBlendMode,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400246 const uint16_t indices[], int indexCount, const SkPaint&);
Mike Reed2f6b5a42017-03-19 15:04:17 -0400247
Brian Osman11052242016-10-27 14:47:55 -0400248 static sk_sp<GrRenderTargetContext> MakeRenderTargetContext(GrContext*,
249 SkBudgeted,
250 const SkImageInfo&,
251 int sampleCount,
252 GrSurfaceOrigin,
Greg Daniele252f082017-10-23 16:05:23 -0400253 const SkSurfaceProps*,
254 GrMipMapped);
kkinnunenabcfab42015-02-22 22:53:44 -0800255
joshualittdbd35932015-04-02 09:19:04 -0700256 friend class GrAtlasTextContext;
robertphillips7b05ff12015-06-19 14:14:54 -0700257 friend class SkSurface_Gpu; // for access to surfaceProps
Mike Reedc5e641c2017-02-17 14:38:11 -0500258 typedef SkClipStackDevice INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000259};
260
261#endif