blob: aadf7349341fefeaba0b79633d4dc8fdfd0f8e92 [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 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;
bsalomonf1b7a1d2015-09-28 06:26:28 -070035class SkPaint;
bsalomonafa95e22015-10-12 10:39:46 -070036class SkPixelRef;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050037class SkPixmap;
bsalomonf1b7a1d2015-09-28 06:26:28 -070038struct SkIRect;
reed8f343722015-08-13 13:32:39 -070039
Brian Osman3b655982017-03-07 16:58:08 -050040////////////////////////////////////////////////////////////////////////////////
41// Color type conversions
reed856e9d92015-09-30 12:21:45 -070042
Brian Osman3b655982017-03-07 16:58:08 -050043static inline GrColor SkColorToPremulGrColor(SkColor c) {
44 SkPMColor pm = SkPreMultiplyColor(c);
45 unsigned r = SkGetPackedR32(pm);
46 unsigned g = SkGetPackedG32(pm);
47 unsigned b = SkGetPackedB32(pm);
48 unsigned a = SkGetPackedA32(pm);
49 return GrColorPackRGBA(r, g, b, a);
50}
51
52static inline GrColor SkColorToUnpremulGrColor(SkColor c) {
53 unsigned r = SkColorGetR(c);
54 unsigned g = SkColorGetG(c);
55 unsigned b = SkColorGetB(c);
56 unsigned a = SkColorGetA(c);
57 return GrColorPackRGBA(r, g, b, a);
58}
59
Brian Osmanf28e55d2018-10-03 16:35:54 -040060/** Similar, but using SkPMColor4f. */
61SkPMColor4f SkColorToPMColor4f(SkColor, const GrColorSpaceInfo&);
62
Brian Osman5c8a6b32018-11-19 11:56:57 -050063/** Converts an SkColor4f to the destination color space. Pins the color if the destination is
64 normalized, or the device does not support half-float vertex attributes. */
65SkColor4f SkColor4fPrepForDst(SkColor4f, const GrColorSpaceInfo&, const GrCaps&);
66
Brian Osman3b655982017-03-07 16:58:08 -050067////////////////////////////////////////////////////////////////////////////////
68// Paint conversion
bsalomonc55271f2015-11-09 11:55:57 -080069
bsalomonf1b7a1d2015-09-28 06:26:28 -070070/** Converts an SkPaint to a GrPaint for a given GrContext. The matrix is required in order
bsalomonaa48d362015-10-01 08:34:17 -070071 to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070072bool SkPaintToGrPaint(GrContext*,
Brian Salomonf3569f02017-10-24 12:52:33 -040073 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -070074 const SkPaint& skPaint,
75 const SkMatrix& viewM,
76 GrPaint* grPaint);
77
bsalomonaa48d362015-10-01 08:34:17 -070078/** Same as above but ignores the SkShader (if any) on skPaint. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070079bool SkPaintToGrPaintNoShader(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -040080 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -070081 const SkPaint& skPaint,
82 GrPaint* grPaint);
83
84/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
bsalomonaa48d362015-10-01 08:34:17 -070085 should expect an unpremul input color and produce a premultiplied output color. There is
86 no primitive color. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070087bool SkPaintToGrPaintReplaceShader(GrContext*,
Brian Salomonf3569f02017-10-24 12:52:33 -040088 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -070089 const SkPaint& skPaint,
Brian Salomonaff329b2017-08-11 09:40:37 -040090 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -070091 GrPaint* grPaint);
92
93/** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
Mike Reed185ba212017-04-28 12:31:05 -040094 GrOp's GrPrimitiveProcesssor. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070095bool SkPaintToGrPaintWithXfermode(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -040096 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -070097 const SkPaint& skPaint,
98 const SkMatrix& viewM,
Mike Reed7d954ad2016-10-28 15:42:34 -040099 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700100 GrPaint* grPaint);
101
bsalomonaa48d362015-10-01 08:34:17 -0700102/** This is used when there is a primitive color, but the shader should be ignored. Currently,
103 the expectation is that the primitive color will be premultiplied, though it really should be
104 unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
105 applied to the primitive color after interpolation. */
Brian Salomonf3569f02017-10-24 12:52:33 -0400106inline bool SkPaintToGrPaintWithPrimitiveColor(GrContext* context,
107 const GrColorSpaceInfo& dstColorSpaceInfo,
brianosman8fe485b2016-07-25 12:31:51 -0700108 const SkPaint& skPaint, GrPaint* grPaint) {
Brian Salomonf3569f02017-10-24 12:52:33 -0400109 return SkPaintToGrPaintWithXfermode(context, dstColorSpaceInfo, skPaint, SkMatrix::I(),
110 SkBlendMode::kDst, grPaint);
bsalomonaa48d362015-10-01 08:34:17 -0700111}
112
joshualitt33a5fce2015-11-18 13:28:51 -0800113/** This is used when there may or may not be a shader, and the caller wants to plugin a texture
114 lookup. If there is a shader, then its output will only be used if the texture is alpha8. */
115bool SkPaintToGrPaintWithTexture(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400116 const GrColorSpaceInfo& dstColorSpaceInfo,
joshualitt33a5fce2015-11-18 13:28:51 -0800117 const SkPaint& paint,
118 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400119 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800120 bool textureIsAlphaOnly,
121 GrPaint* grPaint);
122
Brian Osman3b655982017-03-07 16:58:08 -0500123////////////////////////////////////////////////////////////////////////////////
124// Misc Sk to Gr type conversions
125
Brian Osman2b23c4b2018-06-01 12:25:08 -0400126GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&);
127GrPixelConfig SkColorType2GrPixelConfig(const SkColorType);
128GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info);
Brian Osman3b655982017-03-07 16:58:08 -0500129
130bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*);
131
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400132GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
133 const SkMatrix& viewM,
134 const SkMatrix& localM,
Brian Osmandb78cba2018-02-15 10:09:48 -0500135 bool sharpenMipmappedTextures,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400136 bool* doBicubic);
Brian Osman3b655982017-03-07 16:58:08 -0500137
bsalomonf276ac52015-10-09 13:36:42 -0700138//////////////////////////////////////////////////////////////////////////////
139
Mike Reed887cdf12017-04-03 11:11:09 -0400140static inline GrPrimitiveType SkVertexModeToGrPrimitiveType(SkVertices::VertexMode mode) {
Brian Salomon199fb872017-02-06 09:41:10 -0500141 switch (mode) {
Mike Reed887cdf12017-04-03 11:11:09 -0400142 case SkVertices::kTriangles_VertexMode:
Chris Dalton3809bab2017-06-13 10:55:06 -0600143 return GrPrimitiveType::kTriangles;
Mike Reed887cdf12017-04-03 11:11:09 -0400144 case SkVertices::kTriangleStrip_VertexMode:
Chris Dalton3809bab2017-06-13 10:55:06 -0600145 return GrPrimitiveType::kTriangleStrip;
Mike Reed887cdf12017-04-03 11:11:09 -0400146 case SkVertices::kTriangleFan_VertexMode:
Brian Salomoncccafe82018-04-28 16:13:08 -0400147 break;
Brian Salomon199fb872017-02-06 09:41:10 -0500148 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400149 SK_ABORT("Invalid mode");
Chris Dalton3809bab2017-06-13 10:55:06 -0600150 return GrPrimitiveType::kPoints;
Brian Salomon199fb872017-02-06 09:41:10 -0500151}
152
153//////////////////////////////////////////////////////////////////////////////
154
Mike Reed6b3542a2017-06-06 10:41:18 -0400155GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkBlendModeCoeff::kZero);
156GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkBlendModeCoeff::kOne);
157GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkBlendModeCoeff::kSC);
158GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkBlendModeCoeff::kISC);
159GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkBlendModeCoeff::kDC);
160GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkBlendModeCoeff::kIDC);
161GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkBlendModeCoeff::kSA);
162GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkBlendModeCoeff::kISA);
163GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkBlendModeCoeff::kDA);
164GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkBlendModeCoeff::kIDA);
165//GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10);
Brian Salomon587e08f2017-01-27 10:59:27 -0500166
Brian Osman3b655982017-03-07 16:58:08 -0500167////////////////////////////////////////////////////////////////////////////////
168// Texture management
Brian Salomon587e08f2017-01-27 10:59:27 -0500169
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400170/** Returns a texture representing the bitmap that is compatible with the GrSamplerState. The
Brian Osman3b655982017-03-07 16:58:08 -0500171 * texture is inserted into the cache (unless the bitmap is marked volatile) and can be
172 * retrieved again via this function.
173 * The 'scaleAdjust' in/out parameter will be updated to hold any rescaling that needs to be
174 * performed on the absolute texture coordinates (e.g., if the texture is resized out to
175 * 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 -0700176 */
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400177sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrContext*,
178 const SkBitmap&,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400179 const GrSamplerState&,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400180 SkScalar scaleAdjust[2]);
181
bsalomon045802d2015-10-20 07:58:01 -0700182/**
183 * Creates a new texture for the bitmap. Does not concern itself with cache keys or texture params.
184 * The bitmap must have CPU-accessible pixels. Attempts to take advantage of faster paths for
Robert Phillips92de6312017-05-23 07:43:48 -0400185 * yuv planes.
bsalomon045802d2015-10-20 07:58:01 -0700186 */
Brian Osman2b23c4b2018-06-01 12:25:08 -0400187sk_sp<GrTextureProxy> GrUploadBitmapToTextureProxy(GrProxyProvider*, const SkBitmap&);
Robert Phillipsd3749482017-03-14 09:17:43 -0400188
bsalomon0d996862016-03-09 18:44:43 -0800189/**
Greg Daniel55afd6d2017-09-29 09:32:44 -0400190 * Creates a new texture with mipmap levels and copies the baseProxy into the base layer.
191 */
192sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrContext*,
Greg Daniele1da1d92017-10-06 15:59:27 -0400193 GrTextureProxy* baseProxy);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400194
Greg Daniel7e1912a2018-02-08 09:15:33 -0500195/*
196 * Create a texture proxy from the provided bitmap by wrapping it in an image and calling
197 * GrMakeCachedImageProxy.
198 */
199sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrProxyProvider*, const SkBitmap& bitmap,
200 SkBackingFit fit = SkBackingFit::kExact);
Robert Phillipse14d3052017-02-15 13:18:21 -0500201
Robert Phillips7a926392018-02-01 15:49:54 -0500202/*
203 * Create a texture proxy from the provided 'srcImage' and add it to the texture cache
204 * using the key also extracted from 'srcImage'.
205 */
Greg Daniel490695b2018-02-05 09:34:02 -0500206sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider*, sk_sp<SkImage> srcImage,
207 SkBackingFit fit = SkBackingFit::kExact);
Brian Osman3b655982017-03-07 16:58:08 -0500208
209/**
210 * Our key includes the offset, width, and height so that bitmaps created by extractSubset()
211 * are unique.
212 *
213 * The imageID is in the shared namespace (see SkNextID::ImageID())
214 * - SkBitmap/SkPixelRef
215 * - SkImage
216 * - SkImageGenerator
217 *
218 * Note: width/height must fit in 16bits for this impl.
219 */
220void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
221
222/** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
223 removed should the bitmap's contents change or be destroyed. */
Brian Salomon238069b2018-07-11 15:58:57 -0400224void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID,
225 SkPixelRef* pixelRef);
Brian Osman3b655982017-03-07 16:58:08 -0500226
reed8f343722015-08-13 13:32:39 -0700227#endif