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