blob: a4e39761266ca22260c3b67011414b90347d4105 [file] [log] [blame]
reed8f343722015-08-13 13:32:39 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkGrPriv_DEFINED
9#define SkGrPriv_DEFINED
10
Brian Salomon85eb4222017-01-26 17:35:06 -050011#include "GrTypes.h"
Ethan Nicholas052fd512017-01-27 15:34:34 +000012#include "GrBlend.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070013#include "SkImageInfo.h"
bsalomonafa95e22015-10-12 10:39:46 -070014#include "SkMatrix.h"
Mike Reedd4706732016-11-15 16:44:34 -050015#include "SkXfermodePriv.h"
reed8f343722015-08-13 13:32:39 -070016
17class GrCaps;
bsalomonf1b7a1d2015-09-28 06:26:28 -070018class GrContext;
Brian Osman11052242016-10-27 14:47:55 -040019class GrRenderTargetContext;
bsalomonf1b7a1d2015-09-28 06:26:28 -070020class GrFragmentProcessor;
21class GrPaint;
bsalomonf276ac52015-10-09 13:36:42 -070022class GrTexture;
reed8f343722015-08-13 13:32:39 -070023class GrUniqueKey;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050024class SkBitmap;
bsalomonafa95e22015-10-12 10:39:46 -070025class SkData;
bsalomonf1b7a1d2015-09-28 06:26:28 -070026class SkPaint;
bsalomonafa95e22015-10-12 10:39:46 -070027class SkPixelRef;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050028class SkPixmap;
bsalomonf1b7a1d2015-09-28 06:26:28 -070029struct SkIRect;
reed8f343722015-08-13 13:32:39 -070030
31/**
32 * Our key includes the offset, width, and height so that bitmaps created by extractSubset()
33 * are unique.
34 *
bsalomon045802d2015-10-20 07:58:01 -070035 * The imageID is in the shared namespace (see SkNextID::ImageID())
reed8f343722015-08-13 13:32:39 -070036 * - SkBitmap/SkPixelRef
37 * - SkImage
38 * - SkImageGenerator
39 *
40 * Note: width/height must fit in 16bits for this impl.
41 */
bsalomon045802d2015-10-20 07:58:01 -070042void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
reed856e9d92015-09-30 12:21:45 -070043
bsalomonc55271f2015-11-09 11:55:57 -080044/** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
45 removed should the bitmap's contents change or be destroyed. */
46void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef);
47
bsalomonf1b7a1d2015-09-28 06:26:28 -070048/** Converts an SkPaint to a GrPaint for a given GrContext. The matrix is required in order
bsalomonaa48d362015-10-01 08:34:17 -070049 to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070050bool SkPaintToGrPaint(GrContext*,
Brian Osman11052242016-10-27 14:47:55 -040051 GrRenderTargetContext*,
bsalomonf1b7a1d2015-09-28 06:26:28 -070052 const SkPaint& skPaint,
53 const SkMatrix& viewM,
54 GrPaint* grPaint);
55
bsalomonaa48d362015-10-01 08:34:17 -070056/** Same as above but ignores the SkShader (if any) on skPaint. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070057bool SkPaintToGrPaintNoShader(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -040058 GrRenderTargetContext* rtc,
bsalomonf1b7a1d2015-09-28 06:26:28 -070059 const SkPaint& skPaint,
60 GrPaint* grPaint);
61
62/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
bsalomonaa48d362015-10-01 08:34:17 -070063 should expect an unpremul input color and produce a premultiplied output color. There is
64 no primitive color. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070065bool SkPaintToGrPaintReplaceShader(GrContext*,
Brian Osman11052242016-10-27 14:47:55 -040066 GrRenderTargetContext*,
bsalomonf1b7a1d2015-09-28 06:26:28 -070067 const SkPaint& skPaint,
bungeman06ca8ec2016-06-09 08:01:03 -070068 sk_sp<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -070069 GrPaint* grPaint);
70
71/** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
Brian Salomon25a88092016-12-01 09:36:50 -050072 GrOp's GrPrimitiveProcesssor. Currently there is a bool param to indicate whether the
bsalomonf1b7a1d2015-09-28 06:26:28 -070073 primitive color is the dst or src color to the blend in order to work around differences between
bsalomonaa48d362015-10-01 08:34:17 -070074 drawVertices and drawAtlas. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070075bool SkPaintToGrPaintWithXfermode(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -040076 GrRenderTargetContext* rtc,
bsalomonf1b7a1d2015-09-28 06:26:28 -070077 const SkPaint& skPaint,
78 const SkMatrix& viewM,
Mike Reed7d954ad2016-10-28 15:42:34 -040079 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -070080 bool primitiveIsSrc,
81 GrPaint* grPaint);
82
bsalomonaa48d362015-10-01 08:34:17 -070083/** This is used when there is a primitive color, but the shader should be ignored. Currently,
84 the expectation is that the primitive color will be premultiplied, though it really should be
85 unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
86 applied to the primitive color after interpolation. */
Brian Osman11052242016-10-27 14:47:55 -040087inline bool SkPaintToGrPaintWithPrimitiveColor(GrContext* context, GrRenderTargetContext* rtc,
brianosman8fe485b2016-07-25 12:31:51 -070088 const SkPaint& skPaint, GrPaint* grPaint) {
Mike Reed7d954ad2016-10-28 15:42:34 -040089 return SkPaintToGrPaintWithXfermode(context, rtc, skPaint, SkMatrix::I(), SkBlendMode::kDst,
brianosman8fe485b2016-07-25 12:31:51 -070090 false, grPaint);
bsalomonaa48d362015-10-01 08:34:17 -070091}
92
joshualitt33a5fce2015-11-18 13:28:51 -080093/** This is used when there may or may not be a shader, and the caller wants to plugin a texture
94 lookup. If there is a shader, then its output will only be used if the texture is alpha8. */
95bool SkPaintToGrPaintWithTexture(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -040096 GrRenderTargetContext* rtc,
joshualitt33a5fce2015-11-18 13:28:51 -080097 const SkPaint& paint,
98 const SkMatrix& viewM,
bungeman06ca8ec2016-06-09 08:01:03 -070099 sk_sp<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800100 bool textureIsAlphaOnly,
101 GrPaint* grPaint);
102
bsalomonf276ac52015-10-09 13:36:42 -0700103//////////////////////////////////////////////////////////////////////////////
104
brianosmana6359362016-03-21 06:55:37 -0700105GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&, const GrCaps&);
bsalomonf276ac52015-10-09 13:36:42 -0700106
brianosman396fcdb2016-07-22 06:26:11 -0700107bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*);
bsalomonf276ac52015-10-09 13:36:42 -0700108
Brian Osman11052242016-10-27 14:47:55 -0400109/** When image filter code needs to construct a render target context to do intermediate rendering,
110 we need a renderable pixel config. The source (SkSpecialImage) may not be in a renderable
111 format, but we want to preserve the color space of that source. This picks an appropriate format
112 to use. */
brianosman27a3aa52016-09-23 07:11:34 -0700113GrPixelConfig GrRenderableConfigForColorSpace(const SkColorSpace*);
brianosman2695eaa2016-09-21 06:45:09 -0700114
bsalomonf276ac52015-10-09 13:36:42 -0700115/**
bsalomon045802d2015-10-20 07:58:01 -0700116 * If the compressed data in the SkData is supported (as a texture format, this returns
117 * the pixel-config that should be used, and sets outStartOfDataToUpload to the ptr into
118 * the data where the actual raw data starts (skipping any header bytes).
119 *
120 * If the compressed data is not supported, this returns kUnknown_GrPixelConfig, and
121 * ignores outStartOfDataToUpload.
122 */
bsalomonf276ac52015-10-09 13:36:42 -0700123GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
124 int expectedW, int expectedH,
125 const void** outStartOfDataToUpload);
126
bsalomonf276ac52015-10-09 13:36:42 -0700127
bsalomon045802d2015-10-20 07:58:01 -0700128/**
129 * Creates a new texture for the bitmap. Does not concern itself with cache keys or texture params.
130 * The bitmap must have CPU-accessible pixels. Attempts to take advantage of faster paths for
131 * compressed textures and yuv planes.
132 */
133GrTexture* GrUploadBitmapToTexture(GrContext*, const SkBitmap&);
bsalomonf276ac52015-10-09 13:36:42 -0700134
Brian Osman7b8400d2016-11-08 17:08:54 -0500135GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext*, const SkBitmap&,
Brian Osman61624f02016-12-09 14:51:59 -0500136 SkColorSpace* dstColorSpace);
cblume55f2d2d2016-02-26 13:20:48 -0800137
bsalomon0d996862016-03-09 18:44:43 -0800138/**
139 * Creates a new texture for the pixmap.
140 */
ericrk8bea8902016-03-18 11:52:20 -0700141GrTexture* GrUploadPixmapToTexture(GrContext*, const SkPixmap&, SkBudgeted budgeted);
bsalomon0d996862016-03-09 18:44:43 -0800142
cblume186d2d42016-06-03 11:17:42 -0700143/**
144 * Creates a new texture populated with the mipmap levels.
145 */
146GrTexture* GrUploadMipMapToTexture(GrContext*, const SkImageInfo&, const GrMipLevel* texels,
147 int mipLevelCount);
148
bsalomonf276ac52015-10-09 13:36:42 -0700149//////////////////////////////////////////////////////////////////////////////
150
151GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkXfermode::kZero_Coeff);
152GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkXfermode::kOne_Coeff);
153GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkXfermode::kSC_Coeff);
154GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkXfermode::kISC_Coeff);
155GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkXfermode::kDC_Coeff);
156GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkXfermode::kIDC_Coeff);
157GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkXfermode::kSA_Coeff);
158GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkXfermode::kISA_Coeff);
159GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkXfermode::kDA_Coeff);
160GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkXfermode::kIDA_Coeff);
161GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10);
162
163#define SkXfermodeCoeffToGrBlendCoeff(X) ((GrBlendCoeff)(X))
164
reed8f343722015-08-13 13:32:39 -0700165#endif