1/*
2 * Copyright 2017 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 SkGr_DEFINED
9#define SkGr_DEFINED
10
11#include "include/core/SkCanvas.h"
12#include "include/core/SkColor.h"
13#include "include/core/SkFilterQuality.h"
14#include "include/core/SkImageInfo.h"
15#include "include/core/SkMatrix.h"
16#include "include/gpu/GrTypes.h"
17#include "include/private/SkColorData.h"
18#include "src/core/SkBlendModePriv.h"
19#include "src/gpu/GrBlend.h"
20#include "src/gpu/GrCaps.h"
21#include "src/gpu/GrColor.h"
22#include "src/gpu/GrSamplerState.h"
23
24class GrCaps;
25class GrColorInfo;
26class GrColorSpaceXform;
27class GrContext;
28class GrFragmentProcessor;
29class GrPaint;
30class GrRecordingContext;
31class GrResourceProvider;
32class GrTextureProxy;
33class GrUniqueKey;
34class SkBitmap;
35class SkData;
36class SkPaint;
37class SkPixelRef;
38class SkPixmap;
39struct SkIRect;
40
41////////////////////////////////////////////////////////////////////////////////
42// Color type conversions
43
44static 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
61/** Similar, but using SkPMColor4f. */
62SkPMColor4f SkColorToPMColor4f(SkColor, const GrColorInfo&);
63
64/** Converts an SkColor4f to the destination color space. */
65SkColor4f SkColor4fPrepForDst(SkColor4f, const GrColorInfo&);
66
67////////////////////////////////////////////////////////////////////////////////
68// Paint conversion
69
70/** Converts an SkPaint to a GrPaint for a given GrRecordingContext. The matrix is required in order
71 to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
72bool SkPaintToGrPaint(GrRecordingContext*,
73 const GrColorInfo& dstColorInfo,
74 const SkPaint& skPaint,
75 const SkMatrix& viewM,
76 GrPaint* grPaint);
77
78/** Same as above but ignores the SkShader (if any) on skPaint. */
79bool SkPaintToGrPaintNoShader(GrRecordingContext*,
80 const GrColorInfo& dstColorInfo,
81 const SkPaint& skPaint,
82 GrPaint* grPaint);
83
84/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
85 should expect an unpremul input color and produce a premultiplied output color. There is
86 no primitive color. */
87bool SkPaintToGrPaintReplaceShader(GrRecordingContext*,
88 const GrColorInfo& dstColorInfo,
89 const SkPaint& skPaint,
90 std::unique_ptr<GrFragmentProcessor> shaderFP,
91 GrPaint* grPaint);
92
93/** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
94 GrOp's GrPrimitiveProcesssor. */
95bool SkPaintToGrPaintWithXfermode(GrRecordingContext*,
96 const GrColorInfo& dstColorInfo,
97 const SkPaint& skPaint,
98 const SkMatrix& viewM,
99 SkBlendMode primColorMode,
100 GrPaint* grPaint);
101
102/** This is used when there is a primitive color, but the shader should be ignored. Currently,
103 the expectation is that the primitive color will be premultiplied, though it really should be
104 unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
105 applied to the primitive color after interpolation. */
106inline bool SkPaintToGrPaintWithPrimitiveColor(GrRecordingContext* context,
107 const GrColorInfo& dstColorInfo,
108 const SkPaint& skPaint,
109 GrPaint* grPaint) {
110 return SkPaintToGrPaintWithXfermode(context, dstColorInfo, skPaint, SkMatrix::I(),
111 SkBlendMode::kDst, grPaint);
112}
113
114/** This is used when there may or may not be a shader, and the caller wants to plugin a texture
115 lookup. If there is a shader, then its output will only be used if the texture is alpha8. */
116bool SkPaintToGrPaintWithTexture(GrRecordingContext*,
117 const GrColorInfo& dstColorInfo,
118 const SkPaint& skPaint,
119 const SkMatrix& viewM,
120 std::unique_ptr<GrFragmentProcessor> fp,
121 bool textureIsAlphaOnly,
122 GrPaint* grPaint);
123
124////////////////////////////////////////////////////////////////////////////////
125// Misc Sk to Gr type conversions
126
127GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(int imageWidth, int imageHeight,
128 SkFilterQuality paintFilterQuality,
129 const SkMatrix& viewM,
130 const SkMatrix& localM,
131 bool sharpenMipmappedTextures,
132 bool* doBicubic);
133
134//////////////////////////////////////////////////////////////////////////////
135
136static_assert((int)kZero_GrBlendCoeff == (int)SkBlendModeCoeff::kZero);
137static_assert((int)kOne_GrBlendCoeff == (int)SkBlendModeCoeff::kOne);
138static_assert((int)kSC_GrBlendCoeff == (int)SkBlendModeCoeff::kSC);
139static_assert((int)kISC_GrBlendCoeff == (int)SkBlendModeCoeff::kISC);
140static_assert((int)kDC_GrBlendCoeff == (int)SkBlendModeCoeff::kDC);
141static_assert((int)kIDC_GrBlendCoeff == (int)SkBlendModeCoeff::kIDC);
142static_assert((int)kSA_GrBlendCoeff == (int)SkBlendModeCoeff::kSA);
143static_assert((int)kISA_GrBlendCoeff == (int)SkBlendModeCoeff::kISA);
144static_assert((int)kDA_GrBlendCoeff == (int)SkBlendModeCoeff::kDA);
145static_assert((int)kIDA_GrBlendCoeff == (int)SkBlendModeCoeff::kIDA);
146// static_assert(SkXfermode::kCoeffCount == 10);
147
148////////////////////////////////////////////////////////////////////////////////
149// Texture management
150
151/**
152 * Policies for how to create textures for SkImages (and SkBitmaps).
153 */
154enum class GrImageTexGenPolicy : int {
155 // Choose the cheapest way to generate the texture. Use GrResourceCache if appropriate.
156 kDraw,
157 // Always make a new texture that is uncached and unbudgeted.
158 kNew_Uncached_Unbudgeted,
159 // Always make a new texture that is uncached and budgeted.
160 kNew_Uncached_Budgeted
161};
162
163/**
164 * Returns a view that wraps a texture representing the bitmap. The texture is inserted into the
165 * cache (unless the bitmap is marked volatile and can be retrieved again via this function.
166 * A MIP mapped texture may be returned even when GrMipMapped is kNo. The function will succeed
167 * with a non-MIP mapped texture if GrMipMapped is kYes but MIP mapping is not supported.
168 */
169GrSurfaceProxyView GrRefCachedBitmapView(GrRecordingContext*, const SkBitmap&, GrMipMapped);
170
171/**
172 * Creates a new texture with mipmap levels and copies the baseProxy into the base layer.
173 */
174sk_sp<GrSurfaceProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext*,
175 GrSurfaceProxy* baseProxy,
176 GrSurfaceOrigin origin,
177 SkBudgeted = SkBudgeted::kYes);
178/**
179 * Same as GrCopyBaseMipMapToTextureProxy but takes the src as a view and returns a view with same
180 * origin and swizzle as the src view.
181 */
182GrSurfaceProxyView GrCopyBaseMipMapToView(GrRecordingContext*,
183 GrSurfaceProxyView,
184 SkBudgeted = SkBudgeted::kYes);
185
186/*
187 * Create a texture proxy from the provided bitmap and add it to the texture cache
188 * using the key also extracted from 'bitmp'.
189 */
190GrSurfaceProxyView GrMakeCachedBitmapProxyView(GrRecordingContext*, const SkBitmap& bitmap);
191
192/**
193 * Our key includes the offset, width, and height so that bitmaps created by extractSubset()
194 * are unique.
195 *
196 * The imageID is in the shared namespace (see SkNextID::ImageID())
197 * - SkBitmap/SkPixelRef
198 * - SkImage
199 * - SkImageGenerator
200 */
201void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
202
203/**
204 * Makes a SkIDChangeListener from a GrUniqueKey. The key will be invalidated in the resource
205 * cache if the ID becomes invalid. This also modifies the key so that it will cause the listener
206 * to be deregistered if the key is destroyed (to prevent unbounded listener growth when resources
207 * are purged before listeners trigger).
208 */
209sk_sp<SkIDChangeListener> GrMakeUniqueKeyInvalidationListener(GrUniqueKey*, uint32_t contextID);
210
211#endif
212