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 "SkImageGeneratorUtils.h"
9 #include "SkBitmap.h"
10
11 class GeneratorFromEmpty : public SkImageGenerator {
12 public:
GeneratorFromEmpty(const SkImageInfo & info)13 GeneratorFromEmpty(const SkImageInfo& info) : SkImageGenerator(info) {}
14 };
15
NewEmpty(const SkImageInfo & info)16 SkImageGenerator* SkImageGeneratorUtils::NewEmpty(const SkImageInfo& info) {
17 return new GeneratorFromEmpty(info);
18 }
19
20 ///////////////////////////////////////////////////////////////////////////////////////////////////
21
22 class GeneratorFromBitmap : public SkImageGenerator {
23 public:
GeneratorFromBitmap(const SkBitmap & bm)24 GeneratorFromBitmap(const SkBitmap& bm) : SkImageGenerator(bm.info()), fBM(bm) {
25 if (!bm.isImmutable()) {
26 bm.copyTo(&fBM);
27 }
28 }
29
30 protected:
onGetPixels(const SkImageInfo & info,void * pixels,size_t rowBytes,SkPMColor *,int *)31 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
32 SkPMColor*, int*) override {
33 return fBM.readPixels(info, pixels, rowBytes, 0, 0);
34 }
35
36 private:
37 SkBitmap fBM;
38 };
39
NewFromBitmap(const SkBitmap & bm)40 SkImageGenerator* SkImageGeneratorUtils::NewFromBitmap(const SkBitmap& bm) {
41 return new GeneratorFromBitmap(bm);
42 }
43
44 ///////////////////////////////////////////////////////////////////////////////////////////////////
45
46 #if SK_SUPPORT_GPU
47
48 #include "GrContext.h"
49 #include "GrTexture.h"
50 #include "SkGrPriv.h"
51
52 class GeneratorFromTexture : public SkImageGenerator {
53 public:
GeneratorFromTexture(GrContext * ctx,GrTexture * tex,const SkImageInfo & info)54 GeneratorFromTexture(GrContext* ctx, GrTexture* tex, const SkImageInfo& info)
55 : SkImageGenerator(info), fCtx(ctx), fTexture(tex)
56 {}
57
58 protected:
onGenerateTexture(GrContext * ctx,const SkIRect * subset)59 GrTexture* onGenerateTexture(GrContext* ctx, const SkIRect* subset) override {
60 if (ctx) {
61 SkASSERT(ctx == fCtx.get());
62 }
63
64 if (!subset) {
65 return SkRef(fTexture.get());
66 }
67 // need to copy the subset into a new texture
68 GrSurfaceDesc desc = fTexture->desc();
69 desc.fWidth = subset->width();
70 desc.fHeight = subset->height();
71
72 GrTexture* dst = fCtx->textureProvider()->createTexture(desc, SkBudgeted::kNo);
73 fCtx->copySurface(dst, fTexture, *subset, SkIPoint::Make(0, 0));
74 return dst;
75 }
76 private:
77 SkAutoTUnref<GrContext> fCtx;
78 SkAutoTUnref<GrTexture> fTexture;
79 };
80 #endif
81
NewFromTexture(GrContext * ctx,GrTexture * tex)82 SkImageGenerator* SkImageGeneratorUtils::NewFromTexture(GrContext* ctx, GrTexture* tex) {
83 #if SK_SUPPORT_GPU
84 if (ctx && tex) {
85 const GrSurfaceDesc desc = tex->desc();
86
87 SkColorType ct;
88 SkColorProfileType cpt;
89 if (!GrPixelConfig2ColorAndProfileType(desc.fConfig, &ct, &cpt)) {
90 return nullptr;
91 }
92 const SkAlphaType at = kPremul_SkAlphaType; // take isOpaque from caller?
93 SkImageInfo info = SkImageInfo::Make(desc.fWidth, desc.fHeight, ct, at, cpt);
94 return new GeneratorFromTexture(ctx, tex, info);
95 }
96 #endif
97 return nullptr;
98 }
99
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101
102 #include "SkImage.h"
103
104 class GeneratorFromImage : public SkImageGenerator {
105 public:
GeneratorFromImage(const SkImage * image,const SkImageInfo & info)106 GeneratorFromImage(const SkImage* image, const SkImageInfo& info)
107 : SkImageGenerator(info), fImage(image) {}
108
109 protected:
onGetPixels(const SkImageInfo & info,void * pixels,size_t rowBytes,SkPMColor *,int *)110 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
111 SkPMColor*, int*) override {
112 return fImage->readPixels(info, pixels, rowBytes, 0, 0);
113 }
114
onGenerateTexture(GrContext * ctx,const SkIRect * subset)115 GrTexture* onGenerateTexture(GrContext* ctx, const SkIRect* subset) override {
116 // waiting on https://code.google.com/p/skia/issues/detail?id=4233
117 return nullptr;
118 }
119
120 private:
121 SkAutoTUnref<const SkImage> fImage;
122 };
123
NewFromImage(const SkImage * image)124 SkImageGenerator* SkImageGeneratorUtils::NewFromImage(const SkImage* image) {
125 if (image) {
126 const SkColorType ct = kN32_SkColorType;
127 const SkAlphaType at = image->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
128 const SkImageInfo info = SkImageInfo::Make(image->width(), image->height(), ct, at);
129 return new GeneratorFromImage(image, info);
130 }
131 return nullptr;
132 }
133
134
135