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_GpuYUVA_DEFINED 9 #define SkImage_GpuYUVA_DEFINED 10 11 #include "GrBackendSurface.h" 12 #include "GrContext.h" 13 #include "SkCachedData.h" 14 #include "SkImage_GpuBase.h" 15 16 class GrTexture; 17 struct SkYUVASizeInfo; 18 19 // Wraps the 3 or 4 planes of a YUVA image for consumption by the GPU. 20 // Initially any direct rendering will be done by passing the individual planes to a shader. 21 // Once any method requests a flattened image (e.g., onReadPixels), the flattened RGB 22 // proxy will be stored and used for any future rendering. 23 class SkImage_GpuYUVA : public SkImage_GpuBase { 24 public: 25 friend class GrYUVAImageTextureMaker; 26 27 SkImage_GpuYUVA(sk_sp<GrContext>, int width, int height, uint32_t uniqueID, SkYUVColorSpace, 28 sk_sp<GrTextureProxy> proxies[], int numProxies, const SkYUVAIndex[4], 29 GrSurfaceOrigin, sk_sp<SkColorSpace>); 30 ~SkImage_GpuYUVA() override; 31 32 SkImageInfo onImageInfo() const override; 33 34 // This returns the single backing proxy if the YUV channels have already been flattened but 35 // nullptr if they have not. 36 GrTextureProxy* peekProxy() const override; 37 sk_sp<GrTextureProxy> asTextureProxyRef(GrRecordingContext*) const override; 38 onIsTextureBacked()39 virtual bool onIsTextureBacked() const override { return SkToBool(fProxies[0].get()); } 40 41 sk_sp<SkImage> onMakeColorTypeAndColorSpace(GrRecordingContext*, 42 SkColorType, sk_sp<SkColorSpace>) const final; 43 isYUVA()44 virtual bool isYUVA() const override { return true; } asYUVATextureProxiesRef(sk_sp<GrTextureProxy> proxies[4],SkYUVAIndex yuvaIndices[4],SkYUVColorSpace * yuvColorSpace)45 virtual bool asYUVATextureProxiesRef(sk_sp<GrTextureProxy> proxies[4], 46 SkYUVAIndex yuvaIndices[4], 47 SkYUVColorSpace* yuvColorSpace) const override { 48 for (int i = 0; i < 4; ++i) { 49 proxies[i] = fProxies[i]; 50 yuvaIndices[i] = fYUVAIndices[i]; 51 } 52 *yuvColorSpace = fYUVColorSpace; 53 return true; 54 } 55 56 bool setupMipmapsForPlanes(GrRecordingContext*) const; 57 58 // Returns a ref-ed texture proxy with miplevels 59 sk_sp<GrTextureProxy> asMippedTextureProxyRef(GrRecordingContext*) const; 60 61 /** 62 * This is the implementation of SkDeferredDisplayListRecorder::makeYUVAPromiseTexture. 63 */ 64 static sk_sp<SkImage> MakePromiseYUVATexture(GrContext* context, 65 SkYUVColorSpace yuvColorSpace, 66 const GrBackendFormat yuvaFormats[], 67 const SkISize yuvaSizes[], 68 const SkYUVAIndex yuvaIndices[4], 69 int width, 70 int height, 71 GrSurfaceOrigin imageOrigin, 72 sk_sp<SkColorSpace> imageColorSpace, 73 PromiseImageTextureFulfillProc textureFulfillProc, 74 PromiseImageTextureReleaseProc textureReleaseProc, 75 PromiseImageTextureDoneProc textureDoneProc, 76 PromiseImageTextureContext textureContexts[]); 77 78 private: 79 SkImage_GpuYUVA(const SkImage_GpuYUVA* image, sk_sp<SkColorSpace>); 80 81 // This array will usually only be sparsely populated. 82 // The actual non-null fields are dictated by the 'fYUVAIndices' indices 83 mutable sk_sp<GrTextureProxy> fProxies[4]; 84 int fNumProxies; 85 SkYUVAIndex fYUVAIndices[4]; 86 const SkYUVColorSpace fYUVColorSpace; 87 GrSurfaceOrigin fOrigin; 88 const sk_sp<SkColorSpace> fTargetColorSpace; 89 90 // Repeated calls to onMakeColorSpace will result in a proliferation of unique IDs and 91 // SkImage_GpuYUVA instances. Cache the result of the last successful onMakeColorSpace call. 92 mutable sk_sp<SkColorSpace> fOnMakeColorSpaceTarget; 93 mutable sk_sp<SkImage> fOnMakeColorSpaceResult; 94 95 // This is only allocated when the image needs to be flattened rather than 96 // using the separate YUVA planes. From thence forth we will only use the 97 // the RGBProxy. 98 mutable sk_sp<GrTextureProxy> fRGBProxy; 99 typedef SkImage_GpuBase INHERITED; 100 }; 101 102 #endif 103