| 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/window/DisplayParams.h" |
| 12 |
#include "tools/window/RasterWindowContext.h" |
| 13 |
#include "tools/window/android/WindowContextFactory_android.h" |
| 14 |
|
| 15 |
using skwindow::internal::RasterWindowContext; |
| 16 |
using skwindow::DisplayParams; |
| 17 |
|
| 18 |
namespace { |
| 19 |
class RasterWindowContext_android : public RasterWindowContext { |
| 20 |
public: |
| 21 |
RasterWindowContext_android(ANativeWindow*, std::unique_ptr<const DisplayParams> params); |
| 22 |
|
| 23 |
sk_sp<SkSurface> getBackbufferSurface() override; |
| 24 |
|
| 25 |
bool isValid() override { return SkToBool(fNativeWindow); } |
| 26 |
void resize(int w, int h) override; |
| 27 |
void setDisplayParams(std::unique_ptr<const DisplayParams> params) override; |
| 28 |
|
| 29 |
private: |
| 30 |
void setBuffersGeometry(); |
| 31 |
void onSwapBuffers() override; |
| 32 |
|
| 33 |
sk_sp<SkSurface> fBackbufferSurface = nullptr; |
| 34 |
ANativeWindow* fNativeWindow = nullptr; |
| 35 |
ANativeWindow_Buffer fBuffer; |
| 36 |
ARect fBounds; |
| 37 |
}; |
| 38 |
|
| 39 |
RasterWindowContext_android::RasterWindowContext_android( |
| 40 |
ANativeWindow* window, std::unique_ptr<const DisplayParams> params) |
| 41 |
: RasterWindowContext(std::move(params)) { |
| 42 |
fNativeWindow = window; |
| 43 |
fWidth = ANativeWindow_getWidth(fNativeWindow); |
| 44 |
fHeight = ANativeWindow_getHeight(fNativeWindow); |
| 45 |
this->setBuffersGeometry(); |
| 46 |
} |
| 47 |
|
| 48 |
void RasterWindowContext_android::setBuffersGeometry() { |
| 49 |
int32_t format = 0; |
| 50 |
switch (fDisplayParams->colorType()) { |
| 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 |
|
| 63 |
void RasterWindowContext_android::setDisplayParams(std::unique_ptr<const DisplayParams> params) { |
| 64 |
fDisplayParams = std::move(params); |
| 65 |
this->setBuffersGeometry(); |
| 66 |
} |
| 67 |
|
| 68 |
void RasterWindowContext_android::resize(int w, int h) { |
| 69 |
fWidth = w; |
| 70 |
fHeight = h; |
| 71 |
this->setBuffersGeometry(); |
| 72 |
} |
| 73 |
|
| 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, |
| 79 |
fHeight, |
| 80 |
fDisplayParams->colorType(), |
| 81 |
kPremul_SkAlphaType, |
| 82 |
fDisplayParams->colorSpace()); |
| 83 |
fBackbufferSurface = |
| 84 |
SkSurfaces::WrapPixels(info, fBuffer.bits, fBuffer.stride * bytePerPixel, nullptr); |
| 85 |
} |
| 86 |
return fBackbufferSurface; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
void RasterWindowContext_android::onSwapBuffers() { |
| 91 |
ANativeWindow_unlockAndPost(fNativeWindow); |
| 92 |
fBackbufferSurface.reset(nullptr); |
| 93 |
} |
| 94 |
} // anonymous namespace |
| 95 |
|
| 96 |
namespace skwindow { |
| 97 |
std::unique_ptr<WindowContext> MakeRasterForAndroid(ANativeWindow* window, |
| 98 |
std::unique_ptr<const DisplayParams> params) { |
| 99 |
std::unique_ptr<WindowContext> ctx(new RasterWindowContext_android(window, std::move(params))); |
| 100 |
if (!ctx->isValid()) { |
| 101 |
return nullptr; |
| 102 |
} |
| 103 |
return ctx; |
| 104 |
} |
| 105 |
|
| 106 |
} // namespace skwindow |