blob: 0b972b45cd678f785ebd35b1d9fe6fbef2511d8d [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"
Brian Salomon587e08f2017-01-27 10:59:27 -050022#include "SkPM4f.h"
Mike Reed887cdf12017-04-03 11:11:09 -040023#include "SkVertices.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 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 Salomon4cbb6e62017-10-25 15:12:19 -040061/** Transform an SkColor (sRGB bytes) to GrColor4f for the specified color space info. */
62GrColor4f SkColorToPremulGrColor4f(SkColor, const GrColorSpaceInfo&);
63GrColor4f SkColorToPremulGrColor4fLegacy(SkColor);
64GrColor4f SkColorToUnpremulGrColor4f(SkColor, const GrColorSpaceInfo&);
Brian Osman3b655982017-03-07 16:58:08 -050065
66/** Replicates the SkColor's alpha to all four channels of the GrColor. */
67static inline GrColor SkColorAlphaToGrColor(SkColor c) {
68 U8CPU a = SkColorGetA(c);
69 return GrColorPackRGBA(a, a, a, a);
70}
71
72//////////////////////////////////////////////////////////////////////////////
73
74static inline SkPM4f GrColor4fToSkPM4f(const GrColor4f& c) {
75 SkPM4f pm4f;
76 pm4f.fVec[SkPM4f::R] = c.fRGBA[0];
77 pm4f.fVec[SkPM4f::G] = c.fRGBA[1];
78 pm4f.fVec[SkPM4f::B] = c.fRGBA[2];
79 pm4f.fVec[SkPM4f::A] = c.fRGBA[3];
80 return pm4f;
81}
82
83static inline GrColor4f SkPM4fToGrColor4f(const SkPM4f& c) {
84 return GrColor4f{c.r(), c.g(), c.b(), c.a()};
85}
86
87////////////////////////////////////////////////////////////////////////////////
88// Paint conversion
bsalomonc55271f2015-11-09 11:55:57 -080089
bsalomonf1b7a1d2015-09-28 06:26:28 -070090/** Converts an SkPaint to a GrPaint for a given GrContext. The matrix is required in order
bsalomonaa48d362015-10-01 08:34:17 -070091 to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070092bool SkPaintToGrPaint(GrContext*,
Brian Salomonf3569f02017-10-24 12:52:33 -040093 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -070094 const SkPaint& skPaint,
95 const SkMatrix& viewM,
96 GrPaint* grPaint);
97
bsalomonaa48d362015-10-01 08:34:17 -070098/** Same as above but ignores the SkShader (if any) on skPaint. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070099bool SkPaintToGrPaintNoShader(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400100 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700101 const SkPaint& skPaint,
102 GrPaint* grPaint);
103
104/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
bsalomonaa48d362015-10-01 08:34:17 -0700105 should expect an unpremul input color and produce a premultiplied output color. There is
106 no primitive color. */
bsalomonf1b7a1d2015-09-28 06:26:28 -0700107bool SkPaintToGrPaintReplaceShader(GrContext*,
Brian Salomonf3569f02017-10-24 12:52:33 -0400108 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700109 const SkPaint& skPaint,
Brian Salomonaff329b2017-08-11 09:40:37 -0400110 std::unique_ptr<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700111 GrPaint* grPaint);
112
113/** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
Mike Reed185ba212017-04-28 12:31:05 -0400114 GrOp's GrPrimitiveProcesssor. */
bsalomonf1b7a1d2015-09-28 06:26:28 -0700115bool SkPaintToGrPaintWithXfermode(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400116 const GrColorSpaceInfo& dstColorSpaceInfo,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700117 const SkPaint& skPaint,
118 const SkMatrix& viewM,
Mike Reed7d954ad2016-10-28 15:42:34 -0400119 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -0700120 GrPaint* grPaint);
121
bsalomonaa48d362015-10-01 08:34:17 -0700122/** This is used when there is a primitive color, but the shader should be ignored. Currently,
123 the expectation is that the primitive color will be premultiplied, though it really should be
124 unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
125 applied to the primitive color after interpolation. */
Brian Salomonf3569f02017-10-24 12:52:33 -0400126inline bool SkPaintToGrPaintWithPrimitiveColor(GrContext* context,
127 const GrColorSpaceInfo& dstColorSpaceInfo,
brianosman8fe485b2016-07-25 12:31:51 -0700128 const SkPaint& skPaint, GrPaint* grPaint) {
Brian Salomonf3569f02017-10-24 12:52:33 -0400129 return SkPaintToGrPaintWithXfermode(context, dstColorSpaceInfo, skPaint, SkMatrix::I(),
130 SkBlendMode::kDst, grPaint);
bsalomonaa48d362015-10-01 08:34:17 -0700131}
132
joshualitt33a5fce2015-11-18 13:28:51 -0800133/** This is used when there may or may not be a shader, and the caller wants to plugin a texture
134 lookup. If there is a shader, then its output will only be used if the texture is alpha8. */
135bool SkPaintToGrPaintWithTexture(GrContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -0400136 const GrColorSpaceInfo& dstColorSpaceInfo,
joshualitt33a5fce2015-11-18 13:28:51 -0800137 const SkPaint& paint,
138 const SkMatrix& viewM,
Brian Salomonaff329b2017-08-11 09:40:37 -0400139 std::unique_ptr<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800140 bool textureIsAlphaOnly,
141 GrPaint* grPaint);
142
Brian Osman3b655982017-03-07 16:58:08 -0500143////////////////////////////////////////////////////////////////////////////////
144// Misc Sk to Gr type conversions
145
146GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&, const GrCaps&);
Greg Daniel81e7bf82017-07-19 14:47:42 -0400147GrPixelConfig SkImageInfo2GrPixelConfig(const SkColorType, SkColorSpace*, const GrCaps& caps);
Brian Osman3b655982017-03-07 16:58:08 -0500148GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info, const GrCaps& caps);
149
150bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*);
151
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400152GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
153 const SkMatrix& viewM,
154 const SkMatrix& localM,
155 bool* doBicubic);
Brian Osman3b655982017-03-07 16:58:08 -0500156
bsalomonf276ac52015-10-09 13:36:42 -0700157//////////////////////////////////////////////////////////////////////////////
158
Mike Reed887cdf12017-04-03 11:11:09 -0400159static inline GrPrimitiveType SkVertexModeToGrPrimitiveType(SkVertices::VertexMode mode) {
Brian Salomon199fb872017-02-06 09:41:10 -0500160 switch (mode) {
Mike Reed887cdf12017-04-03 11:11:09 -0400161 case SkVertices::kTriangles_VertexMode:
Chris Dalton3809bab2017-06-13 10:55:06 -0600162 return GrPrimitiveType::kTriangles;
Mike Reed887cdf12017-04-03 11:11:09 -0400163 case SkVertices::kTriangleStrip_VertexMode:
Chris Dalton3809bab2017-06-13 10:55:06 -0600164 return GrPrimitiveType::kTriangleStrip;
Mike Reed887cdf12017-04-03 11:11:09 -0400165 case SkVertices::kTriangleFan_VertexMode:
Chris Dalton3809bab2017-06-13 10:55:06 -0600166 return GrPrimitiveType::kTriangleFan;
Brian Salomon199fb872017-02-06 09:41:10 -0500167 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400168 SK_ABORT("Invalid mode");
Chris Dalton3809bab2017-06-13 10:55:06 -0600169 return GrPrimitiveType::kPoints;
Brian Salomon199fb872017-02-06 09:41:10 -0500170}
171
172//////////////////////////////////////////////////////////////////////////////
173
Mike Reed6b3542a2017-06-06 10:41:18 -0400174GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkBlendModeCoeff::kZero);
175GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkBlendModeCoeff::kOne);
176GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkBlendModeCoeff::kSC);
177GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkBlendModeCoeff::kISC);
178GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkBlendModeCoeff::kDC);
179GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkBlendModeCoeff::kIDC);
180GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkBlendModeCoeff::kSA);
181GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkBlendModeCoeff::kISA);
182GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkBlendModeCoeff::kDA);
183GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkBlendModeCoeff::kIDA);
184//GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10);
Brian Salomon587e08f2017-01-27 10:59:27 -0500185
Brian Osman3b655982017-03-07 16:58:08 -0500186#define SkXfermodeCoeffToGrBlendCoeff(X) ((GrBlendCoeff)(X))
Brian Salomon587e08f2017-01-27 10:59:27 -0500187
Brian Osman3b655982017-03-07 16:58:08 -0500188////////////////////////////////////////////////////////////////////////////////
189// Texture management
Brian Salomon587e08f2017-01-27 10:59:27 -0500190
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400191/** Returns a texture representing the bitmap that is compatible with the GrSamplerState. The
Brian Osman3b655982017-03-07 16:58:08 -0500192 * texture is inserted into the cache (unless the bitmap is marked volatile) and can be
193 * retrieved again via this function.
194 * The 'scaleAdjust' in/out parameter will be updated to hold any rescaling that needs to be
195 * performed on the absolute texture coordinates (e.g., if the texture is resized out to
196 * 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 -0700197 */
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400198sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrContext*,
199 const SkBitmap&,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400200 const GrSamplerState&,
Robert Phillipsbbd7a3b2017-03-21 08:48:40 -0400201 SkScalar scaleAdjust[2]);
202
bsalomon045802d2015-10-20 07:58:01 -0700203/**
204 * Creates a new texture for the bitmap. Does not concern itself with cache keys or texture params.
205 * The bitmap must have CPU-accessible pixels. Attempts to take advantage of faster paths for
Robert Phillips92de6312017-05-23 07:43:48 -0400206 * yuv planes.
bsalomon045802d2015-10-20 07:58:01 -0700207 */
Matt Sarettdedac852017-05-12 10:56:49 -0400208sk_sp<GrTextureProxy> GrUploadBitmapToTextureProxy(GrResourceProvider*, const SkBitmap&,
209 SkColorSpace* dstColorSpace);
Robert Phillipsd3749482017-03-14 09:17:43 -0400210
Robert Phillips0c984a02017-03-16 07:51:56 -0400211sk_sp<GrTextureProxy> GrGenerateMipMapsAndUploadToTextureProxy(GrContext*, const SkBitmap&,
212 SkColorSpace* dstColorSpace);
213
bsalomon0d996862016-03-09 18:44:43 -0800214/**
215 * Creates a new texture for the pixmap.
216 */
Robert Phillips26c90e02017-03-14 14:39:29 -0400217sk_sp<GrTextureProxy> GrUploadPixmapToTextureProxy(GrResourceProvider*,
Matt Sarettdedac852017-05-12 10:56:49 -0400218 const SkPixmap&, SkBudgeted, SkColorSpace*);
bsalomon0d996862016-03-09 18:44:43 -0800219
cblume186d2d42016-06-03 11:17:42 -0700220/**
Greg Daniel55afd6d2017-09-29 09:32:44 -0400221 * Creates a new texture with mipmap levels and copies the baseProxy into the base layer.
222 */
223sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrContext*,
Greg Daniele1da1d92017-10-06 15:59:27 -0400224 GrTextureProxy* baseProxy);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400225
226/**
cblume186d2d42016-06-03 11:17:42 -0700227 * Creates a new texture populated with the mipmap levels.
228 */
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400229sk_sp<GrTextureProxy> GrUploadMipMapToTextureProxy(GrContext*, const SkImageInfo&,
Robert Phillips590533f2017-07-11 14:22:35 -0400230 const GrMipLevel texels[],
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400231 int mipLevelCount,
232 SkDestinationSurfaceColorMode colorMode);
233
Robert Phillipse14d3052017-02-15 13:18:21 -0500234// This is intended to replace:
235// SkAutoLockPixels alp(bitmap, true);
236// if (!bitmap.readyToDraw()) {
237// return nullptr;
238// }
239// sk_sp<GrTexture> texture = GrMakeCachedBitmapTexture(fContext.get(), bitmap,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400240// GrSamplerState::ClampNearest(),
Robert Phillipse14d3052017-02-15 13:18:21 -0500241// nullptr);
242// if (!texture) {
243// return nullptr;
244// }
Robert Phillips26c90e02017-03-14 14:39:29 -0400245sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrResourceProvider*, const SkBitmap& bitmap);
Robert Phillipse14d3052017-02-15 13:18:21 -0500246
Brian Osman3b655982017-03-07 16:58:08 -0500247
248/**
249 * Our key includes the offset, width, and height so that bitmaps created by extractSubset()
250 * are unique.
251 *
252 * The imageID is in the shared namespace (see SkNextID::ImageID())
253 * - SkBitmap/SkPixelRef
254 * - SkImage
255 * - SkImageGenerator
256 *
257 * Note: width/height must fit in 16bits for this impl.
258 */
259void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
260
261/** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
262 removed should the bitmap's contents change or be destroyed. */
263void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef);
264
bsalomonf276ac52015-10-09 13:36:42 -0700265//////////////////////////////////////////////////////////////////////////////
266
Brian Osman3b655982017-03-07 16:58:08 -0500267/** When image filter code needs to construct a render target context to do intermediate rendering,
268 we need a renderable pixel config. The source (SkSpecialImage) may not be in a renderable
269 format, but we want to preserve the color space of that source. This picks an appropriate format
270 to use. */
271GrPixelConfig GrRenderableConfigForColorSpace(const SkColorSpace*);
bsalomonf276ac52015-10-09 13:36:42 -0700272
reed8f343722015-08-13 13:32:39 -0700273#endif