blob: 56238da848aedaa32018ece3f46c9d1c62060866 [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
Brian Salomonc65aec92017-03-09 09:03:58 -050011#include "GrClip.h"
Hal Canary271d4952017-02-15 11:21:32 -050012#include "GrContext.h"
bsalomon3582d3e2015-02-13 14:20:05 -080013#include "GrGpuResourcePriv.h"
Robert Phillipsb726d582017-03-09 16:36:32 -050014#include "GrSurfaceProxyPriv.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -080015#include "SkBitmap.h"
herba7c9d632016-04-19 12:30:22 -070016#include "SkGr.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -080017#include "SkImagePriv.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050018#include "SkImage_Base.h"
bsalomoneaaaf0b2015-01-23 08:08:04 -080019#include "SkSurface.h"
20
Robert Phillips646e4292017-06-13 12:44:56 -040021class GrTexture;
22
bsalomoneaaaf0b2015-01-23 08:08:04 -080023class SkImage_Gpu : public SkImage_Base {
24public:
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;
Greg Daniel56008aa2018-03-14 15:33:42 -040030 SkColorType onColorType() const override;
brianosman69c166d2016-08-17 14:01:05 -070031 SkAlphaType onAlphaType() const override { return fAlphaType; }
herba7c9d632016-04-19 12:30:22 -070032
Brian Osman61624f02016-12-09 14:51:59 -050033 bool getROPixels(SkBitmap*, SkColorSpace* dstColorSpace, CachingHint) const override;
reed7fb4f8b2016-03-11 04:33:52 -080034 sk_sp<SkImage> onMakeSubset(const SkIRect&) const override;
reed85d91782015-09-10 14:33:38 -070035
Brian Salomon8a8dd332018-05-24 14:08:31 -040036 GrContext* context() const override { return fContext.get(); }
Robert Phillips0ae6faa2017-03-21 16:22:00 -040037 GrTextureProxy* peekProxy() const override {
38 return fProxy.get();
39 }
Robert Phillipsb726d582017-03-09 16:36:32 -050040 sk_sp<GrTextureProxy> asTextureProxyRef() const override {
41 return fProxy;
42 }
Brian Salomon2bbdcc42017-09-07 12:36:34 -040043 sk_sp<GrTextureProxy> asTextureProxyRef(GrContext*, const GrSamplerState&, SkColorSpace*,
Robert Phillipsb726d582017-03-09 16:36:32 -050044 sk_sp<SkColorSpace>*,
45 SkScalar scaleAdjust[2]) const override;
Robert Phillips0ae6faa2017-03-21 16:22:00 -040046
Robert Phillips3798c862017-03-27 11:08:16 -040047 sk_sp<GrTextureProxy> refPinnedTextureProxy(uint32_t* uniqueID) const override {
reed2d5b7142016-08-17 11:12:33 -070048 *uniqueID = this->uniqueID();
Robert Phillips3798c862017-03-27 11:08:16 -040049 return fProxy;
reed2d5b7142016-08-17 11:12:33 -070050 }
Robert Phillipsba375a82018-04-11 10:08:06 -040051
Robert Phillipsc5509952018-04-04 15:54:55 -040052 GrBackendTexture onGetBackendTexture(bool flushPendingGrContextIO,
53 GrSurfaceOrigin* origin) const override;
54
Robert Phillips0ae6faa2017-03-21 16:22:00 -040055 GrTexture* onGetTexture() const override;
Robert Phillips538f1a32017-03-08 14:32:55 -050056
reed8b26b992015-05-07 15:36:17 -070057 bool onReadPixels(const SkImageInfo&, void* dstPixels, size_t dstRowBytes,
reed09553032015-11-23 12:32:16 -080058 int srcX, int srcY, CachingHint) const override;
reed88d064d2015-10-12 11:30:02 -070059
Robert Phillips2c862492017-01-18 10:08:39 -050060 sk_sp<SkColorSpace> refColorSpace() { return fColorSpace; }
61
Brian Osmanb62f50c2018-07-12 14:44:27 -040062 sk_sp<SkImage> onMakeColorSpace(sk_sp<SkColorSpace>, SkColorType) const override;
Brian Osman63954c92017-03-14 12:07:12 -040063
Greg Daniela8d92112018-03-09 12:05:04 -050064 typedef ReleaseContext TextureContext;
65 typedef void (*TextureFulfillProc)(TextureContext textureContext, GrBackendTexture* outTexture);
Greg Daniel7278d682018-03-16 14:57:21 -040066 typedef void (*PromiseDoneProc)(TextureContext textureContext);
Greg Daniela8d92112018-03-09 12:05:04 -050067
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 Daniel7278d682018-03-16 14:57:21 -040087 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 Daniela8d92112018-03-09 12:05:04 -050092 @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 Daniel7278d682018-03-16 14:57:21 -0400107 @param promiseDoneProc function called when we will no longer call textureFulfillProc
Greg Daniela8d92112018-03-09 12:05:04 -0500108 @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 Daniel7278d682018-03-16 14:57:21 -0400122 PromiseDoneProc promiseDoneProc,
Greg Daniela8d92112018-03-09 12:05:04 -0500123 TextureContext textureContext);
124
Brian Salomon6a426c12018-03-15 12:16:02 -0400125 /** Implementation of MakeFromYUVTexturesCopy and MakeFromNV12TexturesCopy */
Weiliang Chenbed9d5e2018-05-22 18:44:02 -0400126 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 Salomon6a426c12018-03-15 12:16:02 -0400133
Brian Osman5bbd0762017-05-08 11:07:42 -0400134 bool onIsValid(GrContext*) const override;
135
Robert Phillips96601082018-05-29 16:13:26 -0400136 void resetContext(sk_sp<GrContext> newContext) {
137 SkASSERT(fContext->uniqueID() == newContext->uniqueID());
138 fContext = newContext;
139 }
140
bsalomoneaaaf0b2015-01-23 08:08:04 -0800141private:
Mike Klein015c8992018-08-09 12:23:19 -0400142 sk_sp<GrContext> fContext;
143 sk_sp<GrTextureProxy> fProxy;
144 const SkAlphaType fAlphaType;
145 const SkBudgeted fBudgeted;
146 sk_sp<SkColorSpace> fColorSpace;
bsalomoneaaaf0b2015-01-23 08:08:04 -0800147
148 typedef SkImage_Base INHERITED;
149};
150
151#endif