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