bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 SkImage_Gpu_DEFINED |
| 9 | #define SkImage_Gpu_DEFINED |
| 10 | |
Hal Canary | 271d495 | 2017-02-15 11:21:32 -0500 | [diff] [blame] | 11 | #include "GrContext.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 12 | #include "GrGpuResourcePriv.h" |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 13 | #include "GrSurfaceProxyPriv.h" |
herb | a7c9d63 | 2016-04-19 12:30:22 -0700 | [diff] [blame] | 14 | #include "SkGr.h" |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 15 | #include "SkImagePriv.h" |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 16 | #include "SkImage_GpuBase.h" |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 17 | |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 18 | class GrTexture; |
| 19 | |
Robert Phillips | 79730ac | 2018-09-25 10:40:04 -0400 | [diff] [blame] | 20 | class SkBitmap; |
Robert Phillips | 66a9734 | 2018-10-04 09:10:29 -0400 | [diff] [blame] | 21 | struct SkYUVAIndex; |
Robert Phillips | 79730ac | 2018-09-25 10:40:04 -0400 | [diff] [blame] | 22 | |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 23 | class SkImage_Gpu : public SkImage_GpuBase { |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 24 | public: |
Brian Salomon | 8a8dd33 | 2018-05-24 14:08:31 -0400 | [diff] [blame] | 25 | SkImage_Gpu(sk_sp<GrContext>, uint32_t uniqueID, SkAlphaType, sk_sp<GrTextureProxy>, |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 26 | sk_sp<SkColorSpace>, SkBudgeted); |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 27 | ~SkImage_Gpu() override; |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 28 | |
brianosman | 396fcdb | 2016-07-22 06:26:11 -0700 | [diff] [blame] | 29 | SkImageInfo onImageInfo() const override; |
herb | a7c9d63 | 2016-04-19 12:30:22 -0700 | [diff] [blame] | 30 | |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 31 | GrTextureProxy* peekProxy() const override { |
| 32 | return fProxy.get(); |
| 33 | } |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 34 | sk_sp<GrTextureProxy> asTextureProxyRef() const override { |
| 35 | return fProxy; |
| 36 | } |
Robert Phillips | 538f1a3 | 2017-03-08 14:32:55 -0500 | [diff] [blame] | 37 | |
reed | 8b26b99 | 2015-05-07 15:36:17 -0700 | [diff] [blame] | 38 | bool onReadPixels(const SkImageInfo&, void* dstPixels, size_t dstRowBytes, |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 39 | int srcX, int srcY, CachingHint) const override; |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 40 | |
Robert Phillips | 2c86249 | 2017-01-18 10:08:39 -0500 | [diff] [blame] | 41 | sk_sp<SkColorSpace> refColorSpace() { return fColorSpace; } |
| 42 | |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 43 | typedef ReleaseContext TextureContext; |
| 44 | typedef void (*TextureFulfillProc)(TextureContext textureContext, GrBackendTexture* outTexture); |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 45 | typedef void (*PromiseDoneProc)(TextureContext textureContext); |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | Create a new SkImage that is very similar to an SkImage created by MakeFromTexture. The main |
| 49 | difference is that the client doesn't have the backend texture on the gpu yet but they know |
| 50 | all the properties of the texture. So instead of passing in a GrBackendTexture the client |
| 51 | supplies a GrBackendFormat, width, height, and GrMipMapped state. |
| 52 | |
| 53 | When we actually send the draw calls to the GPU, we will call the textureFulfillProc and |
| 54 | the client will return a GrBackendTexture to us. The properties of the GrBackendTexture must |
| 55 | match those set during the SkImage creation, and it must have a valid backend gpu texture. |
| 56 | The gpu texture supplied by the client must stay valid until we call the textureReleaseProc. |
| 57 | |
| 58 | When we are done with the texture returned by the textureFulfillProc we will call the |
| 59 | textureReleaseProc passing in the textureContext. This is a signal to the client that they |
| 60 | are free to delete the underlying gpu texture. If future draws also use the same promise |
| 61 | image we will call the textureFulfillProc again if we've already called the |
| 62 | textureReleaseProc. We will always call textureFulfillProc and textureReleaseProc in pairs. |
| 63 | In other words we will never call textureFulfillProc or textureReleaseProc multiple times |
| 64 | for the same textureContext before calling the other. |
| 65 | |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 66 | We we call the promiseDoneProc when we will no longer call the textureFulfillProc again. We |
| 67 | also guarantee that there will be no outstanding textureReleaseProcs that still need to be |
| 68 | called when we call the textureDoneProc. Thus when the textureDoneProc gets called the |
| 69 | client is able to cleanup all GPU objects and meta data needed for the textureFulfill call. |
| 70 | |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 71 | @param context Gpu context |
| 72 | @param backendFormat format of promised gpu texture |
| 73 | @param width width of promised gpu texture |
| 74 | @param height height of promised gpu texture |
| 75 | @param mipMapped mip mapped state of promised gpu texture |
| 76 | @param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin |
| 77 | @param colorType one of: kUnknown_SkColorType, kAlpha_8_SkColorType, |
| 78 | kRGB_565_SkColorType, kARGB_4444_SkColorType, |
| 79 | kRGBA_8888_SkColorType, kBGRA_8888_SkColorType, |
| 80 | kGray_8_SkColorType, kRGBA_F16_SkColorType |
| 81 | @param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType, |
| 82 | kPremul_SkAlphaType, kUnpremul_SkAlphaType |
| 83 | @param colorSpace range of colors; may be nullptr |
| 84 | @param textureFulfillProc function called to get actual gpu texture |
| 85 | @param textureReleaseProc function called when texture can be released |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 86 | @param promiseDoneProc function called when we will no longer call textureFulfillProc |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 87 | @param textureContext state passed to textureFulfillProc and textureReleaseProc |
| 88 | @return created SkImage, or nullptr |
| 89 | */ |
| 90 | static sk_sp<SkImage> MakePromiseTexture(GrContext* context, |
| 91 | const GrBackendFormat& backendFormat, |
| 92 | int width, |
| 93 | int height, |
| 94 | GrMipMapped mipMapped, |
| 95 | GrSurfaceOrigin origin, |
| 96 | SkColorType colorType, |
| 97 | SkAlphaType alphaType, |
| 98 | sk_sp<SkColorSpace> colorSpace, |
| 99 | TextureFulfillProc textureFulfillProc, |
| 100 | TextureReleaseProc textureReleaseProc, |
Greg Daniel | 7278d68 | 2018-03-16 14:57:21 -0400 | [diff] [blame] | 101 | PromiseDoneProc promiseDoneProc, |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 102 | TextureContext textureContext); |
| 103 | |
Robert Phillips | 79730ac | 2018-09-25 10:40:04 -0400 | [diff] [blame] | 104 | static sk_sp<SkImage> MakePromiseYUVATexture(GrContext* context, |
| 105 | SkYUVColorSpace yuvColorSpace, |
| 106 | const GrBackendFormat yuvaFormats[], |
| 107 | const SkYUVAIndex yuvaIndices[4], |
| 108 | int imageWidth, |
| 109 | int imageHeight, |
| 110 | GrSurfaceOrigin imageOrigin, |
| 111 | sk_sp<SkColorSpace> imageColorSpace, |
| 112 | TextureFulfillProc textureFulfillProc, |
| 113 | TextureReleaseProc textureReleaseProc, |
| 114 | PromiseDoneProc promiseDoneProc, |
| 115 | TextureContext textureContexts[]); |
| 116 | |
Robert Phillips | 7a3197b | 2018-09-26 21:18:23 +0000 | [diff] [blame] | 117 | void resetContext(sk_sp<GrContext> newContext) { |
| 118 | SkASSERT(fContext->uniqueID() == newContext->uniqueID()); |
| 119 | fContext = newContext; |
| 120 | } |
| 121 | |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 122 | static sk_sp<SkImage> ConvertYUVATexturesToRGB( |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 123 | GrContext*, SkYUVColorSpace yuvColorSpace, const GrBackendTexture yuvaTextures[], |
| 124 | const SkYUVAIndex yuvaIndices[4], SkISize imageSize, GrSurfaceOrigin imageOrigin, |
| 125 | SkBudgeted, GrRenderTargetContext*); |
Brian Salomon | d2fcfb5 | 2018-09-17 21:57:11 -0400 | [diff] [blame] | 126 | |
Robert Phillips | b6df1c1 | 2018-10-05 10:31:34 -0400 | [diff] [blame] | 127 | private: |
Mike Klein | 015c899 | 2018-08-09 12:23:19 -0400 | [diff] [blame] | 128 | sk_sp<GrTextureProxy> fProxy; |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 129 | |
Jim Van Verth | 8026ccc | 2018-10-04 13:10:39 -0400 | [diff] [blame] | 130 | typedef SkImage_GpuBase INHERITED; |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | #endif |