blob: ec407c28f9303dc1696d1b738c5968eb0d2eecea [file] [log] [blame]
bsalomoneaaaf0b2015-01-23 08:08:04 -08001/*
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 Canary271d4952017-02-15 11:21:32 -050011#include "GrContext.h"
bsalomon3582d3e2015-02-13 14:20:05 -080012#include "GrGpuResourcePriv.h"
Robert Phillipsb726d582017-03-09 16:36:32 -050013#include "GrSurfaceProxyPriv.h"
herba7c9d632016-04-19 12:30:22 -070014#include "SkGr.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -080015#include "SkImagePriv.h"
Jim Van Verth8026ccc2018-10-04 13:10:39 -040016#include "SkImage_GpuBase.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -080017
Robert Phillips646e4292017-06-13 12:44:56 -040018class GrTexture;
19
Robert Phillips79730ac2018-09-25 10:40:04 -040020class SkBitmap;
Robert Phillips66a97342018-10-04 09:10:29 -040021struct SkYUVAIndex;
Robert Phillips79730ac2018-09-25 10:40:04 -040022
Jim Van Verth8026ccc2018-10-04 13:10:39 -040023class SkImage_Gpu : public SkImage_GpuBase {
bsalomoneaaaf0b2015-01-23 08:08:04 -080024public:
Brian Salomon8a8dd332018-05-24 14:08:31 -040025 SkImage_Gpu(sk_sp<GrContext>, uint32_t uniqueID, SkAlphaType, sk_sp<GrTextureProxy>,
Robert Phillipsb726d582017-03-09 16:36:32 -050026 sk_sp<SkColorSpace>, SkBudgeted);
reed6f1216a2015-08-04 08:10:13 -070027 ~SkImage_Gpu() override;
bsalomoneaaaf0b2015-01-23 08:08:04 -080028
brianosman396fcdb2016-07-22 06:26:11 -070029 SkImageInfo onImageInfo() const override;
herba7c9d632016-04-19 12:30:22 -070030
Robert Phillips0ae6faa2017-03-21 16:22:00 -040031 GrTextureProxy* peekProxy() const override {
32 return fProxy.get();
33 }
Robert Phillipsb726d582017-03-09 16:36:32 -050034 sk_sp<GrTextureProxy> asTextureProxyRef() const override {
35 return fProxy;
36 }
Robert Phillips538f1a32017-03-08 14:32:55 -050037
reed8b26b992015-05-07 15:36:17 -070038 bool onReadPixels(const SkImageInfo&, void* dstPixels, size_t dstRowBytes,
reed09553032015-11-23 12:32:16 -080039 int srcX, int srcY, CachingHint) const override;
reed88d064d2015-10-12 11:30:02 -070040
Robert Phillips2c862492017-01-18 10:08:39 -050041 sk_sp<SkColorSpace> refColorSpace() { return fColorSpace; }
42
Greg Daniela8d92112018-03-09 12:05:04 -050043 typedef ReleaseContext TextureContext;
44 typedef void (*TextureFulfillProc)(TextureContext textureContext, GrBackendTexture* outTexture);
Greg Daniel7278d682018-03-16 14:57:21 -040045 typedef void (*PromiseDoneProc)(TextureContext textureContext);
Greg Daniela8d92112018-03-09 12:05:04 -050046
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 Daniel7278d682018-03-16 14:57:21 -040066 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 Daniela8d92112018-03-09 12:05:04 -050071 @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 Daniel7278d682018-03-16 14:57:21 -040086 @param promiseDoneProc function called when we will no longer call textureFulfillProc
Greg Daniela8d92112018-03-09 12:05:04 -050087 @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 Daniel7278d682018-03-16 14:57:21 -0400101 PromiseDoneProc promiseDoneProc,
Greg Daniela8d92112018-03-09 12:05:04 -0500102 TextureContext textureContext);
103
Robert Phillips79730ac2018-09-25 10:40:04 -0400104 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 Phillips7a3197b2018-09-26 21:18:23 +0000117 void resetContext(sk_sp<GrContext> newContext) {
118 SkASSERT(fContext->uniqueID() == newContext->uniqueID());
119 fContext = newContext;
120 }
121
Brian Salomond2fcfb52018-09-17 21:57:11 -0400122 static sk_sp<SkImage> ConvertYUVATexturesToRGB(
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400123 GrContext*, SkYUVColorSpace yuvColorSpace, const GrBackendTexture yuvaTextures[],
124 const SkYUVAIndex yuvaIndices[4], SkISize imageSize, GrSurfaceOrigin imageOrigin,
125 SkBudgeted, GrRenderTargetContext*);
Brian Salomond2fcfb52018-09-17 21:57:11 -0400126
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400127private:
Mike Klein015c8992018-08-09 12:23:19 -0400128 sk_sp<GrTextureProxy> fProxy;
bsalomoneaaaf0b2015-01-23 08:08:04 -0800129
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400130 typedef SkImage_GpuBase INHERITED;
bsalomoneaaaf0b2015-01-23 08:08:04 -0800131};
132
133#endif