2016-06-07 21:22:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "tools/sk_app/RasterWindowContext.h"
|
|
|
|
#include "tools/sk_app/android/WindowContextFactory_android.h"
|
2016-06-07 21:22:37 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
using sk_app::RasterWindowContext;
|
|
|
|
using sk_app::DisplayParams;
|
2016-06-07 21:22:37 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
namespace {
|
|
|
|
class RasterWindowContext_android : public RasterWindowContext {
|
|
|
|
public:
|
|
|
|
RasterWindowContext_android(ANativeWindow*, const DisplayParams& params);
|
2016-06-07 21:22:37 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
sk_sp<SkSurface> getBackbufferSurface() override;
|
|
|
|
void swapBuffers() override;
|
|
|
|
|
|
|
|
bool isValid() override { return SkToBool(fNativeWindow); }
|
2016-07-27 15:50:12 +00:00
|
|
|
void resize(int w, int h) override;
|
2016-07-26 19:02:50 +00:00
|
|
|
void setDisplayParams(const DisplayParams& params) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void setBuffersGeometry();
|
|
|
|
sk_sp<SkSurface> fBackbufferSurface = nullptr;
|
|
|
|
ANativeWindow* fNativeWindow = nullptr;
|
|
|
|
ANativeWindow_Buffer fBuffer;
|
|
|
|
ARect fBounds;
|
2017-05-02 20:15:53 +00:00
|
|
|
|
|
|
|
typedef RasterWindowContext INHERITED;
|
2016-07-26 19:02:50 +00:00
|
|
|
};
|
2016-06-07 21:22:37 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
RasterWindowContext_android::RasterWindowContext_android(ANativeWindow* window,
|
2017-05-02 20:15:53 +00:00
|
|
|
const DisplayParams& params)
|
|
|
|
: INHERITED(params) {
|
2016-07-26 19:02:50 +00:00
|
|
|
fNativeWindow = window;
|
2016-06-07 21:22:37 +00:00
|
|
|
fWidth = ANativeWindow_getWidth(fNativeWindow);
|
|
|
|
fHeight = ANativeWindow_getHeight(fNativeWindow);
|
2016-06-09 19:02:34 +00:00
|
|
|
this->setBuffersGeometry();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RasterWindowContext_android::setBuffersGeometry() {
|
2016-06-13 19:26:45 +00:00
|
|
|
int32_t format = 0;
|
2016-06-09 19:02:34 +00:00
|
|
|
switch(fDisplayParams.fColorType) {
|
2016-06-07 21:22:37 +00:00
|
|
|
case kRGBA_8888_SkColorType:
|
|
|
|
format = WINDOW_FORMAT_RGBA_8888;
|
|
|
|
break;
|
|
|
|
case kRGB_565_SkColorType:
|
|
|
|
format = WINDOW_FORMAT_RGB_565;
|
|
|
|
break;
|
|
|
|
default:
|
2016-06-13 19:26:45 +00:00
|
|
|
SK_ABORT("Unsupported Android color type");
|
2016-06-07 21:22:37 +00:00
|
|
|
}
|
|
|
|
ANativeWindow_setBuffersGeometry(fNativeWindow, fWidth, fHeight, format);
|
|
|
|
}
|
|
|
|
|
2016-06-09 19:02:34 +00:00
|
|
|
void RasterWindowContext_android::setDisplayParams(const DisplayParams& params) {
|
|
|
|
fDisplayParams = params;
|
|
|
|
this->setBuffersGeometry();
|
|
|
|
}
|
|
|
|
|
2016-07-27 15:50:12 +00:00
|
|
|
void RasterWindowContext_android::resize(int w, int h) {
|
2016-06-09 19:02:34 +00:00
|
|
|
fWidth = w;
|
|
|
|
fHeight = h;
|
|
|
|
this->setBuffersGeometry();
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:22:37 +00:00
|
|
|
sk_sp<SkSurface> RasterWindowContext_android::getBackbufferSurface() {
|
|
|
|
if (nullptr == fBackbufferSurface) {
|
|
|
|
ANativeWindow_lock(fNativeWindow, &fBuffer, &fBounds);
|
|
|
|
const int bytePerPixel = fBuffer.format == WINDOW_FORMAT_RGB_565 ? 2 : 4;
|
|
|
|
SkImageInfo info = SkImageInfo::Make(fWidth, fHeight,
|
|
|
|
fDisplayParams.fColorType,
|
2016-06-16 21:10:34 +00:00
|
|
|
kPremul_SkAlphaType,
|
2016-06-16 20:03:24 +00:00
|
|
|
fDisplayParams.fColorSpace);
|
2016-06-07 21:22:37 +00:00
|
|
|
fBackbufferSurface = SkSurface::MakeRasterDirect(
|
|
|
|
info, fBuffer.bits, fBuffer.stride * bytePerPixel, nullptr);
|
|
|
|
}
|
|
|
|
return fBackbufferSurface;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RasterWindowContext_android::swapBuffers() {
|
|
|
|
ANativeWindow_unlockAndPost(fNativeWindow);
|
|
|
|
fBackbufferSurface.reset(nullptr);
|
|
|
|
}
|
2016-07-26 19:02:50 +00:00
|
|
|
} // anonymous namespace
|
2016-06-07 21:22:37 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
namespace sk_app {
|
|
|
|
namespace window_context_factory {
|
|
|
|
|
2019-08-01 20:21:49 +00:00
|
|
|
std::unique_ptr<WindowContext> MakeRasterForAndroid(ANativeWindow* window,
|
|
|
|
const DisplayParams& params) {
|
|
|
|
std::unique_ptr<WindowContext> ctx(new RasterWindowContext_android(window, params));
|
2016-07-26 19:02:50 +00:00
|
|
|
if (!ctx->isValid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-06-07 21:22:37 +00:00
|
|
|
} // namespace sk_app
|