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

Side by Side Diff: src/core/SkImageCacherator.cpp

Issue 1282363002: Use SkImageCacherator in SkImages (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: fix rob comments Created 5 years, 3 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
« no previous file with comments | « include/gpu/SkGr.h ('k') | src/gpu/SkGpuDevice.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "SkImageCacherator.h" 10 #include "SkImageCacherator.h"
11 #include "SkMallocPixelRef.h" 11 #include "SkMallocPixelRef.h"
12 #include "SkNextID.h" 12 #include "SkNextID.h"
13 #include "SkPixelRef.h" 13 #include "SkPixelRef.h"
14 14
15 #if SK_SUPPORT_GPU 15 #if SK_SUPPORT_GPU
16 #include "GrContext.h" 16 #include "GrContext.h"
17 #include "GrGpuResourcePriv.h" 17 #include "GrGpuResourcePriv.h"
18 #include "GrResourceKey.h" 18 #include "GrResourceKey.h"
19 #include "GrTextureAccess.h" 19 #include "GrTextureAccess.h"
20 #include "GrYUVProvider.h"
20 #include "SkGr.h" 21 #include "SkGr.h"
21 #include "SkGrPriv.h" 22 #include "SkGrPriv.h"
22 #endif 23 #endif
23 24
24 SkImageCacherator* SkImageCacherator::NewFromGenerator(SkImageGenerator* gen, 25 SkImageCacherator* SkImageCacherator::NewFromGenerator(SkImageGenerator* gen,
25 const SkIRect* subset) { 26 const SkIRect* subset) {
26 if (!gen) { 27 if (!gen) {
27 return nullptr; 28 return nullptr;
28 } 29 }
29 const SkImageInfo& info = gen->getInfo(); 30 const SkImageInfo& info = gen->getInfo();
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 bitmap->pixelRef()->setImmutableWithID(fUniqueID); 152 bitmap->pixelRef()->setImmutableWithID(fUniqueID);
152 SkBitmapCache::Add(fUniqueID, *bitmap); 153 SkBitmapCache::Add(fUniqueID, *bitmap);
153 return check_output_bitmap(*bitmap, fUniqueID); 154 return check_output_bitmap(*bitmap, fUniqueID);
154 #else 155 #else
155 return false; 156 return false;
156 #endif 157 #endif
157 } 158 }
158 159
159 //////////////////////////////////////////////////////////////////////////////// ////////////////// 160 //////////////////////////////////////////////////////////////////////////////// //////////////////
160 161
162 #if SK_SUPPORT_GPU
163 static void make_texture_desc(const SkImageInfo& info, GrSurfaceDesc* desc) {
164 desc->fFlags = kNone_GrSurfaceFlags;
165 desc->fWidth = info.width();
166 desc->fHeight = info.height();
167 desc->fConfig = SkImageInfo2GrPixelConfig(info);
168 desc->fSampleCnt = 0;
169 }
170
171 static GrTexture* load_compressed_into_texture(GrContext* ctx, SkData* data, GrS urfaceDesc desc) {
172 const void* rawStart;
173 GrPixelConfig config = GrIsCompressedTextureDataSupported(ctx, data, desc.fW idth, desc.fHeight,
174 &rawStart);
175 if (kUnknown_GrPixelConfig == config) {
176 return nullptr;
177 }
178
179 desc.fConfig = config;
180 return ctx->textureProvider()->createTexture(desc, true, rawStart, 0);
181 }
182
183 class Generator_GrYUVProvider : public GrYUVProvider {
184 SkImageGenerator* fGen;
185
186 public:
187 Generator_GrYUVProvider(SkImageGenerator* gen) : fGen(gen) {}
188
189 uint32_t onGetID() override { return fGen->uniqueID(); }
190 bool onGetYUVSizes(SkISize sizes[3]) override {
191 return fGen->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
192 }
193 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
194 SkYUVColorSpace* space) override {
195 return fGen->getYUV8Planes(sizes, planes, rowBytes, space);
196 }
197 };
198
199 static GrTexture* set_key_and_return(GrTexture* tex, const GrUniqueKey& key) {
200 tex->resourcePriv().setUniqueKey(key);
201 return tex;
202 }
203 #endif
204
161 /* 205 /*
162 * We have a 5 ways to try to return a texture (in sorted order) 206 * We have a 5 ways to try to return a texture (in sorted order)
163 * 207 *
164 * 1. Check the cache for a pre-existing one 208 * 1. Check the cache for a pre-existing one
165 * 2. Ask the genreator to natively create one 209 * 2. Ask the genreator to natively create one
166 * 3. Ask the generator to return a compressed form that the GPU might support 210 * 3. Ask the generator to return a compressed form that the GPU might support
167 * 4. Ask the generator to return YUV planes, which the GPU can convert 211 * 4. Ask the generator to return YUV planes, which the GPU can convert
168 * 5. Ask the generator to return RGB(A) data, which the GPU can convert 212 * 5. Ask the generator to return RGB(A) data, which the GPU can convert
169 */ 213 */
170 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, SkImageUsageType usa ge) { 214 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, SkImageUsageType usa ge) {
171 #if SK_SUPPORT_GPU 215 #if SK_SUPPORT_GPU
172 if (!ctx) { 216 if (!ctx) {
173 return nullptr; 217 return nullptr;
174 } 218 }
175 219
220 // textures (at least the texture-key) only support 16bit dimensions, so abo rt early
221 // if we're too big.
222 if (fInfo.width() > 0xFFFF || fInfo.height() > 0xFFFF) {
223 return nullptr;
224 }
225
176 GrUniqueKey key; 226 GrUniqueKey key;
177 GrMakeKeyFromImageID(&key, fUniqueID, fInfo.width(), fInfo.height(), SkIPoin t::Make(0, 0), 227 GrMakeKeyFromImageID(&key, fUniqueID, SkIRect::MakeWH(fInfo.width(), fInfo.h eight()),
178 *ctx->caps(), usage); 228 *ctx->caps(), usage);
179 229
230 GrSurfaceDesc desc;
231 make_texture_desc(fInfo, &desc);
232
180 // 1. Check the cache for a pre-existing one 233 // 1. Check the cache for a pre-existing one
181 if (GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(ke y)) { 234 if (GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(ke y)) {
182 return tex; 235 return tex;
183 } 236 }
184 237
185 // 2. Ask the genreator to natively create one 238 // 2. Ask the genreator to natively create one
186 { 239 {
187 ScopedGenerator generator(this); 240 ScopedGenerator generator(this);
188 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width (), fInfo.height()); 241 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width (), fInfo.height());
189 if (GrTexture* tex = generator->generateTexture(ctx, usage, &subset)) { 242 if (GrTexture* tex = generator->generateTexture(ctx, usage, &subset)) {
190 tex->resourcePriv().setUniqueKey(key); 243 return set_key_and_return(tex, key);
191 return tex;
192 } 244 }
193 } 245 }
194 246
195 // 3. Ask the generator to return a compressed form that the GPU might suppo rt 247 // 3. Ask the generator to return a compressed form that the GPU might suppo rt
196 // TODO 248 SkAutoTUnref<SkData> data(this->refEncoded());
249 if (data) {
250 GrTexture* tex = load_compressed_into_texture(ctx, data, desc);
251 if (tex) {
252 return set_key_and_return(tex, key);
253 }
254 }
197 255
198 // 4. Ask the generator to return YUV planes, which the GPU can convert 256 // 4. Ask the generator to return YUV planes, which the GPU can convert
199 // TODO 257 {
200 258 ScopedGenerator generator(this);
259 Generator_GrYUVProvider provider(generator);
260 GrTexture* tex = provider.refAsTexture(ctx, desc, true);
261 if (tex) {
262 return set_key_and_return(tex, key);
263 }
264 }
201 265
202 // 5. Ask the generator to return RGB(A) data, which the GPU can convert 266 // 5. Ask the generator to return RGB(A) data, which the GPU can convert
203 SkBitmap bitmap; 267 SkBitmap bitmap;
204 if (!this->generateBitmap(&bitmap)) { 268 if (this->generateBitmap(&bitmap)) {
205 return nullptr; 269 return GrRefCachedBitmapTexture(ctx, bitmap, usage);
206 } 270 }
207 return GrRefCachedBitmapTexture(ctx, bitmap, usage); 271 #endif
208 #else 272
209 return nullptr; 273 return nullptr;
210 #endif
211 } 274 }
212 275
OLDNEW
« no previous file with comments | « include/gpu/SkGr.h ('k') | src/gpu/SkGpuDevice.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698