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

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

Issue 302333002: Initial KTX file decoder (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Address code review comments Created 6 years, 6 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
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 "SkColorFilter.h" 9 #include "SkColorFilter.h"
10 #include "SkConfig8888.h" 10 #include "SkConfig8888.h"
11 #include "SkData.h" 11 #include "SkData.h"
12 #include "SkKTXFile.h"
12 #include "SkMessageBus.h" 13 #include "SkMessageBus.h"
13 #include "SkPixelRef.h" 14 #include "SkPixelRef.h"
14 #include "GrResourceCache.h" 15 #include "GrResourceCache.h"
15 #include "GrGpu.h" 16 #include "GrGpu.h"
16 #include "GrDrawTargetCaps.h" 17 #include "GrDrawTargetCaps.h"
17 18
18 #if SK_SUPPORT_ETC1 19 #if SK_SUPPORT_ETC1
19 # include "etc1.h" 20 # include "etc1.h"
20 #endif 21 #endif
21 22
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 129
129 static void add_genID_listener(GrResourceKey key, SkPixelRef* pixelRef) { 130 static void add_genID_listener(GrResourceKey key, SkPixelRef* pixelRef) {
130 SkASSERT(NULL != pixelRef); 131 SkASSERT(NULL != pixelRef);
131 pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key))); 132 pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key)));
132 } 133 }
133 134
134 #if SK_SUPPORT_ETC1 135 #if SK_SUPPORT_ETC1
135 static GrTexture *load_etc1_texture(GrContext* ctx, 136 static GrTexture *load_etc1_texture(GrContext* ctx,
136 const GrTextureParams* params, 137 const GrTextureParams* params,
137 const SkBitmap &bm, GrTextureDesc desc) { 138 const SkBitmap &bm, GrTextureDesc desc) {
138 SkData *data = bm.pixelRef()->refEncodedData(); 139 SkData *data = bm.pixelRef()->refEncodedData();
hal.canary 2014/06/03 15:41:58 As I mentioned in person, this will leak. From Sk
krajcevski 2014/06/03 18:56:29 Done.
139 140
140 // Is this even encoded data? 141 // Is this even encoded data?
141 if (NULL == data) { 142 if (NULL == data) {
142 return NULL; 143 return NULL;
143 } 144 }
144 145
145 // Is this a valid PKM encoded data? 146 // Is this a valid PKM encoded data?
146 const uint8_t *bytes = data->bytes(); 147 const uint8_t *bytes = data->bytes();
147 if (!etc1_pkm_is_valid(bytes)) { 148 if (etc1_pkm_is_valid(bytes)) {
149 uint32_t encodedWidth = etc1_pkm_get_width(bytes);
150 uint32_t encodedHeight = etc1_pkm_get_height(bytes);
151
152 // Does the data match the dimensions of the bitmap? If not,
153 // then we don't know how to scale the image to match it...
154 if (encodedWidth != static_cast<uint32_t>(bm.width()) ||
155 encodedHeight != static_cast<uint32_t>(bm.height())) {
156 return NULL;
157 }
158
159 // Everything seems good... skip ahead to the data.
160 bytes += ETC_PKM_HEADER_SIZE;
161 desc.fConfig = kETC1_GrPixelConfig;
162 } else if (SkKTXFile::is_ktx(bytes)) {
163 SkKTXFile ktx(bytes, data->size());
164
165 // Is it actually an ETC1 texture?
166 if (!ktx.isETC1()) {
167 return NULL;
168 }
169
170 // Does the data match the dimensions of the bitmap? If not,
171 // then we don't know how to scale the image to match it...
172 if (ktx.width() != bm.width() || ktx.height() != bm.height()) {
173 return NULL;
174 }
175
176 bytes = ktx.pixelData();
177 desc.fConfig = kETC1_GrPixelConfig;
178 } else {
148 return NULL; 179 return NULL;
149 } 180 }
150 181
151 uint32_t encodedWidth = etc1_pkm_get_width(bytes);
152 uint32_t encodedHeight = etc1_pkm_get_height(bytes);
153
154 // Does the data match the dimensions of the bitmap? If not,
155 // then we don't know how to scale the image to match it...
156 if (encodedWidth != static_cast<uint32_t>(bm.width()) ||
157 encodedHeight != static_cast<uint32_t>(bm.height())) {
158 return NULL;
159 }
160
161 // Everything seems good... skip ahead to the data.
162 bytes += ETC_PKM_HEADER_SIZE;
163 desc.fConfig = kETC1_GrPixelConfig;
164
165 // This texture is likely to be used again so leave it in the cache 182 // This texture is likely to be used again so leave it in the cache
166 GrCacheID cacheID; 183 GrCacheID cacheID;
167 generate_bitmap_cache_id(bm, &cacheID); 184 generate_bitmap_cache_id(bm, &cacheID);
168 185
169 GrResourceKey key; 186 GrResourceKey key;
170 GrTexture* result = ctx->createTexture(params, desc, cacheID, bytes, 0, &key ); 187 GrTexture* result = ctx->createTexture(params, desc, cacheID, bytes, 0, &key );
171 if (NULL != result) { 188 if (NULL != result) {
172 add_genID_listener(key, bm.pixelRef()); 189 add_genID_listener(key, bm.pixelRef());
173 } 190 }
174 return result; 191 return result;
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 copy.setShader(NULL); 510 copy.setShader(NULL);
494 // modulate the paint alpha by the shader's solid color alpha 511 // modulate the paint alpha by the shader's solid color alpha
495 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha()); 512 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
496 copy.setColor(SkColorSetA(color, newA)); 513 copy.setColor(SkColorSetA(color, newA));
497 SkPaint2GrPaintNoShader(context, copy, false, constantColor, grPaint ); 514 SkPaint2GrPaintNoShader(context, copy, false, constantColor, grPaint );
498 } else { 515 } else {
499 SkPaint2GrPaintNoShader(context, skPaint, false, constantColor, grPa int); 516 SkPaint2GrPaintNoShader(context, skPaint, false, constantColor, grPa int);
500 } 517 }
501 } 518 }
502 } 519 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698