Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1053)

Side by Side Diff: src/gpu/SkGr.cpp

Issue 26734003: Proactive SkPixelRef invalidation (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: review changes Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright 2010 Google Inc. 2 * Copyright 2010 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 "SkGr.h" 8 #include "SkGr.h"
9 #include "SkConfig8888.h" 9 #include "SkConfig8888.h"
10 #include "SkMessageBus.h"
11 #include "SkPixelRef.h"
12 #include "GrResourceCache.h"
10 13
11 /* Fill out buffer with the compressed format Ganesh expects from a colortable 14 /* Fill out buffer with the compressed format Ganesh expects from a colortable
12 based bitmap. [palette (colortable) + indices]. 15 based bitmap. [palette (colortable) + indices].
13 16
14 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others 17 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
15 we could detect that the colortable.count is <= 16, and then repack the 18 we could detect that the colortable.count is <= 16, and then repack the
16 indices as nibbles to save RAM, but it would take more time (i.e. a lot 19 indices as nibbles to save RAM, but it would take more time (i.e. a lot
17 slower than memcpy), so skipping that for now. 20 slower than memcpy), so skipping that for now.
18 21
19 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big 22 Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 } 82 }
80 83
81 static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrTextureDesc* desc) { 84 static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrTextureDesc* desc) {
82 desc->fFlags = kNone_GrTextureFlags; 85 desc->fFlags = kNone_GrTextureFlags;
83 desc->fWidth = bitmap.width(); 86 desc->fWidth = bitmap.width();
84 desc->fHeight = bitmap.height(); 87 desc->fHeight = bitmap.height();
85 desc->fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config()); 88 desc->fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
86 desc->fSampleCnt = 0; 89 desc->fSampleCnt = 0;
87 } 90 }
88 91
92 namespace {
93
94 // When the SkPixelRef is invalidated, invalidate a corresponding GrResource des cribed by key.
95 class GrResourceInvalidator : public SkPixelRef::InvalidationListener {
96 public:
97 explicit GrResourceInvalidator(GrResourceKey key) : fKey(key) {}
98 private:
99 GrResourceKey fKey;
100
101 virtual void onInvalidate() SK_OVERRIDE {
102 const GrResourceInvalidatedMessage message = { fKey };
103 SkMessageBus<GrResourceInvalidatedMessage>::Post(message);
104 }
105 };
106
107 } // namespace
108
109 static void add_invalidation_listener(GrResourceKey key, SkPixelRef* pixelRef) {
110 SkASSERT(NULL != pixelRef);
111 if (pixelRef) {
scroggo 2013/10/23 15:39:19 Do you want this check even though we've just asse
mtklein 2013/10/23 16:07:10 Dropped the check.
112 pixelRef->addInvalidationListener(SkNEW_ARGS(GrResourceInvalidator, (key )));
113 }
114 }
115
89 static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx, 116 static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx,
90 bool cache, 117 bool cache,
91 const GrTextureParams* params, 118 const GrTextureParams* params,
92 const SkBitmap& origBitmap) { 119 const SkBitmap& origBitmap) {
93 SkBitmap tmpBitmap; 120 SkBitmap tmpBitmap;
94 121
95 const SkBitmap* bitmap = &origBitmap; 122 const SkBitmap* bitmap = &origBitmap;
96 123
97 GrTextureDesc desc; 124 GrTextureDesc desc;
98 generate_bitmap_texture_desc(*bitmap, &desc); 125 generate_bitmap_texture_desc(*bitmap, &desc);
99 126
100 if (SkBitmap::kIndex8_Config == bitmap->config()) { 127 if (SkBitmap::kIndex8_Config == bitmap->config()) {
101 // build_compressed_data doesn't do npot->pot expansion 128 // build_compressed_data doesn't do npot->pot expansion
102 // and paletted textures can't be sub-updated 129 // and paletted textures can't be sub-updated
103 if (ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->heig ht())) { 130 if (ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->heig ht())) {
104 size_t imagesize = bitmap->width() * bitmap->height() + kGrColorTabl eSize; 131 size_t imagesize = bitmap->width() * bitmap->height() + kGrColorTabl eSize;
105 SkAutoMalloc storage(imagesize); 132 SkAutoMalloc storage(imagesize);
106 133
107 build_compressed_data(storage.get(), origBitmap); 134 build_compressed_data(storage.get(), origBitmap);
108 135
109 // our compressed data will be trimmed, so pass width() for its 136 // our compressed data will be trimmed, so pass width() for its
110 // "rowBytes", since they are the same now. 137 // "rowBytes", since they are the same now.
111 138
112 if (cache) { 139 if (cache) {
113 GrCacheID cacheID; 140 GrCacheID cacheID;
114 generate_bitmap_cache_id(origBitmap, &cacheID); 141 generate_bitmap_cache_id(origBitmap, &cacheID);
115 return ctx->createTexture(params, desc, cacheID, storage.get(), bitmap->width()); 142
143 GrResourceKey key;
144 GrTexture* result = ctx->createTexture(params, desc, cacheID,
145 storage.get(), bitmap->wi dth(), &key);
146 add_invalidation_listener(key, origBitmap.pixelRef());
147 return result;
116 } else { 148 } else {
117 GrTexture* result = ctx->lockAndRefScratchTexture(desc, 149 GrTexture* result = ctx->lockAndRefScratchTexture(desc,
118 GrContext::kExact_Sc ratchTexMatch); 150 GrContext::kExact_Sc ratchTexMatch);
119 result->writePixels(0, 0, bitmap->width(), 151 result->writePixels(0, 0, bitmap->width(),
120 bitmap->height(), desc.fConfig, 152 bitmap->height(), desc.fConfig,
121 storage.get()); 153 storage.get());
122 return result; 154 return result;
123 } 155 }
124 } else { 156 } else {
125 origBitmap.copyTo(&tmpBitmap, SkBitmap::kARGB_8888_Config); 157 origBitmap.copyTo(&tmpBitmap, SkBitmap::kARGB_8888_Config);
126 // now bitmap points to our temp, which has been promoted to 32bits 158 // now bitmap points to our temp, which has been promoted to 32bits
127 bitmap = &tmpBitmap; 159 bitmap = &tmpBitmap;
128 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config()); 160 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config());
129 } 161 }
130 } 162 }
131 163
132 SkAutoLockPixels alp(*bitmap); 164 SkAutoLockPixels alp(*bitmap);
133 if (!bitmap->readyToDraw()) { 165 if (!bitmap->readyToDraw()) {
134 return NULL; 166 return NULL;
135 } 167 }
136 if (cache) { 168 if (cache) {
137 // This texture is likely to be used again so leave it in the cache 169 // This texture is likely to be used again so leave it in the cache
138 GrCacheID cacheID; 170 GrCacheID cacheID;
139 generate_bitmap_cache_id(origBitmap, &cacheID); 171 generate_bitmap_cache_id(origBitmap, &cacheID);
140 return ctx->createTexture(params, desc, cacheID, bitmap->getPixels(), bi tmap->rowBytes()); 172
141 } else { 173 GrResourceKey key;
174 GrTexture* result = ctx->createTexture(params, desc, cacheID,
175 bitmap->getPixels(), bitmap->rowB ytes(), &key);
176 add_invalidation_listener(key, origBitmap.pixelRef());
177 return result;
178 } else {
142 // This texture is unlikely to be used again (in its present form) so 179 // This texture is unlikely to be used again (in its present form) so
143 // just use a scratch texture. This will remove the texture from the 180 // just use a scratch texture. This will remove the texture from the
144 // cache so no one else can find it. Additionally, once unlocked, the 181 // cache so no one else can find it. Additionally, once unlocked, the
145 // scratch texture will go to the end of the list for purging so will 182 // scratch texture will go to the end of the list for purging so will
146 // likely be available for this volatile bitmap the next time around. 183 // likely be available for this volatile bitmap the next time around.
147 GrTexture* result = ctx->lockAndRefScratchTexture(desc, GrContext::kExac t_ScratchTexMatch); 184 GrTexture* result = ctx->lockAndRefScratchTexture(desc, GrContext::kExac t_ScratchTexMatch);
148 result->writePixels(0, 0, 185 result->writePixels(0, 0,
149 bitmap->width(), bitmap->height(), 186 bitmap->width(), bitmap->height(),
150 desc.fConfig, 187 desc.fConfig,
151 bitmap->getPixels(), 188 bitmap->getPixels(),
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 return kRGB_565_GrPixelConfig; 249 return kRGB_565_GrPixelConfig;
213 case SkBitmap::kARGB_4444_Config: 250 case SkBitmap::kARGB_4444_Config:
214 return kRGBA_4444_GrPixelConfig; 251 return kRGBA_4444_GrPixelConfig;
215 case SkBitmap::kARGB_8888_Config: 252 case SkBitmap::kARGB_8888_Config:
216 return kSkia8888_GrPixelConfig; 253 return kSkia8888_GrPixelConfig;
217 default: 254 default:
218 // kNo_Config, kA1_Config missing 255 // kNo_Config, kA1_Config missing
219 return kUnknown_GrPixelConfig; 256 return kUnknown_GrPixelConfig;
220 } 257 }
221 } 258 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698