blob: 9e2aad0ccdb6ac85c25fc7d8c53d70c017e545bb [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
Ethan Nicholas052fd512017-01-27 15:34:34 +000011#include "GrBlend.h"
Brian Salomon587e08f2017-01-27 10:59:27 -050012#include "GrTypes.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070013#include "SkImageInfo.h"
bsalomonafa95e22015-10-12 10:39:46 -070014#include "SkMatrix.h"
Brian Salomon587e08f2017-01-27 10:59:27 -050015#include "SkPM4f.h"
Mike Reedd4706732016-11-15 16:44:34 -050016#include "SkXfermodePriv.h"
reed8f343722015-08-13 13:32:39 -070017
18class GrCaps;
bsalomonf1b7a1d2015-09-28 06:26:28 -070019class GrContext;
Brian Osman11052242016-10-27 14:47:55 -040020class GrRenderTargetContext;
bsalomonf1b7a1d2015-09-28 06:26:28 -070021class GrFragmentProcessor;
22class GrPaint;
bsalomonf276ac52015-10-09 13:36:42 -070023class GrTexture;
reed8f343722015-08-13 13:32:39 -070024class GrUniqueKey;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050025class SkBitmap;
bsalomonafa95e22015-10-12 10:39:46 -070026class SkData;
bsalomonf1b7a1d2015-09-28 06:26:28 -070027class SkPaint;
bsalomonafa95e22015-10-12 10:39:46 -070028class SkPixelRef;
Brian Salomon6f1d36c2017-01-13 12:02:17 -050029class SkPixmap;
bsalomonf1b7a1d2015-09-28 06:26:28 -070030struct SkIRect;
reed8f343722015-08-13 13:32:39 -070031
32/**
33 * Our key includes the offset, width, and height so that bitmaps created by extractSubset()
34 * are unique.
35 *
bsalomon045802d2015-10-20 07:58:01 -070036 * The imageID is in the shared namespace (see SkNextID::ImageID())
reed8f343722015-08-13 13:32:39 -070037 * - SkBitmap/SkPixelRef
38 * - SkImage
39 * - SkImageGenerator
40 *
41 * Note: width/height must fit in 16bits for this impl.
42 */
bsalomon045802d2015-10-20 07:58:01 -070043void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
reed856e9d92015-09-30 12:21:45 -070044
bsalomonc55271f2015-11-09 11:55:57 -080045/** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
46 removed should the bitmap's contents change or be destroyed. */
47void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef);
48
bsalomonf1b7a1d2015-09-28 06:26:28 -070049/** Converts an SkPaint to a GrPaint for a given GrContext. The matrix is required in order
bsalomonaa48d362015-10-01 08:34:17 -070050 to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070051bool SkPaintToGrPaint(GrContext*,
Brian Osman11052242016-10-27 14:47:55 -040052 GrRenderTargetContext*,
bsalomonf1b7a1d2015-09-28 06:26:28 -070053 const SkPaint& skPaint,
54 const SkMatrix& viewM,
55 GrPaint* grPaint);
56
bsalomonaa48d362015-10-01 08:34:17 -070057/** Same as above but ignores the SkShader (if any) on skPaint. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070058bool SkPaintToGrPaintNoShader(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -040059 GrRenderTargetContext* rtc,
bsalomonf1b7a1d2015-09-28 06:26:28 -070060 const SkPaint& skPaint,
61 GrPaint* grPaint);
62
63/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
bsalomonaa48d362015-10-01 08:34:17 -070064 should expect an unpremul input color and produce a premultiplied output color. There is
65 no primitive color. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070066bool SkPaintToGrPaintReplaceShader(GrContext*,
Brian Osman11052242016-10-27 14:47:55 -040067 GrRenderTargetContext*,
bsalomonf1b7a1d2015-09-28 06:26:28 -070068 const SkPaint& skPaint,
bungeman06ca8ec2016-06-09 08:01:03 -070069 sk_sp<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -070070 GrPaint* grPaint);
71
72/** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
Brian Salomon25a88092016-12-01 09:36:50 -050073 GrOp's GrPrimitiveProcesssor. Currently there is a bool param to indicate whether the
bsalomonf1b7a1d2015-09-28 06:26:28 -070074 primitive color is the dst or src color to the blend in order to work around differences between
bsalomonaa48d362015-10-01 08:34:17 -070075 drawVertices and drawAtlas. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070076bool SkPaintToGrPaintWithXfermode(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -040077 GrRenderTargetContext* rtc,
bsalomonf1b7a1d2015-09-28 06:26:28 -070078 const SkPaint& skPaint,
79 const SkMatrix& viewM,
Mike Reed7d954ad2016-10-28 15:42:34 -040080 SkBlendMode primColorMode,
bsalomonf1b7a1d2015-09-28 06:26:28 -070081 bool primitiveIsSrc,
82 GrPaint* grPaint);
83
bsalomonaa48d362015-10-01 08:34:17 -070084/** This is used when there is a primitive color, but the shader should be ignored. Currently,
85 the expectation is that the primitive color will be premultiplied, though it really should be
86 unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
87 applied to the primitive color after interpolation. */
Brian Osman11052242016-10-27 14:47:55 -040088inline bool SkPaintToGrPaintWithPrimitiveColor(GrContext* context, GrRenderTargetContext* rtc,
brianosman8fe485b2016-07-25 12:31:51 -070089 const SkPaint& skPaint, GrPaint* grPaint) {
Mike Reed7d954ad2016-10-28 15:42:34 -040090 return SkPaintToGrPaintWithXfermode(context, rtc, skPaint, SkMatrix::I(), SkBlendMode::kDst,
brianosman8fe485b2016-07-25 12:31:51 -070091 false, grPaint);
bsalomonaa48d362015-10-01 08:34:17 -070092}
93
joshualitt33a5fce2015-11-18 13:28:51 -080094/** This is used when there may or may not be a shader, and the caller wants to plugin a texture
95 lookup. If there is a shader, then its output will only be used if the texture is alpha8. */
96bool SkPaintToGrPaintWithTexture(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -040097 GrRenderTargetContext* rtc,
joshualitt33a5fce2015-11-18 13:28:51 -080098 const SkPaint& paint,
99 const SkMatrix& viewM,
bungeman06ca8ec2016-06-09 08:01:03 -0700100 sk_sp<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -0800101 bool textureIsAlphaOnly,
102 GrPaint* grPaint);
103
bsalomonf276ac52015-10-09 13:36:42 -0700104//////////////////////////////////////////////////////////////////////////////
105
Brian Salomon587e08f2017-01-27 10:59:27 -0500106static inline SkPM4f GrColor4fToSkPM4f(const GrColor4f& c) {
107 SkPM4f pm4f;
108 pm4f.fVec[SkPM4f::R] = c.fRGBA[0];
109 pm4f.fVec[SkPM4f::G] = c.fRGBA[1];
110 pm4f.fVec[SkPM4f::B] = c.fRGBA[2];
111 pm4f.fVec[SkPM4f::A] = c.fRGBA[3];
112 return pm4f;
113}
114
115static inline GrColor4f SkPM4fToGrColor4f(const SkPM4f& c) {
116 return GrColor4f{c.r(), c.g(), c.b(), c.a()};
117}
118
119//////////////////////////////////////////////////////////////////////////////
120
brianosmana6359362016-03-21 06:55:37 -0700121GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&, const GrCaps&);
bsalomonf276ac52015-10-09 13:36:42 -0700122
brianosman396fcdb2016-07-22 06:26:11 -0700123bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*);
bsalomonf276ac52015-10-09 13:36:42 -0700124
Brian Osman11052242016-10-27 14:47:55 -0400125/** When image filter code needs to construct a render target context to do intermediate rendering,
126 we need a renderable pixel config. The source (SkSpecialImage) may not be in a renderable
127 format, but we want to preserve the color space of that source. This picks an appropriate format
128 to use. */
brianosman27a3aa52016-09-23 07:11:34 -0700129GrPixelConfig GrRenderableConfigForColorSpace(const SkColorSpace*);
brianosman2695eaa2016-09-21 06:45:09 -0700130
bsalomonf276ac52015-10-09 13:36:42 -0700131/**
bsalomon045802d2015-10-20 07:58:01 -0700132 * If the compressed data in the SkData is supported (as a texture format, this returns
133 * the pixel-config that should be used, and sets outStartOfDataToUpload to the ptr into
134 * the data where the actual raw data starts (skipping any header bytes).
135 *
136 * If the compressed data is not supported, this returns kUnknown_GrPixelConfig, and
137 * ignores outStartOfDataToUpload.
138 */
bsalomonf276ac52015-10-09 13:36:42 -0700139GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
140 int expectedW, int expectedH,
141 const void** outStartOfDataToUpload);
142
bsalomonf276ac52015-10-09 13:36:42 -0700143
bsalomon045802d2015-10-20 07:58:01 -0700144/**
145 * Creates a new texture for the bitmap. Does not concern itself with cache keys or texture params.
146 * The bitmap must have CPU-accessible pixels. Attempts to take advantage of faster paths for
147 * compressed textures and yuv planes.
148 */
149GrTexture* GrUploadBitmapToTexture(GrContext*, const SkBitmap&);
bsalomonf276ac52015-10-09 13:36:42 -0700150
Brian Osman7b8400d2016-11-08 17:08:54 -0500151GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext*, const SkBitmap&,
Brian Osman61624f02016-12-09 14:51:59 -0500152 SkColorSpace* dstColorSpace);
cblume55f2d2d2016-02-26 13:20:48 -0800153
bsalomon0d996862016-03-09 18:44:43 -0800154/**
155 * Creates a new texture for the pixmap.
156 */
ericrk8bea8902016-03-18 11:52:20 -0700157GrTexture* GrUploadPixmapToTexture(GrContext*, const SkPixmap&, SkBudgeted budgeted);
bsalomon0d996862016-03-09 18:44:43 -0800158
cblume186d2d42016-06-03 11:17:42 -0700159/**
160 * Creates a new texture populated with the mipmap levels.
161 */
162GrTexture* GrUploadMipMapToTexture(GrContext*, const SkImageInfo&, const GrMipLevel* texels,
163 int mipLevelCount);
164
bsalomonf276ac52015-10-09 13:36:42 -0700165//////////////////////////////////////////////////////////////////////////////
166
167GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkXfermode::kZero_Coeff);
168GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkXfermode::kOne_Coeff);
169GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkXfermode::kSC_Coeff);
170GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkXfermode::kISC_Coeff);
171GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkXfermode::kDC_Coeff);
172GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkXfermode::kIDC_Coeff);
173GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkXfermode::kSA_Coeff);
174GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkXfermode::kISA_Coeff);
175GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkXfermode::kDA_Coeff);
176GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkXfermode::kIDA_Coeff);
177GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10);
178
179#define SkXfermodeCoeffToGrBlendCoeff(X) ((GrBlendCoeff)(X))
180
reed8f343722015-08-13 13:32:39 -0700181#endif