• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2016 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 #include "include/core/SkSurface.h"
10 #include "include/core/SkTypes.h"
11 #include "tools/sk_app/RasterWindowContext.h"
12 #include "tools/sk_app/android/WindowContextFactory_android.h"
13 
14 using sk_app::RasterWindowContext;
15 using sk_app::DisplayParams;
16 
17 namespace {
18 class RasterWindowContext_android : public RasterWindowContext {
19 public:
20     RasterWindowContext_android(ANativeWindow*, const DisplayParams& params);
21 
22     sk_sp<SkSurface> getBackbufferSurface() override;
23     void swapBuffers() override;
24 
isValid()25     bool isValid() override { return SkToBool(fNativeWindow); }
26     void resize(int w, int h) override;
27     void setDisplayParams(const DisplayParams& params) override;
28 
29 private:
30     void setBuffersGeometry();
31     sk_sp<SkSurface> fBackbufferSurface = nullptr;
32     ANativeWindow* fNativeWindow = nullptr;
33     ANativeWindow_Buffer fBuffer;
34     ARect fBounds;
35 
36     using INHERITED = RasterWindowContext;
37 };
38 
RasterWindowContext_android(ANativeWindow * window,const DisplayParams & params)39 RasterWindowContext_android::RasterWindowContext_android(ANativeWindow* window,
40                                                          const DisplayParams& params)
41     : INHERITED(params) {
42     fNativeWindow = window;
43     fWidth = ANativeWindow_getWidth(fNativeWindow);
44     fHeight = ANativeWindow_getHeight(fNativeWindow);
45     this->setBuffersGeometry();
46 }
47 
setBuffersGeometry()48 void RasterWindowContext_android::setBuffersGeometry() {
49     int32_t format = 0;
50     switch(fDisplayParams.fColorType) {
51         case kRGBA_8888_SkColorType:
52             format = WINDOW_FORMAT_RGBA_8888;
53             break;
54         case kRGB_565_SkColorType:
55             format = WINDOW_FORMAT_RGB_565;
56             break;
57         default:
58             SK_ABORT("Unsupported Android color type");
59     }
60     ANativeWindow_setBuffersGeometry(fNativeWindow, fWidth, fHeight, format);
61 }
62 
setDisplayParams(const DisplayParams & params)63 void RasterWindowContext_android::setDisplayParams(const DisplayParams& params) {
64     fDisplayParams = params;
65     this->setBuffersGeometry();
66 }
67 
resize(int w,int h)68 void RasterWindowContext_android::resize(int w, int h) {
69     fWidth = w;
70     fHeight = h;
71     this->setBuffersGeometry();
72 }
73 
getBackbufferSurface()74 sk_sp<SkSurface> RasterWindowContext_android::getBackbufferSurface() {
75     if (nullptr == fBackbufferSurface) {
76         ANativeWindow_lock(fNativeWindow, &fBuffer, &fBounds);
77         const int bytePerPixel = fBuffer.format == WINDOW_FORMAT_RGB_565 ? 2 : 4;
78         SkImageInfo info = SkImageInfo::Make(fWidth, fHeight,
79                                              fDisplayParams.fColorType,
80                                              kPremul_SkAlphaType,
81                                              fDisplayParams.fColorSpace);
82         fBackbufferSurface = SkSurface::MakeRasterDirect(
83                 info, fBuffer.bits, fBuffer.stride * bytePerPixel, nullptr);
84     }
85     return fBackbufferSurface;
86 }
87 
88 
swapBuffers()89 void RasterWindowContext_android::swapBuffers() {
90     ANativeWindow_unlockAndPost(fNativeWindow);
91     fBackbufferSurface.reset(nullptr);
92 }
93 }  // anonymous namespace
94 
95 namespace sk_app {
96 namespace window_context_factory {
97 
MakeRasterForAndroid(ANativeWindow * window,const DisplayParams & params)98 std::unique_ptr<WindowContext> MakeRasterForAndroid(ANativeWindow* window,
99                                                     const DisplayParams& params) {
100     std::unique_ptr<WindowContext> ctx(new RasterWindowContext_android(window, params));
101     if (!ctx->isValid()) {
102         return nullptr;
103     }
104     return ctx;
105 }
106 
107 }
108 }   // namespace sk_app
109