Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tools/gpu/YUVUtils.h" |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkData.h" |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/codec/SkCodecImageGenerator.h" |
| 13 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrRecordingContextPriv.h" |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 15 | |
| 16 | namespace sk_gpu_test { |
| 17 | |
Brian Salomon | 6db78b8 | 2020-07-31 08:57:48 -0400 | [diff] [blame^] | 18 | std::unique_ptr<LazyYUVImage> LazyYUVImage::Make(sk_sp<SkData> data, GrMipmapped mipmapped) { |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 19 | std::unique_ptr<LazyYUVImage> image(new LazyYUVImage()); |
Brian Salomon | 6db78b8 | 2020-07-31 08:57:48 -0400 | [diff] [blame^] | 20 | if (image->reset(std::move(data), mipmapped)) { |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 21 | return image; |
| 22 | } else { |
| 23 | return nullptr; |
| 24 | } |
| 25 | } |
| 26 | |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 27 | sk_sp<SkImage> LazyYUVImage::refImage(GrRecordingContext* rContext) { |
| 28 | if (this->ensureYUVImage(rContext)) { |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 29 | return fYUVImage; |
| 30 | } else { |
| 31 | return nullptr; |
| 32 | } |
| 33 | } |
| 34 | |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 35 | const SkImage* LazyYUVImage::getImage(GrRecordingContext* rContext) { |
| 36 | if (this->ensureYUVImage(rContext)) { |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 37 | return fYUVImage.get(); |
| 38 | } else { |
| 39 | return nullptr; |
| 40 | } |
| 41 | } |
| 42 | |
Brian Salomon | 6db78b8 | 2020-07-31 08:57:48 -0400 | [diff] [blame^] | 43 | bool LazyYUVImage::reset(sk_sp<SkData> data, GrMipmapped mipmapped) { |
| 44 | fMipmapped = mipmapped; |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 45 | auto codec = SkCodecImageGenerator::MakeFromEncodedCodec(data); |
| 46 | if (!codec) { |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | if (!codec->queryYUVA8(&fSizeInfo, fComponents, &fColorSpace)) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | fPlaneData.reset(fSizeInfo.computeTotalBytes()); |
| 55 | void* planes[SkYUVASizeInfo::kMaxCount]; |
| 56 | fSizeInfo.computePlanes(fPlaneData.get(), planes); |
| 57 | if (!codec->getYUVA8Planes(fSizeInfo, fComponents, planes)) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) { |
| 62 | if (fSizeInfo.fSizes[i].isEmpty()) { |
| 63 | fPlanes[i].reset(); |
| 64 | } else { |
| 65 | SkASSERT(planes[i]); |
| 66 | auto planeInfo = SkImageInfo::Make(fSizeInfo.fSizes[i].fWidth, |
| 67 | fSizeInfo.fSizes[i].fHeight, |
| 68 | kGray_8_SkColorType, kOpaque_SkAlphaType, nullptr); |
| 69 | fPlanes[i].reset(planeInfo, planes[i], fSizeInfo.fWidthBytes[i]); |
| 70 | } |
| 71 | } |
| 72 | // The SkPixmap data is fully configured now for MakeFromYUVAPixmaps once we get a GrContext |
| 73 | return true; |
| 74 | } |
| 75 | |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 76 | bool LazyYUVImage::ensureYUVImage(GrRecordingContext* rContext) { |
| 77 | if (!rContext) { |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 78 | return false; // Cannot make a YUV image from planes |
| 79 | } |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 80 | if (fYUVImage && fYUVImage->isValid(rContext)) { |
| 81 | return true; // Have already made a YUV image valid for this context. |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 82 | } |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 83 | // Try to make a new YUV image for this context. |
| 84 | fYUVImage = SkImage::MakeFromYUVAPixmaps(rContext->priv().backdoor(), |
| 85 | fColorSpace, |
| 86 | fPlanes, |
| 87 | fComponents, |
| 88 | fSizeInfo.fSizes[0], |
| 89 | kTopLeft_GrSurfaceOrigin, |
Brian Salomon | 6db78b8 | 2020-07-31 08:57:48 -0400 | [diff] [blame^] | 90 | static_cast<bool>(fMipmapped), |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 91 | false); |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 92 | return fYUVImage != nullptr; |
| 93 | } |
| 94 | |
Robert Phillips | f105d38 | 2020-06-19 14:27:14 -0400 | [diff] [blame] | 95 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 96 | void YUVABackendReleaseContext::Unwind(GrDirectContext* dContext, |
| 97 | YUVABackendReleaseContext* beContext, |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 98 | bool fullFlush) { |
| 99 | |
Robert Phillips | f105d38 | 2020-06-19 14:27:14 -0400 | [diff] [blame] | 100 | // Some backends (e.g., Vulkan) require that all work associated w/ texture |
| 101 | // creation be completed before deleting the textures. |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 102 | if (fullFlush) { |
| 103 | // If the release context client performed some operations other than backend texture |
| 104 | // creation then we may require a full flush to ensure that all the work is completed. |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 105 | dContext->flush(); |
| 106 | dContext->submit(true); |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 107 | } else { |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 108 | dContext->submit(); |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 109 | |
| 110 | while (!beContext->creationCompleted()) { |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 111 | dContext->checkAsyncWorkCompletion(); |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 112 | } |
| 113 | } |
Robert Phillips | f105d38 | 2020-06-19 14:27:14 -0400 | [diff] [blame] | 114 | |
| 115 | delete beContext; |
| 116 | } |
| 117 | |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 118 | YUVABackendReleaseContext::YUVABackendReleaseContext(GrDirectContext* dContext) |
| 119 | : fDContext(dContext) { |
Robert Phillips | f105d38 | 2020-06-19 14:27:14 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | YUVABackendReleaseContext::~YUVABackendReleaseContext() { |
| 123 | for (int i = 0; i < 4; ++i) { |
| 124 | if (fBETextures[i].isValid()) { |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 125 | SkASSERT(fCreationComplete[i]); |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 126 | fDContext->deleteBackendTexture(fBETextures[i]); |
Robert Phillips | f105d38 | 2020-06-19 14:27:14 -0400 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 131 | template<int I> static void CreationComplete(void* releaseContext) { |
| 132 | auto beContext = reinterpret_cast<YUVABackendReleaseContext*>(releaseContext); |
| 133 | beContext->setCreationComplete(I); |
| 134 | } |
| 135 | |
| 136 | GrGpuFinishedProc YUVABackendReleaseContext::CreationCompleteProc(int index) { |
| 137 | SkASSERT(index >= 0 && index < 4); |
| 138 | |
| 139 | switch (index) { |
| 140 | case 0: return CreationComplete<0>; |
| 141 | case 1: return CreationComplete<1>; |
| 142 | case 2: return CreationComplete<2>; |
| 143 | case 3: return CreationComplete<3>; |
| 144 | } |
| 145 | |
| 146 | SK_ABORT("Invalid YUVA Index."); |
| 147 | return nullptr; |
| 148 | } |
| 149 | |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 150 | } // namespace sk_gpu_test |