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

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

Issue 1291803002: Extend SkImageGenerator to support natively generated GrTextures (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 4 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 | « src/core/SkImageCacherator.h ('k') | src/core/SkImageGenerator.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "SkBitmap.h"
9 #include "SkBitmapCache.h"
10 #include "SkImageCacherator.h"
11 #include "SkPixelRef.h"
12
13 #if SK_SUPPORT_GPU
14 #include "GrContext.h"
15 #include "GrGpuResourcePriv.h"
16 #include "GrResourceKey.h"
17 #include "GrTextureAccess.h"
18 #include "SkGr.h"
19 #include "SkGrPriv.h"
20 #endif
21
22 SkImageCacherator::SkImageCacherator(SkImageGenerator* gen) : fGenerator(gen) {}
23
24 SkImageCacherator::~SkImageCacherator() {
25 delete fGenerator;
26 }
27
28 static bool check_output_bitmap(const SkBitmap& bitmap, uint32_t expectedID) {
29 SkASSERT(bitmap.getGenerationID() == expectedID);
30 SkASSERT(bitmap.isImmutable());
31 SkASSERT(bitmap.getPixels());
32 return true;
33 }
34
35 static bool generate_bitmap(SkImageGenerator* generator, SkBitmap* bitmap) {
36 if (!bitmap->tryAllocPixels(generator->getInfo()) ||
37 !generator->getPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBy tes()))
38 {
39 bitmap->reset();
40 return false;
41 }
42 return true;
43 }
44
45 //////////////////////////////////////////////////////////////////////////////// //////////////////
46
47 bool SkImageCacherator::tryLockAsBitmap(SkBitmap* bitmap) {
48 const uint32_t uniqueID = fGenerator->uniqueID();
49
50 if (SkBitmapCache::Find(uniqueID, bitmap)) {
51 return check_output_bitmap(*bitmap, uniqueID);
52 }
53 if (!generate_bitmap(fGenerator, bitmap)) {
54 return false;
55 }
56
57 bitmap->pixelRef()->setImmutableWithID(uniqueID);
58 SkBitmapCache::Add(uniqueID, *bitmap);
59 return true;
60 }
61
62 bool SkImageCacherator::lockAsBitmap(SkBitmap* bitmap) {
63 const uint32_t uniqueID = fGenerator->uniqueID();
64
65 if (this->tryLockAsBitmap(bitmap)) {
66 return check_output_bitmap(*bitmap, uniqueID);
67 }
68
69 #if SK_SUPPORT_GPU
70 // Try to get a texture and read it back to raster (and then cache that with our ID)
71
72 SkAutoTUnref<GrTexture> tex(fGenerator->generateTexture(nullptr, kUntiled_Sk ImageUsageType));
73 if (!tex) {
74 bitmap->reset();
75 return false;
76 }
77
78 const SkImageInfo& info = this->info();
79 if (!bitmap->tryAllocPixels(info)) {
80 bitmap->reset();
81 return false;
82 }
83
84 const uint32_t pixelOpsFlags = 0;
85 if (!tex->readPixels(0, 0, bitmap->width(), bitmap->height(), SkImageInfo2Gr PixelConfig(info),
86 bitmap->getPixels(), bitmap->rowBytes(), pixelOpsFlags) ) {
87 bitmap->reset();
88 return false;
89 }
90
91 bitmap->pixelRef()->setImmutableWithID(uniqueID);
92 SkBitmapCache::Add(uniqueID, *bitmap);
93 return check_output_bitmap(*bitmap, uniqueID);
94 #else
95 return false;
96 #endif
97 }
98
99 //////////////////////////////////////////////////////////////////////////////// //////////////////
100
101 GrTexture* SkImageCacherator::tryLockAsTexture(GrContext* ctx, SkImageUsageType usage) {
102 #if SK_SUPPORT_GPU
103 const uint32_t uniqueID = fGenerator->uniqueID();
104 const SkImageInfo& info = this->info();
105
106 GrUniqueKey key;
107 GrMakeKeyFromImageID(&key, uniqueID, info.width(), info.height(), SkIPoint:: Make(0, 0),
108 *ctx->caps(), usage);
109 GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(key);
110 if (tex) {
111 return tex; // we got a cache hit!
112 }
113
114 tex = fGenerator->generateTexture(ctx, usage);
115 if (tex) {
116 tex->resourcePriv().setUniqueKey(key);
117 }
118 return tex;
119 #else
120 return nullptr;
121 #endif
122 }
123
124 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, SkImageUsageType usa ge) {
125 #if SK_SUPPORT_GPU
126 if (!ctx) {
127 return nullptr;
128 }
129 if (GrTexture* tex = this->tryLockAsTexture(ctx, usage)) {
130 return tex;
131 }
132
133 // Try to get a bitmap and then upload/cache it as a texture
134
135 SkBitmap bitmap;
136 if (!generate_bitmap(fGenerator, &bitmap)) {
137 return nullptr;
138 }
139 return GrRefCachedBitmapTexture(ctx, bitmap, usage);
140 #else
141 return nullptr;
142 #endif
143 }
144
OLDNEW
« no previous file with comments | « src/core/SkImageCacherator.h ('k') | src/core/SkImageGenerator.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698