| 1 | /* |
| 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 | |
| 11 | #include "include/core/SkDeferredDisplayListRecorder.h" |
| 12 | #include "include/core/SkYUVAIndex.h" |
| 13 | #include "include/gpu/GrBackendSurface.h" |
| 14 | #include "include/gpu/GrContext.h" |
| 15 | #include "include/private/GrTypesPriv.h" |
| 16 | #include "src/image/SkImage_Base.h" |
| 17 | |
| 18 | class GrColorSpaceXform; |
| 19 | class SkColorSpace; |
| 20 | |
| 21 | class SkImage_GpuBase : public SkImage_Base { |
| 22 | public: |
| 23 | GrContext* context() const final { return fContext.get(); } |
| 24 | |
| 25 | bool getROPixels(SkBitmap*, CachingHint) const final; |
| 26 | sk_sp<SkImage> onMakeSubset(GrRecordingContext*, const SkIRect& subset) const final; |
| 27 | |
| 28 | bool onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, |
| 29 | int srcX, int srcY, CachingHint) const override; |
| 30 | |
| 31 | GrSurfaceProxyView refView(GrRecordingContext*, GrMipMapped) const final; |
| 32 | |
| 33 | GrSurfaceProxyView refPinnedView(GrRecordingContext* context, uint32_t* uniqueID) const final { |
| 34 | *uniqueID = this->uniqueID(); |
| 35 | SkASSERT(this->view(context)); |
| 36 | return *this->view(context); |
| 37 | } |
| 38 | |
| 39 | GrBackendTexture onGetBackendTexture(bool flushPendingGrContextIO, |
| 40 | GrSurfaceOrigin* origin) const final; |
| 41 | |
| 42 | GrTexture* getTexture() const; |
| 43 | |
| 44 | bool onIsValid(GrContext*) const final; |
| 45 | |
| 46 | #if GR_TEST_UTILS |
| 47 | void resetContext(sk_sp<GrContext> newContext); |
| 48 | #endif |
| 49 | |
| 50 | static bool ValidateBackendTexture(const GrCaps*, const GrBackendTexture& tex, |
| 51 | GrColorType grCT, SkColorType ct, SkAlphaType at, |
| 52 | sk_sp<SkColorSpace> cs); |
| 53 | static bool ValidateCompressedBackendTexture(const GrCaps*, const GrBackendTexture& tex, |
| 54 | SkAlphaType); |
| 55 | static bool MakeTempTextureProxies(GrContext* ctx, const GrBackendTexture yuvaTextures[], |
| 56 | int numTextures, const SkYUVAIndex [4], |
| 57 | GrSurfaceOrigin imageOrigin, |
| 58 | GrSurfaceProxyView tempViews[4]); |
| 59 | |
| 60 | static SkAlphaType GetAlphaTypeFromYUVAIndices(const SkYUVAIndex yuvaIndices[4]) { |
| 61 | return -1 != yuvaIndices[SkYUVAIndex::kA_Index].fIndex ? kPremul_SkAlphaType |
| 62 | : kOpaque_SkAlphaType; |
| 63 | } |
| 64 | |
| 65 | using PromiseImageTextureContext = SkDeferredDisplayListRecorder::PromiseImageTextureContext; |
| 66 | using PromiseImageTextureFulfillProc = |
| 67 | SkDeferredDisplayListRecorder::PromiseImageTextureFulfillProc; |
| 68 | using PromiseImageTextureReleaseProc = |
| 69 | SkDeferredDisplayListRecorder::PromiseImageTextureReleaseProc; |
| 70 | using PromiseImageTextureDoneProc = SkDeferredDisplayListRecorder::PromiseImageTextureDoneProc; |
| 71 | |
| 72 | protected: |
| 73 | SkImage_GpuBase(sk_sp<GrContext>, SkISize size, uint32_t uniqueID, SkColorType, SkAlphaType, |
| 74 | sk_sp<SkColorSpace>); |
| 75 | |
| 76 | using PromiseImageApiVersion = SkDeferredDisplayListRecorder::PromiseImageApiVersion; |
| 77 | // Helper for making a lazy proxy for a promise image. The PromiseDoneProc we be called, |
| 78 | // if not null, immediately if this function fails. Othwerwise, it is installed in the |
| 79 | // proxy along with the TextureFulfillProc and TextureReleaseProc. PromiseDoneProc must not |
| 80 | // be null. |
| 81 | static sk_sp<GrTextureProxy> MakePromiseImageLazyProxy( |
| 82 | GrContext*, int width, int height, GrBackendFormat, GrMipMapped, |
| 83 | PromiseImageTextureFulfillProc, PromiseImageTextureReleaseProc, |
| 84 | PromiseImageTextureDoneProc, PromiseImageTextureContext, PromiseImageApiVersion); |
| 85 | |
| 86 | static bool RenderYUVAToRGBA(GrContext* ctx, GrRenderTargetContext* renderTargetContext, |
| 87 | const SkRect& rect, SkYUVColorSpace yuvColorSpace, |
| 88 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
| 89 | GrSurfaceProxyView views[4], |
| 90 | const SkYUVAIndex yuvaIndices[4]); |
| 91 | |
| 92 | sk_sp<GrContext> fContext; |
| 93 | |
| 94 | private: |
| 95 | typedef SkImage_Base INHERITED; |
| 96 | }; |
| 97 | |
| 98 | #endif |
| 99 | |