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" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame^] | 13 | #include "src/gpu/GrDirectContextPriv.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 | |
Brian Salomon | be0e42c | 2020-08-27 11:00:04 -0400 | [diff] [blame] | 50 | SkYUVAPixmapInfo yuvaPixmapInfo; |
Brian Salomon | 59c60b0 | 2020-09-01 15:01:15 -0400 | [diff] [blame] | 51 | if (!codec->queryYUVAInfo(SkYUVAPixmapInfo::SupportedDataTypes::All(), &yuvaPixmapInfo)) { |
Brian Salomon | 87d42e5 | 2020-08-24 09:18:16 -0400 | [diff] [blame] | 52 | return false; |
| 53 | } |
Brian Salomon | be0e42c | 2020-08-27 11:00:04 -0400 | [diff] [blame] | 54 | fPixmaps = SkYUVAPixmaps::Allocate(yuvaPixmapInfo); |
Brian Salomon | 87d42e5 | 2020-08-24 09:18:16 -0400 | [diff] [blame] | 55 | if (!fPixmaps.isValid()) { |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 56 | return false; |
| 57 | } |
| 58 | |
Brian Salomon | be0e42c | 2020-08-27 11:00:04 -0400 | [diff] [blame] | 59 | if (!codec->getYUVAPlanes(fPixmaps)) { |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 60 | return false; |
| 61 | } |
| 62 | |
Brian Salomon | 87d42e5 | 2020-08-24 09:18:16 -0400 | [diff] [blame] | 63 | if (!fPixmaps.toLegacy(&fSizeInfo, fComponents)) { |
| 64 | return false; |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 65 | } |
| 66 | // The SkPixmap data is fully configured now for MakeFromYUVAPixmaps once we get a GrContext |
| 67 | return true; |
| 68 | } |
| 69 | |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 70 | bool LazyYUVImage::ensureYUVImage(GrRecordingContext* rContext) { |
| 71 | if (!rContext) { |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 72 | return false; // Cannot make a YUV image from planes |
| 73 | } |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 74 | if (fYUVImage && fYUVImage->isValid(rContext)) { |
| 75 | return true; // Have already made a YUV image valid for this context. |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 76 | } |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 77 | // Try to make a new YUV image for this context. |
Brian Salomon | c2a9a97 | 2020-09-15 11:24:28 -0400 | [diff] [blame] | 78 | fYUVImage = SkImage::MakeFromYUVAPixmaps(rContext, fPixmaps, fMipmapped, false, nullptr); |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 79 | return fYUVImage != nullptr; |
| 80 | } |
| 81 | |
Robert Phillips | f105d38 | 2020-06-19 14:27:14 -0400 | [diff] [blame] | 82 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 83 | void YUVABackendReleaseContext::Unwind(GrDirectContext* dContext, |
| 84 | YUVABackendReleaseContext* beContext, |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 85 | bool fullFlush) { |
| 86 | |
Robert Phillips | f105d38 | 2020-06-19 14:27:14 -0400 | [diff] [blame] | 87 | // Some backends (e.g., Vulkan) require that all work associated w/ texture |
| 88 | // creation be completed before deleting the textures. |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 89 | if (fullFlush) { |
| 90 | // If the release context client performed some operations other than backend texture |
| 91 | // 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] | 92 | dContext->flush(); |
| 93 | dContext->submit(true); |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 94 | } else { |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 95 | dContext->submit(); |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 96 | |
| 97 | while (!beContext->creationCompleted()) { |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 98 | dContext->checkAsyncWorkCompletion(); |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 99 | } |
| 100 | } |
Robert Phillips | f105d38 | 2020-06-19 14:27:14 -0400 | [diff] [blame] | 101 | |
| 102 | delete beContext; |
| 103 | } |
| 104 | |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 105 | YUVABackendReleaseContext::YUVABackendReleaseContext(GrDirectContext* dContext) |
| 106 | : fDContext(dContext) { |
Robert Phillips | f105d38 | 2020-06-19 14:27:14 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | YUVABackendReleaseContext::~YUVABackendReleaseContext() { |
| 110 | for (int i = 0; i < 4; ++i) { |
| 111 | if (fBETextures[i].isValid()) { |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 112 | SkASSERT(fCreationComplete[i]); |
Robert Phillips | 057c33f | 2020-07-17 11:59:01 -0400 | [diff] [blame] | 113 | fDContext->deleteBackendTexture(fBETextures[i]); |
Robert Phillips | f105d38 | 2020-06-19 14:27:14 -0400 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
Robert Phillips | 98c39ba | 2020-06-26 10:01:19 -0400 | [diff] [blame] | 118 | template<int I> static void CreationComplete(void* releaseContext) { |
| 119 | auto beContext = reinterpret_cast<YUVABackendReleaseContext*>(releaseContext); |
| 120 | beContext->setCreationComplete(I); |
| 121 | } |
| 122 | |
| 123 | GrGpuFinishedProc YUVABackendReleaseContext::CreationCompleteProc(int index) { |
| 124 | SkASSERT(index >= 0 && index < 4); |
| 125 | |
| 126 | switch (index) { |
| 127 | case 0: return CreationComplete<0>; |
| 128 | case 1: return CreationComplete<1>; |
| 129 | case 2: return CreationComplete<2>; |
| 130 | case 3: return CreationComplete<3>; |
| 131 | } |
| 132 | |
| 133 | SK_ABORT("Invalid YUVA Index."); |
| 134 | return nullptr; |
| 135 | } |
| 136 | |
Michael Ludwig | d9958f8 | 2019-03-21 13:08:36 -0400 | [diff] [blame] | 137 | } // namespace sk_gpu_test |