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"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 28 | if (!this->onReadPixels(&fBitmap, 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); |
skia.committer@gmail.com | 96f5fa0 | 2013-12-16 07:01:40 +0000 | [diff] [blame] | 79 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 80 | GrTexture* dst = context->textureProvider()->createTexture(desc, false, nullptr, 0); |
| 81 | if (nullptr == dst) { |
| 82 | return nullptr; |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 83 | } |
| 84 | |
junov | 96c118e | 2014-09-26 13:09:47 -0700 | [diff] [blame] | 85 | // Blink is relying on the above copy being sent to GL immediately in the case when the source |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 86 | // is a WebGL canvas backing store. We could have a TODO to remove this flush flag, but we have |
| 87 | // a larger TODO to remove SkGrPixelRef entirely. |
| 88 | context->copySurface(dst->asRenderTarget(), texture, srcRect, SkIPoint::Make(0,0), |
| 89 | GrContext::kFlushWrites_PixelOp); |
mtklein | 7e6d9c0 | 2015-08-13 14:02:06 -0700 | [diff] [blame] | 90 | |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 91 | SkImageInfo info = SkImageInfo::Make(desc.fWidth, desc.fHeight, dstCT, kPremul_SkAlphaType, |
| 92 | dstPT); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 93 | SkGrPixelRef* pixelRef = new SkGrPixelRef(info, dst); |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 94 | SkSafeUnref(dst); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 95 | return pixelRef; |
| 96 | } |
| 97 | |
| 98 | /////////////////////////////////////////////////////////////////////////////// |
| 99 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 100 | SkGrPixelRef::SkGrPixelRef(const SkImageInfo& info, GrSurface* surface) : INHERITED(info) { |
bsalomon | 7187593 | 2014-08-29 08:08:35 -0700 | [diff] [blame] | 101 | // For surfaces that are both textures and render targets, the texture owns the |
| 102 | // render target but not vice versa. So we ref the texture to keep both alive for |
| 103 | // the lifetime of this pixel ref. |
| 104 | fSurface = SkSafeRef(surface->asTexture()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 105 | if (nullptr == fSurface) { |
bsalomon | 7187593 | 2014-08-29 08:08:35 -0700 | [diff] [blame] | 106 | fSurface = SkSafeRef(surface); |
reed@google.com | 6186787 | 2013-12-09 13:41:06 +0000 | [diff] [blame] | 107 | } |
commit-bot@chromium.org | 8e0993d | 2014-01-28 15:16:45 +0000 | [diff] [blame] | 108 | |
| 109 | if (fSurface) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 110 | SkASSERT(info.width() <= fSurface->width()); |
| 111 | SkASSERT(info.height() <= fSurface->height()); |
commit-bot@chromium.org | 8e0993d | 2014-01-28 15:16:45 +0000 | [diff] [blame] | 112 | } |
reed@google.com | 6186787 | 2013-12-09 13:41:06 +0000 | [diff] [blame] | 113 | } |
| 114 | |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 115 | SkGrPixelRef::~SkGrPixelRef() { |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 116 | SkSafeUnref(fSurface); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 117 | } |
| 118 | |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 119 | GrTexture* SkGrPixelRef::getTexture() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 120 | if (fSurface) { |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 121 | return fSurface->asTexture(); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 122 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 123 | return nullptr; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 124 | } |
| 125 | |
junov | da5469d | 2015-06-15 09:48:15 -0700 | [diff] [blame] | 126 | void SkGrPixelRef::onNotifyPixelsChanged() { |
| 127 | GrTexture* texture = this->getTexture(); |
| 128 | if (texture) { |
| 129 | texture->texturePriv().dirtyMipMaps(true); |
| 130 | } |
| 131 | } |
| 132 | |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 133 | SkPixelRef* SkGrPixelRef::deepCopy(SkColorType dstCT, SkColorProfileType dstPT, |
| 134 | const SkIRect* subset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 135 | if (nullptr == fSurface) { |
| 136 | return nullptr; |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 137 | } |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 138 | |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 139 | // Note that when copying a render-target-backed pixel ref, we |
| 140 | // return a texture-backed pixel ref instead. This is because |
| 141 | // render-target pixel refs are usually created in conjunction with |
| 142 | // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live |
| 143 | // independently of that texture. Texture-backed pixel refs, on the other |
| 144 | // hand, own their GrTextures, and are thus self-contained. |
jvanverth | fa1e8a7 | 2014-12-22 08:31:49 -0800 | [diff] [blame] | 145 | return copy_to_new_texture_pixelref(fSurface->asTexture(), dstCT, dstPT, subset); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 146 | } |
| 147 | |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 148 | static bool tryAllocBitmapPixels(SkBitmap* bitmap) { |
| 149 | SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 150 | if (nullptr != allocator) { |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 151 | return allocator->allocPixelRef(bitmap, 0); |
| 152 | } else { |
| 153 | // DiscardableMemory is not available, fallback to default allocator |
| 154 | return bitmap->tryAllocPixels(); |
| 155 | } |
| 156 | } |
| 157 | |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 158 | bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 159 | if (nullptr == fSurface || fSurface->wasDestroyed()) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 160 | return false; |
| 161 | } |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 162 | |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 163 | SkIRect bounds; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 164 | if (subset) { |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 165 | bounds = *subset; |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 166 | } else { |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 167 | bounds = SkIRect::MakeWH(this->info().width(), this->info().height()); |
robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 168 | } |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 169 | |
| 170 | //Check the cache |
| 171 | if(!SkBitmapCache::Find(this->getGenerationID(), bounds, dst)) { |
| 172 | //Cache miss |
| 173 | |
| 174 | SkBitmap cachedBitmap; |
| 175 | cachedBitmap.setInfo(this->info().makeWH(bounds.width(), bounds.height())); |
| 176 | |
| 177 | // If we can't alloc the pixels, then fail |
| 178 | if (!tryAllocBitmapPixels(&cachedBitmap)) { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | // Try to read the pixels from the surface |
| 183 | void* buffer = cachedBitmap.getPixels(); |
| 184 | bool readPixelsOk = fSurface->readPixels(bounds.fLeft, bounds.fTop, |
| 185 | bounds.width(), bounds.height(), |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 186 | kSkia8888_GrPixelConfig, |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 187 | buffer, cachedBitmap.rowBytes()); |
| 188 | |
| 189 | if (!readPixelsOk) { |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | // If we are here, pixels were read correctly from the surface. |
| 194 | cachedBitmap.setImmutable(); |
| 195 | //Add to the cache |
reed | 83787d0 | 2015-02-25 07:17:11 -0800 | [diff] [blame] | 196 | SkBitmapCache::Add(this, bounds, cachedBitmap); |
piotaixr | 0e97705 | 2014-09-17 16:24:04 -0700 | [diff] [blame] | 197 | |
| 198 | dst->swap(cachedBitmap); |
| 199 | } |
| 200 | |
| 201 | return true; |
| 202 | |
reed@google.com | 4281d65 | 2011-04-08 18:54:20 +0000 | [diff] [blame] | 203 | } |