OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2016 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 "SkCanvas.h" | |
9 #include "SkSpecialImage.h" | |
10 #include "SkSpecialSurface.h" | |
11 | |
12 /////////////////////////////////////////////////////////////////////////////// | |
13 class SkSpecialImage_Base : public SkSpecialImage { | |
bsalomon
2016/01/13 21:39:17
Wondering why the SpecialImage_Base/SpecialImage d
robertphillips
2016/01/14 14:43:59
Mainly b.c. it was based on SkImage, which has it,
| |
14 public: | |
15 SkSpecialImage_Base(int width, int height) : INHERITED(width, height) { } | |
16 virtual ~SkSpecialImage_Base() { } | |
17 | |
18 virtual void onDraw(SkCanvas*, int x, int y, const SkPaint*) const = 0; | |
19 | |
20 virtual const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const { return nullptr; } | |
21 | |
22 virtual GrTexture* onPeekTexture() const { return nullptr; } | |
23 | |
24 virtual SkSpecialSurface* onNewSurface(const SkImageInfo& info) const { retu rn nullptr; } | |
25 | |
26 private: | |
27 typedef SkSpecialImage INHERITED; | |
28 }; | |
29 | |
30 /////////////////////////////////////////////////////////////////////////////// | |
31 static inline const SkSpecialImage_Base* as_IB(const SkSpecialImage* image) { | |
32 return static_cast<const SkSpecialImage_Base*>(image); | |
33 } | |
34 | |
35 void SkSpecialImage::draw(SkCanvas* canvas, int x, int y, const SkPaint* paint) const { | |
36 return as_IB(this)->onDraw(canvas, x, y, paint); | |
37 } | |
38 | |
39 const void* SkSpecialImage::peekPixels(SkImageInfo* info, size_t* rowBytes) cons t { | |
40 SkImageInfo infoStorage; | |
41 size_t rowBytesStorage; | |
42 if (!info) { | |
43 info = &infoStorage; | |
44 } | |
45 if (!rowBytes) { | |
46 rowBytes = &rowBytesStorage; | |
47 } | |
48 return as_IB(this)->onPeekPixels(info, rowBytes); | |
49 } | |
50 | |
51 GrTexture* SkSpecialImage::peekTexture() const { | |
52 return as_IB(this)->onPeekTexture(); | |
53 } | |
54 | |
55 SkSpecialSurface* SkSpecialImage::newSurface(const SkImageInfo& info) const { | |
56 return as_IB(this)->onNewSurface(info); | |
57 } | |
58 | |
59 /////////////////////////////////////////////////////////////////////////////// | |
60 #include "SkImage.h" | |
61 | |
62 class SkSpecialImage_Image : public SkSpecialImage_Base { | |
63 public: | |
64 SkSpecialImage_Image(int width, int height, const SkImage* image) | |
65 : INHERITED(width, height) | |
66 , fImage(SkRef(image)) { | |
67 } | |
68 | |
69 ~SkSpecialImage_Image() override { } | |
70 | |
71 void onDraw(SkCanvas* canvas, int x, int y, const SkPaint* paint) const over ride { | |
72 SkRect dst = SkRect::MakeXYWH(x, y, this->width(), this->height()); | |
73 | |
74 canvas->drawImageRect(fImage, SkIRect::MakeWH(this->width(), this->heigh t()), | |
75 dst, paint, SkCanvas::kStrict_SrcRectConstraint); | |
76 } | |
77 | |
78 const void* onPeekPixels(SkImageInfo* info, size_t* rowBytes) const override { | |
79 return fImage->peekPixels(info, rowBytes); | |
80 } | |
81 | |
82 GrTexture* onPeekTexture() const override { return fImage->getTexture(); } | |
83 | |
84 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { | |
85 return SkSpecialSurface::New(info, nullptr); | |
86 } | |
87 | |
88 private: | |
89 SkAutoTUnref<const SkImage> fImage; | |
90 | |
91 typedef SkSpecialImage_Base INHERITED; | |
92 }; | |
93 | |
94 SkSpecialImage* SkSpecialImage::New(int width, int height, const SkImage* image) { | |
95 SkASSERT(width <= image->width() && height <= image->height()); | |
96 return new SkSpecialImage_Image(width, height, image); | |
97 } | |
98 | |
99 /////////////////////////////////////////////////////////////////////////////// | |
100 #include "SkBitmap.h" | |
101 #include "SkImageInfo.h" | |
102 #include "SkPixelRef.h" | |
103 | |
104 class SkSpecialImage_Raster : public SkSpecialImage_Base { | |
105 public: | |
106 SkSpecialImage_Raster(int width, int height, const SkBitmap& bm) | |
107 : INHERITED(width, height) | |
108 , fBitmap(bm) { | |
109 if (bm.pixelRef()->isPreLocked()) { | |
110 // we only preemptively lock if there is no chance of triggering som ething expensive | |
111 // like a lazy decode or imagegenerator. PreLocked means it is flat pixels already. | |
112 fBitmap.lockPixels(); | |
113 } | |
114 } | |
115 | |
116 ~SkSpecialImage_Raster() override { } | |
117 | |
118 void onDraw(SkCanvas* canvas, int x, int y, const SkPaint* paint) const over ride { | |
119 SkRect dst = SkRect::MakeXYWH(x, y, this->width(), this->height()); | |
120 | |
121 canvas->drawBitmapRect(fBitmap, SkIRect::MakeWH(this->width(), this->hei ght()), | |
122 dst, paint, SkCanvas::kStrict_SrcRectConstraint); | |
123 } | |
124 | |
125 const void* onPeekPixels(SkImageInfo* infoPtr, size_t* rowBytes) const overr ide { | |
126 const SkImageInfo info = fBitmap.info(); | |
127 if ((kUnknown_SkColorType == info.colorType()) || !fBitmap.getPixels()) { | |
128 return nullptr; | |
129 } | |
130 *infoPtr = info; | |
131 *rowBytes = fBitmap.rowBytes(); | |
132 return fBitmap.getPixels(); | |
133 } | |
134 | |
135 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { | |
136 return SkSpecialSurface::New(info, nullptr); | |
137 } | |
138 | |
139 private: | |
140 SkBitmap fBitmap; | |
141 | |
142 typedef SkSpecialImage_Base INHERITED; | |
143 }; | |
144 | |
145 SkSpecialImage* SkSpecialImage::New(int width, int height, const SkBitmap& bm) { | |
146 SkASSERT(nullptr == bm.getTexture()); | |
147 SkASSERT(width <= bm.width() && height <= bm.height()); | |
148 return new SkSpecialImage_Raster(width, height, bm); | |
149 } | |
150 | |
151 #if SK_SUPPORT_GPU | |
152 /////////////////////////////////////////////////////////////////////////////// | |
153 #include "GrTexture.h" | |
154 #include "SkGr.h" | |
155 #include "SkGrPriv.h" | |
156 | |
157 class SkSpecialImage_Gpu : public SkSpecialImage_Base { | |
158 public: | |
159 SkSpecialImage_Gpu(int width, int height, GrTexture* tex) | |
160 : INHERITED(width, height) | |
161 , fTexture(SkRef(tex)) { | |
162 } | |
163 | |
164 ~SkSpecialImage_Gpu() override { } | |
165 | |
166 void onDraw(SkCanvas* canvas, int x, int y, const SkPaint* paint) const over ride { | |
167 SkBitmap bm; | |
168 | |
169 static const bool kUnknownOpacity = false; | |
170 GrWrapTextureInBitmap(fTexture, this->width(), this->height(), kUnknownO pacity, &bm); // :( | |
171 | |
172 canvas->drawBitmap(bm, x, y, paint); | |
173 } | |
174 | |
175 GrTexture* onPeekTexture() const override { return fTexture; } | |
176 | |
177 SkSpecialSurface* onNewSurface(const SkImageInfo& info) const override { | |
178 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(info); | |
179 desc.fFlags = kRenderTarget_GrSurfaceFlag; | |
180 | |
181 return SkSpecialSurface::New(fTexture->getContext(), desc); | |
182 } | |
183 | |
184 private: | |
185 SkAutoTUnref<GrTexture> fTexture; | |
186 | |
187 typedef SkSpecialImage_Base INHERITED; | |
188 }; | |
189 | |
190 SkSpecialImage* SkSpecialImage::New(int width, int height, GrTexture* tex) { | |
191 SkASSERT(width <= tex->width() && height <= tex->height()); | |
192 return new SkSpecialImage_Gpu(width, height, tex); | |
193 } | |
194 | |
195 #else | |
196 | |
197 SkSpecialImage* SkSpecialImage::New(int width, int height, GrTexture* tex) { | |
198 return nullptr; | |
199 } | |
200 | |
201 #endif | |
OLD | NEW |