OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2013 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "SkCachingPixelRef.h" | |
9 #include "SkBitmapCache.h" | |
10 #include "SkRect.h" | |
11 | |
12 bool SkCachingPixelRef::Install(SkImageGenerator* generator, | |
13 SkBitmap* dst) { | |
14 SkASSERT(dst != nullptr); | |
15 if (nullptr == generator) { | |
16 return false; | |
17 } | |
18 const SkImageInfo info = generator->getInfo(); | |
19 if (!dst->setInfo(info)) { | |
20 delete generator; | |
21 return false; | |
22 } | |
23 SkAutoTUnref<SkCachingPixelRef> ref(new SkCachingPixelRef(info, generator, d
st->rowBytes())); | |
24 dst->setPixelRef(ref); | |
25 return true; | |
26 } | |
27 | |
28 SkCachingPixelRef::SkCachingPixelRef(const SkImageInfo& info, | |
29 SkImageGenerator* generator, | |
30 size_t rowBytes) | |
31 : INHERITED(info) | |
32 , fImageGenerator(generator) | |
33 , fErrorInDecoding(false) | |
34 , fRowBytes(rowBytes) { | |
35 SkASSERT(fImageGenerator != nullptr); | |
36 } | |
37 SkCachingPixelRef::~SkCachingPixelRef() { | |
38 delete fImageGenerator; | |
39 // Assert always unlock before unref. | |
40 } | |
41 | |
42 bool SkCachingPixelRef::onNewLockPixels(LockRec* rec) { | |
43 if (fErrorInDecoding) { | |
44 return false; // don't try again. | |
45 } | |
46 | |
47 const SkImageInfo& info = this->info(); | |
48 if (!SkBitmapCache::Find( | |
49 this->getGenerationID(), info.bounds(), &fLockedBitmap)) { | |
50 // Cache has been purged, must re-decode. | |
51 if (!fLockedBitmap.tryAllocPixels(info, fRowBytes)) { | |
52 fErrorInDecoding = true; | |
53 return false; | |
54 } | |
55 if (!fImageGenerator->getPixels(info, fLockedBitmap.getPixels(), fRowByt
es)) { | |
56 fErrorInDecoding = true; | |
57 return false; | |
58 } | |
59 fLockedBitmap.setImmutable(); | |
60 SkBitmapCache::Add(this, info.bounds(), fLockedBitmap); | |
61 } | |
62 | |
63 // Now bitmap should contain a concrete PixelRef of the decoded image. | |
64 void* pixels = fLockedBitmap.getPixels(); | |
65 SkASSERT(pixels != nullptr); | |
66 rec->fPixels = pixels; | |
67 rec->fColorTable = nullptr; | |
68 rec->fRowBytes = fLockedBitmap.rowBytes(); | |
69 return true; | |
70 } | |
71 | |
72 void SkCachingPixelRef::onUnlockPixels() { | |
73 fLockedBitmap.reset(); | |
74 } | |
OLD | NEW |