SkGr.h revision 33a5fce6126dc5d3927a71fdc6c35af6f5893fd5
18f34372f7e97482e5e61ab298b7edaa008ba2f4creed/*
28f34372f7e97482e5e61ab298b7edaa008ba2f4creed * Copyright 2015 Google Inc.
38f34372f7e97482e5e61ab298b7edaa008ba2f4creed *
48f34372f7e97482e5e61ab298b7edaa008ba2f4creed * Use of this source code is governed by a BSD-style license that can be
58f34372f7e97482e5e61ab298b7edaa008ba2f4creed * found in the LICENSE file.
68f34372f7e97482e5e61ab298b7edaa008ba2f4creed */
78f34372f7e97482e5e61ab298b7edaa008ba2f4creed
88f34372f7e97482e5e61ab298b7edaa008ba2f4creed#ifndef SkGrPriv_DEFINED
98f34372f7e97482e5e61ab298b7edaa008ba2f4creed#define SkGrPriv_DEFINED
108f34372f7e97482e5e61ab298b7edaa008ba2f4creed
118f34372f7e97482e5e61ab298b7edaa008ba2f4creed#include "GrTypes.h"
12f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomon#include "GrBlend.h"
13f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon#include "SkImageInfo.h"
14afa95e270c64c9777647b6c58b796750ced57c39bsalomon#include "SkMatrix.h"
15f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon#include "SkXfermode.h"
168f34372f7e97482e5e61ab298b7edaa008ba2f4creed
178f34372f7e97482e5e61ab298b7edaa008ba2f4creedclass GrCaps;
18f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomonclass GrContext;
19f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomonclass GrFragmentProcessor;
20f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomonclass GrPaint;
21f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomonclass GrTexture;
22afa95e270c64c9777647b6c58b796750ced57c39bsalomonclass GrTextureParams;
238f34372f7e97482e5e61ab298b7edaa008ba2f4creedclass GrUniqueKey;
24afa95e270c64c9777647b6c58b796750ced57c39bsalomonclass SkData;
25f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomonclass SkPaint;
26afa95e270c64c9777647b6c58b796750ced57c39bsalomonclass SkPixelRef;
27f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomonstruct SkIRect;
288f34372f7e97482e5e61ab298b7edaa008ba2f4creed
298f34372f7e97482e5e61ab298b7edaa008ba2f4creed/**
308f34372f7e97482e5e61ab298b7edaa008ba2f4creed *  Our key includes the offset, width, and height so that bitmaps created by extractSubset()
318f34372f7e97482e5e61ab298b7edaa008ba2f4creed *  are unique.
328f34372f7e97482e5e61ab298b7edaa008ba2f4creed *
33045802dbb7202b52f5fd2758d725f39c156a6165bsalomon *  The imageID is in the shared namespace (see SkNextID::ImageID())
348f34372f7e97482e5e61ab298b7edaa008ba2f4creed *      - SkBitmap/SkPixelRef
358f34372f7e97482e5e61ab298b7edaa008ba2f4creed *      - SkImage
368f34372f7e97482e5e61ab298b7edaa008ba2f4creed *      - SkImageGenerator
378f34372f7e97482e5e61ab298b7edaa008ba2f4creed *
388f34372f7e97482e5e61ab298b7edaa008ba2f4creed *  Note: width/height must fit in 16bits for this impl.
398f34372f7e97482e5e61ab298b7edaa008ba2f4creed */
40045802dbb7202b52f5fd2758d725f39c156a6165bsalomonvoid GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
41856e9d921462136da8562f8f122d42e114cd4710reed
42c55271f2551533b37043aa2e37f754832a43073cbsalomon/** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
43c55271f2551533b37043aa2e37f754832a43073cbsalomon    removed should the bitmap's contents change or be destroyed. */
44c55271f2551533b37043aa2e37f754832a43073cbsalomonvoid GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef);
45c55271f2551533b37043aa2e37f754832a43073cbsalomon
46f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon/** Converts an SkPaint to a GrPaint for a given GrContext. The matrix is required in order
47aa48d36397f8464dafd38c3f42fbdfb1419e8778bsalomon    to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
48f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomonbool SkPaintToGrPaint(GrContext*,
49f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon                      const SkPaint& skPaint,
50f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon                      const SkMatrix& viewM,
51f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon                      GrPaint* grPaint);
52f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon
53aa48d36397f8464dafd38c3f42fbdfb1419e8778bsalomon/** Same as above but ignores the SkShader (if any) on skPaint. */
54f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomonbool SkPaintToGrPaintNoShader(GrContext* context,
55f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon                              const SkPaint& skPaint,
56f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon                              GrPaint* grPaint);
57f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon
58f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon/** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
59aa48d36397f8464dafd38c3f42fbdfb1419e8778bsalomon    should expect an unpremul input color and produce a premultiplied output color. There is
60aa48d36397f8464dafd38c3f42fbdfb1419e8778bsalomon    no primitive color. */
61f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomonbool SkPaintToGrPaintReplaceShader(GrContext*,
62f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon                                   const SkPaint& skPaint,
63f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon                                   const GrFragmentProcessor* shaderFP,
64f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon                                   GrPaint* grPaint);
65f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon
66f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon/** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
67f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon    GrBatch's GrPrimitiveProcesssor. Currently there is a bool param to indicate whether the
68f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon    primitive color is the dst or src color to the blend in order to work around differences between
69aa48d36397f8464dafd38c3f42fbdfb1419e8778bsalomon    drawVertices and drawAtlas. */
70f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomonbool SkPaintToGrPaintWithXfermode(GrContext* context,
71f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon                                  const SkPaint& skPaint,
72f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon                                  const SkMatrix& viewM,
73f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon                                  SkXfermode::Mode primColorMode,
74f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon                                  bool primitiveIsSrc,
75f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon                                  GrPaint* grPaint);
76f1b7a1d82860e106ed7d3e0e876419e65783fb84bsalomon
77aa48d36397f8464dafd38c3f42fbdfb1419e8778bsalomon/** This is used when there is a primitive color, but the shader should be ignored. Currently,
78aa48d36397f8464dafd38c3f42fbdfb1419e8778bsalomon    the expectation is that the primitive color will be premultiplied, though it really should be
79aa48d36397f8464dafd38c3f42fbdfb1419e8778bsalomon    unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
80aa48d36397f8464dafd38c3f42fbdfb1419e8778bsalomon    applied to the primitive color after interpolation. */
81aa48d36397f8464dafd38c3f42fbdfb1419e8778bsalomoninline bool SkPaintToGrPaintWithPrimitiveColor(GrContext* context, const SkPaint& skPaint,
82aa48d36397f8464dafd38c3f42fbdfb1419e8778bsalomon                                               GrPaint* grPaint) {
83aa48d36397f8464dafd38c3f42fbdfb1419e8778bsalomon    return SkPaintToGrPaintWithXfermode(context, skPaint, SkMatrix::I(), SkXfermode::kDst_Mode,
84aa48d36397f8464dafd38c3f42fbdfb1419e8778bsalomon                                        false, grPaint);
85aa48d36397f8464dafd38c3f42fbdfb1419e8778bsalomon}
86aa48d36397f8464dafd38c3f42fbdfb1419e8778bsalomon
8733a5fce6126dc5d3927a71fdc6c35af6f5893fd5joshualitt/** This is used when there may or may not be a shader, and the caller wants to plugin a texture
8833a5fce6126dc5d3927a71fdc6c35af6f5893fd5joshualitt    lookup.  If there is a shader, then its output will only be used if the texture is alpha8. */
8933a5fce6126dc5d3927a71fdc6c35af6f5893fd5joshualittbool SkPaintToGrPaintWithTexture(GrContext* context,
9033a5fce6126dc5d3927a71fdc6c35af6f5893fd5joshualitt                                 const SkPaint& paint,
9133a5fce6126dc5d3927a71fdc6c35af6f5893fd5joshualitt                                 const SkMatrix& viewM,
9233a5fce6126dc5d3927a71fdc6c35af6f5893fd5joshualitt                                 const GrFragmentProcessor* fp,
9333a5fce6126dc5d3927a71fdc6c35af6f5893fd5joshualitt                                 bool textureIsAlphaOnly,
9433a5fce6126dc5d3927a71fdc6c35af6f5893fd5joshualitt                                 GrPaint* grPaint);
9533a5fce6126dc5d3927a71fdc6c35af6f5893fd5joshualitt
96f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomon//////////////////////////////////////////////////////////////////////////////
97f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomon
98f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomonGrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&);
99f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomon
100f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomonbool GrPixelConfig2ColorAndProfileType(GrPixelConfig, SkColorType*, SkColorProfileType*);
101f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomon
102f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomon/**
103045802dbb7202b52f5fd2758d725f39c156a6165bsalomon *  If the compressed data in the SkData is supported (as a texture format, this returns
104045802dbb7202b52f5fd2758d725f39c156a6165bsalomon *  the pixel-config that should be used, and sets outStartOfDataToUpload to the ptr into
105045802dbb7202b52f5fd2758d725f39c156a6165bsalomon *  the data where the actual raw data starts (skipping any header bytes).
106045802dbb7202b52f5fd2758d725f39c156a6165bsalomon *
107045802dbb7202b52f5fd2758d725f39c156a6165bsalomon *  If the compressed data is not supported, this returns kUnknown_GrPixelConfig, and
108045802dbb7202b52f5fd2758d725f39c156a6165bsalomon *  ignores outStartOfDataToUpload.
109045802dbb7202b52f5fd2758d725f39c156a6165bsalomon */
110f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomonGrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
111f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomon                                                 int expectedW, int expectedH,
112f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomon                                                 const void** outStartOfDataToUpload);
113f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomon
114f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomon
115045802dbb7202b52f5fd2758d725f39c156a6165bsalomon/**
116045802dbb7202b52f5fd2758d725f39c156a6165bsalomon * Creates a new texture for the bitmap. Does not concern itself with cache keys or texture params.
117045802dbb7202b52f5fd2758d725f39c156a6165bsalomon * The bitmap must have CPU-accessible pixels. Attempts to take advantage of faster paths for
118045802dbb7202b52f5fd2758d725f39c156a6165bsalomon * compressed textures and yuv planes.
119045802dbb7202b52f5fd2758d725f39c156a6165bsalomon */
120045802dbb7202b52f5fd2758d725f39c156a6165bsalomonGrTexture* GrUploadBitmapToTexture(GrContext*, const SkBitmap&);
121f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomon
122f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomon//////////////////////////////////////////////////////////////////////////////
123f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomon
124f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomonGR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkXfermode::kZero_Coeff);
125f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomonGR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkXfermode::kOne_Coeff);
126f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomonGR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkXfermode::kSC_Coeff);
127f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomonGR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkXfermode::kISC_Coeff);
128f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomonGR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkXfermode::kDC_Coeff);
129f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomonGR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkXfermode::kIDC_Coeff);
130f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomonGR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkXfermode::kSA_Coeff);
131f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomonGR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkXfermode::kISA_Coeff);
132f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomonGR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkXfermode::kDA_Coeff);
133f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomonGR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkXfermode::kIDA_Coeff);
134f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomonGR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10);
135f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomon
136f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomon#define SkXfermodeCoeffToGrBlendCoeff(X) ((GrBlendCoeff)(X))
137f276ac5c16d39a2b877300d760041f0291bb5ec9bsalomon
1388f34372f7e97482e5e61ab298b7edaa008ba2f4creed#endif
139