reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | |
mtklein | 59c12e3 | 2016-05-02 07:19:41 -0700 | [diff] [blame] | 8 | #include "SkAtomics.h" |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 9 | #include "SkSurface_Base.h" |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 10 | #include "SkImagePriv.h" |
| 11 | #include "SkCanvas.h" |
| 12 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 13 | #include "SkFontLCDConfig.h" |
| 14 | static SkPixelGeometry compute_default_geometry() { |
| 15 | SkFontLCDConfig::LCDOrder order = SkFontLCDConfig::GetSubpixelOrder(); |
| 16 | if (SkFontLCDConfig::kNONE_LCDOrder == order) { |
| 17 | return kUnknown_SkPixelGeometry; |
| 18 | } else { |
| 19 | // Bit0 is RGB(0), BGR(1) |
| 20 | // Bit1 is H(0), V(1) |
| 21 | const SkPixelGeometry gGeo[] = { |
| 22 | kRGB_H_SkPixelGeometry, |
| 23 | kBGR_H_SkPixelGeometry, |
| 24 | kRGB_V_SkPixelGeometry, |
| 25 | kBGR_V_SkPixelGeometry, |
| 26 | }; |
| 27 | int index = 0; |
| 28 | if (SkFontLCDConfig::kBGR_LCDOrder == order) { |
| 29 | index |= 1; |
| 30 | } |
| 31 | if (SkFontLCDConfig::kVertical_LCDOrientation == SkFontLCDConfig::GetSubpixelOrientation()){ |
| 32 | index |= 2; |
| 33 | } |
| 34 | return gGeo[index]; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | SkSurfaceProps::SkSurfaceProps() : fFlags(0), fPixelGeometry(kUnknown_SkPixelGeometry) {} |
| 39 | |
| 40 | SkSurfaceProps::SkSurfaceProps(InitType) : fFlags(0), fPixelGeometry(compute_default_geometry()) {} |
| 41 | |
| 42 | SkSurfaceProps::SkSurfaceProps(uint32_t flags, InitType) |
| 43 | : fFlags(flags) |
| 44 | , fPixelGeometry(compute_default_geometry()) |
| 45 | {} |
| 46 | |
| 47 | SkSurfaceProps::SkSurfaceProps(uint32_t flags, SkPixelGeometry pg) |
| 48 | : fFlags(flags), fPixelGeometry(pg) |
| 49 | {} |
| 50 | |
reed | 4af267b | 2014-11-21 08:46:37 -0800 | [diff] [blame] | 51 | SkSurfaceProps::SkSurfaceProps(const SkSurfaceProps& other) |
| 52 | : fFlags(other.fFlags) |
| 53 | , fPixelGeometry(other.fPixelGeometry) |
| 54 | {} |
| 55 | |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 56 | /////////////////////////////////////////////////////////////////////////////// |
| 57 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 58 | SkSurface_Base::SkSurface_Base(int width, int height, const SkSurfaceProps* props) |
Robert Phillips | a54ccb2 | 2017-01-31 07:40:33 -0500 | [diff] [blame] | 59 | : INHERITED(width, height, props) { |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 60 | } |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 61 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 62 | SkSurface_Base::SkSurface_Base(const SkImageInfo& info, const SkSurfaceProps* props) |
Robert Phillips | a54ccb2 | 2017-01-31 07:40:33 -0500 | [diff] [blame] | 63 | : INHERITED(info, props) { |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 64 | } |
| 65 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 66 | SkSurface_Base::~SkSurface_Base() { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 67 | // in case the canvas outsurvives us, we null the callback |
| 68 | if (fCachedCanvas) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 69 | fCachedCanvas->setSurfaceBase(nullptr); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 70 | } |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 71 | } |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 72 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 73 | void SkSurface_Base::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) { |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 74 | auto image = this->makeImageSnapshot(); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 75 | if (image) { |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 76 | canvas->drawImage(image, x, y, paint); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 80 | bool SkSurface_Base::outstandingImageSnapshot() const { |
| 81 | return fCachedImage && !fCachedImage->unique(); |
| 82 | } |
| 83 | |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 84 | void SkSurface_Base::aboutToDraw(ContentChangeMode mode) { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 85 | this->dirtyGenerationID(); |
| 86 | |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 87 | SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 88 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 89 | if (fCachedImage) { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 90 | // the surface may need to fork its backend, if its sharing it with |
| 91 | // the cached image. Note: we only call if there is an outstanding owner |
| 92 | // on the image (besides us). |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 93 | bool unique = fCachedImage->unique(); |
| 94 | if (!unique) { |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 95 | this->onCopyOnWrite(mode); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // regardless of copy-on-write, we must drop our cached image now, so |
| 99 | // that the next request will get our new contents. |
Robert Phillips | a54ccb2 | 2017-01-31 07:40:33 -0500 | [diff] [blame] | 100 | fCachedImage.reset(); |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 101 | |
| 102 | if (unique) { |
| 103 | // Our content isn't held by any image now, so we can consider that content mutable. |
| 104 | // Raster surfaces need to be told it's safe to consider its pixels mutable again. |
| 105 | // We make this call after the ->unref() so the subclass can assert there are no images. |
| 106 | this->onRestoreBackingMutability(); |
| 107 | } |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 108 | } else if (kDiscard_ContentChangeMode == mode) { |
| 109 | this->onDiscard(); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | uint32_t SkSurface_Base::newGenerationID() { |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 114 | SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 115 | static int32_t gID; |
| 116 | return sk_atomic_inc(&gID) + 1; |
| 117 | } |
| 118 | |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 119 | static SkSurface_Base* asSB(SkSurface* surface) { |
| 120 | return static_cast<SkSurface_Base*>(surface); |
| 121 | } |
| 122 | |
| 123 | /////////////////////////////////////////////////////////////////////////////// |
| 124 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 125 | SkSurface::SkSurface(int width, int height, const SkSurfaceProps* props) |
| 126 | : fProps(SkSurfacePropsCopyOrDefault(props)), fWidth(width), fHeight(height) |
| 127 | { |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame] | 128 | SkASSERT(fWidth > 0); |
| 129 | SkASSERT(fHeight > 0); |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 130 | fGenerationID = 0; |
| 131 | } |
| 132 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 133 | SkSurface::SkSurface(const SkImageInfo& info, const SkSurfaceProps* props) |
| 134 | : fProps(SkSurfacePropsCopyOrDefault(props)), fWidth(info.width()), fHeight(info.height()) |
| 135 | { |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame] | 136 | SkASSERT(fWidth > 0); |
| 137 | SkASSERT(fHeight > 0); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 138 | fGenerationID = 0; |
| 139 | } |
| 140 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 141 | uint32_t SkSurface::generationID() { |
| 142 | if (0 == fGenerationID) { |
| 143 | fGenerationID = asSB(this)->newGenerationID(); |
| 144 | } |
| 145 | return fGenerationID; |
| 146 | } |
| 147 | |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 148 | void SkSurface::notifyContentWillChange(ContentChangeMode mode) { |
| 149 | asSB(this)->aboutToDraw(mode); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 150 | } |
| 151 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 152 | SkCanvas* SkSurface::getCanvas() { |
| 153 | return asSB(this)->getCachedCanvas(); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Robert Phillips | ac6b1fa | 2017-03-20 08:38:50 -0400 | [diff] [blame] | 156 | sk_sp<SkImage> SkSurface::makeImageSnapshot() { |
| 157 | return asSB(this)->refCachedImage(); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 158 | } |
| 159 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 160 | sk_sp<SkSurface> SkSurface::makeSurface(const SkImageInfo& info) { |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 161 | return asSB(this)->onNewSurface(info); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void SkSurface::draw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 165 | const SkPaint* paint) { |
| 166 | return asSB(this)->onDraw(canvas, x, y, paint); |
| 167 | } |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 168 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 169 | bool SkSurface::peekPixels(SkPixmap* pmap) { |
| 170 | return this->getCanvas()->peekPixels(pmap); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 171 | } |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 172 | |
Mike Reed | 353196f | 2017-07-21 11:01:18 -0400 | [diff] [blame^] | 173 | bool SkSurface::readPixels(const SkPixmap& pm, int srcX, int srcY) { |
| 174 | return this->getCanvas()->readPixels(pm, srcX, srcY); |
| 175 | } |
| 176 | |
reed | 7543aa2 | 2014-12-09 14:39:44 -0800 | [diff] [blame] | 177 | bool SkSurface::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
| 178 | int srcX, int srcY) { |
Mike Reed | 353196f | 2017-07-21 11:01:18 -0400 | [diff] [blame^] | 179 | return this->readPixels({dstInfo, dstPixels, dstRowBytes}, srcX, srcY); |
| 180 | } |
| 181 | |
| 182 | bool SkSurface::readPixels(const SkBitmap& bitmap, int srcX, int srcY) { |
| 183 | SkPixmap pm; |
| 184 | return bitmap.peekPixels(&pm) && this->readPixels(pm, srcX, srcY); |
reed | 7543aa2 | 2014-12-09 14:39:44 -0800 | [diff] [blame] | 185 | } |
| 186 | |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 187 | GrBackendObject SkSurface::getTextureHandle(BackendHandleAccess access) { |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 188 | return asSB(this)->onGetTextureHandle(access); |
| 189 | } |
| 190 | |
joshualitt | 8179341 | 2015-07-08 12:54:04 -0700 | [diff] [blame] | 191 | bool SkSurface::getRenderTargetHandle(GrBackendObject* obj, BackendHandleAccess access) { |
| 192 | return asSB(this)->onGetRenderTargetHandle(obj, access); |
| 193 | } |
| 194 | |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 195 | void SkSurface::prepareForExternalIO() { |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 196 | this->flush(); |
| 197 | } |
| 198 | |
| 199 | void SkSurface::flush() { |
| 200 | asSB(this)->onFlush(0, nullptr); |
| 201 | } |
| 202 | |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 203 | bool SkSurface::flushAndSignalSemaphores(int numSemaphores, GrBackendSemaphore* signalSemaphores) { |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 204 | return asSB(this)->onFlush(numSemaphores, signalSemaphores); |
| 205 | } |
| 206 | |
Greg Daniel | c64ee46 | 2017-06-15 16:59:49 -0400 | [diff] [blame] | 207 | bool SkSurface::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) { |
| 208 | return asSB(this)->onWait(numSemaphores, waitSemaphores); |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 209 | } |
| 210 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 211 | ////////////////////////////////////////////////////////////////////////////////////// |
Mike Reed | 44d04bd | 2017-06-28 19:57:21 -0400 | [diff] [blame] | 212 | #include "SkNoDrawCanvas.h" |
| 213 | |
| 214 | class SkNullSurface : public SkSurface_Base { |
| 215 | public: |
| 216 | SkNullSurface(int width, int height) : SkSurface_Base(width, height, nullptr) {} |
| 217 | |
| 218 | protected: |
| 219 | SkCanvas* onNewCanvas() override { |
| 220 | return new SkNoDrawCanvas(this->width(), this->height()); |
| 221 | } |
| 222 | sk_sp<SkSurface> onNewSurface(const SkImageInfo& info) override { |
| 223 | return MakeNull(info.width(), info.height()); |
| 224 | } |
| 225 | sk_sp<SkImage> onNewImageSnapshot() override { return nullptr; } |
| 226 | void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) override {} |
| 227 | void onCopyOnWrite(ContentChangeMode) override {} |
| 228 | }; |
| 229 | |
| 230 | sk_sp<SkSurface> SkSurface::MakeNull(int width, int height) { |
| 231 | if (width < 1 || height < 1) { |
| 232 | return nullptr; |
| 233 | } |
| 234 | return sk_sp<SkSurface>(new SkNullSurface(width, height)); |
| 235 | } |
| 236 | |
| 237 | ////////////////////////////////////////////////////////////////////////////////////// |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 238 | |
| 239 | #if !SK_SUPPORT_GPU |
| 240 | |
robertphillips | 7e92276 | 2016-07-26 11:38:17 -0700 | [diff] [blame] | 241 | sk_sp<SkSurface> SkSurface::MakeRenderTarget(GrContext*, SkBudgeted, const SkImageInfo&, |
| 242 | int, GrSurfaceOrigin, const SkSurfaceProps*) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 243 | return nullptr; |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Greg Daniel | 9440345 | 2017-04-18 15:52:36 -0400 | [diff] [blame] | 246 | sk_sp<SkSurface> SkSurface::MakeFromBackendTexture(GrContext*, const GrBackendTexture&, |
| 247 | GrSurfaceOrigin origin, int sampleCnt, |
| 248 | sk_sp<SkColorSpace>, const SkSurfaceProps*) { |
| 249 | return nullptr; |
| 250 | } |
| 251 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 252 | sk_sp<SkSurface> SkSurface::MakeFromBackendRenderTarget(GrContext*, |
Brian Salomon | 807371c | 2017-07-20 20:48:12 +0000 | [diff] [blame] | 253 | const GrBackendRenderTargetDesc&, |
| 254 | sk_sp<SkColorSpace>, |
| 255 | const SkSurfaceProps*) { |
| 256 | return nullptr; |
| 257 | } |
| 258 | |
| 259 | sk_sp<SkSurface> SkSurface::MakeFromBackendRenderTarget(GrContext*, |
Greg Daniel | 9440345 | 2017-04-18 15:52:36 -0400 | [diff] [blame] | 260 | const GrBackendRenderTarget&, |
| 261 | GrSurfaceOrigin origin, |
| 262 | sk_sp<SkColorSpace>, |
| 263 | const SkSurfaceProps*) { |
| 264 | return nullptr; |
| 265 | } |
| 266 | |
| 267 | sk_sp<SkSurface> SkSurface::MakeFromBackendTextureAsRenderTarget(GrContext*, |
Greg Daniel | 9440345 | 2017-04-18 15:52:36 -0400 | [diff] [blame] | 268 | const GrBackendTexture&, |
| 269 | GrSurfaceOrigin origin, |
| 270 | int sampleCnt, |
| 271 | sk_sp<SkColorSpace>, |
| 272 | const SkSurfaceProps*) { |
ericrk | f7b8b8a | 2016-02-24 14:49:51 -0800 | [diff] [blame] | 273 | return nullptr; |
| 274 | } |
| 275 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 276 | #endif |