blob: dd18eafae148beda1968fc3c13b517efd8a53c96 [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
Ethan Nicholas052fd512017-01-27 15:34:34 +000011#include "GrBlend.h"
Brian Osman3b655982017-03-07 16:58:08 -050012#include "GrColor.h"
Brian Salomon2bbdcc42017-09-07 12:36:34 -040013#include "GrSamplerState.h"
Brian Salomon587e08f2017-01-27 10:59:27 -050014#include "GrTypes.h"
Brian Salomon2bbdcc42017-09-07 12:36:34 -040015#include "SkBlendModePriv.h"
Brian Salomon199fb872017-02-06 09:41:10 -050016#include "SkCanvas.h"
Brian Osman3b655982017-03-07 16:58:08 -050017#include "SkColor.h"
Cary Clarka4083c92017-09-15 11:59:23 -040018#include "SkColorData.h"
Brian Osman3b655982017-03-07 16:58:08 -050019#include "SkFilterQuality.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070020#include "SkImageInfo.h"
bsalomonafa95e22015-10-12 10:39:46 -070021#include "SkMatrix.h"
Mike Reed887cdf12017-04-03 11:11:09 -040022#include "SkVertices.h"
reed8f343722015-08-13 13:32:39 -070023
24class GrCaps;
Brian Salomonf3569f02017-10-24 12:52:33 -040025class GrColorSpaceInfo;
Brian Osman3b655982017-03-07 16:58:08 -050026class GrColorSpaceXform;
bsalomonf1b7a1d2015-09-28 06:26:28 -070027class GrContext;
28class GrFragmentProcessor;
29class GrPaint;
Robert Phillips9338c602019-02-19 12:52:29 -050030class GrRecordingContext;
Robert Phillips26c90e02017-03-14 14:39:29 -040031class GrResourceProvider;
Robert Phillipse14d3052017-02-15 13:18:21 -050032class GrTextureProxy;
reed8f343722015-08-13 13:32:39 -070033class GrUniqueKey;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050034class SkBitmap;
bsalomonafa95e22015-10-12 10:39:46 -070035class SkData;
bsalomonf1b7a1d2015-09-28 06:26:28 -070036class SkPaint;
bsalomonafa95e22015-10-12 10:39:46 -070037class SkPixelRef;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050038class SkPixmap;
bsalomonf1b7a1d2015-09-28 06:26:28 -070039struct SkIRect;
reed8f343722015-08-13 13:32:39 -070040
Brian Osman3b655982017-03-07 16:58:08 -050041////////////////////////////////////////////////////////////////////////////////
42// Color type conversions
reed856e9d92015-09-30 12:21:45 -070043
Brian Osman3b655982017-03-07 16:58:08 -050044static inline GrColor SkColorToPremulGrColor(SkColor c) {
45 SkPMColor pm = SkPreMultiplyColor(c);
46 unsigned r = SkGetPackedR32(pm);
47 unsigned g = SkGetPackedG32(pm);
48 unsigned b = SkGetPackedB32(pm);
49 unsigned a = SkGetPackedA32(pm);
50 return GrColorPackRGBA(r, g, b, a);
51}
52
53static inline GrColor SkColorToUnpremulGrColor(SkColor c) {
54 unsigned r = SkColorGetR(c);
55 unsigned g = SkColorGetG(c);
56 unsigned b = SkColorGetB(c);
57 unsigned a = SkColorGetA(c);
58 return GrColorPackRGBA(r, g, b, a);
59}
60
Brian Osmanf28e55d2018-10-03 16:35:54 -040061/** Similar, but using SkPMColor4f. */
62SkPMColor4f SkColorToPMColor4f(SkColor, const GrColorSpaceInfo&);
63
Brian Osman5c8a6b32018-11-19 11:56:57 -050064/** Converts an SkColor4f to the destination color space. Pins the color if the destination is
65 normalized, or the device does not support half-float vertex attributes. */
66SkColor4f SkColor4fPrepForDst(SkColor4f, const GrColorSpaceInfo&, const GrCaps&);
67
Brian Osman3b655982017-03-07 16:58:08 -050068////////////////////////////////////////////////////////////////////////////////
69// Paint conversion
bsalomonc55271f2015-11-09 11:55:57 -080070
Robert Phillips9338c602019-02-19 12:52:29 -050071/** Converts an SkPaint to a GrPaint for a given GrRecordingContext. The matrix is required in order
bsalomonaa48d362015-10-01 08:34:17 -070072 to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
Robert Phillips9338c602019-02-19 12:52:29 -050073bool SkPaintToGrPaint(GrRecordingContext*,
Brian Salomonf3569f02017-10-24 12:52:33 -040074 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -070075 const SkPaint& skPaint,
76 const SkMatrix& viewM,
77 GrPaint* grPaint);
78
bsalomonaa48d362015-10-01 08:34:17 -070079/** Same as above but ignores the SkShader (if any) on skPaint. */
Robert Phillips9338c602019-02-19 12:52:29 -050080bool SkPaintToGrPaintNoShader(GrRecordingContext*,
Brian Salomonf3569f02017-10-24 12:52:33 -040081 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -070082 const SkPaint& skPaint,
83 GrPaint* grPaint);
84
85/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
bsalomonaa48d362015-10-01 08:34:17 -070086 should expect an unpremul input color and produce a premultiplied output color. There is
87 no primitive color. */
Robert Phillips9338c602019-02-19 12:52:29 -050088bool SkPaintToGrPaintReplaceShader(GrRecordingContext*,
Brian Salomonf3569f02017-10-24 12:52:33 -040089 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -070090 const SkPaint& skPaint,
Brian Salomonaff329b2017-08-11 09:40:37 -040091 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -070092 GrPaint* grPaint);
93
94/** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
Mike Reed185ba212017-04-28 12:31:05 -040095 GrOp's GrPrimitiveProcesssor. */
Robert Phillips9338c602019-02-19 12:52:29 -050096bool SkPaintToGrPaintWithXfermode(GrRecordingContext*,
Brian Salomonf3569f02017-10-24 12:52:33 -040097 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -070098 const SkPaint& skPaint,
99 const SkMatrix& viewM,
Mike Reed7d954ad2016-10-28 15:42:34 -0400100 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700101 GrPaint* grPaint);
102
bsalomonaa48d362015-10-01 08:34:17 -0700103/** This is used when there is a primitive color, but the shader should be ignored. Currently,
104 the expectation is that the primitive color will be premultiplied, though it really should be
105 unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
106 applied to the primitive color after interpolation. */
Robert Phillips9338c602019-02-19 12:52:29 -0500107inline bool SkPaintToGrPaintWithPrimitiveColor(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400108 const GrColorSpaceInfo& dstColorSpaceInfo,
Robert Phillips9338c602019-02-19 12:52:29 -0500109 const SkPaint& skPaint,
110 GrPaint* grPaint) {
Brian Salomonf3569f02017-10-24 12:52:33 -0400111 return SkPaintToGrPaintWithXfermode(context, dstColorSpaceInfo, skPaint, SkMatrix::I(),
112 SkBlendMode::kDst, grPaint);
bsalomonaa48d362015-10-01 08:34:17 -0700113}
114
joshualitt33a5fce2015-11-18 13:28:51 -0800115/** This is used when there may or may not be a shader, and the caller wants to plugin a texture
116 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 -0500117bool SkPaintToGrPaintWithTexture(GrRecordingContext*,
Brian Salomonf3569f02017-10-24 12:52:33 -0400118 const GrColorSpaceInfo& dstColorSpaceInfo,
Robert Phillips9338c602019-02-19 12:52:29 -0500119 const SkPaint& skPaint,
joshualitt33a5fce2015-11-18 13:28:51 -0800120 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400121 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800122 bool textureIsAlphaOnly,
123 GrPaint* grPaint);
124
Brian Osman3b655982017-03-07 16:58:08 -0500125////////////////////////////////////////////////////////////////////////////////
126// Misc Sk to Gr type conversions
127
Brian Osman2b23c4b2018-06-01 12:25:08 -0400128GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&);
129GrPixelConfig SkColorType2GrPixelConfig(const SkColorType);
130GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info);
Brian Osman3b655982017-03-07 16:58:08 -0500131
132bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*);
133
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400134GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
135 const SkMatrix& viewM,
136 const SkMatrix& localM,
Brian Osmandb78cba2018-02-15 10:09:48 -0500137 bool sharpenMipmappedTextures,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400138 bool* doBicubic);
Brian Osman3b655982017-03-07 16:58:08 -0500139
bsalomonf276ac52015-10-09 13:36:42 -0700140//////////////////////////////////////////////////////////////////////////////
141
Mike Reed887cdf12017-04-03 11:11:09 -0400142static inline GrPrimitiveType SkVertexModeToGrPrimitiveType(SkVertices::VertexMode mode) {
Brian Salomon199fb872017-02-06 09:41:10 -0500143 switch (mode) {
Mike Reed887cdf12017-04-03 11:11:09 -0400144 case SkVertices::kTriangles_VertexMode:
Chris Dalton3809bab2017-06-13 10:55:06 -0600145 return GrPrimitiveType::kTriangles;
Mike Reed887cdf12017-04-03 11:11:09 -0400146 case SkVertices::kTriangleStrip_VertexMode:
Chris Dalton3809bab2017-06-13 10:55:06 -0600147 return GrPrimitiveType::kTriangleStrip;
Mike Reed887cdf12017-04-03 11:11:09 -0400148 case SkVertices::kTriangleFan_VertexMode:
Brian Salomoncccafe82018-04-28 16:13:08 -0400149 break;
Brian Salomon199fb872017-02-06 09:41:10 -0500150 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400151 SK_ABORT("Invalid mode");
Chris Dalton3809bab2017-06-13 10:55:06 -0600152 return GrPrimitiveType::kPoints;
Brian Salomon199fb872017-02-06 09:41:10 -0500153}
154
155//////////////////////////////////////////////////////////////////////////////
156
Mike Reed6b3542a2017-06-06 10:41:18 -0400157GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkBlendModeCoeff::kZero);
158GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkBlendModeCoeff::kOne);
159GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkBlendModeCoeff::kSC);
160GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkBlendModeCoeff::kISC);
161GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkBlendModeCoeff::kDC);
162GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkBlendModeCoeff::kIDC);
163GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkBlendModeCoeff::kSA);
164GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkBlendModeCoeff::kISA);
165GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkBlendModeCoeff::kDA);
166GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkBlendModeCoeff::kIDA);
167//GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10);
Brian Salomon587e08f2017-01-27 10:59:27 -0500168
Brian Osman3b655982017-03-07 16:58:08 -0500169////////////////////////////////////////////////////////////////////////////////
170// Texture management
Brian Salomon587e08f2017-01-27 10:59:27 -0500171
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400172/** Returns a texture representing the bitmap that is compatible with the GrSamplerState. The
Brian Osman3b655982017-03-07 16:58:08 -0500173 * texture is inserted into the cache (unless the bitmap is marked volatile) and can be
174 * retrieved again via this function.
175 * The 'scaleAdjust' in/out parameter will be updated to hold any rescaling that needs to be
176 * performed on the absolute texture coordinates (e.g., if the texture is resized out to
177 * 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 -0700178 */
Robert Phillips9338c602019-02-19 12:52:29 -0500179sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrRecordingContext*,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400180 const SkBitmap&,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400181 const GrSamplerState&,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400182 SkScalar scaleAdjust[2]);
183
bsalomon045802d2015-10-20 07:58:01 -0700184/**
185 * Creates a new texture for the bitmap. Does not concern itself with cache keys or texture params.
186 * The bitmap must have CPU-accessible pixels. Attempts to take advantage of faster paths for
Robert Phillips92de6312017-05-23 07:43:48 -0400187 * yuv planes.
bsalomon045802d2015-10-20 07:58:01 -0700188 */
Brian Osman2b23c4b2018-06-01 12:25:08 -0400189sk_sp<GrTextureProxy> GrUploadBitmapToTextureProxy(GrProxyProvider*, const SkBitmap&);
Robert Phillipsd3749482017-03-14 09:17:43 -0400190
bsalomon0d996862016-03-09 18:44:43 -0800191/**
Greg Daniel55afd6d2017-09-29 09:32:44 -0400192 * Creates a new texture with mipmap levels and copies the baseProxy into the base layer.
193 */
Robert Phillips9338c602019-02-19 12:52:29 -0500194sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext*,
Greg Daniele1da1d92017-10-06 15:59:27 -0400195 GrTextureProxy* baseProxy);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400196
Greg Daniel7e1912a2018-02-08 09:15:33 -0500197/*
198 * Create a texture proxy from the provided bitmap by wrapping it in an image and calling
199 * GrMakeCachedImageProxy.
200 */
201sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrProxyProvider*, const SkBitmap& bitmap,
202 SkBackingFit fit = SkBackingFit::kExact);
Robert Phillipse14d3052017-02-15 13:18:21 -0500203
Robert Phillips7a926392018-02-01 15:49:54 -0500204/*
205 * Create a texture proxy from the provided 'srcImage' and add it to the texture cache
206 * using the key also extracted from 'srcImage'.
207 */
Greg Daniel490695b2018-02-05 09:34:02 -0500208sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider*, sk_sp<SkImage> srcImage,
209 SkBackingFit fit = SkBackingFit::kExact);
Brian Osman3b655982017-03-07 16:58:08 -0500210
211/**
212 * Our key includes the offset, width, and height so that bitmaps created by extractSubset()
213 * are unique.
214 *
215 * The imageID is in the shared namespace (see SkNextID::ImageID())
216 * - SkBitmap/SkPixelRef
217 * - SkImage
218 * - SkImageGenerator
219 *
220 * Note: width/height must fit in 16bits for this impl.
221 */
222void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
223
224/** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
225 removed should the bitmap's contents change or be destroyed. */
Robert Phillipsa41c6852019-02-07 10:44:10 -0500226void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextID,
Brian Salomon238069b2018-07-11 15:58:57 -0400227 SkPixelRef* pixelRef);
Brian Osman3b655982017-03-07 16:58:08 -0500228
reed8f343722015-08-13 13:32:39 -0700229#endif