SkImage_GpuYUVA.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #include <cstddef>
  8. #include <cstring>
  9. #include <type_traits>
  10. #include "include/core/SkYUVASizeInfo.h"
  11. #include "include/gpu/GrContext.h"
  12. #include "include/gpu/GrTexture.h"
  13. #include "include/private/GrRecordingContext.h"
  14. #include "src/core/SkAutoPixmapStorage.h"
  15. #include "src/core/SkMipMap.h"
  16. #include "src/core/SkScopeExit.h"
  17. #include "src/gpu/GrClip.h"
  18. #include "src/gpu/GrContextPriv.h"
  19. #include "src/gpu/GrGpu.h"
  20. #include "src/gpu/GrRecordingContextPriv.h"
  21. #include "src/gpu/GrRenderTargetContext.h"
  22. #include "src/gpu/GrTextureProducer.h"
  23. #include "src/gpu/SkGr.h"
  24. #include "src/gpu/effects/GrYUVtoRGBEffect.h"
  25. #include "src/image/SkImage_Gpu.h"
  26. #include "src/image/SkImage_GpuYUVA.h"
  27. static constexpr auto kAssumedColorType = kRGBA_8888_SkColorType;
  28. SkImage_GpuYUVA::SkImage_GpuYUVA(sk_sp<GrContext> context, int width, int height, uint32_t uniqueID,
  29. SkYUVColorSpace colorSpace, sk_sp<GrTextureProxy> proxies[],
  30. int numProxies, const SkYUVAIndex yuvaIndices[4],
  31. GrSurfaceOrigin origin, sk_sp<SkColorSpace> imageColorSpace)
  32. : INHERITED(std::move(context), width, height, uniqueID, kAssumedColorType,
  33. // If an alpha channel is present we always switch to kPremul. This is because,
  34. // although the planar data is always un-premul, the final interleaved RGB image
  35. // is/would-be premul.
  36. GetAlphaTypeFromYUVAIndices(yuvaIndices), std::move(imageColorSpace))
  37. , fNumProxies(numProxies)
  38. , fYUVColorSpace(colorSpace)
  39. , fOrigin(origin) {
  40. // The caller should have done this work, just verifying
  41. SkDEBUGCODE(int textureCount;)
  42. SkASSERT(SkYUVAIndex::AreValidIndices(yuvaIndices, &textureCount));
  43. SkASSERT(textureCount == fNumProxies);
  44. for (int i = 0; i < numProxies; ++i) {
  45. fProxies[i] = std::move(proxies[i]);
  46. }
  47. memcpy(fYUVAIndices, yuvaIndices, 4*sizeof(SkYUVAIndex));
  48. }
  49. // For onMakeColorSpace()
  50. SkImage_GpuYUVA::SkImage_GpuYUVA(const SkImage_GpuYUVA* image, sk_sp<SkColorSpace> targetCS)
  51. : INHERITED(image->fContext, image->width(), image->height(), kNeedNewImageUniqueID,
  52. kAssumedColorType,
  53. // If an alpha channel is present we always switch to kPremul. This is because,
  54. // although the planar data is always un-premul, the final interleaved RGB image
  55. // is/would-be premul.
  56. GetAlphaTypeFromYUVAIndices(image->fYUVAIndices), std::move(targetCS))
  57. , fNumProxies(image->fNumProxies)
  58. , fYUVColorSpace(image->fYUVColorSpace)
  59. , fOrigin(image->fOrigin)
  60. // Since null fFromColorSpace means no GrColorSpaceXform, we turn a null
  61. // image->refColorSpace() into an explicit SRGB.
  62. , fFromColorSpace(image->colorSpace() ? image->refColorSpace() : SkColorSpace::MakeSRGB()) {
  63. // The caller should have done this work, just verifying
  64. SkDEBUGCODE(int textureCount;)
  65. SkASSERT(SkYUVAIndex::AreValidIndices(image->fYUVAIndices, &textureCount));
  66. SkASSERT(textureCount == fNumProxies);
  67. if (image->fRGBProxy) {
  68. fRGBProxy = image->fRGBProxy; // we ref in this case, not move
  69. } else {
  70. for (int i = 0; i < fNumProxies; ++i) {
  71. fProxies[i] = image->fProxies[i]; // we ref in this case, not move
  72. }
  73. }
  74. memcpy(fYUVAIndices, image->fYUVAIndices, 4 * sizeof(SkYUVAIndex));
  75. }
  76. SkImage_GpuYUVA::~SkImage_GpuYUVA() {}
  77. bool SkImage_GpuYUVA::setupMipmapsForPlanes(GrRecordingContext* context) const {
  78. // We shouldn't get here if the planes were already flattened to RGBA.
  79. SkASSERT(fProxies[0] && !fRGBProxy);
  80. if (!context || !fContext->priv().matches(context)) {
  81. return false;
  82. }
  83. for (int i = 0; i < fNumProxies; ++i) {
  84. GrTextureProducer::CopyParams copyParams;
  85. int mipCount = SkMipMap::ComputeLevelCount(fProxies[i]->width(), fProxies[i]->height());
  86. if (mipCount && GrGpu::IsACopyNeededForMips(fContext->priv().caps(),
  87. fProxies[i].get(),
  88. GrSamplerState::Filter::kMipMap,
  89. &copyParams)) {
  90. auto mippedProxy = GrCopyBaseMipMapToTextureProxy(context, fProxies[i].get());
  91. if (!mippedProxy) {
  92. return false;
  93. }
  94. fProxies[i] = mippedProxy;
  95. }
  96. }
  97. return true;
  98. }
  99. //////////////////////////////////////////////////////////////////////////////////////////////////
  100. GrSemaphoresSubmitted SkImage_GpuYUVA::onFlush(GrContext* context, const GrFlushInfo& info) {
  101. if (!context || !fContext->priv().matches(context) || fContext->abandoned()) {
  102. return GrSemaphoresSubmitted::kNo;
  103. }
  104. GrSurfaceProxy* proxies[4] = {fProxies[0].get(), fProxies[1].get(),
  105. fProxies[2].get(), fProxies[3].get()};
  106. int numProxies = fNumProxies;
  107. if (fRGBProxy) {
  108. // Either we've already flushed the flattening draw or the flattening is unflushed. In the
  109. // latter case it should still be ok to just pass fRGBProxy because it in turn depends on
  110. // the planar proxies and will cause all of their work to flush as well.
  111. proxies[0] = fRGBProxy.get();
  112. numProxies = 1;
  113. }
  114. return context->priv().flushSurfaces(proxies, numProxies, info);
  115. }
  116. GrTextureProxy* SkImage_GpuYUVA::peekProxy() const {
  117. return fRGBProxy.get();
  118. }
  119. sk_sp<GrTextureProxy> SkImage_GpuYUVA::asTextureProxyRef(GrRecordingContext* context) const {
  120. if (fRGBProxy) {
  121. return fRGBProxy;
  122. }
  123. if (!context || !fContext->priv().matches(context)) {
  124. return nullptr;
  125. }
  126. // Needs to create a render target in order to draw to it for the yuv->rgb conversion.
  127. sk_sp<GrRenderTargetContext> renderTargetContext(
  128. context->priv().makeDeferredRenderTargetContext(
  129. SkBackingFit::kExact, this->width(), this->height(), GrColorType::kRGBA_8888,
  130. this->refColorSpace(), 1, GrMipMapped::kNo, fOrigin));
  131. if (!renderTargetContext) {
  132. return nullptr;
  133. }
  134. sk_sp<GrColorSpaceXform> colorSpaceXform;
  135. if (fFromColorSpace) {
  136. colorSpaceXform = GrColorSpaceXform::Make(fFromColorSpace.get(), this->alphaType(),
  137. this->colorSpace(), this->alphaType());
  138. }
  139. const SkRect rect = SkRect::MakeIWH(this->width(), this->height());
  140. if (!RenderYUVAToRGBA(fContext.get(), renderTargetContext.get(), rect, fYUVColorSpace,
  141. std::move(colorSpaceXform), fProxies, fYUVAIndices)) {
  142. return nullptr;
  143. }
  144. fRGBProxy = renderTargetContext->asTextureProxyRef();
  145. for (auto& p : fProxies) {
  146. p.reset();
  147. }
  148. return fRGBProxy;
  149. }
  150. sk_sp<GrTextureProxy> SkImage_GpuYUVA::asMippedTextureProxyRef(GrRecordingContext* context) const {
  151. if (!context || !fContext->priv().matches(context)) {
  152. return nullptr;
  153. }
  154. // if invalid or already has miplevels
  155. auto proxy = this->asTextureProxyRef(context);
  156. if (!proxy || GrMipMapped::kYes == fRGBProxy->mipMapped()) {
  157. return proxy;
  158. }
  159. // need to generate mips for the proxy
  160. if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(context, proxy.get())) {
  161. fRGBProxy = mippedProxy;
  162. return mippedProxy;
  163. }
  164. // failed to generate mips
  165. return nullptr;
  166. }
  167. //////////////////////////////////////////////////////////////////////////////////////////////////
  168. sk_sp<SkImage> SkImage_GpuYUVA::onMakeColorTypeAndColorSpace(GrRecordingContext*,
  169. SkColorType,
  170. sk_sp<SkColorSpace> targetCS) const {
  171. // We explicitly ignore color type changes, for now.
  172. // we may need a mutex here but for now we expect usage to be in a single thread
  173. if (fOnMakeColorSpaceTarget &&
  174. SkColorSpace::Equals(targetCS.get(), fOnMakeColorSpaceTarget.get())) {
  175. return fOnMakeColorSpaceResult;
  176. }
  177. sk_sp<SkImage> result = sk_sp<SkImage>(new SkImage_GpuYUVA(this, targetCS));
  178. if (result) {
  179. fOnMakeColorSpaceTarget = targetCS;
  180. fOnMakeColorSpaceResult = result;
  181. }
  182. return result;
  183. }
  184. //////////////////////////////////////////////////////////////////////////////////////////////////
  185. sk_sp<SkImage> SkImage::MakeFromYUVATextures(GrContext* ctx,
  186. SkYUVColorSpace colorSpace,
  187. const GrBackendTexture yuvaTextures[],
  188. const SkYUVAIndex yuvaIndices[4],
  189. SkISize imageSize,
  190. GrSurfaceOrigin imageOrigin,
  191. sk_sp<SkColorSpace> imageColorSpace) {
  192. int numTextures;
  193. if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures)) {
  194. return nullptr;
  195. }
  196. sk_sp<GrTextureProxy> tempTextureProxies[4];
  197. if (!SkImage_GpuBase::MakeTempTextureProxies(ctx, yuvaTextures, numTextures, yuvaIndices,
  198. imageOrigin, tempTextureProxies)) {
  199. return nullptr;
  200. }
  201. return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(ctx), imageSize.width(), imageSize.height(),
  202. kNeedNewImageUniqueID, colorSpace, tempTextureProxies,
  203. numTextures, yuvaIndices, imageOrigin, imageColorSpace);
  204. }
  205. sk_sp<SkImage> SkImage::MakeFromYUVAPixmaps(
  206. GrContext* context, SkYUVColorSpace yuvColorSpace, const SkPixmap yuvaPixmaps[],
  207. const SkYUVAIndex yuvaIndices[4], SkISize imageSize, GrSurfaceOrigin imageOrigin,
  208. bool buildMips, bool limitToMaxTextureSize, sk_sp<SkColorSpace> imageColorSpace) {
  209. if (!context) {
  210. return nullptr; // until we impl this for raster backend
  211. }
  212. int numPixmaps;
  213. if (!SkYUVAIndex::AreValidIndices(yuvaIndices, &numPixmaps)) {
  214. return nullptr;
  215. }
  216. if (!context->priv().caps()->mipMapSupport()) {
  217. buildMips = false;
  218. }
  219. // Make proxies
  220. GrProxyProvider* proxyProvider = context->priv().proxyProvider();
  221. sk_sp<GrTextureProxy> tempTextureProxies[4];
  222. for (int i = 0; i < numPixmaps; ++i) {
  223. const SkPixmap* pixmap = &yuvaPixmaps[i];
  224. SkAutoPixmapStorage resized;
  225. int maxTextureSize = context->priv().caps()->maxTextureSize();
  226. int maxDim = SkTMax(yuvaPixmaps[i].width(), yuvaPixmaps[i].height());
  227. if (limitToMaxTextureSize && maxDim > maxTextureSize) {
  228. float scale = static_cast<float>(maxTextureSize) / maxDim;
  229. int newWidth = SkTMin(static_cast<int>(yuvaPixmaps[i].width() * scale),
  230. maxTextureSize);
  231. int newHeight = SkTMin(static_cast<int>(yuvaPixmaps[i].height() * scale),
  232. maxTextureSize);
  233. SkImageInfo info = yuvaPixmaps[i].info().makeWH(newWidth, newHeight);
  234. if (!resized.tryAlloc(info) ||
  235. !yuvaPixmaps[i].scalePixels(resized, kLow_SkFilterQuality)) {
  236. return nullptr;
  237. }
  238. pixmap = &resized;
  239. }
  240. // Turn the pixmap into a GrTextureProxy
  241. SkBitmap bmp;
  242. bmp.installPixels(*pixmap);
  243. GrMipMapped mipMapped = buildMips ? GrMipMapped::kYes : GrMipMapped::kNo;
  244. tempTextureProxies[i] = proxyProvider->createProxyFromBitmap(bmp, mipMapped);
  245. if (!tempTextureProxies[i]) {
  246. return nullptr;
  247. }
  248. }
  249. return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(context), imageSize.width(), imageSize.height(),
  250. kNeedNewImageUniqueID, yuvColorSpace, tempTextureProxies,
  251. numPixmaps, yuvaIndices, imageOrigin, imageColorSpace);
  252. }
  253. /////////////////////////////////////////////////////////////////////////////////////////////////
  254. sk_sp<SkImage> SkImage_GpuYUVA::MakePromiseYUVATexture(
  255. GrContext* context,
  256. SkYUVColorSpace yuvColorSpace,
  257. const GrBackendFormat yuvaFormats[],
  258. const SkISize yuvaSizes[],
  259. const SkYUVAIndex yuvaIndices[4],
  260. int imageWidth,
  261. int imageHeight,
  262. GrSurfaceOrigin imageOrigin,
  263. sk_sp<SkColorSpace> imageColorSpace,
  264. PromiseImageTextureFulfillProc textureFulfillProc,
  265. PromiseImageTextureReleaseProc textureReleaseProc,
  266. PromiseImageTextureDoneProc promiseDoneProc,
  267. PromiseImageTextureContext textureContexts[],
  268. PromiseImageApiVersion version) {
  269. int numTextures;
  270. bool valid = SkYUVAIndex::AreValidIndices(yuvaIndices, &numTextures);
  271. // The contract here is that if 'promiseDoneProc' is passed in it should always be called,
  272. // even if creation of the SkImage fails. Once we call MakePromiseImageLazyProxy it takes
  273. // responsibility for calling the done proc.
  274. if (!promiseDoneProc) {
  275. return nullptr;
  276. }
  277. int proxiesCreated = 0;
  278. SkScopeExit callDone([promiseDoneProc, textureContexts, numTextures, &proxiesCreated]() {
  279. for (int i = proxiesCreated; i < numTextures; ++i) {
  280. promiseDoneProc(textureContexts[i]);
  281. }
  282. });
  283. if (!valid) {
  284. return nullptr;
  285. }
  286. if (!context) {
  287. return nullptr;
  288. }
  289. if (imageWidth <= 0 || imageHeight <= 0) {
  290. return nullptr;
  291. }
  292. SkAlphaType at = (-1 != yuvaIndices[SkYUVAIndex::kA_Index].fIndex) ? kPremul_SkAlphaType
  293. : kOpaque_SkAlphaType;
  294. SkImageInfo info = SkImageInfo::Make(imageWidth, imageHeight, kAssumedColorType,
  295. at, imageColorSpace);
  296. if (!SkImageInfoIsValid(info)) {
  297. return nullptr;
  298. }
  299. // verify sizes with expected texture count
  300. for (int i = 0; i < numTextures; ++i) {
  301. if (yuvaSizes[i].isEmpty()) {
  302. return nullptr;
  303. }
  304. }
  305. for (int i = numTextures; i < SkYUVASizeInfo::kMaxCount; ++i) {
  306. if (!yuvaSizes[i].isEmpty()) {
  307. return nullptr;
  308. }
  309. }
  310. // Get lazy proxies
  311. sk_sp<GrTextureProxy> proxies[4];
  312. for (int texIdx = 0; texIdx < numTextures; ++texIdx) {
  313. GrColorType colorType = context->priv().caps()->getYUVAColorTypeFromBackendFormat(
  314. yuvaFormats[texIdx]);
  315. if (GrColorType::kUnknown == colorType) {
  316. return nullptr;
  317. }
  318. proxies[texIdx] = MakePromiseImageLazyProxy(
  319. context, yuvaSizes[texIdx].width(), yuvaSizes[texIdx].height(), imageOrigin,
  320. colorType, yuvaFormats[texIdx], GrMipMapped::kNo, textureFulfillProc,
  321. textureReleaseProc, promiseDoneProc, textureContexts[texIdx], version);
  322. ++proxiesCreated;
  323. if (!proxies[texIdx]) {
  324. return nullptr;
  325. }
  326. }
  327. return sk_make_sp<SkImage_GpuYUVA>(sk_ref_sp(context), imageWidth, imageHeight,
  328. kNeedNewImageUniqueID, yuvColorSpace, proxies, numTextures,
  329. yuvaIndices, imageOrigin, std::move(imageColorSpace));
  330. }