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