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