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