Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017 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 | |
| 8 | #ifndef GrDeferredProxyUploader_DEFINED |
| 9 | #define GrDeferredProxyUploader_DEFINED |
| 10 | |
| 11 | #include "SkAutoPixmapStorage.h" |
| 12 | #include "SkMakeUnique.h" |
| 13 | #include "SkRefCnt.h" |
| 14 | #include "SkSemaphore.h" |
| 15 | |
| 16 | #include "GrOpFlushState.h" |
| 17 | #include "GrTextureProxyPriv.h" |
| 18 | |
| 19 | /** |
| 20 | * GrDeferredProxyUploader assists with threaded generation of textures. Currently used by both |
| 21 | * software clip masks, and the software path renderer. The calling code typically needs to store |
| 22 | * some additional data (T) for use on the worker thread. GrTDeferredProxyUploader allows storing |
| 23 | * such data. The common flow is: |
| 24 | * |
| 25 | * 1) A GrTDeferredProxyUploader is created, with some payload (eg an SkPath to draw). |
| 26 | * The uploader is owned by the proxy that it's going to populate. |
| 27 | * 2) A task is created with a pointer to the uploader. A worker thread executes that task, using |
| 28 | * the payload data to allocate and fill in the fPixels pixmap. |
| 29 | * 3) The worker thread calls signalAndFreeData(), which notifies the main thread that the pixmap |
| 30 | * is ready, and then deletes the payload data (which is no longer needed). |
| 31 | * 4) In parallel to 2-3, on the main thread... Some op is created that refers to the proxy. When |
| 32 | * that op is added to an op list, the op list retains a pointer to the "deferred" proxies. |
| 33 | * 5) At flush time, the op list ensures that the deferred proxies are instantiated, then calls |
| 34 | * scheduleUpload on those proxies, which calls scheduleUpload on the uploader (below). |
| 35 | * 6) scheduleUpload defers the upload even further, by adding an ASAPUpload to the flush. |
| 36 | * 7) When the ASAP upload happens, we wait to make sure that the pixels are marked ready |
| 37 | * (from step #3 on the worker thread). Then we perform the actual upload to the texture. |
| 38 | * Finally, we call resetDeferredUploader, which deletes the uploader object, causing fPixels |
| 39 | * to be freed. |
| 40 | */ |
| 41 | class GrDeferredProxyUploader : public SkNoncopyable { |
| 42 | public: |
| 43 | GrDeferredProxyUploader() : fScheduledUpload(false), fWaited(false) {} |
| 44 | |
| 45 | virtual ~GrDeferredProxyUploader() { |
| 46 | // In normal usage (i.e., through GrTDeferredProxyUploader) this will be redundant |
| 47 | this->wait(); |
| 48 | } |
| 49 | |
| 50 | void scheduleUpload(GrOpFlushState* flushState, GrTextureProxy* proxy) { |
| 51 | if (fScheduledUpload) { |
| 52 | // Multiple references to the owning proxy may have caused us to already execute |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | auto uploadMask = [this, proxy](GrDrawOp::WritePixelsFn& writePixelsFn) { |
| 57 | this->wait(); |
| 58 | // If the worker thread was unable to allocate pixels, this check will fail, and we'll |
| 59 | // end up drawing with an uninitialized mask texture, but at least we won't crash. |
| 60 | if (this->fPixels.addr()) { |
| 61 | writePixelsFn(proxy, 0, 0, this->fPixels.width(), this->fPixels.height(), |
| 62 | proxy->config(), this->fPixels.addr(), this->fPixels.rowBytes()); |
| 63 | } |
| 64 | // Upload has finished, so tell the proxy to release this GrDeferredProxyUploader |
| 65 | proxy->texPriv().resetDeferredUploader(); |
| 66 | }; |
| 67 | flushState->addASAPUpload(std::move(uploadMask)); |
| 68 | fScheduledUpload = true; |
| 69 | } |
| 70 | |
| 71 | void signalAndFreeData() { |
| 72 | this->freeData(); |
| 73 | fPixelsReady.signal(); |
| 74 | } |
| 75 | |
| 76 | SkAutoPixmapStorage* getPixels() { return &fPixels; } |
| 77 | |
| 78 | protected: |
| 79 | void wait() { |
| 80 | if (!fWaited) { |
| 81 | fPixelsReady.wait(); |
| 82 | fWaited = true; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | virtual void freeData() {} |
| 88 | |
| 89 | SkAutoPixmapStorage fPixels; |
| 90 | SkSemaphore fPixelsReady; |
| 91 | bool fScheduledUpload; |
| 92 | bool fWaited; |
| 93 | }; |
| 94 | |
| 95 | template <typename T> |
| 96 | class GrTDeferredProxyUploader : public GrDeferredProxyUploader { |
| 97 | public: |
| 98 | template <typename... Args> |
| 99 | GrTDeferredProxyUploader(Args&&... args) |
| 100 | : fData(skstd::make_unique<T>(std::forward<Args>(args)...)) { |
| 101 | } |
| 102 | |
| 103 | ~GrTDeferredProxyUploader() override { |
| 104 | // We need to wait here, so that we don't free fData before the worker thread is done |
| 105 | // with it. (This happens if the proxy is deleted early due to a full clear or failure |
| 106 | // of an op list to instantiate). |
| 107 | this->wait(); |
| 108 | } |
| 109 | |
| 110 | T& data() { return *fData; } |
| 111 | |
| 112 | private: |
| 113 | void freeData() override { |
| 114 | fData.reset(); |
| 115 | } |
| 116 | |
| 117 | std::unique_ptr<T> fData; |
| 118 | }; |
| 119 | |
| 120 | #endif |