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