robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 8 | #include "src/core/SkSpecialSurface.h" |
| 9 | |
| 10 | #include <memory> |
| 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkCanvas.h" |
| 13 | #include "src/core/SkSpecialImage.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/core/SkSurfacePriv.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 15 | |
| 16 | /////////////////////////////////////////////////////////////////////////////// |
| 17 | class SkSpecialSurface_Base : public SkSpecialSurface { |
| 18 | public: |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 19 | SkSpecialSurface_Base(const SkIRect& subset, const SkSurfaceProps* props) |
| 20 | : INHERITED(subset, props) |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 21 | , fCanvas(nullptr) { |
| 22 | } |
| 23 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 24 | // reset is called after an SkSpecialImage has been snapped |
| 25 | void reset() { fCanvas.reset(); } |
| 26 | |
| 27 | // This can return nullptr if reset has already been called or something when wrong in the ctor |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 28 | SkCanvas* onGetCanvas() { return fCanvas.get(); } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 29 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 30 | virtual sk_sp<SkSpecialImage> onMakeImageSnapshot() = 0; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 31 | |
| 32 | protected: |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 33 | std::unique_ptr<SkCanvas> fCanvas; // initialized by derived classes in ctors |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 34 | |
| 35 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 36 | using INHERITED = SkSpecialSurface; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | static SkSpecialSurface_Base* as_SB(SkSpecialSurface* surface) { |
| 41 | return static_cast<SkSpecialSurface_Base*>(surface); |
| 42 | } |
| 43 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 44 | SkSpecialSurface::SkSpecialSurface(const SkIRect& subset, |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 45 | const SkSurfaceProps* props) |
robertphillips | 0b7e5a1 | 2016-04-13 07:18:41 -0700 | [diff] [blame] | 46 | : fProps(SkSurfacePropsCopyOrDefault(props).flags(), kUnknown_SkPixelGeometry) |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 47 | , fSubset(subset) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 48 | SkASSERT(fSubset.width() > 0); |
| 49 | SkASSERT(fSubset.height() > 0); |
| 50 | } |
| 51 | |
| 52 | SkCanvas* SkSpecialSurface::getCanvas() { |
| 53 | return as_SB(this)->onGetCanvas(); |
| 54 | } |
| 55 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 56 | sk_sp<SkSpecialImage> SkSpecialSurface::makeImageSnapshot() { |
| 57 | sk_sp<SkSpecialImage> image(as_SB(this)->onMakeImageSnapshot()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 58 | as_SB(this)->reset(); |
robertphillips | 16aad78 | 2016-02-04 14:59:58 -0800 | [diff] [blame] | 59 | return image; // the caller gets the creation ref |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /////////////////////////////////////////////////////////////////////////////// |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 63 | #include "include/core/SkMallocPixelRef.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 64 | |
| 65 | class SkSpecialSurface_Raster : public SkSpecialSurface_Base { |
| 66 | public: |
Matt Sarett | a6e976a | 2017-04-28 15:43:35 -0400 | [diff] [blame] | 67 | SkSpecialSurface_Raster(const SkImageInfo& info, |
| 68 | sk_sp<SkPixelRef> pr, |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 69 | const SkIRect& subset, |
| 70 | const SkSurfaceProps* props) |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 71 | : INHERITED(subset, props) { |
Matt Sarett | a6e976a | 2017-04-28 15:43:35 -0400 | [diff] [blame] | 72 | SkASSERT(info.width() == pr->width() && info.height() == pr->height()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 73 | fBitmap.setInfo(info, info.minRowBytes()); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 74 | fBitmap.setPixelRef(std::move(pr), 0, 0); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 75 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 76 | fCanvas = std::make_unique<SkCanvas>(fBitmap, this->props()); |
robertphillips | 4c13db2 | 2016-04-19 14:23:42 -0700 | [diff] [blame] | 77 | fCanvas->clipRect(SkRect::Make(subset)); |
robertphillips | 3806b8f | 2016-05-12 10:46:39 -0700 | [diff] [blame] | 78 | #ifdef SK_IS_BOT |
| 79 | fCanvas->clear(SK_ColorRED); // catch any imageFilter sloppiness |
| 80 | #endif |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | ~SkSpecialSurface_Raster() override { } |
| 84 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 85 | sk_sp<SkSpecialImage> onMakeImageSnapshot() override { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 86 | return SkSpecialImage::MakeFromRaster(this->subset(), fBitmap, &this->props()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | private: |
| 90 | SkBitmap fBitmap; |
| 91 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 92 | using INHERITED = SkSpecialSurface_Base; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 93 | }; |
| 94 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 95 | sk_sp<SkSpecialSurface> SkSpecialSurface::MakeFromBitmap(const SkIRect& subset, SkBitmap& bm, |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 96 | const SkSurfaceProps* props) { |
Florin Malita | f40b249 | 2017-05-03 15:23:21 -0400 | [diff] [blame] | 97 | if (subset.isEmpty() || !SkSurfaceValidateRasterInfo(bm.info(), bm.rowBytes())) { |
| 98 | return nullptr; |
| 99 | } |
Matt Sarett | a6e976a | 2017-04-28 15:43:35 -0400 | [diff] [blame] | 100 | return sk_make_sp<SkSpecialSurface_Raster>(bm.info(), sk_ref_sp(bm.pixelRef()), subset, props); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 101 | } |
| 102 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 103 | sk_sp<SkSpecialSurface> SkSpecialSurface::MakeRaster(const SkImageInfo& info, |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 104 | const SkSurfaceProps* props) { |
Florin Malita | f40b249 | 2017-05-03 15:23:21 -0400 | [diff] [blame] | 105 | if (!SkSurfaceValidateRasterInfo(info)) { |
| 106 | return nullptr; |
| 107 | } |
| 108 | |
Mike Klein | 930c295 | 2019-04-12 11:50:39 -0500 | [diff] [blame] | 109 | sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, 0); |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 110 | if (!pr) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 111 | return nullptr; |
| 112 | } |
| 113 | |
Matt Sarett | a6e976a | 2017-04-28 15:43:35 -0400 | [diff] [blame] | 114 | const SkIRect subset = SkIRect::MakeWH(info.width(), info.height()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 115 | |
Matt Sarett | a6e976a | 2017-04-28 15:43:35 -0400 | [diff] [blame] | 116 | return sk_make_sp<SkSpecialSurface_Raster>(info, std::move(pr), subset, props); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | #if SK_SUPPORT_GPU |
| 120 | /////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 121 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 122 | #include "src/gpu/GrRecordingContextPriv.h" |
| 123 | #include "src/gpu/SkGpuDevice.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 124 | |
| 125 | class SkSpecialSurface_Gpu : public SkSpecialSurface_Base { |
| 126 | public: |
Robert Phillips | bc42944 | 2019-02-20 08:26:03 -0500 | [diff] [blame] | 127 | SkSpecialSurface_Gpu(GrRecordingContext* context, |
Brian Salomon | 1aa1f5f | 2020-12-11 17:25:17 -0500 | [diff] [blame] | 128 | std::unique_ptr<GrSurfaceDrawContext> surfaceDrawContext, |
Robert Phillips | 9fab7e9 | 2016-11-17 12:45:04 -0500 | [diff] [blame] | 129 | int width, int height, const SkIRect& subset) |
Brian Salomon | 1aa1f5f | 2020-12-11 17:25:17 -0500 | [diff] [blame] | 130 | : INHERITED(subset, &surfaceDrawContext->surfaceProps()) |
| 131 | , fReadView(surfaceDrawContext->readSurfaceView()) { |
| 132 | auto device = SkGpuDevice::Make(context, std::move(surfaceDrawContext), |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 133 | SkGpuDevice::kUninit_InitContents); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 134 | if (!device) { |
| 135 | return; |
| 136 | } |
| 137 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 138 | fCanvas = std::make_unique<SkCanvas>(std::move(device)); |
robertphillips | 4c13db2 | 2016-04-19 14:23:42 -0700 | [diff] [blame] | 139 | fCanvas->clipRect(SkRect::Make(subset)); |
robertphillips | 3806b8f | 2016-05-12 10:46:39 -0700 | [diff] [blame] | 140 | #ifdef SK_IS_BOT |
| 141 | fCanvas->clear(SK_ColorRED); // catch any imageFilter sloppiness |
| 142 | #endif |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 143 | } |
| 144 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 145 | sk_sp<SkSpecialImage> onMakeImageSnapshot() override { |
Greg Daniel | c52db71 | 2020-01-28 17:03:46 -0500 | [diff] [blame] | 146 | if (!fReadView.asTextureProxy()) { |
Robert Phillips | 833dcf4 | 2016-11-18 08:44:13 -0500 | [diff] [blame] | 147 | return nullptr; |
| 148 | } |
Robert Phillips | 40b05c3 | 2019-09-20 12:40:55 -0400 | [diff] [blame] | 149 | GrColorType ct = SkColorTypeToGrColorType(fCanvas->imageInfo().colorType()); |
| 150 | |
Greg Daniel | c52db71 | 2020-01-28 17:03:46 -0500 | [diff] [blame] | 151 | // Note: SkSpecialImages can only be snapShotted once, so this call is destructive and we |
| 152 | // move fReadMove. |
Robert Phillips | 16bf7d3 | 2020-07-07 10:20:27 -0400 | [diff] [blame] | 153 | return SkSpecialImage::MakeDeferredFromGpu(fCanvas->recordingContext(), |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 154 | this->subset(), |
| 155 | kNeedNewImageUniqueID_SpecialImage, |
Greg Daniel | c52db71 | 2020-01-28 17:03:46 -0500 | [diff] [blame] | 156 | std::move(fReadView), ct, |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 157 | fCanvas->imageInfo().refColorSpace(), |
| 158 | &this->props()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | private: |
Greg Daniel | c52db71 | 2020-01-28 17:03:46 -0500 | [diff] [blame] | 162 | GrSurfaceProxyView fReadView; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 163 | using INHERITED = SkSpecialSurface_Base; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 164 | }; |
| 165 | |
Robert Phillips | bc42944 | 2019-02-20 08:26:03 -0500 | [diff] [blame] | 166 | sk_sp<SkSpecialSurface> SkSpecialSurface::MakeRenderTarget(GrRecordingContext* context, |
robertphillips | 4df1656 | 2016-04-28 15:09:34 -0700 | [diff] [blame] | 167 | int width, int height, |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 168 | GrColorType colorType, |
Robert Phillips | e0e8393 | 2018-09-20 14:18:13 -0400 | [diff] [blame] | 169 | sk_sp<SkColorSpace> colorSpace, |
| 170 | const SkSurfaceProps* props) { |
robertphillips | 4df1656 | 2016-04-28 15:09:34 -0700 | [diff] [blame] | 171 | if (!context) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 172 | return nullptr; |
| 173 | } |
Brian Salomon | 1aa1f5f | 2020-12-11 17:25:17 -0500 | [diff] [blame] | 174 | auto surfaceDrawContext = GrSurfaceDrawContext::Make( |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 175 | context, colorType, std::move(colorSpace), SkBackingFit::kApprox, {width, height}, 1, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 176 | GrMipmapped::kNo, GrProtected::kNo, kBottomLeft_GrSurfaceOrigin, SkBudgeted::kYes, |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 177 | props); |
Brian Salomon | 1aa1f5f | 2020-12-11 17:25:17 -0500 | [diff] [blame] | 178 | if (!surfaceDrawContext) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 179 | return nullptr; |
| 180 | } |
| 181 | |
robertphillips | 4df1656 | 2016-04-28 15:09:34 -0700 | [diff] [blame] | 182 | const SkIRect subset = SkIRect::MakeWH(width, height); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 183 | |
Brian Salomon | 1aa1f5f | 2020-12-11 17:25:17 -0500 | [diff] [blame] | 184 | return sk_make_sp<SkSpecialSurface_Gpu>(context, std::move(surfaceDrawContext), |
Robert Phillips | 9fab7e9 | 2016-11-17 12:45:04 -0500 | [diff] [blame] | 185 | width, height, subset); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | #endif |