1 /*
2 * Copyright 2023 Google LLC
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 SkImageAndroid_DEFINED
9 #define SkImageAndroid_DEFINED
10
11 #include "include/core/SkImage.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/gpu/ganesh/GrTypes.h"
14
15 class SkColorSpace;
16 class GrDirectContext;
17 class SkPixmap;
18 struct AHardwareBuffer;
19
20 namespace SkImages {
21
22 /** (See Skia bug 7447)
23 Creates SkImage from Android hardware buffer.
24 Returned SkImage takes a reference on the buffer.
25 Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
26 @param hardwareBuffer AHardwareBuffer Android hardware buffer
27 @param colorSpace range of colors; may be nullptr
28 @return created SkImage, or nullptr
29 */
30 SK_API sk_sp<SkImage> DeferredFromAHardwareBuffer(AHardwareBuffer* hardwareBuffer,
31 SkAlphaType alphaType = kPremul_SkAlphaType);
32 SK_API sk_sp<SkImage> DeferredFromAHardwareBuffer(
33 AHardwareBuffer* hardwareBuffer,
34 SkAlphaType alphaType,
35 sk_sp<SkColorSpace> colorSpace,
36 GrSurfaceOrigin surfaceOrigin = kTopLeft_GrSurfaceOrigin);
37
38 /** Creates SkImage from Android hardware buffer and uploads the data from the SkPixmap to it.
39 Returned SkImage takes a reference on the buffer.
40 Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
41 @param context GPU context
42 @param pixmap SkPixmap that contains data to be uploaded to the AHardwareBuffer
43 @param hardwareBuffer AHardwareBuffer Android hardware buffer
44 @param surfaceOrigin surface origin for resulting image
45 @return created SkImage, or nullptr
46 */
47 SK_API sk_sp<SkImage> TextureFromAHardwareBufferWithData(
48 GrDirectContext* context,
49 const SkPixmap& pixmap,
50 AHardwareBuffer* hardwareBuffer,
51 GrSurfaceOrigin surfaceOrigin = kTopLeft_GrSurfaceOrigin);
52
53 /**
54 * Like SkImagePriv::SkMakeImageFromRasterBitmap, except this can be pinned using
55 * skgpu::ganesh::PinAsTexture and CopyPixelMode is never.
56 */
57 SK_API sk_sp<SkImage> PinnableRasterFromBitmap(const SkBitmap&);
58
59 } // namespace SkImages
60
61 // TODO(kjlubick) remove this after Android has been ported.
62 namespace sk_image_factory {
MakePinnableFromRasterBitmap(const SkBitmap & b)63 inline sk_sp<SkImage> MakePinnableFromRasterBitmap(const SkBitmap& b) {
64 return SkImages::PinnableRasterFromBitmap(b);
65 }
66 } // namespace sk_image_factory
67
68 namespace skgpu::ganesh {
69 /**
70 * Will attempt to upload and lock the contents of the image as a texture, so that subsequent
71 * draws to a gpu-target will come from that texture (and not by looking at the original image
72 * src). In particular this is intended to use the texture even if the image's original content
73 * changes subsequent to this call (i.e. the src is mutable!).
74 *
75 * Only compatible with SkImages created from SkImages::PinnableRasterFromBitmap.
76 *
77 * All successful calls must be balanced by an equal number of calls to UnpinTexture().
78 *
79 * Once in this "pinned" state, the image has all of the same thread restrictions that exist
80 * for a natively created gpu image (e.g. SkImage::MakeFromTexture)
81 * - all drawing, pinning, unpinning must happen in the same thread as the GrContext.
82 *
83 * @return true if the image was successfully uploaded and locked into a texture
84 */
85 bool PinAsTexture(GrRecordingContext*, SkImage*);
86
87 /**
88 * The balancing call to a successful invocation of PinAsTexture. When a balanced
89 * number of calls have been made, then the "pinned" texture is free to be purged, etc. This
90 * also means that a subsequent "pin" call will look at the original content again, and if
91 * its uniqueID/generationID has changed, then a newer texture will be uploaded/pinned.
92 *
93 * Only compatible with SkImages created from SkImages::PinnableRasterFromBitmap.
94 *
95 * The context passed to unpin must match the one passed to pin.
96 */
97 void UnpinTexture(GrRecordingContext*, SkImage*);
98
99 } // namespace skgpu::ganesh
100
101 #endif
102