blob: 8dba066c50f1073218eaf793c8b1abd586dbcfbb [file] [log] [blame]
reed8f343722015-08-13 13:32:39 -07001/*
Brian Osman3b655982017-03-07 16:58:08 -05002 * Copyright 2017 Google Inc.
reed8f343722015-08-13 13:32:39 -07003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Brian Osman3b655982017-03-07 16:58:08 -05008#ifndef SkGr_DEFINED
9#define SkGr_DEFINED
reed8f343722015-08-13 13:32:39 -070010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
12#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkImageInfo.h"
Mike Reed52130b02020-12-28 15:33:13 -050014#include "include/core/SkSamplingOptions.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/gpu/GrTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/private/SkColorData.h"
17#include "src/core/SkBlendModePriv.h"
Brian Salomon096b0912019-08-14 16:56:13 -040018#include "src/gpu/GrBlend.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrCaps.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040020#include "src/gpu/GrColor.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040021#include "src/gpu/GrSamplerState.h"
reed8f343722015-08-13 13:32:39 -070022
23class GrCaps;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040024class GrColorInfo;
Brian Osman3b655982017-03-07 16:58:08 -050025class GrColorSpaceXform;
Adlai Holler872a32c2020-07-10 14:33:22 -040026class GrDirectContext;
bsalomonf1b7a1d2015-09-28 06:26:28 -070027class GrFragmentProcessor;
28class GrPaint;
Robert Phillips9338c602019-02-19 12:52:29 -050029class GrRecordingContext;
Robert Phillips26c90e02017-03-14 14:39:29 -040030class GrResourceProvider;
Robert Phillipse14d3052017-02-15 13:18:21 -050031class GrTextureProxy;
reed8f343722015-08-13 13:32:39 -070032class GrUniqueKey;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050033class SkBitmap;
bsalomonafa95e22015-10-12 10:39:46 -070034class SkData;
Brian Osman449b1152020-04-15 16:43:00 -040035class SkMatrix;
36class SkMatrixProvider;
bsalomonf1b7a1d2015-09-28 06:26:28 -070037class SkPaint;
bsalomonafa95e22015-10-12 10:39:46 -070038class SkPixelRef;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050039class SkPixmap;
bsalomonf1b7a1d2015-09-28 06:26:28 -070040struct SkIRect;
reed8f343722015-08-13 13:32:39 -070041
Brian Osman3b655982017-03-07 16:58:08 -050042////////////////////////////////////////////////////////////////////////////////
43// Color type conversions
reed856e9d92015-09-30 12:21:45 -070044
Brian Osman3b655982017-03-07 16:58:08 -050045static inline GrColor SkColorToPremulGrColor(SkColor c) {
46 SkPMColor pm = SkPreMultiplyColor(c);
47 unsigned r = SkGetPackedR32(pm);
48 unsigned g = SkGetPackedG32(pm);
49 unsigned b = SkGetPackedB32(pm);
50 unsigned a = SkGetPackedA32(pm);
51 return GrColorPackRGBA(r, g, b, a);
52}
53
54static inline GrColor SkColorToUnpremulGrColor(SkColor c) {
55 unsigned r = SkColorGetR(c);
56 unsigned g = SkColorGetG(c);
57 unsigned b = SkColorGetB(c);
58 unsigned a = SkColorGetA(c);
59 return GrColorPackRGBA(r, g, b, a);
60}
61
Brian Osmanf28e55d2018-10-03 16:35:54 -040062/** Similar, but using SkPMColor4f. */
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040063SkPMColor4f SkColorToPMColor4f(SkColor, const GrColorInfo&);
Brian Osmanf28e55d2018-10-03 16:35:54 -040064
Brian Osman8fa7ab42019-03-18 10:22:42 -040065/** Converts an SkColor4f to the destination color space. */
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040066SkColor4f SkColor4fPrepForDst(SkColor4f, const GrColorInfo&);
Brian Osman8fa7ab42019-03-18 10:22:42 -040067
Brian Osman3b655982017-03-07 16:58:08 -050068////////////////////////////////////////////////////////////////////////////////
Brian Salomon694ec492020-04-14 13:39:31 -040069// SkTileMode conversion
70
71static constexpr GrSamplerState::WrapMode SkTileModeToWrapMode(SkTileMode tileMode) {
72 switch (tileMode) {
73 case SkTileMode::kClamp: return GrSamplerState::WrapMode::kClamp;
74 case SkTileMode::kDecal: return GrSamplerState::WrapMode::kClampToBorder;
75 case SkTileMode::kMirror: return GrSamplerState::WrapMode::kMirrorRepeat;
76 case SkTileMode::kRepeat: return GrSamplerState::WrapMode::kRepeat;
77 }
78 SkUNREACHABLE;
79}
80
81////////////////////////////////////////////////////////////////////////////////
Brian Osman3b655982017-03-07 16:58:08 -050082// Paint conversion
bsalomonc55271f2015-11-09 11:55:57 -080083
Robert Phillips9338c602019-02-19 12:52:29 -050084/** Converts an SkPaint to a GrPaint for a given GrRecordingContext. The matrix is required in order
bsalomonaa48d362015-10-01 08:34:17 -070085 to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
Robert Phillips9338c602019-02-19 12:52:29 -050086bool SkPaintToGrPaint(GrRecordingContext*,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040087 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -070088 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -040089 const SkMatrixProvider& matrixProvider,
bsalomonf1b7a1d2015-09-28 06:26:28 -070090 GrPaint* grPaint);
91
bsalomonaa48d362015-10-01 08:34:17 -070092/** Same as above but ignores the SkShader (if any) on skPaint. */
Robert Phillips9338c602019-02-19 12:52:29 -050093bool SkPaintToGrPaintNoShader(GrRecordingContext*,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040094 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -070095 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -040096 const SkMatrixProvider& matrixProvider,
bsalomonf1b7a1d2015-09-28 06:26:28 -070097 GrPaint* grPaint);
98
99/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
bsalomonaa48d362015-10-01 08:34:17 -0700100 should expect an unpremul input color and produce a premultiplied output color. There is
101 no primitive color. */
Robert Phillips9338c602019-02-19 12:52:29 -0500102bool SkPaintToGrPaintReplaceShader(GrRecordingContext*,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400103 const GrColorInfo& dstColorInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700104 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -0400105 const SkMatrixProvider& matrixProvider,
Brian Salomonaff329b2017-08-11 09:40:37 -0400106 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700107 GrPaint* grPaint);
108
109/** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
Mike Reed185ba212017-04-28 12:31:05 -0400110 GrOp's GrPrimitiveProcesssor. */
John Stilesf743d4e2020-07-23 11:35:08 -0400111bool SkPaintToGrPaintWithBlend(GrRecordingContext*,
112 const GrColorInfo& dstColorInfo,
113 const SkPaint& skPaint,
114 const SkMatrixProvider& matrixProvider,
115 SkBlendMode primColorMode,
116 GrPaint* grPaint);
bsalomonf1b7a1d2015-09-28 06:26:28 -0700117
John Stilesf2f9b0d2020-08-25 13:33:45 -0400118/** Blends the passed-in shader with a per-primitive color which must be setup as a vertex attribute
119 using the specified SkBlendMode. */
120bool SkPaintToGrPaintWithBlendReplaceShader(GrRecordingContext* context,
121 const GrColorInfo& dstColorInfo,
122 const SkPaint& skPaint,
123 const SkMatrixProvider& matrixProvider,
124 std::unique_ptr<GrFragmentProcessor> shaderFP,
125 SkBlendMode primColorMode,
126 GrPaint* grPaint);
127
bsalomonaa48d362015-10-01 08:34:17 -0700128/** This is used when there is a primitive color, but the shader should be ignored. Currently,
129 the expectation is that the primitive color will be premultiplied, though it really should be
130 unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
131 applied to the primitive color after interpolation. */
Robert Phillips9338c602019-02-19 12:52:29 -0500132inline bool SkPaintToGrPaintWithPrimitiveColor(GrRecordingContext* context,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400133 const GrColorInfo& dstColorInfo,
Robert Phillips9338c602019-02-19 12:52:29 -0500134 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -0400135 const SkMatrixProvider& matrixProvider,
Robert Phillips9338c602019-02-19 12:52:29 -0500136 GrPaint* grPaint) {
John Stilesf743d4e2020-07-23 11:35:08 -0400137 return SkPaintToGrPaintWithBlend(context, dstColorInfo, skPaint, matrixProvider,
138 SkBlendMode::kDst, grPaint);
bsalomonaa48d362015-10-01 08:34:17 -0700139}
140
joshualitt33a5fce2015-11-18 13:28:51 -0800141/** This is used when there may or may not be a shader, and the caller wants to plugin a texture
142 lookup. If there is a shader, then its output will only be used if the texture is alpha8. */
Robert Phillips9338c602019-02-19 12:52:29 -0500143bool SkPaintToGrPaintWithTexture(GrRecordingContext*,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400144 const GrColorInfo& dstColorInfo,
Robert Phillips9338c602019-02-19 12:52:29 -0500145 const SkPaint& skPaint,
Brian Osman449b1152020-04-15 16:43:00 -0400146 const SkMatrixProvider& matrixProvider,
Brian Salomonaff329b2017-08-11 09:40:37 -0400147 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800148 bool textureIsAlphaOnly,
149 GrPaint* grPaint);
150
Brian Osman3b655982017-03-07 16:58:08 -0500151////////////////////////////////////////////////////////////////////////////////
152// Misc Sk to Gr type conversions
153
Brian Salomon4dea72a2019-12-18 10:43:10 -0500154static_assert((int)kZero_GrBlendCoeff == (int)SkBlendModeCoeff::kZero);
155static_assert((int)kOne_GrBlendCoeff == (int)SkBlendModeCoeff::kOne);
156static_assert((int)kSC_GrBlendCoeff == (int)SkBlendModeCoeff::kSC);
157static_assert((int)kISC_GrBlendCoeff == (int)SkBlendModeCoeff::kISC);
158static_assert((int)kDC_GrBlendCoeff == (int)SkBlendModeCoeff::kDC);
159static_assert((int)kIDC_GrBlendCoeff == (int)SkBlendModeCoeff::kIDC);
160static_assert((int)kSA_GrBlendCoeff == (int)SkBlendModeCoeff::kSA);
161static_assert((int)kISA_GrBlendCoeff == (int)SkBlendModeCoeff::kISA);
162static_assert((int)kDA_GrBlendCoeff == (int)SkBlendModeCoeff::kDA);
163static_assert((int)kIDA_GrBlendCoeff == (int)SkBlendModeCoeff::kIDA);
164// static_assert(SkXfermode::kCoeffCount == 10);
Brian Salomon587e08f2017-01-27 10:59:27 -0500165
Brian Osman3b655982017-03-07 16:58:08 -0500166////////////////////////////////////////////////////////////////////////////////
167// Texture management
Brian Salomon587e08f2017-01-27 10:59:27 -0500168
Brian Salomonc8d092a2020-02-24 10:14:21 -0500169/**
Brian Salomonbc074a62020-03-18 10:06:13 -0400170 * Policies for how to create textures for SkImages (and SkBitmaps).
171 */
172enum class GrImageTexGenPolicy : int {
173 // Choose the cheapest way to generate the texture. Use GrResourceCache if appropriate.
174 kDraw,
175 // Always make a new texture that is uncached and unbudgeted.
176 kNew_Uncached_Unbudgeted,
177 // Always make a new texture that is uncached and budgeted.
178 kNew_Uncached_Budgeted
179};
180
181/**
Greg Daniel55afd6d2017-09-29 09:32:44 -0400182 * Creates a new texture with mipmap levels and copies the baseProxy into the base layer.
183 */
Brian Salomonc5243782020-04-02 12:50:34 -0400184sk_sp<GrSurfaceProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext*,
Brian Salomon982127b2021-01-21 10:43:35 -0500185 sk_sp<GrSurfaceProxy> baseProxy,
Brian Salomonc5243782020-04-02 12:50:34 -0400186 GrSurfaceOrigin origin,
187 SkBudgeted = SkBudgeted::kYes);
188/**
189 * Same as GrCopyBaseMipMapToTextureProxy but takes the src as a view and returns a view with same
190 * origin and swizzle as the src view.
191 */
192GrSurfaceProxyView GrCopyBaseMipMapToView(GrRecordingContext*,
193 GrSurfaceProxyView,
194 SkBudgeted = SkBudgeted::kYes);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400195
Greg Daniel7e1912a2018-02-08 09:15:33 -0500196/*
Brian Salomon27c42022021-04-28 12:39:21 -0400197 * Create a texture proxy from the provided bitmap and add it to the texture cache using the key
198 * also extracted from the bitmap. If GrMipmapped is kYes a non-mipmapped result may be returned
199 * if mipmapping isn't supported or for a 1x1 bitmap. If GrMipmapped is kNo it indicates mipmaps
200 * aren't required but a previously created mipmapped texture may still be returned. A color type is
201 * returned as color type conversion may be performed if there isn't a texture format equivalent of
Brian Salomon3650f6d2021-06-08 13:22:41 -0400202 * the bitmap's color type.
Greg Daniel7e1912a2018-02-08 09:15:33 -0500203 */
Brian Salomon27c42022021-04-28 12:39:21 -0400204std::tuple<GrSurfaceProxyView, GrColorType>
205GrMakeCachedBitmapProxyView(GrRecordingContext*,
206 const SkBitmap&,
207 GrMipmapped = GrMipmapped::kNo);
208
209/**
Brian Salomon3650f6d2021-06-08 13:22:41 -0400210 * Like above but always uploads the bitmap and never inserts into the cache. Unlike above, the
211 * texture may be approx or scratch and budgeted or not.
Brian Salomon27c42022021-04-28 12:39:21 -0400212 */
213std::tuple<GrSurfaceProxyView, GrColorType>
214GrMakeUncachedBitmapProxyView(GrRecordingContext*,
215 const SkBitmap&,
216 GrMipmapped = GrMipmapped::kNo,
217 SkBackingFit = SkBackingFit::kExact,
218 SkBudgeted = SkBudgeted::kYes);
Robert Phillipse14d3052017-02-15 13:18:21 -0500219
Brian Osman3b655982017-03-07 16:58:08 -0500220/**
221 * Our key includes the offset, width, and height so that bitmaps created by extractSubset()
222 * are unique.
223 *
224 * The imageID is in the shared namespace (see SkNextID::ImageID())
225 * - SkBitmap/SkPixelRef
226 * - SkImage
227 * - SkImageGenerator
Brian Osman3b655982017-03-07 16:58:08 -0500228 */
229void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
230
Brian Salomon99a813c2020-03-02 12:50:47 -0500231/**
232 * Makes a SkIDChangeListener from a GrUniqueKey. The key will be invalidated in the resource
233 * cache if the ID becomes invalid. This also modifies the key so that it will cause the listener
234 * to be deregistered if the key is destroyed (to prevent unbounded listener growth when resources
235 * are purged before listeners trigger).
236 */
237sk_sp<SkIDChangeListener> GrMakeUniqueKeyInvalidationListener(GrUniqueKey*, uint32_t contextID);
238
Mike Reed52130b02020-12-28 15:33:13 -0500239static inline bool GrValidCubicResampler(SkCubicResampler cubic) {
240 return cubic.B >= 0 && cubic.C >= 0;
241}
242
reed8f343722015-08-13 13:32:39 -0700243#endif