blob: c1069d1a207d19ea934e8622442b54e63c8edf27 [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
11#include "GrTypes.h"
bsalomonf276ac52015-10-09 13:36:42 -070012#include "GrBlend.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070013#include "SkImageInfo.h"
bsalomonafa95e22015-10-12 10:39:46 -070014#include "SkMatrix.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070015#include "SkXfermode.h"
reed8f343722015-08-13 13:32:39 -070016
17class GrCaps;
bsalomonf1b7a1d2015-09-28 06:26:28 -070018class GrContext;
brianosman8fe485b2016-07-25 12:31:51 -070019class GrDrawContext;
bsalomonf1b7a1d2015-09-28 06:26:28 -070020class GrFragmentProcessor;
21class GrPaint;
bsalomonf276ac52015-10-09 13:36:42 -070022class GrTexture;
bsalomonafa95e22015-10-12 10:39:46 -070023class GrTextureParams;
reed8f343722015-08-13 13:32:39 -070024class GrUniqueKey;
bsalomonafa95e22015-10-12 10:39:46 -070025class SkData;
bsalomonf1b7a1d2015-09-28 06:26:28 -070026class SkPaint;
bsalomonafa95e22015-10-12 10:39:46 -070027class SkPixelRef;
bsalomonf1b7a1d2015-09-28 06:26:28 -070028struct SkIRect;
reed8f343722015-08-13 13:32:39 -070029
30/**
31 * Our key includes the offset, width, and height so that bitmaps created by extractSubset()
32 * are unique.
33 *
bsalomon045802d2015-10-20 07:58:01 -070034 * The imageID is in the shared namespace (see SkNextID::ImageID())
reed8f343722015-08-13 13:32:39 -070035 * - SkBitmap/SkPixelRef
36 * - SkImage
37 * - SkImageGenerator
38 *
39 * Note: width/height must fit in 16bits for this impl.
40 */
bsalomon045802d2015-10-20 07:58:01 -070041void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
reed856e9d92015-09-30 12:21:45 -070042
bsalomonc55271f2015-11-09 11:55:57 -080043/** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
44 removed should the bitmap's contents change or be destroyed. */
45void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef);
46
bsalomonf1b7a1d2015-09-28 06:26:28 -070047/** Converts an SkPaint to a GrPaint for a given GrContext. The matrix is required in order
bsalomonaa48d362015-10-01 08:34:17 -070048 to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070049bool SkPaintToGrPaint(GrContext*,
brianosman8fe485b2016-07-25 12:31:51 -070050 GrDrawContext*,
bsalomonf1b7a1d2015-09-28 06:26:28 -070051 const SkPaint& skPaint,
52 const SkMatrix& viewM,
53 GrPaint* grPaint);
54
bsalomonaa48d362015-10-01 08:34:17 -070055/** Same as above but ignores the SkShader (if any) on skPaint. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070056bool SkPaintToGrPaintNoShader(GrContext* context,
brianosman8fe485b2016-07-25 12:31:51 -070057 GrDrawContext* dc,
bsalomonf1b7a1d2015-09-28 06:26:28 -070058 const SkPaint& skPaint,
59 GrPaint* grPaint);
60
61/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
bsalomonaa48d362015-10-01 08:34:17 -070062 should expect an unpremul input color and produce a premultiplied output color. There is
63 no primitive color. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070064bool SkPaintToGrPaintReplaceShader(GrContext*,
brianosman8fe485b2016-07-25 12:31:51 -070065 GrDrawContext*,
bsalomonf1b7a1d2015-09-28 06:26:28 -070066 const SkPaint& skPaint,
bungeman06ca8ec2016-06-09 08:01:03 -070067 sk_sp<GrFragmentProcessor> shaderFP,
bsalomonf1b7a1d2015-09-28 06:26:28 -070068 GrPaint* grPaint);
69
70/** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
71 GrBatch's GrPrimitiveProcesssor. Currently there is a bool param to indicate whether the
72 primitive color is the dst or src color to the blend in order to work around differences between
bsalomonaa48d362015-10-01 08:34:17 -070073 drawVertices and drawAtlas. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070074bool SkPaintToGrPaintWithXfermode(GrContext* context,
brianosman8fe485b2016-07-25 12:31:51 -070075 GrDrawContext* dc,
bsalomonf1b7a1d2015-09-28 06:26:28 -070076 const SkPaint& skPaint,
77 const SkMatrix& viewM,
78 SkXfermode::Mode primColorMode,
79 bool primitiveIsSrc,
80 GrPaint* grPaint);
81
bsalomonaa48d362015-10-01 08:34:17 -070082/** This is used when there is a primitive color, but the shader should be ignored. Currently,
83 the expectation is that the primitive color will be premultiplied, though it really should be
84 unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
85 applied to the primitive color after interpolation. */
brianosman8fe485b2016-07-25 12:31:51 -070086inline bool SkPaintToGrPaintWithPrimitiveColor(GrContext* context, GrDrawContext* dc,
87 const SkPaint& skPaint, GrPaint* grPaint) {
88 return SkPaintToGrPaintWithXfermode(context, dc, skPaint, SkMatrix::I(), SkXfermode::kDst_Mode,
89 false, grPaint);
bsalomonaa48d362015-10-01 08:34:17 -070090}
91
joshualitt33a5fce2015-11-18 13:28:51 -080092/** This is used when there may or may not be a shader, and the caller wants to plugin a texture
93 lookup. If there is a shader, then its output will only be used if the texture is alpha8. */
94bool SkPaintToGrPaintWithTexture(GrContext* context,
brianosman8fe485b2016-07-25 12:31:51 -070095 GrDrawContext* dc,
joshualitt33a5fce2015-11-18 13:28:51 -080096 const SkPaint& paint,
97 const SkMatrix& viewM,
bungeman06ca8ec2016-06-09 08:01:03 -070098 sk_sp<GrFragmentProcessor> fp,
joshualitt33a5fce2015-11-18 13:28:51 -080099 bool textureIsAlphaOnly,
100 GrPaint* grPaint);
101
bsalomonf276ac52015-10-09 13:36:42 -0700102//////////////////////////////////////////////////////////////////////////////
103
brianosmana6359362016-03-21 06:55:37 -0700104GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&, const GrCaps&);
bsalomonf276ac52015-10-09 13:36:42 -0700105
brianosman396fcdb2016-07-22 06:26:11 -0700106bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*);
bsalomonf276ac52015-10-09 13:36:42 -0700107
brianosman2695eaa2016-09-21 06:45:09 -0700108/** When image filter code needs to construct a draw context to do intermediate rendering, we need
109 a renderable pixel config. The source (SkSpecialImage) may not be in a renderable format, but
110 we want to preserve the color space of that source. This picks an appropriate format to use. */
111GrPixelConfig GrRenderableConfigForColorSpace(SkColorSpace*);
112
bsalomonf276ac52015-10-09 13:36:42 -0700113/**
bsalomon045802d2015-10-20 07:58:01 -0700114 * If the compressed data in the SkData is supported (as a texture format, this returns
115 * the pixel-config that should be used, and sets outStartOfDataToUpload to the ptr into
116 * the data where the actual raw data starts (skipping any header bytes).
117 *
118 * If the compressed data is not supported, this returns kUnknown_GrPixelConfig, and
119 * ignores outStartOfDataToUpload.
120 */
bsalomonf276ac52015-10-09 13:36:42 -0700121GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
122 int expectedW, int expectedH,
123 const void** outStartOfDataToUpload);
124
bsalomonf276ac52015-10-09 13:36:42 -0700125
bsalomon045802d2015-10-20 07:58:01 -0700126/**
127 * Creates a new texture for the bitmap. Does not concern itself with cache keys or texture params.
128 * The bitmap must have CPU-accessible pixels. Attempts to take advantage of faster paths for
129 * compressed textures and yuv planes.
130 */
131GrTexture* GrUploadBitmapToTexture(GrContext*, const SkBitmap&);
bsalomonf276ac52015-10-09 13:36:42 -0700132
brianosman982eb7f2016-06-06 13:10:58 -0700133GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext*, const SkBitmap&, SkSourceGammaTreatment);
cblume55f2d2d2016-02-26 13:20:48 -0800134
bsalomon0d996862016-03-09 18:44:43 -0800135/**
136 * Creates a new texture for the pixmap.
137 */
ericrk8bea8902016-03-18 11:52:20 -0700138GrTexture* GrUploadPixmapToTexture(GrContext*, const SkPixmap&, SkBudgeted budgeted);
bsalomon0d996862016-03-09 18:44:43 -0800139
cblume186d2d42016-06-03 11:17:42 -0700140/**
141 * Creates a new texture populated with the mipmap levels.
142 */
143GrTexture* GrUploadMipMapToTexture(GrContext*, const SkImageInfo&, const GrMipLevel* texels,
144 int mipLevelCount);
145
bsalomonf276ac52015-10-09 13:36:42 -0700146//////////////////////////////////////////////////////////////////////////////
147
148GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkXfermode::kZero_Coeff);
149GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkXfermode::kOne_Coeff);
150GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkXfermode::kSC_Coeff);
151GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkXfermode::kISC_Coeff);
152GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkXfermode::kDC_Coeff);
153GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkXfermode::kIDC_Coeff);
154GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkXfermode::kSA_Coeff);
155GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkXfermode::kISA_Coeff);
156GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkXfermode::kDA_Coeff);
157GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkXfermode::kIDA_Coeff);
158GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10);
159
160#define SkXfermodeCoeffToGrBlendCoeff(X) ((GrBlendCoeff)(X))
161
reed8f343722015-08-13 13:32:39 -0700162#endif