blob: 0b60a965161c8238f8ef8991712c68bc1b1f3226 [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
11#include "SkGr.h"
reed@google.comaf951c92011-06-16 19:10:39 +000012#include "SkBitmap.h"
Mike Reedc5e641c2017-02-17 14:38:11 -050013#include "SkClipStackDevice.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000014#include "SkPicture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015#include "SkRegion.h"
bsalomonafe30052015-01-16 07:32:33 -080016#include "SkSurface.h"
csmartdaltonc6f411e2016-08-05 22:32:12 -070017#include "GrClipStackClip.h"
Brian Osman11052242016-10-27 14:47:55 -040018#include "GrRenderTargetContext.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000019#include "GrContext.h"
bsalomonafbf2d62014-09-30 12:18:44 -070020#include "GrSurfacePriv.h"
erikchen9a1ed5d2016-02-10 16:32:34 -080021#include "GrTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000022
robertphillips64bf7672014-08-21 13:07:35 -070023class GrAccelData;
bsalomonb1b01992015-11-18 10:56:08 -080024class GrTextureProducer;
robertphillips64bf7672014-08-21 13:07:35 -070025struct GrCachedLayer;
commit-bot@chromium.orgddf2bfb2014-01-30 16:41:23 +000026
robertphillips970587b2016-07-14 14:12:55 -070027class SkSpecialImage;
28
reed@google.comac10a2d2010-12-22 21:39:39 +000029/**
reed89443ab2014-06-27 11:34:19 -070030 * Subclass of SkBaseDevice, which directs all drawing to the GrGpu owned by the
reed@google.comac10a2d2010-12-22 21:39:39 +000031 * canvas.
32 */
Mike Reedc5e641c2017-02-17 14:38:11 -050033class SK_API SkGpuDevice : public SkClipStackDevice {
reed@google.comac10a2d2010-12-22 21:39:39 +000034public:
bsalomon74f681d2015-06-23 14:38:48 -070035 enum InitContents {
36 kClear_InitContents,
37 kUninit_InitContents
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +000038 };
bsalomon@google.com123ac1d2013-03-28 19:18:12 +000039
40 /**
Brian Osman11052242016-10-27 14:47:55 -040041 * Creates an SkGpuDevice from a GrRenderTargetContext whose backing width/height is
senorblancod0d37ca2015-04-02 04:54:56 -070042 * different than its actual width/height (e.g., approx-match scratch texture).
43 */
Robert Phillips9fab7e92016-11-17 12:45:04 -050044 static sk_sp<SkGpuDevice> Make(GrContext*, sk_sp<GrRenderTargetContext> renderTargetContext,
45 int width, int height, InitContents);
senorblancod0d37ca2015-04-02 04:54:56 -070046
47 /**
bsalomonafe30052015-01-16 07:32:33 -080048 * New device that will create an offscreen renderTarget based on the ImageInfo and
49 * sampleCount. The Budgeted param controls whether the device's backing store counts against
halcanary96fcdcc2015-08-27 07:41:13 -070050 * the resource cache budget. On failure, returns nullptr.
Robert Phillips0ae6faa2017-03-21 16:22:00 -040051 * This entry point creates a kExact backing store. It is used when creating SkGpuDevices
52 * for SkSurfaces.
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000053 */
robertphillips24e91282016-04-29 06:46:36 -070054 static sk_sp<SkGpuDevice> Make(GrContext*, SkBudgeted, const SkImageInfo&,
Robert Phillips0ae6faa2017-03-21 16:22:00 -040055 int sampleCount, GrSurfaceOrigin,
robertphillips7e922762016-07-26 11:38:17 -070056 const SkSurfaceProps*, InitContents);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000057
joshualitt06dd0a82016-01-08 06:32:31 -080058 ~SkGpuDevice() override {}
reed@google.com7b201d22011-01-11 18:59:23 +000059
Hal Canary144caf52016-11-07 17:57:18 -050060 GrContext* context() const override { return fContext.get(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000061
reed8eddfb52014-12-04 07:50:14 -080062 // set all pixels to 0
63 void clearAll();
64
Brian Osman11052242016-10-27 14:47:55 -040065 void replaceRenderTargetContext(bool shouldRetainContent);
kkinnunenabcfab42015-02-22 22:53:44 -080066
Brian Osman11052242016-10-27 14:47:55 -040067 GrRenderTargetContext* accessRenderTargetContext() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000068
Mike Reeda1361362017-03-07 09:37:29 -050069 void drawPaint(const SkPaint& paint) override;
70 void drawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint[],
bsalomondcb406c2016-08-22 14:48:36 -070071 const SkPaint& paint) override;
Mike Reeda1361362017-03-07 09:37:29 -050072 void drawRect(const SkRect& r, const SkPaint& paint) override;
73 void drawRRect(const SkRRect& r, const SkPaint& paint) override;
74 void drawDRRect(const SkRRect& outer, const SkRRect& inner,
bsalomondcb406c2016-08-22 14:48:36 -070075 const SkPaint& paint) override;
Mike Reeda1361362017-03-07 09:37:29 -050076 void drawRegion(const SkRegion& r, const SkPaint& paint) override;
77 void drawOval(const SkRect& oval, const SkPaint& paint) override;
78 void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
bsalomondcb406c2016-08-22 14:48:36 -070079 bool useCenter, const SkPaint& paint) override;
Mike Reeda1361362017-03-07 09:37:29 -050080 void drawPath(const SkPath& path, const SkPaint& paint,
bsalomondcb406c2016-08-22 14:48:36 -070081 const SkMatrix* prePathMatrix, bool pathIsMutable) override;
Mike Reeda1361362017-03-07 09:37:29 -050082 void drawBitmap(const SkBitmap& bitmap, const SkMatrix&,
bsalomondcb406c2016-08-22 14:48:36 -070083 const SkPaint&) 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;
Mike Reeda1361362017-03-07 09:37:29 -050088 void drawText(const void* text, size_t len, SkScalar x, SkScalar y,
bsalomondcb406c2016-08-22 14:48:36 -070089 const SkPaint&) override;
Mike Reeda1361362017-03-07 09:37:29 -050090 void drawPosText(const void* text, size_t len, const SkScalar pos[],
bsalomondcb406c2016-08-22 14:48:36 -070091 int scalarsPerPos, const SkPoint& offset, const SkPaint&) override;
Mike Reeda1361362017-03-07 09:37:29 -050092 void drawTextBlob(const SkTextBlob*, SkScalar x, SkScalar y,
bsalomondcb406c2016-08-22 14:48:36 -070093 const SkPaint& paint, SkDrawFilter* drawFilter) override;
Mike Reed2f6b5a42017-03-19 15:04:17 -040094 void drawVertices(const SkVertices*, SkBlendMode, const SkPaint&) override;
Mike Reed4204da22017-05-17 08:53:36 -040095 void drawShadow(const SkPath&, const SkDrawShadowRec&) override;
Mike Reeda1361362017-03-07 09:37:29 -050096 void drawAtlas(const SkImage* atlas, const SkRSXform[], const SkRect[],
Mike Reedfaba3712016-11-03 14:45:31 -040097 const SkColor[], int count, SkBlendMode, const SkPaint&) override;
Mike Reeda1361362017-03-07 09:37:29 -050098 void drawDevice(SkBaseDevice*, int x, int y, const SkPaint&) override;
robertphillips970587b2016-07-14 14:12:55 -070099
Mike Reeda1361362017-03-07 09:37:29 -0500100 void drawImage(const SkImage*, SkScalar x, SkScalar y, const SkPaint&) override;
101 void drawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
reeda5517e22015-07-14 10:54:12 -0700102 const SkPaint&, SkCanvas::SrcRectConstraint) override;
reed@google.com7b201d22011-01-11 18:59:23 +0000103
Mike Reeda1361362017-03-07 09:37:29 -0500104 void drawImageNine(const SkImage* image, const SkIRect& center,
joshualitt33a5fce2015-11-18 13:28:51 -0800105 const SkRect& dst, const SkPaint& paint) override;
Mike Reeda1361362017-03-07 09:37:29 -0500106 void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
joshualitt33a5fce2015-11-18 13:28:51 -0800107 const SkRect& dst, const SkPaint& paint) override;
108
Mike Reeda1361362017-03-07 09:37:29 -0500109 void drawImageLattice(const SkImage*, const SkCanvas::Lattice&,
msarett10e3d9b2016-08-18 15:46:03 -0700110 const SkRect& dst, const SkPaint&) override;
Mike Reeda1361362017-03-07 09:37:29 -0500111 void drawBitmapLattice(const SkBitmap&, const SkCanvas::Lattice&,
msarett10e3d9b2016-08-18 15:46:03 -0700112 const SkRect& dst, const SkPaint&) override;
113
Florin Malita53f77bd2017-04-28 13:48:37 -0400114 void drawSpecial(SkSpecialImage*, int left, int top, const SkPaint& paint,
115 SkImage*, const SkMatrix&) override;
robertphillips6451a0c2016-07-18 08:31:31 -0700116 sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
reede51c3562016-07-19 14:33:20 -0700117 sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
robertphillips6451a0c2016-07-18 08:31:31 -0700118 sk_sp<SkSpecialImage> snapSpecial() override;
robertphillips970587b2016-07-14 14:12:55 -0700119
mtklein36352bf2015-03-25 18:17:31 -0700120 void flush() override;
reed@google.com7b201d22011-01-11 18:59:23 +0000121
reed41e010c2015-06-09 12:16:53 -0700122 bool onAccessPixels(SkPixmap*) override;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000123
reed@google.comac10a2d2010-12-22 21:39:39 +0000124protected:
mtklein36352bf2015-03-25 18:17:31 -0700125 bool onReadPixels(const SkImageInfo&, void*, size_t, int, int) override;
126 bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) override;
robertphillips9c240a12015-05-28 07:45:59 -0700127 bool onShouldDisableLCD(const SkPaint&) const final;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000128
reed@google.comac10a2d2010-12-22 21:39:39 +0000129private:
Brian Osman11052242016-10-27 14:47:55 -0400130 // We want these unreffed in RenderTargetContext, GrContext order.
Hal Canary144caf52016-11-07 17:57:18 -0500131 sk_sp<GrContext> fContext;
132 sk_sp<GrRenderTargetContext> fRenderTargetContext;
joshualitt06dd0a82016-01-08 06:32:31 -0800133
Hal Canary144caf52016-11-07 17:57:18 -0500134 SkISize fSize;
135 bool fOpaque;
bsalomon74f681d2015-06-23 14:38:48 -0700136
137 enum Flags {
138 kNeedClear_Flag = 1 << 0, //!< Surface requires an initial clear
139 kIsOpaque_Flag = 1 << 1, //!< Hint from client that rendering to this device will be
140 // opaque even if the config supports alpha.
141 };
142 static bool CheckAlphaTypeAndGetFlags(const SkImageInfo* info, InitContents init,
143 unsigned* flags);
reed89443ab2014-06-27 11:34:19 -0700144
Robert Phillips9fab7e92016-11-17 12:45:04 -0500145 SkGpuDevice(GrContext*, sk_sp<GrRenderTargetContext>, int width, int height, unsigned flags);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000146
mtklein36352bf2015-03-25 18:17:31 -0700147 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
bsalomon@google.come97f0852011-06-17 13:10:25 +0000148
reede8f30622016-03-23 18:59:25 -0700149 sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
reed@google.com76f10a32014-02-05 15:32:21 +0000150
senorblanco900c3672016-04-27 11:31:23 -0700151 SkImageFilterCache* getImageFilterCache() override;
reed841010f2014-09-10 07:23:32 -0700152
mtklein36352bf2015-03-25 18:17:31 -0700153 bool forceConservativeRasterClip() const override { return true; }
senorblanco55b6d8b2014-07-30 11:26:46 -0700154
Brian Salomon0166cfd2017-03-13 17:58:25 -0400155 GrClipStackClip clip() const { return GrClipStackClip(&this->cs()); }
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000156
157 /**
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000158 * Helper functions called by drawBitmapCommon. By the time these are called the SkDraw's
commit-bot@chromium.org7edad872013-10-25 14:58:12 +0000159 * matrix, clip, and the device's render target has already been set on GrContext.
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000160 */
commit-bot@chromium.org7edad872013-10-25 14:58:12 +0000161
162 // The tileSize and clippedSrcRect will be valid only if true is returned.
reed85d91782015-09-10 14:33:38 -0700163 bool shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
164 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700165 const SkMatrix& srcToDstRectMatrix,
Brian Salomon514baff2016-11-17 15:17:07 -0500166 const GrSamplerParams& 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 Salomon514baff2016-11-17 15:17:07 -0500188 const GrSamplerParams& 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 Salomon514baff2016-11-17 15:17:07 -0500199 const GrSamplerParams& params,
bsalomone553b642016-08-17 09:02:09 -0700200 const SkPaint& paint,
201 SkCanvas::SrcRectConstraint,
202 bool bicubic,
203 bool needsTextureDomain);
204
bsalomonb1b01992015-11-18 10:56:08 -0800205 void drawTextureProducer(GrTextureProducer*,
bsalomonc55271f2015-11-09 11:55:57 -0800206 const SkRect* srcRect,
207 const SkRect* dstRect,
bsalomonb1b01992015-11-18 10:56:08 -0800208 SkCanvas::SrcRectConstraint,
bsalomonc55271f2015-11-09 11:55:57 -0800209 const SkMatrix& viewMatrix,
bsalomonb1b01992015-11-18 10:56:08 -0800210 const GrClip&,
211 const SkPaint&);
bsalomonc55271f2015-11-09 11:55:57 -0800212
bsalomonb1b01992015-11-18 10:56:08 -0800213 void drawTextureProducerImpl(GrTextureProducer*,
bsalomonc55271f2015-11-09 11:55:57 -0800214 const SkRect& clippedSrcRect,
215 const SkRect& clippedDstRect,
bsalomonb1b01992015-11-18 10:56:08 -0800216 SkCanvas::SrcRectConstraint,
bsalomonc55271f2015-11-09 11:55:57 -0800217 const SkMatrix& viewMatrix,
218 const SkMatrix& srcToDstMatrix,
bsalomonb1b01992015-11-18 10:56:08 -0800219 const GrClip&,
220 const SkPaint&);
bsalomonc55271f2015-11-09 11:55:57 -0800221
robertphillipsd7706102016-02-25 09:28:08 -0800222 bool drawFilledDRRect(const SkMatrix& viewMatrix, const SkRRect& outer,
223 const SkRRect& inner, const SkPaint& paint);
224
Mike Reeda1361362017-03-07 09:37:29 -0500225 void drawProducerNine(GrTextureProducer*, const SkIRect& center,
bsalomonf1ecd212015-12-09 17:06:02 -0800226 const SkRect& dst, const SkPaint&);
bsalomon2bbd0c62015-12-09 12:50:56 -0800227
Mike Reeda1361362017-03-07 09:37:29 -0500228 void drawProducerLattice(GrTextureProducer*, const SkCanvas::Lattice& lattice,
msarett10e3d9b2016-08-18 15:46:03 -0700229 const SkRect& dst, const SkPaint&);
230
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000231 bool drawDashLine(const SkPoint pts[2], const SkPaint& paint);
Mike Reeda1361362017-03-07 09:37:29 -0500232 void drawStrokedLine(const SkPoint pts[2], const SkPaint&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000233
Mike Reed887cdf12017-04-03 11:11:09 -0400234 void wireframeVertices(SkVertices::VertexMode, int vertexCount, const SkPoint verts[],
Mike Reed2f6b5a42017-03-19 15:04:17 -0400235 SkBlendMode, const uint16_t indices[], int indexCount, const SkPaint&);
236
Brian Osman11052242016-10-27 14:47:55 -0400237 static sk_sp<GrRenderTargetContext> MakeRenderTargetContext(GrContext*,
238 SkBudgeted,
239 const SkImageInfo&,
240 int sampleCount,
241 GrSurfaceOrigin,
242 const SkSurfaceProps*);
kkinnunenabcfab42015-02-22 22:53:44 -0800243
joshualittdbd35932015-04-02 09:19:04 -0700244 friend class GrAtlasTextContext;
robertphillips7b05ff12015-06-19 14:14:54 -0700245 friend class SkSurface_Gpu; // for access to surfaceProps
Mike Reedc5e641c2017-02-17 14:38:11 -0500246 typedef SkClipStackDevice INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000247};
248
249#endif