1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
|
/*
* Copyright 2010 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkGr.h"
#include "GrCaps.h"
#include "GrDrawContext.h"
#include "GrXferProcessor.h"
#include "GrYUVProvider.h"
#include "SkColorFilter.h"
#include "SkConfig8888.h"
#include "SkCanvas.h"
#include "SkData.h"
#include "SkErrorInternals.h"
#include "SkGrPixelRef.h"
#include "SkMessageBus.h"
#include "SkPixelRef.h"
#include "SkResourceCache.h"
#include "SkTextureCompressor.h"
#include "SkYUVPlanesCache.h"
#include "effects/GrBicubicEffect.h"
#include "effects/GrDitherEffect.h"
#include "effects/GrPorterDuffXferProcessor.h"
#include "effects/GrYUVtoRGBEffect.h"
#ifndef SK_IGNORE_ETC1_SUPPORT
# include "ktx.h"
# include "etc1.h"
#endif
/* Fill out buffer with the compressed format Ganesh expects from a colortable
based bitmap. [palette (colortable) + indices].
At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
we could detect that the colortable.count is <= 16, and then repack the
indices as nibbles to save RAM, but it would take more time (i.e. a lot
slower than memcpy), so skipping that for now.
Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
as the colortable.count says it is.
*/
static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
SkAutoLockPixels alp(bitmap);
if (!bitmap.readyToDraw()) {
SkDEBUGFAIL("bitmap not ready to draw!");
return;
}
SkColorTable* ctable = bitmap.getColorTable();
char* dst = (char*)buffer;
const int count = ctable->count();
SkDstPixelInfo dstPI;
dstPI.fColorType = kRGBA_8888_SkColorType;
dstPI.fAlphaType = kPremul_SkAlphaType;
dstPI.fPixels = buffer;
dstPI.fRowBytes = count * sizeof(SkPMColor);
SkSrcPixelInfo srcPI;
srcPI.fColorType = kN32_SkColorType;
srcPI.fAlphaType = kPremul_SkAlphaType;
srcPI.fPixels = ctable->readColors();
srcPI.fRowBytes = count * sizeof(SkPMColor);
srcPI.convertPixelsTo(&dstPI, count, 1);
// always skip a full 256 number of entries, even if we memcpy'd fewer
dst += 256 * sizeof(GrColor);
if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
memcpy(dst, bitmap.getPixels(), bitmap.getSize());
} else {
// need to trim off the extra bytes per row
size_t width = bitmap.width();
size_t rowBytes = bitmap.rowBytes();
const char* src = (const char*)bitmap.getPixels();
for (int y = 0; y < bitmap.height(); y++) {
memcpy(dst, src, width);
src += rowBytes;
dst += width;
}
}
}
////////////////////////////////////////////////////////////////////////////////
struct Stretch {
enum Type {
kNone_Type,
kBilerp_Type,
kNearest_Type
} fType;
int fWidth;
int fHeight;
};
static void get_stretch(const GrContext* ctx, int width, int height,
const GrTextureParams* params, Stretch* stretch) {
stretch->fType = Stretch::kNone_Type;
bool doStretch = false;
if (params && params->isTiled() && !ctx->caps()->npotTextureTileSupport() &&
(!SkIsPow2(width) || !SkIsPow2(height))) {
doStretch = true;
stretch->fWidth = GrNextPow2(SkTMax(width, ctx->caps()->minTextureSize()));
stretch->fHeight = GrNextPow2(SkTMax(height, ctx->caps()->minTextureSize()));
} else if (width < ctx->caps()->minTextureSize() || height < ctx->caps()->minTextureSize()) {
// The small texture issues appear to be with tiling. Hence it seems ok to scale them
// up using the GPU. If issues persist we may need to CPU-stretch.
doStretch = true;
stretch->fWidth = SkTMax(width, ctx->caps()->minTextureSize());
stretch->fHeight = SkTMax(height, ctx->caps()->minTextureSize());
}
if (doStretch) {
if (params) {
switch(params->filterMode()) {
case GrTextureParams::kNone_FilterMode:
stretch->fType = Stretch::kNearest_Type;
break;
case GrTextureParams::kBilerp_FilterMode:
case GrTextureParams::kMipMap_FilterMode:
stretch->fType = Stretch::kBilerp_Type;
break;
}
} else {
stretch->fType = Stretch::kBilerp_Type;
}
} else {
stretch->fWidth = -1;
stretch->fHeight = -1;
stretch->fType = Stretch::kNone_Type;
}
}
static bool make_stretched_key(const GrUniqueKey& origKey, const Stretch& stretch,
GrUniqueKey* stretchedKey) {
if (origKey.isValid() && Stretch::kNone_Type != stretch.fType) {
uint32_t width = SkToU16(stretch.fWidth);
uint32_t height = SkToU16(stretch.fHeight);
static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
GrUniqueKey::Builder builder(stretchedKey, origKey, kDomain, 2);
builder[0] = stretch.fType;
builder[1] = width | (height << 16);
builder.finish();
return true;
}
SkASSERT(!stretchedKey->isValid());
return false;
}
static void make_unstretched_key(GrUniqueKey* key, uint32_t imageID,
U16CPU width, U16CPU height, SkIPoint origin) {
SkASSERT((uint16_t)width == width);
SkASSERT((uint16_t)height == height);
static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
GrUniqueKey::Builder builder(key, kDomain, 4);
builder[0] = imageID;
builder[1] = origin.fX;
builder[2] = origin.fY;
builder[3] = width | (height << 16);
}
void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID,
U16CPU width, U16CPU height, SkIPoint origin,
const GrCaps& caps, SkImageUsageType usage) {
const Stretch::Type stretches[] = {
Stretch::kNone_Type, // kUntiled_SkImageUsageType
Stretch::kNearest_Type, // kTiled_Unfiltered_SkImageUsageType
Stretch::kBilerp_Type, // kTiled_Filtered_SkImageUsageType
};
const bool isPow2 = SkIsPow2(width) && SkIsPow2(height);
const bool needToStretch = !isPow2 &&
usage != kUntiled_SkImageUsageType &&
!caps.npotTextureTileSupport();
if (needToStretch) {
GrUniqueKey tmpKey;
make_unstretched_key(&tmpKey, imageID, width, height, origin);
Stretch stretch;
stretch.fType = stretches[usage];
stretch.fWidth = SkNextPow2(width);
stretch.fHeight = SkNextPow2(height);
if (!make_stretched_key(tmpKey, stretch, key)) {
goto UNSTRETCHED;
}
} else {
UNSTRETCHED:
make_unstretched_key(key, imageID, width, height, origin);
}
}
static void make_unstretched_key(const SkBitmap& bitmap, GrUniqueKey* key) {
make_unstretched_key(key, bitmap.getGenerationID(), bitmap.width(), bitmap.height(),
bitmap.pixelRefOrigin());
}
static void make_bitmap_keys(const SkBitmap& bitmap,
const Stretch& stretch,
GrUniqueKey* key,
GrUniqueKey* stretchedKey) {
make_unstretched_key(bitmap, key);
if (Stretch::kNone_Type != stretch.fType) {
make_stretched_key(*key, stretch, stretchedKey);
}
}
static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrSurfaceDesc* desc) {
desc->fFlags = kNone_GrSurfaceFlags;
desc->fWidth = bitmap.width();
desc->fHeight = bitmap.height();
desc->fConfig = SkImageInfo2GrPixelConfig(bitmap.info());
desc->fSampleCnt = 0;
}
namespace {
// When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key.
class BitmapInvalidator : public SkPixelRef::GenIDChangeListener {
public:
explicit BitmapInvalidator(const GrUniqueKey& key) : fMsg(key) {}
private:
GrUniqueKeyInvalidatedMessage fMsg;
void onChange() override {
SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
}
};
} // namespace
GrTexture* GrCreateTextureForPixels(GrContext* ctx,
const GrUniqueKey& optionalKey,
GrSurfaceDesc desc,
SkPixelRef* pixelRefForInvalidationNotification,
const void* pixels,
size_t rowBytes) {
GrTexture* result = ctx->textureProvider()->createTexture(desc, true, pixels, rowBytes);
if (result && optionalKey.isValid()) {
if (pixelRefForInvalidationNotification) {
BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
pixelRefForInvalidationNotification->addGenIDChangeListener(listener);
}
ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, result);
}
return result;
}
// creates a new texture that is the input texture scaled up. If optionalKey is valid it will be
// set on the new texture. stretch controls whether the scaling is done using nearest or bilerp
// filtering and the size to stretch the texture to.
GrTexture* stretch_texture(GrTexture* inputTexture, const Stretch& stretch,
SkPixelRef* pixelRef,
const GrUniqueKey& optionalKey) {
SkASSERT(Stretch::kNone_Type != stretch.fType);
GrContext* context = inputTexture->getContext();
SkASSERT(context);
const GrCaps* caps = context->caps();
// Either it's a cache miss or the original wasn't cached to begin with.
GrSurfaceDesc rtDesc = inputTexture->desc();
rtDesc.fFlags = rtDesc.fFlags | kRenderTarget_GrSurfaceFlag;
rtDesc.fWidth = stretch.fWidth;
rtDesc.fHeight = stretch.fHeight;
rtDesc.fConfig = GrMakePixelConfigUncompressed(rtDesc.fConfig);
// If the config isn't renderable try converting to either A8 or an 32 bit config. Otherwise,
// fail.
if (!caps->isConfigRenderable(rtDesc.fConfig, false)) {
if (GrPixelConfigIsAlphaOnly(rtDesc.fConfig)) {
if (caps->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
rtDesc.fConfig = kAlpha_8_GrPixelConfig;
} else if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
rtDesc.fConfig = kSkia8888_GrPixelConfig;
} else {
return nullptr;
}
} else if (kRGB_GrColorComponentFlags ==
(kRGB_GrColorComponentFlags & GrPixelConfigComponentMask(rtDesc.fConfig))) {
if (caps->isConfigRenderable(kSkia8888_GrPixelConfig, false)) {
rtDesc.fConfig = kSkia8888_GrPixelConfig;
} else {
return nullptr;
}
} else {
return nullptr;
}
}
SkAutoTUnref<GrTexture> stretched(GrCreateTextureForPixels(context, optionalKey, rtDesc,
pixelRef, nullptr,0));
if (!stretched) {
return nullptr;
}
GrPaint paint;
// If filtering is not desired then we want to ensure all texels in the resampled image are
// copies of texels from the original.
GrTextureParams params(SkShader::kClamp_TileMode,
Stretch::kBilerp_Type == stretch.fType ?
GrTextureParams::kBilerp_FilterMode :
GrTextureParams::kNone_FilterMode);
paint.addColorTextureProcessor(inputTexture, SkMatrix::I(), params);
SkRect rect = SkRect::MakeWH(SkIntToScalar(rtDesc.fWidth), SkIntToScalar(rtDesc.fHeight));
SkRect localRect = SkRect::MakeWH(1.f, 1.f);
SkAutoTUnref<GrDrawContext> drawContext(context->drawContext());
if (!drawContext) {
return nullptr;
}
drawContext->drawNonAARectToRect(stretched->asRenderTarget(), GrClip::WideOpen(), paint,
SkMatrix::I(), rect, localRect);
return stretched.detach();
}
GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
int expectedW, int expectedH,
const void** outStartOfDataToUpload) {
*outStartOfDataToUpload = nullptr;
#ifndef SK_IGNORE_ETC1_SUPPORT
if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) {
return kUnknown_GrPixelConfig;
}
const uint8_t* bytes = data->bytes();
if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) {
// Does the data match the dimensions of the bitmap? If not,
// then we don't know how to scale the image to match it...
if (etc1_pkm_get_width(bytes) != (unsigned)expectedW ||
etc1_pkm_get_height(bytes) != (unsigned)expectedH)
{
return kUnknown_GrPixelConfig;
}
*outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
return kETC1_GrPixelConfig;
} else if (SkKTXFile::is_ktx(bytes)) {
SkKTXFile ktx(data);
// Is it actually an ETC1 texture?
if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
return kUnknown_GrPixelConfig;
}
// Does the data match the dimensions of the bitmap? If not,
// then we don't know how to scale the image to match it...
if (ktx.width() != expectedW || ktx.height() != expectedH) {
return kUnknown_GrPixelConfig;
}
*outStartOfDataToUpload = ktx.pixelData();
return kETC1_GrPixelConfig;
}
#endif
return kUnknown_GrPixelConfig;
}
static GrTexture* load_etc1_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
const SkBitmap &bm, GrSurfaceDesc desc) {
SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
if (!data) {
return nullptr;
}
const void* startOfTexData;
desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(),
&startOfTexData);
if (kUnknown_GrPixelConfig == desc.fConfig) {
return nullptr;
}
return GrCreateTextureForPixels(ctx, optionalKey, desc, bm.pixelRef(), startOfTexData, 0);
}
/*
* Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
* be gone from SkPixelRef, and we can remove this subclass entirely.
*/
class PixelRef_GrYUVProvider : public GrYUVProvider {
SkPixelRef* fPR;
public:
PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
uint32_t onGetID() override { return fPR->getGenerationID(); }
bool onGetYUVSizes(SkISize sizes[3]) override {
return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
}
bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
SkYUVColorSpace* space) override {
return fPR->getYUV8Planes(sizes, planes, rowBytes, space);
}
};
static GrTexture* load_yuv_texture(GrContext* ctx, const GrUniqueKey& optionalKey,
const SkBitmap& bm, const GrSurfaceDesc& desc) {
// Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
SkPixelRef* pixelRef = bm.pixelRef();
if ((nullptr == pixelRef) ||
(pixelRef->info().width() != bm.info().width()) ||
(pixelRef->info().height() != bm.info().height())) {
return nullptr;
}
const bool useCache = optionalKey.isValid();
PixelRef_GrYUVProvider provider(pixelRef);
GrTexture* texture = provider.refAsTexture(ctx, desc, useCache);
if (!texture) {
return nullptr;
}
if (useCache) {
BitmapInvalidator* listener = new BitmapInvalidator(optionalKey);
pixelRef->addGenIDChangeListener(listener);
ctx->textureProvider()->assignUniqueKeyToTexture(optionalKey, texture);
}
return texture;
}
static GrTexture* create_unstretched_bitmap_texture(GrContext* ctx,
const SkBitmap& origBitmap,
const GrUniqueKey& optionalKey) {
if (origBitmap.width() < ctx->caps()->minTextureSize() ||
origBitmap.height() < ctx->caps()->minTextureSize()) {
return nullptr;
}
SkBitmap tmpBitmap;
const SkBitmap* bitmap = &origBitmap;
GrSurfaceDesc desc;
generate_bitmap_texture_desc(*bitmap, &desc);
const GrCaps* caps = ctx->caps();
if (kIndex_8_SkColorType == bitmap->colorType()) {
if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
bitmap->width(), bitmap->height());
SkAutoMalloc storage(imageSize);
build_index8_data(storage.get(), origBitmap);
// our compressed data will be trimmed, so pass width() for its
// "rowBytes", since they are the same now.
return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
storage.get(), bitmap->width());
} else {
origBitmap.copyTo(&tmpBitmap, kN32_SkColorType);
// now bitmap points to our temp, which has been promoted to 32bits
bitmap = &tmpBitmap;
desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
}
} else if (!bitmap->readyToDraw()) {
// If the bitmap had compressed data and was then uncompressed, it'll still return
// compressed data on 'refEncodedData' and upload it. Probably not good, since if
// the bitmap has available pixels, then they might not be what the decompressed
// data is.
GrTexture *texture = load_etc1_texture(ctx, optionalKey, *bitmap, desc);
if (texture) {
return texture;
}
}
GrTexture *texture = load_yuv_texture(ctx, optionalKey, *bitmap, desc);
if (texture) {
return texture;
}
SkAutoLockPixels alp(*bitmap);
if (!bitmap->readyToDraw()) {
return nullptr;
}
return GrCreateTextureForPixels(ctx, optionalKey, desc, origBitmap.pixelRef(),
bitmap->getPixels(), bitmap->rowBytes());
}
static SkBitmap stretch_on_cpu(const SkBitmap& bmp, const Stretch& stretch) {
SkBitmap stretched;
stretched.allocN32Pixels(stretch.fWidth, stretch.fHeight);
SkCanvas canvas(stretched);
SkPaint paint;
switch (stretch.fType) {
case Stretch::kNearest_Type:
paint.setFilterQuality(kNone_SkFilterQuality);
break;
case Stretch::kBilerp_Type:
paint.setFilterQuality(kLow_SkFilterQuality);
break;
case Stretch::kNone_Type:
SkDEBUGFAIL("Shouldn't get here.");
break;
}
SkRect dstRect = SkRect::MakeWH(SkIntToScalar(stretch.fWidth), SkIntToScalar(stretch.fHeight));
canvas.drawBitmapRect(bmp, dstRect, &paint);
return stretched;
}
static GrTexture* create_bitmap_texture(GrContext* ctx,
const SkBitmap& bmp,
const Stretch& stretch,
const GrUniqueKey& unstretchedKey,
const GrUniqueKey& stretchedKey) {
if (Stretch::kNone_Type != stretch.fType) {
SkAutoTUnref<GrTexture> unstretched;
// Check if we have the unstretched version in the cache, if not create it.
if (unstretchedKey.isValid()) {
unstretched.reset(ctx->textureProvider()->findAndRefTextureByUniqueKey(unstretchedKey));
}
if (!unstretched) {
unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey));
if (!unstretched) {
// We might not have been able to create a unstrecthed texture because it is smaller
// than the min texture size. In that case do cpu stretching.
SkBitmap stretchedBmp = stretch_on_cpu(bmp, stretch);
return create_unstretched_bitmap_texture(ctx, stretchedBmp, stretchedKey);
}
}
return stretch_texture(unstretched, stretch, bmp.pixelRef(), stretchedKey);
}
return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey);
}
bool GrIsBitmapInCache(const GrContext* ctx,
const SkBitmap& bitmap,
const GrTextureParams* params) {
Stretch stretch;
get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch);
// Handle the case where the bitmap is explicitly texture backed.
GrTexture* texture = bitmap.getTexture();
if (texture) {
if (Stretch::kNone_Type == stretch.fType) {
return true;
}
// No keys for volatile bitmaps.
if (bitmap.isVolatile()) {
return false;
}
const GrUniqueKey& key = texture->getUniqueKey();
if (!key.isValid()) {
return false;
}
GrUniqueKey stretchedKey;
make_stretched_key(key, stretch, &stretchedKey);
return ctx->textureProvider()->existsTextureWithUniqueKey(stretchedKey);
}
// We don't cache volatile bitmaps
if (bitmap.isVolatile()) {
return false;
}
GrUniqueKey key, stretchedKey;
make_bitmap_keys(bitmap, stretch, &key, &stretchedKey);
return ctx->textureProvider()->existsTextureWithUniqueKey(
(Stretch::kNone_Type == stretch.fType) ? key : stretchedKey);
}
GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
const SkBitmap& bitmap,
const GrTextureParams* params) {
Stretch stretch;
get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch);
GrTexture* result = bitmap.getTexture();
if (result) {
if (Stretch::kNone_Type == stretch.fType) {
return SkRef(result);
}
GrUniqueKey stretchedKey;
// Don't create a key for the resized version if the bmp is volatile.
if (!bitmap.isVolatile()) {
const GrUniqueKey& key = result->getUniqueKey();
if (key.isValid()) {
make_stretched_key(key, stretch, &stretchedKey);
GrTexture* stretched =
ctx->textureProvider()->findAndRefTextureByUniqueKey(stretchedKey);
if (stretched) {
return stretched;
}
}
}
return stretch_texture(result, stretch, bitmap.pixelRef(), stretchedKey);
}
GrUniqueKey key, resizedKey;
if (!bitmap.isVolatile()) {
// If the bitmap isn't changing try to find a cached copy first.
make_bitmap_keys(bitmap, stretch, &key, &resizedKey);
result = ctx->textureProvider()->findAndRefTextureByUniqueKey(
resizedKey.isValid() ? resizedKey : key);
if (result) {
return result;
}
}
result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey);
if (result) {
return result;
}
SkErrorInternals::SetError( kInternalError_SkError,
"---- failed to create texture for cache [%d %d]\n",
bitmap.width(), bitmap.height());
return nullptr;
}
// TODO: make this be the canonical signature, and turn the version that takes GrTextureParams*
// into a wrapper that contains the inverse of these tables.
GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
const SkBitmap& bitmap,
SkImageUsageType usage) {
// Just need a params that will trigger the correct cache key / etc, since the usage doesn't
// tell us the specifics about filter level or specific tiling.
const SkShader::TileMode tiles[] = {
SkShader::kClamp_TileMode, // kUntiled_SkImageUsageType
SkShader::kRepeat_TileMode, // kTiled_Unfiltered_SkImageUsageType
SkShader::kRepeat_TileMode, // kTiled_Filtered_SkImageUsageType
};
const GrTextureParams::FilterMode filters[] = {
GrTextureParams::kNone_FilterMode, // kUntiled_SkImageUsageType
GrTextureParams::kNone_FilterMode, // kTiled_Unfiltered_SkImageUsageType
GrTextureParams::kBilerp_FilterMode, // kTiled_Filtered_SkImageUsageType
};
GrTextureParams params(tiles[usage], filters[usage]);
return GrRefCachedBitmapTexture(ctx, bitmap, ¶ms);
}
///////////////////////////////////////////////////////////////////////////////
// alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
// alpha info, that will be considered.
GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
switch (ct) {
case kUnknown_SkColorType:
return kUnknown_GrPixelConfig;
case kAlpha_8_SkColorType:
return kAlpha_8_GrPixelConfig;
case kRGB_565_SkColorType:
return kRGB_565_GrPixelConfig;
case kARGB_4444_SkColorType:
return kRGBA_4444_GrPixelConfig;
case kRGBA_8888_SkColorType:
//if (kSRGB_SkColorProfileType == pt) {
// return kSRGBA_8888_GrPixelConfig;
//}
return kRGBA_8888_GrPixelConfig;
case kBGRA_8888_SkColorType:
return kBGRA_8888_GrPixelConfig;
case kIndex_8_SkColorType:
return kIndex_8_GrPixelConfig;
case kGray_8_SkColorType:
return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
}
SkASSERT(0); // shouldn't get here
return kUnknown_GrPixelConfig;
}
bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
SkColorProfileType* ptOut) {
SkColorType ct;
SkColorProfileType pt = kLinear_SkColorProfileType;
switch (config) {
case kAlpha_8_GrPixelConfig:
ct = kAlpha_8_SkColorType;
break;
case kIndex_8_GrPixelConfig:
ct = kIndex_8_SkColorType;
break;
case kRGB_565_GrPixelConfig:
ct = kRGB_565_SkColorType;
break;
case kRGBA_4444_GrPixelConfig:
ct = kARGB_4444_SkColorType;
break;
case kRGBA_8888_GrPixelConfig:
ct = kRGBA_8888_SkColorType;
break;
case kBGRA_8888_GrPixelConfig:
ct = kBGRA_8888_SkColorType;
break;
case kSRGBA_8888_GrPixelConfig:
ct = kRGBA_8888_SkColorType;
pt = kSRGB_SkColorProfileType;
break;
default:
return false;
}
if (ctOut) {
*ctOut = ct;
}
if (ptOut) {
*ptOut = pt;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
bool SkPaint2GrPaintNoShader(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
GrColor paintColor, bool constantColor, GrPaint* grPaint) {
grPaint->setDither(skPaint.isDither());
grPaint->setAntiAlias(skPaint.isAntiAlias());
SkXfermode* mode = skPaint.getXfermode();
GrXPFactory* xpFactory = nullptr;
if (!SkXfermode::AsXPFactory(mode, &xpFactory)) {
// Fall back to src-over
// return false here?
xpFactory = GrPorterDuffXPFactory::Create(SkXfermode::kSrcOver_Mode);
}
SkASSERT(xpFactory);
grPaint->setXPFactory(xpFactory)->unref();
//set the color of the paint to the one of the parameter
grPaint->setColor(paintColor);
SkColorFilter* colorFilter = skPaint.getColorFilter();
if (colorFilter) {
// if the source color is a constant then apply the filter here once rather than per pixel
// in a shader.
if (constantColor) {
SkColor filtered = colorFilter->filterColor(skPaint.getColor());
grPaint->setColor(SkColor2GrColor(filtered));
} else {
SkTDArray<GrFragmentProcessor*> array;
// return false if failed?
if (colorFilter->asFragmentProcessors(context, grPaint->getProcessorDataManager(),
&array)) {
for (int i = 0; i < array.count(); ++i) {
grPaint->addColorFragmentProcessor(array[i]);
array[i]->unref();
}
}
}
}
#ifndef SK_IGNORE_GPU_DITHER
// If the dither flag is set, then we need to see if the underlying context
// supports it. If not, then install a dither effect.
if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
// What are we rendering into?
SkASSERT(rt);
// Suspect the dithering flag has no effect on these configs, otherwise
// fall back on setting the appropriate state.
if (GrPixelConfigIs8888(rt->config()) ||
GrPixelConfigIs8888(rt->config())) {
// The dither flag is set and the target is likely
// not going to be dithered by the GPU.
SkAutoTUnref<GrFragmentProcessor> fp(GrDitherEffect::Create());
if (fp.get()) {
grPaint->addColorFragmentProcessor(fp);
grPaint->setDither(false);
}
}
}
#endif
return true;
}
bool SkPaint2GrPaint(GrContext* context, GrRenderTarget* rt, const SkPaint& skPaint,
const SkMatrix& viewM, bool constantColor, GrPaint* grPaint) {
SkShader* shader = skPaint.getShader();
if (nullptr == shader) {
return SkPaint2GrPaintNoShader(context, rt, skPaint, SkColor2GrColor(skPaint.getColor()),
constantColor, grPaint);
}
GrColor paintColor = SkColor2GrColor(skPaint.getColor());
const GrFragmentProcessor* fp = shader->asFragmentProcessor(context, viewM, NULL,
skPaint.getFilterQuality(), grPaint->getProcessorDataManager());
if (!fp) {
return false;
}
grPaint->addColorFragmentProcessor(fp)->unref();
constantColor = false;
// The grcolor is automatically set when calling asFragmentProcessor.
// If the shader can be seen as an effect it returns true and adds its effect to the grpaint.
return SkPaint2GrPaintNoShader(context, rt, skPaint, paintColor, constantColor, grPaint);
}
SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
#ifdef SK_DEBUG
const GrSurfaceDesc& desc = tex->desc();
SkASSERT(w <= desc.fWidth);
SkASSERT(h <= desc.fHeight);
#endif
const GrPixelConfig config = tex->config();
SkColorType ct;
SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) {
ct = kUnknown_SkColorType;
}
return SkImageInfo::Make(w, h, ct, at);
}
void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
dst->setInfo(info);
dst->setPixelRef(new SkGrPixelRef(info, src))->unref();
}
GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
const SkMatrix& viewM,
const SkMatrix& localM,
bool* doBicubic) {
*doBicubic = false;
GrTextureParams::FilterMode textureFilterMode;
switch (paintFilterQuality) {
case kNone_SkFilterQuality:
textureFilterMode = GrTextureParams::kNone_FilterMode;
break;
case kLow_SkFilterQuality:
textureFilterMode = GrTextureParams::kBilerp_FilterMode;
break;
case kMedium_SkFilterQuality: {
SkMatrix matrix;
matrix.setConcat(viewM, localM);
if (matrix.getMinScale() < SK_Scalar1) {
textureFilterMode = GrTextureParams::kMipMap_FilterMode;
} else {
// Don't trigger MIP level generation unnecessarily.
textureFilterMode = GrTextureParams::kBilerp_FilterMode;
}
break;
}
case kHigh_SkFilterQuality: {
SkMatrix matrix;
matrix.setConcat(viewM, localM);
*doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
break;
}
default:
SkErrorInternals::SetError( kInvalidPaint_SkError,
"Sorry, I don't understand the filtering "
"mode you asked for. Falling back to "
"MIPMaps.");
textureFilterMode = GrTextureParams::kMipMap_FilterMode;
break;
}
return textureFilterMode;
}
|