SkDiscardableMemoryPool to abstract class
[platform/upstream/libSkiaSharp.git] / gm / factory.cpp
1 /*
2  * Copyright 2012 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 "gm.h"
9 #include "SkCanvas.h"
10 #include "SkData.h"
11 #include "SkDecodingImageGenerator.h"
12 #include "SkDiscardableMemoryPool.h"
13 #include "SkDiscardablePixelRef.h"
14 #include "SkImageDecoder.h"
15 #include "SkImageGenerator.h"
16 #include "SkOSFile.h"
17 #include "SkStream.h"
18
19 namespace skiagm {
20
21 /**
22  *  Draw a PNG created by SkBitmapFactory.
23  */
24 class FactoryGM : public GM {
25 public:
26     FactoryGM() {}
27
28 protected:
29     virtual void onOnceBeforeDraw() SK_OVERRIDE {
30         // Copyright-free file from http://openclipart.org/detail/29213/paper-plane-by-ddoo
31         SkString filename = SkOSPath::SkPathJoin(INHERITED::gResourcePath.c_str(),
32                                                  "plane.png");
33         SkAutoDataUnref data(SkData::NewFromFileName(filename.c_str()));
34         if (NULL != data.get()) {
35             // Create a cache which will boot the pixels out anytime the
36             // bitmap is unlocked.
37             SkAutoTUnref<SkDiscardableMemoryPool> pool(
38                 SkDiscardableMemoryPool::Create(1));
39             SkAssertResult(SkInstallDiscardablePixelRef(
40                 SkDecodingImageGenerator::Create(
41                     data, SkDecodingImageGenerator::Options()),
42                 &fBitmap, pool));
43         }
44     }
45
46     virtual SkString onShortName() {
47         return SkString("factory");
48     }
49
50     virtual SkISize onISize() {
51         return make_isize(640, 480);
52     }
53
54     virtual void onDraw(SkCanvas* canvas) {
55         canvas->drawBitmap(fBitmap, 0, 0);
56     }
57
58     // Skip cross process pipe due to https://code.google.com/p/skia/issues/detail?id=1520
59     virtual uint32_t onGetFlags() const {
60         return INHERITED::onGetFlags() | kSkipPipeCrossProcess_Flag;
61     }
62
63 private:
64     SkBitmap fBitmap;
65
66     typedef GM INHERITED;
67 };
68
69 //////////////////////////////////////////////////////////////////////////////
70
71 static GM* MyFactory(void*) { return new FactoryGM; }
72 static GMRegistry reg(MyFactory);
73
74 }  // namespace skiagm