RasterWindowContext_android.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include "include/core/SkSurface.h"
  8. #include "include/core/SkTypes.h"
  9. #include "tools/sk_app/RasterWindowContext.h"
  10. #include "tools/sk_app/android/WindowContextFactory_android.h"
  11. using sk_app::RasterWindowContext;
  12. using sk_app::DisplayParams;
  13. namespace {
  14. class RasterWindowContext_android : public RasterWindowContext {
  15. public:
  16. RasterWindowContext_android(ANativeWindow*, const DisplayParams& params);
  17. sk_sp<SkSurface> getBackbufferSurface() override;
  18. void swapBuffers() override;
  19. bool isValid() override { return SkToBool(fNativeWindow); }
  20. void resize(int w, int h) override;
  21. void setDisplayParams(const DisplayParams& params) override;
  22. private:
  23. void setBuffersGeometry();
  24. sk_sp<SkSurface> fBackbufferSurface = nullptr;
  25. ANativeWindow* fNativeWindow = nullptr;
  26. ANativeWindow_Buffer fBuffer;
  27. ARect fBounds;
  28. typedef RasterWindowContext INHERITED;
  29. };
  30. RasterWindowContext_android::RasterWindowContext_android(ANativeWindow* window,
  31. const DisplayParams& params)
  32. : INHERITED(params) {
  33. fNativeWindow = window;
  34. fWidth = ANativeWindow_getWidth(fNativeWindow);
  35. fHeight = ANativeWindow_getHeight(fNativeWindow);
  36. this->setBuffersGeometry();
  37. }
  38. void RasterWindowContext_android::setBuffersGeometry() {
  39. int32_t format = 0;
  40. switch(fDisplayParams.fColorType) {
  41. case kRGBA_8888_SkColorType:
  42. format = WINDOW_FORMAT_RGBA_8888;
  43. break;
  44. case kRGB_565_SkColorType:
  45. format = WINDOW_FORMAT_RGB_565;
  46. break;
  47. default:
  48. SK_ABORT("Unsupported Android color type");
  49. }
  50. ANativeWindow_setBuffersGeometry(fNativeWindow, fWidth, fHeight, format);
  51. }
  52. void RasterWindowContext_android::setDisplayParams(const DisplayParams& params) {
  53. fDisplayParams = params;
  54. this->setBuffersGeometry();
  55. }
  56. void RasterWindowContext_android::resize(int w, int h) {
  57. fWidth = w;
  58. fHeight = h;
  59. this->setBuffersGeometry();
  60. }
  61. sk_sp<SkSurface> RasterWindowContext_android::getBackbufferSurface() {
  62. if (nullptr == fBackbufferSurface) {
  63. ANativeWindow_lock(fNativeWindow, &fBuffer, &fBounds);
  64. const int bytePerPixel = fBuffer.format == WINDOW_FORMAT_RGB_565 ? 2 : 4;
  65. SkImageInfo info = SkImageInfo::Make(fWidth, fHeight,
  66. fDisplayParams.fColorType,
  67. kPremul_SkAlphaType,
  68. fDisplayParams.fColorSpace);
  69. fBackbufferSurface = SkSurface::MakeRasterDirect(
  70. info, fBuffer.bits, fBuffer.stride * bytePerPixel, nullptr);
  71. }
  72. return fBackbufferSurface;
  73. }
  74. void RasterWindowContext_android::swapBuffers() {
  75. ANativeWindow_unlockAndPost(fNativeWindow);
  76. fBackbufferSurface.reset(nullptr);
  77. }
  78. } // anonymous namespace
  79. namespace sk_app {
  80. namespace window_context_factory {
  81. WindowContext* NewRasterForAndroid(ANativeWindow* window, const DisplayParams& params) {
  82. WindowContext* ctx = new RasterWindowContext_android(window, params);
  83. if (!ctx->isValid()) {
  84. delete ctx;
  85. return nullptr;
  86. }
  87. return ctx;
  88. }
  89. }
  90. } // namespace sk_app