blob: 205698300b1f48b5a85fda8fc6e50172013138e3 [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;
19class GrFragmentProcessor;
20class GrPaint;
bsalomonf276ac52015-10-09 13:36:42 -070021class GrTexture;
bsalomonafa95e22015-10-12 10:39:46 -070022class GrTextureParams;
reed8f343722015-08-13 13:32:39 -070023class GrUniqueKey;
bsalomonafa95e22015-10-12 10:39:46 -070024class SkData;
bsalomonf1b7a1d2015-09-28 06:26:28 -070025class SkPaint;
bsalomonafa95e22015-10-12 10:39:46 -070026class SkPixelRef;
bsalomonf1b7a1d2015-09-28 06:26:28 -070027struct SkIRect;
reed8f343722015-08-13 13:32:39 -070028
29/**
30 * Our key includes the offset, width, and height so that bitmaps created by extractSubset()
31 * are unique.
32 *
bsalomon045802d2015-10-20 07:58:01 -070033 * The imageID is in the shared namespace (see SkNextID::ImageID())
reed8f343722015-08-13 13:32:39 -070034 * - SkBitmap/SkPixelRef
35 * - SkImage
36 * - SkImageGenerator
37 *
38 * Note: width/height must fit in 16bits for this impl.
39 */
bsalomon045802d2015-10-20 07:58:01 -070040void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
reed856e9d92015-09-30 12:21:45 -070041
bsalomonc55271f2015-11-09 11:55:57 -080042/** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
43 removed should the bitmap's contents change or be destroyed. */
44void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef);
45
bsalomonf1b7a1d2015-09-28 06:26:28 -070046/** Converts an SkPaint to a GrPaint for a given GrContext. The matrix is required in order
bsalomonaa48d362015-10-01 08:34:17 -070047 to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070048bool SkPaintToGrPaint(GrContext*,
49 const SkPaint& skPaint,
50 const SkMatrix& viewM,
51 GrPaint* grPaint);
52
bsalomonaa48d362015-10-01 08:34:17 -070053/** Same as above but ignores the SkShader (if any) on skPaint. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070054bool SkPaintToGrPaintNoShader(GrContext* context,
55 const SkPaint& skPaint,
56 GrPaint* grPaint);
57
58/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
bsalomonaa48d362015-10-01 08:34:17 -070059 should expect an unpremul input color and produce a premultiplied output color. There is
60 no primitive color. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070061bool SkPaintToGrPaintReplaceShader(GrContext*,
62 const SkPaint& skPaint,
63 const GrFragmentProcessor* shaderFP,
64 GrPaint* grPaint);
65
66/** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
67 GrBatch's GrPrimitiveProcesssor. Currently there is a bool param to indicate whether the
68 primitive color is the dst or src color to the blend in order to work around differences between
bsalomonaa48d362015-10-01 08:34:17 -070069 drawVertices and drawAtlas. */
bsalomonf1b7a1d2015-09-28 06:26:28 -070070bool SkPaintToGrPaintWithXfermode(GrContext* context,
71 const SkPaint& skPaint,
72 const SkMatrix& viewM,
73 SkXfermode::Mode primColorMode,
74 bool primitiveIsSrc,
75 GrPaint* grPaint);
76
bsalomonaa48d362015-10-01 08:34:17 -070077/** This is used when there is a primitive color, but the shader should be ignored. Currently,
78 the expectation is that the primitive color will be premultiplied, though it really should be
79 unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
80 applied to the primitive color after interpolation. */
81inline bool SkPaintToGrPaintWithPrimitiveColor(GrContext* context, const SkPaint& skPaint,
82 GrPaint* grPaint) {
83 return SkPaintToGrPaintWithXfermode(context, skPaint, SkMatrix::I(), SkXfermode::kDst_Mode,
84 false, grPaint);
85}
86
joshualitt33a5fce2015-11-18 13:28:51 -080087/** This is used when there may or may not be a shader, and the caller wants to plugin a texture
88 lookup. If there is a shader, then its output will only be used if the texture is alpha8. */
89bool SkPaintToGrPaintWithTexture(GrContext* context,
90 const SkPaint& paint,
91 const SkMatrix& viewM,
92 const GrFragmentProcessor* fp,
93 bool textureIsAlphaOnly,
94 GrPaint* grPaint);
95
bsalomonf276ac52015-10-09 13:36:42 -070096//////////////////////////////////////////////////////////////////////////////
97
bsalomonf276ac52015-10-09 13:36:42 -070098GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&);
99
100bool GrPixelConfig2ColorAndProfileType(GrPixelConfig, SkColorType*, SkColorProfileType*);
101
102/**
bsalomon045802d2015-10-20 07:58:01 -0700103 * If the compressed data in the SkData is supported (as a texture format, this returns
104 * the pixel-config that should be used, and sets outStartOfDataToUpload to the ptr into
105 * the data where the actual raw data starts (skipping any header bytes).
106 *
107 * If the compressed data is not supported, this returns kUnknown_GrPixelConfig, and
108 * ignores outStartOfDataToUpload.
109 */
bsalomonf276ac52015-10-09 13:36:42 -0700110GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
111 int expectedW, int expectedH,
112 const void** outStartOfDataToUpload);
113
bsalomonf276ac52015-10-09 13:36:42 -0700114
bsalomon045802d2015-10-20 07:58:01 -0700115/**
116 * Creates a new texture for the bitmap. Does not concern itself with cache keys or texture params.
117 * The bitmap must have CPU-accessible pixels. Attempts to take advantage of faster paths for
118 * compressed textures and yuv planes.
119 */
120GrTexture* GrUploadBitmapToTexture(GrContext*, const SkBitmap&);
bsalomonf276ac52015-10-09 13:36:42 -0700121
cblume55f2d2d2016-02-26 13:20:48 -0800122GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext*, const SkBitmap&);
123
bsalomonf276ac52015-10-09 13:36:42 -0700124//////////////////////////////////////////////////////////////////////////////
125
126GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkXfermode::kZero_Coeff);
127GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkXfermode::kOne_Coeff);
128GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkXfermode::kSC_Coeff);
129GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkXfermode::kISC_Coeff);
130GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkXfermode::kDC_Coeff);
131GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkXfermode::kIDC_Coeff);
132GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkXfermode::kSA_Coeff);
133GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkXfermode::kISA_Coeff);
134GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkXfermode::kDA_Coeff);
135GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkXfermode::kIDA_Coeff);
136GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10);
137
138#define SkXfermodeCoeffToGrBlendCoeff(X) ((GrBlendCoeff)(X))
139
reed8f343722015-08-13 13:32:39 -0700140#endif