OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkBitmapCache.h" | 9 #include "SkBitmapCache.h" |
10 #include "SkImage_Base.h" | 10 #include "SkImage_Base.h" |
11 #include "SkImageCacherator.h" | 11 #include "SkImageCacherator.h" |
12 #include "SkMallocPixelRef.h" | 12 #include "SkMallocPixelRef.h" |
13 #include "SkMutex.h" | |
14 #include "SkNextID.h" | 13 #include "SkNextID.h" |
15 #include "SkPixelRef.h" | 14 #include "SkPixelRef.h" |
16 #include "SkResourceCache.h" | 15 #include "SkResourceCache.h" |
17 | 16 |
18 #if SK_SUPPORT_GPU | 17 #if SK_SUPPORT_GPU |
19 #include "GrContext.h" | 18 #include "GrContext.h" |
20 #include "GrGpuResourcePriv.h" | 19 #include "GrGpuResourcePriv.h" |
21 #include "GrImageIDTextureAdjuster.h" | 20 #include "GrImageIDTextureAdjuster.h" |
22 #include "GrResourceKey.h" | 21 #include "GrResourceKey.h" |
23 #include "GrTextureParams.h" | 22 #include "GrTextureParams.h" |
24 #include "GrYUVProvider.h" | 23 #include "GrYUVProvider.h" |
25 #include "SkGr.h" | 24 #include "SkGr.h" |
26 #include "SkGrPriv.h" | 25 #include "SkGrPriv.h" |
27 #endif | 26 #endif |
28 | 27 |
29 // Until we actually have codecs/etc. that can contain/support a GPU texture for
mat | 28 // Until we actually have codecs/etc. that can contain/support a GPU texture for
mat |
30 // skip this step, since for some generators, returning their encoded data as a
SkData | 29 // skip this step, since for some generators, returning their encoded data as a
SkData |
31 // can be somewhat expensive, and this call doesn't indicate to the generator th
at we're | 30 // can be somewhat expensive, and this call doesn't indicate to the generator th
at we're |
32 // only interested in GPU datas... | 31 // only interested in GPU datas... |
33 // see skbug.com/ 4971, 5128, ... | 32 // see skbug.com/ 4971, 5128, ... |
34 //#define SK_SUPPORT_COMPRESSED_TEXTURES_IN_CACHERATOR | 33 //#define SK_SUPPORT_COMPRESSED_TEXTURES_IN_CACHERATOR |
35 | 34 |
36 // Ref-counted tuple(SkImageGenerator, SkMutex) which allows sharing of one gene
rator | |
37 // among several cacherators. | |
38 class SkImageCacherator::SharedGenerator final : public SkNVRefCnt<SharedGenerat
or> { | |
39 public: | |
40 static sk_sp<SharedGenerator> Make(SkImageGenerator* gen) { | |
41 return gen ? sk_sp<SharedGenerator>(new SharedGenerator(gen)) : nullptr; | |
42 } | |
43 | |
44 private: | |
45 explicit SharedGenerator(SkImageGenerator* gen) : fGenerator(gen) { SkASSERT
(gen); } | |
46 | |
47 friend class ScopedGenerator; | |
48 | |
49 std::unique_ptr<SkImageGenerator> fGenerator; | |
50 SkMutex fMutex; | |
51 }; | |
52 | |
53 | |
54 // Helper for exclusive access to a shared generator. | 35 // Helper for exclusive access to a shared generator. |
55 class SkImageCacherator::ScopedGenerator { | 36 class SkImageCacherator::ScopedGenerator { |
56 public: | 37 public: |
57 ScopedGenerator(const sk_sp<SharedGenerator>& gen) | 38 ScopedGenerator(const sk_sp<SharedGenerator>& gen) |
58 : fSharedGenerator(gen) | 39 : fSharedGenerator(gen) |
59 , fAutoAquire(gen->fMutex) {} | 40 , fAutoAquire(gen->fMutex) {} |
60 | 41 |
61 SkImageGenerator* operator->() const { | 42 SkImageGenerator* operator->() const { |
62 fSharedGenerator->fMutex.assertHeld(); | 43 fSharedGenerator->fMutex.assertHeld(); |
63 return fSharedGenerator->fGenerator.get(); | 44 return fSharedGenerator->fGenerator.get(); |
64 } | 45 } |
65 | 46 |
66 operator SkImageGenerator*() const { | 47 operator SkImageGenerator*() const { |
67 fSharedGenerator->fMutex.assertHeld(); | 48 fSharedGenerator->fMutex.assertHeld(); |
68 return fSharedGenerator->fGenerator.get(); | 49 return fSharedGenerator->fGenerator.get(); |
69 } | 50 } |
70 | 51 |
71 private: | 52 private: |
72 const sk_sp<SharedGenerator>& fSharedGenerator; | 53 const sk_sp<SharedGenerator>& fSharedGenerator; |
73 SkAutoExclusive fAutoAquire; | 54 SkAutoExclusive fAutoAquire; |
74 }; | 55 }; |
75 | 56 |
76 SkImageCacherator::Validator::Validator(SkImageGenerator* gen, const SkIRect* su
bset) | 57 SkImageCacherator::Validator::Validator(sk_sp<SharedGenerator> gen, const SkIRec
t* subset) |
77 // We are required to take ownership of gen, regardless of whether we instan
tiate a cacherator | 58 : fSharedGenerator(std::move(gen)) { |
78 // or not. On instantiation, the client is responsible for transferring own
ership. | |
79 : fSharedGenerator(SkImageCacherator::SharedGenerator::Make(gen)) { | |
80 | 59 |
81 if (!fSharedGenerator) { | 60 if (!fSharedGenerator) { |
82 return; | 61 return; |
83 } | 62 } |
84 | 63 |
85 const SkImageInfo& info = gen->getInfo(); | 64 // The following generator accessors are safe without acquiring the mutex (c
onst getters). |
| 65 // TODO: refactor to use a ScopedGenerator instead, for clarity. |
| 66 const SkImageInfo& info = fSharedGenerator->fGenerator->getInfo(); |
86 if (info.isEmpty()) { | 67 if (info.isEmpty()) { |
87 fSharedGenerator.reset(); | 68 fSharedGenerator.reset(); |
88 return; | 69 return; |
89 } | 70 } |
90 | 71 |
91 fUniqueID = gen->uniqueID(); | 72 fUniqueID = fSharedGenerator->fGenerator->uniqueID(); |
92 const SkIRect bounds = SkIRect::MakeWH(info.width(), info.height()); | 73 const SkIRect bounds = SkIRect::MakeWH(info.width(), info.height()); |
93 if (subset) { | 74 if (subset) { |
94 if (!bounds.contains(*subset)) { | 75 if (!bounds.contains(*subset)) { |
95 fSharedGenerator.reset(); | 76 fSharedGenerator.reset(); |
96 return; | 77 return; |
97 } | 78 } |
98 if (*subset != bounds) { | 79 if (*subset != bounds) { |
99 // we need a different uniqueID since we really are a subset of the
raw generator | 80 // we need a different uniqueID since we really are a subset of the
raw generator |
100 fUniqueID = SkNextID::ImageID(); | 81 fUniqueID = SkNextID::ImageID(); |
101 } | 82 } |
102 } else { | 83 } else { |
103 subset = &bounds; | 84 subset = &bounds; |
104 } | 85 } |
105 | 86 |
106 fInfo = info.makeWH(subset->width(), subset->height()); | 87 fInfo = info.makeWH(subset->width(), subset->height()); |
107 fOrigin = SkIPoint::Make(subset->x(), subset->y()); | 88 fOrigin = SkIPoint::Make(subset->x(), subset->y()); |
108 } | 89 } |
109 | 90 |
110 SkImageCacherator::Validator::~Validator() {} | |
111 | |
112 SkImageCacherator* SkImageCacherator::NewFromGenerator(SkImageGenerator* gen, | 91 SkImageCacherator* SkImageCacherator::NewFromGenerator(SkImageGenerator* gen, |
113 const SkIRect* subset) { | 92 const SkIRect* subset) { |
114 Validator validator(gen, subset); | 93 Validator validator(SharedGenerator::Make(gen), subset); |
115 | 94 |
116 return validator ? new SkImageCacherator(&validator) : nullptr; | 95 return validator ? new SkImageCacherator(&validator) : nullptr; |
117 } | 96 } |
118 | 97 |
119 SkImageCacherator::SkImageCacherator(Validator* validator) | 98 SkImageCacherator::SkImageCacherator(Validator* validator) |
120 : fSharedGenerator(std::move(validator->fSharedGenerator)) // we take owners
hip | 99 : fSharedGenerator(std::move(validator->fSharedGenerator)) // we take owners
hip |
121 , fInfo(validator->fInfo) | 100 , fInfo(validator->fInfo) |
122 , fOrigin(validator->fOrigin) | 101 , fOrigin(validator->fOrigin) |
123 , fUniqueID(validator->fUniqueID) | 102 , fUniqueID(validator->fUniqueID) |
124 { | 103 { |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 | 377 |
399 #else | 378 #else |
400 | 379 |
401 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam
s&, | 380 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam
s&, |
402 SkSourceGammaTreatment gammaTreatmen
t, | 381 SkSourceGammaTreatment gammaTreatmen
t, |
403 const SkImage* client, SkImage::Cach
ingHint) { | 382 const SkImage* client, SkImage::Cach
ingHint) { |
404 return nullptr; | 383 return nullptr; |
405 } | 384 } |
406 | 385 |
407 #endif | 386 #endif |
OLD | NEW |