• Home
  • History
  • Annotate
  • 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 "WindowContextFactory_android.h"
10 #include "../RasterWindowContext.h"
11 #include "SkSurface.h"
12 #include "SkTypes.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 
RasterWindowContext_android(ANativeWindow * window,const DisplayParams & params)37 RasterWindowContext_android::RasterWindowContext_android(ANativeWindow* window,
38                                                          const DisplayParams& params) {
39     fDisplayParams = params;
40     fNativeWindow = window;
41     fWidth = ANativeWindow_getWidth(fNativeWindow);
42     fHeight = ANativeWindow_getHeight(fNativeWindow);
43     this->setBuffersGeometry();
44 }
45 
setBuffersGeometry()46 void RasterWindowContext_android::setBuffersGeometry() {
47     int32_t format = 0;
48     switch(fDisplayParams.fColorType) {
49         case kRGBA_8888_SkColorType:
50             format = WINDOW_FORMAT_RGBA_8888;
51             break;
52         case kRGB_565_SkColorType:
53             format = WINDOW_FORMAT_RGB_565;
54             break;
55         default:
56             SK_ABORT("Unsupported Android color type");
57     }
58     ANativeWindow_setBuffersGeometry(fNativeWindow, fWidth, fHeight, format);
59 }
60 
setDisplayParams(const DisplayParams & params)61 void RasterWindowContext_android::setDisplayParams(const DisplayParams& params) {
62     fDisplayParams = params;
63     this->setBuffersGeometry();
64 }
65 
resize(int w,int h)66 void RasterWindowContext_android::resize(int w, int h) {
67     fWidth = w;
68     fHeight = h;
69     this->setBuffersGeometry();
70 }
71 
getBackbufferSurface()72 sk_sp<SkSurface> RasterWindowContext_android::getBackbufferSurface() {
73     if (nullptr == fBackbufferSurface) {
74         ANativeWindow_lock(fNativeWindow, &fBuffer, &fBounds);
75         const int bytePerPixel = fBuffer.format == WINDOW_FORMAT_RGB_565 ? 2 : 4;
76         SkImageInfo info = SkImageInfo::Make(fWidth, fHeight,
77                                              fDisplayParams.fColorType,
78                                              kPremul_SkAlphaType,
79                                              fDisplayParams.fColorSpace);
80         fBackbufferSurface = SkSurface::MakeRasterDirect(
81                 info, fBuffer.bits, fBuffer.stride * bytePerPixel, nullptr);
82     }
83     return fBackbufferSurface;
84 }
85 
86 
swapBuffers()87 void RasterWindowContext_android::swapBuffers() {
88     ANativeWindow_unlockAndPost(fNativeWindow);
89     fBackbufferSurface.reset(nullptr);
90 }
91 }  // anonymous namespace
92 
93 namespace sk_app {
94 namespace window_context_factory {
95 
NewRasterForAndroid(ANativeWindow * window,const DisplayParams & params)96 WindowContext* NewRasterForAndroid(ANativeWindow* window, const DisplayParams& params) {
97     WindowContext* ctx = new RasterWindowContext_android(window, params);
98     if (!ctx->isValid()) {
99         delete ctx;
100         return nullptr;
101     }
102     return ctx;
103 }
104 
105 }
106 }   // namespace sk_app
107