blob: 098505074a987b9c62bedcc5aff8081e66552cd6 [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"
brianosman396fcdb2016-07-22 06:26:11 -070012#include "SkGrPriv.h"
reed@google.comaf951c92011-06-16 19:10:39 +000013#include "SkBitmap.h"
reed89443ab2014-06-27 11:34:19 -070014#include "SkDevice.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000015#include "SkPicture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016#include "SkRegion.h"
bsalomonafe30052015-01-16 07:32:33 -080017#include "SkSurface.h"
csmartdaltonc6f411e2016-08-05 22:32:12 -070018#include "GrClipStackClip.h"
Brian Osman11052242016-10-27 14:47:55 -040019#include "GrRenderTargetContext.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000020#include "GrContext.h"
bsalomonafbf2d62014-09-30 12:18:44 -070021#include "GrSurfacePriv.h"
erikchen9a1ed5d2016-02-10 16:32:34 -080022#include "GrTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
robertphillips64bf7672014-08-21 13:07:35 -070024class GrAccelData;
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 */
reed89443ab2014-06-27 11:34:19 -070034class SK_API SkGpuDevice : public SkBaseDevice {
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
50 * sampleCount. The Budgeted param controls whether the device's backing store counts against
halcanary96fcdcc2015-08-27 07:41:13 -070051 * the resource cache budget. On failure, returns nullptr.
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000052 */
robertphillips24e91282016-04-29 06:46:36 -070053 static sk_sp<SkGpuDevice> Make(GrContext*, SkBudgeted, const SkImageInfo&,
robertphillips7e922762016-07-26 11:38:17 -070054 int sampleCount, GrSurfaceOrigin,
55 const SkSurfaceProps*, InitContents);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +000056
joshualitt06dd0a82016-01-08 06:32:31 -080057 ~SkGpuDevice() override {}
reed@google.com7b201d22011-01-11 18:59:23 +000058
Hal Canary144caf52016-11-07 17:57:18 -050059 GrContext* context() const override { return fContext.get(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000060
reed8eddfb52014-12-04 07:50:14 -080061 // set all pixels to 0
62 void clearAll();
63
Brian Osman11052242016-10-27 14:47:55 -040064 void replaceRenderTargetContext(bool shouldRetainContent);
kkinnunenabcfab42015-02-22 22:53:44 -080065
Brian Osman11052242016-10-27 14:47:55 -040066 GrRenderTargetContext* accessRenderTargetContext() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000067
mtklein36352bf2015-03-25 18:17:31 -070068 void drawPaint(const SkDraw&, const SkPaint& paint) override;
bsalomondcb406c2016-08-22 14:48:36 -070069 void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count, const SkPoint[],
70 const SkPaint& paint) override;
71 void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint) override;
72 void drawRRect(const SkDraw&, const SkRRect& r, const SkPaint& paint) override;
73 void drawDRRect(const SkDraw& draw, const SkRRect& outer, const SkRRect& inner,
74 const SkPaint& paint) override;
msarettcc319b92016-08-25 18:07:18 -070075 void drawRegion(const SkDraw&, const SkRegion& r, const SkPaint& paint) override;
bsalomondcb406c2016-08-22 14:48:36 -070076 void drawOval(const SkDraw&, const SkRect& oval, const SkPaint& paint) override;
77 void drawArc(const SkDraw&, const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
78 bool useCenter, const SkPaint& paint) override;
79 void drawPath(const SkDraw&, const SkPath& path, const SkPaint& paint,
80 const SkMatrix* prePathMatrix, bool pathIsMutable) override;
81 void drawBitmap(const SkDraw&, const SkBitmap& bitmap, const SkMatrix&,
82 const SkPaint&) override;
83 void drawBitmapRect(const SkDraw&, const SkBitmap&, const SkRect* srcOrNull, const SkRect& dst,
84 const SkPaint& paint, SkCanvas::SrcRectConstraint) override;
85 void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y,
86 const SkPaint& paint) override;
87 void drawText(const SkDraw&, const void* text, size_t len, SkScalar x, SkScalar y,
88 const SkPaint&) override;
89 void drawPosText(const SkDraw&, const void* text, size_t len, const SkScalar pos[],
90 int scalarsPerPos, const SkPoint& offset, const SkPaint&) override;
91 void drawTextBlob(const SkDraw&, const SkTextBlob*, SkScalar x, SkScalar y,
92 const SkPaint& paint, SkDrawFilter* drawFilter) override;
93 void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount, const SkPoint verts[],
Mike Reedfaba3712016-11-03 14:45:31 -040094 const SkPoint texs[], const SkColor colors[], SkBlendMode,
bsalomondcb406c2016-08-22 14:48:36 -070095 const uint16_t indices[], int indexCount, const SkPaint&) override;
reedca109532015-06-25 16:25:25 -070096 void drawAtlas(const SkDraw&, const SkImage* atlas, const SkRSXform[], const SkRect[],
Mike Reedfaba3712016-11-03 14:45:31 -040097 const SkColor[], int count, SkBlendMode, const SkPaint&) override;
robertphillips970587b2016-07-14 14:12:55 -070098 void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, const SkPaint&) override;
99
reeda85d4d02015-05-06 12:56:48 -0700100 void drawImage(const SkDraw&, const SkImage*, SkScalar x, SkScalar y, const SkPaint&) override;
101 void drawImageRect(const SkDraw&, 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
joshualitt33a5fce2015-11-18 13:28:51 -0800104 void drawImageNine(const SkDraw& draw, const SkImage* image, const SkIRect& center,
105 const SkRect& dst, const SkPaint& paint) override;
106 void drawBitmapNine(const SkDraw& draw, const SkBitmap& bitmap, const SkIRect& center,
107 const SkRect& dst, const SkPaint& paint) override;
108
msarett10e3d9b2016-08-18 15:46:03 -0700109 void drawImageLattice(const SkDraw&, const SkImage*, const SkCanvas::Lattice&,
110 const SkRect& dst, const SkPaint&) override;
111 void drawBitmapLattice(const SkDraw&, const SkBitmap&, const SkCanvas::Lattice&,
112 const SkRect& dst, const SkPaint&) override;
113
robertphillips6451a0c2016-07-18 08:31:31 -0700114 void drawSpecial(const SkDraw&, SkSpecialImage*,
115 int left, int top, const SkPaint& paint) override;
116 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
jvanverth0671b962015-12-08 18:53:44 -0800124 // for debugging purposes only
125 void drawTexture(GrTexture*, const SkRect& dst, const SkPaint&);
126
reed@google.comac10a2d2010-12-22 21:39:39 +0000127protected:
mtklein36352bf2015-03-25 18:17:31 -0700128 bool onReadPixels(const SkImageInfo&, void*, size_t, int, int) override;
129 bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) override;
robertphillips9c240a12015-05-28 07:45:59 -0700130 bool onShouldDisableLCD(const SkPaint&) const final;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000131
reed@google.comac10a2d2010-12-22 21:39:39 +0000132private:
Brian Osman11052242016-10-27 14:47:55 -0400133 // We want these unreffed in RenderTargetContext, GrContext order.
Hal Canary144caf52016-11-07 17:57:18 -0500134 sk_sp<GrContext> fContext;
135 sk_sp<GrRenderTargetContext> fRenderTargetContext;
joshualitt06dd0a82016-01-08 06:32:31 -0800136
Hal Canary144caf52016-11-07 17:57:18 -0500137 SkIPoint fClipOrigin;
138 GrClipStackClip fClip;
139 SkISize fSize;
140 bool fOpaque;
bsalomon74f681d2015-06-23 14:38:48 -0700141
142 enum Flags {
143 kNeedClear_Flag = 1 << 0, //!< Surface requires an initial clear
144 kIsOpaque_Flag = 1 << 1, //!< Hint from client that rendering to this device will be
145 // opaque even if the config supports alpha.
146 };
147 static bool CheckAlphaTypeAndGetFlags(const SkImageInfo* info, InitContents init,
148 unsigned* flags);
reed89443ab2014-06-27 11:34:19 -0700149
Robert Phillips9fab7e92016-11-17 12:45:04 -0500150 SkGpuDevice(GrContext*, sk_sp<GrRenderTargetContext>, int width, int height, unsigned flags);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000151
mtklein36352bf2015-03-25 18:17:31 -0700152 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
bsalomon@google.come97f0852011-06-17 13:10:25 +0000153
reede8f30622016-03-23 18:59:25 -0700154 sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
reed@google.com76f10a32014-02-05 15:32:21 +0000155
senorblanco900c3672016-04-27 11:31:23 -0700156 SkImageFilterCache* getImageFilterCache() override;
reed841010f2014-09-10 07:23:32 -0700157
mtklein36352bf2015-03-25 18:17:31 -0700158 bool forceConservativeRasterClip() const override { return true; }
senorblanco55b6d8b2014-07-30 11:26:46 -0700159
joshualitt5531d512014-12-17 15:50:11 -0800160 // sets the render target and clip on context
161 void prepareDraw(const SkDraw&);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000162
163 /**
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000164 * Helper functions called by drawBitmapCommon. By the time these are called the SkDraw's
commit-bot@chromium.org7edad872013-10-25 14:58:12 +0000165 * matrix, clip, and the device's render target has already been set on GrContext.
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000166 */
commit-bot@chromium.org7edad872013-10-25 14:58:12 +0000167
168 // The tileSize and clippedSrcRect will be valid only if true is returned.
reed85d91782015-09-10 14:33:38 -0700169 bool shouldTileImageID(uint32_t imageID, const SkIRect& imageRect,
170 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700171 const SkMatrix& srcToDstRectMatrix,
Brian Salomon514baff2016-11-17 15:17:07 -0500172 const GrSamplerParams& params,
reed85d91782015-09-10 14:33:38 -0700173 const SkRect* srcRectPtr,
174 int maxTileSize,
175 int* tileSize,
176 SkIRect* clippedSubset) const;
reed85d91782015-09-10 14:33:38 -0700177 // Just returns the predicate, not the out-tileSize or out-clippedSubset, as they are not
178 // needed at the moment.
179 bool shouldTileImage(const SkImage* image, const SkRect* srcRectPtr,
180 SkCanvas::SrcRectConstraint constraint, SkFilterQuality quality,
bsalomone553b642016-08-17 09:02:09 -0700181 const SkMatrix& viewMatrix, const SkMatrix& srcToDstRect) const;
bsalomonc55271f2015-11-09 11:55:57 -0800182
robertphillips970587b2016-07-14 14:12:55 -0700183 sk_sp<SkSpecialImage> filterTexture(const SkDraw&,
184 SkSpecialImage*,
185 int left, int top,
186 SkIPoint* offset,
187 const SkImageFilter* filter);
188
bsalomone553b642016-08-17 09:02:09 -0700189 // Splits bitmap into tiles of tileSize and draws them using separate textures for each tile.
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000190 void drawTiledBitmap(const SkBitmap& bitmap,
joshualitt5531d512014-12-17 15:50:11 -0800191 const SkMatrix& viewMatrix,
bsalomone553b642016-08-17 09:02:09 -0700192 const SkMatrix& srcToDstMatrix,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000193 const SkRect& srcRect,
commit-bot@chromium.org7edad872013-10-25 14:58:12 +0000194 const SkIRect& clippedSrcRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500195 const GrSamplerParams& params,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000196 const SkPaint& paint,
reeda5517e22015-07-14 10:54:12 -0700197 SkCanvas::SrcRectConstraint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000198 int tileSize,
199 bool bicubic);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000200
bsalomone553b642016-08-17 09:02:09 -0700201 // Used by drawTiledBitmap to draw each tile.
202 void drawBitmapTile(const SkBitmap&,
203 const SkMatrix& viewMatrix,
204 const SkRect& dstRect,
205 const SkRect& srcRect,
Brian Salomon514baff2016-11-17 15:17:07 -0500206 const GrSamplerParams& params,
bsalomone553b642016-08-17 09:02:09 -0700207 const SkPaint& paint,
208 SkCanvas::SrcRectConstraint,
209 bool bicubic,
210 bool needsTextureDomain);
211
bsalomonb1b01992015-11-18 10:56:08 -0800212 void drawTextureProducer(GrTextureProducer*,
bsalomonc55271f2015-11-09 11:55:57 -0800213 const SkRect* srcRect,
214 const SkRect* dstRect,
bsalomonb1b01992015-11-18 10:56:08 -0800215 SkCanvas::SrcRectConstraint,
bsalomonc55271f2015-11-09 11:55:57 -0800216 const SkMatrix& viewMatrix,
bsalomonb1b01992015-11-18 10:56:08 -0800217 const GrClip&,
218 const SkPaint&);
bsalomonc55271f2015-11-09 11:55:57 -0800219
bsalomonb1b01992015-11-18 10:56:08 -0800220 void drawTextureProducerImpl(GrTextureProducer*,
bsalomonc55271f2015-11-09 11:55:57 -0800221 const SkRect& clippedSrcRect,
222 const SkRect& clippedDstRect,
bsalomonb1b01992015-11-18 10:56:08 -0800223 SkCanvas::SrcRectConstraint,
bsalomonc55271f2015-11-09 11:55:57 -0800224 const SkMatrix& viewMatrix,
225 const SkMatrix& srcToDstMatrix,
bsalomonb1b01992015-11-18 10:56:08 -0800226 const GrClip&,
227 const SkPaint&);
bsalomonc55271f2015-11-09 11:55:57 -0800228
robertphillipsd7706102016-02-25 09:28:08 -0800229 bool drawFilledDRRect(const SkMatrix& viewMatrix, const SkRRect& outer,
230 const SkRRect& inner, const SkPaint& paint);
231
bsalomonf1ecd212015-12-09 17:06:02 -0800232 void drawProducerNine(const SkDraw&, GrTextureProducer*, const SkIRect& center,
233 const SkRect& dst, const SkPaint&);
bsalomon2bbd0c62015-12-09 12:50:56 -0800234
msarett10e3d9b2016-08-18 15:46:03 -0700235 void drawProducerLattice(const SkDraw&, GrTextureProducer*, const SkCanvas::Lattice& lattice,
236 const SkRect& dst, const SkPaint&);
237
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000238 bool drawDashLine(const SkPoint pts[2], const SkPaint& paint);
robertphillips0851d2d2016-06-02 05:21:34 -0700239 void drawStrokedLine(const SkPoint pts[2], const SkDraw&, const SkPaint&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000240
Brian Osman11052242016-10-27 14:47:55 -0400241 static sk_sp<GrRenderTargetContext> MakeRenderTargetContext(GrContext*,
242 SkBudgeted,
243 const SkImageInfo&,
244 int sampleCount,
245 GrSurfaceOrigin,
246 const SkSurfaceProps*);
kkinnunenabcfab42015-02-22 22:53:44 -0800247
joshualittdbd35932015-04-02 09:19:04 -0700248 friend class GrAtlasTextContext;
robertphillips7b05ff12015-06-19 14:14:54 -0700249 friend class SkSurface_Gpu; // for access to surfaceProps
reed89443ab2014-06-27 11:34:19 -0700250 typedef SkBaseDevice INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000251};
252
253#endif