OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "gm.h" | 8 #include "gm.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkImage.h" | 10 #include "SkImage.h" |
| 11 #include "SkImageCacherator.h" |
11 #include "SkPictureRecorder.h" | 12 #include "SkPictureRecorder.h" |
| 13 #include "SkSurface.h" |
12 | 14 |
13 #if SK_SUPPORT_GPU | 15 #if SK_SUPPORT_GPU |
14 #include "GrContext.h" | 16 #include "GrContext.h" |
| 17 #include "GrTexture.h" |
| 18 #include "../src/image/SkImage_Gpu.h" |
15 #endif | 19 #endif |
16 | 20 |
17 static void draw_something(SkCanvas* canvas, const SkRect& bounds) { | 21 static void draw_something(SkCanvas* canvas, const SkRect& bounds) { |
18 SkPaint paint; | 22 SkPaint paint; |
19 paint.setAntiAlias(true); | 23 paint.setAntiAlias(true); |
20 paint.setColor(SK_ColorRED); | 24 paint.setColor(SK_ColorRED); |
21 paint.setStyle(SkPaint::kStroke_Style); | 25 paint.setStyle(SkPaint::kStroke_Style); |
22 paint.setStrokeWidth(10); | 26 paint.setStrokeWidth(10); |
23 canvas->drawRect(bounds, paint); | 27 canvas->drawRect(bounds, paint); |
24 paint.setStyle(SkPaint::kFill_Style); | 28 paint.setStyle(SkPaint::kFill_Style); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 canvas->scale(2, 2); | 91 canvas->scale(2, 2); |
88 this->drawSet(canvas); | 92 this->drawSet(canvas); |
89 canvas->restore(); | 93 canvas->restore(); |
90 } | 94 } |
91 | 95 |
92 private: | 96 private: |
93 typedef skiagm::GM INHERITED; | 97 typedef skiagm::GM INHERITED; |
94 }; | 98 }; |
95 DEF_GM( return new ImagePictGM; ) | 99 DEF_GM( return new ImagePictGM; ) |
96 | 100 |
| 101 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 102 |
| 103 |
| 104 class ImageCacheratorGM : public skiagm::GM { |
| 105 SkAutoTUnref<SkPicture> fPicture; |
| 106 SkAutoTDelete<SkImageCacherator> fCache; |
| 107 |
| 108 public: |
| 109 ImageCacheratorGM() {} |
| 110 |
| 111 protected: |
| 112 SkString onShortName() override { |
| 113 return SkString("image-cacherator"); |
| 114 } |
| 115 |
| 116 SkISize onISize() override { |
| 117 return SkISize::Make(850, 450); |
| 118 } |
| 119 |
| 120 void onOnceBeforeDraw() override { |
| 121 const SkRect bounds = SkRect::MakeXYWH(100, 100, 100, 100); |
| 122 SkPictureRecorder recorder; |
| 123 draw_something(recorder.beginRecording(bounds), bounds); |
| 124 fPicture.reset(recorder.endRecording()); |
| 125 |
| 126 // extract enough just for the oval. |
| 127 const SkISize size = SkISize::Make(100, 100); |
| 128 |
| 129 SkMatrix matrix; |
| 130 matrix.setTranslate(-100, -100); |
| 131 fCache.reset(new SkImageCacherator(SkImageGenerator::NewFromPicture(size
, |
| 132 fPic
ture, |
| 133 &mat
rix, |
| 134 null
ptr))); |
| 135 } |
| 136 |
| 137 void drawSet(SkCanvas* canvas) const { |
| 138 SkMatrix matrix = SkMatrix::MakeTrans(-100, -100); |
| 139 canvas->drawPicture(fPicture, &matrix, nullptr); |
| 140 |
| 141 { |
| 142 SkBitmap bitmap; |
| 143 fCache->lockAsBitmap(&bitmap); |
| 144 canvas->drawBitmap(bitmap, 150, 0); |
| 145 } |
| 146 #if SK_SUPPORT_GPU |
| 147 { |
| 148 SkAutoTUnref<GrTexture> texture(fCache->lockAsTexture(canvas->getGrC
ontext(), |
| 149 kUntiled_SkIm
ageUsageType)); |
| 150 if (!texture) { |
| 151 return; |
| 152 } |
| 153 // No API to draw a GrTexture directly, so we cheat and create a pri
vate image subclass |
| 154 SkAutoTUnref<SkImage> image(new SkImage_Gpu(100, 100, fCache->genera
tor()->uniqueID(), |
| 155 kPremul_SkAlphaType, tex
ture, 0, |
| 156 SkSurface::kNo_Budgeted)
); |
| 157 canvas->drawImage(image, 300, 0); |
| 158 } |
| 159 #endif |
| 160 } |
| 161 |
| 162 void onDraw(SkCanvas* canvas) override { |
| 163 canvas->translate(20, 20); |
| 164 |
| 165 this->drawSet(canvas); |
| 166 |
| 167 canvas->save(); |
| 168 canvas->translate(0, 130); |
| 169 canvas->scale(0.25f, 0.25f); |
| 170 this->drawSet(canvas); |
| 171 canvas->restore(); |
| 172 |
| 173 canvas->save(); |
| 174 canvas->translate(0, 200); |
| 175 canvas->scale(2, 2); |
| 176 this->drawSet(canvas); |
| 177 canvas->restore(); |
| 178 } |
| 179 |
| 180 private: |
| 181 typedef skiagm::GM INHERITED; |
| 182 }; |
| 183 DEF_GM( return new ImageCacheratorGM; ) |
| 184 |
| 185 |
| 186 |
OLD | NEW |