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 const SkImageInfo& info = fSharedGenerator->fGenerator->getInfo(); |
reed1
2016/11/04 17:55:29
I presume its "safe" to access the generator w/o g
f(malita)
2016/11/04 18:10:16
Added comment, will follow up with cleanup CL.
| |
86 if (info.isEmpty()) { | 65 if (info.isEmpty()) { |
87 fSharedGenerator.reset(); | 66 fSharedGenerator.reset(); |
88 return; | 67 return; |
89 } | 68 } |
90 | 69 |
91 fUniqueID = gen->uniqueID(); | 70 fUniqueID = fSharedGenerator->fGenerator->uniqueID(); |
92 const SkIRect bounds = SkIRect::MakeWH(info.width(), info.height()); | 71 const SkIRect bounds = SkIRect::MakeWH(info.width(), info.height()); |
93 if (subset) { | 72 if (subset) { |
94 if (!bounds.contains(*subset)) { | 73 if (!bounds.contains(*subset)) { |
95 fSharedGenerator.reset(); | 74 fSharedGenerator.reset(); |
96 return; | 75 return; |
97 } | 76 } |
98 if (*subset != bounds) { | 77 if (*subset != bounds) { |
99 // we need a different uniqueID since we really are a subset of the raw generator | 78 // we need a different uniqueID since we really are a subset of the raw generator |
100 fUniqueID = SkNextID::ImageID(); | 79 fUniqueID = SkNextID::ImageID(); |
101 } | 80 } |
102 } else { | 81 } else { |
103 subset = &bounds; | 82 subset = &bounds; |
104 } | 83 } |
105 | 84 |
106 fInfo = info.makeWH(subset->width(), subset->height()); | 85 fInfo = info.makeWH(subset->width(), subset->height()); |
107 fOrigin = SkIPoint::Make(subset->x(), subset->y()); | 86 fOrigin = SkIPoint::Make(subset->x(), subset->y()); |
108 } | 87 } |
109 | 88 |
110 SkImageCacherator::Validator::~Validator() {} | |
111 | |
112 SkImageCacherator* SkImageCacherator::NewFromGenerator(SkImageGenerator* gen, | 89 SkImageCacherator* SkImageCacherator::NewFromGenerator(SkImageGenerator* gen, |
113 const SkIRect* subset) { | 90 const SkIRect* subset) { |
114 Validator validator(gen, subset); | 91 Validator validator(SharedGenerator::Make(gen), subset); |
115 | 92 |
116 return validator ? new SkImageCacherator(&validator) : nullptr; | 93 return validator ? new SkImageCacherator(&validator) : nullptr; |
117 } | 94 } |
118 | 95 |
119 SkImageCacherator::SkImageCacherator(Validator* validator) | 96 SkImageCacherator::SkImageCacherator(Validator* validator) |
120 : fSharedGenerator(std::move(validator->fSharedGenerator)) // we take owners hip | 97 : fSharedGenerator(std::move(validator->fSharedGenerator)) // we take owners hip |
121 , fInfo(validator->fInfo) | 98 , fInfo(validator->fInfo) |
122 , fOrigin(validator->fOrigin) | 99 , fOrigin(validator->fOrigin) |
123 , fUniqueID(validator->fUniqueID) | 100 , fUniqueID(validator->fUniqueID) |
124 { | 101 { |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
398 | 375 |
399 #else | 376 #else |
400 | 377 |
401 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam s&, | 378 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam s&, |
402 SkSourceGammaTreatment gammaTreatmen t, | 379 SkSourceGammaTreatment gammaTreatmen t, |
403 const SkImage* client, SkImage::Cach ingHint) { | 380 const SkImage* client, SkImage::Cach ingHint) { |
404 return nullptr; | 381 return nullptr; |
405 } | 382 } |
406 | 383 |
407 #endif | 384 #endif |
OLD | NEW |