SkImagePriv.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2012 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. #ifndef SkImagePriv_DEFINED
  8. #define SkImagePriv_DEFINED
  9. #include "include/core/SkImage.h"
  10. #include "include/core/SkSurface.h"
  11. #include "include/core/SkTileMode.h"
  12. enum SkCopyPixelsMode {
  13. kIfMutable_SkCopyPixelsMode, //!< only copy src pixels if they are marked mutable
  14. kAlways_SkCopyPixelsMode, //!< always copy src pixels (even if they are marked immutable)
  15. kNever_SkCopyPixelsMode, //!< never copy src pixels (even if they are marked mutable)
  16. };
  17. // A good size for creating shader contexts on the stack.
  18. enum {kSkBlitterContextSize = 3332};
  19. // If alloc is non-nullptr, it will be used to allocate the returned SkShader, and MUST outlive
  20. // the SkShader.
  21. sk_sp<SkShader> SkMakeBitmapShader(const SkBitmap& src, SkTileMode, SkTileMode,
  22. const SkMatrix* localMatrix, SkCopyPixelsMode);
  23. // Convenience function to return a shader that implements the shader+image behavior defined for
  24. // drawImage/Bitmap where the paint's shader is ignored when the bitmap is a color image, but
  25. // properly compose them together when it is an alpha image. This allows the returned paint to
  26. // be assigned to a paint clone without discarding the original behavior.
  27. sk_sp<SkShader> SkMakeBitmapShaderForPaint(const SkPaint& paint, const SkBitmap& src,
  28. SkTileMode, SkTileMode,
  29. const SkMatrix* localMatrix, SkCopyPixelsMode);
  30. /**
  31. * Examines the bitmap to decide if it can share the existing pixelRef, or
  32. * if it needs to make a deep-copy of the pixels.
  33. *
  34. * The bitmap's pixelref will be shared if either the bitmap is marked as
  35. * immutable, or CopyPixelsMode allows it. Shared pixel refs are also
  36. * locked when kLocked_SharedPixelRefMode is specified.
  37. *
  38. * Passing kLocked_SharedPixelRefMode allows the image's peekPixels() method
  39. * to succeed, but it will force any lazy decodes/generators to execute if
  40. * they exist on the pixelref.
  41. *
  42. * It is illegal to call this with a texture-backed bitmap.
  43. *
  44. * If the bitmap's colortype cannot be converted into a corresponding
  45. * SkImageInfo, or the bitmap's pixels cannot be accessed, this will return
  46. * nullptr.
  47. */
  48. extern SK_API sk_sp<SkImage> SkMakeImageFromRasterBitmap(const SkBitmap&, SkCopyPixelsMode);
  49. // Given an image created from SkNewImageFromBitmap, return its pixelref. This
  50. // may be called to see if the surface and the image share the same pixelref,
  51. // in which case the surface may need to perform a copy-on-write.
  52. extern const SkPixelRef* SkBitmapImageGetPixelRef(const SkImage* rasterImage);
  53. /**
  54. * Will attempt to upload and lock the contents of the image as a texture, so that subsequent
  55. * draws to a gpu-target will come from that texture (and not by looking at the original image
  56. * src). In particular this is intended to use the texture even if the image's original content
  57. * changes subsequent to this call (i.e. the src is mutable!).
  58. *
  59. * All successful calls must be balanced by an equal number of calls to SkImage_unpinAsTexture().
  60. *
  61. * Once in this "pinned" state, the image has all of the same thread restrictions that exist
  62. * for a natively created gpu image (e.g. SkImage::MakeFromTexture)
  63. * - all drawing, pinning, unpinning must happen in the same thread as the GrContext.
  64. *
  65. * @return true if the image was successfully uploaded and locked into a texture
  66. */
  67. bool SkImage_pinAsTexture(const SkImage*, GrContext*);
  68. /**
  69. * The balancing call to a successful invokation of SkImage_pinAsTexture. When a balanced number of
  70. * calls have been made, then the "pinned" texture is free to be purged, etc. This also means that a
  71. * subsequent "pin" call will look at the original content again, and if its uniqueID/generationID
  72. * has changed, then a newer texture will be uploaded/pinned.
  73. *
  74. * The context passed to unpin must match the one passed to pin.
  75. */
  76. void SkImage_unpinAsTexture(const SkImage*, GrContext*);
  77. /**
  78. * Returns the bounds of the image relative to its encoded buffer. For all non-lazy images,
  79. * this returns (0,0,width,height). For a lazy-image, it may return a subset of that rect.
  80. */
  81. SkIRect SkImage_getSubset(const SkImage*);
  82. /**
  83. * Returns a new image containing the same pixel values as the source, but with a different color
  84. * space assigned. This performs no color space conversion. Primarily used in tests, to visualize
  85. * the results of rendering in wide or narrow gamuts.
  86. */
  87. sk_sp<SkImage> SkImageMakeRasterCopyAndAssignColorSpace(const SkImage*, SkColorSpace*);
  88. #endif