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" |
Brian Salomon | 87d42e5 | 2020-08-24 09:18:16 -0400 | [diff] [blame^] | 13 | #include "src/core/SkYUVAInfoPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrRecordingContextPriv.h" |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 16 | |
| 17 | namespace sk_gpu_test { |
| 18 | |
Brian Salomon | 87d42e5 | 2020-08-24 09:18:16 -0400 | [diff] [blame^] | 19 | YUVAPixmaps::YUVAPixmaps(const SkYUVAInfo& yuvaInfo, |
| 20 | SkColorType colorTypes[SkYUVAInfo::kMaxPlanes], |
| 21 | size_t rowBytes[SkYUVAInfo::kMaxPlanes]) |
| 22 | : fYUVAInfo(yuvaInfo) { |
| 23 | if (yuvaInfo.dimensions().isEmpty()) { |
| 24 | return; |
| 25 | } |
| 26 | SkISize planeDims[SkYUVAInfo::kMaxPlanes]; |
| 27 | SkImageInfo ii[SkYUVAInfo::kMaxPlanes]; |
| 28 | size_t planeSizes[SkYUVAInfo::kMaxPlanes]; |
| 29 | size_t totalBytes = yuvaInfo.computeTotalBytes(rowBytes, planeSizes); |
| 30 | int numPlanes = yuvaInfo.expectedPlaneDims(planeDims); |
| 31 | for (int i = 0; i < numPlanes; ++i) { |
| 32 | ii[i] = SkImageInfo::Make(planeDims[i], colorTypes[i], kPremul_SkAlphaType); |
| 33 | if (!ii[i].validRowBytes(rowBytes[i])) { |
| 34 | return; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | fStorage.reset(new char[totalBytes]); |
| 39 | char* addr = fStorage.get(); |
| 40 | for (int i = 0; i < numPlanes; ++i) { |
| 41 | fPlanes[i].reset(ii[i], addr, rowBytes[i]); |
| 42 | addr += planeSizes[i]; |
| 43 | } |
| 44 | fIsValid = true; |
| 45 | } |
| 46 | |
| 47 | bool YUVAPixmaps::toLegacy(SkYUVASizeInfo* yuvaSizeInfo, SkYUVAIndex yuvaIndices[4]) { |
| 48 | if (!this->isValid()) { |
| 49 | return false; |
| 50 | } |
| 51 | return SkYUVAInfoPriv::InitLegacyInfo(fYUVAInfo, fPlanes, yuvaSizeInfo, yuvaIndices); |
| 52 | } |
| 53 | |
Brian Salomon | 6db78b8 | 2020-07-31 08:57:48 -0400 | [diff] [blame] | 54 | std::unique_ptr<LazyYUVImage> LazyYUVImage::Make(sk_sp<SkData> data, GrMipmapped mipmapped) { |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 55 | std::unique_ptr<LazyYUVImage> image(new LazyYUVImage()); |
Brian Salomon | 6db78b8 | 2020-07-31 08:57:48 -0400 | [diff] [blame] | 56 | if (image->reset(std::move(data), mipmapped)) { |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 57 | return image; |
| 58 | } else { |
| 59 | return nullptr; |
| 60 | } |
| 61 | } |
| 62 | |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 63 | sk_sp<SkImage> LazyYUVImage::refImage(GrRecordingContext* rContext) { |
| 64 | if (this->ensureYUVImage(rContext)) { |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 65 | return fYUVImage; |
| 66 | } else { |
| 67 | return nullptr; |
| 68 | } |
| 69 | } |
| 70 | |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 71 | const SkImage* LazyYUVImage::getImage(GrRecordingContext* rContext) { |
| 72 | if (this->ensureYUVImage(rContext)) { |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 73 | return fYUVImage.get(); |
| 74 | } else { |
| 75 | return nullptr; |
| 76 | } |
| 77 | } |
| 78 | |
Brian Salomon | 6db78b8 | 2020-07-31 08:57:48 -0400 | [diff] [blame] | 79 | bool LazyYUVImage::reset(sk_sp<SkData> data, GrMipmapped mipmapped) { |
| 80 | fMipmapped = mipmapped; |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 81 | auto codec = SkCodecImageGenerator::MakeFromEncodedCodec(data); |
| 82 | if (!codec) { |
| 83 | return false; |
| 84 | } |
| 85 | |
Brian Salomon | 87d42e5 | 2020-08-24 09:18:16 -0400 | [diff] [blame^] | 86 | SkColorType colorTypes[4]; |
| 87 | size_t rowBytes[4]; |
| 88 | SkYUVAInfo yuvaInfo; |
| 89 | if (!codec->queryYUVAInfo(&yuvaInfo, colorTypes, rowBytes)) { |
| 90 | return false; |
| 91 | } |
| 92 | fPixmaps = YUVAPixmaps(yuvaInfo, colorTypes, rowBytes); |
| 93 | if (!fPixmaps.isValid()) { |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | |
Brian Salomon | 87d42e5 | 2020-08-24 09:18:16 -0400 | [diff] [blame^] | 97 | if (!codec->getYUVAPlanes(fPixmaps.planes())) { |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 98 | return false; |
| 99 | } |
| 100 | |
Brian Salomon | 87d42e5 | 2020-08-24 09:18:16 -0400 | [diff] [blame^] | 101 | if (!fPixmaps.toLegacy(&fSizeInfo, fComponents)) { |
| 102 | return false; |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 103 | } |
| 104 | // The SkPixmap data is fully configured now for MakeFromYUVAPixmaps once we get a GrContext |
| 105 | return true; |
| 106 | } |
| 107 | |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 108 | bool LazyYUVImage::ensureYUVImage(GrRecordingContext* rContext) { |
| 109 | if (!rContext) { |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 110 | return false; // Cannot make a YUV image from planes |
| 111 | } |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 112 | if (fYUVImage && fYUVImage->isValid(rContext)) { |
| 113 | return true; // Have already made a YUV image valid for this context. |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 114 | } |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 115 | // Try to make a new YUV image for this context. |
| 116 | fYUVImage = SkImage::MakeFromYUVAPixmaps(rContext->priv().backdoor(), |
Brian Salomon | 87d42e5 | 2020-08-24 09:18:16 -0400 | [diff] [blame^] | 117 | fPixmaps.yuvaInfo().yuvColorSpace(), |
| 118 | fPixmaps.planes(), |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 119 | fComponents, |
| 120 | fSizeInfo.fSizes[0], |
| 121 | kTopLeft_GrSurfaceOrigin, |
Brian Salomon | 6db78b8 | 2020-07-31 08:57:48 -0400 | [diff] [blame] | 122 | static_cast<bool>(fMipmapped), |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 123 | false); |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 124 | return fYUVImage != nullptr; |
| 125 | } |
| 126 | |
Robert Phillips | f105d38 | 2020-06-19 14:27:14 -0400 | [diff] [blame] | 127 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 128 | void YUVABackendReleaseContext::Unwind(GrDirectContext* dContext, |
| 129 | YUVABackendReleaseContext* beContext, |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 130 | bool fullFlush) { |
| 131 | |
Robert Phillips | f105d38 | 2020-06-19 14:27:14 -0400 | [diff] [blame] | 132 | // Some backends (e.g., Vulkan) require that all work associated w/ texture |
| 133 | // creation be completed before deleting the textures. |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 134 | if (fullFlush) { |
| 135 | // If the release context client performed some operations other than backend texture |
| 136 | // 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] | 137 | dContext->flush(); |
| 138 | dContext->submit(true); |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 139 | } else { |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 140 | dContext->submit(); |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 141 | |
| 142 | while (!beContext->creationCompleted()) { |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 143 | dContext->checkAsyncWorkCompletion(); |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 144 | } |
| 145 | } |
Robert Phillips | f105d38 | 2020-06-19 14:27:14 -0400 | [diff] [blame] | 146 | |
| 147 | delete beContext; |
| 148 | } |
| 149 | |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 150 | YUVABackendReleaseContext::YUVABackendReleaseContext(GrDirectContext* dContext) |
| 151 | : fDContext(dContext) { |
Robert Phillips | f105d38 | 2020-06-19 14:27:14 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | YUVABackendReleaseContext::~YUVABackendReleaseContext() { |
| 155 | for (int i = 0; i < 4; ++i) { |
| 156 | if (fBETextures[i].isValid()) { |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 157 | SkASSERT(fCreationComplete[i]); |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 158 | fDContext->deleteBackendTexture(fBETextures[i]); |
Robert Phillips | f105d38 | 2020-06-19 14:27:14 -0400 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 163 | template<int I> static void CreationComplete(void* releaseContext) { |
| 164 | auto beContext = reinterpret_cast<YUVABackendReleaseContext*>(releaseContext); |
| 165 | beContext->setCreationComplete(I); |
| 166 | } |
| 167 | |
| 168 | GrGpuFinishedProc YUVABackendReleaseContext::CreationCompleteProc(int index) { |
| 169 | SkASSERT(index >= 0 && index < 4); |
| 170 | |
| 171 | switch (index) { |
| 172 | case 0: return CreationComplete<0>; |
| 173 | case 1: return CreationComplete<1>; |
| 174 | case 2: return CreationComplete<2>; |
| 175 | case 3: return CreationComplete<3>; |
| 176 | } |
| 177 | |
| 178 | SK_ABORT("Invalid YUVA Index."); |
| 179 | return nullptr; |
| 180 | } |
| 181 | |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 182 | } // namespace sk_gpu_test |