Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2010 Google Inc. | 3 * Copyright 2010 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 | 10 |
| 11 #include "SkGrPixelRef.h" | 11 #include "SkGrPixelRef.h" |
| 12 | |
| 12 #include "GrContext.h" | 13 #include "GrContext.h" |
| 13 #include "GrTexture.h" | 14 #include "GrTexture.h" |
| 15 #include "SkBitmapCache.h" | |
| 14 #include "SkGr.h" | 16 #include "SkGr.h" |
| 15 #include "SkRect.h" | 17 #include "SkRect.h" |
| 16 | 18 |
| 17 // since we call lockPixels recursively on fBitmap, we need a distinct mutex, | 19 // since we call lockPixels recursively on fBitmap, we need a distinct mutex, |
| 18 // to avoid deadlock with the default one provided by SkPixelRef. | 20 // to avoid deadlock with the default one provided by SkPixelRef. |
| 19 SK_DECLARE_STATIC_MUTEX(gROLockPixelsPixelRefMutex); | 21 SK_DECLARE_STATIC_MUTEX(gROLockPixelsPixelRefMutex); |
| 20 | 22 |
| 21 SkROLockPixelsPixelRef::SkROLockPixelsPixelRef(const SkImageInfo& info) | 23 SkROLockPixelsPixelRef::SkROLockPixelsPixelRef(const SkImageInfo& info) |
| 22 : INHERITED(info, &gROLockPixelsPixelRefMutex) {} | 24 : INHERITED(info, &gROLockPixelsPixelRefMutex) {} |
| 23 | 25 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 136 if (NULL != fSurface) { | 138 if (NULL != fSurface) { |
| 137 return fSurface->asTexture(); | 139 return fSurface->asTexture(); |
| 138 } | 140 } |
| 139 return NULL; | 141 return NULL; |
| 140 } | 142 } |
| 141 | 143 |
| 142 SkPixelRef* SkGrPixelRef::deepCopy(SkColorType dstCT, const SkIRect* subset) { | 144 SkPixelRef* SkGrPixelRef::deepCopy(SkColorType dstCT, const SkIRect* subset) { |
| 143 if (NULL == fSurface) { | 145 if (NULL == fSurface) { |
| 144 return NULL; | 146 return NULL; |
| 145 } | 147 } |
| 146 | 148 |
| 147 // Note that when copying a render-target-backed pixel ref, we | 149 // Note that when copying a render-target-backed pixel ref, we |
| 148 // return a texture-backed pixel ref instead. This is because | 150 // return a texture-backed pixel ref instead. This is because |
| 149 // render-target pixel refs are usually created in conjunction with | 151 // render-target pixel refs are usually created in conjunction with |
| 150 // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live | 152 // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live |
| 151 // independently of that texture. Texture-backed pixel refs, on the other | 153 // independently of that texture. Texture-backed pixel refs, on the other |
| 152 // hand, own their GrTextures, and are thus self-contained. | 154 // hand, own their GrTextures, and are thus self-contained. |
| 153 return copyToTexturePixelRef(fSurface->asTexture(), dstCT, subset); | 155 return copyToTexturePixelRef(fSurface->asTexture(), dstCT, subset); |
| 154 } | 156 } |
| 155 | 157 |
| 158 static bool tryAllocBitmapPixels(SkBitmap& bitmap) { | |
|
reed1
2014/09/04 18:49:59
style: skia passes mutable args by pointer, not re
Rémi Piotaix
2014/09/05 21:37:36
Done.
| |
| 159 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); | |
| 160 if (NULL != allocator) { | |
| 161 return allocator->allocPixelRef(&bitmap, 0); | |
| 162 } else { | |
| 163 // DiscardableMemory is not available, fallback to default allocator | |
| 164 return bitmap.tryAllocPixels(); | |
| 165 } | |
| 166 } | |
| 167 | |
| 156 bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { | 168 bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { |
| 157 if (NULL == fSurface || fSurface->wasDestroyed()) { | 169 if (NULL == fSurface || fSurface->wasDestroyed()) { |
| 158 return false; | 170 return false; |
| 159 } | 171 } |
| 160 | 172 |
| 161 int left, top, width, height; | 173 SkIRect bounds; |
| 162 if (NULL != subset) { | 174 if (NULL != subset) { |
| 163 left = subset->fLeft; | 175 bounds = *subset; |
| 164 width = subset->width(); | |
| 165 top = subset->fTop; | |
| 166 height = subset->height(); | |
| 167 } else { | 176 } else { |
| 168 left = 0; | 177 bounds = SkIRect::MakeWH(this->info().width(), this->info().height()); |
| 169 width = this->info().width(); | |
| 170 top = 0; | |
| 171 height = this->info().height(); | |
| 172 } | 178 } |
| 173 if (!dst->tryAllocN32Pixels(width, height)) { | 179 |
| 174 SkDebugf("SkGrPixelRef::onReadPixels failed to alloc bitmap for result!\ n"); | 180 //Check the cache |
| 175 return false; | 181 if(!SkBitmapCache::Find(this->getGenerationID(), bounds, dst)) { |
| 182 //Cache miss | |
| 183 | |
| 184 SkBitmap cachedBitmap; | |
|
reed1
2014/09/04 18:49:59
nit: could use
this->info().makeWH(bounds.width()
Rémi Piotaix
2014/09/05 21:37:36
Done.
| |
| 185 cachedBitmap.setInfo(SkImageInfo::Make(bounds.width(), bounds.height(), | |
| 186 this->info().colorType(), | |
| 187 this->info().alphaType())); | |
| 188 // If we can't alloc the pixels, then fail | |
| 189 if (!tryAllocBitmapPixels(cachedBitmap)) | |
|
reed1
2014/09/04 18:49:59
style: Skia *always* use { } with "if" blocks.
Rémi Piotaix
2014/09/05 21:37:36
Done.
| |
| 190 return false; | |
| 191 | |
| 192 { // Try to read the pixels from the surface | |
| 193 SkAutoLockPixels alp(cachedBitmap); | |
|
reed1
2014/09/04 18:49:59
allocPixels always returns a locked bitmap, so thi
Rémi Piotaix
2014/09/05 21:37:36
Done.
| |
| 194 void* buffer = cachedBitmap.getPixels(); | |
| 195 bool readPixelsOk = fSurface->readPixels(bounds.fLeft, bounds.fTop, bounds.width(), bounds.width(), | |
| 196 kSkia8888_GrPixelConfig, | |
| 197 buffer, cachedBitmap.rowBytes()); | |
| 198 | |
| 199 if (!readPixelsOk) | |
| 200 return false; | |
| 201 } | |
| 202 | |
| 203 // If we are here, pixels were read correctly from the surface. | |
| 204 cachedBitmap.setImmutable(); | |
| 205 //Add to the cache | |
| 206 SkBitmapCache::Add(this->getGenerationID(), bounds, cachedBitmap); | |
| 207 | |
| 208 dst->swap(cachedBitmap); | |
| 176 } | 209 } |
| 177 SkAutoLockPixels al(*dst); | 210 |
| 178 void* buffer = dst->getPixels(); | 211 // Unlock the pixels (lock comes from Find or allocPixelRef) |
|
reed1
2014/09/04 18:49:59
I see that the old code did this, but I think we d
Rémi Piotaix
2014/09/05 21:37:36
Done.
| |
| 179 return fSurface->readPixels(left, top, width, height, | 212 dst->unlockPixels(); |
| 180 kSkia8888_GrPixelConfig, | 213 |
| 181 buffer, dst->rowBytes()); | 214 // The pixels of the bitmap should not be locked |
| 215 SkASSERT(NULL == dst->getPixels()); | |
| 216 | |
| 217 return true; | |
| 218 | |
| 182 } | 219 } |
| OLD | NEW |