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