blob: 94efe77acd0134dcafb8355135a4a40d388a26fa [file] [log] [blame]
Jim Van Verth8026ccc2018-10-04 13:10:39 -04001/*
2 * Copyright 2018 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_GpuBase_DEFINED
9#define SkImage_GpuBase_DEFINED
10
Jim Van Verth8bbce0e2018-10-08 14:34:52 -040011#include "GrBackendSurface.h"
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050012#include "GrContext.h"
Jim Van Verth8bbce0e2018-10-08 14:34:52 -040013#include "GrTypesPriv.h"
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050014#include "SkDeferredDisplayListRecorder.h"
Jim Van Verth8026ccc2018-10-04 13:10:39 -040015#include "SkImage_Base.h"
Jim Van Verth53275362018-11-09 15:42:35 -050016#include "SkYUVAIndex.h"
Jim Van Verth8026ccc2018-10-04 13:10:39 -040017
Brian Osmane9560492019-02-05 17:00:03 -050018class GrColorSpaceXform;
Jim Van Verth8026ccc2018-10-04 13:10:39 -040019class SkColorSpace;
20
21class SkImage_GpuBase : public SkImage_Base {
22public:
Brian Salomonf05e6d32018-12-20 08:41:41 -050023 SkImage_GpuBase(sk_sp<GrContext>, int width, int height, uint32_t uniqueID, SkAlphaType,
24 sk_sp<SkColorSpace>);
Jim Van Verth8026ccc2018-10-04 13:10:39 -040025 ~SkImage_GpuBase() override;
26
27 GrContext* context() const final { return fContext.get(); }
28
Brian Osmane50cdf02018-10-19 13:02:14 -040029 bool getROPixels(SkBitmap*, CachingHint) const final;
Jim Van Verth8026ccc2018-10-04 13:10:39 -040030 sk_sp<SkImage> onMakeSubset(const SkIRect& subset) const final;
31
Jim Van Verth8bbce0e2018-10-08 14:34:52 -040032 bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
33 int srcX, int srcY, CachingHint) const override;
34
Jim Van Verth8026ccc2018-10-04 13:10:39 -040035 sk_sp<GrTextureProxy> asTextureProxyRef() const override {
36 // we shouldn't end up calling this
37 SkASSERT(false);
38 return this->INHERITED::asTextureProxyRef();
39 }
Brian Osman6064e1c2018-10-19 14:27:54 -040040 sk_sp<GrTextureProxy> asTextureProxyRef(GrContext*, const GrSamplerState&,
Jim Van Verth8026ccc2018-10-04 13:10:39 -040041 SkScalar scaleAdjust[2]) const final;
42
43 sk_sp<GrTextureProxy> refPinnedTextureProxy(uint32_t* uniqueID) const final {
44 *uniqueID = this->uniqueID();
45 return this->asTextureProxyRef();
46 }
47
48 GrBackendTexture onGetBackendTexture(bool flushPendingGrContextIO,
49 GrSurfaceOrigin* origin) const final;
50
51 GrTexture* onGetTexture() const final;
52
Jim Van Verth8026ccc2018-10-04 13:10:39 -040053 bool onIsValid(GrContext*) const final;
54
Jim Van Verth21bd60d2018-10-12 15:00:20 -040055#if GR_TEST_UTILS
Robert Phillipsfd0d9702019-02-01 10:19:42 -050056 void resetContext(sk_sp<GrContext> newContext);
Jim Van Verth21bd60d2018-10-12 15:00:20 -040057#endif
58
Jim Van Verth8026ccc2018-10-04 13:10:39 -040059 static bool ValidateBackendTexture(GrContext* ctx, const GrBackendTexture& tex,
60 GrPixelConfig* config, SkColorType ct, SkAlphaType at,
61 sk_sp<SkColorSpace> cs);
Jim Van Verth0e671942018-11-09 12:03:57 -050062 static bool MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[],
Jim Van Verth53275362018-11-09 15:42:35 -050063 int numTextures, const SkYUVAIndex [4],
64 GrSurfaceOrigin imageOrigin,
Jim Van Verth0e671942018-11-09 12:03:57 -050065 sk_sp<GrTextureProxy> tempTextureProxies[4]);
Jim Van Verth8bbce0e2018-10-08 14:34:52 -040066
Jim Van Verth53275362018-11-09 15:42:35 -050067 static SkAlphaType GetAlphaTypeFromYUVAIndices(const SkYUVAIndex yuvaIndices[4]) {
68 return -1 != yuvaIndices[SkYUVAIndex::kA_Index].fIndex ? kPremul_SkAlphaType
69 : kOpaque_SkAlphaType;
70 }
71
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050072 using PromiseImageTextureContext = SkDeferredDisplayListRecorder::PromiseImageTextureContext;
73 using PromiseImageTextureFulfillProc =
74 SkDeferredDisplayListRecorder::PromiseImageTextureFulfillProc;
75 using PromiseImageTextureReleaseProc =
76 SkDeferredDisplayListRecorder::PromiseImageTextureReleaseProc;
77 using PromiseImageTextureDoneProc = SkDeferredDisplayListRecorder::PromiseImageTextureDoneProc;
Brian Salomonf55e8d52019-01-30 17:28:20 -050078 using DelayReleaseCallback = SkDeferredDisplayListRecorder::DelayReleaseCallback;
Jim Van Verth8bbce0e2018-10-08 14:34:52 -040079
Jim Van Verth8026ccc2018-10-04 13:10:39 -040080protected:
Brian Salomonbe5a0932018-12-10 10:03:26 -050081 // Helper for making a lazy proxy for a promise image. The PromiseDoneProc we be called,
82 // if not null, immediately if this function fails. Othwerwise, it is installed in the
83 // proxy along with the TextureFulfillProc and TextureReleaseProc. PromiseDoneProc must not
84 // be null.
85 static sk_sp<GrTextureProxy> MakePromiseImageLazyProxy(
86 GrContext*, int width, int height, GrSurfaceOrigin, GrPixelConfig, GrBackendFormat,
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050087 GrMipMapped, PromiseImageTextureFulfillProc, PromiseImageTextureReleaseProc,
Brian Salomonf55e8d52019-01-30 17:28:20 -050088 PromiseImageTextureDoneProc, PromiseImageTextureContext, DelayReleaseCallback);
Brian Salomonbe5a0932018-12-10 10:03:26 -050089
Jim Van Verth0e671942018-11-09 12:03:57 -050090 static bool RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext,
91 const SkRect& rect, SkYUVColorSpace yuvColorSpace,
Brian Osmane9560492019-02-05 17:00:03 -050092 sk_sp<GrColorSpaceXform> colorSpaceXform,
Jim Van Verth0e671942018-11-09 12:03:57 -050093 const sk_sp<GrTextureProxy> proxies[4],
94 const SkYUVAIndex yuvaIndices[4]);
95
Jim Van Verth8026ccc2018-10-04 13:10:39 -040096 sk_sp<GrContext> fContext;
97 const SkAlphaType fAlphaType; // alpha type for final image
Jim Van Verth8026ccc2018-10-04 13:10:39 -040098 sk_sp<SkColorSpace> fColorSpace; // color space for final image
99
100private:
101 typedef SkImage_Base INHERITED;
102};
103
Jim Van Verth8026ccc2018-10-04 13:10:39 -0400104#endif