blob: 126dc4c66ce13040154c33ff575c53534917a9cc [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"
Robert Phillipse14d3052017-02-15 13:18:21 -050013#include "GrSamplerParams.h"
Brian Salomon587e08f2017-01-27 10:59:27 -050014#include "GrTypes.h"
Brian Salomon199fb872017-02-06 09:41:10 -050015#include "SkCanvas.h"
Brian Osman3b655982017-03-07 16:58:08 -050016#include "SkColor.h"
17#include "SkColorPriv.h"
18#include "SkFilterQuality.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070019#include "SkImageInfo.h"
bsalomonafa95e22015-10-12 10:39:46 -070020#include "SkMatrix.h"
Brian Salomon587e08f2017-01-27 10:59:27 -050021#include "SkPM4f.h"
Mike Reed887cdf12017-04-03 11:11:09 -040022#include "SkVertices.h"
Mike Reed6b3542a2017-06-06 10:41:18 -040023#include "SkBlendModePriv.h"
reed8f343722015-08-13 13:32:39 -070024
25class GrCaps;
Brian Osman3b655982017-03-07 16:58:08 -050026class GrColorSpaceXform;
bsalomonf1b7a1d2015-09-28 06:26:28 -070027class GrContext;
Brian Osman11052242016-10-27 14:47:55 -040028class GrRenderTargetContext;
bsalomonf1b7a1d2015-09-28 06:26:28 -070029class GrFragmentProcessor;
30class GrPaint;
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
61/** Transform an SkColor (sRGB bytes) to GrColor4f for the specified color space. */
62GrColor4f SkColorToPremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace);
63GrColor4f SkColorToUnpremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace);
64
65/**
66 * As above, but with a caller-supplied color space xform object. Faster for the cases where we
67 * have that cached.
68 */
69GrColor4f SkColorToPremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace,
70 GrColorSpaceXform* gamutXform);
71GrColor4f SkColorToUnpremulGrColor4f(SkColor c, SkColorSpace* dstColorSpace,
72 GrColorSpaceXform* gamutXform);
73
74/** Replicates the SkColor's alpha to all four channels of the GrColor. */
75static inline GrColor SkColorAlphaToGrColor(SkColor c) {
76 U8CPU a = SkColorGetA(c);
77 return GrColorPackRGBA(a, a, a, a);
78}
79
80//////////////////////////////////////////////////////////////////////////////
81
82static inline SkPM4f GrColor4fToSkPM4f(const GrColor4f& c) {
83 SkPM4f pm4f;
84 pm4f.fVec[SkPM4f::R] = c.fRGBA[0];
85 pm4f.fVec[SkPM4f::G] = c.fRGBA[1];
86 pm4f.fVec[SkPM4f::B] = c.fRGBA[2];
87 pm4f.fVec[SkPM4f::A] = c.fRGBA[3];
88 return pm4f;
89}
90
91static inline GrColor4f SkPM4fToGrColor4f(const SkPM4f& c) {
92 return GrColor4f{c.r(), c.g(), c.b(), c.a()};
93}
94
95////////////////////////////////////////////////////////////////////////////////
96// Paint conversion
bsalomonc55271f2015-11-09 11:55:57 -080097
bsalomonf1b7a1d2015-09-28 06:26:28 -070098/** Converts an SkPaint to a GrPaint for a given GrContext. The matrix is required in order
bsalomonaa48d362015-10-01 08:34:17 -070099 to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
bsalomonf1b7a1d2015-09-28 06:26:28 -0700100bool SkPaintToGrPaint(GrContext*,
Brian Osman11052242016-10-27 14:47:55 -0400101 GrRenderTargetContext*,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700102 const SkPaint& skPaint,
103 const SkMatrix& viewM,
104 GrPaint* grPaint);
105
bsalomonaa48d362015-10-01 08:34:17 -0700106/** Same as above but ignores the SkShader (if any) on skPaint. */
bsalomonf1b7a1d2015-09-28 06:26:28 -0700107bool SkPaintToGrPaintNoShader(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400108 GrRenderTargetContext* rtc,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700109 const SkPaint& skPaint,
110 GrPaint* grPaint);
111
112/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
bsalomonaa48d362015-10-01 08:34:17 -0700113 should expect an unpremul input color and produce a premultiplied output color. There is
114 no primitive color. */
bsalomonf1b7a1d2015-09-28 06:26:28 -0700115bool SkPaintToGrPaintReplaceShader(GrContext*,
Brian Osman11052242016-10-27 14:47:55 -0400116 GrRenderTargetContext*,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700117 const SkPaint& skPaint,
Brian Salomonaff329b2017-08-11 09:40:37 -0400118 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700119 GrPaint* grPaint);
120
121/** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
Mike Reed185ba212017-04-28 12:31:05 -0400122 GrOp's GrPrimitiveProcesssor. */
bsalomonf1b7a1d2015-09-28 06:26:28 -0700123bool SkPaintToGrPaintWithXfermode(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400124 GrRenderTargetContext* rtc,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700125 const SkPaint& skPaint,
126 const SkMatrix& viewM,
Mike Reed7d954ad2016-10-28 15:42:34 -0400127 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700128 GrPaint* grPaint);
129
bsalomonaa48d362015-10-01 08:34:17 -0700130/** This is used when there is a primitive color, but the shader should be ignored. Currently,
131 the expectation is that the primitive color will be premultiplied, though it really should be
132 unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
133 applied to the primitive color after interpolation. */
Brian Osman11052242016-10-27 14:47:55 -0400134inline bool SkPaintToGrPaintWithPrimitiveColor(GrContext* context, GrRenderTargetContext* rtc,
brianosman8fe485b2016-07-25 12:31:51 -0700135 const SkPaint& skPaint, GrPaint* grPaint) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400136 return SkPaintToGrPaintWithXfermode(context, rtc, skPaint, SkMatrix::I(), SkBlendMode::kDst,
Mike Reed185ba212017-04-28 12:31:05 -0400137 grPaint);
bsalomonaa48d362015-10-01 08:34:17 -0700138}
139
joshualitt33a5fce2015-11-18 13:28:51 -0800140/** This is used when there may or may not be a shader, and the caller wants to plugin a texture
141 lookup. If there is a shader, then its output will only be used if the texture is alpha8. */
142bool SkPaintToGrPaintWithTexture(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400143 GrRenderTargetContext* rtc,
joshualitt33a5fce2015-11-18 13:28:51 -0800144 const SkPaint& paint,
145 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400146 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800147 bool textureIsAlphaOnly,
148 GrPaint* grPaint);
149
Brian Osman3b655982017-03-07 16:58:08 -0500150////////////////////////////////////////////////////////////////////////////////
151// Misc Sk to Gr type conversions
152
153GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&, const GrCaps&);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400154GrPixelConfig SkImageInfo2GrPixelConfig(const SkColorType, SkColorSpace*, const GrCaps& caps);
Brian Osman3b655982017-03-07 16:58:08 -0500155GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info, const GrCaps& caps);
156
157bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*);
158
159GrSamplerParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
160 const SkMatrix& viewM,
161 const SkMatrix& localM,
162 bool* doBicubic);
163
bsalomonf276ac52015-10-09 13:36:42 -0700164//////////////////////////////////////////////////////////////////////////////
165
Mike Reed887cdf12017-04-03 11:11:09 -0400166static inline GrPrimitiveType SkVertexModeToGrPrimitiveType(SkVertices::VertexMode mode) {
Brian Salomon199fb872017-02-06 09:41:10 -0500167 switch (mode) {
Mike Reed887cdf12017-04-03 11:11:09 -0400168 case SkVertices::kTriangles_VertexMode:
Chris Dalton3809bab2017-06-13 10:55:06 -0600169 return GrPrimitiveType::kTriangles;
Mike Reed887cdf12017-04-03 11:11:09 -0400170 case SkVertices::kTriangleStrip_VertexMode:
Chris Dalton3809bab2017-06-13 10:55:06 -0600171 return GrPrimitiveType::kTriangleStrip;
Mike Reed887cdf12017-04-03 11:11:09 -0400172 case SkVertices::kTriangleFan_VertexMode:
Chris Dalton3809bab2017-06-13 10:55:06 -0600173 return GrPrimitiveType::kTriangleFan;
Brian Salomon199fb872017-02-06 09:41:10 -0500174 }
175 SkFAIL("Invalid mode");
Chris Dalton3809bab2017-06-13 10:55:06 -0600176 return GrPrimitiveType::kPoints;
Brian Salomon199fb872017-02-06 09:41:10 -0500177}
178
179//////////////////////////////////////////////////////////////////////////////
180
Mike Reed6b3542a2017-06-06 10:41:18 -0400181GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkBlendModeCoeff::kZero);
182GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkBlendModeCoeff::kOne);
183GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkBlendModeCoeff::kSC);
184GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkBlendModeCoeff::kISC);
185GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkBlendModeCoeff::kDC);
186GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkBlendModeCoeff::kIDC);
187GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkBlendModeCoeff::kSA);
188GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkBlendModeCoeff::kISA);
189GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkBlendModeCoeff::kDA);
190GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkBlendModeCoeff::kIDA);
191//GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10);
Brian Salomon587e08f2017-01-27 10:59:27 -0500192
Brian Osman3b655982017-03-07 16:58:08 -0500193#define SkXfermodeCoeffToGrBlendCoeff(X) ((GrBlendCoeff)(X))
Brian Salomon587e08f2017-01-27 10:59:27 -0500194
Brian Osman3b655982017-03-07 16:58:08 -0500195////////////////////////////////////////////////////////////////////////////////
196// Texture management
Brian Salomon587e08f2017-01-27 10:59:27 -0500197
Brian Osman3b655982017-03-07 16:58:08 -0500198/** Returns a texture representing the bitmap that is compatible with the GrSamplerParams. The
199 * texture is inserted into the cache (unless the bitmap is marked volatile) and can be
200 * retrieved again via this function.
201 * The 'scaleAdjust' in/out parameter will be updated to hold any rescaling that needs to be
202 * performed on the absolute texture coordinates (e.g., if the texture is resized out to
203 * 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 -0700204 */
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400205sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrContext*,
206 const SkBitmap&,
207 const GrSamplerParams&,
208 SkScalar scaleAdjust[2]);
209
bsalomon045802d2015-10-20 07:58:01 -0700210/**
211 * Creates a new texture for the bitmap. Does not concern itself with cache keys or texture params.
212 * The bitmap must have CPU-accessible pixels. Attempts to take advantage of faster paths for
Robert Phillips92de6312017-05-23 07:43:48 -0400213 * yuv planes.
bsalomon045802d2015-10-20 07:58:01 -0700214 */
Matt Sarettdedac852017-05-12 10:56:49 -0400215sk_sp<GrTextureProxy> GrUploadBitmapToTextureProxy(GrResourceProvider*, const SkBitmap&,
216 SkColorSpace* dstColorSpace);
Robert Phillipsd3749482017-03-14 09:17:43 -0400217
Robert Phillips0c984a02017-03-16 07:51:56 -0400218sk_sp<GrTextureProxy> GrGenerateMipMapsAndUploadToTextureProxy(GrContext*, const SkBitmap&,
219 SkColorSpace* dstColorSpace);
220
bsalomon0d996862016-03-09 18:44:43 -0800221/**
222 * Creates a new texture for the pixmap.
223 */
Robert Phillips26c90e02017-03-14 14:39:29 -0400224sk_sp<GrTextureProxy> GrUploadPixmapToTextureProxy(GrResourceProvider*,
Matt Sarettdedac852017-05-12 10:56:49 -0400225 const SkPixmap&, SkBudgeted, SkColorSpace*);
bsalomon0d996862016-03-09 18:44:43 -0800226
cblume186d2d42016-06-03 11:17:42 -0700227/**
228 * Creates a new texture populated with the mipmap levels.
229 */
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400230sk_sp<GrTextureProxy> GrUploadMipMapToTextureProxy(GrContext*, const SkImageInfo&,
Robert Phillips590533f2017-07-11 14:22:35 -0400231 const GrMipLevel texels[],
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400232 int mipLevelCount,
233 SkDestinationSurfaceColorMode colorMode);
234
Robert Phillipse14d3052017-02-15 13:18:21 -0500235// This is intended to replace:
236// SkAutoLockPixels alp(bitmap, true);
237// if (!bitmap.readyToDraw()) {
238// return nullptr;
239// }
240// sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext.get(), bitmap,
241// GrSamplerParams::ClampNoFilter(),
242// nullptr);
243// if (!texture) {
244// return nullptr;
245// }
Robert Phillips26c90e02017-03-14 14:39:29 -0400246sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrResourceProvider*, const SkBitmap& bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -0500247
Brian Osman3b655982017-03-07 16:58:08 -0500248
249/**
250 * Our key includes the offset, width, and height so that bitmaps created by extractSubset()
251 * are unique.
252 *
253 * The imageID is in the shared namespace (see SkNextID::ImageID())
254 * - SkBitmap/SkPixelRef
255 * - SkImage
256 * - SkImageGenerator
257 *
258 * Note: width/height must fit in 16bits for this impl.
259 */
260void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
261
262/** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
263 removed should the bitmap's contents change or be destroyed. */
264void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef);
265
bsalomonf276ac52015-10-09 13:36:42 -0700266//////////////////////////////////////////////////////////////////////////////
267
Brian Osman3b655982017-03-07 16:58:08 -0500268/** When image filter code needs to construct a render target context to do intermediate rendering,
269 we need a renderable pixel config. The source (SkSpecialImage) may not be in a renderable
270 format, but we want to preserve the color space of that source. This picks an appropriate format
271 to use. */
272GrPixelConfig GrRenderableConfigForColorSpace(const SkColorSpace*);
bsalomonf276ac52015-10-09 13:36:42 -0700273
reed8f343722015-08-13 13:32:39 -0700274#endif