blob: 7182d71693a92ab4030ffe96edfa0ac7c147e706 [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"
13#include "include/core/SkFilterQuality.h"
14#include "include/core/SkImageInfo.h"
15#include "include/core/SkMatrix.h"
16#include "include/core/SkVertices.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/gpu/GrTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/private/SkColorData.h"
19#include "src/core/SkBlendModePriv.h"
Brian Salomon096b0912019-08-14 16:56:13 -040020#include "src/gpu/GrBlend.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrCaps.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040022#include "src/gpu/GrColor.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040023#include "src/gpu/GrSamplerState.h"
reed8f343722015-08-13 13:32:39 -070024
25class GrCaps;
Brian Salomonf3569f02017-10-24 12:52:33 -040026class GrColorSpaceInfo;
Brian Osman3b655982017-03-07 16:58:08 -050027class GrColorSpaceXform;
bsalomonf1b7a1d2015-09-28 06:26:28 -070028class GrContext;
29class GrFragmentProcessor;
30class GrPaint;
Robert Phillips9338c602019-02-19 12:52:29 -050031class GrRecordingContext;
Robert Phillips26c90e02017-03-14 14:39:29 -040032class GrResourceProvider;
Robert Phillipse14d3052017-02-15 13:18:21 -050033class GrTextureProxy;
reed8f343722015-08-13 13:32:39 -070034class GrUniqueKey;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050035class SkBitmap;
bsalomonafa95e22015-10-12 10:39:46 -070036class SkData;
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. */
63SkPMColor4f SkColorToPMColor4f(SkColor, const GrColorSpaceInfo&);
64
Brian Osman8fa7ab42019-03-18 10:22:42 -040065/** Converts an SkColor4f to the destination color space. */
66SkColor4f SkColor4fPrepForDst(SkColor4f, const GrColorSpaceInfo&);
67
68/** Returns true if half-floats are required to store the color in a vertex (and half-floats
69 are supported). */
70static inline bool SkPMColor4fNeedsWideColor(SkPMColor4f color, GrClampType clampType,
71 const GrCaps& caps) {
72 return GrClampType::kNone == clampType &&
73 caps.halfFloatVertexAttributeSupport() &&
74 !color.fitsInBytes();
75}
Brian Osman5c8a6b32018-11-19 11:56:57 -050076
Brian Osman3b655982017-03-07 16:58:08 -050077////////////////////////////////////////////////////////////////////////////////
78// Paint conversion
bsalomonc55271f2015-11-09 11:55:57 -080079
Robert Phillips9338c602019-02-19 12:52:29 -050080/** Converts an SkPaint to a GrPaint for a given GrRecordingContext. The matrix is required in order
bsalomonaa48d362015-10-01 08:34:17 -070081 to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
Robert Phillips9338c602019-02-19 12:52:29 -050082bool SkPaintToGrPaint(GrRecordingContext*,
Brian Salomonf3569f02017-10-24 12:52:33 -040083 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -070084 const SkPaint& skPaint,
85 const SkMatrix& viewM,
86 GrPaint* grPaint);
87
bsalomonaa48d362015-10-01 08:34:17 -070088/** Same as above but ignores the SkShader (if any) on skPaint. */
Robert Phillips9338c602019-02-19 12:52:29 -050089bool SkPaintToGrPaintNoShader(GrRecordingContext*,
Brian Salomonf3569f02017-10-24 12:52:33 -040090 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -070091 const SkPaint& skPaint,
92 GrPaint* grPaint);
93
94/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
bsalomonaa48d362015-10-01 08:34:17 -070095 should expect an unpremul input color and produce a premultiplied output color. There is
96 no primitive color. */
Robert Phillips9338c602019-02-19 12:52:29 -050097bool SkPaintToGrPaintReplaceShader(GrRecordingContext*,
Brian Salomonf3569f02017-10-24 12:52:33 -040098 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -070099 const SkPaint& skPaint,
Brian Salomonaff329b2017-08-11 09:40:37 -0400100 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700101 GrPaint* grPaint);
102
103/** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
Mike Reed185ba212017-04-28 12:31:05 -0400104 GrOp's GrPrimitiveProcesssor. */
Robert Phillips9338c602019-02-19 12:52:29 -0500105bool SkPaintToGrPaintWithXfermode(GrRecordingContext*,
Brian Salomonf3569f02017-10-24 12:52:33 -0400106 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700107 const SkPaint& skPaint,
108 const SkMatrix& viewM,
Mike Reed7d954ad2016-10-28 15:42:34 -0400109 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700110 GrPaint* grPaint);
111
bsalomonaa48d362015-10-01 08:34:17 -0700112/** This is used when there is a primitive color, but the shader should be ignored. Currently,
113 the expectation is that the primitive color will be premultiplied, though it really should be
114 unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
115 applied to the primitive color after interpolation. */
Robert Phillips9338c602019-02-19 12:52:29 -0500116inline bool SkPaintToGrPaintWithPrimitiveColor(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400117 const GrColorSpaceInfo& dstColorSpaceInfo,
Robert Phillips9338c602019-02-19 12:52:29 -0500118 const SkPaint& skPaint,
119 GrPaint* grPaint) {
Brian Salomonf3569f02017-10-24 12:52:33 -0400120 return SkPaintToGrPaintWithXfermode(context, dstColorSpaceInfo, skPaint, SkMatrix::I(),
121 SkBlendMode::kDst, grPaint);
bsalomonaa48d362015-10-01 08:34:17 -0700122}
123
joshualitt33a5fce2015-11-18 13:28:51 -0800124/** This is used when there may or may not be a shader, and the caller wants to plugin a texture
125 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 -0500126bool SkPaintToGrPaintWithTexture(GrRecordingContext*,
Brian Salomonf3569f02017-10-24 12:52:33 -0400127 const GrColorSpaceInfo& dstColorSpaceInfo,
Robert Phillips9338c602019-02-19 12:52:29 -0500128 const SkPaint& skPaint,
joshualitt33a5fce2015-11-18 13:28:51 -0800129 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400130 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800131 bool textureIsAlphaOnly,
132 GrPaint* grPaint);
133
Brian Osman3b655982017-03-07 16:58:08 -0500134////////////////////////////////////////////////////////////////////////////////
135// Misc Sk to Gr type conversions
136
Brian Osman2b23c4b2018-06-01 12:25:08 -0400137GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&);
138GrPixelConfig SkColorType2GrPixelConfig(const SkColorType);
139GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info);
Brian Osman3b655982017-03-07 16:58:08 -0500140
141bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*);
142
Chris Dalton309c6c02019-08-13 10:32:47 -0600143GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(int imageWidth, int imageHeight,
144 SkFilterQuality paintFilterQuality,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400145 const SkMatrix& viewM,
146 const SkMatrix& localM,
Brian Osmandb78cba2018-02-15 10:09:48 -0500147 bool sharpenMipmappedTextures,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400148 bool* doBicubic);
Brian Osman3b655982017-03-07 16:58:08 -0500149
bsalomonf276ac52015-10-09 13:36:42 -0700150//////////////////////////////////////////////////////////////////////////////
151
Mike Reed887cdf12017-04-03 11:11:09 -0400152static inline GrPrimitiveType SkVertexModeToGrPrimitiveType(SkVertices::VertexMode mode) {
Brian Salomon199fb872017-02-06 09:41:10 -0500153 switch (mode) {
Mike Reed887cdf12017-04-03 11:11:09 -0400154 case SkVertices::kTriangles_VertexMode:
Chris Dalton3809bab2017-06-13 10:55:06 -0600155 return GrPrimitiveType::kTriangles;
Mike Reed887cdf12017-04-03 11:11:09 -0400156 case SkVertices::kTriangleStrip_VertexMode:
Chris Dalton3809bab2017-06-13 10:55:06 -0600157 return GrPrimitiveType::kTriangleStrip;
Mike Reed887cdf12017-04-03 11:11:09 -0400158 case SkVertices::kTriangleFan_VertexMode:
Brian Salomoncccafe82018-04-28 16:13:08 -0400159 break;
Brian Salomon199fb872017-02-06 09:41:10 -0500160 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400161 SK_ABORT("Invalid mode");
Brian Salomon199fb872017-02-06 09:41:10 -0500162}
163
164//////////////////////////////////////////////////////////////////////////////
165
Mike Reed6b3542a2017-06-06 10:41:18 -0400166GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkBlendModeCoeff::kZero);
167GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkBlendModeCoeff::kOne);
168GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkBlendModeCoeff::kSC);
169GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkBlendModeCoeff::kISC);
170GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkBlendModeCoeff::kDC);
171GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkBlendModeCoeff::kIDC);
172GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkBlendModeCoeff::kSA);
173GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkBlendModeCoeff::kISA);
174GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkBlendModeCoeff::kDA);
175GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkBlendModeCoeff::kIDA);
176//GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10);
Brian Salomon587e08f2017-01-27 10:59:27 -0500177
Brian Osman3b655982017-03-07 16:58:08 -0500178////////////////////////////////////////////////////////////////////////////////
179// Texture management
Brian Salomon587e08f2017-01-27 10:59:27 -0500180
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400181/** Returns a texture representing the bitmap that is compatible with the GrSamplerState. The
Brian Osman3b655982017-03-07 16:58:08 -0500182 * texture is inserted into the cache (unless the bitmap is marked volatile) and can be
183 * retrieved again via this function.
184 * The 'scaleAdjust' in/out parameter will be updated to hold any rescaling that needs to be
185 * performed on the absolute texture coordinates (e.g., if the texture is resized out to
186 * the next power of two). It can be null if the caller is sure the bitmap won't be resized.
bsalomon045802d2015-10-20 07:58:01 -0700187 */
Robert Phillips9338c602019-02-19 12:52:29 -0500188sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrRecordingContext*,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400189 const SkBitmap&,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400190 const GrSamplerState&,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400191 SkScalar scaleAdjust[2]);
192
bsalomon045802d2015-10-20 07:58:01 -0700193/**
Greg Daniel55afd6d2017-09-29 09:32:44 -0400194 * Creates a new texture with mipmap levels and copies the baseProxy into the base layer.
195 */
Robert Phillips9338c602019-02-19 12:52:29 -0500196sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext*,
Greg Daniele1da1d92017-10-06 15:59:27 -0400197 GrTextureProxy* baseProxy);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400198
Greg Daniel7e1912a2018-02-08 09:15:33 -0500199/*
200 * Create a texture proxy from the provided bitmap by wrapping it in an image and calling
201 * GrMakeCachedImageProxy.
202 */
203sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrProxyProvider*, const SkBitmap& bitmap,
204 SkBackingFit fit = SkBackingFit::kExact);
Robert Phillipse14d3052017-02-15 13:18:21 -0500205
Robert Phillips7a926392018-02-01 15:49:54 -0500206/*
207 * Create a texture proxy from the provided 'srcImage' and add it to the texture cache
208 * using the key also extracted from 'srcImage'.
209 */
Greg Daniel490695b2018-02-05 09:34:02 -0500210sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider*, sk_sp<SkImage> srcImage,
211 SkBackingFit fit = SkBackingFit::kExact);
Brian Osman3b655982017-03-07 16:58:08 -0500212
213/**
214 * Our key includes the offset, width, and height so that bitmaps created by extractSubset()
215 * are unique.
216 *
217 * The imageID is in the shared namespace (see SkNextID::ImageID())
218 * - SkBitmap/SkPixelRef
219 * - SkImage
220 * - SkImageGenerator
221 *
222 * Note: width/height must fit in 16bits for this impl.
223 */
224void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
225
226/** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
227 removed should the bitmap's contents change or be destroyed. */
Robert Phillipsa41c6852019-02-07 10:44:10 -0500228void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextID,
Brian Salomon238069b2018-07-11 15:58:57 -0400229 SkPixelRef* pixelRef);
Brian Osman3b655982017-03-07 16:58:08 -0500230
reed8f343722015-08-13 13:32:39 -0700231#endif