OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2015 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 "SkTypes.h" | |
9 | |
10 #if SK_SUPPORT_GPU | |
11 #include "GrContext.h" | |
12 #include "GrGpuResourcePriv.h" | |
13 #include "GrResourceKey.h" | |
14 #include "GrTextureAccess.h" | |
15 #endif | |
16 | |
17 #include "SkBitmap.h" | |
18 #include "SkBitmapCache.h" | |
19 #include "SkGr.h" | |
20 #include "SkGrPriv.h" | |
21 #include "SkImageCacherator.h" | |
22 #include "SkPixelRef.h" | |
23 | |
24 | |
25 SkImageCacherator::SkImageCacherator(SkImageGenerator* gen) : fGenerator(gen) {} | |
26 | |
27 SkImageCacherator::~SkImageCacherator() { | |
28 delete fGenerator; | |
scroggo
2015/08/13 19:59:13
SkDELETE?
reed1
2015/08/13 20:57:08
Done.
| |
29 } | |
30 | |
31 static bool check_output_bitmap(const SkBitmap& bitmap, uint32_t expectedID) { | |
scroggo
2015/08/13 19:59:13
This always returns true, so maybe it should retur
reed1
2015/08/13 20:57:08
Acknowledged.
| |
32 SkASSERT(bitmap.getGenerationID() == expectedID); | |
33 SkASSERT(bitmap.isImmutable()); | |
34 SkASSERT(bitmap.getPixels()); | |
35 return true; | |
36 } | |
37 | |
38 static bool generate_bitmap(SkImageGenerator* generator, SkBitmap* bitmap) { | |
39 if (!bitmap->tryAllocPixels(generator->getInfo()) || | |
40 !generator->getPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBy tes())) | |
41 { | |
42 bitmap->reset(); | |
43 return false; | |
44 } | |
45 return true; | |
46 } | |
47 | |
48 //////////////////////////////////////////////////////////////////////////////// ////////////////// | |
49 | |
50 bool SkImageCacherator::tryLockAsBitmap(SkBitmap* bitmap) { | |
51 const uint32_t uniqueID = fGenerator->uniqueID(); | |
52 | |
53 if (SkBitmapCache::Find(uniqueID, bitmap)) { | |
54 return check_output_bitmap(*bitmap, uniqueID); | |
55 } | |
56 if (!generate_bitmap(fGenerator, bitmap)) { | |
57 return false; | |
58 } | |
59 | |
60 bitmap->pixelRef()->setImmutableWithID(uniqueID); | |
61 SkBitmapCache::Add(uniqueID, *bitmap); | |
62 return true; | |
63 } | |
64 | |
65 bool SkImageCacherator::lockAsBitmap(SkBitmap* bitmap) { | |
66 const uint32_t uniqueID = fGenerator->uniqueID(); | |
67 const SkImageInfo& info = this->info(); | |
scroggo
2015/08/13 19:59:13
Maybe move this down closer to where it's used?
reed1
2015/08/13 20:57:08
Done.
| |
68 | |
69 if (this->tryLockAsBitmap(bitmap)) { | |
70 return check_output_bitmap(*bitmap, uniqueID); | |
71 } | |
72 | |
73 // Try to get a texture and read it back to raster (and then cache that with our ID) | |
scroggo
2015/08/13 19:59:13
A read back is potentially expensive, right? Shoul
reed1
2015/08/13 20:57:08
Calling the generator itself can be very expensive
| |
74 | |
75 SkAutoTUnref<GrTexture> tex(fGenerator->generateTexture(nullptr, kUntiled_Sk ImageUsageType)); | |
76 if (!tex) { | |
77 bitmap->reset(); | |
78 return false; | |
79 } | |
80 | |
81 if (!bitmap->tryAllocPixels(info)) { | |
82 bitmap->reset(); | |
83 return false; | |
84 } | |
85 | |
86 const uint32_t pixelOpsFlags = 0; | |
87 if (!tex->readPixels(0, 0, bitmap->width(), bitmap->height(), SkImageInfo2Gr PixelConfig(info), | |
88 bitmap->getPixels(), bitmap->rowBytes(), pixelOpsFlags) ) { | |
89 bitmap->reset(); | |
90 return false; | |
91 } | |
92 | |
93 bitmap->pixelRef()->setImmutableWithID(uniqueID); | |
94 SkBitmapCache::Add(uniqueID, *bitmap); | |
95 return check_output_bitmap(*bitmap, uniqueID); | |
96 } | |
97 | |
98 //////////////////////////////////////////////////////////////////////////////// ////////////////// | |
99 | |
100 GrTexture* SkImageCacherator::tryLockAsTexture(GrContext* ctx, SkImageUsageType usage) { | |
101 #if SK_SUPPORT_GPU | |
102 const uint32_t uniqueID = fGenerator->uniqueID(); | |
103 const SkImageInfo& info = this->info(); | |
104 | |
105 GrUniqueKey key; | |
106 GrMakeKeyFromImageID(&key, uniqueID, info.width(), info.height(), SkIPoint:: Make(0, 0), | |
107 *ctx->caps(), usage); | |
108 GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(key); | |
109 if (tex) { | |
110 return tex; // we got a cache hit! | |
111 } | |
112 | |
113 tex = fGenerator->generateTexture(ctx, usage); | |
114 if (tex) { | |
115 tex->resourcePriv().setUniqueKey(key); | |
116 } | |
117 return tex; | |
118 #else | |
119 return nullptr; | |
120 #endif | |
121 } | |
122 | |
123 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, SkImageUsageType usa ge) { | |
124 #if SK_SUPPORT_GPU | |
125 if (!ctx) { | |
126 return nullptr; | |
127 } | |
128 if (GrTexture* tex = this->tryLockAsTexture(ctx, usage)) { | |
129 return tex; | |
130 } | |
131 | |
132 // Try to get a bitmap and then upload/cache it as a texture | |
133 | |
134 SkBitmap bitmap; | |
135 if (!generate_bitmap(fGenerator, &bitmap)) { | |
136 return nullptr; | |
137 } | |
138 return GrRefCachedBitmapTexture(ctx, bitmap, usage); | |
139 #else | |
140 return nullptr; | |
141 #endif | |
142 } | |
143 | |
OLD | NEW |