epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
robertphillips@google.com | 5088eb4 | 2012-06-28 20:59:13 +0000 | [diff] [blame] | 11 | #include "SkGrPixelRef.h" |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 12 | |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 13 | #include "GrContext.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | #include "GrTexture.h" |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 15 | #include "GrTexturePriv.h" |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 16 | #include "SkBitmapCache.h" |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 17 | #include "SkGr.h" |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 18 | #include "SkRect.h" |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 19 | |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 20 | SkROLockPixelsPixelRef::SkROLockPixelsPixelRef(const SkImageInfo& info) |
mtklein | 7e6d9c0 | 2015-08-13 14:02:06 -0700 | [diff] [blame] | 21 | : INHERITED(info) {} |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 22 | |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 23 | SkROLockPixelsPixelRef::~SkROLockPixelsPixelRef() {} |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 24 | |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 25 | bool SkROLockPixelsPixelRef::onNewLockPixels(LockRec* rec) { |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 26 | fBitmap.reset(); |
| 27 | // SkDebugf("---------- calling readpixels in support of lockpixels\n"); |
bsalomon | 9d22fd6 | 2016-01-11 11:14:17 -0800 | [diff] [blame] | 28 | if (!this->onReadPixels(&fBitmap, this->info().colorType(), nullptr)) { |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 29 | SkDebugf("SkROLockPixelsPixelRef::onLockPixels failed!\n"); |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 30 | return false; |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 31 | } |
| 32 | fBitmap.lockPixels(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 33 | if (nullptr == fBitmap.getPixels()) { |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 34 | return false; |
| 35 | } |
| 36 | |
| 37 | rec->fPixels = fBitmap.getPixels(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 38 | rec->fColorTable = nullptr; |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 39 | rec->fRowBytes = fBitmap.rowBytes(); |
| 40 | return true; |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void SkROLockPixelsPixelRef::onUnlockPixels() { |
| 44 | fBitmap.unlockPixels(); |
| 45 | } |
| 46 | |
| 47 | bool SkROLockPixelsPixelRef::onLockPixelsAreWritable() const { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 52 | |
junov | 96c118e | 2014-09-26 13:09:47 -0700 | [diff] [blame] | 53 | static SkGrPixelRef* copy_to_new_texture_pixelref(GrTexture* texture, SkColorType dstCT, |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 54 | SkColorProfileType dstPT, const SkIRect* subset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 55 | if (nullptr == texture || kUnknown_SkColorType == dstCT) { |
| 56 | return nullptr; |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 57 | } |
| 58 | GrContext* context = texture->getContext(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 59 | if (nullptr == context) { |
| 60 | return nullptr; |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 61 | } |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 62 | GrSurfaceDesc desc; |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 63 | |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 64 | SkIRect srcRect; |
| 65 | |
| 66 | if (!subset) { |
| 67 | desc.fWidth = texture->width(); |
| 68 | desc.fHeight = texture->height(); |
| 69 | srcRect = SkIRect::MakeWH(texture->width(), texture->height()); |
| 70 | } else { |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 71 | SkASSERT(SkIRect::MakeWH(texture->width(), texture->height()).contains(*subset)); |
| 72 | // Create a new texture that is the size of subset. |
| 73 | desc.fWidth = subset->width(); |
| 74 | desc.fHeight = subset->height(); |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 75 | srcRect = *subset; |
scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 76 | } |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 77 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 78 | desc.fConfig = SkImageInfo2GrPixelConfig(dstCT, kPremul_SkAlphaType, dstPT); |
erikchen | 9a1ed5d | 2016-02-10 16:32:34 -0800 | [diff] [blame] | 79 | desc.fTextureStorageAllocator = texture->desc().fTextureStorageAllocator; |
skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 80 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 81 | GrTexture* dst = context->textureProvider()->createTexture(desc, false, nullptr, 0); |
| 82 | if (nullptr == dst) { |
| 83 | return nullptr; |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 84 | } |
| 85 | |
junov | 96c118e | 2014-09-26 13:09:47 -0700 | [diff] [blame] | 86 | // Blink is relying on the above copy being sent to GL immediately in the case when the source |
kjlubick | 0eed945 | 2016-02-11 12:05:24 -0800 | [diff] [blame] | 87 | // is a WebGL canvas backing store. We could have a TODO to remove this flush flag, but we have |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 88 | // a larger TODO to remove SkGrPixelRef entirely. |
kjlubick | 0eed945 | 2016-02-11 12:05:24 -0800 | [diff] [blame] | 89 | context->copySurface(dst->asRenderTarget(), texture, srcRect, SkIPoint::Make(0,0), |
| 90 | GrContext::kFlushWrites_PixelOp); |
mtklein | 7e6d9c0 | 2015-08-13 14:02:06 -0700 | [diff] [blame] | 91 | |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 92 | SkImageInfo info = SkImageInfo::Make(desc.fWidth, desc.fHeight, dstCT, kPremul_SkAlphaType, |
| 93 | dstPT); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 94 | SkGrPixelRef* pixelRef = new SkGrPixelRef(info, dst); |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 95 | SkSafeUnref(dst); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 96 | return pixelRef; |
| 97 | } |
| 98 | |
| 99 | /////////////////////////////////////////////////////////////////////////////// |
| 100 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 101 | SkGrPixelRef::SkGrPixelRef(const SkImageInfo& info, GrSurface* surface) : INHERITED(info) { |
bsalomon | 7187593 | 2014-08-29 08:08:35 -0700 | [diff] [blame] | 102 | // For surfaces that are both textures and render targets, the texture owns the |
| 103 | // render target but not vice versa. So we ref the texture to keep both alive for |
| 104 | // the lifetime of this pixel ref. |
| 105 | fSurface = SkSafeRef(surface->asTexture()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 106 | if (nullptr == fSurface) { |
bsalomon | 7187593 | 2014-08-29 08:08:35 -0700 | [diff] [blame] | 107 | fSurface = SkSafeRef(surface); |
reed@google.com | 6186787 | 2013-12-09 13:41:06 +0000 | [diff] [blame] | 108 | } |
commit-bot@chromium.org | 8e0993d | 2014-01-28 15:16:45 +0000 | [diff] [blame] | 109 | |
| 110 | if (fSurface) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 111 | SkASSERT(info.width() <= fSurface->width()); |
| 112 | SkASSERT(info.height() <= fSurface->height()); |
commit-bot@chromium.org | 8e0993d | 2014-01-28 15:16:45 +0000 | [diff] [blame] | 113 | } |
reed@google.com | 6186787 | 2013-12-09 13:41:06 +0000 | [diff] [blame] | 114 | } |
| 115 | |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 116 | SkGrPixelRef::~SkGrPixelRef() { |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 117 | SkSafeUnref(fSurface); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 118 | } |
| 119 | |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 120 | GrTexture* SkGrPixelRef::getTexture() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 121 | if (fSurface) { |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 122 | return fSurface->asTexture(); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 123 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 124 | return nullptr; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 125 | } |
| 126 | |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 127 | void SkGrPixelRef::onNotifyPixelsChanged() { |
| 128 | GrTexture* texture = this->getTexture(); |
| 129 | if (texture) { |
| 130 | texture->texturePriv().dirtyMipMaps(true); |
| 131 | } |
| 132 | } |
| 133 | |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 134 | SkPixelRef* SkGrPixelRef::deepCopy(SkColorType dstCT, SkColorProfileType dstPT, |
| 135 | const SkIRect* subset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 136 | if (nullptr == fSurface) { |
| 137 | return nullptr; |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 138 | } |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 139 | |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 140 | // Note that when copying a render-target-backed pixel ref, we |
| 141 | // return a texture-backed pixel ref instead. This is because |
| 142 | // render-target pixel refs are usually created in conjunction with |
| 143 | // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live |
| 144 | // independently of that texture. Texture-backed pixel refs, on the other |
| 145 | // hand, own their GrTextures, and are thus self-contained. |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 146 | return copy_to_new_texture_pixelref(fSurface->asTexture(), dstCT, dstPT, subset); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 147 | } |
| 148 | |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 149 | static bool tryAllocBitmapPixels(SkBitmap* bitmap) { |
| 150 | SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 151 | if (nullptr != allocator) { |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 152 | return allocator->allocPixelRef(bitmap, 0); |
| 153 | } else { |
| 154 | // DiscardableMemory is not available, fallback to default allocator |
| 155 | return bitmap->tryAllocPixels(); |
| 156 | } |
| 157 | } |
| 158 | |
bsalomon | 9d22fd6 | 2016-01-11 11:14:17 -0800 | [diff] [blame] | 159 | bool SkGrPixelRef::onReadPixels(SkBitmap* dst, SkColorType colorType, const SkIRect* subset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 160 | if (nullptr == fSurface || fSurface->wasDestroyed()) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 161 | return false; |
| 162 | } |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 163 | |
bsalomon | 9d22fd6 | 2016-01-11 11:14:17 -0800 | [diff] [blame] | 164 | GrPixelConfig config; |
| 165 | if (kRGBA_8888_SkColorType == colorType) { |
| 166 | config = kRGBA_8888_GrPixelConfig; |
| 167 | } else if (kBGRA_8888_SkColorType == colorType) { |
| 168 | config = kBGRA_8888_GrPixelConfig; |
| 169 | } else { |
| 170 | return false; |
| 171 | } |
| 172 | |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 173 | SkIRect bounds; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 174 | if (subset) { |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 175 | bounds = *subset; |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 176 | } else { |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 177 | bounds = SkIRect::MakeWH(this->info().width(), this->info().height()); |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 178 | } |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 179 | |
| 180 | //Check the cache |
| 181 | if(!SkBitmapCache::Find(this->getGenerationID(), bounds, dst)) { |
| 182 | //Cache miss |
| 183 | |
| 184 | SkBitmap cachedBitmap; |
bsalomon | 9d22fd6 | 2016-01-11 11:14:17 -0800 | [diff] [blame] | 185 | cachedBitmap.setInfo(SkImageInfo::Make(bounds.width(), bounds.height(), colorType, |
| 186 | this->info().alphaType(), |
| 187 | this->info().profileType())); |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 188 | |
| 189 | // If we can't alloc the pixels, then fail |
| 190 | if (!tryAllocBitmapPixels(&cachedBitmap)) { |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | // Try to read the pixels from the surface |
| 195 | void* buffer = cachedBitmap.getPixels(); |
| 196 | bool readPixelsOk = fSurface->readPixels(bounds.fLeft, bounds.fTop, |
| 197 | bounds.width(), bounds.height(), |
bsalomon | 9d22fd6 | 2016-01-11 11:14:17 -0800 | [diff] [blame] | 198 | config, buffer, cachedBitmap.rowBytes()); |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 199 | |
| 200 | if (!readPixelsOk) { |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | // If we are here, pixels were read correctly from the surface. |
| 205 | cachedBitmap.setImmutable(); |
| 206 | //Add to the cache |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 207 | SkBitmapCache::Add(this, bounds, cachedBitmap); |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 208 | |
| 209 | dst->swap(cachedBitmap); |
| 210 | } |
| 211 | |
| 212 | return true; |
| 213 | |
reed@google.com | 4281d65 | 2011-04-08 18:54:20 +0000 | [diff] [blame] | 214 | } |