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 | { |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 60 | fCachedCanvas = NULL; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 61 | fCachedImage = NULL; |
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 | { |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 67 | fCachedCanvas = NULL; |
| 68 | fCachedImage = NULL; |
| 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) { |
| 74 | fCachedCanvas->setSurfaceBase(NULL); |
| 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) { |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 82 | SkImage* image = this->newImageSnapshot(kYes_Budgeted); |
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 | image->unref(); |
| 86 | } |
| 87 | } |
| 88 | |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 89 | void SkSurface_Base::aboutToDraw(ContentChangeMode mode) { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 90 | this->dirtyGenerationID(); |
| 91 | |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 92 | SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 93 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 94 | if (fCachedImage) { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 95 | // the surface may need to fork its backend, if its sharing it with |
| 96 | // the cached image. Note: we only call if there is an outstanding owner |
| 97 | // on the image (besides us). |
bungeman@google.com | f64c684 | 2013-07-19 23:18:52 +0000 | [diff] [blame] | 98 | if (!fCachedImage->unique()) { |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 99 | this->onCopyOnWrite(mode); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // regardless of copy-on-write, we must drop our cached image now, so |
| 103 | // that the next request will get our new contents. |
| 104 | fCachedImage->unref(); |
| 105 | fCachedImage = NULL; |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 106 | } else if (kDiscard_ContentChangeMode == mode) { |
| 107 | this->onDiscard(); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | uint32_t SkSurface_Base::newGenerationID() { |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 112 | SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 113 | static int32_t gID; |
| 114 | return sk_atomic_inc(&gID) + 1; |
| 115 | } |
| 116 | |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 117 | static SkSurface_Base* asSB(SkSurface* surface) { |
| 118 | return static_cast<SkSurface_Base*>(surface); |
| 119 | } |
| 120 | |
| 121 | /////////////////////////////////////////////////////////////////////////////// |
| 122 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 123 | SkSurface::SkSurface(int width, int height, const SkSurfaceProps* props) |
| 124 | : fProps(SkSurfacePropsCopyOrDefault(props)), fWidth(width), fHeight(height) |
| 125 | { |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame] | 126 | SkASSERT(fWidth > 0); |
| 127 | SkASSERT(fHeight > 0); |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 128 | fGenerationID = 0; |
| 129 | } |
| 130 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 131 | SkSurface::SkSurface(const SkImageInfo& info, const SkSurfaceProps* props) |
| 132 | : fProps(SkSurfacePropsCopyOrDefault(props)), fWidth(info.width()), fHeight(info.height()) |
| 133 | { |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame] | 134 | SkASSERT(fWidth > 0); |
| 135 | SkASSERT(fHeight > 0); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 136 | fGenerationID = 0; |
| 137 | } |
| 138 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 139 | uint32_t SkSurface::generationID() { |
| 140 | if (0 == fGenerationID) { |
| 141 | fGenerationID = asSB(this)->newGenerationID(); |
| 142 | } |
| 143 | return fGenerationID; |
| 144 | } |
| 145 | |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 146 | void SkSurface::notifyContentWillChange(ContentChangeMode mode) { |
| 147 | asSB(this)->aboutToDraw(mode); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 148 | } |
| 149 | |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 150 | SkCanvas* SkSurface::getCanvas() { |
| 151 | return asSB(this)->getCachedCanvas(); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 152 | } |
| 153 | |
bsalomon | eaaaf0b | 2015-01-23 08:08:04 -0800 | [diff] [blame] | 154 | SkImage* SkSurface::newImageSnapshot(Budgeted budgeted) { |
| 155 | SkImage* image = asSB(this)->getCachedImage(budgeted); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 156 | SkSafeRef(image); // the caller will call unref() to balance this |
| 157 | return image; |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 158 | } |
| 159 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 160 | SkSurface* SkSurface::newSurface(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 | |
| 169 | const void* SkSurface::peekPixels(SkImageInfo* info, size_t* rowBytes) { |
| 170 | return this->getCanvas()->peekPixels(info, rowBytes); |
| 171 | } |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 172 | |
reed | 7543aa2 | 2014-12-09 14:39:44 -0800 | [diff] [blame] | 173 | bool SkSurface::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
| 174 | int srcX, int srcY) { |
| 175 | return this->getCanvas()->readPixels(dstInfo, dstPixels, dstRowBytes, srcX, srcY); |
| 176 | } |
| 177 | |
reed | fa5e68e | 2015-06-29 07:37:01 -0700 | [diff] [blame] | 178 | GrBackendObject SkSurface::getTextureHandle(TextureHandleAccess access) { |
| 179 | return asSB(this)->onGetTextureHandle(access); |
| 180 | } |
| 181 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 182 | ////////////////////////////////////////////////////////////////////////////////////// |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 183 | |
| 184 | #if !SK_SUPPORT_GPU |
| 185 | |
| 186 | SkSurface* SkSurface::NewRenderTargetDirect(GrRenderTarget*, const SkSurfaceProps*) { |
| 187 | return NULL; |
| 188 | } |
| 189 | |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 190 | SkSurface* SkSurface::NewRenderTarget(GrContext*, Budgeted, const SkImageInfo&, int, |
| 191 | const SkSurfaceProps*) { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 192 | return NULL; |
| 193 | } |
| 194 | |
bsalomon | d3e259a | 2015-06-30 12:04:40 -0700 | [diff] [blame^] | 195 | SkSurface* SkSurface::NewFromBackendTexture(GrContext*, const GrBackendTextureDesc&, |
bsalomon | e4579ad | 2015-04-08 08:38:40 -0700 | [diff] [blame] | 196 | const SkSurfaceProps*) { |
| 197 | return NULL; |
| 198 | } |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 199 | |
bsalomon | d3e259a | 2015-06-30 12:04:40 -0700 | [diff] [blame^] | 200 | SkSurface* SkSurface::NewFromBackendRenderTarget(GrContext*, const GrBackendRenderTargetDesc&, |
| 201 | const SkSurfaceProps*) { |
| 202 | return NULL; |
| 203 | } |
| 204 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 205 | #endif |