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 (fSurface) { | 138 if (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) { |
| 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 (subset) { | 174 if (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; |
| 185 cachedBitmap.setInfo(this->info().makeWH(bounds.width(), bounds.height()
)); |
| 186 |
| 187 // If we can't alloc the pixels, then fail |
| 188 if (!tryAllocBitmapPixels(&cachedBitmap)) { |
| 189 return false; |
| 190 } |
| 191 |
| 192 // Try to read the pixels from the surface |
| 193 void* buffer = cachedBitmap.getPixels(); |
| 194 bool readPixelsOk = fSurface->readPixels(bounds.fLeft, bounds.fTop, |
| 195 bounds.width(), bounds.height(), |
| 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 return true; |
179 return fSurface->readPixels(left, top, width, height, | 212 |
180 kSkia8888_GrPixelConfig, | |
181 buffer, dst->rowBytes()); | |
182 } | 213 } |
OLD | NEW |